Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.

Slides:



Advertisements
Similar presentations
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Advertisements

Lecture 4: Embedded Application Framework Qt Tutorial Cheng-Liang (Paul) Hsieh ECE 424 Embedded Systems Design.
QT GUI Programming CS340 – Software Design © 2009 – Jason Leigh University of Illinois at Chicago.
Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Creating Computer Programs lesson 27. This lesson includes the following sections: What is a Computer Program? How Programs Solve Problems Two Approaches:
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
1 Chapter 1 Object-Oriented Programming. 2 OO programming and design Object-oriented programming and design can be contrasted with alternative programming.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 7 Improving the User Interface
1 Standard Widget Toolkit. 2 SWT l a widget toolkit for Java developers l provides a portable API and tight integration with the underlying native OS.
Dale Roberts Introduction to Qt Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Qt Igor November 8, 2002 Friday’s HENP Group Meeting.
Lecture 7: Objects and Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 
Introduction 01_intro.ppt
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Beans.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Blanchette and Summerfield, Ch. 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 8: More About OOP and GUIs.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Software Construction Lecture 10 Frameworks
Welcome to CIS 083 ! Events CIS 068.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 10Slide 1 Architectural Design l Establishing the overall structure of a software system.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Using Qt for GUI development Brad Whitlock March 2002.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Lecture 11: Exam Revision 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271  Coursework.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Applications Development
Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Build-A-Button Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, October 8, 2003.
I Power Higher Computing Software Development Development Languages and Environments.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CITA 342 Section 2 Visual Programming. Allows the use of visual expressions (such as graphics, drawings, or animation) in the process of programming.
Object Oriented Programming.  Interface  Event Handling.
Programming in the Simple Raster Graphics Package (SRGP) Chapter 2.
Chapter 5 Introduction to Defining Classes
QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
Introduction to OOP CPS235: Introduction.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Dale Roberts Programming with Qt Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Basic Organization of UI Software. 2 The User Interface n Typically want to think of “UI” as only one component of an overall system – The part that “deals.
Slide 1 Chapter 8 Architectural Design. Slide 2 Topics covered l System structuring l Control models l Modular decomposition l Domain-specific architectures.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
1. 2 Qt Toolkit ● C++ toolkit for cross-platform GUI application development – GUI functions (GUI = graphical user interface) – Data access components,
Visit for more Learning Resources
Support for the Development of Interactive Systems
HCI/CHI: Computer-Human Interaction
QT graphical user interface framework
User Interface Software Look under the hood
GTK + Programming.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Presentation transcript:

Dale Roberts Introduction to Visual Programming Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts Event Driven Processing Application Level Programmer Uses widget sets and style guidelines to create application GUIs. Examples: Motif, MFC, Qt, GTK, etc. Graphical Systems Programmer Person who implements the widget sets and windowing systems above You!

Dale Roberts Programming Paradigms By now, you have been exposed to two programming paradigms: Functional Programming Object-oriented Programming Can you explain their differences in terms of: How to decompose a problem? How to express control?

Dale Roberts Functional Decomposition How do to decompose a problem? Top-down functional decomposition approach. Step-wise refinement Successive versions of algorithms written in pseudo-code Problem is divided into smaller subproblems Each “decomposition” is a function call. Keep decomposing until functions are close enough to target language to code. How do you express control? Each step is a function call. Flow of control statements are used within functions

Dale Roberts Object-Oriented Design How do to decompose a problem? Bottom-up building block approach. Identify objects needed to solve the problem. Create an Abstract Data Type implemented with objects Identify how values are represented in private data members. Define operations that act upon values as public member functions. Encapsulate implementation details within the class. How do you express control? Use UML to define relationships between classes. Flow-of-control between classes is relatively unstructured. Must still define a client that uses classes to solve a problem. Client is usually responsible for user-interaction, not classes. Stratifies application into presentation, processing, and data layers.

Dale Roberts Event-driven Programming Event-driven programming is the standard approach to creating graphical user interfaces (GUIs) An event-driven program is object-oriented Object-oriented programming was originally development to implement graphical objects within GUI operating systems Although object-oriented, flow-of-control is sufficiently different that we are going to classify it as a different paradigm that extends OOP. However, top-level control is expressed differently The user is the top-level loop Think of Word, or a game program Every action in your program is a reaction to the user Decompose program in terms of “what will I do if the user does…” User inaction may trigger background actions (e.g. games)

Dale Roberts Detecting Asynchronous Events Polling Repeatedly read input devices in an infinite loop Interrupt-driven Hardware-triggered context-switching in response to user- events Event-driven Explicit event waiting Call-back functions (i.e. signals) in response to user events Qt uses event-driven processing.

