- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / pkg / YQPkgPatchFilterView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgPatchFilterView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define y2log_component "qt-pkg"
22 #include <ycp/y2log.h>
23
24 #include <QComboBox>
25 #include <QLabel>
26 #include <QSplitter>
27 #include <QTabWidget>
28 #include <QDateTime>
29 #include <QFrame>
30 #include <QVBoxLayout>
31
32 #include <FSize.h>
33 #include <zypp/ui/PatchContents.h>
34
35 #include "YQPkgPatchFilterView.h"
36 #include "YQPkgPatchList.h"
37 #include "YQPkgDescriptionView.h"
38 #include "YQPkgSelList.h"
39 #include "QY2LayoutUtils.h"
40 #include "YQi18n.h"
41
42 typedef zypp::ui::PatchContents                 ZyppPatchContents;
43 typedef zypp::ui::PatchContents::const_iterator ZyppPatchContentsIterator;
44
45 using std::set;
46
47 #define ENABLE_TOTAL_DOWNLOAD_SIZE      0
48
49 #define SPACING                 6       // between subwidgets
50 #define MARGIN                  4       // around the widget
51
52
53 YQPkgPatchFilterView::YQPkgPatchFilterView( QWidget * parent )
54     : QWidget( parent )
55 {
56     QVBoxLayout *layout = new QVBoxLayout();
57     setLayout(layout);
58     
59
60     _splitter                   = new QSplitter( Qt::Vertical, this );  Q_CHECK_PTR( _splitter  );
61     layout->addWidget(_splitter);
62     QVBoxLayout * vbox = new QVBoxLayout(_splitter);
63     Q_CHECK_PTR( vbox           );
64     layout->addLayout(vbox);
65     _patchList                  = new YQPkgPatchList( this );                   Q_CHECK_PTR( _patchList         );
66     vbox->addWidget(_patchList);
67     
68     //addVSpacing( vbox, 4 );
69
70     QHBoxLayout * hbox          = new QHBoxLayout(); Q_CHECK_PTR( hbox );
71     vbox->addLayout(hbox);
72     hbox->setSpacing( SPACING );
73
74     QLabel * label              = new QLabel( _( "&Show Patch Category:" ), this );
75     hbox->addWidget(label);
76
77     _patchFilter                = new QComboBox( this );
78     Q_CHECK_PTR( _patchFilter );
79     hbox->addWidget(_patchFilter);
80
81     _patchFilter->addItem( _( "Installable Patches" ));
82     _patchFilter->addItem( _( "Installable and Installed Patches" ));
83     _patchFilter->addItem( _( "All Patches" ),                          2 );
84     _patchFilter->setCurrentIndex( 0 );
85     _patchFilter->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
86     label->setBuddy( _patchFilter );
87
88     connect( _patchFilter, SIGNAL( activated( int ) ), this, SLOT( fillPatchList() ) );
89     //addVSpacing( vbox, 4 );
90
91     vbox                        = new QVBoxLayout( _splitter );                 Q_CHECK_PTR( vbox                       );
92     //addVSpacing( vbox, 8 );
93
94     _detailsViews               = new QTabWidget( this );                       Q_CHECK_PTR( _detailsViews      );
95     vbox->addWidget(_detailsViews);
96
97     _descriptionView            = new YQPkgDescriptionView( _detailsViews );    Q_CHECK_PTR( _descriptionView   );
98
99     _descriptionView->setMinimumSize( 0, 0 );
100     _detailsViews->addTab( _descriptionView, _( "Patch Description" ) );
101
102     
103 #if ENABLE_TOTAL_DOWNLOAD_SIZE
104     //
105     // HBox for total download size
106     //
107
108     hbox = new QHBoxLayout(); Q_CHECK_PTR( hbox );
109     vbox->addLayout(hbox);
110     //addHStretch( hbox );
111
112     hbox->addWidget(new QLabel( _( "Estimated Download Size:" ) + " ", this ));
113     _totalDownloadSize          = new QLabel( FSize(0).asString().c_str(), this );
114     hbox->addWidget(_totalDownloadSize);
115     Q_CHECK_PTR( _totalDownloadSize );
116
117
118     // Give the total download size a 3D look
119
120     _totalDownloadSize->setFrameStyle( Q3Frame::Panel | Q3Frame::Sunken );
121     _totalDownloadSize->setLineWidth(1);
122     _totalDownloadSize->setMidLineWidth(2);
123 #endif
124
125
126     connect( _patchList,        SIGNAL( currentItemChanged    ( ZyppSel ) ),
127              _descriptionView,  SLOT  ( showDetailsIfVisible( ZyppSel ) ) );
128
129     connect( _patchList,        SIGNAL( statusChanged()                 ),
130              this,              SLOT  ( updateTotalDownloadSize()       ) );
131
132     updateTotalDownloadSize();
133 }
134
135
136 YQPkgPatchFilterView::~YQPkgPatchFilterView()
137 {
138     // NOP
139 }
140
141
142 void
143 YQPkgPatchFilterView::updateTotalDownloadSize()
144 {
145     set<ZyppSel> selectablesToInstall;
146     QTime calcTime;
147     calcTime.start();
148
149     for ( ZyppPoolIterator patches_it = zyppPatchesBegin();
150           patches_it != zyppPatchesEnd();
151           ++patches_it )
152     {
153         ZyppPatch patch = tryCastToZyppPatch( (*patches_it)->theObj() );
154
155         if ( patch )
156         {
157             ZyppPatchContents patchContents( patch );
158
159             for ( ZyppPatchContentsIterator contents_it = patchContents.begin();
160                   contents_it != patchContents.end();
161                   ++contents_it )
162             {
163                 ZyppPkg pkg = tryCastToZyppPkg( *contents_it );
164                 ZyppSel sel;
165
166                 if ( pkg )
167                     sel = _selMapper.findZyppSel( pkg );
168
169                 if ( sel )
170                 {
171                     switch ( sel->status() )
172                     {
173                         case S_Install:
174                         case S_AutoInstall:
175                         case S_Update:
176                         case S_AutoUpdate:
177                             // Insert the patch contents selectables into a set,
178                             // don't immediately sum up their sizes: The same
179                             // package could be in more than one patch, but of
180                             // course it will be downloaded only once.
181
182                             selectablesToInstall.insert( sel );
183                             break;
184
185                         case S_Del:
186                         case S_AutoDel:
187                         case S_NoInst:
188                         case S_KeepInstalled:
189                         case S_Taboo:
190                         case S_Protected:
191                             break;
192
193                             // intentionally omitting 'default' branch so the compiler can
194                             // catch unhandled enum states
195                     }
196
197                 }
198             }
199         }
200     }
201
202
203     FSize totalSize = 0;
204
205     for ( set<ZyppSel>::iterator it = selectablesToInstall.begin();
206           it != selectablesToInstall.end();
207           ++it )
208     {
209         if ( (*it)->candidateObj() )
210             totalSize += (*it)->candidateObj()->size();
211     }
212
213 #if ENABLE_TOTAL_DOWNLOAD_SIZE
214     _totalDownloadSize->setText( totalSize.asString().c_str() );
215 #endif
216
217     y2debug( "Calculated total download size in %d millisec", calcTime.elapsed() );
218 }
219
220
221 void
222 YQPkgPatchFilterView::fillPatchList()
223 {
224     switch ( _patchFilter->currentIndex() )
225     {
226         case 0:         _patchList->setFilterCriteria( YQPkgPatchList::RelevantPatches             );   break;
227         case 1:         _patchList->setFilterCriteria( YQPkgPatchList::RelevantAndInstalledPatches );   break;
228         case 2:         _patchList->setFilterCriteria( YQPkgPatchList::AllPatches                  );   break;
229         default:        _patchList->setFilterCriteria( YQPkgPatchList::RelevantPatches             );   break;
230     }
231
232     _patchList->fillList();
233     _patchList->selectSomething();
234 }
235
236
237 #include "YQPkgPatchFilterView.moc"