File indexing completed on 2024-05-12 04:05:53
0001 /*************************************************************************** 0002 * Copyright 2008 Johannes Bergmeier <johannes.bergmeier@gmx.net> * 0003 * Copyright 2015 Ian Wadham <iandw.au@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 0019 ***************************************************************************/ 0020 0021 #include "gameactions.h" 0022 0023 #include <QAction> 0024 #include <QKeySequence> 0025 0026 #include <KActionCollection> 0027 #include <KLocalizedString> 0028 0029 namespace ksudoku { 0030 0031 GameActions::GameActions(KActionCollection* collection, QObject *parent) 0032 : QObject(parent) 0033 { 0034 m_collection = collection; 0035 0036 } 0037 0038 void GameActions::init() { 0039 // NOTE: Qt::Key_Asterisk cannot be used for Multiply, because it 0040 // clashes with Shift+8, used for cancelling '8' or 'h' marker. 0041 // Instead, we use 'x' for Multiply, which is OK because Mathdoku 0042 // and Killer Sudoku puzzles require only digits 1 to 9. 0043 0044 // Extras: zero, divide, subtract, add, add cell, end cage, remove cell. 0045 const Qt::Key extras[] = {Qt::Key_0, Qt::Key_Slash, Qt::Key_Minus, 0046 /* Qt::Key_Asterisk, */ Qt::Key_Plus, Qt::Key_Space, 0047 Qt::Key_Return}; 0048 QAction * a; 0049 QList<QKeySequence> shortcuts; 0050 for(int i = 0; i < 31; ++i) { 0051 shortcuts.clear(); 0052 a = new QAction(this); 0053 m_collection->addAction(QStringLiteral("val-select%1").arg(i+1,2,10,QLatin1Char('0')), a); 0054 a->setText(i18n("Select %1 (%2)", QChar('a'+i), i+1)); 0055 connect(a, &QAction::triggered, this, [this, i] { Q_EMIT selectValue(i + 1); }); 0056 m_actions << a; 0057 0058 a = new QAction(this); 0059 m_collection->addAction(QStringLiteral("val-enter%1").arg(i+1,2,10,QLatin1Char('0')), a); 0060 a->setText(i18n("Enter %1 (%2)", QChar('a'+i), i+1)); 0061 if (i < 25) { 0062 // Keys A to Y, for Sudoku puzzles. 0063 shortcuts << QKeySequence(Qt::Key_A + i); 0064 } 0065 else { 0066 // Extras for keying in Mathdoku and Killer Sudoku puzzles. 0067 shortcuts << QKeySequence(extras[i - 25]); 0068 } 0069 if (i < 9) { 0070 // Keys 1 to 9, for puzzles of order 9 or less. 0071 shortcuts << QKeySequence(Qt::Key_1 + i); 0072 } 0073 KActionCollection::setDefaultShortcuts(a, shortcuts); 0074 connect(a, &QAction::triggered, this, [this, i] { Q_EMIT enterValue(i + 1); }); 0075 m_actions << a; 0076 if (i >= 25) { 0077 continue; 0078 } 0079 0080 shortcuts.clear(); 0081 a = new QAction(this); 0082 m_collection->addAction(QStringLiteral("val-mark%1").arg(i+1,2,10,QLatin1Char('0')), a); 0083 a->setText(i18n("Mark %1 (%2)", QChar('a'+i), i+1)); 0084 shortcuts << QKeySequence(Qt::ShiftModifier | (Qt::Key_A + i)); 0085 if(i < 9) { 0086 shortcuts << QKeySequence(Qt::ShiftModifier | (Qt::Key_1 + i)); 0087 } 0088 KActionCollection::setDefaultShortcuts(a, shortcuts); 0089 connect(a, &QAction::triggered, this, [this, i] { Q_EMIT markValue(i + 1); }); 0090 m_actions << a; 0091 } 0092 0093 a = new QAction(this); 0094 m_collection->addAction(QStringLiteral("move_up"), a); 0095 a->setText(i18n("Move Up")); 0096 KActionCollection::setDefaultShortcut(a, Qt::Key_Up); 0097 connect(a, &QAction::triggered, this, &GameActions::moveUp); 0098 m_actions << a; 0099 0100 a = new QAction(this); 0101 m_collection->addAction(QStringLiteral("move_down"), a); 0102 a->setText(i18n("Move Down")); 0103 KActionCollection::setDefaultShortcut(a, Qt::Key_Down); 0104 connect(a, &QAction::triggered, this, &GameActions::moveDown); 0105 m_actions << a; 0106 0107 a = new QAction(this); 0108 m_collection->addAction(QStringLiteral("move_left"), a); 0109 a->setText(i18n("Move Left")); 0110 KActionCollection::setDefaultShortcut(a, Qt::Key_Left); 0111 connect(a, &QAction::triggered, this, &GameActions::moveLeft); 0112 m_actions << a; 0113 0114 a = new QAction(this); 0115 m_collection->addAction(QStringLiteral("move_right"), a); 0116 a->setText(i18n("Move Right")); 0117 KActionCollection::setDefaultShortcut(a, Qt::Key_Right); 0118 connect(a, &QAction::triggered, this, &GameActions::moveRight); 0119 m_actions << a; 0120 0121 a = new QAction(this); 0122 m_collection->addAction(QStringLiteral("move_clear_cell"), a); 0123 a->setText(i18n("Clear Cell")); 0124 KActionCollection::setDefaultShortcuts(a, {QKeySequence(Qt::Key_Backspace), QKeySequence(Qt::Key_Delete)}); 0125 connect(a, &QAction::triggered, this, &GameActions::clearValue); 0126 m_actions << a; 0127 } 0128 0129 void GameActions::associateWidget(QWidget* widget) { 0130 QList<QAction *>::iterator it; 0131 for(it = m_actions.begin(); it != m_actions.end(); ++it) { 0132 widget->addAction(*it); 0133 } 0134 } 0135 0136 void GameActions::clearValue() { 0137 Q_EMIT enterValue(32); // Delete: not always the same as Qt::Key_0. 0138 } 0139 0140 void GameActions::moveUp() { 0141 Q_EMIT move(0, -1); 0142 } 0143 0144 void GameActions::moveDown() { 0145 Q_EMIT move(0, +1); 0146 } 0147 0148 void GameActions::moveLeft() { 0149 Q_EMIT move(-1, 0); 0150 } 0151 0152 void GameActions::moveRight() { 0153 Q_EMIT move(+1, 0); 0154 } 0155 0156 } 0157 0158 #include "moc_gameactions.cpp"