22
22
#include "shortcuthandler.h"
24
#include <QApplication>
25
25
#include <QSettings>
27
27
#include "documentview.h"
29
ShortcutHandler::ShortcutHandler(QObject* parent) : QAbstractTableModel(parent),
35
m_settings = new QSettings("qpdfview", "shortcuts", this);
37
// skip backward shortcut
39
m_skipBackwardAction = new QAction(tr("Skip backward"), this);
40
m_skipBackwardAction->setObjectName(QLatin1String("skipBackward"));
42
m_skipBackwardAction->setShortcut(::DocumentView::skipBackwardShortcut());
43
registerAction(m_skipBackwardAction);
45
// skip forward shortcut
47
m_skipForwardAction = new QAction(tr("Skip forward"), this);
48
m_skipForwardAction->setObjectName(QLatin1String("skipForward"));
50
m_skipForwardAction->setShortcut(::DocumentView::skipForwardShortcut());
51
registerAction(m_skipForwardAction);
55
m_moveUpAction = new QAction(tr("Move up"), this);
56
m_moveUpAction->setObjectName(QLatin1String("moveUp"));
58
m_moveUpAction->setShortcut(::DocumentView::moveUpShortcut());
59
registerAction(m_moveUpAction);
63
m_moveDownAction = new QAction(tr("Move down"), this);
64
m_moveDownAction->setObjectName(QLatin1String("moveDown"));
66
m_moveDownAction->setShortcut(::DocumentView::moveDownShortcut());
67
registerAction(m_moveDownAction);
71
m_moveLeftAction = new QAction(tr("Move left"), this);
72
m_moveLeftAction->setObjectName(QLatin1String("moveLeft"));
74
m_moveLeftAction->setShortcut(::DocumentView::moveLeftShortcut());
75
registerAction(m_moveLeftAction);
77
// move right shortcut
79
m_moveRightAction = new QAction(tr("Move right"), this);
80
m_moveRightAction->setObjectName(QLatin1String("moveRight"));
82
m_moveRightAction->setShortcut(::DocumentView::moveRightShortcut());
83
registerAction(m_moveRightAction);
29
static QList< QKeySequence > toShortcuts(const QStringList& stringList)
31
QList< QKeySequence > shortcuts;
33
foreach(const QString& string, stringList)
35
QKeySequence shortcut(string.trimmed());
37
if(!shortcut.isEmpty())
39
shortcuts.append(shortcut);
46
static QStringList toStringList(const QList< QKeySequence >& shortcuts, QKeySequence::SequenceFormat format = QKeySequence::PortableText)
48
QStringList stringList;
50
foreach(const QKeySequence& shortcut, shortcuts)
52
stringList.append(shortcut.toString(format));
58
static bool matches(const QKeySequence& keySequence, const QList< QKeySequence >& shortcuts)
60
foreach(const QKeySequence& shortcut, shortcuts)
62
if(keySequence.matches(shortcut) == QKeySequence::ExactMatch)
71
ShortcutHandler* ShortcutHandler::s_instance = 0;
73
ShortcutHandler* ShortcutHandler::instance()
77
s_instance = new ShortcutHandler(qApp);
83
ShortcutHandler::~ShortcutHandler()
86
88
void ShortcutHandler::registerAction(QAction* action)
88
90
Q_ASSERT(!action->objectName().isEmpty());
90
QKeySequence defaultShortcut = action->shortcut();
91
QKeySequence shortcut = m_settings->value(action->objectName(), action->shortcut()).value< QKeySequence >();
92
const QList< QKeySequence > defaultShortcuts = action->shortcuts();
93
const QList< QKeySequence > shortcuts = toShortcuts(m_settings->value(action->objectName(), toStringList(defaultShortcuts)).toStringList());
93
action->setShortcut(shortcut);
95
action->setShortcuts(shortcuts);
95
97
m_actions.append(action);
96
m_shortcuts.insert(action, shortcut);
97
m_defaultShortcuts.insert(action, defaultShortcut);
98
m_shortcuts.insert(action, shortcuts);
99
m_defaultShortcuts.insert(action, defaultShortcuts);
100
102
int ShortcutHandler::columnCount(const QModelIndex& parent) const
190
bool ShortcutHandler::matchesSkipBackward(const QKeySequence& keySequence) const
192
return matches(keySequence, m_skipBackwardAction->shortcuts());
195
bool ShortcutHandler::matchesSkipForward(const QKeySequence& keySequence) const
197
return matches(keySequence, m_skipForwardAction->shortcuts());
200
bool ShortcutHandler::matchesMoveUp(const QKeySequence& keySequence) const
202
return matches(keySequence, m_moveUpAction->shortcuts());
205
bool ShortcutHandler::matchesMoveDown(const QKeySequence& keySequence) const
207
return matches(keySequence, m_moveDownAction->shortcuts());
210
bool ShortcutHandler::matchesMoveLeft(const QKeySequence& keySequence) const
212
return matches(keySequence, m_moveLeftAction->shortcuts());
215
bool ShortcutHandler::matchesMoveRight(const QKeySequence& keySequence) const
217
return matches(keySequence, m_moveRightAction->shortcuts());
188
220
bool ShortcutHandler::submit()
190
for(QMap< QAction*, QKeySequence >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
192
iterator.key()->setShortcut(iterator.value());
195
foreach(QAction* action, m_actions)
197
m_settings->setValue(action->objectName(), action->shortcut().toString(QKeySequence::PortableText));
200
DocumentView::setSkipBackwardShortcut(m_skipBackwardAction->shortcut());
201
DocumentView::setSkipForwardShortcut(m_skipForwardAction->shortcut());
203
DocumentView::setMoveUpShortcut(m_moveUpAction->shortcut());
204
DocumentView::setMoveDownShortcut(m_moveDownAction->shortcut());
205
DocumentView::setMoveLeftShortcut(m_moveLeftAction->shortcut());
206
DocumentView::setMoveRightShortcut(m_moveRightAction->shortcut());
222
for(QMap< QAction*, QList< QKeySequence > >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
224
iterator.key()->setShortcuts(iterator.value());
227
foreach(const QAction* action, m_actions)
229
m_settings->setValue(action->objectName(), toStringList(action->shortcuts()));
211
235
void ShortcutHandler::revert()
213
for(QMap< QAction*, QKeySequence >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
237
for(QMap< QAction*, QList< QKeySequence > >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator)
215
iterator.value() = iterator.key()->shortcut();
239
iterator.value() = iterator.key()->shortcuts();
219
243
void ShortcutHandler::reset()
221
m_shortcuts = m_defaultShortcuts;
245
for(QMap< QAction*, QList< QKeySequence > >::iterator iterator = m_defaultShortcuts.begin(); iterator != m_defaultShortcuts.end(); ++iterator)
247
m_shortcuts.insert(iterator.key(), iterator.value());
223
250
emit dataChanged(createIndex(0, 1), createIndex(m_actions.count(), 1));
253
ShortcutHandler::ShortcutHandler(QObject* parent) : QAbstractTableModel(parent),
254
m_settings(new QSettings("qpdfview", "shortcuts", this)),
259
// skip backward shortcut
261
m_skipBackwardAction = new QAction(tr("Skip backward"), this);
262
m_skipBackwardAction->setObjectName(QLatin1String("skipBackward"));
263
m_skipBackwardAction->setShortcut(QKeySequence(Qt::Key_PageUp));
264
registerAction(m_skipBackwardAction);
266
// skip forward shortcut
268
m_skipForwardAction = new QAction(tr("Skip forward"), this);
269
m_skipForwardAction->setObjectName(QLatin1String("skipForward"));
270
m_skipForwardAction->setShortcut(QKeySequence(Qt::Key_PageDown));
271
registerAction(m_skipForwardAction);
275
m_moveUpAction = new QAction(tr("Move up"), this);
276
m_moveUpAction->setObjectName(QLatin1String("moveUp"));
277
m_moveUpAction->setShortcut(QKeySequence(Qt::Key_Up));
278
registerAction(m_moveUpAction);
280
// move down shortcut
282
m_moveDownAction = new QAction(tr("Move down"), this);
283
m_moveDownAction->setObjectName(QLatin1String("moveDown"));
284
m_moveDownAction->setShortcut(QKeySequence(Qt::Key_Down));
285
registerAction(m_moveDownAction);
287
// move left shortcut
289
m_moveLeftAction = new QAction(tr("Move left"), this);
290
m_moveLeftAction->setObjectName(QLatin1String("moveLeft"));
291
m_moveLeftAction->setShortcut(QKeySequence(Qt::Key_Left));
292
registerAction(m_moveLeftAction);
294
// move right shortcut
296
m_moveRightAction = new QAction(tr("Move right"), this);
297
m_moveRightAction->setObjectName(QLatin1String("moveRight"));
298
m_moveRightAction->setShortcut(QKeySequence(Qt::Key_Right));
299
registerAction(m_moveRightAction);