libyui-qt  2.53.0
YQUI.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQUI.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YQUI_h
26 #define YQUI_h
27 
28 #include <qapplication.h>
29 #include <QMap>
30 #include <QTimer>
31 #include <QIcon>
32 #include <vector>
33 #include <type_traits>
34 
35 #include <yui/YUI.h>
36 #include <yui/YSimpleEventHandler.h>
37 #include <yui/YCommandLine.h>
38 
39 #define YQWidgetMargin 4
40 #define YQWidgetSpacing 4
41 #define YQButtonBorder 3
42 
43 //! The class of a pointer expression.
44 // To be used in connect(foo, &pclass(foo)::mysignal, bar, &pclass(bar)::myslot);
45 // That checks types at compile time,
46 // unlike the string based SIGNAL and SLOT macros.
47 #define pclass(ptr) std::remove_reference<decltype(*ptr)>::type
48 
49 
50 class QCursor;
51 class QFrame;
52 class QStackedWidget;
53 class YEvent;
55 class YQWidgetFactory;
56 class YQApplication;
57 class YQUISignalReceiver;
58 
59 using std::string;
60 using std::vector;
61 
62 class YQUI: public YUI
63 {
64  friend class YQUISignalReceiver;
65 
66 public:
67 
68  /**
69  * Constructors. Having second boolean topmostConstructor to be
70  * called only when there is topmost constructor and not a plugin which
71  * inherits from YQUI. Used by integration testing framework
72  **/
73  YQUI( bool withThreads, bool topmostConstructor = true );
74 
75  /**
76  * Destructor.
77  **/
78  virtual ~YQUI();
79 
80  /**
81  * Access the global Qt-UI.
82  **/
83  static YQUI * ui() { return _ui; }
84 
85  /**
86  * Post-constructor initialization. If running with threads, this has to be
87  * called in the UI thread. Any subsequent calls will do nothing.
88  **/
89  virtual void initUI();
90 
91 protected:
92  /**
93  * Create the widget factory that provides all the createXY() methods for
94  * standard (mandatory, i.e. non-optional) widgets.
95  *
96  * Reimplemented from YUI.
97  **/
98  virtual YWidgetFactory * createWidgetFactory();
99 
100  /**
101  * Create the widget factory that provides all the createXY() methods for
102  * optional ("special") widgets and the corresponding hasXYWidget()
103  * methods.
104  *
105  * Reimplemented from YUI.
106  **/
107  virtual YOptionalWidgetFactory * createOptionalWidgetFactory();
108 
109  /*
110  * Create the YApplication object that provides global methods.
111  *
112  * Reimplemented from YUI.
113  **/
114  virtual YApplication * createApplication();
115 
116 public:
117 
118  /**
119  * Return the global YApplication object as YQApplication.
120  *
121  * This will create the Y(Q)Application upon the first call and return a
122  * pointer to the one and only (singleton) Y(Q)Application upon each
123  * subsequent call. This may throw exceptions if the Y(Q)Application
124  * cannot be created.
125  **/
126  static YQApplication * yqApp();
127 
128  /**
129  * Widget event handlers (slots) call this when an event occured that
130  * should be the answer to a UserInput() / PollInput() (etc.) call.
131  *
132  * The UI assumes ownership of the event object that 'event' points to.
133  * In particular, it takes care to delete that object.
134  *
135  * It is an error to pass 0 for 'event'.
136  **/
137  void sendEvent( YEvent * event );
138 
139  /**
140  * Returns 'true' if there is any event pending for the specified widget.
141  **/
142  bool eventPendingFor( YWidget * widget ) const
143  { return _eventHandler.eventPendingFor( widget ); }
144 
145  /**
146  * Returns the last event that isn't processed yet or 0 if there is none.
147  *
148  * The Qt UI keeps track of only one single (the last one) event.
149  **/
150  YEvent * pendingEvent() const { return _eventHandler.pendingEvent(); }
151 
152  /**
153  * Return the pending event, if there is one, and mark it as "consumed".
154  *
155  * This returns 0 if there is no pending event.
156  **/
157  YEvent * consumePendingEvent() { return _eventHandler.consumePendingEvent(); }
158 
159  /**
160  * Notification that a widget is being deleted.
161  *
162  * Reimplemented from YUI.
163  **/
164  virtual void deleteNotify( YWidget * widget );
165 
166  /**
167  * Return 'true' if defaultsize windows should use the full screen.
168  **/
169  bool fullscreen() const { return _fullscreen; }
170 
171  /**
172  * Return 'true' if defaultsize windows should not get window manager
173  * borders / frames.
174  **/
175  bool noBorder() const { return _noborder; }
176  /**
177  * Returns 'true' if the UI had a fatal error that requires the application
178  * to abort.
179  **/
180  bool fatalError() const { return _fatalError; }
181 
182  /**
183  * Raise a fatal UI error. It will be delivered when it is safe to do so.
184  * The caller should make sure it can continue for some time until the
185  * error is delivered.
186  **/
187  void raiseFatalError() { _fatalError = true; }
188 
189  /**
190  * Returns size for `opt(`defaultsize) dialogs (in one dimension).
191  **/
192  int defaultSize( YUIDimension dim ) const;
193 
194  /**
195  * Make a screen shot in .png format and save it to 'filename'.
196  * Opens a file selection box if 'filename' is empty.
197  **/
198  void makeScreenShot( std::string filename );
199 
200  /**
201  * UI-specific runPkgSeleciton method: Start the package selection.
202  * This implementation does the same as UserInput().
203  *
204  * Reimplemented from YUI.
205  **/
206  virtual YEvent * runPkgSelection( YWidget * packageSelector );
207 
208  /**
209  * Toggle macro recording (activated by Ctrl-Shift-Alt-M):
210  * Stop macro recording if it is in progress,
211  * open a file selection box and ask for a macro file name to save to and
212  * start recording if no recording has been in progress.
213  **/
214  void toggleRecordMacro();
215 
216  /**
217  * Open file selection box and ask for a macro file to play
218  * (activated by Ctrl-Shift-Alt-P)
219  **/
220  void askPlayMacro();
221 
222  /**
223  * Open a pop-up dialog to ask the user for a widget ID and then send it
224  * with sendWidgetID().
225  **/
226  void askSendWidgetID();
227 
228  /**
229  * Block (or unblock) events. If events are blocked, any event sent
230  * should be ignored until events are unblocked again.
231  *
232  * Reimplemented from YUI.
233  **/
234  virtual void blockEvents( bool block = true );
235 
236  /**
237  * Returns 'true' if events are currently blocked.
238  *
239  * Reimplemented from YUI.
240  **/
241  virtual bool eventsBlocked() const;
242 
243  /**
244  * Force unblocking all events, no matter how many times blockEvents() has
245  * This returns 0 if there is no pending eventbeen called before.
246  **/
247  void forceUnblockEvents();
248 
249  /**
250  * Show mouse cursor indicating busy state.
251  **/
252  void busyCursor();
253 
254  /**
255  * Show normal mouse cursor not indicating busy status.
256  **/
257  void normalCursor();
258 
259  /**
260  * Show mouse cursor indicating busy state if the UI is unable to respond
261  * to user input for more than a predefined timeout (200 millisec).
262  **/
263  void timeoutBusyCursor();
264 
265  /**
266  * Open file selection box and let the user save y2logs to that location.
267  * (Shift-F8)
268  **/
269  void askSaveLogs();
270 
271  /**
272  * Open dialog to configure logging.
273  * (Shift-F7)
274  **/
275  void askConfigureLogging();
276 
277  /**
278  * Initialize and set a textdomain for gettext()
279  **/
280  static void setTextdomain( const char * domain );
281 
282  /**
283  * Returns the application name for the window title (e.g. "YaST2@hostname")
284  **/
285  QString applicationTitle() { return _applicationTitle; }
286 
287  /**
288  * Sets the application name for the window title
289  **/
290  void setApplicationTitle(const QString & title) { _applicationTitle=title; }
291 
292  /**
293  * Load an icon. This tries several locations:
294  *
295  * - The icon theme from the current desktop
296  * - The compiled-in Qt resources
297  * - An external file
298  *
299  * If the icon does not have a filename extension, the icon theme will try
300  * to append ".svg" and ".png". For the compiled-in Qt resources, there are
301  * aliases specified ("foo" -> "foo.svg"), so it will also work without an
302  * extension.
303  *
304  * For external files, a path and an extension will be necessary.
305  *
306  * If no icon could be loaded, this will return a null QIcon (check with
307  * icon.isNull()), and a warning is logged.
308  **/
309  QIcon loadIcon( const string & iconName ) const;
310 
311 protected:
312 
313  /**
314  * Handle command line args
315  **/
316  void processCommandLineArgs( int argc, char **argv );
317 
318  /**
319  * Probe the X11 display. Throw exception upon failure.
320  * A "-display" command line argument is taken into account.
321  **/
322  void probeX11Display( const YCommandLine & cmdLine );
323 
324  /**
325  * Calculate size of `opt(`defaultsize) dialogs
326  **/
327  void calcDefaultSize();
328 
329  /**
330  * Idle around until fd_ycp is readable and handle repaints.
331  * This is only used when a separate ui thread is running.
332  *
333  * Reimplemented from YUI.
334  **/
335  virtual void idleLoop( int fd_ycp );
336 
337  /**
338  * Destroy whatever needs to be destroyed within the UI thread.
339  *
340  * Reimplemented from YUI.
341  **/
342  virtual void uiThreadDestructor();
343 
344  /**
345  * Notification that a YCP command has been received on fd_ycp
346  * to leave idleLoop()
347  **/
348  void receivedYCPCommand();
349 
350  /**
351  * Application shutdown
352  **/
353  bool close();
354 
355 
356  //
357  // Data members
358  //
359 
360  static YQUI * _ui;
361 
362  QMap<QString, int> screenShotNo;
363 
364  bool _fullscreen;
365  bool _noborder;
366  QSize _defaultSize;
367 
368  bool _do_exit_loop;
369  bool _received_ycp_command;
370  bool _fatalError;
371 
372  QTimer * _busyCursorTimer;
373 
374  YSimpleEventHandler _eventHandler;
375  int _blockedLevel;
376 
377  bool _leftHandedMouse;
378  bool _askedForLeftHandedMouse;
379 
380  bool _uiInitialized;
381 
382  YQUISignalReceiver * _signalReceiver;
383  QString _applicationTitle;
384 
385  // Qt copies the _reference_ to argc, so we need to store argc
386  int _ui_argc;
387 };
388 
389 
390 /**
391  * Helper class that acts as a Qt signal receiver for YQUI.
392  * YQUI itself cannot be a QObject to avoid problems with starting up the UI
393  * with multiple threads.
394  **/
395 class YQUISignalReceiver : public QObject
396 {
397  Q_OBJECT
398 
399 public:
401 
402 public slots:
403 
404  void slotBusyCursor();
405  void slotReceivedYCPCommand();
406 };
407 
408 
409 /**
410  * Create a new UI if there is none yet or return the existing one if there is.
411  *
412  * This is the UI plugin's interface to the outside world, so don't change the
413  * name or signature!
414  **/
415 YUI * createUI( bool withThreads );
416 
417 
418 #endif // YQUI_h
YQUI::idleLoop
virtual void idleLoop(int fd_ycp)
Idle around until fd_ycp is readable and handle repaints.
Definition: YQUI.cc:441
YQUI::probeX11Display
void probeX11Display(const YCommandLine &cmdLine)
Probe the X11 display.
Definition: YQUI.cc:593
YQUI::deleteNotify
virtual void deleteNotify(YWidget *widget)
Notification that a widget is being deleted.
Definition: YQUI.cc:599
YQUI::sendEvent
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:480
YQUI::busyCursor
void busyCursor()
Show mouse cursor indicating busy state.
Definition: YQUI.cc:562
YQUI::processCommandLineArgs
void processCommandLineArgs(int argc, char **argv)
Handle command line args.
Definition: YQUI.cc:274
YQUI::YQUI
YQUI(bool withThreads, bool topmostConstructor=true)
Constructors.
Definition: YQUI.cc:106
YQUI::consumePendingEvent
YEvent * consumePendingEvent()
Return the pending event, if there is one, and mark it as "consumed".
Definition: YQUI.h:157
YQOptionalWidgetFactory
Widget factory for optional ("special") widgets.
Definition: YQOptionalWidgetFactory.h:55
YQUI::uiThreadDestructor
virtual void uiThreadDestructor()
Destroy whatever needs to be destroyed within the UI thread.
Definition: YQUI.cc:341
YQUI
Definition: YQUI.h:63
YQUI::fullscreen
bool fullscreen() const
Return 'true' if defaultsize windows should use the full screen.
Definition: YQUI.h:169
YQUI::blockEvents
virtual void blockEvents(bool block=true)
Block (or unblock) events.
Definition: YQUI.cc:514
YQUI::close
bool close()
Application shutdown.
Definition: YQUI.cc:605
YQUI::~YQUI
virtual ~YQUI()
Destructor.
Definition: YQUI.cc:323
YQUI::timeoutBusyCursor
void timeoutBusyCursor()
Show mouse cursor indicating busy state if the UI is unable to respond to user input for more than a ...
Definition: YQUI.cc:578
YQUI::setTextdomain
static void setTextdomain(const char *domain)
Initialize and set a textdomain for gettext()
Definition: YQUI.cc:500
YQUI::normalCursor
void normalCursor()
Show normal mouse cursor not indicating busy status.
Definition: YQUI.cc:568
YQUI::initUI
virtual void initUI()
Post-constructor initialization.
Definition: YQUI.cc:133
YQUI::createWidgetFactory
virtual YWidgetFactory * createWidgetFactory()
Create the widget factory that provides all the createXY() methods for standard (mandatory,...
Definition: YQUI.cc:362
YQUI::askPlayMacro
void askPlayMacro()
Open file selection box and ask for a macro file to play (activated by Ctrl-Shift-Alt-P)
Definition: YQUI_builtins.cc:344
YQUISignalReceiver
Helper class that acts as a Qt signal receiver for YQUI.
Definition: YQUI.h:396
YQUI::receivedYCPCommand
void receivedYCPCommand()
Notification that a YCP command has been received on fd_ycp to leave idleLoop()
Definition: YQUI.cc:474
YQUI::raiseFatalError
void raiseFatalError()
Raise a fatal UI error.
Definition: YQUI.h:187
YQUI::ui
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:83
YQUI::eventsBlocked
virtual bool eventsBlocked() const
Returns 'true' if events are currently blocked.
Definition: YQUI.cc:556
YQUI::fatalError
bool fatalError() const
Returns 'true' if the UI had a fatal error that requires the application to abort.
Definition: YQUI.h:180
YQUI::defaultSize
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
Definition: YQUI.cc:587
YQApplication
Definition: YQApplication.h:44
YQUI::forceUnblockEvents
void forceUnblockEvents()
Force unblocking all events, no matter how many times blockEvents() has This returns 0 if there is no...
Definition: YQUI.cc:548
YQUI::eventPendingFor
bool eventPendingFor(YWidget *widget) const
Returns 'true' if there is any event pending for the specified widget.
Definition: YQUI.h:142
YQUI::noBorder
bool noBorder() const
Return 'true' if defaultsize windows should not get window manager borders / frames.
Definition: YQUI.h:175
YQUI::setApplicationTitle
void setApplicationTitle(const QString &title)
Sets the application name for the window title.
Definition: YQUI.h:290
YQUI::pendingEvent
YEvent * pendingEvent() const
Returns the last event that isn't processed yet or 0 if there is none.
Definition: YQUI.h:150
YQUI::yqApp
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:268
YQUI::askConfigureLogging
void askConfigureLogging()
Open dialog to configure logging.
Definition: YQUI_builtins.cc:274
YQUI::makeScreenShot
void makeScreenShot(std::string filename)
Make a screen shot in .png format and save it to 'filename'.
Definition: YQUI_builtins.cc:97
YQUI::askSaveLogs
void askSaveLogs()
Open file selection box and let the user save y2logs to that location.
Definition: YQUI_builtins.cc:215
YQUI::runPkgSelection
virtual YEvent * runPkgSelection(YWidget *packageSelector)
UI-specific runPkgSeleciton method: Start the package selection.
Definition: YQUI_builtins.cc:67
YQUI::askSendWidgetID
void askSendWidgetID()
Open a pop-up dialog to ask the user for a widget ID and then send it with sendWidgetID().
Definition: YQUI.cc:613
YQUI::applicationTitle
QString applicationTitle()
Returns the application name for the window title (e.g.
Definition: YQUI.h:285
YQUI::toggleRecordMacro
void toggleRecordMacro()
Toggle macro recording (activated by Ctrl-Shift-Alt-M): Stop macro recording if it is in progress,...
Definition: YQUI_builtins.cc:302
YQUI::createOptionalWidgetFactory
virtual YOptionalWidgetFactory * createOptionalWidgetFactory()
Create the widget factory that provides all the createXY() methods for optional ("special") widgets a...
Definition: YQUI.cc:372
YQWidgetFactory
Concrete widget factory for mandatory widgets.
Definition: YQWidgetFactory.h:71
YQUI::calcDefaultSize
void calcDefaultSize()
Calculate size of opt(defaultsize) dialogs.
Definition: YQUI.cc:391
YQUI::loadIcon
QIcon loadIcon(const string &iconName) const
Load an icon.
Definition: YQUI.cc:708