summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/tableviewer.cpp
blob: 9538cb37053ca1c4107832633934d6aea1343bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/ 

#include "tableviewer.h"
#include "ui/tvbrowseview.h"
#include "ui/tvfilterview.h"
#include "ui/tvlistview.h"
#include "ui/tveditview.h"
#include "ui/tvkeyedit.h"
#include "db/datacache.h"

/* OPIE */
#include <opie2/odebug.h>
#include <qpe/fileselector.h>
#include <qpe/resource.h>
using namespace Opie::Core;

/* QT */
#include <qmenubar.h>
#include <qtoolbar.h>
#include <qpopupmenu.h>
#include <qapplication.h>
#include <qwidgetstack.h>
#include <qlayout.h>
#include <qbuffer.h>

/*!
    \class TableViewerWindow
    \brief The main window widget of the application

    This is the main widget of the table viewer application.  
    It is the co-ordination point.
*/

/*!
    Constructs a new TableViewerWindow
*/
TableViewerWindow::TableViewerWindow(QWidget *parent, const char *name, WFlags f)
    : QMainWindow(parent, name, f)
{
    setCaption(tr("Table Viewer"));

/* Build data */
    ds = new DBStore();
    doc.setType("text/x-xml-tableviewer");
    doc.setName("table");

    dirty = FALSE;
    ts.current_column = 0;
    ts.kRep = ds->getKeys();

/* build menus */
    menu = new QMenuBar(this, 0);

    QPopupMenu *file_menu = new QPopupMenu;
    file_menu->insertItem("New", this, SLOT(newDocument()));

    file_menu->insertItem("Open", this, SLOT(selectDocument()));
    file_menu->insertSeparator();
    file_menu->insertItem("Properties");

    /* later will want to set this up to clean up first via this, SLOT(quit) */
    menu->insertItem("Document", file_menu); 

    QPopupMenu *edit_menu = new QPopupMenu;
    edit_menu->insertItem("Edit Item", this, SLOT(editItemSlot()));
    edit_menu->insertItem("Edit Keys", this, SLOT(editKeysSlot()));
    edit_menu->insertItem("Edit filters", this, SLOT(filterViewSlot()));
    menu->insertItem("Edit", edit_menu);

    QPopupMenu *view_menu = new QPopupMenu;
    view_menu->insertItem("Browse View", this, SLOT(browseViewSlot()));
    view_menu->insertItem("List View", this, SLOT(listViewSlot()));
    menu->insertItem("View", view_menu); 

    QVBoxLayout *main_layout = new QVBoxLayout;

    /* Build tool bar */
    navigation = new QToolBar(this, "navigation");
    QToolButton *newItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("new")), "New Item", QString::null,
      this, SLOT(newItemSlot()), navigation, "New Item");
    QToolButton *editItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("edit")), "Edit Item", QString::null,
      this, SLOT(editItemSlot()), navigation, "Edit Item");
    QToolButton *deleteItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("trash")), "Delete Item", 
      QString::null, this, 
      SLOT(deleteItemSlot()), navigation, "Delete Item");

    navigation->addSeparator();

    QToolButton *firstItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("fastback")), "First Item", 
      QString::null, this, 
      SLOT(firstItem()), navigation, "First Item");
    QToolButton *previousItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("back")), "Previous Item", 
      QString::null, this, 
      SLOT(previousItem()), navigation, "Previous Item");
    QToolButton *nextItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("forward")), "Next Item", 
      QString::null, this, 
      SLOT(nextItem()), navigation, "Next Item");
    QToolButton *lastItemButton = new QToolButton(
      QIconSet(Resource::loadPixmap("fastforward")), "Last Item", 
      QString::null, this, 
      SLOT(lastItem()), navigation, "Last Item");

    navigation->addSeparator();
    QToolButton *browseButton = new QToolButton(
      QIconSet(Resource::loadPixmap("day")), "View Single Item", 
      QString::null, this, 
      SLOT(browseViewSlot()), navigation, "View Single Item");
    QToolButton *listButton = new QToolButton(
      QIconSet(Resource::loadPixmap("month")), "View Multiple Items", 
      QString::null, this, 
      SLOT(listViewSlot()), navigation, "View Multiple Items");

    setToolBarsMovable(FALSE);
    setToolBarsMovable(FALSE);
    setToolBarsMovable(FALSE);

