simplify build requires
[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(this);
57     
58
59     _splitter                   = new QSplitter( Qt::Vertical, this );  Q_CHECK_PTR( _splitter  );
60     layout->addWidget(_splitter);
61     QVBoxLayout * vbox = new QVBoxLayout(_splitter);
62     Q_CHECK_PTR( vbox           );
63     layout->addLayout(vbox);
64     _patchList                  = new YQPkgPatchList( this );                   Q_CHECK_PTR( _patchList         );
65     vbox->addWidget(_patchList);
66     
67     //addVSpacing( vbox, 4 );
68
69     QHBoxLayout * hbox          = new QHBoxLayout( this ); Q_CHECK_PTR( hbox );
70     vbox->addLayout(hbox);
71     hbox->setSpacing( SPACING );
72
73     QLabel * label              = new QLabel( _( "&Show Patch Category:" ), this );
74     hbox->addWidget(label);
75
76     _patchFilter                = new QComboBox( this );
77     Q_CHECK_PTR( _patchFilter );
78     hbox->addWidget(_patchFilter);
79
80     _patchFilter->addItem( _( "Installable Patches" ));
81     _patchFilter->addItem( _( "Installable and Installed Patches" ));
82     _patchFilter->addItem( _( "All Patches" ),                          2 );
83     _patchFilter->setCurrentIndex( 0 );
84     _patchFilter->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
85     label->setBuddy( _patchFilter );
86
87     connect( _patchFilter, SIGNAL( activated( int ) ), this, SLOT( fillPatchList() ) );
88     //addVSpacing( vbox, 4 );
89
90     vbox                        = new QVBoxLayout( _splitter );                 Q_CHECK_PTR( vbox                       );
91     //addVSpacing( vbox, 8 );
92
93     _detailsViews               = new QTabWidget( this );                       Q_CHECK_PTR( _detailsViews      );
94     vbox->addWidget(_detailsViews);
95
96     _descriptionView            = new YQPkgDescriptionView( _detailsViews );    Q_CHECK_PTR( _descriptionView   );
97
98     _descriptionView->setMinimumSize( 0, 0 );
99     _detailsViews->addTab( _descriptionView, _( "Patch Description" ) );
100
101     
102 #if ENABLE_TOTAL_DOWNLOAD_SIZE
103     //
104     // HBox for total download size
105     //
106
107     hbox = new QHBoxLayout( this ); Q_CHECK_PTR( hbox );
108     vbox->addLayout(hbox);
109     //addHStretch( hbox );
110
111     hbox->addWidget(new QLabel( _( "Estimated Download Size:" ) + " ", this ));
112     _totalDownloadSize          = new QLabel( FSize(0).asString().c_str(), this );
113     hbox->addWidget(_totalDownloadSize);
114     Q_CHECK_PTR( _totalDownloadSize );
115
116
117     // Give the total download size a 3D look
118
119     _totalDownloadSize->setFrameStyle( Q3Frame::Panel | Q3Frame::Sunken );
120     _totalDownloadSize->setLineWidth(1);
121     _totalDownloadSize->setMidLineWidth(2);
122 #endif
123
124
125     connect( _patchList,        SIGNAL( currentItemChanged    ( ZyppSel ) ),
126              _descriptionView,  SLOT  ( showDetailsIfVisible( ZyppSel ) ) );
127
128     connect( _patchList,        SIGNAL( statusChanged()                 ),
129              this,              SLOT  ( updateTotalDownloadSize()       ) );
130
131     updateTotalDownloadSize();
132 }
133
134
135 YQPkgPatchFilterView::~YQPkgPatchFilterView()
136 {
137     // NOP
138 }
139
140
141 void
142 YQPkgPatchFilterView::updateTotalDownloadSize()
143 {
144     set<ZyppSel> selectablesToInstall;
145     QTime calcTime;
146     calcTime.start();
147
148     for ( ZyppPoolIterator patches_it = zyppPatchesBegin();
149           patches_it != zyppPatchesEnd();
150           ++patches_it )
151     {
152         ZyppPatch patch = tryCastToZyppPatch( (*patches_it)->theObj() );
153
154         if ( patch )
155         {
156             ZyppPatchContents patchContents( patch );
157
158             for ( ZyppPatchContentsIterator contents_it = patchContents.begin();
159                   contents_it != patchContents.end();
160                   ++contents_it )
161             {
162                 ZyppPkg pkg = tryCastToZyppPkg( *contents_it );
163                 ZyppSel sel;
164
165                 if ( pkg )
166                     sel = _selMapper.findZyppSel( pkg );
167
168                 if ( sel )
169                 {
170                     switch ( sel->status() )
171                     {
172                         case S_Install:
173                         case S_AutoInstall:
174                         case S_Update:
175                         case S_AutoUpdate:
176                             // Insert the patch contents selectables into a set,
177                             // don't immediately sum up their sizes: The same
178                             // package could be in more than one patch, but of
179                             // course it will be downloaded only once.
180
181                             selectablesToInstall.insert( sel );
182                             break;
183
184                         case S_Del:
185                         case S_AutoDel:
186                         case S_NoInst:
187                         case S_KeepInstalled:
188                         case S_Taboo:
189                         case S_Protected:
190                             break;
191
192                             // intentionally omitting 'default' branch so the compiler can
193                             // catch unhandled enum states
194                     }
195
196                 }
197             }
198         }
199     }
200
201
202     FSize totalSize = 0;
203
204     for ( set<ZyppSel>::iterator it = selectablesToInstall.begin();
205           it != selectablesToInstall.end();
206           ++it )
207     {
208         if ( (*it)->candidateObj() )
209             totalSize += (*it)->candidateObj()->size();
210     }
211
212 #if ENABLE_TOTAL_DOWNLOAD_SIZE
213     _totalDownloadSize->setText( totalSize.asString().c_str() );
214 #endif
215
216     y2debug( "Calculated total download size in %d millisec", calcTime.elapsed() );
217 }
218
219
220 void
221 YQPkgPatchFilterView::fillPatchList()
222 {
223     switch ( _patchFilter->currentIndex() )
224     {
225         case 0:         _patchList->setFilterCriteria( YQPkgPatchList::RelevantPatches             );   break;
226         case 1:         _patchList->setFilterCriteria( YQPkgPatchList::RelevantAndInstalledPatches );   break;
227         case 2:         _patchList->setFilterCriteria( YQPkgPatchList::AllPatches                  );   break;
228         default:        _patchList->setFilterCriteria( YQPkgPatchList::RelevantPatches             );   break;
229     }
230
231     _patchList->fillList();
232     _patchList->selectSomething();
233 }
234
235
236 #include "YQPkgPatchFilterView.moc"