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