Dale Roberts Method #1 Polling Interaction is governed by a simple loop: Loop forever: { read input respond to input } What limitations does this have? Does it have any advantages?

Dale Roberts Method #2 Interrupt-driven Processing Enable device, then proceed with “background” processing until an interrupt is received, at which point Save state (context-switch) Respond to input Restore state and go to step #2. What advantages/disadvantages does this have?

Dale Roberts Method #3: Event-driven Processing Interaction is once again governed by a loop: Loop forever: { if (event) then respond else do (one unit of) background processing or go to sleep (for one unit) }

Dale Roberts Event-driven Processing (cont) All major GUI packages (Motif, MGC, Qt, GTK, Java AWT, …) are event driven. Why? More portable than interrupt-driven. At the expense of what? More efficient than polling most do have polling commands Can rely on operating system to do time-slicing context-switching is very hardware/operating system specific.

Dale Roberts Events / Signals Any event-driven graphics package has devices that can signal events In old standards, this was limited to hardware devices In newer packages (e.g. Qt), any widget can signal events; the (hardware) mouse is the same as a (software) slider or button. Generally, the event tells you Which device/widget signaled the event Some “measure” giving the new state E.g., whether a mouse button was depressed or released Warning: old systems tend to use the term “events” while newer systems may call them signals (e.g. Qt)

Dale Roberts Callback Function One of the most feared and hackish aspects of GUI programming has always been the dreaded callback-function. A register table for widget (e.g. Motif) - no type checking - example: 1. quit = XtVaCreateManagedWidget(……); 2. tAddCallback( quit, XmNactivateCallback, QuitCallback, NULL); 3. void QuitCallback( Widget w, XtPointer, clientData, XtPointer callData);

Dale Roberts Callback Function Virtual function (e.g. wxWindows) - too many classes need to inherit for all widgets - high dependence between GUI and kernel of application - would be slower in a inefficient vtable Macro (e.g. MFC 、 OWL) - message map was complicated and difficult to coding - need additional preprocessor, IDE or application builder

Dale Roberts Event Handling QT's new approach: signals and slots A widget sends out various signals Object methods can be declared as slots Compatible signals and slots can be connected or plugged together like a telephone switchboard (parameter types must match) Strict separation This strict separation between UI components and program elements lends itself to component-based programming Goal: separate UI from program logic

Dale Roberts Signals and Slots clicked_method()

Dale Roberts Signals and Slots (cont) advantage: - independent interface - type-safe - process transparence disadvantage: - not as fast as a direct function pointer call. ( A signal triggering a slot has been measured to ( A signal triggering a slot has been measured to approximately 50 microseconds on a SPARC2. ) approximately 50 microseconds on a SPARC2. )

