korganizer Library API Documentation

searchdialog.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 1998 Preston Brown
00004     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qlayout.h>
00026 #include <qcheckbox.h>
00027 #include <qgroupbox.h>
00028 #include <qhbuttongroup.h>
00029 #include <qlabel.h>
00030 #include <qlineedit.h>
00031 
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 
00035 #include <libkdepim/kdateedit.h>
00036 
00037 #include "koglobals.h"
00038 #include "koprefs.h"
00039 #include "kolistview.h"
00040 
00041 #include "searchdialog.h"
00042 #include "searchdialog.moc"
00043 
00044 SearchDialog::SearchDialog(Calendar *calendar,QWidget *parent)
00045   : KDialogBase(Plain,i18n("Find Events"),User1|Close,User1,parent,0,false,false,
00046                 KGuiItem( i18n("&Find"), "find") )
00047 {
00048   mCalendar = calendar;
00049 
00050   QFrame *topFrame = plainPage();
00051   QVBoxLayout *layout = new QVBoxLayout(topFrame,0,spacingHint());
00052 
00053   // Search expression
00054   QHBoxLayout *subLayout = new QHBoxLayout();
00055   layout->addLayout(subLayout);
00056 
00057   searchEdit = new QLineEdit( "*", topFrame ); // Find all events by default
00058   searchLabel = new QLabel( searchEdit, i18n("&Search for:"), topFrame );
00059   subLayout->addWidget( searchLabel );
00060   subLayout->addWidget( searchEdit );
00061   searchEdit->setFocus();
00062   connect( searchEdit, SIGNAL( textChanged( const QString & ) ),
00063            this, SLOT( searchTextChanged( const QString & ) ) );
00064 
00065 
00066   QHButtonGroup *itemsGroup = new QHButtonGroup( i18n("Search For"), topFrame );
00067   layout->addWidget( itemsGroup );
00068   mEventsCheck = new QCheckBox( i18n("&Events"), itemsGroup );
00069   mTodosCheck = new QCheckBox( i18n("To&dos"), itemsGroup );
00070   mJournalsCheck = new QCheckBox( i18n("&Journal entries"), itemsGroup );
00071   mEventsCheck->setChecked( true );
00072   mTodosCheck->setChecked( true );
00073 
00074   // Date range
00075   QGroupBox *rangeGroup = new QGroupBox( 1, Horizontal, i18n( "Date Range" ),
00076                                         topFrame );
00077   layout->addWidget( rangeGroup );
00078 
00079   QWidget *rangeWidget = new QWidget( rangeGroup );
00080   QHBoxLayout *rangeLayout = new QHBoxLayout( rangeWidget, 0, spacingHint() );
00081 
00082   mStartDate = new KDateEdit( rangeWidget );
00083   rangeLayout->addWidget( new QLabel( mStartDate, i18n("Fr&om:"), rangeWidget ) );
00084   rangeLayout->addWidget( mStartDate );
00085 
00086   mEndDate = new KDateEdit( rangeWidget );
00087   rangeLayout->addWidget( new QLabel( mEndDate, i18n("&To:"), rangeWidget ) );
00088   mEndDate->setDate( QDate::currentDate().addDays( 365 ) );
00089   rangeLayout->addWidget( mEndDate );
00090 
00091   mInclusiveCheck = new QCheckBox( i18n("E&vents have to be completely included"),
00092                                   rangeGroup );
00093   mInclusiveCheck->setChecked( false );
00094   mIncludeUndatedTodos = new QCheckBox( i18n("Include todos &without due date"), rangeGroup );
00095   mIncludeUndatedTodos->setChecked( true );
00096 
00097   // Subjects to search
00098   QHButtonGroup *subjectGroup = new QHButtonGroup( i18n("Search In"), topFrame );
00099   layout->addWidget(subjectGroup);
00100 
00101   mSummaryCheck = new QCheckBox( i18n("Su&mmaries"), subjectGroup );
00102   mSummaryCheck->setChecked( true );
00103   mDescriptionCheck = new QCheckBox( i18n("Desc&riptions"), subjectGroup );
00104   mCategoryCheck = new QCheckBox( i18n("Cate&gories"), subjectGroup );
00105 
00106 
00107   // Results list view
00108   listView = new KOListView( mCalendar, topFrame );
00109   listView->showDates();
00110   layout->addWidget( listView );
00111 
00112   if ( KOPrefs::instance()->mCompactDialogs ) {
00113     KOGlobals::fitDialogToScreen( this, true );
00114   }
00115 
00116   connect( this,SIGNAL(user1Clicked()),SLOT(doSearch()));
00117 
00118   // Propagate edit and delete event signals from event list view
00119   connect( listView, SIGNAL( showIncidenceSignal( Incidence * ) ),
00120           SIGNAL( showIncidenceSignal( Incidence *) ) );
00121   connect( listView, SIGNAL( editIncidenceSignal( Incidence * ) ),
00122           SIGNAL( editIncidenceSignal( Incidence * ) ) );
00123   connect( listView, SIGNAL( deleteIncidenceSignal( Incidence * ) ),
00124           SIGNAL( deleteIncidenceSignal( Incidence * ) ) );
00125 }
00126 
00127 SearchDialog::~SearchDialog()
00128 {
00129 }
00130 
00131 void SearchDialog::searchTextChanged( const QString &_text )
00132 {
00133   enableButton( KDialogBase::User1, !_text.isEmpty() );
00134 }
00135 
00136 void SearchDialog::doSearch()
00137 {
00138   QRegExp re;
00139 
00140   re.setWildcard( true ); // most people understand these better.
00141   re.setCaseSensitive( false );
00142   re.setPattern( searchEdit->text() );
00143   if ( !re.isValid() ) {
00144     KMessageBox::sorry( this,
00145                         i18n("Invalid search expression, cannot perform "
00146                             "the search. Please enter a search expression "
00147                             "using the wildcard characters '*' and '?' "
00148                             "where needed." ) );
00149     return;
00150   }
00151 
00152   search( re );
00153 
00154   listView->showIncidences( mMatchedEvents );
00155 
00156   if ( mMatchedEvents.count() == 0 ) {
00157     KMessageBox::information( this,
00158         i18n("No events were found matching your search expression.") );
00159   }
00160 }
00161 
00162 void SearchDialog::updateView()
00163 {
00164   QRegExp re;
00165   re.setWildcard( true ); // most people understand these better.
00166   re.setCaseSensitive( false );
00167   re.setPattern( searchEdit->text() );
00168   if ( re.isValid() ) {
00169     search( re );
00170   } else {
00171     mMatchedEvents.clear();
00172   }
00173 
00174   listView->showIncidences( mMatchedEvents );
00175 }
00176 
00177 void SearchDialog::search( const QRegExp &re )
00178 {
00179   QDate startDt = mStartDate->date();
00180   QDate endDt = mEndDate->date();
00181 
00182   Event::List events;
00183   if (mEventsCheck->isChecked()) {
00184     events = mCalendar->events( startDt, endDt, mInclusiveCheck->isChecked() );
00185   }
00186   Todo::List todos;
00187   if (mTodosCheck->isChecked()) {
00188     if ( mIncludeUndatedTodos->isChecked() ) {
00189       Todo::List alltodos = mCalendar->todos();
00190       Todo::List::iterator it;
00191       Todo *todo;
00192       for (it=alltodos.begin(); it!=alltodos.end(); ++it) {
00193         todo = *it;
00194         if ( (!todo->hasStartDate() && !todo->hasDueDate() ) || // undated
00195              ( todo->hasStartDate() && (todo->dtStart()>=startDt) && (todo->dtStart()<=endDt) ) || // start dt in range
00196              ( todo->hasDueDate() && (todo->dtDue().date()>=startDt) && (todo->dtDue()<=endDt) ) || // due dt in range
00197              ( todo->hasCompletedDate() && (todo->completed().date()>=startDt) && (todo->completed()<=endDt) ) ) { // completed dt in range
00198           todos.append( todo );
00199         }
00200       } 
00201     } else {
00202       QDate dt = startDt;
00203       while ( dt <= endDt ) {
00204         todos += mCalendar->todos( dt );
00205         dt = dt.addDays( 1 );
00206       }
00207     }
00208   }
00209 
00210   Journal::List journals;
00211   if (mJournalsCheck->isChecked()) {
00212     QDate dt = startDt;
00213     while ( dt <= endDt ) {
00214       Journal* j=mCalendar->journal( dt );
00215       if (j) journals.append( j );
00216       dt = dt.addDays( 1 );
00217     }
00218   }
00219 
00220   Incidence::List allIncidences = Calendar::mergeIncidenceList( events, todos, journals );
00221 
00222   mMatchedEvents.clear();
00223   Incidence::List::ConstIterator it;
00224   for( it = allIncidences.begin(); it != allIncidences.end(); ++it ) {
00225     Incidence *ev = *it;
00226     if ( mSummaryCheck->isChecked() ) {
00227 #if QT_VERSION >= 300
00228       if ( re.search( ev->summary() ) != -1 ) {
00229 #else
00230       if ( re.match( ev->summary() ) != -1 ) {
00231 #endif
00232         mMatchedEvents.append( ev );
00233         continue;
00234       }
00235     }
00236     if ( mDescriptionCheck->isChecked() ) {
00237 #if QT_VERSION >= 300
00238       if ( re.search( ev->description() ) != -1 ) {
00239 #else
00240       if ( re.match( ev->description() ) != -1 ) {
00241 #endif
00242         mMatchedEvents.append( ev );
00243         continue;
00244       }
00245     }
00246     if ( mCategoryCheck->isChecked() ) {
00247 #if QT_VERSION >= 300
00248       if ( re.search( ev->categoriesStr() ) != -1 ) {
00249 #else
00250       if ( re.match( ev->categoriesStr() ) != -1 ) {
00251 #endif
00252         mMatchedEvents.append( ev );
00253         continue;
00254       }
00255     }
00256   }
00257 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:21:01 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003