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