Permit use of up/down keys to increment/decrement counter value; Fix #3618 (#3646)

* Fix #3618

* clanfigy me softly

* fix unused var and params

* Frce the dialog being modal; ensure self deletion

* More qt-like behavior

* Restore dialogSemaphore logic
This commit is contained in:
ctrlaltca 2019-03-10 22:22:19 +01:00 committed by GitHub
parent 389f7fdc25
commit 0326f0d4c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 7 deletions

View file

@ -8,8 +8,10 @@
#include <QApplication> #include <QApplication>
#include <QGraphicsSceneHoverEvent> #include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMenu> #include <QMenu>
#include <QPainter> #include <QPainter>
#include <QString>
AbstractCounter::AbstractCounter(Player *_player, AbstractCounter::AbstractCounter(Player *_player,
int _id, int _id,
@ -160,24 +162,59 @@ void AbstractCounter::incrementCounter()
void AbstractCounter::setCounter() void AbstractCounter::setCounter()
{ {
bool ok;
dialogSemaphore = true; dialogSemaphore = true;
QString expression = QInputDialog::getText(nullptr, tr("Set counter"), tr("New value for counter '%1':").arg(name), AbstractCounterDialog dialog(name, QString::number(value));
QLineEdit::Normal, QString::number(value), &ok); const int ok = dialog.exec();
Expression exp(value);
int newValue = static_cast<int>(exp.parse(expression));
if (deleteAfterDialog) { if (deleteAfterDialog) {
deleteLater(); deleteLater();
return; return;
} }
dialogSemaphore = false; dialogSemaphore = false;
if (!ok) if (!ok)
return; return;
Expression exp(value);
int newValue = static_cast<int>(exp.parse(dialog.textValue()));
Command_SetCounter cmd; Command_SetCounter cmd;
cmd.set_counter_id(id); cmd.set_counter_id(id);
cmd.set_value(newValue); cmd.set_value(newValue);
player->sendGameCommand(cmd); player->sendGameCommand(cmd);
} }
AbstractCounterDialog::AbstractCounterDialog(const QString &name, const QString &value) : QInputDialog(nullptr)
{
setWindowTitle(tr("Set counter"));
setLabelText(tr("New value for counter '%1':").arg(name));
setTextValue(value);
qApp->installEventFilter(this);
}
bool AbstractCounterDialog::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj);
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) {
case Qt::Key_Up:
changeValue(+1);
return true;
case Qt::Key_Down:
changeValue(-1);
return true;
}
}
return false;
}
void AbstractCounterDialog::changeValue(int diff)
{
bool ok;
int curValue = textValue().toInt(&ok);
if (!ok)
return;
curValue += diff;
setTextValue(QString::number(curValue));
}

View file

@ -2,10 +2,13 @@
#define COUNTER_H #define COUNTER_H
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QInputDialog>
class Player; class Player;
class QMenu;
class QAction; class QAction;
class QKeyEvent;
class QMenu;
class QString;
class AbstractCounter : public QObject, public QGraphicsItem class AbstractCounter : public QObject, public QGraphicsItem
{ {
@ -74,4 +77,15 @@ public:
} }
}; };
class AbstractCounterDialog : public QInputDialog
{
Q_OBJECT
public:
AbstractCounterDialog(const QString &name, const QString &value);
protected:
bool eventFilter(QObject *obj, QEvent *event);
void changeValue(int diff);
};
#endif #endif