[go: up one dir, main page]

File: toresulttableview.h

package info (click to toggle)
tora 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 27,560 kB
  • ctags: 21,885
  • sloc: cpp: 189,278; sh: 17,561; makefile: 692; python: 159; xml: 69; ansic: 56
file content (638 lines) | stat: -rw-r--r-- 13,592 bytes parent folder | download
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

/* BEGIN_COMMON_COPYRIGHT_HEADER
 *
 * TOra - An Oracle Toolkit for DBA's and developers
 * 
 * Shared/mixed copyright is held throughout files in this product
 * 
 * Portions Copyright (C) 2000-2001 Underscore AB
 * Portions Copyright (C) 2003-2005 Quest Software, Inc.
 * Portions Copyright (C) 2004-2008 Numerous Other Contributors
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation;  only version 2 of
 * the License is valid for this program.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 *      As a special exception, you have permission to link this program
 *      with the Oracle Client libraries and distribute executables, as long
 *      as you follow the requirements of the GNU GPL in regard to all of the
 *      software in the executable aside from Oracle client libraries.
 * 
 *      Specifically you are not permitted to link this program with the
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 *      And you are not permitted to distribute binaries compiled against
 *      these libraries. 
 * 
 *      You may link this product with any GPL'd Qt library.
 * 
 * All trademarks belong to their respective owners.
 *
 * END_COMMON_COPYRIGHT_HEADER */

#ifndef TORESULTTABLEVIEW_H
#define TORESULTTABLEVIEW_H

#include "config.h"
#include "toqvalue.h"
#include "tosql.h"
#include "toresult.h"
#include "toconnection.h"
#include "toresultmodel.h"
#include "toeditwidget.h"

#include <QAbstractTableModel>
#include <QHeaderView>
#include <QItemDelegate>
#include <QLabel>
#include <QList>
#include <QMenu>
#include <QModelIndex>
#include <QObject>
#include <QPushButton>
#include <QTableView>

class toResultStats;
class toViewFilter;
class toTableViewIterator;
class toWorkingWidget;
class toExportSettings;


/**
 * This is a simple class for providing sensible size hints to the
 * view.
 *
 */
class toResultTableViewDelegate : public QItemDelegate
{
    static const int maxWidth = 200; // the maximum size to grow a column

public:
    toResultTableViewDelegate(QObject *parent = 0)
            : QItemDelegate(parent)
    {
    }


