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

tvlistview.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 
00021 #include "tvlistview.h"
00022 #include "../db/common.h"
00023 
00024 /* OPIE */
00025 #include <opie2/odebug.h>
00026 using namespace Opie::Core;
00027 
00028 /* QT */
00029 #include <qtoolbutton.h>
00030 #include <qlistview.h>
00031 #include <qlayout.h>
00032 
00033 void TVListViewPrivate::setColumnWidth(int column, int width)
00034 {
00035     if(width > 70) width = 70;
00036     QListView::setColumnWidth(column, width);
00037 }
00038 
00039 void TVListViewPrivate::setSorting(int column, bool increasing)
00040 {
00041     emit sortChanged(column);
00042     QListView::setSorting(column, increasing);
00043 }
00044 
00045 TVListViewPrivate::TVListViewPrivate(QWidget *parent, const char* name,
00046         WFlags fl) : QListView(parent, name, fl) {
00047         ;
00048 }
00049 
00050 class TVListViewItem : public QListViewItem 
00051 {
00052 public:
00053 
00054     TVListViewItem(QListView *parent, DataElem *d);
00055     ~TVListViewItem();
00056 
00057     QString text(int i) const
00058     {
00059         return data_reference->toQString(i);
00060     }
00061 
00062     /* Do nothing... all data for this item should be generated */
00063     void setText(int i, const QString &) 
00064     {
00065         ;
00066     }
00067     QString key(int i, bool a) const 
00068     {
00069         return data_reference->toSortableQString(i);
00070     }
00071 
00072     void setDataElem(DataElem *d) 
00073     {
00074         data_reference = d;
00075     }
00076 
00077     DataElem *getDataElem() {
00078         return data_reference;
00079     }
00080 private: 
00081     DataElem *data_reference;
00082 };
00083 
00084 TVListViewItem::TVListViewItem(QListView *parent, DataElem *d) 
00085         : QListViewItem(parent)
00086 {
00087     data_reference =  d;
00088 }
00089 
00090 TVListViewItem::~TVListViewItem()
00091 {
00092     data_reference = 0;
00093 }
00094 
00095 TVListView::TVListView(TableState *t, QWidget* parent, 
00096         const char *name, WFlags fl ) : QWidget(parent, name, fl)
00097 {
00098     if (!name) 
00099         setName("TVListView");
00100 
00101     // the next two lines need to be rationalized.
00102     resize(318,457);
00103     setSizePolicy(QSizePolicy((QSizePolicy::SizeType)7, 
00104                   (QSizePolicy::SizeType)7, sizePolicy().hasHeightForWidth()));
00105     setCaption(tr("List View"));
00106 
00107     QVBoxLayout *layout = new QVBoxLayout(this);
00108     layout->setSpacing(0);
00109     layout->setMargin(0);
00110 
00111     listViewDisplay = new TVListViewPrivate(this, "listViewDisplay");
00112     layout->addWidget(listViewDisplay);
00113 
00114     connect(listViewDisplay, SIGNAL(currentChanged(QListViewItem*)), this,
00115                 SLOT(setCurrent(QListViewItem*)));
00116     connect(listViewDisplay, SIGNAL(sortChanged(int)), this, 
00117                 SLOT(setSorting(int)));
00118 
00119     listViewDisplay->setShowSortIndicator(true);
00120 
00121     it = new QListViewItemIterator(listViewDisplay);
00122     ts = t;
00123 }
00124 
00125 TVListView::~TVListView()
00126 {
00127 }
00128 
00129 void TVListView::addItem(DataElem *d) 
00130 {
00131     TVListViewItem *i = new TVListViewItem(listViewDisplay, d);
00132 
00133     delete it;
00134     it = new QListViewItemIterator(i);
00135 }
00136 
00137 /* remove current (it) item */
00138 void TVListView::removeItem() 
00139 {
00140     QListViewItemIterator other(*it);
00141 
00142     QListViewItemIterator tmp = *it;
00143     (*it)++;
00144     if (!it->current()) {
00145         *it = tmp;
00146                 (*it)--;
00147                 if (!it->current()) {
00148                    delete it;
00149                    it = 0;
00150            }
00151     }
00152 
00153    delete other.current();
00154 }
00155 
00156 void TVListView::clearItems() 
00157 {
00158     /* This is ok since the destructor for TVListItem does not know about
00159     the data_reference pointer.. and hence will leave it alone */
00160     listViewDisplay->clear();
00161     delete it;
00162     it = new QListViewItemIterator(listViewDisplay);
00163 }
00164 
00165 void TVListView::first()
00166 {
00167     delete it;
00168     it = new QListViewItemIterator(listViewDisplay);
00169 }
00170 
00171 void TVListView::last()
00172 {
00173     owarn << "TVListView::last not yet implemented" << oendl; 
00174 }
00175 
00176 void TVListView::next()
00177 {
00178     QListViewItemIterator tmp = *it;
00179     (*it)++;
00180     if (!it->current()) {
00181         *it = tmp;
00182     }
00183 }
00184 
00185 void TVListView::previous()
00186 {
00187     QListViewItemIterator tmp = *it; 
00188     (*it)--;
00189     if (!it->current()) {
00190         *it = tmp;
00191     }
00192 }
00193 
00194 DataElem *TVListView::getCurrentData() {
00195     if (it->current()) {
00196         return ((TVListViewItem *)it->current())->getDataElem();
00197     }
00198     return NULL;
00199 }
00200 
00202 void TVListView::findItem(int keyId, TVVariant value) 
00203 {
00204     QListViewItem *i;
00205     TVListViewItem *best_so_far = NULL;
00206     /* start at the beginning... go through till find the closest elem */
00207     i = listViewDisplay->firstChild();
00208     while (i) {
00209         /* search stuff */
00210         if(best_so_far) {
00211             if (DataElem::closer(
00212                     ((TVListViewItem *)i)->getDataElem(), 
00213                     best_so_far->getDataElem(), value, keyId)) 
00214                 best_so_far = (TVListViewItem *)i;
00215         } else {
00216             if (DataElem::closer(
00217                     ((TVListViewItem *)i)->getDataElem(), 
00218                     NULL, value, keyId)) 
00219                 best_so_far = (TVListViewItem *)i;
00220         }
00221             
00222         i = i->itemBelow();
00223     }
00224     if (best_so_far) {
00225         /* set best_so_far to current element */
00226         delete it;
00227         it = new QListViewItemIterator(best_so_far);
00228     }
00229 }
00230 
00231 void TVListView::rebuildKeys()
00232 {
00233     int i;
00234     if(!ts) return;
00235     if(!ts->kRep) return;
00236 
00237     i = listViewDisplay->columns();
00238 
00239     while(i > 0) 
00240         listViewDisplay->removeColumn(--i);
00241 
00242     KeyListIterator kit(*ts->kRep);
00243     i = 0;
00244     while(kit.current()) {
00245         if(!kit.current()->delFlag()) {
00246             listViewDisplay->addColumn(kit.current()->name());
00247             keyIds.insert(i, kit.currentKey());
00248             ++i;
00249         }
00250         ++kit;
00251     }
00252 }
00253 
00254 
00255 void TVListView::setSorting(int column) 
00256 {
00257     /* Without table state can't do anything */
00258     if (ts == 0) 
00259         return;
00260     if (keyIds[column] != ts->current_column) {
00261         ts->current_column = keyIds[column];
00262     }
00263 }
00264 
00265 void TVListView::rebuildData() {
00266     int i;
00267     QMap<int, int>::Iterator kit;
00268     /* Need to set sort order */
00269     if(!ts)
00270         return;
00271 
00272     /* revers lookup the column */
00273     i = -1;
00274     for(kit = keyIds.begin(); kit != keyIds.end(); ++kit) {
00275         if (kit.data() == ts->current_column) {
00276             i = kit.key();
00277             break;
00278         }
00279     }
00280     if (i == -1)
00281         return;
00282 
00283     listViewDisplay->setSorting(i);
00284     listViewDisplay->sort();
00285 
00286     /* reset current element */
00287     listViewDisplay->setCurrentItem(it->current());
00288     listViewDisplay->setSelected(it->current(), true);
00289     listViewDisplay->ensureItemVisible(it->current());
00290 }
00291 
00292 void TVListView::reset() 
00293 {
00294     int i;
00295     listViewDisplay->clear();
00296 
00297     i = listViewDisplay->columns();
00298     while (i > 0)
00299         listViewDisplay->removeColumn(--i);
00300 
00301     keyIds.clear();
00302 }
00303 
00304 void TVListView::setCurrent(QListViewItem *i) 
00305 {
00306     /* cast */
00307     TVListViewItem *t = (TVListViewItem *)i;
00308 
00309     if(!t) {
00310         /* set current to null */
00311         ts->current_elem = 0;
00312         return;
00313     }
00314 
00315     ts->current_elem = t->getDataElem();
00316     /* now also set up the iterator */
00317 
00318     delete it;
00319     it = new QListViewItemIterator(i);
00320 
00321     //emit browseView();
00322 }

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