porting motif applications to qt - webinar

Post on 24-Jan-2018

328 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Motif Applications to Qt

Jeff Tranter <jtranter@ics.com>Integrated Computer Solutions, Inc.

© Integrated Computer Solutions, Inc. All Rights Reserved

Agenda

• Introduction• Anatomy of an Application• Toolkit Comparison• Development Environment• Inter-client Communication• Automation: GUI builders• I18N, Session Management and Help• Extras• Porting Your Application• Getting Help• References• Q&A

© Integrated Computer Solutions, Inc. All Rights Reserved

Introduction

• Despite its age, many legacy Motif applications still in production.• A number of advantages to migrating to newer platforms.• Porting to Qt framework can achieve many of the benefits without

requiring a total rewrite.• Will look at scenarios, strategies, and advice for porting Motif

applications to Qt.• ICS has done many Motif to Qt conversions and you can benefit

from our real-world experience.

© Integrated Computer Solutions, Inc. All Rights Reserved

What is Motif?

• GUI toolkit for graphical desktop applications.• Emerged in the 1980s on UNIX workstations.• C based, uses X Windowing System.• Often used for high-end complex applications in domains like

finance, engineering, RADAR, air traffic control, military.• Was commercial; made free for open source platforms (e.g.

Linux) in 2000; released under LGPL in 2012.• Still some minimal maintenance being done, latest release 2.3.6

in 2016.

© Integrated Computer Solutions, Inc. All Rights Reserved

What is Qt?

• Cross-platform application (not just GUI) framework.• C++ based.• Supports desktop, mobile, embedded platforms.• Commercial and open source (LGPL) licensing.

© Integrated Computer Solutions, Inc. All Rights Reserved

Reasons for Porting to Qt

• Main incentive usually cross-platform or additional platform support (e.g. Windows, Mac).• Improve UX: color depth, higher-quality fonts, visual effects,

native look and feel, localization.• Want to add new features that are difficult with Motif or want to

leverage features of Qt (e.g. web view).• Desire to move from C to cleaner, type-safe, object-oriented code

in C++.• Need a toolkit with professional support, bug fixes and frequent

releases.

© Integrated Computer Solutions, Inc. All Rights Reserved

Reasons to Rewrite Rather Than Port• Moving to mobile, tablet, or embedded touchscreen platforms

may require entirely new UX design.• May be moving to a platform that does not support Qt or C++.• May have decided to use native APIs on the new platform.• Current system is not maintainable, documented, understood or

missing source code.• Want to entirely re-architect system.

© Integrated Computer Solutions, Inc. All Rights Reserved

Evaluating Your Application

Effort and cost to port can vary depending on a number of factors:

• Complexity of the existing application.• Hardware dependencies.• Available time and resources.• User expectations.• Handwritten GUI code vs. builder-generated UI code.• Use of third party or custom widgets or other third party code or

libraries.

© Integrated Computer Solutions, Inc. All Rights Reserved

Evaluating Your Application

• Understanding of existing design and code.• Hidden/undocumented features or behavior.• Existing unit and regression tests.• Documentation.• Your team's knowledge of Qt.• Commercial or open source version of Qt.

© Integrated Computer Solutions, Inc. All Rights Reserved

Anatomy of an Application

• At a high level, both Motif and Qt:● are event-driven.● have similar basic widgets with hierarchical parent-child

relationships.• Motif uses callbacks, Qt uses more sophisticated and safer

signals and slots.• Qt separates widgets from layout managers.

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison

Description Motif Qt

Initialize XtAppInitialize() QApplication()

Create Widgets XmCreateWidgetName() QWidgetName()

Event Response XtAddCallback() QObject::connect()

Show Widget XtRealizeWidget() QWidget::show()

Enter Event Loop XtAppMainLoop() QApplication::exec()

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Motif - Simple Motif Program With Callbacks#include <Xm/Xm.h>#include <Xm/MainW.h>#include <Xm/ToggleB.h>#include <stdio.h>#include <stdbool.h>

