introduction to visual programming

18
Dale Roberts Introduction to Visual Introduction to Visual Programming Programming Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected] Department of Computer and Information Science, School of Science, IUPUI

Upload: michel

Post on 27-Jan-2016

129 views

Category:

Documents


13 download

DESCRIPTION

Department of Computer and Information Science, School of Science, IUPUI. Introduction to Visual Programming. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected]. Event-driven Programming. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Visual Programming

Dale Roberts

Introduction to Visual ProgrammingIntroduction to Visual Programming

Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]

Department of Computer and Information Science,School of Science, IUPUI

Page 2: Introduction to Visual Programming

Dale Roberts

Event-driven ProgrammingEvent-driven Programming

Event-driven programming is the standard Event-driven programming is the standard approach to creating graphical user interfaces approach to creating graphical user interfaces (GUIs)(GUIs)

An event-driven program is object-orientedAn event-driven program is object-orientedObject-oriented programming was originally development to Object-oriented programming was originally development to implement graphical objects within GUI operating systemsimplement graphical objects within GUI operating systemsAlthough object-oriented, flow-of-control is sufficiently different Although object-oriented, flow-of-control is sufficiently different that we are going to classify it as a different paradigm that that we are going to classify it as a different paradigm that extends OOP.extends OOP.

However, top-level control is expressed differentlyHowever, top-level control is expressed differentlyThe user is the top-level loopThe user is the top-level loop

Think of Word, or a game programThink of Word, or a game program

Every action in your program is a reaction to the userEvery action in your program is a reaction to the userDecompose program in terms of “what will I do if the user does…”Decompose program in terms of “what will I do if the user does…”User inaction may trigger background actions (e.g. games)User inaction may trigger background actions (e.g. games)

Page 3: Introduction to Visual Programming

Dale Roberts

Detecting Asynchronous EventsDetecting Asynchronous Events

PollingPollingRepeatedly read input devices in an infinite loopRepeatedly read input devices in an infinite loop

Interrupt-drivenInterrupt-drivenHardware-triggered context-switching in response to user-Hardware-triggered context-switching in response to user-events events

Event-drivenEvent-drivenExplicit event waitingExplicit event waitingInvokes functions in response to user eventsInvokes functions in response to user eventsNote that function invocations are Note that function invocations are asynchronousasynchronous

Qt uses event-driven processing.

Page 4: Introduction to Visual Programming

Dale Roberts

Method #1 PollingMethod #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?

Page 5: Introduction to Visual Programming

Dale Roberts

Method #2 Interrupt-driven ProcessingMethod #2 Interrupt-driven Processing

1. Enable device, then

2. proceed with “background” processing until an interrupt is received, at which point

3. Save state (context-switch)

4. Respond to input

5. Restore state and go to step #2.

What advantages/disadvantages does this have?

Page 6: Introduction to Visual Programming

Dale Roberts

Method #3: Event-driven ProcessingMethod #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)

}

Page 7: Introduction to Visual Programming

Dale Roberts

Events / Messages / SignalsEvents / Messages / Signals

Any event-driven graphics package has devices that can signal events

In old standards, this was limited to hardware devicesIn 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 youWhich device/widget signaled the eventSome “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 messages or signals

Page 8: Introduction to Visual Programming

Dale Roberts

Event handlersEvent handlers

Event handlers are functions invoked in Event handlers are functions invoked in response to an event.response to an event.

Predominant approach is through the use of Predominant approach is through the use of “call-back” functions.“call-back” functions.

Functions are registered with the eventFunctions are registered with the event

Signature of function most conform with language Signature of function most conform with language standard for the event standard for the event

Page 9: Introduction to Visual Programming

Dale Roberts

Pick CorrelationPick Correlation

Events are originally Events are originally detected by the operating detected by the operating system, and then routed system, and then routed to application through to application through “Pick Correlation”. The “Pick Correlation”. The application may continue application may continue to your the event to one to your the event to one of its children widgets.of its children widgets.

The principle is to The principle is to encapsulate the event by encapsulate the event by routing it to the lowest routing it to the lowest level object.level object.

(10, 20)

(40, 80)

(600, 700)

“click”

Coordinates are relative to (0,0) in the upper-left corner of desktop/windows/widget..

(0, 0)

(0, 0)

(0, 0)

Page 10: Introduction to Visual Programming

Dale Roberts

Call-back function: MotifCall-back function: Motif

quit = XtVaCreateManagedWidget(……);

tAddCallback( quit, XmNactivateCallback, QuitCallback, NULL);

void QuitCallback( Widget w, XtPointer, clientData, XtPointer callData);

Page 11: Introduction to Visual Programming

Dale Roberts

Call-back function: JavaCall-back function: JavabtnJava.addActionListener(this);btnWishJava.addActionListener(this);btnNoJava.addActionListener(this);

public void actionPerformed(ActionEvent e) {Object source = e.getSource();lblInstructionLabel.setText("Got it!");if (btnJava.equals(source)) lblFeedback.setText("Yes");

else if (btnWishJava.equals(source)) lblFeedback.setText("No");else if (btnNoJava.equals(source)) lblFeedback.setText("Maybe so");}//end action performed

Page 12: Introduction to Visual Programming

Dale Roberts

Event HandlingEvent Handling

QT's new approach: signals and slotsQT's new approach: signals and slotsA widget sends out various A widget sends out various signalssignals

Object methods can be declared as Object methods can be declared as slotsslots

Compatible signals and slots can be Compatible signals and slots can be connectedconnected or or plugged together like a telephone switchboard (parameter plugged together like a telephone switchboard (parameter types must match)types must match)

Strict separation Strict separation This strict separation between UI components and This strict separation between UI components and program elements lends itself to component-based program elements lends itself to component-based programmingprogramming