Dale Roberts Signals and Slots(cont) 1 class PixmapRotator : public QWidget { 1 class PixmapRotator : public QWidget { 2 Q_OBJECT 2 Q_OBJECT 3 public: 3 public: 4 PixmapRotator(QWidget *parent=0, const char *name=0); 4 PixmapRotator(QWidget *parent=0, const char *name=0); 5 public slots: 5 public slots: 6 void setAngle(int degrees); 6 void setAngle(int degrees); 7 signals: 7 signals: 8 void angleChanged(int); 8 void angleChanged(int); 9 private: 9 private: 10 int ang; 11 }; 12 void PixmapRotator::setAngle( int degrees ) { 13 degrees = degrees % 360;// keep in range 13 degrees = degrees % 360;// keep in range 14 if(ang == degrees) 15 return; // actual state change? 16 ang = degrees; // a new angle 17 emit angleChanged(ang); // tell world } 19 QObject::connect(scrollBar, SIGNAL(valueChanged(int)), rotator, SLOT(setAngle(int)));

Dale Roberts Signals and Slots(cont) Qt meta object compiler (moc) - It parses C++ header files and generates C++ code necessary for Qt to handle signals and slots. The signals, necessary for Qt to handle signals and slots. The signals, slots and emit keywords are macros, so the compiler slots and emit keywords are macros, so the compiler preprocessor changes or removes them. preprocessor changes or removes them. How to do? 1. moc –o moc_file.moc moc_file.cpp moc_file.h 1. moc –o moc_file.moc moc_file.cpp moc_file.h 2. #include “moc_file.moc” 2. #include “moc_file.moc” Fortunately, the Qt utility qmake takes care of all this.

Dale Roberts qmake The qmake utility is typically invoked with the following three commands] qmake –project qmake make (or nmake under Windows) Rules: Be sure to place code in its own directory. qmake scans all subdirectories for dependencies. Do not place archive version under a “save” subdirectory. If you reorganize your files, like adding a new.h, delete all the.pro and other working files, then start over.

Dale Roberts Defining Signals and Slots New C++ syntax for defining signals and slots, added to public, private, etc. class myClass : public Qobject { Q_OBJECT//required macro, no semicolon …signals: void somethingHappened(); … public slots: void slotDoSomething(); … private slots: void slotDoSomethingInternal(); …};

Dale Roberts Events Signals: emit events declare as signals, otherwise normal member functions You don't implement them. Rather, you send them with the (new) keyword emit E.g. emit(sliderChanged(5)) Slots: receive and handle events Normal member functions declared as slots Connect: must connect signals to slots QObject::connect( mymenu, SIGNAL(activated(int)), myobject, SLOT(slotDoMenuFunction(int)) ); moc: meta object compiler (preprocessor) converts these new keywords to real C++

Dale Roberts Widgets Base class for all UI widgets Properties width, height, backgroundColor, font, mouseTracking, backgroundPixmap, etc. Slots repaint, show, hide, move, setGeometry, setMainWidget, etc. Signals: mouseMoveEvent, keyPressEvent, resizeEvent, paintEvent, enterEvent, leaveEvent, etc.

Dale Roberts Qt, a GUI toolkit Events processed with signals and slots signal generates an event, e.g., button push slot processes the event, e.g., pop up a file dialog box QPushButton * quitB = new QPushButton(“Quit”,...,...); connect (quitB, SIGNAL(clicked()), qApp, SLOT(quit()); qApp is a global variable, of type QApplication one QApplication per program defined first in main() main returns qApp.exec() SIGNAL and SLOT are macros, expanded by a meta-object compiler (moc) moc generates.cpp files from user-defined Qt subclasses

Dale Roberts Other Features of Qt The Qt Paint Engine QPainter is highly optimized and contains several caching mechanisms to speed up drawing. Under X11, it caches GCs (graphics contexts), which often make it faster than native X11 programs. QPainter contains all the functionality one would expect from a professional 2D graphics library. The coordinate system of a QPainter can be transformed using the standard 2D transformations (translate, scale, rotate and shear). Qt supports Open GL for 3D graphics. Qt also contains a set of general purpose classes and a number of collection-classes to ease the development of multi-platform applications. Qt has platform independent support for the operating system dependent functions, such as time/date, files/directories and TCP/IP sockets.

Dale Roberts Call-back Functions / Slots A call-back function is an application-specific function called in response to an event In Qt, these are called “slots”, but this term is unique to Qt Generally, the “measure” of the event is passed as an argument The main loop of a GUI program is: Wait for an event Call the associated call-back function Return to the top of the loop

Dale Roberts GUI API Event Loops Loop forever: { if (input) then { find out which application receives the event; invoke the callback function; } else select one application with a background callback function; invoke background callback; }

Dale Roberts Pick Correlation The process of selecting which window (or application) an event belongs to is called pick correlation Pick correlation is usually object-oriented: Every window knows where its children are children send parents a message when they move, etc. The top-level window assigns events to children Child processes may handle the event through a call-back; or ask their children “is this yours”?

Dale Roberts Hiding the Main Loop Modern widget packages (like Qt) hide the main loop from the programmer. Programmers declare signals i.e. what events to respond to Programmers define slots i.e. how to respond to events Programmers connect signals to slots The main loop (wait for signal / call slot / loop) is part of the widget package Programmers call the main loop, but can’t alter it, other than through signals and slots

Dale Roberts Example: A Main Loop in Qt int main (int argc, char* argv[]) { QApplication app( argc, argv); QApplication app( argc, argv); GUI main( &app, &state) GUI main( &app, &state) app.setMainWidget( &main); app.setMainWidget( &main); main.Show(); main.Show(); return app.exec(); return app.exec();} Where’s the program? GUI is a widget. The GUI class defines signals and slots, and the GUI’s constructor connection them…

Dale Roberts What (really) are widgets? The objects in an object-oriented GUI are called widgets. Every widget: Knows its location (for pick correlation) Knows whether of not its visible Knows how to resize itself Knows how to redraw itself Knows its “children” widgets (if it’s a container) Has call-back functions (slots) for handling events (signals) Every window is a widget Not all widgets are windows

Dale Roberts Examples of widgets Text editing windows (canvases) Push buttons MenusSliders Radio buttons LED displays Borders

Dale Roberts Building a GUI Every application has a top-level widget In Qt, the top-level widget is called QApplication QApplication implements the main signal/slot loop QApplication is a widget but not a window QApplication in turn has a single top-level window Inside is a hierarchy of lesser widgets: frames, etc., for grouping and position widgets low-level widgets:buttons, pop-up menus, etc. Call-back functions (signals) are attached to implement responded to user actions Events are passed by the OS to the application’s top-level widget.

Dale Roberts Acknowledgements Some of the slides were originally written by J. Ross Beveridge, updated by Dale Roberts.