~ubuntu-branches/ubuntu/karmic/kid3/karmic

« back to all changes in this revision

Viewing changes to kid3/configtable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-20 16:12:30 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090520161230-qetp532r8ydujkz2
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file configtable.cpp
 
3
 * Context menu commands configuration table.
 
4
 *
 
5
 * \b Project: Kid3
 
6
 * \author Urs Fleisch
 
7
 * \date 13 Jan 2009
 
8
 *
 
9
 * Copyright (C) 2009  Urs Fleisch
 
10
 *
 
11
 * This file is part of Kid3.
 
12
 *
 
13
 * Kid3 is free software; you can redistribute it and/or modify
 
14
 * it under the terms of the GNU General Public License as published by
 
15
 * the Free Software Foundation; either version 2 of the License, or
 
16
 * (at your option) any later version.
 
17
 *
 
18
 * Kid3 is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 *
 
23
 * You should have received a copy of the GNU General Public License
 
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
 */
 
26
 
 
27
#include "configtable.h"
 
28
 
 
29
#if QT_VERSION >= 0x040000
 
30
#include <QMenu>
 
31
#include <QHeaderView>
 
32
#else 
 
33
#include <qpopupmenu.h>
 
34
#endif
 
35
 
 
36
/** Column indices. */
 
37
enum ColumnIndex {
 
38
        CI_Confirm,
 
39
        CI_Output,
 
40
        CI_Name,
 
41
        CI_Command,
 
42
        CI_NumColumns
 
43
};
 
44
 
 
45
/**
 
46
 * Constructor.
 
47
 *
 
48
 * @param labels column labels
 
49
 * @param parent parent widget
 
50
 */
 
51
#if QT_VERSION >= 0x040000
 
52
ConfigTable::ConfigTable(const QStringList& labels, QWidget* parent) :
 
53
        QTableWidget(parent)
 
54
{
 
55
        const int numColumns = labels.size();
 
56
        setRowCount(1);
 
57
        setColumnCount(numColumns);
 
58
        setHorizontalHeaderLabels(labels);
 
59
        horizontalHeader()->setResizeMode(QHeaderView::Stretch);
 
60
        setContextMenuPolicy(Qt::CustomContextMenu);
 
61
        connect(this, SIGNAL(cellActivated(int, int)),
 
62
                        this, SLOT(valueChanged(int, int)));
 
63
        connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
 
64
                        this, SLOT(customContextMenu(const QPoint&)));
 
65
}
 
66
#else
 
67
ConfigTable::ConfigTable(const QStringList& labels, QWidget* parent) :
 
68
        QTable(parent)
 
69
{
 
70
        const int numColumns = labels.size();
 
71
        setNumRows(1);
 
72
        setNumCols(numColumns);
 
73
        setColumnLabels(labels);
 
74
        for (int col = 0; col < numColumns; ++col) {
 
75
                setColumnStretchable(col, true);
 
76
        }
 
77
        connect(this, SIGNAL(valueChanged(int, int)),
 
78
                        this, SLOT(valueChanged(int, int)));
 
79
        connect(this, SIGNAL(contextMenuRequested(int, int, const QPoint&)),
 
80
                        this, SLOT(contextMenu(int, int, const QPoint&)));
 
81
}
 
82
#endif
 
83
 
 
84
/**
 
85
 * Destructor.
 
86
 */
 
87
ConfigTable::~ConfigTable() {}
 
88
 
 
89
/**
 
90
 * Called when a value in the table is changed.
 
91
 * If the command cell in the last row is changed to a non-empty
 
92
 * value, a new row is added. If it is changed to an empty value,
 
93
 * the row is deleted.
 
94
 *
 
95
 * @param row table row of changed item
 
96
 * @param col table column of changed item
 
97
 */
 
98
#if QT_VERSION >= 0x040000
 
99
void ConfigTable::valueChanged(int row, int col)
 
100
{
 
101
        QTableWidgetItem* twi;
 
102
        if (row == rowCount() - 1 && col == columnCount() - 1 &&
 
103
                        (twi = item(row, col)) != 0) {
 
104
                if (twi->text().isEmpty()) {
 
105
                        if (row != 0) {
 
106
                                deleteRow(row);
 
107
                        }
 
108
                } else {
 
109
                        addRow(row);
 
110
                }
 
111
        }
 
112
}
 
113
#else
 
114
void ConfigTable::valueChanged(int row, int col)
 
115
{
 
116
        if (row == numRows() - 1 && col == numCols() - 1) {
 
117
                if (text(row, col).isEmpty()) {
 
118
                        if (row != 0) {
 
119
                                deleteRow(row);
 
120
                        }
 
121
                } else {
 
122
                        addRow(row);
 
123
                }
 
124
        }
 
125
}
 
