| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #include <QtWidgets> |
| 24 | #include "undoprompt.h" |
| 25 | |
| 26 | UndoPrompt::UndoPrompt(const QString * _name, bool multiple, int _moves) : name(_name), moves(_moves) |
| 27 | { |
| 28 | movesSpin = 0; |
| 29 | |
| 30 | if(name) |
| 31 | { |
| 32 | if(!multiple) |
| 33 | mainlabel = new QLabel(tr("%1 wants to undo the last move").arg(*name)); |
| 34 | else |
| 35 | mainlabel = new QLabel(tr("%1 wants to undo to move %2").arg(*name).arg(moves)); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | if(!multiple) |
| 40 | mainlabel = new QLabel(tr("Undo the last move?").arg(*name)); |
| 41 | else |
| 42 | { |
| 43 | mainlabel = new QLabel(tr("Undo to what move?")); |
| 44 | movesSpin = new QSpinBox(); |
| 45 | movesSpin->setValue(moves); // really shouldn't this be moves -1? |
| 46 | movesSpin->setRange(0, moves); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if(!movesSpin) |
| 51 | { |
| 52 | acceptButton = new QPushButton(tr("&Accept")); |
| 53 | acceptButton->setDefault(true); |
| 54 | |
| 55 | declineButton = new QPushButton(tr("&Decline")); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | acceptButton = new QPushButton(tr("&Request")); |
| 60 | acceptButton->setDefault(true); |
| 61 | |
| 62 | declineButton = new QPushButton(tr("&Cancel")); |
| 63 | } |
| 64 | buttonBox = new QDialogButtonBox(Qt::Horizontal); |
| 65 | buttonBox->addButton(acceptButton, QDialogButtonBox::ActionRole); |
| 66 | buttonBox->addButton(declineButton, QDialogButtonBox::ActionRole); |
| 67 | |
| 68 | connect(acceptButton, &QPushButton::clicked, this, &UndoPrompt::slot_accept); |
| 69 | connect(declineButton, &QPushButton::clicked, this, &UndoPrompt::slot_decline); |
| 70 | |
| 71 | QGridLayout * mainLayout = new QGridLayout; |
| 72 | mainLayout->setSizeConstraint(QLayout::SetFixedSize); |
| 73 | mainLayout->addWidget(mainlabel, 0, 0); |
| 74 | if(movesSpin) |
| 75 | mainLayout->addWidget(movesSpin, 1, 0); |
| 76 | mainLayout->addWidget(buttonBox, (movesSpin ? 2 : 1), 0); |
| 77 | setLayout(mainLayout); |
| 78 | |
| 79 | if(name) |
| 80 | setWindowTitle(tr("Undo requested")); |
| 81 | else |
| 82 | setWindowTitle(tr("Request undo?")); |
| 83 | |
| 84 | |
| 85 | // we might want to set modality here |
| 86 | //startTimer(1000); |
| 87 | } |
| 88 | |
| 89 | void UndoPrompt::timerEvent(QTimerEvent*) |
| 90 | { |
| 91 | // in case we need this? |
| 92 | } |
| 93 | |
| 94 | UndoPrompt::~UndoPrompt() |
| 95 | { |
| 96 | delete acceptButton; |
| 97 | delete declineButton; |
| 98 | delete mainlabel; |
| 99 | delete movesSpin; |
| 100 | delete buttonBox; |
| 101 | } |
| 102 | |
| 103 | /* I'm assuming here that a close by time returns 0 */ |
| 104 | void UndoPrompt::slot_accept(void) |
| 105 | { |
| 106 | if(!movesSpin) |
| 107 | done(moves); |
| 108 | else |
| 109 | done(movesSpin->value()); |
| 110 | } |
| 111 | |
| 112 | void UndoPrompt::slot_decline(void) |
| 113 | { |
| 114 | done(-1); |
| 115 | } |