maplistpanel.cpp
1 //------------------------------------------------------------------------------
2 // maplistpanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 "gui/createserverdialog.h"
28 #include "plugins/engineplugin.h"
29 #include "serverapi/gamecreateparams.h"
30 #include "commongui.h"
31 #include <QStandardItemModel>
32 #include <QTimer>
33 
34 DClass<MapListPanel> : public Ui::MapListPanel
35 {
36 public:
37  CreateServerDialog *parentDialog;
38 };
39 
40 DPointered(MapListPanel)
41 
42 MapListPanel::MapListPanel(QWidget *parent)
43 :QWidget(parent)
44 {
45  d->setupUi(this);
46  d->lstMaplist->setModel(new QStandardItemModel(this));
47  this->connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)),
48  SLOT(onFocusChanged(QWidget*, QWidget*)));
49 
50  d->parentDialog = NULL;
51 
52  d->lblWarning->setText(MapListPanel::tr("Current map isn't present on map list. Game may misbehave."));
53  d->lblWarning->setPixmap(QPixmap(":/icons/exclamation.png"));
54  d->lblWarning->setWordWrap(true);
55 }
56 
57 MapListPanel::~MapListPanel()
58 {
59 }
60 
61 void MapListPanel::addMapFromEditBoxToList()
62 {
63  addMapToMaplist(d->leMapname->text().trimmed());
64 }
65 
66 void MapListPanel::addMapToMaplist(const QString &map)
67 {
68  if (map.isEmpty())
69  {
70  return;
71  }
72  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstMaplist->model());
73  QStandardItem* it = new QStandardItem(map);
74  it->setDragEnabled(true);
75  it->setDropEnabled(false);
76  model->appendRow(it);
77  updateMapWarningVisibility();
78 }
79 
80 void MapListPanel::onFocusChanged(QWidget* old, QWidget* now)
81 {
82  if (now == d->leMapname)
83  {
84  d->btnAddMapToMaplist->setDefault(true);
85  }
86  else if (old == d->leMapname)
87  {
88  d->btnAddMapToMaplist->setDefault(false);
89  }
90 }
91 
92 void MapListPanel::removeSelectedFromList()
93 {
94  const bool bSelectNextLowest = true;
95  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstMaplist, bSelectNextLowest);
96  updateMapWarningVisibility();
97 }
98 
99 void MapListPanel::fillInParams(GameCreateParams &params)
100 {
101  params.setMapList(CommonGUI::listViewStandardItemsToStringList(d->lstMaplist));
102  params.setRandomMapRotation(d->cbRandomMapRotation->isChecked());
103 }
104 
105 bool MapListPanel::hasMaps() const
106 {
107  return d->lstMaplist->model()->rowCount() > 0;
108 }
109 
110 bool MapListPanel::isMapOnList(const QString &mapName) const
111 {
112  foreach (const QString &candidate, CommonGUI::listViewStandardItemsToStringList(d->lstMaplist))
113  {
114  if (candidate.compare(mapName, Qt::CaseInsensitive) == 0)
115  {
116  return true;
117  }
118  }
119  return false;
120 }
121 
122 void MapListPanel::showEvent(QShowEvent *event)
123 {
124  updateMapWarningVisibility();
125 }
126 
127 void MapListPanel::updateMapWarningVisibility()
128 {
129  d->lblWarning->setVisible(hasMaps() && !isMapOnList(d->parentDialog->mapName()));
130 }
131 
132 void MapListPanel::loadConfig(Ini &config)
133 {
134  IniSection section = config.section("Rules");
135  QStringList stringList = section["maplist"].valueString().split(";");
136  QAbstractItemModel *model = d->lstMaplist->model();
137  model->removeRows(0, model->rowCount());
138  foreach(QString s, stringList)
139  {
140  addMapToMaplist(s);
141  }
142  d->cbRandomMapRotation->setChecked(section["randomMapRotation"]);
143 
144  // Timer triggers slot after config is fully loaded.
145  QTimer::singleShot(0, this, SLOT(updateMapWarningVisibility()));
146 }
147 
148 void MapListPanel::saveConfig(Ini &config)
149 {
150  IniSection section = config.section("Rules");
151  QStringList stringList = CommonGUI::listViewStandardItemsToStringList(d->lstMaplist);
152  section["maplist"] = stringList.join(";");
153  section["randomMapRotation"] = d->cbRandomMapRotation->isChecked();
154 }
155 
156 void MapListPanel::setCreateServerDialog(CreateServerDialog *dialog)
157 {
158  d->parentDialog = dialog;
159 }
160 
161 void MapListPanel::setupForEngine(const EnginePlugin *engine)
162 {
163  d->cbRandomMapRotation->setVisible(engine->data()->supportsRandomMapRotation);
164 }
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:51
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:84
Dialog window allowing user to create a game.
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:95