void toggleChangedCB( Widget tb, XtPointer client_data, XtPointer call_data){ XmToggleButtonCallbackStruct *tbInfo = (XmToggleButtonCallbackStruct *)call_data; printf("The button state is: %s\n", tbInfo->set == XmSET ? "ON" : "OFF"); printf("The app data is: %ld\n", (long) client_data);}

...

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Motif - Simple Motif Program With Callbacksint main(int argc, char **argv){ Widget topLevel, mainW, tb; XtAppContext app; topLevel = XtVaOpenApplication(&app, "Example", NULL, 0, &argc, argv, NULL, sessionShellWidgetClass, NULL); mainW = XmCreateMainWindow(topLevel, "main_w", NULL, 0);

tb = XmCreateToggleButton(mainW, "Hello Motif", NULL, 0);

XtAddCallback(tb, XmNvalueChangedCallback, toggleChangedCB, (XtPointer) 42);

XtManageChild(tb); XtManageChild(mainW); XtRealizeWidget(topLevel); XtAppMainLoop(app); return 0;}

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Motif - Simple Motif Program With Callbacks

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Qt – Simple Qt Program with Signals and Slots// qtexample.h

#include <QObject>

class SomeObj : public QObject{ Q_OBJECTpublic: SomeObj(int objData) : QObject(nullptr), _objData(objData) {};

public slots: void toggled(bool toggleVal);

protected: int _objData;};

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Qt – Simple Qt Program with Signals and Slots// qtexample.cpp

#include <QtWidgets/QApplication>#include <QtWidgets/QPushButton>#include <QObject>#include <iostream>#include "qtexample.h"

void SomeObj::toggled(bool toggleVal){ std::cerr << "The button state is: " << toggleVal << std::endl; std::cerr << "The app data is: " << _objData << std::endl;}

...

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Qt – Simple Qt Program with Signals and Slotsint main(int argc, char ** argv){ QApplication app(argc, argv); QPushButton tb("Hello Qt");

tb.setCheckable(true); tb.setChecked(true); tb.show();

SomeObj myObject(42); QObject::connect(&tb, &QPushButton::toggled, &myObject, &SomeObj::toggled);

return app.exec();}

© Integrated Computer Solutions, Inc. All Rights Reserved

Hello Qt – Simple Qt Program with Signals and Slots

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison – Common UI Objects

Functionality Motif Qt Major Differences

Text/Pixmap Label XmLabel QLabel QLabel can also display rich text or a movie.

PushButton, ToggleButton XmPushButton, XmToggleButton

QPushButton Same class offers both kinds of functionality. Otherwise similar.

RadioButton, CheckBox XmToggleButton, XmRowColumn

QCheckBox, QRadioButton, QButtonGroup

In Motif, the parent RowColumn's resources dictate behavior.

Scrollbar XmScrollBar QScrollBar Very similar.

Scale XmScale QSlider Very similar.

List of items XmList, XmContainer QListView QListView can do multicolumn, hierarchical lists of arbitrary type. Motif XmList is limited to single column, linear list of XmStrings, but XMContainer is as powerful.

Single Line Text Field XmTextField QLineEdit QLineEdit supports rich text, encrypted entry, validator & built-in undo/redo.

Text XmText QTextEdit Same as above. Also QPlainTextEdit for plain text.

Horiz/Vert Tiled Windows XmPanedWindow QSplitter Very similar.

SpinBox XmSpinBox QSpinBox Very similar.

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison – Menus and OptionsFunctionality Motif Qt Major Differences

Horizontal Menubar XmMenuBar QMenuBar QMenuBar has several insertItem methods.

Pulldown Menu XmPulldownMenu QMenu Qt provides a cleaner abstraction using QMenu as the common object for pulldowns and popups.Popup Menu XmCreatePopupMenu QMenu

