QTfrontend/pageplayrecord.cpp
changeset 6060 fdfc01419815
parent 6059 ddf020d0941a
child 6061 15b4b485a1c5
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
     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 QLayout * PagePlayDemo::bodyLayoutDefinition()
       
    32 {
       
    33     QGridLayout * pageLayout = new QGridLayout();
       
    34     
       
    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     return pageLayout;
       
    58 }
       
    59 
       
    60 void PagePlayDemo::connectSignals()
       
    61 {
       
    62     connect(BtnRenameRecord, SIGNAL(clicked()), this, SLOT(renameRecord()));
       
    63     connect(BtnRemoveRecord, SIGNAL(clicked()), this, SLOT(removeRecord()));
       
    64 }
       
    65 
       
    66 PagePlayDemo::PagePlayDemo(QWidget* parent) : AbstractPage(parent)
       
    67 {
       
    68     initPage();
       
    69 }
       
    70 
       
    71 void PagePlayDemo::FillFromDir(RecordType rectype)
       
    72 {
       
    73     QDir dir;
       
    74     QString extension;
       
    75 
       
    76     recType = rectype;
       
    77 
       
    78     dir.cd(cfgdir->absolutePath());
       
    79     if (rectype == RT_Demo)
       
    80     {
       
    81         dir.cd("Demos");
       
    82         extension = "hwd";
       
    83         BtnPlayDemo->setText(QPushButton::tr("Play demo"));
       
    84     } else
       
    85     {
       
    86         dir.cd("Saves");
       
    87         extension = "hws";
       
    88         BtnPlayDemo->setText(QPushButton::tr("Load"));
       
    89     }
       
    90     dir.setFilter(QDir::Files);
       
    91 
       
    92     QStringList sl = dir.entryList(QStringList(QString("*.%2.%1").arg(extension, *cProtoVer)));
       
    93     sl.replaceInStrings(QRegExp(QString("^(.*)\\.%2\\.%1$").arg(extension, *cProtoVer)), "\\1");
       
    94 
       
    95     DemosList->clear();
       
    96     DemosList->addItems(sl);
       
    97 
       
    98     for (int i = 0; i < DemosList->count(); ++i)
       
    99     {
       
   100         DemosList->item(i)->setData(Qt::UserRole, dir.absoluteFilePath(QString("%1.%3.%2").arg(sl[i], extension, *cProtoVer)));
       
   101         DemosList->item(i)->setIcon(recType == RT_Demo ? QIcon(":/res/file_demo.png") : QIcon(":/res/file_save.png"));
       
   102     }
       
   103 }
       
   104 
       
   105 void PagePlayDemo::renameRecord()
       
   106 {
       
   107     QListWidgetItem * curritem = DemosList->currentItem();
       
   108     if (!curritem)
       
   109     {
       
   110         QMessageBox::critical(this,
       
   111                 tr("Error"),
       
   112                 tr("Please select record from the list"),
       
   113                 tr("OK"));
       
   114         return ;
       
   115     }
       
   116     QFile rfile(curritem->data(Qt::UserRole).toString());
       
   117 
       
   118     QFileInfo finfo(rfile);
       
   119 
       
   120     bool ok;
       
   121 
       
   122     QString newname = QInputDialog::getText(this, tr("Rename dialog"), tr("Enter new file name:"), QLineEdit::Normal, finfo.completeBaseName().replace("." + *cProtoVer, ""), &ok);
       
   123 
       
   124     if(ok && newname.size())
       
   125     {
       
   126         QString newfullname = QString("%1/%2.%3.%4")
       
   127                                       .arg(finfo.absolutePath())
       
   128                                       .arg(newname)
       
   129                                       .arg(*cProtoVer)
       
   130                                       .arg(finfo.suffix());
       
   131 
       
   132         ok = rfile.rename(newfullname);
       
   133         if(!ok)
       
   134             QMessageBox::critical(this, tr("Error"), tr("Cannot rename to") + newfullname);
       
   135         else
       
   136             FillFromDir(recType);
       
   137     }
       
   138 }
       
   139 
       
   140 void PagePlayDemo::removeRecord()
       
   141 {
       
   142     QListWidgetItem * curritem = DemosList->currentItem();
       
   143     if (!curritem)
       
   144     {
       
   145         QMessageBox::critical(this,
       
   146                 tr("Error"),
       
   147                 tr("Please select record from the list"),
       
   148                 tr("OK"));
       
   149         return ;
       
   150     }
       
   151     QFile rfile(curritem->data(Qt::UserRole).toString());
       
   152 
       
   153     bool ok;
       
   154 
       
   155     ok = rfile.remove();
       
   156     if(!ok)
       
   157         QMessageBox::critical(this, tr("Error"), tr("Cannot delete file"));
       
   158     else
       
   159         FillFromDir(recType);
       
   160 }
       
   161 
       
   162 bool PagePlayDemo::isSave()
       
   163 {
       
   164     return recType == RT_Save;
       
   165 }