1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQPkgSearchFilterView.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
23 #include <qcheckbox.h>
24 #include <qcombobox.h>
27 #include <qpushbutton.h>
28 #include <qradiobutton.h>
29 #include <q3vgroupbox.h>
30 #include <q3progressdialog.h>
31 #include <qdatetime.h>
35 #define y2log_component "qt-pkg"
36 #include <ycp/y2log.h>
38 #include "YQPkgSearchFilterView.h"
39 #include "QY2LayoutUtils.h"
42 #include "YQApplication.h"
49 #define SPACING 6 // between subwidgets
50 #define MARGIN 4 // around the widget
53 YQPkgSearchFilterView::YQPkgSearchFilterView( QWidget * parent )
58 setSpacing( SPACING );
63 QLabel * label = new QLabel( _( "Searc&h:" ), this );
65 label->setFont( YQUI::yqApp()->headingFont() );
67 // Input field ( combo box ) for search text
68 _searchText = new QComboBox( this );
69 Q_CHECK_PTR( _searchText );
70 _searchText->setEditable( true );
71 label->setBuddy( _searchText );
74 // Box for search button
75 Q3HBox * hbox = new Q3HBox( this );
81 _searchButton = new QPushButton( _( "&Search" ), hbox );
82 Q_CHECK_PTR( _searchButton );
84 connect( _searchButton, SIGNAL( clicked() ),
85 this, SLOT ( filter() ) );
93 Q3VGroupBox * gbox = new Q3VGroupBox( _( "Search in" ), this );
97 _searchInName = new QCheckBox( _( "&Name" ), gbox ); Q_CHECK_PTR( _searchInName );
98 _searchInSummary = new QCheckBox( _( "Su&mmary" ), gbox ); Q_CHECK_PTR( _searchInSummary );
99 _searchInDescription = new QCheckBox( _( "Descr&iption" ), gbox ); Q_CHECK_PTR( _searchInDescription );
103 // Intentionally NOT marking RPM tags for translation
104 _searchInProvides = new QCheckBox( "RPM \"&Provides\"" , gbox ); Q_CHECK_PTR( _searchInProvides );
105 _searchInRequires = new QCheckBox( "RPM \"Re&quires\"" , gbox ); Q_CHECK_PTR( _searchInRequires );
107 _searchInName->setChecked( true );
108 _searchInSummary->setChecked( true );
117 label = new QLabel( _( "Search &Mode:" ), this );
118 Q_CHECK_PTR( label );
120 _searchMode = new QComboBox( this );
121 Q_CHECK_PTR( _searchMode );
122 _searchMode->setEditable( false );
124 label->setBuddy( _searchMode );
126 // Caution: combo box items must be inserted in the same order as enum SearchMode!
127 _searchMode->insertItem( _( "Contains" ) );
128 _searchMode->insertItem( _( "Begins with" ) );
129 _searchMode->insertItem( _( "Exact Match" ) );
130 _searchMode->insertItem( _( "Use Wild Cards" ) );
131 _searchMode->insertItem( _( "Use Regular Expression" ) );
133 _searchMode->setCurrentItem( Contains );
138 _caseSensitive = new QCheckBox( _( "Case Sensiti&ve" ), this );
139 Q_CHECK_PTR( _caseSensitive );
141 for ( int i=0; i < 6; i++ )
146 YQPkgSearchFilterView::~YQPkgSearchFilterView()
153 YQPkgSearchFilterView::keyPressEvent( QKeyEvent * event )
157 if ( event->state() == 0 ) // No Ctrl / Alt / Shift etc. pressed
159 if ( event->key() == Qt::Key_Return ||
160 event->key() == Qt::Key_Enter )
162 _searchButton->animateClick();
169 Q3VBox::keyPressEvent( event );
174 YQPkgSearchFilterView::setFocus()
176 _searchText->setFocus();
181 YQPkgSearchFilterView::minimumSizeHint() const
183 return QSize( 0, 0 );
188 YQPkgSearchFilterView::filterIfVisible()
196 YQPkgSearchFilterView::filter()
201 if ( ! _searchText->currentText().isEmpty() )
203 // Create a progress dialog that is only displayed if the search takes
204 // longer than a couple of seconds ( default: 4 ).
206 Q3ProgressDialog progress( _( "Searching..." ), // text
207 _( "&Cancel" ), // cancelButtonLabel
209 Y2PM::packageManager().size(), // totalSteps
213 this, 0, // parent, name
215 progress.setCaption( "" );
216 progress.setMinimumDuration( 2000 ); // millisec
219 QRegExp regexp( _searchText->currentText() );
220 regexp.setCaseSensitive( _caseSensitive->isChecked() );
221 regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
228 for ( ZyppPoolIterator it = zyppPkgBegin();
229 it != zyppPkgEnd() && ! progress.wasCancelled();
232 ZyppSel selectable = *it;
235 check( selectable, selectable->candidateObj(), regexp ) ||
236 check( selectable, selectable->installedObj(), regexp );
238 // If there is neither an installed nor a candidate package, check
239 // any other instance.
242 ! selectable->candidateObj() &&
243 ! selectable->installedObj() )
244 check( selectable, selectable->theObj(), regexp );
247 progress.setProgress( count++ );
249 if ( timer.elapsed() > 300 ) // milisec
251 // Process events only every 300 milliseconds - this is very
252 // expensive since both the progress dialog and the package
253 // list change all the time, thus display updates are necessary
256 qApp->processEvents();
261 if ( _matchCount == 0 )
262 emit message( _( "No Results." ) );
265 emit filterFinished();
270 YQPkgSearchFilterView::check( ZyppSel selectable,
273 QRegExp regexp( _searchText->currentText() );
274 regexp.setCaseSensitive( _caseSensitive->isChecked() );
275 regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
277 return check( selectable, zyppObj, regexp );
282 YQPkgSearchFilterView::check( ZyppSel selectable,
284 const QRegExp & regexp )
290 ( _searchInName->isChecked() && check( zyppObj->name(), regexp ) ) ||
291 ( _searchInSummary->isChecked() && check( zyppObj->summary(), regexp ) ) ||
292 ( _searchInDescription->isChecked() && check( zyppObj->description(), regexp ) ) ||
293 ( _searchInProvides->isChecked() && check( zyppObj->dep( zypp::Dep::PROVIDES ), regexp ) ) ||
294 ( _searchInRequires->isChecked() && check( zyppObj->dep( zypp::Dep::REQUIRES ), regexp ) );
298 ZyppPkg zyppPkg = tryCastToZyppPkg( zyppObj );
303 emit filterMatch( selectable, zyppPkg );
312 YQPkgSearchFilterView::check( const string & attribute,
313 const QRegExp & regexp )
315 QString att = fromUTF8( attribute );
316 QString searchText = _searchText->currentText();
319 switch ( _searchMode->currentItem() )
322 match = att.contains( searchText, _caseSensitive->isChecked() );
326 match = att.startsWith( searchText ); // only case sensitive
330 match = ( att == searchText );
335 // Both cases differ in how the regexp is set up during initialization
336 match = att.contains( regexp );
339 // Intentionally omitting "default" branch - let gcc watch for unhandled enums
347 YQPkgSearchFilterView::check( const zypp::CapSet & capSet, const QRegExp & regexp )
349 for ( zypp::CapSet::const_iterator it = capSet.begin();
353 if ( check( ( *it).index(), regexp ) )
355 // y2debug( "Match for %s", (*it).asString().c_str() );
365 #include "YQPkgSearchFilterView.moc"