1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/***************************************************************************
Savedialog - dialog for saving game
-------------------
begin : Mon 17 Apr 2006
copyright : (C) 2006 Michal Rudolf <[email protected]>
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "database.h"
#include "game.h"
#include "messagedialog.h"
#include "partialdate.h"
#include "savedialog.h"
#include "settings.h"
#include "tags.h"
#include <QLineEdit>
#include <QCompleter>
#include <QButtonGroup>
#if defined(_MSC_VER) && defined(_DEBUG)
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif // _MSC_VER
SaveDialog::SaveDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
{
ui.setupUi(this);
restoreLayout();
group = new QButtonGroup(this);
group->addButton(ui.result1Button);
group->addButton(ui.result5Button);
group->addButton(ui.result0Button);
group->addButton(ui.resultNoneButton);
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(buttonClicked(QAbstractButton*)));
}
void SaveDialog::restoreLayout()
{
AppSettings->layout(this);
}
SaveDialog::~SaveDialog()
{
delete group;
}
QString formatTagValue(const QString& s)
{
return (s.trimmed().isEmpty()) ? "?" : s;
}
QString formatTagDate(const QString& s)
{
return (s.trimmed().isEmpty()) ?
PDInvalidDate.asString() :
PartialDate().fromString(s).asString();
}
int SaveDialog::save(Database* database, Game& game)
{
QString gameTitle;
QString baseName = database->name();
if(game.tag(TagNameWhite).isEmpty() && game.tag(TagNameBlack).isEmpty())
{
gameTitle = "Save game to ";
}
else
{
QString name1 = game.tag(TagNameWhite);
if(name1.isEmpty())
{
name1 = "?";
}
QString name2 = game.tag(TagNameBlack);
if(name2.isEmpty())
{
name2 = "?";
}
gameTitle = QString("Save game '%1 vs. %2' to ").arg(name1).arg(name2);
}
gameTitle.append(baseName);
setWindowTitle(gameTitle);
ui.whiteEdit->setText(game.tag(TagNameWhite));
ui.whiteEloEdit->setText(game.tag(TagNameWhiteElo));
ui.blackEdit->setText(game.tag(TagNameBlack));
ui.blackEloEdit->setText(game.tag(TagNameBlackElo));
ui.eventEdit->setText(game.tag(TagNameEvent));
ui.siteEdit->setText(game.tag(TagNameSite));
ui.roundEdit->setText(game.tag(TagNameRound));
ui.dateEdit->setText(game.tag(TagNameDate));
ui.eventDateEdit->setText(game.tag(TagNameEventDate));
ui.whiteTeamEdit->setText(game.tag(TagNameWhiteTeam));
ui.blackTeamEdit->setText(game.tag(TagNameBlackTeam));
QList<QAbstractButton*> buttons = ui.result1Button->group()->buttons();
for(int i = 0; i < buttons.count(); i)
if(buttons[i]->text() == game.tag(TagNameResult))
{
buttons[i]->setChecked(true);
}
// Completion
setLineEdit(ui.whiteEdit, database, TagNameWhite);
setLineEdit(ui.blackEdit, database, TagNameBlack);
setLineEdit(ui.siteEdit, database, TagNameSite);
setLineEdit(ui.eventEdit, database, TagNameEvent);
int result = QDialog::exec();
if(result == Accepted)
{
game.setTag(TagNameWhite, formatTagValue(ui.whiteEdit->text()));
game.setTag(TagNameBlack, formatTagValue(ui.blackEdit->text()));
game.setTag(TagNameEvent, formatTagValue(ui.eventEdit->text()));
game.setTag(TagNameSite, formatTagValue(ui.siteEdit->text()));
game.setTag(TagNameRound, formatTagValue(ui.roundEdit->text()));
game.setTag(TagNameDate, formatTagDate(ui.dateEdit->text()));
game.setTag(TagNameEventDate, formatTagDate(ui.eventDateEdit->text()));
game.setTag(TagNameResult, ui.result1Button->group()->checkedButton()->text());
// Optional tag
if(ui.whiteEloEdit->text().toInt() || game.tag(TagNameWhiteElo).toInt())
{
game.setTag(TagNameWhiteElo, ui.whiteEloEdit->text());
}
if(ui.blackEloEdit->text().toInt() || game.tag(TagNameBlackElo).toInt())
{
game.setTag(TagNameBlackElo, ui.blackEloEdit->text());
}
QString t = ui.timeControl->text();
QString format = "H:mm:ss";
if(!t.isEmpty())
{
game.setTag(TagNameTimeControl, t);
}
QTime tt = ui.whiteStartTime->time();
if(tt.secsTo(QTime(0, 0)) > 0)
{
t = tt.toString(format);
game.setTag(TagNameWhiteClock, t);
}
tt = ui.blackStartTime->time();
if(tt.secsTo(QTime(0, 0)) > 0)
{
t = tt.toString(format);
game.setTag(TagNameBlackClock, t);
}
t = ui.whiteTeamEdit->text();
if(!t.isEmpty())
{
game.setTag(TagNameWhiteTeam, t);
}
t = ui.blackTeamEdit->text();
if(!t.isEmpty())
{
game.setTag(TagNameBlackTeam, t);
}
}
return result;
}
void SaveDialog::setLineEdit(QLineEdit* edit, Database* database, const QString &tagName)
{
QStringList words;
if((tagName == TagNameWhite) || (tagName == TagNameBlack))
{
words = database->index()->playerNames();
}
else
{
words = database->index()->tagValues(tagName);
}
QCompleter* completer = new QCompleter(words, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
edit->setCompleter(completer);
}
void SaveDialog::accept()
{
AppSettings->setLayout(this);
if((PartialDate().fromString(ui.dateEdit->text()).isValid()) &&
(PartialDate().fromString(ui.eventDateEdit->text()).isValid()))
{
// dates are formatted properly
QDialog::accept();
}
else
{
MessageDialog::error(tr("Dates are not properly formatted!"), tr("Invalid Data"));
}
}
void SaveDialog::discardChanges()
{
AppSettings->setLayout(this);
done(Discard);
}
void SaveDialog::reject()
{
AppSettings->setLayout(this);
QDialog::reject();
}
void SaveDialog::buttonClicked(QAbstractButton* button)
{
QDialogButtonBox::StandardButton sb = ui.buttonBox->standardButton(button);
switch(sb)
{
case QDialogButtonBox::Ok:
accept();
break;
case QDialogButtonBox::Cancel:
reject();
break;
case QDialogButtonBox::Discard:
discardChanges();
break;
default:
break;
}
}
|