author | nemo |
Fri, 26 Feb 2010 00:03:57 +0000 | |
changeset 2871 | eec42a0b7014 |
parent 2742 | 21c0d2e69753 |
child 2948 | 3f21a9dc93d0 |
permissions | -rw-r--r-- |
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"); |
|
2742 | 74 |
extension = "hwd"; |
581 | 75 |
BtnPlayDemo->setText(QPushButton::tr("Play demo")); |
76 |
} else |
|
77 |
{ |
|
78 |
dir.cd("Saves"); |
|
2742 | 79 |
extension = "hws"; |
581 | 80 |
BtnPlayDemo->setText(QPushButton::tr("Load")); |
81 |
} |
|
579 | 82 |
dir.setFilter(QDir::Files); |
580 | 83 |
|
2742 | 84 |
QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer))); |
85 |
sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\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 |
{ |
|
2742 | 92 |
DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%3.%2").arg(sl[i], extension, *cProtoVer))); |
93 |
DemosList->item(i)->setIcon(recType == RT_Demo ? QIcon(":/res/file_demo.png") : QIcon(":/res/file_save.png")); |
|
580 | 94 |
} |
579 | 95 |
} |
96 |
||
627 | 97 |
void PagePlayDemo::renameRecord() |
98 |
{ |
|
99 |
QListWidgetItem * curritem = DemosList->currentItem(); |
|
100 |
if (!curritem) |
|
101 |
{ |
|
102 |
QMessageBox::critical(this, |
|
103 |
tr("Error"), |
|
2631
163b0128bd21
A rather tedious rename of "Please," to "Please" triggered by bruce89 in interests of proper grammar. Also updated lrelease-qt4 which should pick up artart78's prior hedgewars_fr.ts fixes. Also a couple of typos in en.txt / fi.txt / pl.txt
nemo
parents:
1153
diff
changeset
|
104 |
tr("Please select record from the list"), |
627 | 105 |
tr("OK")); |
106 |
return ; |
|
107 |
} |
|
108 |
QFile rfile(curritem->data(Qt::UserRole).toString()); |
|
109 |
||
110 |
QFileInfo finfo(rfile); |
|
111 |
||
112 |
bool ok; |
|
113 |
||
2742 | 114 |
QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok); |
627 | 115 |
|
116 |
if(ok && newname.size()) |
|
117 |
{ |
|
2742 | 118 |
QString newfullname = QString("%1/%2.%3.%4") |
627 | 119 |
.arg(finfo.absolutePath()) |
120 |
.arg(newname) |
|
2742 | 121 |
.arg(*cProtoVer) |
627 | 122 |
.arg(finfo.suffix()); |
123 |
||
124 |
ok = rfile.rename(newfullname); |
|
125 |
if(!ok) |
|
628 | 126 |
QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname); |
627 | 127 |
else |
128 |
FillFromDir(recType); |
|
129 |
} |
|
130 |
} |
|
720 | 131 |
|
132 |
void PagePlayDemo::removeRecord() |
|
133 |
{ |
|
134 |
QListWidgetItem * curritem = DemosList->currentItem(); |
|
135 |
if (!curritem) |
|
136 |
{ |
|
137 |
QMessageBox::critical(this, |
|
138 |
tr("Error"), |
|
2631
163b0128bd21
A rather tedious rename of "Please," to "Please" triggered by bruce89 in interests of proper grammar. Also updated lrelease-qt4 which should pick up artart78's prior hedgewars_fr.ts fixes. Also a couple of typos in en.txt / fi.txt / pl.txt
nemo
parents:
1153
diff
changeset
|
139 |
tr("Please select record from the list"), |
720 | 140 |
tr("OK")); |
141 |
return ; |
|
142 |
} |
|
143 |
QFile rfile(curritem->data(Qt::UserRole).toString()); |
|
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 |
} |