]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgPatternList.cc
make the layout half way working at least
[duncan/yast2-qt4.git] / src / pkg / YQPkgPatternList.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgPatternList.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define QT3_SUPPORT 1
22
23 #define y2log_component "qt-pkg"
24 #include <ycp/y2log.h>
25 #include <QRegExp>
26 #include <zypp/ZYppFactory.h>
27 #include <zypp/Resolver.h>
28 #include <QPainter>
29 #include <QHeaderView>
30 #include <zypp/ui/PatternContents.h>
31
32 #include "YQi18n.h"
33 #include "utf8.h"
34 #include "YQPkgPatternList.h"
35 #include "YQIconPool.h"
36 #include "YQApplication.h"
37 #include "YQUI.h"
38
39
40 #define CATEGORY_BACKGROUND     QColor( 0xFF, 0xC0, 0x50 )
41
42
43 YQPkgPatternList::YQPkgPatternList( QWidget * parent, bool autoFill, bool autoFilter )
44     : YQPkgObjList( parent )
45 {
46     y2debug( "Creating pattern list" );
47
48     int numCol = 0;
49 #if FIXME
50     QStringList headers;
51     headers << "";      _statusCol      = numCol++;
52
53     // Translators: "Pattern" refers to so-called "installation patterns",
54     // i.e., specific task-oriented groups of packages, like "everything that
55     // is needed to run a web server". The idea of patterns is that they also
56     // include the configuration workflow needed for that task, such of
57     // configuring the web server. For the scope of the package selector, this
58     // is only of little relevance, though.
59
60     headers << _( "Pattern" );  _summaryCol     = numCol++;
61
62     setHeaderLabels(headers);
63     // Can use the same colum for "broken" and "satisfied":
64     // Both states are mutually exclusive
65
66     _satisfiedIconCol   = _summaryCol;
67     _brokenIconCol      = _summaryCol;
68
69     header()->setStretchEnabled( _statusCol , false );
70     header()->setStretchEnabled( _summaryCol, true  );
71
72     setAllColumnsShowFocus( true );
73     setTreeStepSize( 0 );
74
75     if ( autoFilter )
76     {
77         connect( this, SIGNAL( currentItemChanged( QTreeWidgetItem * ) ),
78                  this, SLOT  ( filter()                            ) );
79     }
80
81     if ( autoFill )
82     {
83         fillList();
84         selectSomething();
85     }
86 #endif
87     y2debug( "Creating pattern list done" );
88 }
89
90
91 YQPkgPatternList::~YQPkgPatternList()
92 {
93     // NOP
94 }
95
96
97 void
98 YQPkgPatternList::fillList()
99 {
100     clear();
101     y2debug( "Filling pattern list" );
102
103
104     for ( ZyppPoolIterator it = zyppPatternsBegin();
105           it != zyppPatternsEnd();
106           ++it )
107     {
108         ZyppPattern zyppPattern = tryCastToZyppPattern( (*it)->theObj() );
109
110         if ( zyppPattern )
111         {
112             if ( zyppPattern->userVisible() )
113             {
114                 addPatternItem( *it, zyppPattern );
115             }
116             else
117                 y2debug( "Pattern %s is not user-visible", zyppPattern->name().c_str() );
118         }
119         else
120         {
121             y2error( "Found non-Pattern selectable" );
122         }
123     }
124
125     y2debug( "Pattern list filled" );
126 }
127
128
129 YQPkgPatternCategoryItem *
130 YQPkgPatternList::category( const QString & categoryName )
131 {
132     if ( categoryName.isEmpty() )
133         return 0;
134
135     YQPkgPatternCategoryItem * cat = _categories[ categoryName ];
136
137     if ( ! cat )
138     {
139         y2debug( "New pattern category \"%s\"", (const char *) categoryName );
140
141         cat = new YQPkgPatternCategoryItem( this, categoryName );
142         Q_CHECK_PTR( cat );
143         _categories.insert( categoryName, cat );
144     }
145
146
147     return cat;
148 }
149
150
151 void
152 YQPkgPatternList::filterIfVisible()
153 {
154     if ( isVisible() )
155         filter();
156 }
157
158
159 void
160 YQPkgPatternList::filter()
161 {
162     emit filterStart();
163
164     if ( selection() )  // The seleted QListViewItem
165     {
166         ZyppPattern zyppPattern = selection()->zyppPattern();
167
168         if ( zyppPattern )
169         {
170             zypp::ui::PatternContents patternContents( zyppPattern );
171             set<string> wanted = patternContents.install_packages();
172
173             for ( ZyppPoolIterator it = zyppPkgBegin();
174                   it != zyppPkgEnd();
175                   ++it )
176             {
177                 string name = (*it)->theObj()->name();
178
179                 if ( contains( wanted, name ) )
180                 {
181                     ZyppPkg zyppPkg = tryCastToZyppPkg( (*it)->theObj() );
182
183                     if ( zyppPkg )
184                     {
185                         emit filterMatch( *it, zyppPkg );
186                     }
187                 }
188             }
189         }
190     }
191
192     emit filterFinished();
193 }
194
195
196 void
197 YQPkgPatternList::addPatternItem( ZyppSel       selectable,
198                                   ZyppPattern   zyppPattern )
199 {
200     if ( ! selectable )
201     {
202         y2error( "NULL ZyppSelectable!" );
203         return;
204     }
205
206     YQPkgPatternCategoryItem * cat = category( fromUTF8( zyppPattern->category() ) );
207     YQPkgPatternListItem * item = 0;
208
209     if ( cat )
210         item = new YQPkgPatternListItem( this, cat, selectable, zyppPattern );
211     else
212         item = new YQPkgPatternListItem( this, selectable, zyppPattern );
213
214     addTopLevelItem(item);
215     applyExcludeRules( item );
216 }
217
218
219 YQPkgPatternListItem *
220 YQPkgPatternList::selection() const
221 {
222 #if FIXME
223     QTreeWidgetItem * item = currentItem();
224
225     if ( ! item )
226         return 0;
227
228     return dynamic_cast<YQPkgPatternListItem *> (item);
229 #else
230     return 0;
231 #endif
232 }
233
234
235 void
236 YQPkgPatternList::pkgObjClicked( int                    button,
237                                  QTreeWidgetItem *      listViewItem,
238                                  int                    col,
239                                  const QPoint &         pos )
240 {
241 #if FIXME
242     YQPkgPatternCategoryItem * categoryItem
243         = dynamic_cast<YQPkgPatternCategoryItem *> (listViewItem);
244
245     if ( categoryItem )
246     {
247         if ( button == Qt::LeftButton )
248         {
249             if ( col == statusCol() )
250             {
251                 categoryItem->setOpen( ! categoryItem->isExpanded() );
252             }
253         }
254     }
255     else
256     {
257         YQPkgObjList::pkgObjClicked( button, listViewItem, col, pos );
258     }
259 #endif
260 }
261
262
263 void
264 YQPkgPatternList::selectSomething()
265 {
266 #if FIXME
267     QTreeWidgetItemIterator it( this );
268
269     while ( *it )
270     {
271         QY2ListViewItem * item = dynamic_cast<QY2ListViewItem *> (*it);
272         YQPkgPatternCategoryItem * categoryItem =
273             dynamic_cast<YQPkgPatternCategoryItem *> (*it);
274
275         if ( item && item->isSelectable() && ! categoryItem )
276         {
277             setSelected( item, true ); // emits signal, too
278             return;
279         }
280
281         ++it;
282     }
283 #endif
284 }
285
286 void YQPkgPatternList::drawRow ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
287 {
288     painter->setFont( YQUI::yqApp()->headingFont() );
289     QTreeWidget::drawRow ( painter, option, index );
290 }
291
292 YQPkgPatternListItem::YQPkgPatternListItem( YQPkgPatternList *  patternList,
293                                             ZyppSel             selectable,
294                                             ZyppPattern         zyppPattern )
295     : YQPkgObjListItem( patternList, selectable, zyppPattern )
296     , _patternList( patternList )
297     , _zyppPattern( zyppPattern )
298 {
299     init();
300 }
301
302
303 YQPkgPatternListItem::YQPkgPatternListItem( YQPkgPatternList *          patternList,
304                                             YQPkgPatternCategoryItem *  parentCategory,
305                                             ZyppSel                     selectable,
306                                             ZyppPattern                 zyppPattern )
307     : YQPkgObjListItem( patternList, parentCategory, selectable, zyppPattern )
308     , _patternList( patternList )
309     , _zyppPattern( zyppPattern )
310 {
311     init();
312     parentCategory->addPattern( _zyppPattern );
313 }
314
315
316 void
317 YQPkgPatternListItem::init()
318 {
319     if ( ! _zyppPattern )
320         _zyppPattern = tryCastToZyppPattern( selectable()->theObj() );
321
322     setStatusIcon();
323 }
324
325
326
327 YQPkgPatternListItem::~YQPkgPatternListItem()
328 {
329     // NOP
330 }
331
332
333 void
334 YQPkgPatternListItem::applyChanges()
335 {
336     solveResolvableCollections();
337 }
338
339
340
341 /**
342  * Comparison function used for sorting the list.
343  * Returns:
344  * -1 if this <  other
345  *  0 if this == other
346  * +1 if this >  other
347  **/
348 int
349 YQPkgPatternListItem::compare( QTreeWidgetItem *        otherListViewItem,
350                                int              col,
351                                bool             ascending ) const
352 {
353     YQPkgPatternListItem * otherPatternListitem  = dynamic_cast<YQPkgPatternListItem     *>(otherListViewItem);
354
355     if ( _zyppPattern && otherPatternListitem && otherPatternListitem->zyppPattern() )
356         return _zyppPattern->order().compare( otherPatternListitem->zyppPattern()->order() );
357
358     YQPkgPatternCategoryItem * otherCategoryItem = dynamic_cast<YQPkgPatternCategoryItem *>(otherListViewItem);
359
360     if ( otherCategoryItem )    // Patterns without category should always be sorted
361         return -1;              // before any category
362
363     return false; //FIXME QTreeWidgetItem::compare( otherListViewItem, col, ascending );
364 }
365
366
367
368
369
370
371 YQPkgPatternCategoryItem::YQPkgPatternCategoryItem( YQPkgPatternList *  patternList,
372                                                     const QString &     category        )
373     : QY2ListViewItem( patternList )
374     , _patternList( patternList )
375 {
376     setText( _patternList->summaryCol(), category );
377     setBackgroundColor( CATEGORY_BACKGROUND );
378     setOpen( true );
379     setTreeIcon();
380 }
381
382
383 YQPkgPatternCategoryItem::~YQPkgPatternCategoryItem()
384 {
385     // NOP
386 }
387
388 void
389 YQPkgPatternCategoryItem::addPattern( ZyppPattern pattern )
390 {
391     if ( ! _firstPattern )
392     {
393         _firstPattern = pattern;
394     }
395     else
396     {
397         if ( _firstPattern->order().compare( pattern->order() ) < 0 )
398             _firstPattern = pattern;
399     }
400 }
401
402
403 void
404 YQPkgPatternCategoryItem::setOpen( bool open )
405 {
406     //FIXME QTreeWidgetItem::setOpen( open );
407     setTreeIcon();
408 }
409
410
411 void
412 YQPkgPatternCategoryItem::setTreeIcon()
413 {
414 #if FIXME
415     setPixmap( _patternList->statusCol(),
416                isOpen() ?
417                YQIconPool::treeMinus() :
418                YQIconPool::treePlus()   );
419 #endif
420 }
421
422
423
424 /**
425  * Comparison function used for sorting the list.
426  * Returns:
427  * -1 if this <  other
428  *  0 if this == other
429  * +1 if this >  other
430  **/
431 int
432 YQPkgPatternCategoryItem::compare( QTreeWidgetItem *    otherListViewItem,
433                                    int                  col,
434                                    bool                 ascending ) const
435 {
436     YQPkgPatternCategoryItem * otherCategoryItem = dynamic_cast<YQPkgPatternCategoryItem *>(otherListViewItem);
437
438     if ( _firstPattern && otherCategoryItem && otherCategoryItem->firstPattern() )
439         return _firstPattern->order().compare( otherCategoryItem->firstPattern()->order() );
440
441
442     YQPkgPatternListItem * otherPatternListitem  = dynamic_cast<YQPkgPatternListItem *>(otherListViewItem);
443
444     if ( otherPatternListitem ) // Patterns without category should always be sorted
445         return 1;               // before any category
446
447     return false; // FIXME QTreeWidgetItem::compare( otherListViewItem, col, ascending );
448 }
449
450
451
452 #include "YQPkgPatternList.moc"