1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQPkgSearchFilterView.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
21 #include <qcheckbox.h>
22 #include <qcombobox.h>
25 #include <qpushbutton.h>
26 #include <qradiobutton.h>
27 #include <q3vgroupbox.h>
28 #include <q3progressdialog.h>
29 #include <qdatetime.h>
33 #define y2log_component "qt-pkg"
34 #include <ycp/y2log.h>
36 #include "YQPkgSearchFilterView.h"
37 #include "QY2LayoutUtils.h"
40 #include "YQApplication.h"
47 #define SPACING 6 // between subwidgets
48 #define MARGIN 4 // around the widget
51 YQPkgSearchFilterView::YQPkgSearchFilterView( QWidget * parent )
56 setSpacing( SPACING );
61 QLabel * label = new QLabel( _( "Searc&h:" ), this );
63 label->setFont( YQUI::yqApp()->headingFont() );
65 // Input field ( combo box ) for search text
66 _searchText = new QComboBox( this );
67 Q_CHECK_PTR( _searchText );
68 _searchText->setEditable( true );
69 label->setBuddy( _searchText );
72 // Box for search button
73 Q3HBox * hbox = new Q3HBox( this );
79 _searchButton = new QPushButton( _( "&Search" ), hbox );
80 Q_CHECK_PTR( _searchButton );
82 connect( _searchButton, SIGNAL( clicked() ),
83 this, SLOT ( filter() ) );
91 Q3VGroupBox * gbox = new Q3VGroupBox( _( "Search in" ), this );
95 _searchInName = new QCheckBox( _( "&Name" ), gbox ); Q_CHECK_PTR( _searchInName );
96 _searchInSummary = new QCheckBox( _( "Su&mmary" ), gbox ); Q_CHECK_PTR( _searchInSummary );
97 _searchInDescription = new QCheckBox( _( "Descr&iption" ), gbox ); Q_CHECK_PTR( _searchInDescription );
101 // Intentionally NOT marking RPM tags for translation
102 _searchInProvides = new QCheckBox( "RPM \"&Provides\"" , gbox ); Q_CHECK_PTR( _searchInProvides );
103 _searchInRequires = new QCheckBox( "RPM \"Re&quires\"" , gbox ); Q_CHECK_PTR( _searchInRequires );
105 _searchInName->setChecked( true );
106 _searchInSummary->setChecked( true );
115 label = new QLabel( _( "Search &Mode:" ), this );
116 Q_CHECK_PTR( label );
118 _searchMode = new QComboBox( this );
119 Q_CHECK_PTR( _searchMode );
120 _searchMode->setEditable( false );
122 label->setBuddy( _searchMode );
124 // Caution: combo box items must be inserted in the same order as enum SearchMode!
125 _searchMode->insertItem( _( "Contains" ) );
126 _searchMode->insertItem( _( "Begins with" ) );
127 _searchMode->insertItem( _( "Exact Match" ) );
128 _searchMode->insertItem( _( "Use Wild Cards" ) );
129 _searchMode->insertItem( _( "Use Regular Expression" ) );
131 _searchMode->setCurrentItem( Contains );
136 _caseSensitive = new QCheckBox( _( "Case Sensiti&ve" ), this );
137 Q_CHECK_PTR( _caseSensitive );
139 for ( int i=0; i < 6; i++ )
144 YQPkgSearchFilterView::~YQPkgSearchFilterView()
151 YQPkgSearchFilterView::keyPressEvent( QKeyEvent * event )
155 if ( event->state() == 0 ) // No Ctrl / Alt / Shift etc. pressed
157 if ( event->key() == Qt::Key_Return ||
158 event->key() == Qt::Key_Enter )
160 _searchButton->animateClick();
167 Q3VBox::keyPressEvent( event );
172 YQPkgSearchFilterView::setFocus()
174 _searchText->setFocus();
179 YQPkgSearchFilterView::minimumSizeHint() const
181 return QSize( 0, 0 );
186 YQPkgSearchFilterView::filterIfVisible()
194 YQPkgSearchFilterView::filter()
199 if ( ! _searchText->currentText().isEmpty() )
201 // Create a progress dialog that is only displayed if the search takes
202 // longer than a couple of seconds ( default: 4 ).
204 Q3ProgressDialog progress( _( "Searching..." ), // text
205 _( "&Cancel" ), // cancelButtonLabel
207 Y2PM::packageManager().size(), // totalSteps
211 this, 0, // parent, name
213 progress.setCaption( "" );
214 progress.setMinimumDuration( 2000 ); // millisec
217 QRegExp regexp( _searchText->currentText() );
218 regexp.setCaseSensitive( _caseSensitive->isChecked() );
219 regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
226 for ( ZyppPoolIterator it = zyppPkgBegin();
227 it != zyppPkgEnd() && ! progress.wasCancelled();
230 ZyppSel selectable = *it;
233 check( selectable, selectable->candidateObj(), regexp ) ||
234 check( selectable, selectable->installedObj(), regexp );
236 // If there is neither an installed nor a candidate package, check
237 // any other instance.
240 ! selectable->candidateObj() &&
241 ! selectable->installedObj() )
242 check( selectable, selectable->theObj(), regexp );
245 progress.setProgress( count++ );
247 if ( timer.elapsed() > 300 ) // milisec
249 // Process events only every 300 milliseconds - this is very
250 // expensive since both the progress dialog and the package
251 // list change all the time, thus display updates are necessary
254 qApp->processEvents();
259 if ( _matchCount == 0 )
260 emit message( _( "No Results." ) );
263 emit filterFinished();
268 YQPkgSearchFilterView::check( ZyppSel selectable,
271 QRegExp regexp( _searchText->currentText() );
272 regexp.setCaseSensitive( _caseSensitive->isChecked() );
273 regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
275 return check( selectable, zyppObj, regexp );
280 YQPkgSearchFilterView::check( ZyppSel selectable,
282 const QRegExp & regexp )
288 ( _searchInName->isChecked() && check( zyppObj->name(), regexp ) ) ||
289 ( _searchInSummary->isChecked() && check( zyppObj->summary(), regexp ) ) ||
290 ( _searchInDescription->isChecked() && check( zyppObj->description(), regexp ) ) ||
291 ( _searchInProvides->isChecked() && check( zyppObj->dep( zypp::Dep::PROVIDES ), regexp ) ) ||
292 ( _searchInRequires->isChecked() && check( zyppObj->dep( zypp::Dep::REQUIRES ), regexp ) );
296 ZyppPkg zyppPkg = tryCastToZyppPkg( zyppObj );
301 emit filterMatch( selectable, zyppPkg );
310 YQPkgSearchFilterView::check( const string & attribute,
311 const QRegExp & regexp )
313 QString att = fromUTF8( attribute );
314 QString searchText = _searchText->currentText();
317 switch ( _searchMode->currentItem() )
320 match = att.contains( searchText, _caseSensitive->isChecked() );
324 match = att.startsWith( searchText ); // only case sensitive
328 match = ( att == searchText );
333 // Both cases differ in how the regexp is set up during initialization
334 match = att.contains( regexp );
337 // Intentionally omitting "default" branch - let gcc watch for unhandled enums
345 YQPkgSearchFilterView::check( const zypp::CapSet & capSet, const QRegExp & regexp )
347 for ( zypp::CapSet::const_iterator it = capSet.begin();
351 if ( check( ( *it).index(), regexp ) )
353 // y2debug( "Match for %s", (*it).asString().c_str() );
363 #include "YQPkgSearchFilterView.moc"