    virtual QSize sizeHint(const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
    {
        QSize size = QItemDelegate::sizeHint(option, index);
        if (size.width() > maxWidth)
            size.setWidth(maxWidth);

        return size;
    }
};


class toResultTableView : public QTableView,
            public toResult,
            public toEditWidget
{
    Q_OBJECT;

    // Widget to store query statistics to.
    toResultStats *Statistics;

    // if edit delegate should be created
    bool Editable;

    // if all records should be read
    bool ReadAll;

    // if column headers should be modified to be readable
    bool ReadableColumns;

    // number of visible columns
    int VisibleColumns;

    // if vertical header should be displayed
    bool NumberColumn;

    // if user resized columns
    bool ColumnsResized;

    // filter object if set
    toViewFilter *Filter;

    // superimposed until model is ready
    toWorkingWidget *Working;

    // set true when model has signaled
    bool Ready;


    /**
     * context menu items. may be null.
     *
     */
    QMenu   *Menu;
    QAction *displayAct;
    QAction *refreshAct;
    QAction *leftAct;
    QAction *rightAct;
    QAction *centerAct;
    QAction *copyAct;
    QAction *copyFormatAct;
    QAction *copyTransAct;
    QAction *selectAllAct;
    QAction *exportAct;
    QAction *editAct;
    QAction *rowCountAct;
    QAction *readAllAct;

    void createActions(void);

    /*! \brief Common setup function called from constructors
    */
    void setup(bool readable, bool numberColumn, bool editable);


protected slots:
    void displayMenu(const QPoint &pos);
    void menuCallback(QAction *action);
    void handleDone(void);
    void handleReset(void);
    void handleFirst(const toConnection::exception &res,
                     bool error);
    void handleDoubleClick(const QModelIndex &);
    // override parent
    virtual void selectionChanged(const QItemSelection &selected,
                                  const QItemSelection &deselected);

    // apply column rules, numbercolumn, readable columns
    void applyColumnRules(void);

protected:
    // pointer to model
    QPointer<toResultModel> Model;

    /**
     * overridden from parent.
     *
     * Returns the size hint for the given row's height or -1 if there
     * is no model.
     *
     */
    virtual int sizeHintForRow(int row) const;


    /**
     * overrides parent to overlay working working message until model
     * is ready.
     *
     */
    virtual void paintEvent(QPaintEvent *event);


    /**
     * Overrides QWidget to resize columns when applicable.
     *
     */
    virtual void resizeEvent(QResizeEvent *event);

    /*! Catch special keyboard shortcuts.
        Copy, etc.
    */
    virtual void keyPressEvent(QKeyEvent * event);

signals:

    /**
     * Called before the menu is displayed so that you can add items
     * to it before it is shown.
     *
     * @param menu Pointer to the menu about to be shown.
     */
    void displayMenu(QMenu *menu);


    /**
     * Emitted when table's selection changes.
     *
     */
    void selectionChanged(void);

public:

    typedef toTableViewIterator iterator;


    /**
     * Creates a new tableview.
     *
     * @param readable use readable headers
     * @param numberColumn display vertical header
     * @param parent qobject parent
     * @param name sets objectName property
     * @param editable data is editable. you must setModel() with something
     * able to modify data.
     */
    toResultTableView(bool readable,
                      bool numberColumn,
                      QWidget *parent,
                      const char *name = 0,
                      bool editable = false);
    /*! \brief Constructor provided for Qt designer. See setup()
    */
    toResultTableView(QWidget * parent = 0);
    virtual ~toResultTableView(void);

    virtual bool searchNext(const QString & text);
    virtual bool searchPrevious(const QString & text);
    virtual void searchReplace(const QString & text) {};
    virtual void searchReplaceAll(const QString & text) {};
    virtual bool searchCanReplace(bool all) { return false; };

    /**
     * Reimplemented to create query and new model.
     *
     */
    virtual void query(const QString &sql, const toQList &param);


    /**
     * Reimplemented to create query and new model.
     *
     */
    void query(const QString &sql)
    {
        toQList p;
        query(sql, p);
    }


    /**
     * Set statistics widget.
     * @param stats Statistics widget.
     */
    void setStatistics(toResultStats *stats)
    {
        Statistics = stats;
    }


    /**
     * override parent to return toResultModel pointer.
     *
     */
    toResultModel* model(void) const
    {
        return Model;
    }

    /**
     * True if query is running.
     *
     */
    bool running(void);


    /**
     * Enable or disable vertical header
     *
     */
    void setNumberColumn(bool b)
    {
        NumberColumn = b;
        verticalHeader()->setVisible(b);
    }


    /**
     * True by default
     *
     */
    virtual bool canHandle(toConnection &)
    {
        return true;
    }


    /**
     * Set a filter to this list.
     *
     * @param filter The new filter or NULL if no filter is to be used.
     */
    void setFilter(toViewFilter *filter);


    /**
     * Sets the model for the view to present.
     *
     * Must be set before query()
     */
    virtual void setModel(toResultModel *model);


    /**
     * Should view read all data.
     *
     */
    virtual void setReadAll(bool b)
    {
        ReadAll = b;
    }


    /**
     * Convenience function to determine if the row indicated by index
     * is selected. Disregards column information.
     *
     */
    virtual bool isRowSelected(QModelIndex index);


    /**
     * Convenience function to return the first selected index. Useful
     * for tableviews that can only have one selected row.
     *
     * @param col returns an index with column col
     */
    virtual QModelIndex selectedIndex(int col = 1);


    /**
     * Returns 1 if number column is displayed or 0 if not.
     *
     */
    virtual int numberColumn(void)
    {
        if (NumberColumn)
            return 1;
        return 0;
    }


    /**
     * Export list as a string.
     *
     */
    QString exportAsText(toExportSettings settings);


    // ---------------------------------------- overrides toEditWidget

    /**
     * Perform a save on this widget.
     *
     * @param askfile Ask filename even if default filename is available.
     */
    virtual bool editSave(bool askfile);


    /**
     * Print this widgets contents.
     *
     */
    virtual void editPrint(void);


    /**
     * Perform copy.
     *
     */
    virtual void editCopy(void);


    /**
     * Select all contents.
     *
     */
    virtual void editSelectAll(void);



public slots:
    /**
     * Resizes all columns based on the size hints of the delegate
     * used to render each item in the columns.
     *
     * overridden from parent
     */
    void resizeColumnsToContents(void);


    /**
     * Connected to horizontal header so we know when a column was
     * resized. Prevents resizeColumnsToContents from further
     * modifying column widths.
     *
     */
    void columnWasResized(int, int, int);


    /**
     * Overrides toResult to keep column sizes and declare a slot.
     *
     */
    virtual void refresh(void);


    /**
     * Stop running query.
     *
     */
    void stop(void)
    {
        if (Model)
            Model->stop();
    }


    /**
     * apply Filter to row visibility
     *
     */
    void applyFilter(void);


signals:

    /**
     * Emitted when the first result is available.
     *
     * @param sql SQL that was run.
     * @param res String describing result.
     * @param error Error has occurred.
     */
    void firstResult(const QString &sql,
                     const toConnection::exception &res,
                     bool error);


    /**
     * Emitted when query is finished.
     *
     */
    void done(void);
};



/**
 * A simple iterator to walk the rows of a QTableView.
 *
 */
class toTableViewIterator
{
    int                         _row;
    QPointer<toResultTableView> _view;
    QModelIndex                 _index;

