579
|
1 |
/*
|
1066
|
2 |
* Hedgewars, a free turn based strategy game
|
579
|
3 |
* Copyright (c) 2006, 2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
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
|
|
8 |
*
|
|
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.
|
|
13 |
*
|
|
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
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QFont>
|
|
20 |
#include <QGridLayout>
|
|
21 |
#include <QPushButton>
|
|
22 |
#include <QListWidget>
|
627
|
23 |
#include <QListWidgetItem>
|
|
24 |
#include <QFileInfo>
|
|
25 |
#include <QMessageBox>
|
|
26 |
#include <QInputDialog>
|
579
|
27 |
|
|
28 |
#include "hwconsts.h"
|
|
29 |
#include "playrecordpage.h"
|
|
30 |
|
1153
|
31 |
PagePlayDemo::PagePlayDemo(QWidget* parent) : AbstractPage(parent)
|
579
|
32 |
{
|
|
33 |
QFont * font14 = new QFont("MS Shell Dlg", 14);
|
|
34 |
QGridLayout * pageLayout = new QGridLayout(this);
|
|
35 |
pageLayout->setColumnStretch(0, 1);
|
|
36 |
pageLayout->setColumnStretch(1, 2);
|
|
37 |
pageLayout->setColumnStretch(2, 1);
|
720
|
38 |
pageLayout->setRowStretch(2, 100);
|
579
|
39 |
|
1153
|
40 |
BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true);
|
579
|
41 |
|
627
|
42 |
BtnPlayDemo = new QPushButton(this);
|
579
|
43 |
BtnPlayDemo->setFont(*font14);
|
|
44 |
BtnPlayDemo->setText(QPushButton::tr("Play demo"));
|
720
|
45 |
pageLayout->addWidget(BtnPlayDemo, 3, 2);
|
579
|
46 |
|
627
|
47 |
BtnRenameRecord = new QPushButton(this);
|
|
48 |
BtnRenameRecord->setText(QPushButton::tr("Rename"));
|
|
49 |
pageLayout->addWidget(BtnRenameRecord, 0, 2);
|
|
50 |
|
720
|
51 |
BtnRemoveRecord = new QPushButton(this);
|
|
52 |
BtnRemoveRecord->setText(QPushButton::tr("Delete"));
|
|
53 |
pageLayout->addWidget(BtnRemoveRecord, 1, 2);
|
|
54 |
|
627
|
55 |
DemosList = new QListWidget(this);
|
579
|
56 |
DemosList->setGeometry(QRect(170, 10, 311, 311));
|
720
|
57 |
pageLayout->addWidget(DemosList, 0, 1, 3, 1);
|
627
|
58 |
|
|
59 |
connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
|
720
|
60 |
connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
|
579
|
61 |
}
|
|
62 |
|
581
|
63 |
void PagePlayDemo::FillFromDir(RecordType rectype)
|
579
|
64 |
{
|
581
|
65 |
QDir dir;
|
|
66 |
QString extension;
|
|
67 |
|
627
|
68 |
recType = rectype;
|
|
69 |
|
581
|
70 |
dir.cd(cfgdir->absolutePath());
|
|
71 |
if (rectype == RT_Demo)
|
|
72 |
{
|
|
73 |
dir.cd("Demos");
|
|
74 |
extension = "hwd_" + *cProtoVer;
|
|
75 |
BtnPlayDemo->setText(QPushButton::tr("Play demo"));
|
|
76 |
} else
|
|
77 |
{
|
|
78 |
dir.cd("Saves");
|
|
79 |
extension = "hws_" + *cProtoVer;
|
|
80 |
BtnPlayDemo->setText(QPushButton::tr("Load"));
|
|
81 |
}
|
579
|
82 |
dir.setFilter(QDir::Files);
|
580
|
83 |
|
|
84 |
QStringList sl = dir.entryList(QStringList(QString("*.%1").arg(extension)));
|
603
|
85 |
sl.replaceInStrings(QRegExp(QString("^(.*)\\.%1$").arg(extension)), "\\1");
|
580
|
86 |
|
579
|
87 |
DemosList->clear();
|
580
|
88 |
DemosList->addItems(sl);
|
579
|
89 |
|
580
|
90 |
for (int i = 0; i < DemosList->count(); ++i)
|
|
91 |
{
|
|
92 |
DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%2").arg(sl[i], extension)));
|
|
93 |
}
|
579
|
94 |
}
|
|
95 |
|
627
|
96 |
void PagePlayDemo::renameRecord()
|
|
97 |
{
|
|
98 |
QListWidgetItem * curritem = DemosList->currentItem();
|
|
99 |
if (!curritem)
|
|
100 |
{
|
|
101 |
QMessageBox::critical(this,
|
|
102 |
tr("Error"),
|
|
103 |
tr("Please, select record from the list"),
|
|
104 |
tr("OK"));
|
|
105 |
return ;
|
|
106 |
}
|
|
107 |
QFile rfile(curritem->data(Qt::UserRole).toString());
|
|
108 |
|
|
109 |
QFileInfo finfo(rfile);
|
|
110 |
|
|
111 |
bool ok;
|
|
112 |
|
|
113 |
QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName(), &ok);
|
|
114 |
|
|
115 |
if(ok && newname.size())
|
|
116 |
{
|
|
117 |
QString newfullname = QString("%1/%2.%3")
|
|
118 |
.arg(finfo.absolutePath())
|
|
119 |
.arg(newname)
|
|
120 |
.arg(finfo.suffix());
|
|
121 |
|
|
122 |
ok = rfile.rename(newfullname);
|
|
123 |
if(!ok)
|
628
|
124 |
QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname);
|
627
|
125 |
else
|
|
126 |
FillFromDir(recType);
|
|
127 |
}
|
|
128 |
}
|
720
|
129 |
|
|
130 |
void PagePlayDemo::removeRecord()
|
|
131 |
{
|
|
132 |
QListWidgetItem * curritem = DemosList->currentItem();
|
|
133 |
if (!curritem)
|
|
134 |
{
|
|
135 |
QMessageBox::critical(this,
|
|
136 |
tr("Error"),
|
|
137 |
tr("Please, select record from the list"),
|
|
138 |
tr("OK"));
|
|
139 |
return ;
|
|
140 |
}
|
|
141 |
QFile rfile(curritem->data(Qt::UserRole).toString());
|
|
142 |
|
|
143 |
QFileInfo finfo(rfile);
|
|
144 |
|
|
145 |
bool ok;
|
|
146 |
|
|
147 |
ok = rfile.remove();
|
|
148 |
if(!ok)
|
|
149 |
QMessageBox::critical(this, tr("Error"), tr("Cannot delete file"));
|
|
150 |
else
|
|
151 |
FillFromDir(recType);
|
|
152 |
}
|