126
#endif
 
127
 
 
128
/**
 
129
 * Insert a new row into the table.
 
130
 *
 
131
 * @param row the new row is inserted after this row
 
132
 */
 
133
void ConfigTable::addRow(int row)
 
134
{
 
135
#if QT_VERSION >= 0x040000
 
136
        insertRow(row + 1);
 
137
 
 
138
        QTableWidgetItem* twi;
 
139
        for (int col = 0; col < columnCount(); ++col) {
 
140
                if ((twi = item(row + 1, col)) != 0)
 
141
                        twi->setText("");
 
142
                else
 
143
                        setItem(row + 1, col, new QTableWidgetItem(""));
 
144
        }
 
145
#else
 
146
        insertRows(row + 1);
 
147
#endif
 
148
}
 
149
 
 
150
/**
 
151
 * Delete a row from the table.
 
152
 *
 
153
 * @param row row to delete
 
154
 */
 
155
void ConfigTable::deleteRow(int row)
 
156
{
 
157
#if QT_VERSION >= 0x040000
 
158
        if (rowCount() <= 1) return;
 
159
#endif
 
160
        removeRow(row);
 
161
}
 
162
 
 
163
/**
 
164
 * Clear a row in the table.
 
165
 *
 
166
 * @param row row to clear
 
167
 */
 
168
void ConfigTable::clearRow(int row)
 
169
{
 
170
#if QT_VERSION >= 0x040000
 
171
        QTableWidgetItem* twi;
 
172
        for (int col = 0; col < columnCount(); ++col) {
 
173
                twi = item(row, col);
 
174
                if (twi) twi->setText("");
 
175
        }
 
176
#else
 
177
        for (int col = 0; col < numCols(); ++col) {
 
178
                setText(row, col, "");
 
179
        }
 
180
#endif
 
181
}
 
182
 
 
183
/**
 
184
 * Execute a context menu action.
 
185
 *
 
186
 * @param action action of selected menu
 
187
 */
 
188
#if QT_VERSION >= 0x040000
 
189
void ConfigTable::executeAction(QAction* action)
 
190
{
 
191
        if (action) {
 
192
                int row = action->data().toInt();
 
193
                int cmd = row & 3;
 
194
                row >>= 2;
 
195
                switch (cmd) {
 
196
                        case 0:
 
197
                                addRow(row);
 
198
                                break;
 
199
                        case 1:
 
200
                                deleteRow(row);
 
201
                                break;
 
202
                        case 2:
 
203
                        default:
 
204
                                clearRow(row);
 
205
                                break;
 
206
                }
 
207
        }
 
208
}
 
209
#else
 
210
void ConfigTable::executeAction(QAction*) {}
 
211
#endif
 
212
 
 
213
/**
 
214
 * Display context menu.
 
215
 *
 
216
 * @param row row at which context menu is displayed
 
217
 * @param col column at which context menu is displayed
 
218
 * @param pos position where context menu is drawn on screen
 
219
 */
 
220
void ConfigTable::contextMenu(int row, int /* col */, const QPoint& pos)
 
221
{
 
222
#if QT_VERSION >= 0x040000
 
223
        QMenu menu(this);
 
224
        QAction* action;
 
225
        if (row >= -1) {
 
226
                action = menu.addAction(i18n("&Insert row"));
 
227
                if (action) action->setData((row << 2) | 0);
 
228
        }
 
229
        if (row >= 0) {
 
230
                action = menu.addAction(i18n("&Delete row"));
 
231
                if (action) action->setData((row << 2) | 1);
 
232
        }
 
233
        if (row >= 0) {
 
234
                action = menu.addAction(i18n("&Clear row"));
 
235
                if (action) action->setData((row << 2) | 2);
 
236
        }
 
237
        connect(&menu, SIGNAL(triggered(QAction*)), this, SLOT(executeAction(QAction*)));
 
238
#else
 
239
        QPopupMenu menu(this);
 
240
        if (row >= -1) {
 
241
                menu.insertItem(i18n("&Insert row"), this, SLOT(addRow(int)), 0, 0);
 
242
                menu.setItemParameter(0, row);
 
243
        }
 
244
        if (row >= 0) {
 
245
                menu.insertItem(i18n("&Delete row"), this, SLOT(deleteRow(int)), 0, 1);
 
246
                menu.setItemParameter(1, row);
 
247
        }
 
248
        if (row >= 0) {
 
249
                menu.insertItem(i18n("&Clear row"), this, SLOT(clearRow(int)), 0, 2);
 
250
                menu.setItemParameter(2, row);
 
251
        }
 
252
#endif
 
253
        menu.setMouseTracking(true);
 
254
        menu.exec(pos);
 
255
}
 
