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