QTfrontend/playrecordpage.cpp
author unc0rr
Sun, 22 Jun 2008 19:26:01 +0000
changeset 1022 f6a3fcb329ca
parent 720 97a9d67d5c3e
child 1066 1f1b3686a2b0
permissions -rw-r--r--
Start exit confirmation implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     1
/*
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2006, 2007 Andrey Korotaev <unC0Rr@gmail.com>
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     4
 *
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     8
 *
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    13
 *
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    17
 */
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    18
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    19
#include <QFont>
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    20
#include <QGridLayout>
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    21
#include <QPushButton>
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    22
#include <QListWidget>
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    23
#include <QListWidgetItem>
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    24
#include <QFileInfo>
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    25
#include <QMessageBox>
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    26
#include <QInputDialog>
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    27
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    28
#include "hwconsts.h"
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    29
#include "playrecordpage.h"
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    30
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    31
PagePlayDemo::PagePlayDemo(QWidget* parent) : QWidget(parent)
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    32
{
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    33
	QFont * font14 = new QFont("MS Shell Dlg", 14);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    34
	QGridLayout * pageLayout = new QGridLayout(this);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    35
	pageLayout->setColumnStretch(0, 1);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    36
	pageLayout->setColumnStretch(1, 2);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    37
	pageLayout->setColumnStretch(2, 1);
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    38
	pageLayout->setRowStretch(2, 100);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    39
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    40
	BtnBack = new QPushButton(this);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    41
	BtnBack->setFont(*font14);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    42
	BtnBack->setText(QPushButton::tr("Back"));
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    43
	pageLayout->addWidget(BtnBack, 3, 0);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    44
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    45
	BtnPlayDemo = new QPushButton(this);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    46
	BtnPlayDemo->setFont(*font14);
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    47
	BtnPlayDemo->setText(QPushButton::tr("Play demo"));
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    48
	pageLayout->addWidget(BtnPlayDemo, 3, 2);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    49
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    50
	BtnRenameRecord = new QPushButton(this);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    51
	BtnRenameRecord->setText(QPushButton::tr("Rename"));
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    52
	pageLayout->addWidget(BtnRenameRecord, 0, 2);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    53
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    54
	BtnRemoveRecord = new QPushButton(this);
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    55
	BtnRemoveRecord->setText(QPushButton::tr("Delete"));
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    56
	pageLayout->addWidget(BtnRemoveRecord, 1, 2);
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    57
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    58
	DemosList = new QListWidget(this);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    59
	DemosList->setGeometry(QRect(170, 10, 311, 311));
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    60
	pageLayout->addWidget(DemosList, 0, 1, 3, 1);
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    61
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    62
	connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
    63
	connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    64
}
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    65
581
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    66
void PagePlayDemo::FillFromDir(RecordType rectype)
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    67
{
581
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    68
	QDir dir;
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    69
	QString extension;
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    70
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    71
	recType = rectype;
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    72
581
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    73
	dir.cd(cfgdir->absolutePath());
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    74
	if (rectype == RT_Demo)
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    75
	{
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    76
		dir.cd("Demos");
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    77
		extension = "hwd_" + *cProtoVer;
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    78
		BtnPlayDemo->setText(QPushButton::tr("Play demo"));
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    79
	} else
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    80
	{
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    81
		dir.cd("Saves");
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    82
		extension = "hws_" + *cProtoVer;
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    83
		BtnPlayDemo->setText(QPushButton::tr("Load"));
909141b17f68 Better 'Load' page
unc0rr
parents: 580
diff changeset
    84
	}
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    85
	dir.setFilter(QDir::Files);
580
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    86
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    87
	QStringList sl = dir.entryList(QStringList(QString("*.%1").arg(extension)));
603
d7877468653b - Use current screen color depth in engine
unc0rr
parents: 581
diff changeset
    88
	sl.replaceInStrings(QRegExp(QString("^(.*)\\.%1$").arg(extension)), "\\1");
580
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    89
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    90
	DemosList->clear();
580
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    91
	DemosList->addItems(sl);
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    92
580
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    93
	for (int i = 0; i < DemosList->count(); ++i)
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    94
	{
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    95
		DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%2").arg(sl[i], extension)));
d3ebf84e9fad Working 'Load' page
unc0rr
parents: 579
diff changeset
    96
	}
579
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    97
}
94db15de0392 - Some changes to make build process clear
unc0rr
parents:
diff changeset
    98
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
    99
void PagePlayDemo::renameRecord()
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   100
{
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   101
	QListWidgetItem * curritem = DemosList->currentItem();
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   102
	if (!curritem)
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   103
	{
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   104
		QMessageBox::critical(this,
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   105
				tr("Error"),
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   106
				tr("Please, select record from the list"),
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   107
				tr("OK"));
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   108
		return ;
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   109
	}
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   110
	QFile rfile(curritem->data(Qt::UserRole).toString());
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   111
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   112
	QFileInfo finfo(rfile);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   113
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   114
	bool ok;
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   115
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   116
	QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName(), &ok);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   117
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   118
	if(ok && newname.size())
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   119
	{
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   120
		QString newfullname = QString("%1/%2.%3")
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   121
		                              .arg(finfo.absolutePath())
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   122
		                              .arg(newname)
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   123
		                              .arg(finfo.suffix());
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   124
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   125
		ok = rfile.rename(newfullname);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   126
		if(!ok)
628
1d592e3ae7ae Oops, forgot to save file before commiting
unc0rr
parents: 627
diff changeset
   127
			QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname);
627
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   128
		else
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   129
			FillFromDir(recType);
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   130
	}
92e136c3eb46 Button for renaming demo and save files
unc0rr
parents: 603
diff changeset
   131
}
720
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   132
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   133
void PagePlayDemo::removeRecord()
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   134
{
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   135
	QListWidgetItem * curritem = DemosList->currentItem();
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   136
	if (!curritem)
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   137
	{
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   138
		QMessageBox::critical(this,
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   139
				tr("Error"),
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   140
				tr("Please, select record from the list"),
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   141
				tr("OK"));
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   142
		return ;
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   143
	}
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   144
	QFile rfile(curritem->data(Qt::UserRole).toString());
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   145
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   146
	QFileInfo finfo(rfile);
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   147
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   148
	bool ok;
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   149
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   150
	ok = rfile.remove();
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   151
	if(!ok)
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   152
		QMessageBox::critical(this, tr("Error"), tr("Cannot delete file"));
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   153
	else
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   154
		FillFromDir(recType);
97a9d67d5c3e - Add 'Delete' button to demos and saves pages
unc0rr
parents: 628
diff changeset
   155
}