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