Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tvfilterview.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include "tvfilterview.h"
00021 
00022 /* OPIE */
00023 #include <opie2/odebug.h>
00024 #include <qpe/qpeapplication.h>
00025 using namespace Opie::Core;
00026 
00027 /* QT */
00028 #include <qtoolbutton.h>
00029 #include <qcombobox.h>
00030 #include <qlistview.h>
00031 #include <qlayout.h>
00032 #include <qheader.h>
00033 #include <qpushbutton.h>
00034 #include <qlabel.h>
00035 
00036 
00037 TVFilterView::TVFilterView(TableState *t, QWidget* parent,
00038                            const char *name, WFlags fl ) : QDialog(parent, name, TRUE, fl)
00039 {
00040     if ( !name )
00041     setName( "Filter View" );
00042 
00043     QVBoxLayout *vlayout = new QVBoxLayout(this);
00044 
00045     display = new QListView(this, "display");
00046     display->addColumn("Key");
00047     display->addColumn("Constraint");
00048     display->addColumn("Value");
00049     display->header()->setClickEnabled(FALSE);
00050     display->header()->setResizeEnabled(FALSE);
00051 
00052     vlayout->addWidget(display);
00053 
00054     QHBoxLayout *hlayout = new QHBoxLayout;
00055     hlayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
00056 
00057     newFilterButton = new QPushButton(this, "new Filter");
00058     newFilterButton->setMaximumSize(QSize(50, 32767));
00059     newFilterButton->setText("New");
00060     hlayout->addWidget(newFilterButton);
00061 
00062     deleteFilterButton = new QPushButton(this, "delete Filter");
00063     deleteFilterButton->setMaximumSize(QSize(50, 32767));
00064     deleteFilterButton->setText("Delete");
00065     hlayout->addWidget(deleteFilterButton);
00066 
00067     clearFilterButton = new QPushButton(this, "delete Filter");
00068     clearFilterButton->setMaximumSize(QSize(60, 32767));
00069     clearFilterButton->setText("Clear All");
00070     hlayout->addWidget(clearFilterButton);
00071 
00072     vlayout->addLayout(hlayout);
00073 
00074     QHBoxLayout *hlayout2 = new QHBoxLayout;
00075 
00076     keyNameCombo = new QComboBox(FALSE, this, "key name");
00077     keyNameCombo->setEnabled(FALSE);
00078     hlayout2->addWidget(keyNameCombo);
00079 
00080     QLabel *label = new QLabel(this);
00081     label->setText("has value");
00082     hlayout2->addWidget(label);
00083 
00084     keyEntry = new TVFilterKeyEntry(this, "key entry");
00085     keyEntry->setEnabled(FALSE);
00086 
00087     vlayout->addLayout(hlayout2);
00088     vlayout->addWidget(keyEntry);
00089 
00090     connect(newFilterButton, SIGNAL( clicked() ), this, SLOT( newTerm() ));
00091     connect(deleteFilterButton, SIGNAL( clicked() ), this, SLOT( deleteTerm()));
00092     connect(clearFilterButton, SIGNAL( clicked() ), this, SLOT( clearTerms()));
00093 
00094     connect(keyEntry, SIGNAL(valueChanged()), this, SLOT( updateTerm() ));
00095     connect(keyNameCombo, SIGNAL(activated(int)), this, SLOT( updateTerm() ));
00096 
00097     connect(display, SIGNAL(selectionChanged(QListViewItem*)), this,
00098         SLOT(setTerm(QListViewItem*)));
00099 
00100     ts = t;
00101     current = 0;
00102     terms.setAutoDelete(true);
00103     do_filter = false;
00104 
00105 #ifdef Q_WS_QWS
00106     QPEApplication::showDialog( this );
00107 #endif
00108 }
00109 
00113 TVFilterView::~TVFilterView()
00114 {
00115 }
00116 
00117 void TVFilterView::rebuildData()
00118 {
00119 }
00120 
00121 void TVFilterView::reset()
00122 {
00123     keyNameCombo->clear();
00124     keyIds.clear();
00125 }
00126 
00127 void TVFilterView::rebuildKeys()
00128 {
00129     int i;
00130 
00131     if (!ts) return;
00132     if(!ts->kRep) return;
00133     keyEntry->setTableState(ts);
00134 
00135     /* set up the list of keys that can be compared on */
00136     keyNameCombo->clear();
00137     KeyListIterator it(*ts->kRep);
00138 
00139     i = 0;
00140     while(it.current()) {
00141         if(ts->kRep->validIndex(it.currentKey())) {
00142             keyNameCombo->insertItem(it.current()->name());
00143             keyIds.insert(i, it.currentKey());
00144             ++i;
00145         }
00146         ++it;
00147     }
00148 }
00149 
00150 bool TVFilterView::passesFilter(DataElem *d) {
00151     if (!filterActive()) return true;
00152 
00153 
00154     FilterTerm *t;
00155 
00156     for (t = terms.first(); t != 0; t = terms.next() ) {
00157         /* check against filter */
00158         switch(t->ct) {
00159             case ct_less:
00160                 if (!d->lessThan(t->keyIndex, t->value))
00161                     return false;
00162                 break;
00163             case ct_more:
00164                 if (!d->moreThan(t->keyIndex, t->value))
00165                     return false;
00166                 break;
00167             case ct_equal:
00168                 if (!d->equalTo(t->keyIndex, t->value))
00169                     return false;
00170                 break;
00171             case ct_contains:
00172                 if (!d->contains(t->keyIndex, t->value))
00173                     return false;
00174                 break;
00175             case ct_startswith:
00176                 if (!d->startsWith(t->keyIndex, t->value))
00177                     return false;
00178                 break;
00179             case ct_endswith:
00180                 if (!d->endsWith(t->keyIndex, t->value))
00181                     return false;
00182                 break;
00183             default:
00184                 owarn << "TVFilterView::passesFilter() unrecognized filter type" << oendl;
00185                 return false;
00186         }
00187     }
00188     return true;
00189 }
00190 
00191 bool TVFilterView::filterActive() const
00192 {
00193     /* when button operated, also check the do_filter value
00194     return do_filter;
00195     */
00196     if (terms.isEmpty())
00197         return false;
00198     return true;
00199 }
00200 
00201 /* SLOTS */
00202 void TVFilterView::newTerm()
00203 {
00204     if (!ts) return;
00205 
00206     FilterTerm *term = new FilterTerm;
00207     current = term;
00208 
00209     term->view = 0;
00210 
00211     updateTerm();
00212 
00213     display->setSelected(term->view, true);
00214     terms.append(term);
00215 
00216     keyEntry->setEnabled(true);
00217     keyNameCombo->setEnabled(true);
00218 }
00219 
00220 void TVFilterView::updateTerm()
00221 {
00222     FilterTerm *term;
00223     /* Read the widget values (keyname, compare type, value)
00224      * and build the lists */
00225     if (!ts) return;
00226     if (!current) return;
00227 
00228     QString keyString;
00229     QString cmpString;
00230     QString vString;
00231 
00232     term = current;
00233 
00234     /* create new list item, set initial values, enable widgets */
00235     term->keyIndex = keyIds[keyNameCombo->currentItem()];
00236     keyEntry->setKey(term->keyIndex); /* so the next two items make sense */
00237     term->ct = keyEntry->getCompareType(),
00238     term->value = keyEntry->getCompareValue();
00239 
00240     keyString = keyNameCombo->currentText();
00241 
00242     switch(term->ct) {
00243         case ct_less:
00244             cmpString = " less than ";
00245             break;
00246         case ct_more:
00247             cmpString = " more than ";
00248             break;
00249         case ct_equal:
00250             cmpString = " equal to ";
00251             break;
00252         case ct_contains:
00253             cmpString = " containing ";
00254             break;
00255         case ct_startswith:
00256             cmpString = " starting with ";
00257             break;
00258         case ct_endswith:
00259             cmpString = " ending with ";
00260             break;
00261         default:
00262             cmpString = " ERROR ";
00263     }
00264 
00265     vString = term->value.toString();
00266 
00267     /* remove old view */
00268     if (term->view)
00269         delete(term->view);
00270     term->view = new QListViewItem(display, 0, keyString, cmpString, vString);
00271     display->setSelected(term->view, true);
00272 }
00273 
00274 /* deletes current term */
00275 void TVFilterView::deleteTerm()
00276 {
00277     if(!current) return;
00278     if (current->view)
00279         delete(current->view);
00280 
00281     terms.removeRef(current);
00282 
00283     current = terms.first();
00284 
00285     if(terms.isEmpty()) {
00286         keyEntry->setEnabled(false);
00287         keyNameCombo->setEnabled(false);
00288     }
00289 }
00290 
00291 /* clears all terminations */
00292 void TVFilterView::clearTerms()
00293 {
00294     while(current)
00295         deleteTerm();
00296 }
00297 
00298 void TVFilterView::setTerm(QListViewItem *target)
00299 {
00300     /* Iterate through the list to find item with view=target..
00301      * set as current, delete */
00302     FilterTerm *term = current;
00303 
00304     for (current = terms.first(); current != 0; current = terms.next() )
00305         if (current->view == target)
00306             break;
00307 
00308     if (!current) {
00309         current = term;
00310     }
00311 }

Generated on Sat Nov 5 16:17:08 2005 for OPIE by  doxygen 1.4.2