]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgVersionsView.cc
- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / pkg / YQPkgVersionsView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgVersionsView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define y2log_component "qt-pkg"
22
23 #include <YQZypp.h>
24 #include <zypp/Repository.h>
25 #include <ycp/y2log.h>
26 #include <QTabWidget>
27 #include <QRegExp>
28 #include <QHeaderView>
29
30 #include "YQPkgVersionsView.h"
31 #include "YQPkgRepoList.h"
32 #include "YQIconPool.h"
33 #include "YQi18n.h"
34 #include "utf8.h"
35
36
37 YQPkgVersionsView::YQPkgVersionsView( QWidget * parent, bool userCanSwitch )
38     : QY2ListView( parent )
39 {
40     _selectable         = 0;
41     _parentTab          = dynamic_cast<QTabWidget *> (parent);
42     _userCanSwitch      = userCanSwitch;
43
44     _versionCol = -42;
45     _archCol    = -42;
46     _productCol = -42;
47     _urlCol     = -42;
48     _repoCol    = -42;
49     _statusCol  = -42;
50     _nameCol    = -42;
51     _summaryCol = -42;
52
53 #if FIXME
54     int numCol = 0;
55     addColumn( _( "Version"     ) );    _versionCol     = numCol++;
56     addColumn( _( "Arch."       ) );    _archCol        = numCol++;
57     addColumn( _( "Product"     ) );    _productCol     = numCol++;
58     addColumn( _( "Repository"  ) );    _repoCol        = numCol++;
59     addColumn( _( "URL"         ) );    _urlCol         = numCol++;
60 #endif
61     _statusCol  = _productCol;
62
63     _nameCol    = _versionCol;
64     _summaryCol = _repoCol;
65
66     setSortByInsertionSequence( true );
67
68     // saveColumnWidths();      // Minimize column widths
69     // header()->hide();
70
71
72     if ( _parentTab )
73     {
74         connect( parent, SIGNAL( currentChanged(QWidget *) ),
75                  this,   SLOT  ( reload        (QWidget *) ) );
76     }
77
78     connect( this,      SIGNAL( currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ) ),
79              this,      SLOT  ( checkForChangedCandidate() ) );
80 }
81
82
83 YQPkgVersionsView::~YQPkgVersionsView()
84 {
85     // NOP
86 }
87
88
89 void
90 YQPkgVersionsView::reload( QWidget * newCurrent )
91 {
92     if ( newCurrent == this )
93         showDetailsIfVisible( _selectable );
94 }
95
96
97 void
98 YQPkgVersionsView::showDetailsIfVisible( ZyppSel selectable )
99 {
100     _selectable = selectable;
101
102     if ( _parentTab )           // Is this view embedded into a tab widget?
103     {
104         if ( _parentTab->currentWidget() == this )  // Is this page the topmost?
105             showDetails( selectable );
106     }
107     else        // No tab parent - simply show data unconditionally.
108     {
109         showDetails( selectable );
110     }
111 }
112
113
114 void
115 YQPkgVersionsView::showDetails( ZyppSel selectable )
116 {
117     _selectable = selectable;
118     clear();
119
120     if ( ! selectable )
121         return;
122
123 #if FIXME
124     QY2CheckListItem * root = new QY2CheckListItem( this, selectable->theObj()->name().c_str() );
125     //FIXME add element
126     Q_CHECK_PTR( root );
127     root->setExpanded( true );
128
129     bool installedIsAvailable = false;
130
131     zypp::ui::Selectable::available_iterator it = selectable->availableBegin();
132
133     while ( it != selectable->availableEnd() )
134     {
135         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
136
137         if ( selectable->installedObj() &&
138              selectable->installedObj()->edition() == (*it)->edition() &&
139              selectable->installedObj()->arch()    == (*it)->arch()      )
140             // FIXME: In future releases, also the vendor will make a difference
141             installedIsAvailable = true;
142
143 #if 0
144         // DEBUG
145         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
146         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
147         new YQPkgVersion( this, root, selectable, *it, _userCanSwitch );
148         // DEBUG
149 #endif
150         ++it;
151     }
152
153     if ( selectable->hasInstalledObj() && ! installedIsAvailable )
154         new YQPkgVersion( this, root, selectable, selectable->installedObj(), false );
155
156 #endif
157 }
158
159
160 void
161 YQPkgVersionsView::checkForChangedCandidate()
162 {
163     QTreeWidgetItemIterator iter(this);
164     QTreeWidgetItem *first = *iter;
165 #if FIXME
166     if ( ! first || ! _selectable )
167         return;
168
169     
170     QTreeWidgetItemIterator iter_c(first);
171     
172     while ( *iter_c )
173     {
174         YQPkgVersion * versionItem = dynamic_cast<YQPkgVersion *> (*iter_c);
175
176         if ( versionItem && versionItem->isOn() )
177         {
178             ZyppObj newCandidate = versionItem->zyppObj();
179
180             if ( newCandidate != _selectable->candidateObj() )
181             {
182                 y2milestone( "Candidate changed" );
183
184                 // Change status of selectable
185
186                 ZyppStatus status = _selectable->status();
187
188                 if ( _selectable->installedObj() &&
189                      _selectable->installedObj()->arch()    == newCandidate->arch() &&
190                      _selectable->installedObj()->edition() == newCandidate->edition() )
191                 {
192                     // Switch back to the original instance -
193                     // the version that was previously installed
194                     status = S_KeepInstalled;
195                 }
196                 else
197                 {
198                     switch ( status )
199                     {
200                         case S_KeepInstalled:
201                         case S_Protected:
202                         case S_AutoDel:
203                         case S_AutoUpdate:
204                         case S_Del:
205                         case S_Update:
206
207                             status = S_Update;
208                             break;
209
210                         case S_NoInst:
211                         case S_Taboo:
212                         case S_Install:
213                         case S_AutoInstall:
214                             status = S_Install;
215                             break;
216                     }
217                 }
218
219                 _selectable->set_status( status );
220
221
222                 // Set candidate
223
224                 _selectable->setCandidate( newCandidate );
225                 emit candidateChanged( newCandidate );
226                 return;
227             }
228         }
229
230         ++iter_c;
231     }
232 #endif
233 }
234
235
236 QSize
237 YQPkgVersionsView::minimumSizeHint() const
238 {
239     return QSize( 0, 0 );
240 }
241
242
243
244
245
246
247 YQPkgVersion::YQPkgVersion( YQPkgVersionsView * pkgVersionList,
248                             QY2CheckListItem *  parent,
249                             ZyppSel             selectable,
250                             ZyppObj             zyppObj,
251                             bool                enabled )
252     : QY2CheckListItem( parent, "" /*
253                         enabled ?
254                         Q3CheckListItem::RadioButton :
255                         Q3CheckListItem::Controller */ )        // cheap way to make it read-only
256     , _pkgVersionList( pkgVersionList )
257     , _selectable( selectable )
258     , _zyppObj( zyppObj )
259 {
260     // FIXME setOn( _zyppObj == _selectable->candidateObj() );
261
262     if ( versionCol() >= 0 )    setText( versionCol(), zyppObj->edition().asString().c_str() );
263     if ( archCol()    >= 0 )    setText( archCol(),    zyppObj->arch().asString().c_str() );
264     if ( repoCol() >= 0 )       setText( repoCol(), fromUTF8(zyppObj->repository().info().alias()) );
265     if ( productCol() >= 0 )
266     {
267         ZyppProduct product = YQPkgRepoListItem::singleProduct( zyppObj->repository() );
268
269         if ( product )
270             setText( productCol(), QString::fromStdString( product->summary() ) );
271     }
272     if ( urlCol() >= 0 )
273     {
274         zypp::Url repoUrl;
275
276         if ( ! zyppObj->repository().info().baseUrlsEmpty() )
277             repoUrl = *zyppObj->repository().info().baseUrlsBegin();
278
279         setText( urlCol(), repoUrl.asString().c_str() );
280     }
281
282     if ( _selectable->hasInstalledObj() )
283     {
284         if ( _zyppObj->edition() == _selectable->installedObj()->edition() &&
285              _zyppObj->arch()    == _selectable->installedObj()->arch()      )
286         {
287             // FIXME setPixmap( statusCol(), YQIconPool::pkgKeepInstalled() );
288             setBackgroundColor( QColor( 0xF0, 0xF0, 0xF0 ) );   // light grey
289             setTextColor( QColor( 0, 0x90, 0 ) );               // green
290         }
291     }
292 }
293
294
295 YQPkgVersion::~YQPkgVersion()
296 {
297     // NOP
298 }
299
300
301 QString
302 YQPkgVersion::toolTip(int)
303 {
304     QString tip;
305
306     if ( zyppObj() == selectable()->installedObj() )
307         tip = _( "This version is installed in your system." );
308
309     return tip;
310 }
311
312
313 bool YQPkgVersion::operator< ( const QTreeWidgetItem & otherListViewItem ) const
314 {
315     const YQPkgVersion * other = dynamic_cast<const YQPkgVersion *> (&otherListViewItem);
316
317     if ( other )
318     {
319         return ( this->zyppObj()->edition() < other->zyppObj()->edition() );
320     }
321
322     // Fallback: Use parent class method
323     return QY2CheckListItem::operator<( otherListViewItem );
324 }
325
326
327
328 #include "YQPkgVersionsView.moc"