Option Menu, Combo Box

XmComboBox, XmOptionMenu

QComboBox Qt uses QComboBox for both.

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison - DialogsFunctionality Motif Qt

Error XmCreateErrorDialog QErrorMessage

Information XmCreateInformationDialog QMessageBox

Question XmCreateQuestionDialog QMessageBox

Warning XmCreateWarningDialog QMessageBox

Progress XmCreateWorkingDialog QProgressDialog

Simple Input XmCreatePromptDialog QInputDialog

File Chooser XmCreateFileDialog QFileDialog

Item Chooser XmCreateSelectionDialog Use QListWidget or QListView

Command History XmCreateCommand Use QListWidget or QListView

Generic/Custom XmCreateMessageDialog QDialog

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison – Complex Widgets• Main Window• OpenGL• Table• MDI• Tab Widget• GraphicsView• Frame

© Integrated Computer Solutions, Inc. All Rights Reserved

Toolkit Comparison - Complex Widgets• QDial• QLCDNumber• QDateTimeEdit• QToolBox• QToolBar, QToolButton• QDockWidget• QSplashScreen• QSvgWidget, QSvgRenderer, QSvgGenerator

© Integrated Computer Solutions, Inc. All Rights Reserved

Layout Management

• XmBulletinBoard• XmScrolledWindow: QAbstractScrollArea, QScrollArea• XmRowColumn: QHBoxLayout, QVBoxLayout, QGridLayout• XmPanedWindow: QSplitter• XmFrame: QGroupBox (not QFrame)• XmRadioBox: QButtonGroup, QRadioButton• XmForm: implement using Qt's layout management

© Integrated Computer Solutions, Inc. All Rights Reserved

Layout Management

Tips for layout management troubleshooting:

• Use Qt Designer.• Use simple QWidgets with bright background colors or labels for

all widgets in a layout.• May have to subclass certain widgets to override the sizeHint and

minimumSizeHint values to be able to resize them properly.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handling - Overview

• Motif: At the basic level, each widget instance contains a translation table that maps events to procedure names. Another table, the action table, maps procedure names to actual procedures. When an event gets dispatched to a widget, the corresponding action procedure is invoked.• In Qt, events are delivered to objects derived from the base

QObject class using the QObject::event interface.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handling – Do Nothing

• In Motif, widget instances “inherit” built-in event handlers that take care of the basic behavior (for example, pressing the backspace key in a text widget erases a character).• In Qt, the C++ inheritance model takes care of the default

behavior of widget instances/subclasses.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handling – Behavioral Events

• Motif: programmer can add procedures for certain combinations of events.• Example: XmPushButton callbacks called when user presses and

releases active mouse button inside the widget.• Qt has signals and slots.• Signal emitted when an event occurs.• Slot is a method called in response to a signal.• Signal from an object can be connected to a slot in any other

object as long as type signature is compatible.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handing – Finer Control

• Motif: For finer control over event handling, the translation tables and actions procedures can be changed or added to Motif widgets.• Qt: Additional signals can be easily added in sub-classed Qt

widgets.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handling – Direct Dispatch

• Motif: Application can register event handler procedures with the Xt event dispatcher for a particular widget. Procedures are called before the ones in the translation table.• Qt: QObject::event virtual method can be overridden to bypass or

to add event handling for a widget.• Most Qt widgets have several event handling methods that are at

a higher level of abstraction and can be overridden without completely changing the behavior of a widget through the lower level QObject::event method.• Qt widget can observe another widget’s events using the

QObject::eventFilter, QObject::installEventFilter methods.

© Integrated Computer Solutions, Inc. All Rights Reserved

Event Handling – More Control

• Some applications also grab the keyboard, pointer, or the entire server for specialized event handling. It is also possible to go down to the level of X11 to peek at, extract, resend events from the event queue.• In Qt, more control can be exercised using the available methods

