maplistpanel.cpp
1 //------------------------------------------------------------------------------
2 // maplistpanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "maplistpanel.h"
24 #include "ui_maplistpanel.h"
25 
26 #include "ini/ini.h"
27 #include "plugins/engineplugin.h"
28 #include "serverapi/gamecreateparams.h"
29 #include "commongui.h"
30 #include <QStandardItemModel>
31 
32 DClass<MapListPanel> : public Ui::MapListPanel
33 {
34 };
35 
36 DPointered(MapListPanel)
37 
38 MapListPanel::MapListPanel(QWidget *parent)
39 :QWidget(parent)
40 {
41  d->setupUi(this);
42  d->lstMaplist->setModel(new QStandardItemModel(this));
43  this->connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)),
44  SLOT(onFocusChanged(QWidget*, QWidget*)));
45 }
46 
47 MapListPanel::~MapListPanel()
48 {
49 }
50 
51 void MapListPanel::addMapFromEditBoxToList()
52 {
53  addMapToMaplist(d->leMapname->text().trimmed());
54 }
55 
56 void MapListPanel::addMapToMaplist(const QString &map)
57 {
58  if (map.isEmpty())
59  {
60  return;
61  }
62  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstMaplist->model());
63  QStandardItem* it = new QStandardItem(map);
64  it->setDragEnabled(true);
65  it->setDropEnabled(false);
66  model->appendRow(it);
67 }
68 
69 void MapListPanel::onFocusChanged(QWidget* old, QWidget* now)
70 {
71  if (now == d->leMapname)
72  {
73  d->btnAddMapToMaplist->setDefault(true);
74  }
75  else if (old == d->leMapname)
76  {
77  d->btnAddMapToMaplist->setDefault(false);
78  }
79 }
80 
81 void MapListPanel::removeSelectedFromList()
82 {
83  const bool bSelectNextLowest = true;
84  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstMaplist, bSelectNextLowest);
85 }
86 
87 void MapListPanel::fillInParams(GameCreateParams &params)
88 {
89  params.setMapList(CommonGUI::listViewStandardItemsToStringList(d->lstMaplist));
90  params.setRandomMapRotation(d->cbRandomMapRotation->isChecked());
91 }
92 
93 void MapListPanel::loadConfig(Ini &config)
94 {
95  IniSection section = config.section("Rules");
96  QStringList stringList = section["maplist"].valueString().split(";");
97  QAbstractItemModel *model = d->lstMaplist->model();
98  model->removeRows(0, model->rowCount());
99  foreach(QString s, stringList)
100  {
101  addMapToMaplist(s);
102  }
103  d->cbRandomMapRotation->setChecked(section["randomMapRotation"]);
104 }
105 
106 void MapListPanel::saveConfig(Ini &config)
107 {
108  IniSection section = config.section("Rules");
109  QStringList stringList = CommonGUI::listViewStandardItemsToStringList(d->lstMaplist);
110  section["maplist"] = stringList.join(";");
111  section["randomMapRotation"] = d->cbRandomMapRotation->isChecked();
112 }
113 
114 void MapListPanel::setupForEngine(const EnginePlugin *engine)
115 {
116  d->cbRandomMapRotation->setVisible(engine->data()->supportsRandomMapRotation);
117 }
static QStringList listViewStandardItemsToStringList(QListView *listview)
Reads items from a QListView that uses QStandardItemModel and puts texts of these items into a list o...
Definition: commongui.cpp:50
Game parametrization data used when creating new games.
static void removeSelectedRowsFromStandardItemView(QAbstractItemView *view, bool bSelectNextItem=false)
Removes all selected rows from a QAbstractItemView.
Definition: commongui.cpp:83
Configuration handler.
Definition: ini.h:69
INI section representation.
Definition: inisection.h:40
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91