From: Arto Hyvättinen Date: Sun, 15 Aug 2010 23:09:52 +0000 (+0300) Subject: Time control base classes X-Git-Tag: v1.9.0~110 X-Git-Url: http://git.maemo.org/git/?p=chessclock;a=commitdiff_plain;h=ba3a771506c5a43c9f93d99930537215400f75e6 Time control base classes Added time control base class, basic dialog and no-time controll. Compiled but untested --- diff --git a/chessclock.pro b/chessclock.pro index 82b2d01..4d0f3c7 100644 --- a/chessclock.pro +++ b/chessclock.pro @@ -16,14 +16,19 @@ SOURCES += main.cpp\ classes/chessclock.cpp \ classes/chessclockwidget.cpp \ classes/clockswidget.cpp \ - classes/welcomescreenwidget.cpp + classes/welcomescreenwidget.cpp \ + classes/timecontrol/basicdialog.cpp \ + classes/timecontrol/notimecontrol.cpp HEADERS += chessclockwindow.h \ classes/turninformation.h \ classes/chessclock.h \ classes/chessclockwidget.h \ classes/clockswidget.h \ - classes/welcomescreenwidget.h + classes/welcomescreenwidget.h \ + classes/timecontrol.h \ + classes/timecontrol/basicdialog.h \ + classes/timecontrol/notimecontrol.h CONFIG += mobility MOBILITY = diff --git a/chessclock.qrc b/chessclock.qrc index 1b1983b..c0e9b9d 100644 --- a/chessclock.qrc +++ b/chessclock.qrc @@ -7,5 +7,7 @@ pic/white_gray.png pic/chessclock.png pic/logo.png + pic/white_small.png + pic/black_small.png diff --git a/classes/chessclockwidget.h b/classes/chessclockwidget.h index 356b8d6..d8f9e4c 100644 --- a/classes/chessclockwidget.h +++ b/classes/chessclockwidget.h @@ -65,7 +65,7 @@ protected: virtual void initBottom(); /*! Convert msecs to h:mm:ss QString */ - QString timeString(int msecs); + static QString timeString(int msecs); protected: diff --git a/classes/timecontrol.h b/classes/timecontrol.h new file mode 100644 index 0000000..7881e68 --- /dev/null +++ b/classes/timecontrol.h @@ -0,0 +1,53 @@ + /************************************************************************** + + Chess Clock + + Copyright (c) Arto Hyvättinen 2010 + + This file is part of Chess Clock software. + + Chess Clock is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Chess Clock is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + +**************************************************************************/ + +#ifndef TIMECONTROL_H +#define TIMECONTROL_H + +#include +#include +class ClocksWidget; + +/*! Base class of virtual controls + + @author Arto Hyvättinen + @date 2010-08-15 + + */ +class TimeControl +{ +public: + /*! Name of the time control */ + virtual QString getName() = 0; + /*! Description of the time control */ + virtual QString getDescription() = 0; + /*! Icon of the time control */ + virtual QIcon getIcon() { return QIcon(":/rc/pic/chessclock.png"); } + /*! Init new game + + Ask options, and init new game. + @param useLastSettings Use last settings, don't ask + @return Clocks widget for new game, or NULL if unsuccess. */ + virtual ClocksWidget* initGame(bool useLastSettings=false) = 0; +}; + + +#endif // TIMECONTROL_H diff --git a/classes/timecontrol/basicdialog.cpp b/classes/timecontrol/basicdialog.cpp new file mode 100644 index 0000000..ebfdf8a --- /dev/null +++ b/classes/timecontrol/basicdialog.cpp @@ -0,0 +1,211 @@ + /************************************************************************** + + Chess Clock + + Copyright (c) Arto Hyvättinen 2010 + + This file is part of Chess Clock software. + + Chess Clock is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Chess Clock is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + +**************************************************************************/ + +#include "basicdialog.h" + +#include +#include +#include +#include +#include +#include +#include +#include + + +BasicDialog::BasicDialog(QString timeControlName, QWidget *parent) : + QDialog(parent) +{ + timeControlName_ = timeControlName; + + // Initial times + initialLabel_ = new QLabel( tr("Initial time")); + whiteInitial_ = initTimeEdit(); + blackInitial_ = initTimeEdit(); + // Additional times + additionLabel_ = new QLabel( tr("Additional time")); + whiteAddition_ = initTimeEdit(); + blackAddition_ = initTimeEdit(); + // Turns per addition + perTurnLabel_ = new QLabel( tr("Addition per turn")); + whiteTurns_=new QSpinBox; + whiteTurns_->setRange(1,99); + + blackTurns_=new QSpinBox; + blackTurns_->setRange(1,99); + + // Equal times + equals_ = new QCheckBox( tr("Equal times")); + + connect( equals_, SIGNAL(stateChanged(bool)),blackInitial_,SLOT(setDisabled(bool))); + connect( equals_, SIGNAL(stateChanged(bool)),blackAddition_,SLOT(setDisabled(bool))); + connect( equals_, SIGNAL(stateChanged(bool)),blackTurns_,SLOT(setDisabled(bool))); + + equals_->setChecked(true); + + whiteLabel_ = new QLabel; + whiteLabel_->setPixmap(QPixmap(":/rc/pic/white_small.png")); + blackLabel_ = new QLabel; + blackLabel_->setPixmap(QPixmap(":/rc/pic/black_small.png")); + + // Lay out + QGridLayout* layout = new QGridLayout; + layout->addWidget(equals_,0,0); + layout->addWidget(whiteLabel_,0,1,1,1,Qt::AlignCenter); + layout->addWidget(blackLabel_,0,2,1,1,Qt::AlignCenter); + + layout->addWidget(initialLabel_,1,0); + layout->addWidget(whiteInitial_,1,1); + layout->addWidget(blackInitial_,1,2); + + layout->addWidget(additionLabel_,2,0); + layout->addWidget(whiteAddition_,2,1); + layout->addWidget(blackAddition_,2,2); + + layout->addWidget(perTurnLabel_,3,0); + layout->addWidget(whiteTurns_,3,1); + layout->addWidget(blackTurns_,3,2); + + QPushButton* button = new QPushButton( tr("Start game")); + connect( button, SIGNAL(clicked()), this, SLOT(accept())); + + setLayout( layout ); + setWindowTitle( timeControlName_); + +} + +void BasicDialog::disablePerTurns() +{ + perTurnLabel_->setVisible(false); + whiteTurns_->setVisible(false); + blackTurns_->setVisible(false); +} + +void BasicDialog::disableAddition() +{ + disablePerTurns(); + whiteAddition_->setVisible(false); + blackAddition_->setVisible(false); + additionLabel_->setVisible(false); +} + +void BasicDialog::disableUnEquals() +{ + equals_->setChecked(true); + equals_->setVisible(false); + whiteLabel_->setVisible(false); + blackLabel_->setVisible(false); + blackInitial_->setVisible(false); + blackAddition_->setVisible(false); + blackTurns_->setVisible(false); +} + +void BasicDialog::store() +{ + // Store into QSettings + QSettings s; + s.beginGroup(timeControlName_); + s.setValue("Equals",equals_->isChecked()); + s.setValue("WhiteInitial",whiteInitial_->time()); + s.setValue("WhiteAddition",whiteAddition_->time()); + s.setValue("WhitePerTurns",whiteTurns_->value()); + + s.setValue("BlackInitial",blackInitial_->time()); + s.setValue("BlackAddition",blackAddition_->time()); + s.setValue("BlackPerTurns",blackTurns_->value()); +} + +void BasicDialog::init(QTime whiteInitial, QTime blackInitial, QTime whiteAddition, QTime blackAddition, int whitePerTurns, int blackPerTurns) +{ + // Load from QSettings + // first time use defaults + QSettings s; + s.beginGroup(timeControlName_); + + equals_->setChecked(s.value("Equals",true).toBool() ); + whiteInitial_->setTime( s.value("WhiteInitial",whiteInitial).toTime()); + blackInitial_->setTime(s.value("BlackInitial",blackInitial).toTime()); + whiteAddition_->setTime(s.value("WhiteAddition",whiteAddition).toTime()); + blackAddition_->setTime(s.value("BlackAddition",blackAddition).toTime()); + whiteTurns_->setValue(s.value("WhitePerTurns",whitePerTurns).toInt()); + blackTurns_->setValue(s.value("BlackPerTurns",blackPerTurns).toInt()); + +} + +int BasicDialog::getWhiteInitial() +{ + return toMsecs( whiteInitial_); +} + +int BasicDialog::getWhiteAddition() +{ + return toMsecs( whiteAddition_); +} + +int BasicDialog::getWhitePerTurns() +{ + return whiteTurns_->value(); +} + +int BasicDialog::getBlackInitial() +{ + // if Equals is checked, black values are disabled + // and white values returned. + if( equals_->isChecked()) + return toMsecs( whiteInitial_); + else + return toMsecs( blackInitial_); +} + +int BasicDialog::getBlackAddition() +{ + if( equals_->isChecked()) + return toMsecs( whiteAddition_); + else + return toMsecs( blackAddition_ ); + +} + +int BasicDialog::getBlackPerTurns() +{ + if( equals_->isChecked()) + return whiteTurns_->value(); + else + return blackTurns_->value(); +} + + +int BasicDialog::toMsecs(QTimeEdit *timeEdit) +{ + QTime qtime=timeEdit->time(); + return 0-qtime.msecsTo(QTime(0,0,0)); +} + + +QTimeEdit* BasicDialog::initTimeEdit() +{ + // Make QTimeEdit and set up range and display format + QTimeEdit* timeEdit = new QTimeEdit; + timeEdit->setMinimumTime( QTime(0,0,0)); + timeEdit->setMaximumTime( QTime(12,0,0)); + timeEdit->setDisplayFormat( tr("hh:mm:ss","Time Edit display format")); + return timeEdit; +} diff --git a/classes/timecontrol/basicdialog.h b/classes/timecontrol/basicdialog.h new file mode 100644 index 0000000..54ab82b --- /dev/null +++ b/classes/timecontrol/basicdialog.h @@ -0,0 +1,98 @@ + /************************************************************************** + + Chess Clock + + Copyright (c) Arto Hyvättinen 2010 + + This file is part of Chess Clock software. + + Chess Clock is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Chess Clock is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + +**************************************************************************/ + +#ifndef BASICDIALOG_H +#define BASICDIALOG_H + +#include +#include +#include + +class QTimeEdit; +class QSpinBox; +class QCheckBox; +class QLabel; + +/*! Basic dialog for Time Control settings + + @author Arto Hyvättinen + @date 2010-08-16 + + Basic dialog setting up clock. + Time control can use this dialog. + Using disableAddition(), disablePerTurns() or disableUnEquals() + it is possible to customize dialog. + + All the widgets are public for customization. + */ + +class BasicDialog : public QDialog +{ + Q_OBJECT +public: + /*! Constructor + + @param timeControlName Name of time controller, will be window title */ + BasicDialog(QString timeContolName, QWidget *parent = 0); + + void disableAddition(); + void disablePerTurns(); + void disableUnEquals(); + + int getWhiteInitial(); + int getBlackInitial(); + int getWhiteAddition(); + int getBlackAddition(); + int getWhitePerTurns(); + int getBlackPerTurns(); + /*! Store values to settings */ + void store(); + /*! Load values from settings, or init to initial values */ + void init(QTime whiteInitial=QTime(1,30), + QTime blackInitial=QTime(1,30), + QTime whiteAddition=QTime(0,0,30), + QTime blackAddition=QTime(0,0,30), + int whitePerTurns = 1, + int blackPerTurns = 1); +signals: + +public slots: + +protected: + static QTimeEdit* initTimeEdit(); + static int toMsecs(QTimeEdit* timeEdit); + +public: + QTimeEdit *whiteInitial_, *blackInitial_; + QTimeEdit *whiteAddition_, *blackAddition_; + QSpinBox *whiteTurns_, *blackTurns_; + QCheckBox *equals_; + + QLabel *initialLabel_, *additionLabel_, *perTurnLabel_, *equalsLabel_; + QLabel *whiteLabel_, *blackLabel_; + +protected: + QString timeControlName_; + + +}; + +#endif // BASICDIALOG_H diff --git a/classes/timecontrol/notimecontrol.cpp b/classes/timecontrol/notimecontrol.cpp new file mode 100644 index 0000000..3163070 --- /dev/null +++ b/classes/timecontrol/notimecontrol.cpp @@ -0,0 +1,62 @@ + /************************************************************************** + + Chess Clock + + Copyright (c) Arto Hyvättinen 2010 + + This file is part of Chess Clock software. + + Chess Clock is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Chess Clock is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + +**************************************************************************/ + +#include "notimecontrol.h" + +#include "../chessclock.h" +#include "basicdialog.h" +#include "../chessclockwidget.h" +#include "../clockswidget.h" + +#include + +NoTimeControl::NoTimeControl() +{ +} + + +QString NoTimeControl::getDescription() +{ + return qApp->translate("NoneTimeControl","Never add time."); +} + +ClocksWidget* NoTimeControl::initGame(bool useLastSettings) +{ + BasicDialog dialog(getName()); + + dialog.disableAddition(); + dialog.init(); + + if( useLastSettings || dialog.exec() == QDialog::Accepted) + { + dialog.store(); + ChessClockWidget* white = new ChessClockWidget(true); + white->addTime(dialog.getWhiteInitial()); + + ChessClockWidget* black = new ChessClockWidget(false); + black->addTime( dialog.getBlackInitial()); + + return( new ClocksWidget(white,black)); + + } + else + return 0; +} diff --git a/classes/timecontrol/notimecontrol.h b/classes/timecontrol/notimecontrol.h new file mode 100644 index 0000000..1befb70 --- /dev/null +++ b/classes/timecontrol/notimecontrol.h @@ -0,0 +1,38 @@ + /************************************************************************** + + Chess Clock + + Copyright (c) Arto Hyvättinen 2010 + + This file is part of Chess Clock software. + + Chess Clock is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Chess Clock is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + +**************************************************************************/ + +#ifndef NOTIMECONTROL_H +#define NOTIMECONTROL_H + +#include "../timecontrol.h" +#include + +class NoTimeControl : public TimeControl +{ +public: + NoTimeControl(); + + QString getName() { return qApp->translate("NoTimeControl","Normal clock"); } + QString getDescription(); + ClocksWidget* initGame(bool useLastSettings); +}; + +#endif // NOTIMECONTROL_H diff --git a/main.cpp b/main.cpp index dab223c..b54e0f9 100644 --- a/main.cpp +++ b/main.cpp @@ -54,6 +54,8 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setApplicationName( a.tr("Chess Clock","Application name") ); + a.setOrganizationName("Chess Clock"); + a.setOrganizationDomain("chessclock.garage.maemo.org"); a.setApplicationVersion("0.1.0"); ChessClockWindow w; diff --git a/pic/black_small.png b/pic/black_small.png index 7c02939..1799de5 100644 Binary files a/pic/black_small.png and b/pic/black_small.png differ diff --git a/pic/white_small.png b/pic/white_small.png index 53e98b8..946b643 100644 Binary files a/pic/white_small.png and b/pic/white_small.png differ