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