[e0e368b] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2013 Adam Reichold |
---|
| 4 | |
---|
| 5 | This file is part of qpdfview. |
---|
| 6 | |
---|
| 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. |
---|
| 11 | |
---|
| 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. |
---|
| 16 | |
---|
| 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/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "shortcuthandler.h" |
---|
| 23 | |
---|
| 24 | #include <QApplication> |
---|
| 25 | #include <QSettings> |
---|
| 26 | |
---|
| 27 | #include "documentview.h" |
---|
| 28 | |
---|
| 29 | namespace |
---|
| 30 | { |
---|
| 31 | |
---|
| 32 | QList< QKeySequence > toShortcuts(const QStringList& stringList) |
---|
| 33 | { |
---|
| 34 | QList< QKeySequence > shortcuts; |
---|
| 35 | |
---|
| 36 | foreach(const QString& string, stringList) |
---|
| 37 | { |
---|
| 38 | QKeySequence shortcut(string.trimmed()); |
---|
| 39 | |
---|
| 40 | if(!shortcut.isEmpty()) |
---|
| 41 | { |
---|
| 42 | shortcuts.append(shortcut); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | return shortcuts; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | QStringList toStringList(const QList< QKeySequence >& shortcuts, QKeySequence::SequenceFormat format = QKeySequence::PortableText) |
---|
| 50 | { |
---|
| 51 | QStringList stringList; |
---|
| 52 | |
---|
| 53 | foreach(const QKeySequence& shortcut, shortcuts) |
---|
| 54 | { |
---|
| 55 | stringList.append(shortcut.toString(format)); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | return stringList; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | inline bool matches(const QKeySequence& keySequence, const QList< QKeySequence >& shortcuts) |
---|
| 62 | { |
---|
| 63 | foreach(const QKeySequence& shortcut, shortcuts) |
---|
| 64 | { |
---|
| 65 | if(keySequence.matches(shortcut) == QKeySequence::ExactMatch) |
---|
| 66 | { |
---|
| 67 | return true; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | return false; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | } // anonymous |
---|
| 75 | |
---|
| 76 | namespace qpdfview |
---|
| 77 | { |
---|
| 78 | |
---|
| 79 | ShortcutHandler* ShortcutHandler::s_instance = 0; |
---|
| 80 | |
---|
| 81 | ShortcutHandler* ShortcutHandler::instance() |
---|
| 82 | { |
---|
| 83 | if(s_instance == 0) |
---|
| 84 | { |
---|
| 85 | s_instance = new ShortcutHandler(qApp); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return s_instance; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | ShortcutHandler::~ShortcutHandler() |
---|
| 92 | { |
---|
| 93 | s_instance = 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void ShortcutHandler::registerAction(QAction* action) |
---|
| 97 | { |
---|
| 98 | Q_ASSERT(!action->objectName().isEmpty()); |
---|
| 99 | |
---|
| 100 | const QList< QKeySequence > defaultShortcuts = action->shortcuts(); |
---|
| 101 | const QList< QKeySequence > shortcuts = toShortcuts(m_settings->value(action->objectName(), toStringList(defaultShortcuts)).toStringList()); |
---|
| 102 | |
---|
| 103 | action->setShortcuts(shortcuts); |
---|
| 104 | |
---|
| 105 | m_actions.append(action); |
---|
| 106 | m_shortcuts.insert(action, shortcuts); |
---|
| 107 | m_defaultShortcuts.insert(action, defaultShortcuts); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | int ShortcutHandler::columnCount(const QModelIndex& parent) const |
---|
| 111 | { |
---|
| 112 | Q_UNUSED(parent); |
---|
| 113 | |
---|
| 114 | return 2; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int ShortcutHandler::rowCount(const QModelIndex& parent) const |
---|
| 118 | { |
---|
| 119 | Q_UNUSED(parent); |
---|
| 120 | |
---|
| 121 | return m_actions.count(); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | Qt::ItemFlags ShortcutHandler::flags(const QModelIndex& index) const |
---|
| 125 | { |
---|
| 126 | switch(index.column()) |
---|
| 127 | { |
---|
| 128 | case 0: |
---|
| 129 | return Qt::ItemIsEnabled; |
---|
| 130 | break; |
---|
| 131 | case 1: |
---|
| 132 | return Qt::ItemIsEnabled | Qt::ItemIsEditable; |
---|
| 133 | break; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | return Qt::NoItemFlags; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | QVariant ShortcutHandler::headerData(int section, Qt::Orientation orientation, int role) const |
---|
| 140 | { |
---|
| 141 | Q_UNUSED(orientation); |
---|
| 142 | |
---|
| 143 | if(role == Qt::DisplayRole) |
---|
| 144 | { |
---|
| 145 | switch(section) |
---|
| 146 | { |
---|
| 147 | case 0: |
---|
| 148 | return tr("Action"); |
---|
| 149 | break; |
---|
| 150 | case 1: |
---|
| 151 | return tr("Key sequence"); |
---|
| 152 | break; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | return QVariant(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | QVariant ShortcutHandler::data(const QModelIndex& index, int role) const |
---|
| 160 | { |
---|
| 161 | if((role == Qt::DisplayRole || role == Qt::EditRole) && index.row() >= 0 && index.row() < m_actions.count()) |
---|
| 162 | { |
---|
| 163 | QAction* action = m_actions.at(index.row()); |
---|
| 164 | |
---|
| 165 | switch(index.column()) |
---|
| 166 | { |
---|
| 167 | case 0: |
---|
| 168 | return action->text().remove(QLatin1Char('&')); |
---|
| 169 | break; |
---|
| 170 | case 1: |
---|
| 171 | return toStringList(m_shortcuts.value(action), QKeySequence::NativeText).join(";"); |
---|
| 172 | break; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return QVariant(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | bool ShortcutHandler::setData(const QModelIndex& index, const QVariant& value, int role) |
---|
| 180 | { |
---|
| 181 | if(role == Qt::EditRole && index.column() == 1 && index.row() >= 0 && index.row() < m_actions.count()) |
---|
| 182 | { |
---|
| 183 | QList< QKeySequence > shortcuts = toShortcuts(value.toString().split(";", QString::SkipEmptyParts)); |
---|
| 184 | |
---|
| 185 | if(!shortcuts.isEmpty() || value.toString().isEmpty()) |
---|
| 186 | { |
---|
| 187 | m_shortcuts.insert(m_actions.at(index.row()), shortcuts); |
---|
| 188 | |
---|
| 189 | emit dataChanged(index, index); |
---|
| 190 | |
---|
| 191 | return true; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | return false; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | bool ShortcutHandler::matchesSkipBackward(const QKeySequence& keySequence) const |
---|
| 199 | { |
---|
| 200 | return matches(keySequence, m_skipBackwardAction->shortcuts()); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | bool ShortcutHandler::matchesSkipForward(const QKeySequence& keySequence) const |
---|
| 204 | { |
---|
| 205 | return matches(keySequence, m_skipForwardAction->shortcuts()); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | bool ShortcutHandler::matchesMoveUp(const QKeySequence& keySequence) const |
---|
| 209 | { |
---|
| 210 | return matches(keySequence, m_moveUpAction->shortcuts()); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | bool ShortcutHandler::matchesMoveDown(const QKeySequence& keySequence) const |
---|
| 214 | { |
---|
| 215 | return matches(keySequence, m_moveDownAction->shortcuts()); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | bool ShortcutHandler::matchesMoveLeft(const QKeySequence& keySequence) const |
---|
| 219 | { |
---|
| 220 | return matches(keySequence, m_moveLeftAction->shortcuts()); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | bool ShortcutHandler::matchesMoveRight(const QKeySequence& keySequence) const |
---|
| 224 | { |
---|
| 225 | return matches(keySequence, m_moveRightAction->shortcuts()); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | bool ShortcutHandler::submit() |
---|
| 229 | { |
---|
| 230 | for(QHash< QAction*, QList< QKeySequence > >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator) |
---|
| 231 | { |
---|
| 232 | iterator.key()->setShortcuts(iterator.value()); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | foreach(const QAction* action, m_actions) |
---|
| 236 | { |
---|
| 237 | m_settings->setValue(action->objectName(), toStringList(action->shortcuts())); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | return true; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | void ShortcutHandler::revert() |
---|
| 244 | { |
---|
| 245 | for(QHash< QAction*, QList< QKeySequence > >::iterator iterator = m_shortcuts.begin(); iterator != m_shortcuts.end(); ++iterator) |
---|
| 246 | { |
---|
| 247 | iterator.value() = iterator.key()->shortcuts(); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void ShortcutHandler::reset() |
---|
| 252 | { |
---|
| 253 | for(QHash< QAction*, QList< QKeySequence > >::iterator iterator = m_defaultShortcuts.begin(); iterator != m_defaultShortcuts.end(); ++iterator) |
---|
| 254 | { |
---|
| 255 | m_shortcuts.insert(iterator.key(), iterator.value()); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | emit dataChanged(createIndex(0, 1), createIndex(m_actions.count(), 1)); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | ShortcutHandler::ShortcutHandler(QObject* parent) : QAbstractTableModel(parent), |
---|
[8964fdc] | 262 | // m_settings(new QSettings("qpdfview", "shortcuts", this)), |
---|
| 263 | m_settings(new QSettings("murrupuy", "shortcuts", this)), |
---|
[e0e368b] | 264 | m_actions(), |
---|
| 265 | m_shortcuts(), |
---|
| 266 | m_defaultShortcuts() |
---|
| 267 | { |
---|
| 268 | // skip backward shortcut |
---|
| 269 | |
---|
| 270 | m_skipBackwardAction = new QAction(tr("Skip backward"), this); |
---|
| 271 | m_skipBackwardAction->setObjectName(QLatin1String("skipBackward")); |
---|
| 272 | m_skipBackwardAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_PageUp) << QKeySequence(Qt::KeypadModifier + Qt::Key_PageUp)); |
---|
| 273 | registerAction(m_skipBackwardAction); |
---|
| 274 | |
---|
| 275 | // skip forward shortcut |
---|
| 276 | |
---|
| 277 | m_skipForwardAction = new QAction(tr("Skip forward"), this); |
---|
| 278 | m_skipForwardAction->setObjectName(QLatin1String("skipForward")); |
---|
| 279 | m_skipForwardAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_PageDown) << QKeySequence(Qt::KeypadModifier + Qt::Key_PageDown)); |
---|
| 280 | registerAction(m_skipForwardAction); |
---|
| 281 | |
---|
| 282 | // move up shortcut |
---|
| 283 | |
---|
| 284 | m_moveUpAction = new QAction(tr("Move up"), this); |
---|
| 285 | m_moveUpAction->setObjectName(QLatin1String("moveUp")); |
---|
| 286 | m_moveUpAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_Up) << QKeySequence(Qt::KeypadModifier + Qt::Key_Up)); |
---|
| 287 | registerAction(m_moveUpAction); |
---|
| 288 | |
---|
| 289 | // move down shortcut |
---|
| 290 | |
---|
| 291 | m_moveDownAction = new QAction(tr("Move down"), this); |
---|
| 292 | m_moveDownAction->setObjectName(QLatin1String("moveDown")); |
---|
| 293 | m_moveDownAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_Down) << QKeySequence(Qt::KeypadModifier + Qt::Key_Down)); |
---|
| 294 | registerAction(m_moveDownAction); |
---|
| 295 | |
---|
| 296 | // move left shortcut |
---|
| 297 | |
---|
| 298 | m_moveLeftAction = new QAction(tr("Move left"), this); |
---|
| 299 | m_moveLeftAction->setObjectName(QLatin1String("moveLeft")); |
---|
| 300 | m_moveLeftAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_Left) << QKeySequence(Qt::KeypadModifier + Qt::Key_Left)); |
---|
| 301 | registerAction(m_moveLeftAction); |
---|
| 302 | |
---|
| 303 | // move right shortcut |
---|
| 304 | |
---|
| 305 | m_moveRightAction = new QAction(tr("Move right"), this); |
---|
| 306 | m_moveRightAction->setObjectName(QLatin1String("moveRight")); |
---|
| 307 | m_moveRightAction->setShortcuts(QList< QKeySequence >() << QKeySequence(Qt::Key_Right) << QKeySequence(Qt::KeypadModifier + Qt::Key_Right)); |
---|
| 308 | registerAction(m_moveRightAction); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | } // qpdfview |
---|