/* Build widgets */
    browseView = new TVBrowseView(&ts, this, 0);
    listView = new TVListView(&ts, this, 0);
    filterView = new TVFilterView(&ts, this, 0);
    fileSelector = new FileSelector("text/csv;text/x-xml-tableviewer", 
                this, "fileselector");
    fileSelector->setNewVisible(FALSE);
    fileSelector->setCloseVisible(FALSE);

    cw = new QWidgetStack(this, 0);
    cw->addWidget(listView, ListState);
    cw->addWidget(browseView, BrowseState);
    cw->addWidget(filterView, FilterState);
    cw->addWidget(fileSelector, FileState);

    current_view = FileState;
    cw->raiseWidget(current_view);
    fileSelector->reread();

    connect(browseView, SIGNAL(searchOnKey(int,TVVariant)), 
            this, SLOT(searchOnKey(int,TVVariant)));
    connect(browseView, SIGNAL(sortChanged(int)), 
            this, SLOT(setPrimaryKey(int)));

    connect(fileSelector, SIGNAL(closeMe()), this, SLOT(browseViewSlot()));
    connect(fileSelector, SIGNAL(fileSelected(const DocLnk&)), 
             this, SLOT(openDocument(const DocLnk&)));

    main_layout->addWidget(menu);
    main_layout->addWidget(cw);

    setCentralWidget(cw);

}

/*!
    Destroys the TableViewerWindow
*/
TableViewerWindow::~TableViewerWindow()
{
    if(dirty)
      saveDocument();
}

/*!
    Opens a file dialog and loads the file specified by the dialog
*/
void TableViewerWindow::selectDocument()
{
    if(dirty)
  saveDocument();
    current_view = FileState;
    cw->raiseWidget(current_view);
    fileSelector->reread();
}

void TableViewerWindow::saveDocument()
{
    if(!dirty)
  return;

    FileManager fm;
    QIODevice *dev = fm.saveFile(doc);

    if(!ds->saveSource(dev, doc.type())){
  owarn << "Save unsuccessful" << oendl; 
  return;
    }
    dev->close();
    dirty = FALSE;
}

void TableViewerWindow::newDocument()
{
    DocLnk nf;
    nf.setType("text/x-xml-tableviewer");
    nf.setName("table");
   
    delete ds;
    ds = new DBStore();
   
    ts.current_column = 0;
    ts.kRep = ds->getKeys();
    browseView->reset();
    listView->reset();
    filterView->reset();

    doc = nf;
    dirty = FALSE;

    current_view = BrowseState;
    cw->raiseWidget(current_view);

  /* now set up for editing the keys */
  ts.kRep->addKey("key", TVVariant::String);
  editKeysSlot();
}

void TableViewerWindow::setDocument(const QString &f)
{
    openDocument(DocLnk(f, TRUE));
}

void TableViewerWindow::openDocument(const DocLnk &f)
{

    if (!f.isValid()) 
  return;

    FileManager fm;
    QIODevice *dev = fm.openFile(f);
    doc = f;

    if(ds->openSource(dev, doc.type())) {
        DataElem *d;

        browseView->reset();
        listView->reset();
        filterView->reset();

  current_view = BrowseState;
  cw->raiseWidget(current_view);

        /* set up new table state and ensure sub widgets have a reference */
        ts.current_column = 0;
        ts.kRep = ds->getKeys();
        browseView->rebuildKeys();
        listView->rebuildKeys();
        filterView->rebuildKeys();

        ds->first();
        /* set up the list view */
        listView->clearItems();
        do {
            d = ds->getCurrentData();
            if(d) 
                listView->addItem(d);
        } while(ds->next());

        /* Set up browse view, Will be based of structure of listView */
        listView->first();
        ts.current_elem = listView->getCurrentData();
        browseView->rebuildData();
        listView->rebuildData();

        QString scratch = "Table Viewer";/* later take from constant */
        scratch += " - ";
        scratch += ds->getName();
        setCaption(tr(scratch));

  dirty = FALSE;
    } else {
        owarn << tr("could not load Document") << oendl; 
    }
    dev->close();
}

