QTfrontend/playrecordpage.cpp
changeset 5205 78138ae93820
parent 5204 e1a5f4d5d86a
child 5206 db775bddf771
equal deleted inserted replaced
5204:e1a5f4d5d86a 5205:78138ae93820
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2011 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>
       
    23 #include <QListWidgetItem>
       
    24 #include <QFileInfo>
       
    25 #include <QMessageBox>
       
    26 #include <QInputDialog>
       
    27 
       
    28 #include "hwconsts.h"
       
    29 #include "playrecordpage.h"
       
    30 
       
    31 PagePlayDemo::PagePlayDemo(QWidget* parent) : AbstractPage(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);
       
    38     pageLayout->setRowStretch(2, 100);
       
    39 
       
    40     BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true);
       
    41 
       
    42     BtnPlayDemo = new QPushButton(this);
       
    43     BtnPlayDemo->setFont(*font14);
       
    44     BtnPlayDemo->setText(QPushButton::tr("Play demo"));
       
    45     pageLayout->addWidget(BtnPlayDemo, 3, 2);
       
    46 
       
    47     BtnRenameRecord = new QPushButton(this);
       
    48     BtnRenameRecord->setText(QPushButton::tr("Rename"));
       
    49     pageLayout->addWidget(BtnRenameRecord, 0, 2);
       
    50 
       
    51     BtnRemoveRecord = new QPushButton(this);
       
    52     BtnRemoveRecord->setText(QPushButton::tr("Delete"));
       
    53     pageLayout->addWidget(BtnRemoveRecord, 1, 2);
       
    54 
       
    55     DemosList = new QListWidget(this);
       
    56     DemosList->setGeometry(QRect(170, 10, 311, 311));
       
    57     pageLayout->addWidget(DemosList, 0, 1, 3, 1);
       
    58 
       
    59     connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
       
    60     connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
       
    61 }
       
    62 
       
    63 void PagePlayDemo::FillFromDir(RecordType rectype)
       
    64 {
       
    65     QDir dir;
       
    66     QString extension;
       
    67 
       
    68     recType = rectype;
       
    69 
       
    70     dir.cd(cfgdir->absolutePath());
       
    71     if (rectype == RT_Demo)
       
    72     {
       
    73         dir.cd("Demos");
       
    74         extension = "hwd";
       
    75         BtnPlayDemo->setText(QPushButton::tr("Play demo"));
       
    76     } else
       
    77     {
       
    78         dir.cd("Saves");
       
    79         extension = "hws";
       
    80         BtnPlayDemo->setText(QPushButton::tr("Load"));
       
    81     }
       
    82     dir.setFilter(QDir::Files);
       
    83 
       
    84     QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer)));
       
    85     sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\1");
       
    86 
       
    87     DemosList->clear();
       
    88     DemosList->addItems(sl);
       
    89 
       
    90     for (int i = 0; i < DemosList->count(); ++i)
       
    91     {
       
    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"));
       
    94     }
       
    95 }
       
    96 
       
    97 void PagePlayDemo::renameRecord()
       
    98 {
       
    99     QListWidgetItem * curritem = DemosList->currentItem();
       
   100     if (!curritem)
       
   101     {
       
   102         QMessageBox::critical(this,
       
   103                 tr("Error"),
       
   104                 tr("Please select record from the list"),
       
   105                 tr("OK"));
       
   106         return ;
       
   107     }
       
   108     QFile rfile(curritem->data(Qt::UserRole).toString());
       
   109 
       
   110     QFileInfo finfo(rfile);
       
   111 
       
   112     bool ok;
       
   113 
       
   114     QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok);
       
   115 
       
   116     if(ok && newname.size())
       
   117     {
       
   118         QString newfullname = QString("%1/%2.%3.%4")
       
   119                                       .arg(finfo.absolutePath())
       
   120                                       .arg(newname)
       
   121                                       .arg(*cProtoVer)
       
   122                                       .arg(finfo.suffix());
       
   123 
       
   124         ok = rfile.rename(newfullname);
       
   125         if(!ok)
       
   126             QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname);
       
   127         else
       
   128             FillFromDir(recType);
       
   129     }
       
   130 }
       
   131 
       
   132 void PagePlayDemo::removeRecord()
       
   133 {
       
   134     QListWidgetItem * curritem = DemosList->currentItem();
       
   135     if (!curritem)
       
   136     {
       
   137         QMessageBox::critical(this,
       
   138                 tr("Error"),
       
   139                 tr("Please select record from the list"),
       
   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 }