libyui-qt  2.47.1.1
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  * Block (or unblock) events. If events are blocked, any event sent
221  * should be ignored until events are unblocked again.
222  *
223  * Reimplemented from YUI.
224  **/
225  virtual void blockEvents( bool block = true );
226 
227  /**
228  * Returns 'true' if events are currently blocked.
229  *
230  * Reimplemented from YUI.
231  **/
232  virtual bool eventsBlocked() const;
233 
234  /**
235  * Force unblocking all events, no matter how many times blockEvents() has
236  * This returns 0 if there is no pending eventbeen called before.
237  **/
238  void forceUnblockEvents();
239 
240  /**
241  * Show mouse cursor indicating busy state.
242  **/
243  void busyCursor();
244 
245  /**
246  * Show normal mouse cursor not indicating busy status.
247  **/
248  void normalCursor();
249 
250  /**
251  * Show mouse cursor indicating busy state if the UI is unable to respond
252  * to user input for more than a predefined timeout (200 millisec).
253  **/
254  void timeoutBusyCursor();
255 
256  /**
257  * Open file selection box and let the user save y2logs to that location.
258  * (Shift-F8)
259  **/
260  void askSaveLogs();
261 
262  /**
263  * Open dialog to configure logging.
264  * (Shift-F7)
265  **/
266  void askConfigureLogging();
267 
268  /**
269  * Initialize and set a textdomain for gettext()
270  **/
271  static void setTextdomain( const char * domain );
272 
273  /**
274  * Returns the application name for the window title (e.g. "YaST2@hostname")
275  **/
276  QString applicationTitle() { return _applicationTitle; }
277 
278  /**
279  * Sets the application name for the window title
280  **/
281  void setApplicationTitle(const QString& title) { _applicationTitle=title; }
282 
283 protected:
284 
285  /**
286  * Handle command line args
287  **/
288  void processCommandLineArgs( int argc, char **argv );
289 
290  /**
291  * Probe the X11 display. Throw exception upon failure.
292  * A "-display" command line argument is taken into account.
293  **/
294  void probeX11Display( const YCommandLine & cmdLine );
295 
296  /**
297  * Calculate size of `opt(`defaultsize) dialogs
298  **/
299  void calcDefaultSize();
300 
301  /**
302  * Idle around until fd_ycp is readable and handle repaints.
303  * This is only used when a separate ui thread is running.
304  *
305  * Reimplemented from YUI.
306  **/
307  virtual void idleLoop( int fd_ycp );
308 
309  /**
310  * Destroy whatever needs to be destroyed within the UI thread.
311  *
312  * Reimplemented from YUI.
313  **/
314  virtual void uiThreadDestructor();
315 
316  /**
317  * Notification that a YCP command has been received on fd_ycp
318  * to leave idleLoop()
319  **/
320  void receivedYCPCommand();
321 
322  /**
323  * Application shutdown
324  **/
325  bool close();
326 
327 
328  //
329  // Data members
330  //
331 
332  static YQUI * _ui;
333 
334  QMap<QString, int> screenShotNo;
335  QString screenShotNameTemplate;
336 
337  bool _fullscreen;
338  bool _noborder;
339  QSize _defaultSize;
340 
341  bool _do_exit_loop;
342  bool _received_ycp_command;
343  bool _fatalError;
344 
345  QTimer * _busyCursorTimer;
346 
347  YSimpleEventHandler _eventHandler;
348  int _blockedLevel;
349 
350  bool _leftHandedMouse;
351  bool _askedForLeftHandedMouse;
352 
353  bool _uiInitialized;
354 
355  YQUISignalReceiver * _signalReceiver;
356  QString _applicationTitle;
357 
358  // Qt copies the _reference_ to argc, so we need to store argc
359  int _ui_argc;
360 };
361 
362 
363 /**
364  * Helper class that acts as a Qt signal receiver for YQUI.
365  * YQUI itself cannot be a QObject to avoid problems with starting up the UI
366  * with multiple threads.
367  **/
368 class YQUISignalReceiver : public QObject
369 {
370  Q_OBJECT
371 
372 public:
374 
375 public slots:
376 
377  void slotBusyCursor();
378  void slotReceivedYCPCommand();
379 };
380 
381 
382 /**
383  * Create a new UI if there is none yet or return the existing one if there is.
384  *
385  * This is the UI plugin's interface to the outside world, so don't change the
386  * name or signature!
387  **/
388 YUI * createUI( bool withThreads );
389 
390 
391 #endif // YQUI_h
bool fullscreen() const
Return &#39;true&#39; if defaultsize windows should use the full screen.
Definition: YQUI.h:166
void receivedYCPCommand()
Notification that a YCP command has been received on fd_ycp to leave idleLoop()
Definition: YQUI.cc:488
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:282
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:562
bool eventPendingFor(YWidget *widget) const
Returns &#39;true&#39; if there is any event pending for the specified widget.
Definition: YQUI.h:139
void setApplicationTitle(const QString &title)
Sets the application name for the window title.
Definition: YQUI.h:281
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
Definition: YQUI.cc:601
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:387
QString applicationTitle()
Returns the application name for the window title (e.g.
Definition: YQUI.h:276
void calcDefaultSize()
Calculate size of opt(defaultsize) dialogs.
Definition: YQUI.cc:406
Helper class that acts as a Qt signal receiver for YQUI.
Definition: YQUI.h:368
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:494
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:455
void probeX11Display(const YCommandLine &cmdLine)
Probe the X11 display.
Definition: YQUI.cc:607
virtual bool eventsBlocked() const
Returns &#39;true&#39; if events are currently blocked.
Definition: YQUI.cc:570
bool noBorder() const
Return &#39;true&#39; if defaultsize windows should not get window manager borders / frames.
Definition: YQUI.h:172
virtual void deleteNotify(YWidget *widget)
Notification that a widget is being deleted.
Definition: YQUI.cc:634
void busyCursor()
Show mouse cursor indicating busy state.
Definition: YQUI.cc:576
Definition: YQUI.h:61
void processCommandLineArgs(int argc, char **argv)
Handle command line args.
Definition: YQUI.cc:288
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:355
virtual void blockEvents(bool block=true)
Block (or unblock) events.
Definition: YQUI.cc:528
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:592
bool fatalError() const
Returns &#39;true&#39; if the UI had a fatal error that requires the application to abort.
Definition: YQUI.h:177
bool close()
Application shutdown.
Definition: YQUI.cc:640
void normalCursor()
Show normal mouse cursor not indicating busy status.
Definition: YQUI.cc:582
void initUI()
Post-constructor initialization.
Definition: YQUI.cc:118
YEvent * pendingEvent() const
Returns the last event that isn&#39;t processed yet or 0 if there is none.
Definition: YQUI.h:147
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:338
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:514
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:80
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:376