/*! 
    Moves to the first item of the current table 
*/
void TableViewerWindow::firstItem()
{
    listView->first();
    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();
}

/*! 
    Moves to the lat item of the current table 
*/
void TableViewerWindow::lastItem()
{
    listView->last();
    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();
}

/*! 
    Moves to the next item of the current table 
*/
void TableViewerWindow::nextItem()
{
    listView->next();
    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();
}

/*! 
    Moves to the previous item of the current table 
*/
void TableViewerWindow::previousItem()
{
    listView->previous();
    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();
}

/*!
    Raises the List View.  This is a mode change for the application.
*/
void TableViewerWindow::listViewSlot()
{
    if(current_view == FilterState)
        applyFilter();
    current_view = ListState;
    cw->raiseWidget(current_view);
}

void TableViewerWindow::applyFilter()
{
    DataElem *d;

    listView->clearItems();
    ds->first();
    do {
        d = ds->getCurrentData();
        if(d) 
            if(filterView->passesFilter(d))
    listView->addItem(d);
    } while(ds->next());
    listView->first();
    listView->rebuildData();
}

/*!
    Raises the Browse View.  This is a mode change for the application.
*/
void TableViewerWindow::browseViewSlot()
{
    if(current_view == FilterState)
        applyFilter();

    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();

    current_view = BrowseState;
    cw->raiseWidget(current_view);
}

/*!
    Raises the List View.  This is a mode change for the application.
*/
void TableViewerWindow::filterViewSlot()
{
    current_view = FilterState;
    cw->raiseWidget(current_view);
}




void TableViewerWindow::editItemSlot()
{
    if(TVEditView::openEditItemDialog(&ts, ts.current_elem, this)) {
  listView->rebuildData();
  browseView->rebuildData();
  dirty = TRUE;
    }
}

void TableViewerWindow::newItemSlot()
{
    DataElem *d = new DataElem(ds);
    if (TVEditView::openEditItemDialog(&ts, d, this)) {

  ds->addItem(d);
  ts.current_elem = d;
  applyFilter();
  listView->rebuildData();
  browseView->rebuildData();
  dirty = TRUE;
    }
}

void TableViewerWindow::deleteItemSlot()
{
  /* delete the actual item, then do a 'filter' */
  DataElem *to_remove = ts.current_elem;

  if(!to_remove) 
    return;

    listView->removeItem();
  ds->removeItem(to_remove);
    
    applyFilter();
    listView->rebuildData();
    browseView->rebuildData();
    dirty = TRUE;
}

void TableViewerWindow::editKeysSlot()
{
    DataElem *d;
    KeyList *k = TVKeyEdit::openEditKeysDialog(&ts, this);

    if(k) {
        /* set as new keys */
        ds->setKeys(k);

        ts.current_column = 0;
        ts.kRep = k;
    
        browseView->reset();
        listView->reset();
        filterView->reset();

        browseView->rebuildKeys();
        listView->rebuildKeys();
        filterView->rebuildKeys();

        ds->first();
        /* set up the list view */
        listView->clearItems();
        do {
            d = ds->getCurrentData();
            if(d) 
                listView->addItem(d);
        } while(ds->next());

        /* Set up browse view, Will be based of structure of listView */
  dirty = TRUE;
    }
}

/*! 
    A Slot that allows for widgets above to indicate a search should be 
    done on a specified key index for a specified value 
*/
void TableViewerWindow::searchOnKey(int i, TVVariant v)
{
    listView->findItem(i, v);
    ts.current_elem = listView->getCurrentData();
    browseView->rebuildData();
}

void TableViewerWindow::setPrimaryKey(int i)
{
    ts.current_column = i;
    listView->rebuildData();
    browseView->rebuildData();
}