60ec96bf96027615a8df2199daea052602e06c26
[duncan/yast2-qt4.git] / src / pkg / YQPatternSelector.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPatternSelector.cc
14   See also:   YQPatternSelectorHelp.cc
15
16   Author:     Stefan Hundhammer <sh@suse.de>
17
18   Textdomain "packages-qt"
19
20 /-*/
21
22 #include <qapplication.h>
23 #include <q3hbox.h>
24 #include <q3header.h>
25 #include <qpushbutton.h>
26 #include <qsplitter.h>
27
28 #define y2log_component "qt-pkg"
29 #include <ycp/y2log.h>
30
31 #include "QY2LayoutUtils.h"
32
33 #include "YQPatternSelector.h"
34 #include "YQPkgConflictDialog.h"
35 #include "YQPkgSelDescriptionView.h"
36 #include "YQPkgDiskUsageList.h"
37 #include "YQPkgPatternList.h"
38 #include "YQPkgSelList.h"
39 #include "YQWizard.h"
40 #include "YQDialog.h"
41
42 #include "utf8.h"
43 #include "YQUI.h"
44 #include "YEvent.h"
45 #include "YQi18n.h"
46
47 #define ALWAYS_SOLVE_IMMEDIATELY 1
48
49 using std::max;
50 using std::string;
51
52 #define SPACING                 6
53 #define MARGIN                  6
54
55
56 YQPatternSelector::YQPatternSelector( YWidget * parent, long modeFlags )
57     : YQPackageSelectorBase( parent, modeFlags )
58 {
59     _patternList                = 0;
60     _selList                    = 0;
61     _descriptionView            = 0;
62     _wizard                     = findWizard();
63
64     basicLayout();
65     makeConnections();
66
67     if ( _patternList )
68     {
69         _patternList->fillList();
70         _patternList->selectSomething();
71     }
72     else if ( _selList )
73     {
74         _selList->fillList();
75         _selList->selectSomething();
76     }
77
78     if ( zyppPool().empty<zypp::Pattern  >() &&
79          zyppPool().empty<zypp::Selection>()   )
80     {
81         y2warning( "Neither patterns nor selections in ZyppPool" );
82     }
83
84
85     if ( _diskUsageList )
86         _diskUsageList->updateDiskUsage();
87 }
88
89
90
91 YQWizard *
92 YQPatternSelector::findWizard() const
93 {
94     YQWizard * wizard = 0;
95
96     YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog() );
97
98     if ( dialog )
99         wizard = dialog->findWizard();
100
101     return wizard;
102 }
103
104
105
106 void
107 YQPatternSelector::basicLayout()
108 {
109     QSplitter * outer_splitter = new QSplitter( Qt::Horizontal, this );
110     Q_CHECK_PTR( outer_splitter );
111
112     QWidget * left_pane  = layoutLeftPane ( outer_splitter );
113     QWidget * right_pane = layoutRightPane( outer_splitter );
114
115     int left_pane_width = (int) ( 0.3 * YQUI::ui()->defaultSize( YD_HORIZ ) );
116     left_pane->resize( QSize( left_pane_width, left_pane->height() ) );
117
118     outer_splitter->setResizeMode( left_pane,  QSplitter::KeepSize );
119     outer_splitter->setResizeMode( right_pane, QSplitter::Stretch  );
120
121     if ( ! _wizard )
122         layoutButtons( this );
123 }
124
125
126
127 QWidget *
128 YQPatternSelector::layoutLeftPane( QWidget * parent )
129 {
130     Q3VBox * vbox = new Q3VBox( parent );
131     Q_CHECK_PTR( vbox );
132     vbox->setMargin( MARGIN );
133
134     if ( ! zyppPool().empty<zypp::Pattern>() )
135     {
136         //
137         // Patterns list
138         //
139
140         _patternList = new YQPkgPatternList( vbox,
141                                              false,     // no autoFill - need to connect to details view first
142                                              false );   // no autoFilter - filterMatch() is not connected
143         Q_CHECK_PTR( _patternList );
144         _patternList->header()->hide();
145     }
146
147     if ( ! _patternList )
148     {
149         //
150         // Fallback: selections list
151         //
152
153         /**
154          * Create a selections list even if there are no selections, otherwise
155          * the layout will look very weird. An empty selections list still
156          * looks better than a lot of grey empty space.
157          **/
158
159         y2warning( "No patterns in ZyppPool - using selections instead" );
160         _selList = new YQPkgSelList( vbox,
161                                      false,     // no autoFill - need to connect to details view first
162                                      false );   // no autoFilter - filterMatch() is not connected
163         Q_CHECK_PTR( _selList );
164         _selList->header()->hide();
165     }
166
167     if ( _wizard )      // No button box - add "Details..." button here
168     {
169         //
170         // "Details" button
171         //
172
173         addVSpacing( vbox, SPACING );
174
175         Q3HBox * hbox = new Q3HBox( vbox );
176         Q_CHECK_PTR( hbox );
177
178         QPushButton * details_button = new QPushButton( _( "&Details..." ), hbox );
179         Q_CHECK_PTR( details_button );
180
181         connect( details_button, SIGNAL( clicked() ),
182                  this,           SLOT  ( detailedPackageSelection() ) );
183
184         addHStretch( hbox );
185     }
186
187     return vbox;
188 }
189
190
191
192 QWidget *
193 YQPatternSelector::layoutRightPane( QWidget * parent )
194 {
195     QSplitter * splitter = new QSplitter( Qt::Vertical, parent );
196     Q_CHECK_PTR( splitter );
197     splitter->setMargin( MARGIN );
198
199
200     //
201     // Selection / Pattern description
202     //
203
204     Q3VBox * upper_vbox = new Q3VBox( splitter );
205     Q_CHECK_PTR( upper_vbox );
206
207     _descriptionView = new YQPkgSelDescriptionView( upper_vbox );
208     Q_CHECK_PTR( _descriptionView );
209
210     addVSpacing( upper_vbox, MARGIN );
211
212
213     //
214     // Disk usage
215     //
216
217     Q3VBox * lower_vbox = new Q3VBox( splitter );
218     Q_CHECK_PTR( lower_vbox );
219     addVSpacing( lower_vbox, MARGIN );
220
221     _diskUsageList = new YQPkgDiskUsageList( lower_vbox );
222     Q_CHECK_PTR( _diskUsageList );
223
224     splitter->setResizeMode( upper_vbox, QSplitter::Stretch );
225     splitter->setResizeMode( lower_vbox, QSplitter::FollowSizeHint );
226
227     return splitter;
228 }
229
230
231
232 void
233 YQPatternSelector::layoutButtons( QWidget * parent )
234 {
235     Q3HBox * button_box = new Q3HBox( parent );
236     Q_CHECK_PTR( button_box );
237     button_box->setMargin ( MARGIN  );
238     button_box->setSpacing( SPACING );
239
240
241     QPushButton * details_button = new QPushButton( _( "&Details..." ), button_box );
242     Q_CHECK_PTR( details_button );
243     details_button->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ); // hor/vert
244
245     connect( details_button,    SIGNAL( clicked() ),
246              this,              SLOT  ( detailedPackageSelection() ) );
247
248
249     addHStretch( button_box );
250
251     QPushButton * cancel_button = new QPushButton( _( "&Cancel" ), button_box );
252     Q_CHECK_PTR( cancel_button );
253     cancel_button->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ); // hor/vert
254
255     connect( cancel_button, SIGNAL( clicked() ),
256              this,          SLOT  ( reject()   ) );
257
258
259     QPushButton * accept_button = new QPushButton( _( "&Accept" ), button_box );
260     Q_CHECK_PTR( accept_button );
261     accept_button->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ); // hor/vert
262
263     connect( accept_button, SIGNAL( clicked() ),
264              this,          SLOT  ( accept()   ) );
265
266     button_box->setFixedHeight( button_box->sizeHint().height() );
267 }
268
269
270
271 void
272 YQPatternSelector::makeConnections()
273 {
274     if ( _patternList )
275     {
276 #if ALWAYS_SOLVE_IMMEDIATELY
277         connect( _patternList,          SIGNAL( statusChanged()         ),
278                  this,                  SLOT  ( resolveDependencies()   ) );
279
280         if ( _pkgConflictDialog )
281         {
282             connect( _pkgConflictDialog, SIGNAL( updatePackages()       ),
283                      _patternList,       SLOT  ( updateItemStates()     ) );
284         }
285 #endif
286
287         if ( _descriptionView )
288         {
289             connect( _patternList,      SIGNAL( selectionChanged( ZyppSel ) ),
290                      _descriptionView,  SLOT  ( showDetails     ( ZyppSel ) ) );
291         }
292
293         if ( _diskUsageList )
294         {
295             connect( _patternList,      SIGNAL( updatePackages()  ),
296                      _diskUsageList,    SLOT  ( updateDiskUsage() ) );
297         }
298
299     }
300
301     if ( _selList )
302     {
303 #if ALWAYS_SOLVE_IMMEDIATELY
304         connect( _selList,              SIGNAL( statusChanged()         ),
305                  this,                  SLOT  ( resolveDependencies()   ) );
306
307         if ( _pkgConflictDialog )
308         {
309             connect( _pkgConflictDialog, SIGNAL( updatePackages()       ),
310                      _selList,           SLOT  ( updateItemStates()     ) );
311         }
312
313 #endif
314
315         if ( _descriptionView )
316         {
317             connect( _selList,          SIGNAL( selectionChanged( ZyppSel ) ),
318                      _descriptionView,  SLOT  ( showDetails     ( ZyppSel ) ) );
319         }
320
321         if ( _diskUsageList )
322         {
323             connect( _selList,          SIGNAL( updatePackages()  ),
324                      _diskUsageList,    SLOT  ( updateDiskUsage() ) );
325         }
326     }
327
328     y2milestone( "Connection set up" );
329
330
331     if ( _wizard )
332     {
333         connect( _wizard,       SIGNAL( nextClicked()   ),
334                  this,          SLOT  ( accept()        ) );
335
336         connect( _wizard,       SIGNAL( backClicked()   ),
337                  this,          SLOT  ( reject()        ) );
338
339         connect( _wizard,       SIGNAL( abortClicked()  ),
340                  this,          SLOT  ( reject()        ) );
341     }
342 }
343
344
345 void
346 YQPatternSelector::detailedPackageSelection()
347 {
348     y2milestone( "\"Details..\" button clicked" );
349     YQUI::ui()->sendEvent( new YMenuEvent( YCPSymbol( "details" ) ) );
350 }
351
352
353 void
354 YQPatternSelector::debugTrace()
355 {
356     y2warning( "debugTrace" );
357 }
358
359
360
361 #include "YQPatternSelector.moc"