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