libyui-qt  2.46.13
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 <QPalette>
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  * Constructor.
70  **/
71  YQUI( bool withThreads );
72 
73  /**
74  * Destructor.
75  **/
76  virtual ~YQUI();
77 
78  /**
79  * Access the global Qt-UI.
80  **/
81  static YQUI * ui() { return _ui; }
82 
83  /**
84  * Post-constructor initialization. If running with threads, this has to be
85  * called in the UI thread. Any subsequent calls will do nothing.
86  **/
87  void initUI();
88 
89 protected:
90  /**
91  * Create the widget factory that provides all the createXY() methods for
92  * standard (mandatory, i.e. non-optional) widgets.
93  *
94  * Reimplemented from YUI.
95  **/
96  virtual YWidgetFactory * createWidgetFactory();
97 
98  /**
99  * Create the widget factory that provides all the createXY() methods for
100  * optional ("special") widgets and the corresponding hasXYWidget()
101  * methods.
102  *
103  * Reimplemented from YUI.
104  **/
105  virtual YOptionalWidgetFactory * createOptionalWidgetFactory();
106 
107  /*
108  * Create the YApplication object that provides global methods.
109  *
110  * Reimplemented from YUI.
111  **/
112  virtual YApplication * createApplication();
113 
114 public:
115 
116  /**
117  * Return the global YApplication object as YQApplication.
118  *
119  * This will create the Y(Q)Application upon the first call and return a
120  * pointer to the one and only (singleton) Y(Q)Application upon each
121  * subsequent call. This may throw exceptions if the Y(Q)Application
122  * cannot be created.
123  **/
124  static YQApplication * yqApp();
125 
126  /**
127  * Widget event handlers (slots) call this when an event occured that
128  * should be the answer to a UserInput() / PollInput() (etc.) call.
129  *
130  * The UI assumes ownership of the event object that 'event' points to.
131  * In particular, it takes care to delete that object.
132  *
133  * It is an error to pass 0 for 'event'.
134  **/
135  void sendEvent( YEvent * event );
136 
137  /**
138  * Returns 'true' if there is any event pending for the specified widget.
139  **/
140  bool eventPendingFor( YWidget * widget ) const
141  { return _eventHandler.eventPendingFor( widget ); }
142 
143  /**
144  * Returns the last event that isn't processed yet or 0 if there is none.
145  *
146  * The Qt UI keeps track of only one single (the last one) event.
147  **/
148  YEvent * pendingEvent() const { return _eventHandler.pendingEvent(); }
149 
150  /**
151  * Return the pending event, if there is one, and mark it as "consumed".
152  *
153  * This returns 0 if there is no pending event.
154  **/
155  YEvent * consumePendingEvent() { return _eventHandler.consumePendingEvent(); }
156 
157  /**
158  * Notification that a widget is being deleted.
159  *
160  * Reimplemented from YUI.
161  **/
162  virtual void deleteNotify( YWidget * widget );
163 
164  /**
165  * Return 'true' if defaultsize windows should use the full screen.
166  **/
167  bool fullscreen() const { return _fullscreen; }
168 
169  /**
170  * Return 'true' if defaultsize windows should not get window manager
171  * borders / frames.
172  **/
173  bool noBorder() const { return _noborder; }
174  /**
175  * Returns 'true' if the UI had a fatal error that requires the application
176  * to abort.
177  **/
178  bool fatalError() const { return _fatalError; }
179 
180  /**
181  * Raise a fatal UI error. It will be delivered when it is safe to do so.
182  * The caller should make sure it can continue for some time until the
183  * error is delivered.
184  **/
185  void raiseFatalError() { _fatalError = true; }
186 
187  /**
188  * Returns size for `opt(`defaultsize) dialogs (in one dimension).
189  **/
190  int defaultSize( YUIDimension dim ) const;
191 
192  /**
193  * Make a screen shot in .png format and save it to 'filename'.
194  * Opens a file selection box if 'filename' is empty.
195  **/
196  void makeScreenShot( std::string filename );
197 
198  /**
199  * UI-specific runPkgSeleciton method: Start the package selection.
200  * This implementation does the same as UserInput().
201  *
202  * Reimplemented from YUI.
203  **/
204  virtual YEvent * runPkgSelection( YWidget * packageSelector );
205 
206  /**
207  * Toggle macro recording (activated by Ctrl-Shift-Alt-M):
208  * Stop macro recording if it is in progress,
209  * open a file selection box and ask for a macro file name to save to and
210  * start recording if no recording has been in progress.
211  **/
212  void toggleRecordMacro();
213 
214  /**
215  * Open file selection box and ask for a macro file to play
216  * (activated by Ctrl-Shift-Alt-P)
217  **/
218  void askPlayMacro();
219 
220  /**
221  * Block (or unblock) events. If events are blocked, any event sent
222  * should be ignored until events are unblocked again.
223  *
224  * Reimplemented from YUI.
225  **/
226  virtual void blockEvents( bool block = true );
227 
228  /**
229  * Returns 'true' if events are currently blocked.
230  *
231  * Reimplemented from YUI.
232  **/
233  virtual bool eventsBlocked() const;
234 
235  /**
236  * Force unblocking all events, no matter how many times blockEvents() has
237  * This returns 0 if there is no pending eventbeen called before.
238  **/
239  void forceUnblockEvents();
240 
241  /**
242  * Show mouse cursor indicating busy state.
243  **/
244  void busyCursor();
245 
246  /**
247  * Show normal mouse cursor not indicating busy status.
248  **/
249  void normalCursor();
250 
251  /**
252  * Show mouse cursor indicating busy state if the UI is unable to respond
253  * to user input for more than a predefined timeout (200 millisec).
254  **/
255  void timeoutBusyCursor();
256 
257  /**
258  * Open file selection box and let the user save y2logs to that location.
259  * (Shift-F8)
260  **/
261  void askSaveLogs();
262 
263  /**
264  * Open dialog to configure logging.
265  * (Shift-F7)
266  **/
267  void askConfigureLogging();
268 
269  /**
270  * Initialize and set a textdomain for gettext()
271  **/
272  static void setTextdomain( const char * domain );
273 
274  /**
275  * Returns a high-contrast color palette suitable for vision impaired users.
276  **/
277  static QPalette visionImpairedPalette();
278 
279  /**
280  * Returns the normal color palette
281  **/
282  QPalette normalPalette() const { return _normalPalette; }
283 
284  /**
285  * Toggle between the vision impaired and the normal color palette.
286  **/
288 
289  /**
290  * Returns 'true' if high-contrast colors for vision impaired users is in use.
291  * This should be queried at other places before using custom colors.
292  **/
293  bool usingVisionImpairedPalette() const { return _usingVisionImpairedPalette; }
294 
295  /**
296  * Returns the application name for the window title (e.g. "YaST2@hostname")
297  **/
298  QString applicationTitle() { return _applicationTitle; }
299 
300  /**
301  * Sets the application name for the window title
302  **/
303  void setApplicationTitle(const QString& title) { _applicationTitle=title; }
304 
305 protected:
306 
307  /**
308  * Handle command line args
309  **/
310  void processCommandLineArgs( int argc, char **argv );
311 
312  /**
313  * Probe the X11 display. Throw exception upon failure.
314  * A "-display" command line argument is taken into account.
315  **/
316  void probeX11Display( const YCommandLine & cmdLine );
317 
318  /**
319  * Calculate size of `opt(`defaultsize) dialogs
320  **/
321  void calcDefaultSize();
322 
323  /**
324  * Idle around until fd_ycp is readable and handle repaints.
325  * This is only used when a separate ui thread is running.
326  *
327  * Reimplemented from YUI.
328  **/
329  virtual void idleLoop( int fd_ycp );
330 
331  /**
332  * Destroy whatever needs to be destroyed within the UI thread.
333  *
334  * Reimplemented from YUI.
335  **/
336  virtual void uiThreadDestructor();
337 
338  /**
339  * Notification that a YCP command has been received on fd_ycp
340  * to leave idleLoop()
341  **/
342  void receivedYCPCommand();
343 
344  /**
345  * Application shutdown
346  **/
347  bool close();
348 
349 
350  //
351  // Data members
352  //
353 
354  static YQUI * _ui;
355 
356  QMap<QString, int> screenShotNo;
357  QString screenShotNameTemplate;
358 
359  bool _fullscreen;
360  bool _noborder;
361  QSize _defaultSize;
362 
363  bool _do_exit_loop;
364  bool _received_ycp_command;
365  bool _fatalError;
366 
367  QTimer * _busyCursorTimer;
368 
369  YSimpleEventHandler _eventHandler;
370  int _blockedLevel;
371 
372  QPalette _normalPalette;
373  bool _usingVisionImpairedPalette;
374 
375  bool _leftHandedMouse;
376  bool _askedForLeftHandedMouse;
377 
378  bool _uiInitialized;
379 
380  YQUISignalReceiver * _signalReceiver;
381  QString _applicationTitle;
382 
383  // Qt copies the _reference_ to argc, so we need to store argc
384  int _ui_argc;
385 };
386 
387 
388 /**
389  * Helper class that acts as a Qt signal receiver for YQUI.
390  * YQUI itself cannot be a QObject to avoid problems with starting up the UI
391  * with multiple threads.
392  **/
393 class YQUISignalReceiver : public QObject
394 {
395  Q_OBJECT
396 
397 public:
399 
400 public slots:
401 
402  void slotBusyCursor();
403  void slotReceivedYCPCommand();
404 };
405 
406 
407 /**
408  * Create a new UI if there is none yet or return the existing one if there is.
409  *
410  * This is the UI plugin's interface to the outside world, so don't change the
411  * name or signature!
412  **/
413 YUI * createUI( bool withThreads );
414 
415 
416 #endif // YQUI_h
bool fullscreen() const
Return 'true' if defaultsize windows should use the full screen.
Definition: YQUI.h:167
void receivedYCPCommand()
Notification that a YCP command has been received on fd_ycp to leave idleLoop()
Definition: YQUI.cc:480
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:284
void askConfigureLogging()
Open dialog to configure logging.
void forceUnblockEvents()
Force unblocking all events, no matter how many times blockEvents() has This returns 0 if there is no...
Definition: YQUI.cc:554
bool eventPendingFor(YWidget *widget) const
Returns 'true' if there is any event pending for the specified widget.
Definition: YQUI.h:140
void setApplicationTitle(const QString &title)
Sets the application name for the window title.
Definition: YQUI.h:303
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
Definition: YQUI.cc:593
void makeScreenShot(std::string filename)
Make a screen shot in .png format and save it to 'filename'.
void askSaveLogs()
Open file selection box and let the user save y2logs to that location.
virtual YEvent * runPkgSelection(YWidget *packageSelector)
UI-specific runPkgSeleciton method: Start the package selection.
void toggleRecordMacro()
Toggle macro recording (activated by Ctrl-Shift-Alt-M): Stop macro recording if it is in progress...
virtual YOptionalWidgetFactory * createOptionalWidgetFactory()
Create the widget factory that provides all the createXY() methods for optional ("special") widgets a...
Definition: YQUI.cc:379
QString applicationTitle()
Returns the application name for the window title (e.g.
Definition: YQUI.h:298
void calcDefaultSize()
Calculate size of opt(defaultsize) dialogs.
Definition: YQUI.cc:398
bool usingVisionImpairedPalette() const
Returns 'true' if high-contrast colors for vision impaired users is in use.
Definition: YQUI.h:293
Helper class that acts as a Qt signal receiver for YQUI.
Definition: YQUI.h:393
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:486
YQUI(bool withThreads)
Constructor.
Definition: YQUI.cc:92
virtual void idleLoop(int fd_ycp)
Idle around until fd_ycp is readable and handle repaints.
Definition: YQUI.cc:447
void probeX11Display(const YCommandLine &cmdLine)
Probe the X11 display.
Definition: YQUI.cc:599
virtual bool eventsBlocked() const
Returns 'true' if events are currently blocked.
Definition: YQUI.cc:562
bool noBorder() const
Return 'true' if defaultsize windows should not get window manager borders / frames.
Definition: YQUI.h:173
virtual void deleteNotify(YWidget *widget)
Notification that a widget is being deleted.
Definition: YQUI.cc:626
void busyCursor()
Show mouse cursor indicating busy state.
Definition: YQUI.cc:568
Definition: YQUI.h:62
void processCommandLineArgs(int argc, char **argv)
Handle command line args.
Definition: YQUI.cc:290
static QPalette visionImpairedPalette()
Returns a high-contrast color palette suitable for vision impaired users.
Definition: YQUI.cc:650
YEvent * consumePendingEvent()
Return the pending event, if there is one, and mark it as "consumed".
Definition: YQUI.h:155
Concrete widget factory for mandatory widgets.
virtual void uiThreadDestructor()
Destroy whatever needs to be destroyed within the UI thread.
Definition: YQUI.cc:357
QPalette normalPalette() const
Returns the normal color palette.
Definition: YQUI.h:282
virtual void blockEvents(bool block=true)
Block (or unblock) events.
Definition: YQUI.cc:520
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:584
bool fatalError() const
Returns 'true' if the UI had a fatal error that requires the application to abort.
Definition: YQUI.h:178
bool close()
Application shutdown.
Definition: YQUI.cc:686
void normalCursor()
Show normal mouse cursor not indicating busy status.
Definition: YQUI.cc:574
void initUI()
Post-constructor initialization.
Definition: YQUI.cc:119
YEvent * pendingEvent() const
Returns the last event that isn't processed yet or 0 if there is none.
Definition: YQUI.h:148
void askPlayMacro()
Open file selection box and ask for a macro file to play (activated by Ctrl-Shift-Alt-P) ...
virtual ~YQUI()
Destructor.
Definition: YQUI.cc:340
void raiseFatalError()
Raise a fatal UI error.
Definition: YQUI.h:185
static void setTextdomain(const char *domain)
Initialize and set a textdomain for gettext()
Definition: YQUI.cc:506
void toggleVisionImpairedPalette()
Toggle between the vision impaired and the normal color palette.
Definition: YQUI.cc:632
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:81
Widget factory for optional ("special") widgets.
virtual YWidgetFactory * createWidgetFactory()
Create the widget factory that provides all the createXY() methods for standard (mandatory, i.e.
Definition: YQUI.cc:368