256
 
 
257
#if QT_VERSION >= 0x040000
 
258
/**
 
259
 * Display custom context menu.
 
260
 *
 
261
 * @param pos position where context menu is drawn on screen
 
262
 */
 
263
void ConfigTable::customContextMenu(const QPoint& pos)
 
264
{
 
265
        QTableWidgetItem* item = itemAt(pos);
 
266
        if (item) {
 
267
#if QT_VERSION >= 0x040200
 
268
                contextMenu(item->row(), item->column(), mapToGlobal(pos));
 
269
#else
 
270
                contextMenu(currentRow(), currentColumn(), mapToGlobal(pos));
 
271
#endif
 
272
        }
 
273
}
 
274
#else
 
275
void ConfigTable::customContextMenu(const QPoint&) {}
 
276
#endif
 
277
 
 
278
/**
 
279
 * Set the values from a map.
 
280
 *
 
281
 * @param map map with keys and values
 
282
 */
 
283
void ConfigTable::fromMap(const QMap<QString, QString>& map)
 
284
{
 
285
        int i;
 
286
        QMap<QString, QString>::ConstIterator it;
 
287
#if QT_VERSION >= 0x040000
 
288
        if (columnCount() < 2) return;
 
289
        QTableWidgetItem* twi;
 
290
        for (i = 0, it = map.begin(); it != map.end(); ++it, ++i) {
 
291
                if (rowCount() <= i) {
 
292
                        insertRow(i);
 
293
                }
 
294
                if ((twi = item(i, 0)) != 0)
 
295
                        twi->setText(it.key());
 
296
                else
 
297
                        setItem(i, 0, new QTableWidgetItem(it.key()));
 
298
 
 
299
                if ((twi = item(i, 1)) != 0)
 
300
                        twi->setText(*it);
 
301
                else
 
302
                        setItem(i, 1, new QTableWidgetItem(*it));
 
303
        }
 
304
        if (rowCount() <= i) {
 
305
                insertRow(i);
 
306
        }
 
307
        // add an empty row as last row and remove all rows below
 
308
        if ((twi = item(i, 0)) != 0)
 
309
                twi->setText("");
 
310
        else
 
311
                setItem(i, 0, new QTableWidgetItem(""));
 
312
 
 
313
        if ((twi = item(i, 1)) != 0)
 
314
                twi->setText("");
 
315
        else
 
316
                setItem(i, 1, new QTableWidgetItem(""));
 
317
 
 
318
        int row = rowCount();
 
319
#else
 
320
        if (numCols() < 2) return;
 
321
        for (i = 0, it = map.begin(); it != map.end(); ++it, ++i) {
 
322
                if (numRows() <= i) {
 
323
                        insertRows(i);
 
324
                }
 
325
                setText(i, 0, it.key());
 
326
                setText(i, 1, it.data());
 
327
        }
 
328
        if (numRows() <= i) {
 
329
                insertRows(i);
 
330
        }
 
331
        // add an empty row as last row and remove all rows below
 
332
        setText(i, 0, "");
 
333
        setText(i, 1, "");
 
334
        int row = numRows();
 
335
#endif
 
336
        while (--row > i) {
 
337
                removeRow(row);
 
338
        }
 
339
}
 
340
 
 
341
/**
 
342
 * Store the values in a map.
 
343
 *
 
344
 * @param map to be filled
 
345
 */
 
346
void ConfigTable::toMap(QMap<QString, QString>& map) const
 
347
{
 
348
        map.clear();
 
349
#if QT_VERSION >= 0x040000
 
350
        if (columnCount() < 2) return;
 
351
        for (int i = 0; i < rowCount(); ++i) {
 
352
                QString key;
 
353
                QTableWidgetItem* twi;
 
354
                if ((twi = item(i, 0)) != 0 &&
 
355
                                !(key = twi->text()).isEmpty() &&
 
356
                                (twi = item(i, 1)) != 0) {
 
357
                        map[key] = twi->text();
 
358
                }
 
359
        }
 
360
#else
 
361
        if (numCols() < 2) return;
 
362
        for (int i = 0; i < numRows(); ++i) {
 
363
                QString key(text(i, 0));
 
364
                if (!key.isNull() && !key.isEmpty()) {
 
365
                        map[key] = text(i, 1);
 
366
                }
 
367
        }
 
368
#endif
 
369
}