author | smxx |
Sat, 01 May 2010 20:48:39 +0000 | |
changeset 3393 | 595655a09b26 |
parent 3236 | 4ab3917d7d44 |
child 4976 | 088d40d8aba2 |
permissions | -rw-r--r-- |
579 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
3236
4ab3917d7d44
Update (c) lines to 2010 as unc0rr requested - they all had varying values so I just took the first year mentioned, then tacked on -2010
nemo
parents:
2948
diff
changeset
|
3 |
* Copyright (c) 2006-2010 Andrey Korotaev <unC0Rr@gmail.com> |
579 | 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 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
33 |
QFont * font14 = new QFont("MS Shell Dlg", 14); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
34 |
QGridLayout * pageLayout = new QGridLayout(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
35 |
pageLayout->setColumnStretch(0, 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
36 |
pageLayout->setColumnStretch(1, 2); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
37 |
pageLayout->setColumnStretch(2, 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
38 |
pageLayout->setRowStretch(2, 100); |
579 | 39 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
40 |
BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true); |
579 | 41 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
42 |
BtnPlayDemo = new QPushButton(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
43 |
BtnPlayDemo->setFont(*font14); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
44 |
BtnPlayDemo->setText(QPushButton::tr("Play demo")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
45 |
pageLayout->addWidget(BtnPlayDemo, 3, 2); |
579 | 46 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
47 |
BtnRenameRecord = new QPushButton(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
48 |
BtnRenameRecord->setText(QPushButton::tr("Rename")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
49 |
pageLayout->addWidget(BtnRenameRecord, 0, 2); |
627 | 50 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
51 |
BtnRemoveRecord = new QPushButton(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
52 |
BtnRemoveRecord->setText(QPushButton::tr("Delete")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
53 |
pageLayout->addWidget(BtnRemoveRecord, 1, 2); |
720 | 54 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
55 |
DemosList = new QListWidget(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
56 |
DemosList->setGeometry(QRect(170, 10, 311, 311)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
57 |
pageLayout->addWidget(DemosList, 0, 1, 3, 1); |
627 | 58 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
59 |
connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord())); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
60 |
connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord())); |
579 | 61 |
} |
62 |
||
581 | 63 |
void PagePlayDemo::FillFromDir(RecordType rectype) |
579 | 64 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
65 |
QDir dir; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
66 |
QString extension; |
581 | 67 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
68 |
recType = rectype; |
627 | 69 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
70 |
dir.cd(cfgdir->absolutePath()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
71 |
if (rectype == RT_Demo) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
72 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
73 |
dir.cd("Demos"); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
74 |
extension = "hwd"; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
75 |
BtnPlayDemo->setText(QPushButton::tr("Play demo")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
76 |
} else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
77 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
78 |
dir.cd("Saves"); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
79 |
extension = "hws"; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
80 |
BtnPlayDemo->setText(QPushButton::tr("Load")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
81 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
82 |
dir.setFilter(QDir::Files); |
580 | 83 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
84 |
QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer))); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
85 |
sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\1"); |
580 | 86 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
87 |
DemosList->clear(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
88 |
DemosList->addItems(sl); |
579 | 89 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
90 |
for (int i = 0; i < DemosList->count(); ++i) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
91 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
92 |
DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%3.%2").arg(sl[i], extension, *cProtoVer))); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
93 |
DemosList->item(i)->setIcon(recType == RT_Demo ? QIcon(":/res/file_demo.png") : QIcon(":/res/file_save.png")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
94 |
} |
579 | 95 |
} |
96 |
||
627 | 97 |
void PagePlayDemo::renameRecord() |
98 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
99 |
QListWidgetItem * curritem = DemosList->currentItem(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
100 |
if (!curritem) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
101 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
102 |
QMessageBox::critical(this, |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
103 |
tr("Error"), |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
104 |
tr("Please select record from the list"), |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
105 |
tr("OK")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
106 |
return ; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
107 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
108 |
QFile rfile(curritem->data(Qt::UserRole).toString()); |
627 | 109 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
110 |
QFileInfo finfo(rfile); |
627 | 111 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
112 |
bool ok; |
627 | 113 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
114 |
QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok); |
627 | 115 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
116 |
if(ok && newname.size()) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
117 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
118 |
QString newfullname = QString("%1/%2.%3.%4") |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
119 |
.arg(finfo.absolutePath()) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
120 |
.arg(newname) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
121 |
.arg(*cProtoVer) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
122 |
.arg(finfo.suffix()); |
627 | 123 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
124 |
ok = rfile.rename(newfullname); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
125 |
if(!ok) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
126 |
QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
127 |
else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
128 |
FillFromDir(recType); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
129 |
} |
627 | 130 |
} |
720 | 131 |
|
132 |
void PagePlayDemo::removeRecord() |
|
133 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
134 |
QListWidgetItem * curritem = DemosList->currentItem(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
135 |
if (!curritem) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
136 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
137 |
QMessageBox::critical(this, |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
138 |
tr("Error"), |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
139 |
tr("Please select record from the list"), |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
140 |
tr("OK")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
141 |
return ; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
142 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
143 |
QFile rfile(curritem->data(Qt::UserRole).toString()); |
720 | 144 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
145 |
bool ok; |
720 | 146 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
147 |
ok = rfile.remove(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
148 |
if(!ok) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
149 |
QMessageBox::critical(this, tr("Error"), tr("Cannot delete file")); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
150 |
else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2742
diff
changeset
|
151 |
FillFromDir(recType); |
720 | 152 |
} |