~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to src/toresultdata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-04-07 13:16:05 UTC
  • mfrom: (1.2.7 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090407131605-u422yigfv7jgg0l0
Tags: 2.0.0-3
* Cleaned up packaging a little bit.
* Added homepage information to control file.
* Bumped Standards-Version to 3.8.1.
* Released to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* BEGIN_COMMON_COPYRIGHT_HEADER
 
3
 *
 
4
 * TOra - An Oracle Toolkit for DBA's and developers
 
5
 * 
 
6
 * Shared/mixed copyright is held throughout files in this product
 
7
 * 
 
8
 * Portions Copyright (C) 2000-2001 Underscore AB
 
9
 * Portions Copyright (C) 2003-2005 Quest Software, Inc.
 
10
 * Portions Copyright (C) 2004-2008 Numerous Other Contributors
 
11
 * 
 
12
 * This program is free software; you can redistribute it and/or
 
13
 * modify it under the terms of the GNU General Public License
 
14
 * as published by the Free Software Foundation;  only version 2 of
 
15
 * the License is valid for this program.
 
16
 * 
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 * 
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, write to the Free Software
 
24
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
 * 
 
26
 *      As a special exception, you have permission to link this program
 
27
 *      with the Oracle Client libraries and distribute executables, as long
 
28
 *      as you follow the requirements of the GNU GPL in regard to all of the
 
29
 *      software in the executable aside from Oracle client libraries.
 
30
 * 
 
31
 *      Specifically you are not permitted to link this program with the
 
32
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 
33
 *      And you are not permitted to distribute binaries compiled against
 
34
 *      these libraries. 
 
35
 * 
 
36
 *      You may link this product with any GPL'd Qt library.
 
37
 * 
 
38
 * All trademarks belong to their respective owners.
 
39
 *
 
40
 * END_COMMON_COPYRIGHT_HEADER */
 
41
 
 
42
#include "config.h"
 
43
#include "toresultdata.h"
 
44
#include "utils.h"
 
45
#include "toconf.h"
 
46
#include "toconfiguration.h"
 
47
#include "toresultdatasingle.h"
 
48
 
 
49
#include "ui_toresultcontentfilterui.h"
 
50
 
 
51
#include <QToolBar>
 
52
#include <QAction>
 
53
#include <QVBoxLayout>
 
54
#include <QMessageBox>
 
55
#include <QCloseEvent>
 
56
 
 
57
#include "icons/refresh.xpm"
 
58
#include "icons/addrecord.xpm"
 
59
#include "icons/canceledit.xpm"
 
60
#include "icons/duplicaterecord.xpm"
 
61
#include "icons/filter.xpm"
 
62
#include "icons/forward.xpm"
 
63
#include "icons/next.xpm"
 
64
#include "icons/nofilter.xpm"
 
65
#include "icons/previous.xpm"
 
66
#include "icons/rewind.xpm"
 
67
#include "icons/filesave.xpm"
 
68
#include "icons/single.xpm"
 
69
#include "icons/trash.xpm"
 
70
 
 
71
 
 
72
toResultData::toResultData(QWidget *parent,
 
73
                           const char *name,
 
74
                           Qt::WindowFlags f)
 
75
    : QWidget(parent, f)
 
76
{
 
77
    AllFilter = false;
 
78
    Discard   = false;
 
79
 
 
80
    if (name)
 
81
        setObjectName(name);
 
82
 
 
83
    QVBoxLayout *vbox = new QVBoxLayout;
 
84
    vbox->setContentsMargins(0, 0, 0, 0);
 
85
    vbox->setSpacing(0);
 
86
 
 
87
    QToolBar *toolbar = toAllocBar(this, tr("Content editor"));
 
88
    vbox->addWidget(toolbar);
 
89
 
 
90
    Edit = new toResultTableViewEdit(false, false, this);
 
91
    vbox->addWidget(Edit);
 
92
 
 
93
    Form = new toResultDataSingle(this);
 
94
    vbox->addWidget(Form);
 
95
 
 
96
    connect(Edit,
 
97
            SIGNAL(firstResult(
 
98
                       const QString &,
 
99
                       const toConnection::exception &,
 
100
                       bool)),
 
101
            this,
 
102
            SLOT(updateForm()));
 
103
 
 
104
    singleRecordForm(false);
 
105
 
 
106
    filterAct = toolbar->addAction(
 
107
                    QIcon(QPixmap(const_cast<const char**>(filter_xpm))),
 
108
                    tr("Define filter for editor"));
 
109
    filterAct->setCheckable(true);
 
110
    connect(filterAct, SIGNAL(triggered(bool)), this, SLOT(changeFilter(bool)));
 
111
 
 
112
    removeAct = toolbar->addAction(
 
113
                    QIcon(QPixmap(const_cast<const char**>(nofilter_xpm))),
 
114
                    tr("Remove filters"));
 
115
    connect(removeAct, SIGNAL(triggered(bool)), this, SLOT(removeFilter(bool)));
 
116
 
 
117
    toolbar->addSeparator();
 
118
 
 
119
    saveAct = toolbar->addAction(
 
120
                  QIcon(QPixmap(const_cast<const char**>(filesave_xpm))),
 
121
                  tr("Save changes"));
 
122
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
123
    connect(Edit, SIGNAL(changed(bool)), saveAct, SLOT(setEnabled(bool)));
 
124
 
 
125
    addAct = toolbar->addAction(
 
126
                 QIcon(QPixmap(const_cast<const char**>(addrecord_xpm))),
 
127
                 tr("Add a new record"));
 
128
    connect(addAct, SIGNAL(triggered()), this, SLOT(addRecord()));
 
129
 
 
130
    duplicateAct = toolbar->addAction(
 
131
                       QIcon(QPixmap(const_cast<const char**>(duplicaterecord_xpm))),
 
132
                       tr("Duplicate an existing record"));
 
133
    connect(duplicateAct, SIGNAL(triggered()), Edit, SLOT(duplicateRecord()));
 
134
 
 
135
    deleteAct = toolbar->addAction(
 
136
                    QIcon(QPixmap(const_cast<const char**>(trash_xpm))),
 
137
                    tr("Delete current record from table"));
 
138
    connect(deleteAct, SIGNAL(triggered()), Edit, SLOT(deleteRecord()));
 
139
 
 
140
    toolbar->addSeparator();
 
141
 
 
142
    refreshAct = toolbar->addAction(
 
143
                     QIcon(QPixmap(const_cast<const char**>(refresh_xpm))),
 
144
                     tr("Refresh data"));
 
145
    connect(refreshAct, SIGNAL(triggered()), this, SLOT(refreshWarn()));
 
146
 
 
147
    toolbar->addSeparator();
 
148
 
 
149
    firstAct = toolbar->addAction(
 
150
                   QIcon(QPixmap(const_cast<const char**>(rewind_xpm))),
 
151
                   tr("Go to first row"));
 
152
 
 
153
    previousAct = toolbar->addAction(
 
154
                      QIcon(QPixmap(const_cast<const char**>(previous_xpm))),
 
155
                      tr("Go to previous row"));
 
156
 
 
157
    nextAct = toolbar->addAction(
 
158
                  QIcon(QPixmap(const_cast<const char**>(next_xpm))),
 
159
                  tr("Go to next row"));
 
160
 
 
161
    lastAct = toolbar->addAction(
 
162
                  QIcon(QPixmap(const_cast<const char**>(forward_xpm))),
 
163
                  tr("Go to last row"));
 
164
 
 
165
    toolbar->addSeparator();
 
166
 
 
167
    singleAct = toolbar->addAction(
 
168
                    QIcon(QPixmap(const_cast<const char**>(single_xpm))),
 
169
                    tr("Toggle between table or single record editing"));
 
170
    singleAct->setCheckable(true);
 
171
    connect(singleAct, SIGNAL(toggled(bool)), this, SLOT(singleRecordForm(bool)));
 
172
 
 
173
    connect(toolbar,
 
174
            SIGNAL(actionTriggered(QAction *)),
 
175
            this,
 
176
            SLOT(navigate(QAction *)));
 
177
 
 
178
    setLayout(vbox);
 
179
}
 
180
 
 
181
 
 
182
void toResultData::query(const QString &, const toQList &params)
 
183
{
 
184
    if (!maybeSave())
 
185
        return;
 
186
 
 
187
    Discard = false;
 
188
 
 
189
    if (params.size() == 2)
 
190
    {
 
191
        toQList::const_iterator par = params.begin();
 
192
        Owner = *par;
 
193
        par++;
 
194
        Table = *par;
 
195
    }
 
196
 
 
197
    /* Always check, if either is empty, query will fail */
 
198
    if (Owner.isEmpty() || Table.isEmpty())
 
199
    {
 
200
        /* Need to clear columns here */
 
201
        return;
 
202
    }
 
203
 
 
204
    FilterName = filterName();
 
205
    bool filter = AllFilter || Criteria.contains(FilterName);
 
206
    filterAct->setChecked(filter);
 
207
 
 
208
    toConnection &conn = connection();
 
209
 
 
210
    SQL = "SELECT * FROM %1.%2";
 
211
    SQL = SQL.arg(conn.quote(Owner)).arg(conn.quote(Table));
 
212
 
 
213
    bool where = false;
 
214
    if (filter && !Criteria[FilterName].isEmpty())
 
215
    {
 
216
        SQL += " WHERE ";
 
217
        SQL += Criteria[FilterName];
 
218
        SQL += " ";
 
219
        where = true;
 
220
    }
 
221
 
 
222
    if (filter && !Order[FilterName].isEmpty())
 
223
    {
 
224
        SQL += " ORDER BY ";
 
225
        SQL += Order[FilterName];
 
226
    }
 
227
 
 
228
    Edit->query(SQL, params);
 
229
}
 
230
 
 
231
 
 
232
void toResultData::exportData(std::map<QString, QString> &data,
 
233
                              const QString &prefix)
 
234
{
 
235
    ;                           // stub
 
236
}
 
237
 
 
238
 
 
239
void toResultData::importData(std::map<QString, QString> &data,
 
240
                              const QString &prefix)
 
241
{
 
242
    ;                           // stub
 
243
}
 
244
 
 
245
 
 
246
void toResultData::closeEvent(QCloseEvent *event)
 
247
{
 
248
    if (maybeSave())
 
249
        event->accept();
 
250
    else
 
251
        event->ignore();
 
252
}
 
253
 
 
254
 
 
255
void toResultData::navigate(QAction *action)
 
256
{
 
257
    Edit->setFocus();
 
258
    if (!Edit->selectionModel())
 
259
        return;
 
260
 
 
261
    QModelIndex current = Edit->selectionModel()->currentIndex();
 
262
    int row = current.row();
 
263
    int col = current.column();
 
264
 
 
265
    if (col < 1)
 
266
        col = 1;                // can't select hidden first column
 
267
 
 
268
    if (action == firstAct)
 
269
        row = 0;
 
270
    else if (action == previousAct)
 
271
        row--;
 
272
    else if (action == nextAct)
 
273
        row++;
 
274
    else if (action == lastAct)
 
275
        row = Edit->model()->rowCount() - 1;
 
276
    else
 
277
        return;                 // not a nav action
 
278
 
 
279
    if (row < 0)
 
280
        row = 0;
 
281
    if (row >= Edit->model()->rowCount())
 
282
        row = Edit->model()->rowCount() - 1;
 
283
 
 
284
    QModelIndex left = Edit->model()->createIndex(row, col);
 
285
    Edit->selectionModel()->select(QItemSelection(left, left),
 
286
                                   QItemSelectionModel::ClearAndSelect);
 
287
    Edit->setCurrentIndex(left);
 
288
 
 
289
    // will update form if shown
 
290
    singleRecordForm(singleAct->isChecked());
 
291
}
 
292
 
 
293
 
 
294
QString toResultData::filterName()
 
295
{
 
296
    QString nam;
 
297
    if (AllFilter)
 
298
        nam = "";
 
299
    else
 
300
    {
 
301
        nam = Owner;
 
302
        nam += ".";
 
303
        nam += Table;
 
304
    }
 
305
 
 
306
    return nam;
 
307
}
 
308
 
 
309
 
 
310
void toResultData::removeFilter(bool)
 
311
{
 
312
    if (!AllFilter)
 
313
    {
 
314
        switch (TOMessageBox::information(
 
315
                    this,
 
316
                    tr("Remove filter"),
 
317
                    tr("Remove the filter for this table only or for all tables?"),
 
318
                    tr("&All"), tr("&This"), tr("Cancel"), 0))
 
319
        {
 
320
        case 0:
 
321
            Criteria.clear();
 
322
            Order.clear();
 
323
            // Intentionally no break
 
324
        case 1:
 
325
            Criteria.remove(FilterName);
 
326
            Order.remove(FilterName);
 
327
            filterAct->setChecked(false);
 
328
            break;
 
329
        case 2:
 
330
            return;
 
331
        }
 
332
    }
 
333
    else
 
334
    {
 
335
        QString nam = "";
 
336
        Criteria.remove(nam);
 
337
        Order.remove(nam);
 
338
        AllFilter = false;
 
339
        filterAct->setChecked(false);
 
340
    }
 
341
 
 
342
    maybeSave();
 
343
    refresh();
 
344
}
 
345
 
 
346
 
 
347
void toResultData::changeFilter(bool checked)
 
348
{
 
349
    QDialog dialog(this);
 
350
    Ui::toResultContentFilterUI filter;
 
351
    filter.setupUi(&dialog);
 
352
 
 
353
    FilterName = filterName();
 
354
 
 
355
    filter.AllTables->setChecked(AllFilter);
 
356
    filter.Order->setText(Order[FilterName]);
 
357
    filter.Criteria->setText(Criteria[FilterName]);
 
358
 
 
359
    filter.Columns->changeParams(Owner, Table);
 
360
 
 
361
    if (dialog.exec())
 
362
    {
 
363
        AllFilter = filter.AllTables->isChecked();
 
364
 
 
365
        FilterName = filterName();
 
366
        Criteria[FilterName] = filter.Criteria->text();
 
367
        Order[FilterName] = filter.Order->text();
 
368
 
 
369
        maybeSave();
 
370
        refresh();
 
371
    }
 
372
 
 
373
    if (Criteria[FilterName].isEmpty() && Order[FilterName].isEmpty())
 
374
        filterAct->setChecked(false);
 
375
    else
 
376
        filterAct->setChecked(true);
 
377
}
 
378
 
 
379
 
 
380
bool toResultData::maybeSave(void)
 
381
{
 
382
    if (!Edit->changed())
 
383
        return true;
 
384
    if (!isVisible())
 
385
        return true;
 
386
 
 
387
    // grab focus so user can see file and decide to save
 
388
    setFocus(Qt::OtherFocusReason);
 
389
 
 
390
    int ret = TOMessageBox::information(
 
391
                  this,
 
392
                  tr("Save changes"),
 
393
                  tr("Save changes to %1.%2?").arg(Owner).arg(Table),
 
394
                  QMessageBox::Save |
 
395
                  QMessageBox::Discard |
 
396
                  QMessageBox::Cancel);
 
397
 
 
398
    if (ret == QMessageBox::Save)
 
399
    {
 
400
        if (Edit->commitChanges())
 
401
            emit changesSaved();
 
402
        return true;
 
403
    }
 
404
    else if (ret == QMessageBox::Discard)
 
405
        return Discard = true;
 
406
    else
 
407
        return false;
 
408
}
 
409
 
 
410
 
 
411
void toResultData::singleRecordForm(bool display)
 
412
{
 
413
    if (display)
 
414
    {
 
415
        if (!Edit->selectionModel()->currentIndex().isValid())
 
416
        {
 
417
            QModelIndex left = Edit->model()->createIndex(0, 0);
 
418
            Edit->selectionModel()->select(QItemSelection(left, left),
 
419
                                           QItemSelectionModel::ClearAndSelect);
 
420
            Edit->setCurrentIndex(left);
 
421
        }
 
422
        Form->changeRow(Edit->selectionModel()->currentIndex().row());
 
423
    }
 
424
    else {
 
425
        if(Form->currentRow() >= 0) {
 
426
            // set selection to same as the form's
 
427
            QModelIndex left = Edit->model()->createIndex(Form->currentRow(), 1);
 
428
            Edit->selectionModel()->select(QItemSelection(left, left),
 
429
                                           QItemSelectionModel::ClearAndSelect);
 
430
            Edit->setCurrentIndex(left);
 
431
        }
 
432
    }
 
433
 
 
434
    Form->setVisible(display);
 
435
    Edit->setVisible(!display);
 
436
}
 
437
 
 
438
 
 
439
void toResultData::updateForm()
 
440
{
 
441
    // update single edit form
 
442
    if (Edit->model())
 
443
    {
 
444
        Form->changeSource(Edit->model(),
 
445
                           Edit->selectionModel()->currentIndex().row());
 
446
    }
 
447
}
 
448
 
 
449
 
 
450
void toResultData::save()
 
451
{
 
452
    if (Edit->commitChanges())
 
453
        emit changesSaved();
 
454
}
 
455
 
 
456
 
 
457
void toResultData::addRecord()
 
458
{
 
459
    Edit->addRecord();
 
460
    // don't navigate here. done by Edit.
 
461
//     navigate(lastAct);
 
462
}
 
463
 
 
464
 
 
465
void toResultData::refreshWarn()
 
466
{
 
467
    if (Edit->changed())
 
468
    {
 
469
        switch (TOMessageBox(
 
470
                    QMessageBox::Warning,
 
471
                    tr("Warning"),
 
472
                    tr("Refreshing from the database will lose current changes."),
 
473
                    QMessageBox::Ok | QMessageBox::Cancel,
 
474
                    this).exec())
 
475
        {
 
476
        case QMessageBox::Ok:
 
477
            break;
 
478
        case QMessageBox::Cancel:
 
479
            return;
 
480
        default:
 
481
            return;
 
482
        }
 
483
    }
 
484
 
 
485
    Edit->refresh();
 
486
}