2
* Hedgewars, a free turn based strategy game
3
* Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 2 of the License
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20
#include <QGridLayout>
21
#include <QPushButton>
22
#include <QListWidget>
23
#include <QListWidgetItem>
25
#include <QMessageBox>
26
#include <QInputDialog>
29
#include "pageplayrecord.h"
31
QLayout * PagePlayDemo::bodyLayoutDefinition()
33
QGridLayout * pageLayout = new QGridLayout();
35
pageLayout->setColumnStretch(0, 1);
36
pageLayout->setColumnStretch(1, 2);
37
pageLayout->setColumnStretch(2, 1);
38
pageLayout->setRowStretch(2, 100);
40
BtnPlayDemo = new QPushButton(this);
41
BtnPlayDemo->setFont(*font14);
42
BtnPlayDemo->setText(QPushButton::tr("Play demo"));
43
pageLayout->addWidget(BtnPlayDemo, 3, 2);
45
BtnRenameRecord = new QPushButton(this);
46
BtnRenameRecord->setText(QPushButton::tr("Rename"));
47
pageLayout->addWidget(BtnRenameRecord, 0, 2);
49
BtnRemoveRecord = new QPushButton(this);
50
BtnRemoveRecord->setText(QPushButton::tr("Delete"));
51
pageLayout->addWidget(BtnRemoveRecord, 1, 2);
53
DemosList = new QListWidget(this);
54
DemosList->setGeometry(QRect(170, 10, 311, 311));
55
pageLayout->addWidget(DemosList, 0, 1, 3, 1);
60
void PagePlayDemo::connectSignals()
62
connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
63
connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
66
PagePlayDemo::PagePlayDemo(QWidget* parent) : AbstractPage(parent)
71
void PagePlayDemo::FillFromDir(RecordType rectype)
78
dir.cd(cfgdir->absolutePath());
79
if (rectype == RT_Demo)
83
BtnPlayDemo->setText(QPushButton::tr("Play demo"));
88
BtnPlayDemo->setText(QPushButton::tr("Load"));
90
dir.setFilter(QDir::Files);
92
QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer)));
93
sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\1");
96
DemosList->addItems(sl);
98
for (int i = 0; i < DemosList->count(); ++i)
100
DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%3.%2").arg(sl[i], extension, *cProtoVer)));
101
DemosList->item(i)->setIcon(recType == RT_Demo ? QIcon(":/res/file_demo.png") : QIcon(":/res/file_save.png"));
105
void PagePlayDemo::renameRecord()
107
QListWidgetItem * curritem = DemosList->currentItem();
110
QMessageBox::critical(this,
112
tr("Please select record from the list"),
116
QFile rfile(curritem->data(Qt::UserRole).toString());
118
QFileInfo finfo(rfile);
122
QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok);
124
if(ok && newname.size())
126
QString newfullname = QString("%1/%2.%3.%4")
127
.arg(finfo.absolutePath())
130
.arg(finfo.suffix());
132
ok = rfile.rename(newfullname);
134
QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname);
136
FillFromDir(recType);
140
void PagePlayDemo::removeRecord()
142
QListWidgetItem * curritem = DemosList->currentItem();
145
QMessageBox::critical(this,
147
tr("Please select record from the list"),
151
QFile rfile(curritem->data(Qt::UserRole).toString());
157
QMessageBox::critical(this, tr("Error"), tr("Cannot delete file"));
159
FillFromDir(recType);
162
bool PagePlayDemo::isSave()
164
return recType == RT_Save;