picking up branches/tmp/sh/qt4-port/, merging it with trunk
[duncan/yast2-qt4.git] / src / pkg / YQPkgRpmGroupTagsFilterView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgRpmGroupTagsFilterView.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
25 #include "YQPkgRpmGroupTagsFilterView.h"
26 #include "YQi18n.h"
27 #include "utf8.h"
28
29
30
31 YRpmGroupsTree * YQPkgRpmGroupTagsFilterView::_rpmGroupsTree = 0;
32
33
34 YQPkgRpmGroupTagsFilterView::YQPkgRpmGroupTagsFilterView( QWidget * parent )
35     : Q3ListView( parent )
36 {
37     addColumn( _( "Package Groups" ) );
38     setRootIsDecorated( true );
39     cloneTree( rpmGroupsTree()->root(), 0 );
40
41     new YQPkgRpmGroupTag( this, _( "zzz All" ), 0 );
42
43     connect( this, SIGNAL( selectionChanged     ( Q3ListViewItem * ) ),
44              this, SLOT  ( slotSelectionChanged ( Q3ListViewItem * ) ) );
45
46     selectSomething();
47 }
48
49
50 YQPkgRpmGroupTagsFilterView::~YQPkgRpmGroupTagsFilterView()
51 {
52 }
53
54
55 YRpmGroupsTree *
56 YQPkgRpmGroupTagsFilterView::rpmGroupsTree()
57 {
58     if ( ! _rpmGroupsTree )
59     {
60         _rpmGroupsTree = new YRpmGroupsTree();
61         Q_CHECK_PTR( _rpmGroupsTree );
62
63         fillRpmGroupsTree();
64     }
65
66     return _rpmGroupsTree;
67 }
68
69
70 void
71 YQPkgRpmGroupTagsFilterView::fillRpmGroupsTree()
72 {
73     y2debug( "Filling RPM groups tree" );
74
75     for ( ZyppPoolIterator it = zyppPkgBegin();
76           it != zyppPkgEnd();
77           ++it )
78     {
79         ZyppPkg zyppPkg = tryCastToZyppPkg( (*it)->theObj() );
80
81         if ( zyppPkg )
82             rpmGroupsTree()->addRpmGroup( zyppPkg->group() );
83     }
84
85     y2debug( "Filling RPM groups tree done" );
86 }
87
88
89 void
90 YQPkgRpmGroupTagsFilterView::cloneTree( YStringTreeItem *       parentRpmGroup,
91                                         YQPkgRpmGroupTag *      parentClone )
92 {
93     YStringTreeItem *   child = parentRpmGroup->firstChild();
94     YQPkgRpmGroupTag *  clone;
95
96     while ( child )
97     {
98         if ( parentClone )
99             clone = new YQPkgRpmGroupTag( this, parentClone, child );
100         else
101             clone = new YQPkgRpmGroupTag( this, child );
102
103         Q_CHECK_PTR( clone );
104         clone->setOpen( clone->depth() < 1 );
105
106         cloneTree( child, clone );
107         child = child->next();
108     }
109 }
110
111
112 void
113 YQPkgRpmGroupTagsFilterView::selectSomething()
114 {
115     Q3ListViewItem * item = firstChild();
116
117     if ( item )
118         setSelected( item, true );
119 }
120
121
122 void
123 YQPkgRpmGroupTagsFilterView::filterIfVisible()
124 {
125     if ( isVisible() )
126         filter();
127 }
128
129
130 void
131 YQPkgRpmGroupTagsFilterView::filter()
132 {
133     emit filterStart();
134     // y2debug( "Filtering packages for RPM group \"%s\"", selectedRpmGroup().c_str() );
135     
136     if ( selection() )
137     {
138         for ( ZyppPoolIterator it = zyppPkgBegin();
139               it != zyppPkgEnd();
140               ++it )
141         {
142             ZyppSel selectable = *it;
143
144             // Multiple instances of this package may or may not be in the same
145             // RPM group, so let's check both the installed version (if there
146             // is any) and the candidate version.
147             //
148             // Make sure we emit only one filterMatch() signal if both exist
149             // and both are in the same RPM group. We don't want multiple list
150             // entries for the same package!
151
152             bool match =
153                 check( selectable, tryCastToZyppPkg( selectable->candidateObj() ) ) ||
154                 check( selectable, tryCastToZyppPkg( selectable->installedObj() ) );
155
156             // If there is neither an installed nor a candidate package, check
157             // any other instance.
158
159             if ( ! match                        &&
160                  ! selectable->candidateObj()   &&
161                  ! selectable->installedObj()     )
162                 check( selectable, tryCastToZyppPkg( selectable->theObj() ) );
163         }
164     }
165
166     emit filterFinished();
167 }
168
169
170 void
171 YQPkgRpmGroupTagsFilterView::slotSelectionChanged( Q3ListViewItem * newSelection )
172 {
173     YQPkgRpmGroupTag * sel = dynamic_cast<YQPkgRpmGroupTag *>( newSelection );
174
175     if ( sel )
176     {
177         if ( sel->rpmGroup() )
178             _selectedRpmGroup = rpmGroupsTree()->rpmGroup( sel->rpmGroup() );
179         else
180             _selectedRpmGroup = "*";    // "zzz_All"
181     }
182     else
183     {
184         _selectedRpmGroup = "";
185     }
186
187     filter();
188 }
189
190
191 bool
192 YQPkgRpmGroupTagsFilterView::check( ZyppSel     selectable,
193                                     ZyppPkg     pkg             )
194 {
195     if ( ! pkg || ! selection() )
196         return false;
197
198     if ( selection()->rpmGroup() == 0 )         // Special case: All packages
199     {
200         emit filterMatch( selectable, pkg );
201         return true;
202     }
203
204     if ( selectedRpmGroup().empty() )
205         return false;
206     
207     if ( pkg->group() == selectedRpmGroup() ||                  // full match?
208          pkg->group().find( selectedRpmGroup() + "/" ) == 0 )   // starts with selected?
209     {
210         emit filterMatch( selectable, pkg );
211         return true;
212     }
213
214     return false;
215 }
216
217
218 YQPkgRpmGroupTag *
219 YQPkgRpmGroupTagsFilterView::selection() const
220 {
221     Q3ListViewItem * item = selectedItem();
222
223     if ( ! item )
224         return 0;
225
226     return dynamic_cast<YQPkgRpmGroupTag *> ( selectedItem() );
227 }
228
229
230
231
232
233
234 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
235                                     YStringTreeItem *                   rpmGroup        )
236     : Q3ListViewItem( parentFilterView )
237     , _filterView( parentFilterView )
238     , _rpmGroup( rpmGroup )
239 {
240     setText( 0,  fromUTF8( _rpmGroup->value().translation() ) );
241 }
242
243
244 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
245                                     YQPkgRpmGroupTag *                  parentGroupTag,
246                                     YStringTreeItem *                   rpmGroup        )
247     : Q3ListViewItem( parentGroupTag )
248     , _filterView( parentFilterView )
249     , _rpmGroup( rpmGroup )
250 {
251     setText( 0,  fromUTF8( _rpmGroup->value().translation() )  );
252 }
253
254
255 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
256                                     const QString &                     rpmGroupName,
257                                     YStringTreeItem *                   rpmGroup        )
258     : Q3ListViewItem( parentFilterView )
259     , _filterView( parentFilterView )
260     , _rpmGroup( rpmGroup )
261 {
262     setText( 0,  rpmGroupName );
263 }
264
265
266 YQPkgRpmGroupTag::~YQPkgRpmGroupTag()
267 {
268     // NOP
269 }
270
271
272
273 #include "YQPkgRpmGroupTagsFilterView.moc"