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

tableviewer.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 "tableviewer.h"
00022 #include "ui/tvbrowseview.h"
00023 #include "ui/tvfilterview.h"
00024 #include "ui/tvlistview.h"
00025 #include "ui/tveditview.h"
00026 #include "ui/tvkeyedit.h"
00027 #include "db/datacache.h"
00028 
00029 /* OPIE */
00030 #include <opie2/odebug.h>
00031 #include <opie2/oresource.h>
00032 #include <qpe/fileselector.h>
00033 using namespace Opie::Core;
00034 
00035 /* QT */
00036 #include <qmenubar.h>
00037 #include <qtoolbar.h>
00038 #include <qpopupmenu.h>
00039 #include <qapplication.h>
00040 #include <qwidgetstack.h>
00041 #include <qlayout.h>
00042 #include <qbuffer.h>
00043 
00055 TableViewerWindow::TableViewerWindow(QWidget *parent, const char *name, WFlags f)
00056     : QMainWindow(parent, name, f)
00057 {
00058     setCaption(tr("Table Viewer"));
00059 
00060 /* Build data */
00061     ds = new DBStore();
00062     doc.setType("text/x-xml-tableviewer");
00063     doc.setName("table");
00064 
00065     dirty = FALSE;
00066     ts.current_column = 0;
00067     ts.kRep = ds->getKeys();
00068 
00069 /* build menus */
00070     menu = new QMenuBar(this, 0);
00071 
00072     QPopupMenu *file_menu = new QPopupMenu;
00073     file_menu->insertItem("New", this, SLOT(newDocument()));
00074 
00075     file_menu->insertItem("Open", this, SLOT(selectDocument()));
00076     file_menu->insertSeparator();
00077     file_menu->insertItem("Properties");
00078 
00079     /* later will want to set this up to clean up first via this, SLOT(quit) */
00080     menu->insertItem("Document", file_menu); 
00081 
00082     QPopupMenu *edit_menu = new QPopupMenu;
00083     edit_menu->insertItem("Edit Item", this, SLOT(editItemSlot()));
00084     edit_menu->insertItem("Edit Keys", this, SLOT(editKeysSlot()));
00085     edit_menu->insertItem("Edit filters", this, SLOT(filterViewSlot()));
00086     menu->insertItem("Edit", edit_menu);
00087 
00088     QPopupMenu *view_menu = new QPopupMenu;
00089     view_menu->insertItem("Browse View", this, SLOT(browseViewSlot()));
00090     view_menu->insertItem("List View", this, SLOT(listViewSlot()));
00091     menu->insertItem("View", view_menu); 
00092 
00093     QVBoxLayout *main_layout = new QVBoxLayout;
00094 
00095     /* Build tool bar */
00096     bool useBigIcon = qApp->desktop()->size().width() > 330;
00097 
00098     navigation = new QToolBar(this, "navigation");
00099     QToolButton *btn = new QToolButton( Opie::Core::OResource::loadPixmap("new", Opie::Core::OResource::SmallIcon),
00100                                         "New Item", QString::null, this, SLOT(newItemSlot()), navigation, "New Item");
00101     btn->setUsesBigPixmap( useBigIcon );
00102     btn = new QToolButton( Opie::Core::OResource::loadPixmap("edit", Opie::Core::OResource::SmallIcon),
00103                            "Edit Item", QString::null, this, SLOT(editItemSlot()), navigation, "Edit Item");
00104     btn->setUsesBigPixmap( useBigIcon );
00105     btn = new QToolButton( Opie::Core::OResource::loadPixmap("trash", Opie::Core::OResource::SmallIcon),
00106                            "Delete Item", QString::null, this, SLOT(deleteItemSlot()), navigation, "Delete Item");
00107     btn->setUsesBigPixmap( useBigIcon );
00108 
00109     navigation->addSeparator();
00110 
00111     btn = new QToolButton( Opie::Core::OResource::loadPixmap("fastback", Opie::Core::OResource::SmallIcon),
00112                            "First Item", QString::null, this, SLOT(firstItem()), navigation, "First Item");
00113     btn->setUsesBigPixmap( useBigIcon );
00114     btn = new QToolButton( Opie::Core::OResource::loadPixmap("back", Opie::Core::OResource::SmallIcon),
00115                            "Previous Item", QString::null, this, SLOT(previousItem()), navigation, "Previous Item");
00116     btn->setUsesBigPixmap( useBigIcon );
00117     btn = new QToolButton( Opie::Core::OResource::loadPixmap("forward", Opie::Core::OResource::SmallIcon),
00118                            "Next Item", QString::null, this, SLOT(nextItem()), navigation, "Next Item");
00119     btn->setUsesBigPixmap( useBigIcon );
00120     btn = new QToolButton( Opie::Core::OResource::loadPixmap("fastforward", Opie::Core::OResource::SmallIcon),
00121                            "Last Item", QString::null, this, SLOT(lastItem()), navigation, "Last Item");
00122     btn->setUsesBigPixmap( useBigIcon );
00123 
00124     navigation->addSeparator();
00125     btn = new QToolButton( Opie::Core::OResource::loadPixmap("day", Opie::Core::OResource::SmallIcon),
00126                            "View Single Item", QString::null, this, SLOT(browseViewSlot()), navigation, "View Single Item");
00127     btn->setUsesBigPixmap( useBigIcon );
00128     btn = new QToolButton( Opie::Core::OResource::loadPixmap("month", Opie::Core::OResource::SmallIcon),
00129                            "View Multiple Items", QString::null, this, SLOT(listViewSlot()), navigation, "View Multiple Items");
00130     btn->setUsesBigPixmap( useBigIcon );
00131 
00132     setToolBarsMovable(FALSE);
00133 
00134 /* Build widgets */
00135     browseView = new TVBrowseView(&ts, this, 0);
00136     listView = new TVListView(&ts, this, 0);
00137     filterView = new TVFilterView(&ts, this, 0);
00138     fileSelector = new FileSelector("text/csv;text/x-xml-tableviewer", 
00139                 this, "fileselector");
00140     fileSelector->setNewVisible(FALSE);
00141     fileSelector->setCloseVisible(FALSE);
00142 
00143     cw = new QWidgetStack(this, 0);
00144     cw->addWidget(listView, ListState);
00145     cw->addWidget(browseView, BrowseState);
00146     cw->addWidget(filterView, FilterState);
00147     cw->addWidget(fileSelector, FileState);
00148 
00149     current_view = FileState;
00150     cw->raiseWidget(current_view);
00151     fileSelector->reread();
00152 
00153     connect(browseView, SIGNAL(searchOnKey(int,TVVariant)), 
00154             this, SLOT(searchOnKey(int,TVVariant)));
00155     connect(browseView, SIGNAL(sortChanged(int)), 
00156             this, SLOT(setPrimaryKey(int)));
00157 
00158     connect(fileSelector, SIGNAL(closeMe()), this, SLOT(browseViewSlot()));
00159     connect(fileSelector, SIGNAL(fileSelected(const DocLnk&)), 
00160              this, SLOT(openDocument(const DocLnk&)));
00161 
00162     main_layout->addWidget(menu);
00163     main_layout->addWidget(cw);
00164 
00165     setCentralWidget(cw);
00166 
00167 }
00168 
00172 TableViewerWindow::~TableViewerWindow()
00173 {
00174     if(dirty)
00175       saveDocument();
00176 }
00177 
00181 void TableViewerWindow::selectDocument()
00182 {
00183     if(dirty)
00184   saveDocument();
00185     current_view = FileState;
00186     cw->raiseWidget(current_view);
00187     fileSelector->reread();
00188 }
00189 
00190 void TableViewerWindow::saveDocument()
00191 {
00192     if(!dirty)
00193   return;
00194 
00195     FileManager fm;
00196     QIODevice *dev = fm.saveFile(doc);
00197 
00198     if(!ds->saveSource(dev, doc.type())){
00199   owarn << "Save unsuccessful" << oendl; 
00200   return;
00201     }
00202     dev->close();
00203     dirty = FALSE;
00204 }
00205 
00206 void TableViewerWindow::newDocument()
00207 {
00208     DocLnk nf;
00209     nf.setType("text/x-xml-tableviewer");
00210     nf.setName("table");
00211    
00212     delete ds;
00213     ds = new DBStore();
00214    
00215     ts.current_column = 0;
00216     ts.kRep = ds->getKeys();
00217     browseView->reset();
00218     listView->reset();
00219     filterView->reset();
00220 
00221     doc = nf;
00222     dirty = FALSE;
00223 
00224     current_view = BrowseState;
00225     cw->raiseWidget(current_view);
00226 
00227   /* now set up for editing the keys */
00228   ts.kRep->addKey("key", TVVariant::String);
00229   editKeysSlot();
00230 }
00231 
00232 void TableViewerWindow::setDocument(const QString &f)
00233 {
00234     openDocument(DocLnk(f, TRUE));
00235 }
00236 
00237 void TableViewerWindow::openDocument(const DocLnk &f)
00238 {
00239 
00240     if (!f.isValid()) 
00241   return;
00242 
00243     FileManager fm;
00244     QIODevice *dev = fm.openFile(f);
00245     doc = f;
00246 
00247     if(ds->openSource(dev, doc.type())) {
00248         DataElem *d;
00249 
00250         browseView->reset();
00251         listView->reset();
00252         filterView->reset();
00253 
00254   current_view = BrowseState;
00255   cw->raiseWidget(current_view);
00256 
00257         /* set up new table state and ensure sub widgets have a reference */
00258         ts.current_column = 0;
00259         ts.kRep = ds->getKeys();
00260         browseView->rebuildKeys();
00261         listView->rebuildKeys();
00262         filterView->rebuildKeys();
00263 
00264         ds->first();
00265         /* set up the list view */
00266         listView->clearItems();
00267         do {
00268             d = ds->getCurrentData();
00269             if(d) 
00270                 listView->addItem(d);
00271         } while(ds->next());
00272 
00273         /* Set up browse view, Will be based of structure of listView */
00274         listView->first();
00275         ts.current_elem = listView->getCurrentData();
00276         browseView->rebuildData();
00277         listView->rebuildData();
00278 
00279         QString scratch = "Table Viewer";/* later take from constant */
00280         scratch += " - ";
00281         scratch += ds->getName();
00282         setCaption(tr(scratch));
00283 
00284   dirty = FALSE;
00285     } else {
00286         owarn << tr("could not load Document") << oendl; 
00287     }
00288     dev->close();
00289 }
00290 
00294 void TableViewerWindow::firstItem()
00295 {
00296     listView->first();
00297     ts.current_elem = listView->getCurrentData();
00298     browseView->rebuildData();
00299 }
00300 
00304 void TableViewerWindow::lastItem()
00305 {
00306     listView->last();
00307     ts.current_elem = listView->getCurrentData();
00308     browseView->rebuildData();
00309 }
00310 
00314 void TableViewerWindow::nextItem()
00315 {
00316     listView->next();
00317     ts.current_elem = listView->getCurrentData();
00318     browseView->rebuildData();
00319 }
00320 
00324 void TableViewerWindow::previousItem()
00325 {
00326     listView->previous();
00327     ts.current_elem = listView->getCurrentData();
00328     browseView->rebuildData();
00329 }
00330 
00334 void TableViewerWindow::listViewSlot()
00335 {
00336     if(current_view == FilterState)
00337         applyFilter();
00338     current_view = ListState;
00339     cw->raiseWidget(current_view);
00340 }
00341 
00342 void TableViewerWindow::applyFilter()
00343 {
00344     DataElem *d;
00345 
00346     listView->clearItems();
00347     ds->first();
00348     do {
00349         d = ds->getCurrentData();
00350         if(d) 
00351             if(filterView->passesFilter(d))
00352     listView->addItem(d);
00353     } while(ds->next());
00354     listView->first();
00355     listView->rebuildData();
00356 }
00357 
00361 void TableViewerWindow::browseViewSlot()
00362 {
00363     if(current_view == FilterState)
00364         applyFilter();
00365 
00366     ts.current_elem = listView->getCurrentData();
00367     browseView->rebuildData();
00368 
00369     current_view = BrowseState;
00370     cw->raiseWidget(current_view);
00371 }
00372 
00376 void TableViewerWindow::filterViewSlot()
00377 {
00378     current_view = FilterState;
00379     cw->raiseWidget(current_view);
00380 }
00381 
00382 
00383 
00384 
00385 void TableViewerWindow::editItemSlot()
00386 {
00387     if(TVEditView::openEditItemDialog(&ts, ts.current_elem, this)) {
00388   listView->rebuildData();
00389   browseView->rebuildData();
00390   dirty = TRUE;
00391     }
00392 }
00393 
00394 void TableViewerWindow::newItemSlot()
00395 {
00396     DataElem *d = new DataElem(ds);
00397     if (TVEditView::openEditItemDialog(&ts, d, this)) {
00398 
00399   ds->addItem(d);
00400   ts.current_elem = d;
00401   applyFilter();
00402   listView->rebuildData();
00403   browseView->rebuildData();
00404   dirty = TRUE;
00405     }
00406 }
00407 
00408 void TableViewerWindow::deleteItemSlot()
00409 {
00410   /* delete the actual item, then do a 'filter' */
00411   DataElem *to_remove = ts.current_elem;
00412 
00413   if(!to_remove) 
00414     return;
00415 
00416     listView->removeItem();
00417   ds->removeItem(to_remove);
00418     
00419     applyFilter();
00420     listView->rebuildData();
00421     browseView->rebuildData();
00422     dirty = TRUE;
00423 }
00424 
00425 void TableViewerWindow::editKeysSlot()
00426 {
00427     DataElem *d;
00428     KeyList *k = TVKeyEdit::openEditKeysDialog(&ts, this);
00429 
00430     if(k) {
00431         /* set as new keys */
00432         ds->setKeys(k);
00433 
00434         ts.current_column = 0;
00435         ts.kRep = k;
00436     
00437         browseView->reset();
00438         listView->reset();
00439         filterView->reset();
00440 
00441         browseView->rebuildKeys();
00442         listView->rebuildKeys();
00443         filterView->rebuildKeys();
00444 
00445         ds->first();
00446         /* set up the list view */
00447         listView->clearItems();
00448         do {
00449             d = ds->getCurrentData();
00450             if(d) 
00451                 listView->addItem(d);
00452         } while(ds->next());
00453 
00454         /* Set up browse view, Will be based of structure of listView */
00455   dirty = TRUE;
00456     }
00457 }
00458 
00463 void TableViewerWindow::searchOnKey(int i, TVVariant v)
00464 {
00465     listView->findItem(i, v);
00466     ts.current_elem = listView->getCurrentData();
00467     browseView->rebuildData();
00468 }
00469 
00470 void TableViewerWindow::setPrimaryKey(int i)
00471 {
00472     ts.current_column = i;
00473     listView->rebuildData();
00474     browseView->rebuildData();
00475 }

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