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