]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgRpmGroupTagsFilterView.cc
don't create QObjects in ycp thread - make timeouts work
[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     : QTreeWidget( parent )
36 {
37     setHeaderLabels( QStringList(_( "Package Groups" )) );
38     setRootIsDecorated( true );
39     cloneTree( rpmGroupsTree()->root(), 0 );
40
41     new YQPkgRpmGroupTag( this, _( "zzz All" ), 0 );
42
43     connect( this, SIGNAL( currentItemChanged     ( QTreeWidgetItem *, QTreeWidgetItem * ) ),
44              this, SLOT  ( slotSelectionChanged ( QTreeWidgetItem * ) ) );
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         //FIXME clone->setExpanded( clone->depth() < 1 );
105         clone->setExpanded( true );
106         cloneTree( child, clone );
107         child = child->next();
108     }
109 }
110
111
112 void
113 YQPkgRpmGroupTagsFilterView::selectSomething()
114 {
115 // FIXME
116 //     QTreeWidgetItem * item = children().first();
117 // 
118 //     if ( item )
119 //      setCurrentItem(item);
120 }
121
122
123 void
124 YQPkgRpmGroupTagsFilterView::filterIfVisible()
125 {
126     if ( isVisible() )
127         filter();
128 }
129
130
131 void
132 YQPkgRpmGroupTagsFilterView::filter()
133 {
134     emit filterStart();
135     // y2debug( "Filtering packages for RPM group \"%s\"", selectedRpmGroup().c_str() );
136     
137     if ( selection() )
138     {
139         for ( ZyppPoolIterator it = zyppPkgBegin();
140               it != zyppPkgEnd();
141               ++it )
142         {
143             ZyppSel selectable = *it;
144
145             // Multiple instances of this package may or may not be in the same
146             // RPM group, so let's check both the installed version (if there
147             // is any) and the candidate version.
148             //
149             // Make sure we emit only one filterMatch() signal if both exist
150             // and both are in the same RPM group. We don't want multiple list
151             // entries for the same package!
152
153             bool match =
154                 check( selectable, tryCastToZyppPkg( selectable->candidateObj() ) ) ||
155                 check( selectable, tryCastToZyppPkg( selectable->installedObj() ) );
156
157             // If there is neither an installed nor a candidate package, check
158             // any other instance.
159
160             if ( ! match                        &&
161                  ! selectable->candidateObj()   &&
162                  ! selectable->installedObj()     )
163                 check( selectable, tryCastToZyppPkg( selectable->theObj() ) );
164         }
165     }
166
167     emit filterFinished();
168 }
169
170
171 void
172 YQPkgRpmGroupTagsFilterView::slotSelectionChanged( QTreeWidgetItem * newSelection )
173 {
174     YQPkgRpmGroupTag * sel = dynamic_cast<YQPkgRpmGroupTag *>( newSelection );
175
176     if ( sel )
177     {
178         if ( sel->rpmGroup() )
179             _selectedRpmGroup = rpmGroupsTree()->rpmGroup( sel->rpmGroup() );
180         else
181             _selectedRpmGroup = "*";    // "zzz_All"
182     }
183     else
184     {
185         _selectedRpmGroup = "";
186     }
187
188     filter();
189 }
190
191
192 bool
193 YQPkgRpmGroupTagsFilterView::check( ZyppSel     selectable,
194                                     ZyppPkg     pkg             )
195 {
196     if ( ! pkg || ! selection() )
197         return false;
198
199     if ( selection()->rpmGroup() == 0 )         // Special case: All packages
200     {
201         emit filterMatch( selectable, pkg );
202         return true;
203     }
204
205     if ( selectedRpmGroup().empty() )
206         return false;
207     
208     if ( pkg->group() == selectedRpmGroup() ||                  // full match?
209          pkg->group().find( selectedRpmGroup() + "/" ) == 0 )   // starts with selected?
210     {
211         emit filterMatch( selectable, pkg );
212         return true;
213     }
214
215     return false;
216 }
217
218
219 YQPkgRpmGroupTag *
220 YQPkgRpmGroupTagsFilterView::selection() const
221 {
222     QTreeWidgetItem * item = currentItem();
223
224     if ( ! item )
225         return 0;
226
227     return dynamic_cast<YQPkgRpmGroupTag *> ( item );
228 }
229
230
231
232
233
234
235 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
236                                     YStringTreeItem *                   rpmGroup        )
237     : QTreeWidgetItem( parentFilterView )
238     , _filterView( parentFilterView )
239     , _rpmGroup( rpmGroup )
240 {
241     setText( 0,  fromUTF8( _rpmGroup->value().translation() ) );
242 }
243
244
245 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
246                                     YQPkgRpmGroupTag *                  parentGroupTag,
247                                     YStringTreeItem *                   rpmGroup        )
248     : QTreeWidgetItem( parentGroupTag )
249     , _filterView( parentFilterView )
250     , _rpmGroup( rpmGroup )
251 {
252     setText( 0,  fromUTF8( _rpmGroup->value().translation() )  );
253 }
254
255
256 YQPkgRpmGroupTag::YQPkgRpmGroupTag( YQPkgRpmGroupTagsFilterView *       parentFilterView,
257                                     const QString &                     rpmGroupName,
258                                     YStringTreeItem *                   rpmGroup        )
259     : QTreeWidgetItem( parentFilterView )
260     , _filterView( parentFilterView )
261     , _rpmGroup( rpmGroup )
262 {
263     setText( 0,  rpmGroupName );
264 }
265
266
267 YQPkgRpmGroupTag::~YQPkgRpmGroupTag()
268 {
269     // NOP
270 }
271
272
273
274 #include "YQPkgRpmGroupTagsFilterView.moc"