]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgSearchFilterView.cc
more porting and make files use qt3support one by one
[duncan/yast2-qt4.git] / src / pkg / YQPkgSearchFilterView.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgSearchFilterView.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   Textdomain "packages-qt"
18
19 /-*/
20
21 #define QT3_SUPPORT 1
22
23 #include <qcheckbox.h>
24 #include <qcombobox.h>
25 #include <qlabel.h>
26 #include <qlayout.h>
27 #include <qpushbutton.h>
28 #include <qradiobutton.h>
29 #include <q3vgroupbox.h>
30 #include <q3progressdialog.h>
31 #include <qdatetime.h>
32 //Added by qt3to4:
33 #include <qevent.h>
34
35 #define y2log_component "qt-pkg"
36 #include <ycp/y2log.h>
37
38 #include "YQPkgSearchFilterView.h"
39 #include "QY2LayoutUtils.h"
40 #include "YQi18n.h"
41 #include "utf8.h"
42 #include "YQApplication.h"
43 #include "YQUI.h"
44
45 using std::list;
46 using std::string;
47
48
49 #define SPACING                 6       // between subwidgets
50 #define MARGIN                  4       // around the widget
51
52
53 YQPkgSearchFilterView::YQPkgSearchFilterView( QWidget * parent )
54     : Q3VBox( parent )
55 {
56     _matchCount = 0;
57     setMargin( MARGIN );
58     setSpacing( SPACING );
59
60     addVStretch( this );
61
62     // Headline
63     QLabel * label = new QLabel( _( "Searc&h:" ), this );
64     Q_CHECK_PTR( label );
65     label->setFont( YQUI::yqApp()->headingFont() );
66
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 );
72
73
74     // Box for search button
75     Q3HBox * hbox = new Q3HBox( this );
76     Q_CHECK_PTR( hbox );
77
78     addHStretch( hbox );
79
80     // Search button
81     _searchButton = new QPushButton( _( "&Search" ), hbox );
82     Q_CHECK_PTR( _searchButton );
83
84     connect( _searchButton, SIGNAL( clicked() ),
85              this,          SLOT  ( filter()  ) );
86
87     addVStretch( this );
88
89     //
90     // Where to search
91     //
92
93     Q3VGroupBox * gbox = new Q3VGroupBox( _( "Search in" ), this );
94     Q_CHECK_PTR( gbox );
95
96
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 );
100
101     addVStretch( gbox );
102
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    );
106
107     _searchInName->setChecked( true );
108     _searchInSummary->setChecked( true );
109
110     addVStretch( this );
111
112
113     //
114     // Search mode
115     //
116
117     label = new QLabel( _( "Search &Mode:" ), this );
118     Q_CHECK_PTR( label );
119
120     _searchMode = new QComboBox( this );
121     Q_CHECK_PTR( _searchMode );
122     _searchMode->setEditable( false );
123
124     label->setBuddy( _searchMode );
125
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" ) );
132
133     _searchMode->setCurrentItem( Contains );
134
135
136     addVStretch( this );
137
138     _caseSensitive = new QCheckBox( _( "Case Sensiti&ve" ), this );
139     Q_CHECK_PTR( _caseSensitive );
140
141     for ( int i=0; i < 6; i++ )
142         addVStretch( this );
143 }
144
145
146 YQPkgSearchFilterView::~YQPkgSearchFilterView()
147 {
148     // NOP
149 }
150
151
152 void
153 YQPkgSearchFilterView::keyPressEvent( QKeyEvent * event )
154 {
155     if ( event )
156     {
157         if ( event->state() == 0 )      // No Ctrl / Alt / Shift etc. pressed
158         {
159             if ( event->key() == Qt::Key_Return ||
160                  event->key() == Qt::Key_Enter    )
161             {
162                 _searchButton->animateClick();
163                 return;
164             }
165         }
166
167     }
168
169     Q3VBox::keyPressEvent( event );
170 }
171
172
173 void
174 YQPkgSearchFilterView::setFocus()
175 {
176     _searchText->setFocus();
177 }
178
179
180 QSize
181 YQPkgSearchFilterView::minimumSizeHint() const
182 {
183     return QSize( 0, 0 );
184 }
185
186
187 void
188 YQPkgSearchFilterView::filterIfVisible()
189 {
190     if ( isVisible() )
191         filter();
192 }
193
194
195 void
196 YQPkgSearchFilterView::filter()
197 {
198     emit filterStart();
199     _matchCount = 0;
200
201     if ( ! _searchText->currentText().isEmpty() )
202     {
203         // Create a progress dialog that is only displayed if the search takes
204         // longer than a couple of seconds ( default: 4 ).
205
206         Q3ProgressDialog progress( _( "Searching..." ),                 // text
207                                   _( "&Cancel" ),                       // cancelButtonLabel
208 #ifdef FIXME
209                                   Y2PM::packageManager().size(),        // totalSteps
210 #else
211                                   1000,
212 #endif
213                                   this, 0,                              // parent, name
214                                   true );                               // modal
215         progress.setCaption( "" );
216         progress.setMinimumDuration( 2000 ); // millisec
217         QTime timer;
218
219         QRegExp regexp( _searchText->currentText() );
220         regexp.setCaseSensitive( _caseSensitive->isChecked() );
221         regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
222
223         timer.start();
224
225
226         int count = 0;
227
228         for ( ZyppPoolIterator it = zyppPkgBegin();
229               it != zyppPkgEnd() && ! progress.wasCancelled();
230               ++it )
231         {
232             ZyppSel selectable = *it;
233
234             bool match =
235                 check( selectable, selectable->candidateObj(), regexp ) ||
236                 check( selectable, selectable->installedObj(), regexp );
237
238             // If there is neither an installed nor a candidate package, check
239             // any other instance.
240
241             if ( ! match                      &&
242                  ! selectable->candidateObj() &&
243                  ! selectable->installedObj()   )
244                 check( selectable, selectable->theObj(), regexp );
245
246
247             progress.setProgress( count++ );
248
249             if ( timer.elapsed() > 300 ) // milisec
250             {
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
254                 // each time.
255
256                 qApp->processEvents();
257                 timer.restart();
258             }
259         }
260
261         if ( _matchCount == 0 )
262             emit message( _( "No Results." ) );
263     }
264
265     emit filterFinished();
266 }
267
268
269 bool
270 YQPkgSearchFilterView::check( ZyppSel   selectable,
271                               ZyppObj   zyppObj )
272 {
273     QRegExp regexp( _searchText->currentText() );
274     regexp.setCaseSensitive( _caseSensitive->isChecked() );
275     regexp.setWildcard( _searchMode->currentItem() == UseWildcards );
276
277     return check( selectable, zyppObj, regexp );
278 }
279
280
281 bool
282 YQPkgSearchFilterView::check( ZyppSel           selectable,
283                               ZyppObj           zyppObj,
284                               const QRegExp &   regexp )
285 {
286     if ( ! zyppObj )
287         return false;
288
289     bool match =
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 ) );
295
296     if ( match )
297     {
298         ZyppPkg zyppPkg = tryCastToZyppPkg( zyppObj );
299
300         if ( zyppPkg )
301         {
302             _matchCount++;
303             emit filterMatch( selectable, zyppPkg );
304         }
305     }
306
307     return match;
308 }
309
310
311 bool
312 YQPkgSearchFilterView::check( const string &    attribute,
313                               const QRegExp &   regexp )
314 {
315     QString att         = fromUTF8( attribute );
316     QString searchText  = _searchText->currentText();
317     bool match          = false;
318
319     switch ( _searchMode->currentItem() )
320     {
321         case Contains:
322             match = att.contains( searchText, _caseSensitive->isChecked() );
323             break;
324
325         case BeginsWith:
326             match = att.startsWith( searchText );       // only case sensitive
327             break;
328
329         case ExactMatch:
330             match = ( att == searchText );
331             break;
332
333         case UseWildcards:
334         case UseRegExp:
335             // Both cases differ in how the regexp is set up during initialization
336             match = att.contains( regexp );
337             break;
338
339             // Intentionally omitting "default" branch - let gcc watch for unhandled enums
340     }
341
342     return match;
343 }
344
345
346 bool
347 YQPkgSearchFilterView::check( const zypp::CapSet & capSet, const QRegExp & regexp )
348 {
349     for ( zypp::CapSet::const_iterator it = capSet.begin();
350           it != capSet.end();
351           ++it )
352     {
353         if ( check( ( *it).index(), regexp ) )
354         {
355             // y2debug( "Match for %s", (*it).asString().c_str() );
356             return true;
357         }
358     }
359
360     return false;
361 }
362
363
364
365 #include "YQPkgSearchFilterView.moc"