on the main application object to manipulate the event loop or by installing an application-wide event filter. Creation and dispatch of custom and non-GUI events is also possible.

© Integrated Computer Solutions, Inc. All Rights Reserved

Look and Feel

• Motif has a unique look and feel.• Qt has some built-in styles that generally look native on the

platform.• Support for CSS-like stylesheets, style plugins, or custom widgets.

© Integrated Computer Solutions, Inc. All Rights Reserved

Development Environment – Language, Compilers, Libraries• Need to develop at least some of your application in C++.• Can call existing C code if desired, or wrap it in C++ classes.• May need to port legacy code to build with modern compilers.• IDEs: Qt Creator, Visual Studio, vi/emacs...• Build system: qmake, cmake.• Dependencies on third-party libraries.

© Integrated Computer Solutions, Inc. All Rights Reserved

Dev. Environment – Distribution, Installation, Documentation • Qt available under open source and commercial license options.• Generally not hard to comply with license requirements.• Typically will want to ship Qt with your application.• Qt renowned for quality of developer documentation.• Doxygen tool for documenting your code, especially libraries.• May want install wizard on Windows, dmg on Mac OS, deb/rpm on

Linux.

© Integrated Computer Solutions, Inc. All Rights Reserved

Inter-client Communication – Selection and Clipboard• X Windows supports Primary selection and Clipboard selection.• Primary selection enables availability of data as soon as selected.• Clipboard provides more traditional support by making data

available explicitly using copy or cut operation.• Qt has built-in support for both using the QClipboard class.

© Integrated Computer Solutions, Inc. All Rights Reserved

Inter-client Communication – Drag and Drop• Drag and Drop can be used to transfer data within and between

applications.• Motif’s support of drag and drop implemented on top of the

selection mechanisms provided by Xt and the X ICCCM.• Under X11, Drag and Drop support in Qt based on the XDND

protocol.• Relevant Qt classes: QDrag, QDragEnterEvent, QDragLeaveEvent,

QDragMoveEvent, QDropEvent

© Integrated Computer Solutions, Inc. All Rights Reserved

Automation – GUI Builder

• Qt provides Qt Designer GUI builder.• Allows laying out windows, widgets, dialogs and other elements.• Can set properties and connect signals and slots.• Saved as XML, which is then used to generate C++ code using

uic.• Generally considered best practice to use it.

© Integrated Computer Solutions, Inc. All Rights Reserved

Scripting

• Qt provides support for application scripting with ECMAScript (JavaScript).• See QScriptEngine and related classes• Considered deprecated for new applications. Instead use

QJSEngine class which is part of QML.• Also support for JavaScript in browser via QtWebEngine.• Qt applications can also be written entirely in Python using PyQt

or PySide bindings.

© Integrated Computer Solutions, Inc. All Rights Reserved

Testing and Inspection

• Qt provides Qt Test module, a framework for unit testing.• Can also use Google Test, etc. with Qt.• For automated, functional, scripting GUI record/playback testing,

recommend commercial SQUISH tool.• Various debug and analysis tools, some available from Qt Creator

and some external (e.g. Gamma Ray).

© Integrated Computer Solutions, Inc. All Rights Reserved

Localization and Internationalization• QString: uses Unicode.• QObject::tr(): For translating user visible strings.• Translations looked up at run time from a catalog.• Tools:

● lupdate: Extracts translatable strings from source files.● Linguist: Application for translators to enter translations.● lrelease: Creates compact binary table used at run time.

• Support for various languages using Qt's text engine for all input (QTextEdit, QLineEdit) and display (QLabel) controls.• Support for localized formats for numbers, times, timezones,

dates, currency, etc.

© Integrated Computer Solutions, Inc. All Rights Reserved

Session Management

• Often a requirement for applications to save settings or state.• X11 applications can register with X Session Manager.• Supported in Qt by QSessionManager class.• More common just to save settings using QSettings class.

© Integrated Computer Solutions, Inc. All Rights Reserved

