]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQUI_x11.cc
follow the widget state - for reference: you don't
[duncan/yast2-qt4.git] / src / YQUI_x11.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:         YUIQt_x11.cc
14
15   Author:       Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21
22 #include <qevent.h>
23 #include <qcursor.h>
24 #include <q3widgetstack.h>
25 #include <q3vbox.h>
26 #include <qwidget.h>
27 #include <qtextcodec.h>
28 #include <qregexp.h>
29 #include <qlocale.h>
30 #include <qmessagebox.h>
31 #include <QtGui/qdesktopwidget.h>
32
33
34 #define y2log_component "qt-ui"
35 #include <ycp/y2log.h>
36
37 #include "YEvent.h"
38 #include "YQDialog.h"
39 #include "YQi18n.h"
40 #include "QY2Settings.h"
41 #include "YQUI.h"
42
43 // Include low-level X headers AFTER Qt headers:
44 // X.h pollutes the global namespace (!!!) with pretty useless #defines
45 // like "Above", "Below" etc. that clash with some Qt headers.
46 #include <X11/Xlib.h>
47
48
49 int YQUI::getDisplayWidth()
50 {
51     return qApp->desktop()->width();
52 }
53
54
55 int YQUI::getDisplayHeight()
56 {
57     return qApp->desktop()->height();
58 }
59
60
61 int YQUI::getDisplayDepth()
62 {
63     return qApp->desktop()->depth();
64 }
65
66
67 long YQUI::getDisplayColors()
68 {
69     return 1L << qApp->desktop()->depth();
70 }
71
72
73 int YQUI::getDefaultWidth()
74 {
75     return _default_size.width();
76 }
77
78
79 int YQUI::getDefaultHeight()
80 {
81     return _default_size.height();
82 }
83
84
85 int YQUI::defaultSize(YUIDimension dim) const
86 {
87     if ( haveWM() )
88         return dim == YD_HORIZ ? _default_size.width() : _default_size.height();
89     else
90         return dim == YD_HORIZ ? qApp->desktop()->width() : qApp->desktop()->height();
91 }
92
93
94 QWidget* YQUI::mainWidget()
95 {
96     return _main_win;
97 };
98
99
100 void YQUI::beep()
101 {
102     qApp->beep();
103 }
104
105
106 void
107 YQUI::busyCursor( void )
108 {
109     qApp->setOverrideCursor( Qt::BusyCursor );
110 }
111
112
113 void
114 YQUI::normalCursor( void )
115 {
116     if ( _busy_cursor_timer->isActive() )
117         _busy_cursor_timer->stop();
118
119     while ( qApp->overrideCursor() )
120         qApp->restoreOverrideCursor();
121 }
122
123
124 void YQUI::toggleVisionImpairedPalette()
125 {
126     if ( _usingVisionImpairedPalette )
127     {
128         qApp->setPalette( normalPalette(),
129                           true );  // informWidgets
130
131         _usingVisionImpairedPalette = false;
132     }
133     else
134     {
135         qApp->setPalette( visionImpairedPalette(),
136                           true );  // informWidgets
137
138         _usingVisionImpairedPalette = true;
139     }
140 }
141
142
143 QPalette YQUI::visionImpairedPalette()
144 {
145     const QColor dark  ( 0x20, 0x20, 0x20 );
146
147     QColorGroup activeCg;       // for the active window (the one with the keyboard focus)
148
149     activeCg.setColor( QColorGroup::Background,         Qt::black       );
150     activeCg.setColor( QColorGroup::Foreground,         Qt::cyan        );
151     activeCg.setColor( QColorGroup::Text,               Qt::cyan        );
152     activeCg.setColor( QColorGroup::Base,               dark            );
153     activeCg.setColor( QColorGroup::Button,             dark            );
154     activeCg.setColor( QColorGroup::ButtonText,         Qt::green       );
155     activeCg.setColor( QColorGroup::Highlight,          Qt::yellow      );
156     activeCg.setColor( QColorGroup::HighlightedText,    Qt::black       );
157
158     QColorGroup inactiveCg;     // for other windows (those that don't have the keyboard focus)
159
160     inactiveCg.setColor( QColorGroup::Background,       Qt::black       );
161     inactiveCg.setColor( QColorGroup::Foreground,       Qt::cyan        );
162     inactiveCg.setColor( QColorGroup::Text,             Qt::cyan        );
163     inactiveCg.setColor( QColorGroup::Base,             dark            );
164     inactiveCg.setColor( QColorGroup::Button,           dark            );
165     inactiveCg.setColor( QColorGroup::ButtonText,       Qt::green       );
166
167     QColorGroup disabledCg;     // for disabled widgets
168
169     disabledCg.setColor( QColorGroup::Background,       Qt::black       );
170     disabledCg.setColor( QColorGroup::Foreground,       Qt::gray        );
171     disabledCg.setColor( QColorGroup::Text,             Qt::gray        );
172     disabledCg.setColor( QColorGroup::Base,             dark            );
173     disabledCg.setColor( QColorGroup::Button,           dark            );
174     disabledCg.setColor( QColorGroup::ButtonText,       Qt::gray        );
175
176     QPalette pal( activeCg, disabledCg, inactiveCg );
177
178     return pal;
179 }
180
181
182 bool YQUI::close()
183 {
184     sendEvent( new YCancelEvent() );
185     return true;
186 }
187
188
189 bool YQUI::eventFilter( QObject * obj, QEvent * ev )
190 {
191     if ( ev->type() == QEvent::Close )
192     {
193         // Handle WM_CLOSE - but only if it comes from a dialog that is managed by the UI,
194         // not from an independent Qt dialog (such as the package selector popups)
195
196         QWidget * objDialog = 0;
197
198         if ( obj && obj->isWidgetType() )
199         {
200             objDialog = (QWidget *) obj;
201             objDialog = objDialog->topLevelWidget();
202         }
203
204         if ( objDialog &&
205              ( objDialog == mainWidget() ||
206                objDialog == (QObject *) YDialog::currentDialog()->widgetRep() ) )
207         {
208             emit wmClose();
209
210             if ( ! _wm_close_blocked )
211             {
212                 // Don't simply close the application window, return from UserInput()
213                 // with `id(`cancel) and let the YCP application decide how to handle
214                 // that (e.g., ask for confirmation).
215
216                 y2debug( "Caught window close event - returning with `cancel" );
217                 sendEvent( new YCancelEvent() );
218             }
219
220             return true;        // Event processed
221         }
222     }
223     else if ( ev->type() == QEvent::Show )
224     {
225         if ( obj == _main_win )
226         {
227 #ifdef FIXME_PROBABLY_OBSOLETE
228             if ( _widget_stack->count() > 0 )
229             {
230                 // Work around QWidgetStack bug: The last raiseWidget() call
231                 // (from closeDialog() ) might have failed if the widget was
232                 // invisible at that time, e.g., because the user had switched to
233                 // some other virtual desktop (bugzilla bug #11310)
234
235                 _widget_stack->setCurrentWidget( whatever );
236             }
237 #endif
238         }
239         else
240         {
241             return showEventFilter( obj, ev );
242         }
243     }
244     return QObject::eventFilter( obj, ev );
245 }
246
247
248 bool YQUI::showEventFilter( QObject * obj, QEvent * ev )
249 {
250     if ( ! haveWM() )
251     {
252         // Make sure newly opened windows get the keyboard focus even without a
253         // window manager. Otherwise the app might be unusable without a mouse.
254
255         QWidget * widget = dynamic_cast<QWidget *> (obj);
256
257         if ( widget )
258             widget->setActiveWindow();
259     }
260
261     return false;       // Don't stop event processing
262 }
263
264
265 /**
266  * UI-specific conversion from logical layout spacing units (80x25)
267  * to device dependent units (640x480).
268  **/
269 int YQUI::deviceUnits( YUIDimension dim, float size )
270 {
271     if ( dim==YD_HORIZ )        size *= ( 640.0/80 );
272     else                        size *= ( 480.0/25 );
273
274     return (int) ( size + 0.5 );
275 }
276
277
278 /**
279  * Default conversion from device dependent layout spacing units (640x480)
280  * to logical layout units (80x25).
281  *
282  * This default function assumes 80x25 units.
283  * Derived UIs may want to reimplement this.
284  **/
285 float YQUI::layoutUnits( YUIDimension dim, int device_units )
286 {
287     float size = (float) device_units;
288
289     if ( dim==YD_HORIZ )        size *= ( 80/640.0 );
290     else                        size *= ( 25/480.0 );
291
292     return size;
293 }
294
295
296 void YQUI::maybeLeftHandedUser()
297 {
298     if ( _askedForLeftHandedMouse )
299         return;
300
301
302     QString message =
303         _( "You clicked the right mouse button "
304            "where a left-click was expected."
305            "\n"
306            "Switch left and right mouse buttons?"
307            );
308     int button = QMessageBox::question( 0,
309                                         // Popup dialog caption
310                                         _( "Unexpected Click" ),
311                                         message,
312                                         QMessageBox::Yes | QMessageBox::Default,
313                                         QMessageBox::No,
314                                         QMessageBox::Cancel | QMessageBox::Escape );
315
316     if ( button == QMessageBox::Yes )
317     {
318
319         const char * command = 
320             _leftHandedMouse ?
321             "xmodmap -e \"pointer = 1 2 3\"":   // switch back to right-handed mouse
322             "xmodmap -e \"pointer = 3 2 1\"";   // switch to left-handed mouse
323
324         _leftHandedMouse         = ! _leftHandedMouse;  // might be set repeatedly!
325         _askedForLeftHandedMouse = false;       // give the user a chance to switch back
326         y2milestone( "Switching mouse buttons: %s", command );
327         
328         system( command );
329     }
330     else if ( button == 1 )     // No
331     {
332         _askedForLeftHandedMouse = true;
333     }
334 }
335
336
337
338 // EOF