libyui-qt-pkg  2.47.5
YQPkgClassFilterView.cc
1 /**************************************************************************
2 Copyright (C) 2000 - 2010 Novell, Inc.
3 All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 **************************************************************************/
20 
21 
22 /*---------------------------------------------------------------------\
23 | |
24 | __ __ ____ _____ ____ |
25 | \ \ / /_ _/ ___|_ _|___ \ |
26 | \ V / _` \___ \ | | __) | |
27 | | | (_| |___) || | / __/ |
28 | |_|\__,_|____/ |_| |_____| |
29 | |
30 | core system |
31 | (C) SuSE GmbH |
32 \----------------------------------------------------------------------/
33 
34  File: YQPkgClassFilterView.cc
35 
36  Authors: Duncan Mac-Vicar Prett <duncan@suse.de>
37  Stefan Hundhammer <sh@suse.de>
38 
39  Textdomain "qt-pkg"
40 
41 /-*/
42 
43 
44 #define YUILogComponent "qt-pkg"
45 #include "YUILog.h"
46 
47 #include <QApplication>
48 
49 #include "YQPackageSelector.h"
50 
51 #include "YQPkgClassFilterView.h"
52 #include "YQi18n.h"
53 #include "utf8.h"
54 
55 using std::string;
56 
57 
58 QString
59 translatedText( YQPkgClass pkgClass )
60 {
61  switch ( pkgClass )
62  {
63  case YQPkgClassNone: return _( "No Packages" );
64  case YQPkgClassRecommended: return _( "Recommended Packages" );
65  case YQPkgClassSuggested: return _( "Suggested Packages" );
66  case YQPkgClassOrphaned: return _( "Orphaned Packages" );
67  case YQPkgClassUnneeded: return _( "Unneeded Packages" );
68  case YQPkgClassMultiversion: return _( "Multiversion Packages" );
69  case YQPkgClassRetracted: return _( "Retracted Packages" );
70  case YQPkgClassRetractedInstalled: return _( "Retracted Installed Packages" );
71  case YQPkgClassAll: return _( "All Packages" );
72 
73  // Intentionally omitting 'default' case so gcc can catch unhandled enums
74  }
75 
76  // this should never be reached, not marked for translation
77  return "Unknown PkgClass";
78 }
79 
80 
81 string
82 pkgClassIcon( YQPkgClass pkgClass )
83 {
84  switch ( pkgClass )
85  {
86  case YQPkgClassAll:
87  return( "preferences-other" );
88 
89  default:
90  return( "preferences-desktop-locale" );
91  }
92 
93  return( "" );
94 }
95 
96 
98  : QTreeWidget( parent )
99 {
100  setIconSize( QSize( 32, 32 ) );
101  setHeaderLabels( QStringList( _( "Package Classification" ) ) );
102  setRootIsDecorated( false );
103  setSortingEnabled( true );
104 
105  connect( this, SIGNAL( currentItemChanged ( QTreeWidgetItem *, QTreeWidgetItem * ) ),
106  this, SLOT ( slotSelectionChanged ( QTreeWidgetItem * ) ) );
107 
108  fillPkgClasses();
109 }
110 
111 
113 {
114 }
115 
116 
117 void
118 YQPkgClassFilterView::fillPkgClasses()
119 {
120  new YQPkgClassItem( this, YQPkgClassRecommended );
121  new YQPkgClassItem( this, YQPkgClassSuggested );
122  new YQPkgClassItem( this, YQPkgClassOrphaned );
123  new YQPkgClassItem( this, YQPkgClassUnneeded );
124  new YQPkgClassItem( this, YQPkgClassRetracted );
125  new YQPkgClassItem( this, YQPkgClassRetractedInstalled );
126 
127  if ( ! zypp::sat::Pool::instance().multiversion().empty() )
128  new YQPkgClassItem( this, YQPkgClassMultiversion );
129 
130  new YQPkgClassItem( this, YQPkgClassAll );
131 }
132 
133 
134 void
136 {
137  if ( isVisible() )
138  filter();
139 }
140 
141 
142 void
144 {
145  emit filterStart();
146 
147  if ( selectedPkgClass() != YQPkgClassNone )
148  {
149  for ( ZyppPoolIterator it = zyppPkgBegin();
150  it != zyppPkgEnd();
151  ++it )
152  {
153  ZyppSel selectable = *it;
154  bool match = false;
155 
156  // If there is an installed obj, check this first. The bits are set for the installed
157  // obj only and the installed obj is not contained in the pick list if there in an
158  // identical candidate available from a repo.
159 
160  if ( selectable->installedObj() )
161  {
162  match = check( selectable, tryCastToZyppPkg( selectable->installedObj() ) );
163  }
164  if ( selectable->candidateObj() && ! match )
165  {
166  match = check( selectable, tryCastToZyppPkg( selectable->candidateObj() ) );
167  }
168 
169  // And then check the pick list which contain all availables and all objects for multi
170  // version packages and the installed obj if there isn't same version in a repo.
171 
172  if ( ! match )
173  {
174  zypp::ui::Selectable::picklist_iterator it = selectable->picklistBegin();
175 
176  while ( it != selectable->picklistEnd() && ! match )
177  {
178  check( selectable, tryCastToZyppPkg( *it ) );
179  ++it;
180  }
181  }
182  }
183  }
184 
185  emit filterFinished();
186 }
187 
188 
189 void
190 YQPkgClassFilterView::slotSelectionChanged( QTreeWidgetItem * newSelection )
191 {
192  YQPkgClassItem * sel = dynamic_cast<YQPkgClassItem *>( newSelection );
193 
194  if ( sel )
195  {
196  bool needSolverRun;
197 
198  switch ( sel->pkgClass() )
199  {
200  case YQPkgClassRecommended:
201  case YQPkgClassSuggested:
202  case YQPkgClassOrphaned:
203  case YQPkgClassUnneeded:
204  needSolverRun = true;
205  break;
206 
207  default:
208  needSolverRun = false;
209  break;
210  }
211 
212  if ( needSolverRun )
213  {
214  QApplication::setOverrideCursor(Qt::WaitCursor);
215  zypp::getZYpp()->resolver()->resolvePool();
216  QApplication::restoreOverrideCursor();
217  }
218  }
219 
220  filter();
221 }
222 
223 
224 bool
225 YQPkgClassFilterView::check( ZyppSel selectable, ZyppPkg pkg )
226 {
227  bool match = checkMatch( selectable, pkg );
228 
229  if ( match )
230  emit filterMatch( selectable, pkg );
231 
232  return match;
233 }
234 
235 
236 bool
237 YQPkgClassFilterView::checkMatch( ZyppSel selectable, ZyppPkg pkg )
238 {
239  if ( ! pkg )
240  return false;
241 
242  switch ( selectedPkgClass() )
243  {
244  case YQPkgClassNone: return false;
245  case YQPkgClassRecommended: return zypp::PoolItem( pkg ).status().isRecommended();
246  case YQPkgClassSuggested: return zypp::PoolItem( pkg ).status().isSuggested();
247  case YQPkgClassOrphaned: return zypp::PoolItem( pkg ).status().isOrphaned();
248  case YQPkgClassUnneeded: return zypp::PoolItem( pkg ).status().isUnneeded();
249  case YQPkgClassMultiversion: return selectable->multiversionInstall();
250  case YQPkgClassRetracted: return selectable->hasRetracted();
251  case YQPkgClassRetractedInstalled: return selectable->hasRetractedInstalled();
252  case YQPkgClassAll: return true;
253 
254  // No 'default' branch to let the compiler catch unhandled enum values
255  }
256 
257  return false;
258 }
259 
260 
261 YQPkgClass
263 {
264  QTreeWidgetItem * qItem = currentItem();
265 
266  if ( ! qItem )
267  return YQPkgClassNone;
268 
269  YQPkgClassItem * pkgClassItem = dynamic_cast<YQPkgClassItem *> ( qItem );
270 
271  if ( ! pkgClassItem )
272  return YQPkgClassNone;
273  else
274  return pkgClassItem->pkgClass();
275 }
276 
277 
278 void
280 {
281  QTreeWidgetItemIterator it( this );
282 
283  while ( *it )
284  {
285  YQPkgClassItem * item = dynamic_cast<YQPkgClassItem *>( *it );
286 
287  if ( item && item->pkgClass() == pkgClass )
288  {
289  setCurrentItem( item );
290  // This will also send the currentItemChanged() signal which will
291  // start filtering, i.e. it will populate the package list.
292  }
293 
294  ++it;
295  }
296 }
297 
298 
299 
300 
301 
302 
303 YQPkgClassItem::YQPkgClassItem( YQPkgClassFilterView * parentFilterView,
304  YQPkgClass pkgClass )
305  : QTreeWidgetItem( parentFilterView )
306  , _pkgClass( pkgClass )
307 {
308  setText( 0, translatedText( pkgClass ) );
309 }
310 
311 
312 YQPkgClassItem::~YQPkgClassItem()
313 {
314  // NOP
315 }
316 
317 
318 bool
319 YQPkgClassItem::operator< ( const QTreeWidgetItem & otherListViewItem ) const
320 {
321  const YQPkgClassItem * otherCategoryItem =
322  dynamic_cast<const YQPkgClassItem *>(&otherListViewItem);
323 
324  if ( otherCategoryItem )
325  return pkgClass() > otherCategoryItem->pkgClass();
326  else
327  return true;
328 }
329 
330 
Filter view for package classes (categories) like suggested, recommended, orphaned etc...
void filterFinished()
Emitted when filtering is finished.
bool check(ZyppSel selectable, ZyppPkg pkg)
Check if &#39;pkg&#39; matches the selected package class and send a filterMatch signal if it does...
YQPkgClassFilterView(QWidget *parent)
Constructor.
void filter()
Filter according to the view&#39;s rules and current selection.
void filterStart()
Emitted when the filtering starts.
void filterMatch(ZyppSel selectable, ZyppPkg pkg)
Emitted during filtering for each pkg that matches the filter.
void showPkgClass(YQPkgClass pkgClass)
Show the specified package class, i.e.
YQPkgClass selectedPkgClass() const
Returns the currently selected YQPkgClass.
bool checkMatch(ZyppSel selectable, ZyppPkg pkg)
Check if &#39;pkg&#39; matches the selected package class.
void filterIfVisible()
Same as filter(), but only if this widget is currently visible.
virtual ~YQPkgClassFilterView()
Destructor.