Help

• Limited support in Motif for application help (callback for context sensitive help and tooltip resource).• Qt provides tooltips when hovering over a widget, What's This?

help, status bar.• Applications can use Qt's hypertext help system used by

Assistant (QHelpEngine and related classes).• Could also implement HTML-based help in a web view.

© Integrated Computer Solutions, Inc. All Rights Reserved

Extras

• Unlike Motif, Qt is much more than a GUI toolkit.• Support for:

● I/O, networking● Images● XML● Date/time● SQL databases● Strings, containers● Model/View pattern● Threading● Resource system

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Your Application

• Recommend to start with small non-mission critical application to port first.• Learn, gain experience, add more features.• Gain experience, and then move on to larger applications next.

© Integrated Computer Solutions, Inc. All Rights Reserved

Port or Redesign?

• Porting effort can have large impact on architecture for complex applications.• May want to re-architect.• Opportunity to modernize.• More object-oriented design.• Model/View architecture to separate visual design from data.

© Integrated Computer Solutions, Inc. All Rights Reserved

Gradual Migration

• Qt Motif Extension allowed running Motif widgets in a Qt application.• Was dropped in Qt 4, but still works with a few code changes.• Porting to Qt 5 difficult as Qt now uses xcb and not xlib.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies for Applications

• Will look at some different porting strategies.• Often initial port stays on Linux/UNIX, then later moves to other

platforms.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – Hand Written GUI• Dynamic layout of widgets.• Custom layout or constraints.• Hard to implement using GUI builder.• Typically can do this with Qt's layout engine.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – GUI Builder Generated• e.g. using Builder Xcessory (BX Pro) GUI builder tool on Motif.• Typically convert to Qt Designer.• Automated conversion possible in theory.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – C Based

• Need to port some or all of your code to C++.• Your team may need to learn object-oriented design.• Maybe require re-architecting (e.g. to separate GUI from business

logic).

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – Graphics Heavy

• Qt port can be opportunity to better leverage modern graphics hardware.• Many facilities available in Qt (QPainter, QGraphicsView,

OpenGL).• OpenGL code can be ported quite easily.• X11 painting code straightforward to port to Qt's painter API.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – Custom Widgets• Custom widgets in Motif are very complex, typically 10K-20K LOC.• Much easier with Qt, subclass existing widget or QWidget.• Qt custom widgets have properties with setters and getters, can

be used with Designer.• Don't have to implement layout management in the widget.

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Strategies – Custom Widgets

Description Motif Qt

Initialize ClassInitialize, Initialize, Create widget constructor(s)

Re-render the widget Redisplay update(), repaint()

Draw the widget DrawVisual, DrawShadow paintEvent, QPainter

Optimal widget size WidgetSize, VisualSize sizeHint

Accommodate widget Resize adjustSize

Changes in resource setValues public set/get methods

Event handling Callbacks/actions/translations events, signals & slots

Geometry management geometry_manager QLayout

Event propagation parent_process automatic, event filter

© Integrated Computer Solutions, Inc. All Rights Reserved

Getting Help

• Porting can be a significant effort.• Can also involve re-architecting application.• Consider bringing in outside consultants for:

● assistance (staff augmentation).● turnkey porting.● training, mentoring.● UX design.

© Integrated Computer Solutions, Inc. All Rights Reserved

References

1. Qt website, https://www.qt.io/2. ICS Motif website, http://motif.ics.com/3. Porting X/Motif Applications to Qt, Scenarios and Advice for a Smooth

Migration, ICS whitepaper, https://motif.ics.com/sites/default/files/porting_motif_to_qt.pdf

© Integrated Computer Solutions, Inc. All Rights Reserved

Questions and Answers

© Integrated Computer Solutions, Inc. All Rights Reserved

Porting Motif Applications to Qt

Jeff Tranter <jtranter@ics.com>Integrated Computer Solutions, Inc.

top related