commandstablemodel.cpp 7.38 KB
Newer Older
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
/**
 * \file commandstablemodel.cpp
 * Context menu commands configuration table model.
 *
 * \b Project: Kid3
 * \author Urs Fleisch
 * \date 13 Mar 2011
 *
 * Copyright (C) 2005-2011  Urs Fleisch
 *
 * This file is part of Kid3.
 *
 * Kid3 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Kid3 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, see <http://www.gnu.org/licenses/>.
 */

#include "commandstablemodel.h"
28
#include "commandformatreplacer.h"
Urs Fleisch's avatar
Urs Fleisch committed
29
#include "qtcompatmac.h"
30
31
32

/** Column indices. */
enum ColumnIndex {
Urs Fleisch's avatar
Urs Fleisch committed
33
34
35
36
37
  CI_Confirm,
  CI_Output,
  CI_Name,
  CI_Command,
  CI_NumColumns
38
39
40
41
42
43
44
};

/**
 * Constructor.
 * @param parent parent widget
 */
CommandsTableModel::CommandsTableModel(QObject* parent) :
Urs Fleisch's avatar
Urs Fleisch committed
45
  QAbstractTableModel(parent)
46
{
47
  setObjectName("CommandsTableModel");
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
}

/**
 * Destructor.
 */
CommandsTableModel::~CommandsTableModel()
{
}

/**
 * Get item flags for index.
 * @param index model index
 * @return item flags
 */
Qt::ItemFlags CommandsTableModel::flags(const QModelIndex& index) const
{
Urs Fleisch's avatar
Urs Fleisch committed
64
65
66
67
68
69
70
71
72
73
  Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
  if (index.isValid()) {
    theFlags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    if (index.column() == CI_Confirm || index.column() == CI_Output) {
      theFlags |= Qt::ItemIsUserCheckable;
    } else {
      theFlags |= Qt::ItemIsEditable;
    }
  }
  return theFlags;
74
75
76
77
78
79
80
81
82
83
}

/**
 * Get data for a given role.
 * @param index model index
 * @param role item data role
 * @return data for role
 */
QVariant CommandsTableModel::data(const QModelIndex& index, int role) const
{
Urs Fleisch's avatar
Urs Fleisch committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= m_cmdList.size() ||
      index.column() < 0 || index.column() >= CI_NumColumns)
    return QVariant();
  const MiscConfig::MenuCommand& item = m_cmdList.at(index.row());
  if (role == Qt::DisplayRole || role == Qt::EditRole) {
    switch (index.column()) {
    case CI_Name:
      return item.getName();
    case CI_Command:
      return item.getCommand();
    default: ;
    }
  }
  if (role == Qt::CheckStateRole) {
    switch (index.column()) {
    case CI_Confirm:
      return item.mustBeConfirmed() ? Qt::Checked : Qt::Unchecked;
    case CI_Output:
      return item.outputShown() ? Qt::Checked : Qt::Unchecked;
    default: ;
    }
  }
  return QVariant();
108
109
110
111
112
113
114
115
116
117
}

/**
 * Set data for a given role.
 * @param index model index
 * @param value data value
 * @param role item data role
 * @return true if successful
 */
bool CommandsTableModel::setData(const QModelIndex& index,
Urs Fleisch's avatar
Urs Fleisch committed
118
                                 const QVariant& value, int role)
119
{
Urs Fleisch's avatar
Urs Fleisch committed
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
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= m_cmdList.size() ||
      index.column() < 0 || index.column() >= CI_NumColumns)
    return false;
  MiscConfig::MenuCommand& item = m_cmdList[index.row()];
  if (role == Qt::EditRole) {
    switch (index.column()) {
    case CI_Name:
      item.setName(value.toString());
      break;
    case CI_Command:
      item.setCommand(value.toString());
      break;
    default:
      return false;
    }
  } else if (role == Qt::CheckStateRole) {
    switch (index.column()) {
    case CI_Confirm:
      item.setMustBeConfirmed(value.toInt() == Qt::Checked);
      break;
    case CI_Output:
      item.setOutputShown(value.toInt() == Qt::Checked);
      break;
    default:
      return false;
    }
  } else {
    return false;
  }
  emit dataChanged(index, index);
  return true;
152
153
154
155
156
157
158
159
160
161
}

/**
 * Get data for header section.
 * @param section column or row
 * @param orientation horizontal or vertical
 * @param role item data role
 * @return header data for role
 */
QVariant CommandsTableModel::headerData(
Urs Fleisch's avatar
Urs Fleisch committed
162
    int section, Qt::Orientation orientation, int role) const
