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 "commongui.h"
27 #include "gui/createserverdialog.h"
28 #include "ini/ini.h"
29 #include "maplistselector.h"
30 #include "plugins/engineplugin.h"
31 #include "serverapi/gamecreateparams.h"
32 
33 #include <QStandardItemModel>
34 #include <QTimer>
35 
36 DClass<MapListPanel> : public Ui::MapListPanel
37 {
38 public:
39  CreateServerDialog *parentDialog;
40 };
41 
42 DPointered(MapListPanel)
43 
44 MapListPanel::MapListPanel(QWidget *parent)
45  : QWidget(parent)
46 {
47  d->setupUi(this);
48  auto model = new QStandardItemModel(this);
49  d->lstMaplist->setModel(model);
50  this->connect(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
51  SLOT(updateMapWarningVisibility()));
52  this->connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*,QWidget*)),
53  SLOT(onFocusChanged(QWidget*,QWidget*)));
54 
55  d->parentDialog = nullptr;
56 
57  d->lblWarning->setText(MapListPanel::tr("The current map isn't present on the map list. The game may misbehave."));
58  d->lblWarning->setPixmap(QPixmap(":/icons/exclamation.png"));
59  d->lblWarning->setWordWrap(true);
60 }
61 
62 MapListPanel::~MapListPanel()
63 {
64 }
65 
66 void MapListPanel::addMapFromEditBoxToList()
67 {
68  addMapToMaplist(d->leMapname->text().trimmed());
69 }
70 
71 void MapListPanel::addMapsFromLoadedWads()
72 {
73  auto mapListSelector = new MapListSelector(this);
74  mapListSelector->addPaths(d->parentDialog->wadPaths());
75  if (mapListSelector->exec() == QDialog::Accepted)
76  {
77  for (QString map : mapListSelector->selectedMaps())
78  {
79  addMapToMaplist(map);
80  }
81  }
82 }
83 
84 void MapListPanel::addMapToMaplist(const QString &map)
85 {
86  if (map.isEmpty())
87  return;
88  auto model = static_cast<QStandardItemModel *>(d->lstMaplist->model());
89  auto it = new QStandardItem(map);
90  it->setDragEnabled(true);
91  it->setDropEnabled(false);
92  model->appendRow(it);
93  updateMapWarningVisibility();
94 }
95 
96 void MapListPanel::onFocusChanged(QWidget *old, QWidget *now)
97 {
98  if (now == d->leMapname)
99  d->btnAddMapToMaplist->setDefault(true);
100  else if (old == d->leMapname)
101  d->btnAddMapToMaplist->setDefault(false);
102 }
103 
104 void MapListPanel::removeSelectedFromList()
105 {
106  const bool bSelectNextLowest = true;
107  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstMaplist, bSelectNextLowest);
108  updateMapWarningVisibility();
109 }
110 
111 void MapListPanel::fillInParams(GameCreateParams &params)
112 {
113  params.setMapList(CommonGUI::listViewStandardItemsToStringList(d->lstMaplist));
114  params.setRandomMapRotation(d->cbRandomMapRotation->isChecked());
115 }
116 
117 bool MapListPanel::hasMaps() const
118 {
119  return d->lstMaplist->model()->rowCount() > 0;
120 }
121 
122 bool MapListPanel::isMapOnList(const QString &mapName) const
123 {
124  for (const QString &candidate : CommonGUI::listViewStandardItemsToStringList(d->lstMaplist))
125  {
126  const QStringList tokens = candidate.trimmed().split(" ");
127  if (!tokens.isEmpty())
128  {
129  const QString mapFromList = tokens[0];
130  if (mapFromList.compare(mapName, Qt::CaseInsensitive) == 0)
131  return true;
132  }
133  }
134  return false;
135 }
136 
137 void MapListPanel::showEvent(QShowEvent *event)
138 {
139  Q_UNUSED(event);
140  updateMapWarningVisibility();
141 }
142 
143 void MapListPanel::updateMapWarningVisibility()
144 {
145  d->lblWarning->setVisible(hasMaps() && !isMapOnList(d->parentDialog->mapName()));
146 }
147 
148 void MapListPanel::loadConfig(Ini &config)
149 {
150  IniSection section = config.section("Rules");
151  QStringList stringList = section["maplist"].valueString().split(";");
152  QAbstractItemModel *model = d->lstMaplist->model();
153  model->removeRows(0, model->rowCount());
154  for (QString s : stringList)
155  {
156  addMapToMaplist(s);
157  }
158  d->cbRandomMapRotation->setChecked(section["randomMapRotation"]);
159 
160  // Timer triggers slot after config is fully loaded.
161  QTimer::singleShot(0, this, SLOT(updateMapWarningVisibility()));
162 }
163 
164 void MapListPanel::saveConfig(Ini &config)
165 {
166  IniSection section = config.section("Rules");
167  QStringList stringList = CommonGUI::listViewStandardItemsToStringList(d->lstMaplist);
168  section["maplist"] = stringList.join(";");
169  section["randomMapRotation"] = d->cbRandomMapRotation->isChecked();
170 }
171 
172 void MapListPanel::setCreateServerDialog(CreateServerDialog *dialog)
173 {
174  d->parentDialog = dialog;
175 }
176 
177 void MapListPanel::setupForEngine(const EnginePlugin *engine)
178 {
179  d->cbRandomMapRotation->setVisible(engine->data()->supportsRandomMapRotation);
180 }