QTfrontend/pageeditteam.cpp
changeset 5078 3527f0e7bb21
child 5204 e1a5f4d5d86a
equal deleted inserted replaced
5076:b3bb27f4ba6b 5078:3527f0e7bb21
       
     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 <QGridLayout>
       
    20 #include <QPushButton>
       
    21 #include <QComboBox>
       
    22 #include <QLabel>
       
    23 #include <QLineEdit>
       
    24 #include <QTabWidget>
       
    25 #include <QGroupBox>
       
    26 #include <QToolBox>
       
    27 #include <QApplication>
       
    28 
       
    29 #include "pages.h"
       
    30 #include "sdlkeys.h"
       
    31 #include "hwconsts.h"
       
    32 #include "SquareLabel.h"
       
    33 #include "hats.h"
       
    34 
       
    35 PageEditTeam::PageEditTeam(QWidget* parent, SDLInteraction * sdli) :
       
    36   AbstractPage(parent)
       
    37 {
       
    38     mySdli = sdli;
       
    39     QGridLayout * pageLayout = new QGridLayout(this);
       
    40     QTabWidget * tbw = new QTabWidget(this);
       
    41     QWidget * page1 = new QWidget(this);
       
    42     QWidget * page2 = new QWidget(this);
       
    43     tbw->addTab(page1, tr("General"));
       
    44     tbw->addTab(page2, tr("Advanced"));
       
    45     pageLayout->addWidget(tbw, 0, 0, 1, 3);
       
    46     BtnTeamDiscard = addButton(":/res/Exit.png", pageLayout, 1, 0, true);
       
    47     BtnTeamSave = addButton(":/res/Save.png", pageLayout, 1, 2, true);;
       
    48     BtnTeamSave->setStyleSheet("QPushButton{margin: 12px 0px 12px 0px;}");
       
    49     BtnTeamDiscard->setFixedHeight(BtnTeamSave->height());
       
    50     BtnTeamDiscard->setStyleSheet("QPushButton{margin-top: 31px;}");
       
    51 
       
    52     QHBoxLayout * page1Layout = new QHBoxLayout(page1);
       
    53     page1Layout->setAlignment(Qt::AlignTop);
       
    54     QGridLayout * page2Layout = new QGridLayout(page2);
       
    55 
       
    56 // ====== Page 1 ======
       
    57     QVBoxLayout * vbox1 = new QVBoxLayout();
       
    58     QVBoxLayout * vbox2 = new QVBoxLayout();
       
    59     page1Layout->addLayout(vbox1);
       
    60     page1Layout->addLayout(vbox2);
       
    61 
       
    62     GBoxHedgehogs = new QGroupBox(this);
       
    63     GBoxHedgehogs->setTitle(QGroupBox::tr("Team Members"));
       
    64     GBoxHedgehogs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
       
    65     QGridLayout * GBHLayout = new QGridLayout(GBoxHedgehogs);
       
    66 
       
    67     signalMapper = new QSignalMapper(this);
       
    68 
       
    69     HatsModel * hatsModel = new HatsModel(GBoxHedgehogs);
       
    70     for(int i = 0; i < 8; i++)
       
    71     {
       
    72         HHHats[i] = new QComboBox(GBoxHedgehogs);
       
    73         HHHats[i]->setModel(hatsModel);
       
    74         HHHats[i]->setIconSize(QSize(32, 37));
       
    75         //HHHats[i]->setSizeAdjustPolicy(QComboBox::AdjustToContents);
       
    76         //HHHats[i]->setModelColumn(1);
       
    77         //HHHats[i]->setMinimumWidth(132);
       
    78         GBHLayout->addWidget(HHHats[i], i, 0);
       
    79 
       
    80         HHNameEdit[i] = new QLineEdit(GBoxHedgehogs);
       
    81         HHNameEdit[i]->setMaxLength(64);
       
    82         HHNameEdit[i]->setMinimumWidth(120);
       
    83         GBHLayout->addWidget(HHNameEdit[i], i, 1);
       
    84 
       
    85         randButton[i] = addButton(":/res/dice.png", GBHLayout, i, 3, true);
       
    86 
       
    87         connect(randButton[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
       
    88             signalMapper->setMapping(randButton[i], i);
       
    89 
       
    90     }
       
    91 
       
    92     randTeamButton = addButton(QPushButton::tr("Random Team"), GBHLayout, 9, false);
       
    93 
       
    94     vbox1->addWidget(GBoxHedgehogs);
       
    95 
       
    96 
       
    97     GBoxTeam = new QGroupBox(this);
       
    98     GBoxTeam->setTitle(QGroupBox::tr("Team Settings"));
       
    99     GBoxTeam->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
       
   100     QGridLayout * GBTLayout = new QGridLayout(GBoxTeam);
       
   101     QLabel * tmpLabel = new QLabel(GBoxTeam);
       
   102     tmpLabel->setText(QLabel::tr("Name"));
       
   103     GBTLayout->addWidget(tmpLabel, 0, 0);
       
   104     tmpLabel = new QLabel(GBoxTeam);
       
   105     tmpLabel->setText(QLabel::tr("Type"));
       
   106     GBTLayout->addWidget(tmpLabel, 1, 0);
       
   107     tmpLabel = new QLabel(GBoxTeam);
       
   108     tmpLabel->setText(QLabel::tr("Grave"));
       
   109     GBTLayout->addWidget(tmpLabel, 2, 0);
       
   110     tmpLabel = new QLabel(GBoxTeam);
       
   111     tmpLabel->setText(QLabel::tr("Flag"));
       
   112     GBTLayout->addWidget(tmpLabel, 3, 0);
       
   113     tmpLabel = new QLabel(GBoxTeam);
       
   114     tmpLabel->setText(QLabel::tr("Voice"));
       
   115     GBTLayout->addWidget(tmpLabel, 4, 0);
       
   116 
       
   117 
       
   118     TeamNameEdit = new QLineEdit(GBoxTeam);
       
   119     TeamNameEdit->setMaxLength(64);
       
   120     GBTLayout->addWidget(TeamNameEdit, 0, 1);
       
   121     vbox2->addWidget(GBoxTeam);
       
   122 
       
   123     CBTeamLvl = new QComboBox(GBoxTeam);
       
   124     CBTeamLvl->setIconSize(QSize(48, 48));
       
   125     CBTeamLvl->addItem(QIcon(":/res/botlevels/0.png"), QComboBox::tr("Human"));
       
   126     for(int i = 5; i > 0; i--)
       
   127         CBTeamLvl->addItem(
       
   128                 QIcon(QString(":/res/botlevels/%1.png").arg(6 - i)),
       
   129                 QString("%1 %2").arg(QComboBox::tr("Level")).arg(i)
       
   130                 );
       
   131     GBTLayout->addWidget(CBTeamLvl, 1, 1);
       
   132 
       
   133     CBGrave = new QComboBox(GBoxTeam);
       
   134     CBGrave->setMaxCount(65535);
       
   135     CBGrave->setIconSize(QSize(32, 32));
       
   136     GBTLayout->addWidget(CBGrave, 2, 1);
       
   137 
       
   138     CBFlag = new QComboBox(GBoxTeam);
       
   139     CBFlag->setMaxCount(65535);
       
   140     CBFlag->setIconSize(QSize(22, 15));
       
   141     GBTLayout->addWidget(CBFlag, 3, 1);
       
   142 
       
   143     {
       
   144         QHBoxLayout * hbox = new QHBoxLayout();
       
   145         CBVoicepack = new QComboBox(GBoxTeam);
       
   146         {
       
   147             QDir tmpdir;
       
   148             tmpdir.cd(datadir->absolutePath());
       
   149             tmpdir.cd("Sounds/voices");
       
   150             QStringList list = tmpdir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
       
   151             CBVoicepack->addItems(list);
       
   152         }
       
   153         hbox->addWidget(CBVoicepack, 100);
       
   154         BtnTestSound = addButton(":/res/PlaySound.png", hbox, 1, true);
       
   155         hbox->setStretchFactor(BtnTestSound, 1);
       
   156         connect(BtnTestSound, SIGNAL(clicked()), this, SLOT(testSound()));
       
   157         GBTLayout->addLayout(hbox, 4, 1);
       
   158     }
       
   159 
       
   160     GBoxFort = new QGroupBox(this);
       
   161     GBoxFort->setTitle(QGroupBox::tr("Fort"));
       
   162     QGridLayout * GBFLayout = new QGridLayout(GBoxFort);
       
   163     CBFort = new QComboBox(GBoxFort);
       
   164     CBFort->setMaxCount(65535);
       
   165     GBFLayout->addWidget(CBFort, 0, 0);
       
   166     FortPreview = new SquareLabel(GBoxFort);
       
   167     FortPreview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   168     FortPreview->setMinimumSize(128, 128);
       
   169     FortPreview->setPixmap(QPixmap());
       
   170     // perhaps due to handling its own paintevents, SquareLabel doesn't play nice with the stars
       
   171     //FortPreview->setAttribute(Qt::WA_PaintOnScreen, true);
       
   172     GBFLayout->addWidget(FortPreview, 1, 0);
       
   173     vbox2->addWidget(GBoxFort);
       
   174 
       
   175     QDir tmpdir;
       
   176     tmpdir.cd(datadir->absolutePath());
       
   177     tmpdir.cd("Forts");
       
   178     tmpdir.setFilter(QDir::Files);
       
   179 
       
   180     connect(CBFort, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(CBFort_activated(const QString &)));
       
   181     CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1"));
       
   182 
       
   183     tmpdir.cd("../Graphics/Graves");
       
   184     QStringList list = tmpdir.entryList(QStringList("*.png"));
       
   185     for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
       
   186     {
       
   187         QPixmap pix(datadir->absolutePath() + "/Graphics/Graves/" + *it);
       
   188         QIcon icon(pix.copy(0, 0, 32, 32));
       
   189         CBGrave->addItem(icon, (*it).replace(QRegExp("^(.*)\\.png"), "\\1"));
       
   190     }
       
   191 
       
   192     tmpdir.cd(datadir->absolutePath());
       
   193     tmpdir.cd("Graphics/Flags");
       
   194     list = tmpdir.entryList(QStringList("*.png"));
       
   195     
       
   196     // add the default flag
       
   197     CBFlag->addItem(QIcon(QPixmap(datadir->absolutePath() + "/Graphics/Flags/hedgewars.png").copy(0, 0, 22, 15)), "Hedgewars", "hedgewars");
       
   198 
       
   199     CBFlag->insertSeparator(CBFlag->count());
       
   200     // add all country flags
       
   201     for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
       
   202     {
       
   203         QPixmap pix(datadir->absolutePath() + "/Graphics/Flags/" + *it);
       
   204         QIcon icon(pix.copy(0, 0, 22, 15));
       
   205         if(it->compare("cpu.png") && it->compare("hedgewars.png") && (it->indexOf("cm_") == -1)) // skip cpu and hedgewars flags as well as all community flags
       
   206         {
       
   207             QString flag = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
       
   208             CBFlag->addItem(icon, QString(flag).replace("_", " "), flag);
       
   209         }
       
   210     }
       
   211     CBFlag->insertSeparator(CBFlag->count());
       
   212     // add all community flags
       
   213     for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
       
   214     {
       
   215         QPixmap pix(datadir->absolutePath() + "/Graphics/Flags/" + *it);
       
   216         QIcon icon(pix.copy(0, 0, 22, 15));
       
   217         if(it->indexOf("cm_") > -1) // skip non community flags this time
       
   218         {
       
   219             QString flag = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
       
   220             CBFlag->addItem(icon, QString(flag).replace("cm_", QComboBox::tr("Community") + ": "), flag);
       
   221         }
       
   222     }
       
   223 
       
   224     vbox1->addStretch();
       
   225     vbox2->addStretch();
       
   226 
       
   227 // ====== Page 2 ======
       
   228     GBoxBinds = new QGroupBox(this);
       
   229     GBoxBinds->setTitle(QGroupBox::tr("Key binds"));
       
   230     QGridLayout * GBBLayout = new QGridLayout(GBoxBinds);
       
   231     BindsBox = new QToolBox(GBoxBinds);
       
   232     BindsBox->setLineWidth(0);
       
   233     GBBLayout->addWidget(BindsBox);
       
   234     page2Layout->addWidget(GBoxBinds, 0, 0);
       
   235 
       
   236     quint16 i = 0;
       
   237     quint16 num = 0;
       
   238     QWidget * curW = NULL;
       
   239     QGridLayout * pagelayout = NULL;
       
   240     QLabel* l = NULL;
       
   241     while (i < BINDS_NUMBER) {
       
   242         if(cbinds[i].category != NULL)
       
   243         {
       
   244             if(curW != NULL)
       
   245             {
       
   246                 l = new QLabel(curW);
       
   247                 l->setText("");
       
   248                 pagelayout->addWidget(l, num++, 0, 1, 2);
       
   249             }
       
   250             curW = new QWidget(this);
       
   251             BindsBox->addItem(curW, QApplication::translate("binds (categories)", cbinds[i].category));
       
   252             pagelayout = new QGridLayout(curW);
       
   253             num = 0;
       
   254         }
       
   255         if(cbinds[i].description != NULL)
       
   256         {
       
   257             l = new QLabel(curW);
       
   258             l->setText((num > 0 ? QString("\n") : QString("")) + QApplication::translate("binds (descriptions)", cbinds[i].description));
       
   259             pagelayout->addWidget(l, num++, 0, 1, 2);
       
   260         }
       
   261 
       
   262         l = new QLabel(curW);
       
   263         l->setText(QApplication::translate("binds", cbinds[i].name));
       
   264         l->setAlignment(Qt::AlignRight);
       
   265         pagelayout->addWidget(l, num, 0);
       
   266         CBBind[i] = new QComboBox(curW);
       
   267         for(int j = 0; sdlkeys[j][1][0] != '\0'; j++)
       
   268             CBBind[i]->addItem(QApplication::translate("binds (keys)", sdlkeys[j][1]).contains(": ") ? QApplication::translate("binds (keys)", sdlkeys[j][1]) : QApplication::translate("binds (keys)", "Keyboard") + QString(": ") + QApplication::translate("binds (keys)", sdlkeys[j][1]), sdlkeys[j][0]);
       
   269         pagelayout->addWidget(CBBind[i++], num++, 1);
       
   270     }
       
   271 }
       
   272 
       
   273 void PageEditTeam::CBFort_activated(const QString & fortname)
       
   274 {
       
   275     QPixmap pix(datadir->absolutePath() + "/Forts/" + fortname + "L.png");
       
   276     FortPreview->setPixmap(pix);
       
   277 }
       
   278 
       
   279 void PageEditTeam::testSound()
       
   280 {
       
   281     Mix_Chunk *sound;
       
   282     QDir tmpdir;
       
   283     mySdli->SDLMusicInit();
       
   284     
       
   285     tmpdir.cd(datadir->absolutePath());
       
   286     tmpdir.cd("Sounds/voices");
       
   287     tmpdir.cd(CBVoicepack->currentText());
       
   288     QStringList list = tmpdir.entryList(QStringList() << "Illgetyou.ogg" << "Incoming.ogg" << "Stupid.ogg" << "Coward.ogg" << "Firstblood.ogg", QDir::Files);
       
   289     if (list.size()) {
       
   290         sound = Mix_LoadWAV(QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData());
       
   291         Mix_PlayChannel(-1, sound, 0);
       
   292     }
       
   293 }