Goal: separate UI from program logicGoal: separate UI from program logic

Page 13: Introduction to Visual Programming

Dale Roberts

Signals and SlotsSignals and Slots

All that is required to All that is required to connect signals and slots connect signals and slots is that the signatures is that the signatures match.match.

Developers may use Developers may use predefined or user-predefined or user-defined member defined member functions as signals and functions as signals and slots.slots.

clicked_method()

Page 14: Introduction to Visual Programming

Dale Roberts

Making Connections in QtMaking Connections in Qt #include <qapplication.h>

#include <qpushbutton.h>

int main(int argc, char *argv[]){ QApplication app(argc, argv); QPushButton *button = new QPushButton("Quit", 0); QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit())); app.setMainWidget(button); button->show(); return app.exec();}

Note that the connect function is a static member function of QOBJECT. How do you know?

quit.exe

Page 15: Introduction to Visual Programming

Dale Roberts

qmakeqmake

The qmake utility is typically invoked with the following The qmake utility is typically invoked with the following three commands]three commands]

qmake –projectqmake –project

qmakeqmake

make (or nmake under Windows)make (or nmake under Windows)

Rules:Rules:Be sure to place code in its own directory.Be sure to place code in its own directory.

qmake scans all subdirectories for dependencies. Do not place qmake scans all subdirectories for dependencies. Do not place archive version under a “save” subdirectory.archive version under a “save” subdirectory.

If you reorganize your files, like adding a new .h, delete all the .pro If you reorganize your files, like adding a new .h, delete all the .pro and other working files, then start over.and other working files, then start over.

Page 16: Introduction to Visual Programming

Dale Roberts

Making Connections with QtMaking Connections with Qt1.1. #include <qapplication.h>#include <qapplication.h>2.2. #include <qhbox.h>#include <qhbox.h>3.3. #include <qslider.h>#include <qslider.h>4.4. #include <qspinbox.h>#include <qspinbox.h>

5.5. int main(int argc, char *argv[])int main(int argc, char *argv[])6.6. {{7.7. QApplication app(argc, argv);QApplication app(argc, argv);

8.8. QHBox *hbox = new QHBox(0);QHBox *hbox = new QHBox(0);9.9. hbox->setCaption("Enter Your Age");hbox->setCaption("Enter Your Age");10.10. hbox->setMargin(6);hbox->setMargin(6);11.11. hbox->setSpacing(6);hbox->setSpacing(6);

12.12. QSpinBox *spinBox = new QSpinBox(hbox);QSpinBox *spinBox = new QSpinBox(hbox);13.13. QSlider *slider = new QSlider(Qt::Horizontal, hbox);QSlider *slider = new QSlider(Qt::Horizontal, hbox);14.14. spinBox->setRange(0, 130);spinBox->setRange(0, 130);15.15. slider->setRange(0, 130);slider->setRange(0, 130);

16.16. QObject::connect(spinBox, SIGNAL(valueChanged(int)),QObject::connect(spinBox, SIGNAL(valueChanged(int)),17.17. slider, SLOT(setValue(int)));slider, SLOT(setValue(int)));18.18. QObject::connect(slider, SIGNAL(valueChanged(int)),QObject::connect(slider, SIGNAL(valueChanged(int)),19.19. spinBox, SLOT(setValue(int)));spinBox, SLOT(setValue(int)));20.20. spinBox->setValue(35);spinBox->setValue(35);

21.21. app.setMainWidget(hbox);app.setMainWidget(hbox);22.22. hbox->show();hbox->show();

23.23. return app.exec();return app.exec();24.24. }}

•Lines 1 – 4: You must include header files for each Qt object referenced in this file.•Line 5 - 7: Declaring argc and argv and passing it through to the QApplication constructor is standard procedure. This allows all Qt programs to specificy the presentation style on the command line. Examples are age –style=windows, age –style=motif, and age –style=cde.•Lines 8 – 11: Declare a Layout Manager. QHBox automatically arranged controls horizontally from left to right. Layout managers are used to alleviate the need for programmers to calculate actual coordinates for each control in the window. setCaption set the Title Bar. setMargin controls spacing around and in between child widgets.•Lines 12 – 13: Create a spin box and slider with QHBox as the parent.•Lines 14 – 15: Set the valid range for the spin box and the slider.•Lines 16 – 19: Ensure that the spin box and slider are sychronized. Changing one always changes the other.•Line 20 – Sets the initial value of the spin box to 35. Note that this causes the spin box to emit a valueChanged(int) signal, which is connected to slider’s setValue(int) slot. The slider will emit a valueChanged(int) signal, which is connected to the spin box’s setValue(int) slot. Since the value is already 35, the spin box does not emit another valueChanged(int) signal and avoids and infinite recursion loop.Lines 21 – 23: Sets the main widget to the hbox, makes the hbox visible, and starts the window’s main event loop.

age.exe

Page 17: Introduction to Visual Programming

Dale Roberts

Widget StylesWidget Styles

Qt supports dynamic widget Qt supports dynamic widget styles because of it’s GUI styles because of it’s GUI emulation approach.emulation approach.

age –style=windowsage –style=windows

age –style=motifage –style=motif

age –style=cdeage –style=cde

age –style=platinumage –style=platinum

The Windows XP and Mac The Windows XP and Mac styles are platform specific styles are platform specific because they rely on the because they rely on the platform’s theme engine.platform’s theme engine.

Enabled via argc and argv.Enabled via argc and argv.

Page 18: Introduction to Visual Programming

Dale Roberts

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

Code examples are from Blanchette, Jasmin and Summerfield, Mark. C++ GUI Programming With Qt3.