3
Copyright 2013 Adam Reichold
5
This file is part of qpdfview.
7
qpdfview is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 2 of the License, or
10
(at your option) any later version.
12
qpdfview is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with qpdfview. If not, see <http://www.gnu.org/licenses/>.
22
#include "shortcutstablemodel.h"
27
ShortcutsTableModel::ShortcutsTableModel(const QList< QAction* >& actions, const QMap< QAction*, QKeySequence >& defaultShortcuts, QObject* parent) : QAbstractTableModel(parent),
29
m_defaultShortcuts(defaultShortcuts),
32
foreach(QAction* action, m_actions)
34
m_shortcuts.insert(action, action->shortcut());
38
int ShortcutsTableModel::columnCount(const QModelIndex& parent) const
45
int ShortcutsTableModel::rowCount(const QModelIndex& parent) const
49
return m_actions.count();
52
Qt::ItemFlags ShortcutsTableModel::flags(const QModelIndex& index) const
54
switch(index.column())
57
return Qt::ItemIsEnabled;
60
return Qt::ItemIsEnabled | Qt::ItemIsEditable;
64
return Qt::NoItemFlags;
67
QVariant ShortcutsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
69
Q_UNUSED(orientation);
71
if(role == Qt::DisplayRole)
79
return tr("Key sequence");
87
QVariant ShortcutsTableModel::data(const QModelIndex& index, int role) const
89
if((role == Qt::DisplayRole || role == Qt::EditRole) && index.row() >= 0 && index.row() < m_actions.count())
91
switch(index.column())
94
return m_actions.at(index.row())->text().remove(QLatin1Char('&'));
97
return m_shortcuts.value(m_actions.at(index.row()));
101
return QString::number(index.row()) + ":" + QString::number(index.column());
107
bool ShortcutsTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
109
if(role == Qt::EditRole && index.column() == 1 && index.row() >= 0 && index.row() < m_actions.count())
111
QKeySequence shortcut(value.toString());
113
if(!shortcut.isEmpty() || value.toString().isEmpty())
115
m_shortcuts.insert(m_actions.at(index.row()), shortcut);
117
emit dataChanged(index, index);
126
void ShortcutsTableModel::accept()
128
for(QMap< QAction*, QKeySequence >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
130
iterator.key()->setShortcut(iterator.value());
134
void ShortcutsTableModel::reset()
136
m_shortcuts = m_defaultShortcuts;
138
emit dataChanged(createIndex(0, 1), createIndex(m_actions.count(), 1));