163
{
Urs Fleisch's avatar
Urs Fleisch committed
164
165
  if (role == Qt::ToolTipRole && orientation == Qt::Horizontal &&
      section == CI_Command)
166
    return CommandFormatReplacer::getToolTip();
Urs Fleisch's avatar
Urs Fleisch committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  if (role != Qt::DisplayRole)
    return QVariant();
  if (orientation == Qt::Horizontal) {
    switch (section) {
    case CI_Confirm:
      return i18n("Confirm");
    case CI_Output:
      return i18n("Output");
    case CI_Name:
      return i18n("Name");
    case CI_Command:
      return i18n("Command");
    default:
      return section + 1;
    }
  }
  return section + 1;
184
185
186
187
188
189
190
191
192
193
}

/**
 * Get number of rows.
 * @param parent parent model index, invalid for table models
 * @return number of rows,
 * if parent is valid number of children (0 for table models)
 */
int CommandsTableModel::rowCount(const QModelIndex& parent) const
{
Urs Fleisch's avatar
Urs Fleisch committed
194
  return parent.isValid() ? 0 : m_cmdList.size();
195
196
197
198
199
200
201
202
203
204
}

/**
 * Get number of columns.
 * @param parent parent model index, invalid for table models
 * @return number of columns,
 * if parent is valid number of children (0 for table models)
 */
int CommandsTableModel::columnCount(const QModelIndex& parent) const
{
Urs Fleisch's avatar
Urs Fleisch committed
205
  return parent.isValid() ? 0 : CI_NumColumns;
206
207
208
209
210
211
212
213
214
215
}

/**
 * Insert rows.
 * @param row rows are inserted before this row, if 0 at the begin,
 * if rowCount() at the end
 * @param count number of rows to insert
 * @return true if successful
 */
bool CommandsTableModel::insertRows(int row, int count,
Urs Fleisch's avatar
Urs Fleisch committed
216
                        const QModelIndex&)
217
{
Urs Fleisch's avatar
Urs Fleisch committed
218
219
220
221
222
  beginInsertRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    m_cmdList.insert(row, MiscConfig::MenuCommand());
  endInsertRows();
  return true;
223
224
225
226
227
228
229
230
231
}

/**
 * Remove rows.
 * @param row rows are removed starting with this row
 * @param count number of rows to remove
 * @return true if successful
 */
bool CommandsTableModel::removeRows(int row, int count,
Urs Fleisch's avatar
Urs Fleisch committed
232
                        const QModelIndex&)
233
{
Urs Fleisch's avatar
Urs Fleisch committed
234
235
236
237
238
  beginRemoveRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    m_cmdList.removeAt(row);
  endRemoveRows();
  return true;
239
240
241
242
243
244
245
}

/**
 * Get the resize modes to be used for the columns.
 * @return list of resize modes for the columns
 */
QList<QHeaderView::ResizeMode>
Urs Fleisch's avatar
Urs Fleisch committed
246
    CommandsTableModel::getHorizontalResizeModes() const
247
{
Urs Fleisch's avatar
Urs Fleisch committed
248
249
250
251
252
253
254
255
256
257
  QList<QHeaderView::ResizeMode> resizeModes;
  for (int i = 0; i < CI_NumColumns; ++i) {
    QHeaderView::ResizeMode mode = QHeaderView::Interactive;
    if (i == CI_Confirm || i == CI_Output)
      mode = QHeaderView::ResizeToContents;
    else if (i == CI_Command)
      mode = QHeaderView::Stretch;
    resizeModes.append(mode);
  }
  return resizeModes;
258
259
260
261
262
263
264
}

/**
 * Set the model from the command list.
 * @param cmdList command list
 */
void CommandsTableModel::setCommandList(
Urs Fleisch's avatar
Urs Fleisch committed
265
  const QList<MiscConfig::MenuCommand>& cmdList)
266
{
Urs Fleisch's avatar
Urs Fleisch committed
267
268
  m_cmdList = cmdList;
  reset();
269
270
271
272
273
274
275
276
}

/**
 * Get the command list from the model.
 * @return command list
 */
QList<MiscConfig::MenuCommand> CommandsTableModel::getCommandList() const
{
Urs Fleisch's avatar
Urs Fleisch committed
277
278
279
280
281
282
283
284
  QList<MiscConfig::MenuCommand> cmdList;
  for (QList<MiscConfig::MenuCommand>::const_iterator it = m_cmdList.constBegin();
       it != m_cmdList.constEnd();
       ++it) {
    if (!it->getName().isEmpty()) {
      cmdList.append(*it);
    }
  }
285
286
287
288
289
  if (cmdList.isEmpty()) {
    // Make sure that their is at least one entry, so that new entries can
    // be added.
    cmdList.append(MiscConfig::MenuCommand());
  }
Urs Fleisch's avatar
Urs Fleisch committed
290
  return cmdList;
291
}