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 <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMenu>
#include <QPainter>
#include <QString>
AbstractCounter::AbstractCounter(Player *_player,
int _id,
@ -160,24 +162,59 @@ void AbstractCounter::incrementCounter()
void AbstractCounter::setCounter()
{
bool ok;
dialogSemaphore = true;
QString expression = QInputDialog::getText(nullptr, tr("Set counter"), tr("New value for counter '%1':").arg(name),
QLineEdit::Normal, QString::number(value), &ok);
Expression exp(value);
int newValue = static_cast<int>(exp.parse(expression));
AbstractCounterDialog dialog(name, QString::number(value));
const int ok = dialog.exec();
if (deleteAfterDialog) {
deleteLater();
return;
}
dialogSemaphore = false;
if (!ok)
return;
Expression exp(value);
int newValue = static_cast<int>(exp.parse(dialog.textValue()));
Command_SetCounter cmd;
cmd.set_counter_id(id);
cmd.set_value(newValue);
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
#include <QGraphicsItem>
#include <QInputDialog>
class Player;
class QMenu;
class QAction;
class QKeyEvent;
class QMenu;
class QString;
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