    friend class toTableViewIteratorItem;

    // update _index
    void updateIndex(void);

public:

    /**
     * Create an iterator starting at 0 on a view
     *
     */
    toTableViewIterator(toResultTableView *view);


    /**
     * Create a copy of other
     *
     */
    toTableViewIterator(toTableViewIterator &other);


    /**
     * Get a QModelIndex for this row. May be invalid if you've gone
     * too far, use QModelIndex.isValid()
     *
     */
    QModelIndex operator*() const;


    /**
     * Increment by one
     *
     */
    toTableViewIterator& operator++();


    /**
     * Postfix operator
     *
     */
    const toTableViewIterator operator++(int);


    /**
     * Go forward by n
     *
     */
    toTableViewIterator& operator+=(int n);


    /**
     * Decrement by one
     *
     */
    toTableViewIterator& operator--();


    /**
     * Postfix operator
     *
     */
    const toTableViewIterator operator--(int);


    /**
     * Go back by n
     *
     */
    toTableViewIterator& operator-=(int n);


    /**
     * Make a copy of it and return a reference
     *
     */
    toTableViewIterator& operator=(const toTableViewIterator &it);
};


/**
 * Baseclass for filters to apply to the view to hide out rows that
 * you don't want.
 *
 */
class toViewFilter
{

public:
    toViewFilter()
    {
    }

    virtual ~toViewFilter()
    {
    }

    virtual void startingQuery(void)
    {
    }

    /**
     *  This function can inspect the item to be added and decide if
     *  it is valid for adding or not.
     *
     * @param model toResultModel for fetching data.
     * @param row to inspect.
     * @return If false is returned the item isn't added.
     */
    virtual bool check(const toResultModel *model, const int row) = 0;


    /**
     * Create a copy of this filter.
     *
     * @return A newly created copy of this filter.
     */
    virtual toViewFilter *clone(void) = 0;


    /**
     * Export data to a map.
     *
     * @param data A map that can be used to recreate the data of a chart.
     * @param prefix Prefix to add to the map.
     */
    virtual void exportData(std::map<QString, QString> &data,
                            const QString &prefix)
    {
    }


    /**
     * Import data
     *
     * @param data Data to read from a map.
     * @param prefix Prefix to read data from.
     */
    virtual void importData(std::map<QString, QString> &data,
                            const QString &prefix)
    {
    }
};


#endif