2
* \file configtable.cpp
3
* Context menu commands configuration table.
9
* Copyright (C) 2009 Urs Fleisch
11
* This file is part of Kid3.
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.
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.
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/>.
27
#include "configtable.h"
29
#if QT_VERSION >= 0x040000
31
#include <QHeaderView>
33
#include <qpopupmenu.h>
36
/** Column indices. */
48
* @param labels column labels
49
* @param parent parent widget
51
#if QT_VERSION >= 0x040000
52
ConfigTable::ConfigTable(const QStringList& labels, QWidget* parent) :
55
const int numColumns = labels.size();
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&)));
67
ConfigTable::ConfigTable(const QStringList& labels, QWidget* parent) :
70
const int numColumns = labels.size();
72
setNumCols(numColumns);
73
setColumnLabels(labels);
74
for (int col = 0; col < numColumns; ++col) {
75
setColumnStretchable(col, true);
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&)));
87
ConfigTable::~ConfigTable() {}
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,
95
* @param row table row of changed item
96
* @param col table column of changed item
98
#if QT_VERSION >= 0x040000
99
void ConfigTable::valueChanged(int row, int col)
101
QTableWidgetItem* twi;
102
if (row == rowCount() - 1 && col == columnCount() - 1 &&
103
(twi = item(row, col)) != 0) {
104
if (twi->text().isEmpty()) {
114
void ConfigTable::valueChanged(int row, int col)
116
if (row == numRows() - 1 && col == numCols() - 1) {
117
if (text(row, col).isEmpty()) {
129
* Insert a new row into the table.
131
* @param row the new row is inserted after this row
133
void ConfigTable::addRow(int row)
135
#if QT_VERSION >= 0x040000
138
QTableWidgetItem* twi;
139
for (int col = 0; col < columnCount(); ++col) {
140
if ((twi = item(row + 1, col)) != 0)
143
setItem(row + 1, col, new QTableWidgetItem(""));
151
* Delete a row from the table.
153
* @param row row to delete
155
void ConfigTable::deleteRow(int row)
157
#if QT_VERSION >= 0x040000
158
if (rowCount() <= 1) return;
164
* Clear a row in the table.
166
* @param row row to clear
168
void ConfigTable::clearRow(int row)
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("");
177
for (int col = 0; col < numCols(); ++col) {
178
setText(row, col, "");
184
* Execute a context menu action.
186
* @param action action of selected menu
188
#if QT_VERSION >= 0x040000
189
void ConfigTable::executeAction(QAction* action)
192
int row = action->data().toInt();
210
void ConfigTable::executeAction(QAction*) {}
214
* Display context menu.
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
220
void ConfigTable::contextMenu(int row, int /* col */, const QPoint& pos)
222
#if QT_VERSION >= 0x040000
226
action = menu.addAction(i18n("&Insert row"));
227
if (action) action->setData((row << 2) | 0);
230
action = menu.addAction(i18n("&Delete row"));
231
if (action) action->setData((row << 2) | 1);
234
action = menu.addAction(i18n("&Clear row"));
235
if (action) action->setData((row << 2) | 2);
237
connect(&menu, SIGNAL(triggered(QAction*)), this, SLOT(executeAction(QAction*)));
239
QPopupMenu menu(this);
241
menu.insertItem(i18n("&Insert row"), this, SLOT(addRow(int)), 0, 0);
242
menu.setItemParameter(0, row);
245
menu.insertItem(i18n("&Delete row"), this, SLOT(deleteRow(int)), 0, 1);
246
menu.setItemParameter(1, row);
249
menu.insertItem(i18n("&Clear row"), this, SLOT(clearRow(int)), 0, 2);
250
menu.setItemParameter(2, row);
253
menu.setMouseTracking(true);
257
#if QT_VERSION >= 0x040000
259
* Display custom context menu.
261
* @param pos position where context menu is drawn on screen
263
void ConfigTable::customContextMenu(const QPoint& pos)
265
QTableWidgetItem* item = itemAt(pos);
267
#if QT_VERSION >= 0x040200
268
contextMenu(item->row(), item->column(), mapToGlobal(pos));
270
contextMenu(currentRow(), currentColumn(), mapToGlobal(pos));
275
void ConfigTable::customContextMenu(const QPoint&) {}
279
* Set the values from a map.
281
* @param map map with keys and values
283
void ConfigTable::fromMap(const QMap<QString, QString>& map)
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) {
294
if ((twi = item(i, 0)) != 0)
295
twi->setText(it.key());
297
setItem(i, 0, new QTableWidgetItem(it.key()));
299
if ((twi = item(i, 1)) != 0)
302
setItem(i, 1, new QTableWidgetItem(*it));
304
if (rowCount() <= i) {
307
// add an empty row as last row and remove all rows below
308
if ((twi = item(i, 0)) != 0)
311
setItem(i, 0, new QTableWidgetItem(""));
313
if ((twi = item(i, 1)) != 0)
316
setItem(i, 1, new QTableWidgetItem(""));
318
int row = rowCount();
320
if (numCols() < 2) return;
321
for (i = 0, it = map.begin(); it != map.end(); ++it, ++i) {
322
if (numRows() <= i) {
325
setText(i, 0, it.key());
326
setText(i, 1, it.data());
328
if (numRows() <= i) {
331
// add an empty row as last row and remove all rows below
342
* Store the values in a map.
344
* @param map to be filled
346
void ConfigTable::toMap(QMap<QString, QString>& map) const
349
#if QT_VERSION >= 0x040000
350
if (columnCount() < 2) return;
351
for (int i = 0; i < rowCount(); ++i) {
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();
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);