]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQMainWinDock.cc
one theory what it can be
[duncan/yast2-qt4.git] / src / YQMainWinDock.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQMainWinDock.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22 #include <QTimer>
23 #include <QResizeEvent>
24 #include <YDialog.h>
25 #include <YQUI.h>
26 #include <YEvent.h>
27 #include "YQMainWinDock.h"
28
29
30
31 YQMainWinDock *
32 YQMainWinDock::mainWinDock()
33 {
34     static YQMainWinDock * mainWinDock = 0;
35
36     if ( ! mainWinDock )
37         mainWinDock = new YQMainWinDock();
38
39     return mainWinDock;
40 }
41
42
43 YQMainWinDock::YQMainWinDock()
44     : QWidget( 0, // parent, name
45                YQUI::ui()->noBorder() ?
46                Qt::FramelessWindowHint :
47                Qt::Window )
48 {
49     setWindowTitle( "YaST2" );
50
51     setFocusPolicy( Qt::StrongFocus );
52
53     resize( YQUI::ui()->defaultSize( YD_HORIZ ),
54             YQUI::ui()->defaultSize( YD_VERT  ) );
55
56     y2debug( "MainWinDock initial size: %d x %d",
57              size().width(), size().height() );
58 }
59
60
61 YQMainWinDock::~YQMainWinDock()
62 {
63     // NOP
64 }
65
66
67 void
68 YQMainWinDock::resizeEvent( QResizeEvent * event )
69 {
70     if ( event )
71     {
72         resize( event->size() );
73         resizeVisibleChild();
74     }
75 }
76
77
78 void
79 YQMainWinDock::resizeVisibleChild()
80 {
81     if ( ! _widgetStack.empty() )
82     {
83         QWidget * dialog = _widgetStack.back();
84
85         if ( dialog->size() != size() )
86         {
87             // y2debug( "Resizing child dialog %p to %d x %d", dialog, size().width(), size().height() );
88             dialog->resize( size() );
89         }
90     }
91 }
92
93
94 void
95 YQMainWinDock::show()
96 {
97     QWidget::show();
98
99     if ( ! _widgetStack.empty() )
100     {
101         QWidget * dialog = _widgetStack.back();
102         dialog->raise();
103         dialog->show();
104     }
105 }
106
107
108 void
109 YQMainWinDock::add( QWidget * dialog )
110 {
111     YUI_CHECK_PTR( dialog );
112
113     if ( !_widgetStack.empty() )
114         _widgetStack.back()->hide();
115
116     dialog->raise();
117     dialog->show();
118
119     y2debug( "Adding dialog %p to mainWinDock", dialog );
120     _widgetStack.push_back( dialog );
121     resizeVisibleChild();
122
123     show();
124 }
125
126
127 void
128 YQMainWinDock::showCurrentDialog()
129 {
130     if ( ! _widgetStack.empty() )
131     {
132         QWidget * dialog = _widgetStack.back();
133         y2debug( "Showing dialog %p", dialog );
134         dialog->raise();
135         update();
136     }
137 }
138
139
140 void
141 YQMainWinDock::remove( QWidget * dialog )
142 {
143     if ( _widgetStack.empty() )
144         return;
145
146     if ( ! dialog )
147         dialog = _widgetStack.back();
148
149     if ( dialog == _widgetStack.back() )
150     {
151         // The most common case:
152         // The topmost dialog is to be removed
153
154         _widgetStack.pop_back();
155
156         y2debug( "Removing dialog %p from mainWinDock", dialog );
157     }
158     else // The less common (but more generic) case: Remove any dialog
159     {
160         YQMainWinDock::YQWidgetStack::iterator pos = findInStack( dialog );
161
162         if ( pos == _widgetStack.end() )
163             return;
164
165         y2warning( "Found dialog somewhere in the middle of the widget stack" );
166         y2debug( "Removing dialog %p from mainWinDock", dialog );
167
168         _widgetStack.erase( pos );
169     }
170
171     if ( _widgetStack.empty() )         // No more main dialog?
172         hide();                         // -> hide dock
173     else
174     {
175         dialog = _widgetStack.back();   // Get the next dialog from the stack
176         dialog->raise();                // and raise it
177         dialog->show();
178         resizeVisibleChild();
179     }
180 }
181
182
183 YQMainWinDock::YQWidgetStack::iterator
184 YQMainWinDock::findInStack( QWidget * dialog )
185 {
186     for ( YQMainWinDock::YQWidgetStack::iterator it = _widgetStack.begin();
187           it != _widgetStack.end();
188           ++it )
189     {
190         if ( *it == dialog )
191             return it;
192     }
193
194     return _widgetStack.end();
195 }
196
197
198 QWidget *
199 YQMainWinDock::topmostDialog() const
200 {
201     if ( _widgetStack.empty() )
202         return 0;
203     else
204         return _widgetStack.back();
205 }
206
207
208 bool
209 YQMainWinDock::couldDock()
210 {
211     YDialog * topDialog = YDialog::topmostDialog( false ); // don't throw
212
213     if ( ! topDialog )  // No dialog at all?
214         return true;    // Can dock the next one without problems
215
216     // The next dialog can be docked if there is no popup dialog currently open.
217     // This is equivalent to the topmost dialog on the YDialog stack being the
218     // same dialog as the topmost dialog of this MainWinDock.
219
220     return topDialog->widgetRep() == this->topmostDialog();
221 }
222
223
224 void
225 YQMainWinDock::closeEvent( QCloseEvent * event )
226 {
227     // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
228     // handled just like the user had clicked on the `id`( `cancel ) button in
229     // that dialog. It's up to the YCP application to handle this (if desired).
230
231     y2milestone( "Caught window manager close event - returning with YCancelEvent" );
232     event->ignore();
233     YQUI::ui()->sendEvent( new YCancelEvent() );
234 }
235
236
237 void
238 YQMainWinDock::paintEvent( QPaintEvent * event )
239 {
240     // NOP
241 }
242
243
244 #include "YQMainWinDock.moc"