maplistselector.cpp
1 //------------------------------------------------------------------------------
2 // maplistselector.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) 2019 Pol Marcet Sardà <polmarcetsarda@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "maplistselector.h"
24 
25 #include "commongui.h"
26 #include "modreader.h"
27 
28 #include <QFileInfo>
29 #include <QMenu>
30 #include <QScopedPointer>
31 #include <QSharedPointer>
32 #include <QTimer>
33 
34 MapEntry::MapEntry(const QString &name, const QString &originalFile, const bool &isIwad)
35 {
36  this->name = name;
37  this->originalFile = originalFile;
38  this->isIwad = isIwad;
39 }
40 
41 DClass<MapListSelector> : public Ui::MapListSelector
42 {
43 public:
44  QStringList paths;
45  QList<MapEntry> mapList;
46  QScopedPointer<QTimer> checkWadTimer;
47 };
48 
49 DPointeredNoCopy(MapListSelector)
50 
51 MapListSelector::MapListSelector(QWidget *parent) : QDialog(parent)
52 {
53  d->checkWadTimer.reset(new QTimer);
54 
55  d->setupUi(this);
56 
57  connect(d->checkWadTimer.data(), SIGNAL(timeout()), this, SLOT(performCheckStep()));
58 }
59 
60 MapListSelector::~MapListSelector()
61 {
62 }
63 
64 void MapListSelector::addPaths(const QStringList &paths)
65 {
66  d->paths << paths;
67  d->progressBar->setMaximum(d->progressBar->value() + paths.size());
68  setIfFinishedStateIsEnabled(false);
69  d->checkWadTimer->start(0);
70 }
71 
72 const QStringList MapListSelector::selectedMaps()
73 {
74  QStringList tempSelectedMapList;
75  for (int i = 0; i < d->listMapsWidget->count(); ++i)
76  {
77  QListWidgetItem *item = d->listMapsWidget->item(i);
78  if (item->checkState() == Qt::Checked)
79  tempSelectedMapList << item->text();
80  }
81  return tempSelectedMapList;
82 }
83 
84 void MapListSelector::performCheckStep()
85 {
86  if (d->paths.isEmpty())
87  {
88  d->checkWadTimer->stop();
89  setMapsOfCheckableList(d->mapList);
90  setIfFinishedStateIsEnabled(true);
91  return;
92  }
93  QString path = d->paths.takeFirst();
94  QSharedPointer<ModReader> modReader = ModReader::create(path);
95  if (!modReader.isNull() && modReader->load())
96  {
97  for (const QString &tempMap : modReader->getAllMaps())
98  {
99  addEntryToMapList(MapEntry(tempMap, QFile(path).fileName(), modReader->isIwad()));
100  }
101  }
102  d->progressBar->setValue(d->progressBar->value() + 1);
103 }
104 
105 void MapListSelector::addEntryToMapList(const MapEntry &newEntry)
106 {
107  bool equalEntryFound = false;
108  for (int i = 0; i < d->mapList.size(); ++i)
109  {
110  if (d->mapList[i].name == newEntry.name)
111  {
112  d->mapList.replace(i, newEntry);
113  equalEntryFound = true;
114  }
115  }
116  if (!equalEntryFound)
117  d->mapList.append(newEntry);
118 }
119 
120 void MapListSelector::setMapsOfCheckableList(const QList<MapEntry> &list)
121 {
122  d->listMapsWidget->clear();
123  for (const MapEntry &mapEntry : list)
124  {
125  auto item = new QListWidgetItem(mapEntry.name, d->listMapsWidget);
126  item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
127  item->setCheckState(Qt::Checked);
128  if (mapEntry.isIwad)
129  {
130  item->setBackground(Qt::lightGray);
131  item->setForeground(Qt::black);
132  }
133  item->setToolTip("File: " + mapEntry.originalFile);
134  }
135 }
136 
137 void MapListSelector::setIfFinishedStateIsEnabled(const bool &state)
138 {
139  d->btnAdd->setEnabled(state);
140  d->checkAll->setEnabled(state);
141  d->descriptionSelect->setVisible(state);
142  d->listMapsWidget->setVisible(state);
143  d->descriptionLoading->setVisible(!state);
144  d->progressBar->setVisible(!state);
145  adjustSize();
146 }
147 
148 void MapListSelector::accept()
149 {
150  done(Accepted);
151 }
152 
153 void MapListSelector::reject()
154 {
155  d->listMapsWidget->clear();
156  d->checkWadTimer->stop();
157  done(Rejected);
158 }
159 
160 void MapListSelector::selectAll()
161 {
162  Qt::CheckState checkState = d->checkAll->checkState();
163  for (int i = 0; i < d->listMapsWidget->count(); ++i)
164  d->listMapsWidget->item(i)->setCheckState(checkState);
165 }
166 
167 void MapListSelector::listItemChanged()
168 {
169  QStringList checkedMaps = selectedMaps();
170  if (checkedMaps.size() > 0 && checkedMaps.size() < d->listMapsWidget->count())
171  d->checkAll->setCheckState(Qt::PartiallyChecked);
172  else
173  {
174  d->checkAll->setCheckState((checkedMaps.size() == 0) ?
175  Qt::Unchecked : Qt::Checked);
176  }
177 }
178 
179 void MapListSelector::showContextMenu(const QPoint &pos)
180 {
181  QPoint globalPos = d->listMapsWidget->mapToGlobal(pos);
182 
183  QMenu myMenu;
184  myMenu.addAction(tr("Invert selection"), this, SLOT(invertSelection()));
185  myMenu.exec(globalPos);
186 }
187 
188 void MapListSelector::invertSelection()
189 {
190  for (int i = 0; i < d->listMapsWidget->count(); ++i)
191  {
192  QListWidgetItem *item = d->listMapsWidget->item(i);
193  if (item->isSelected())
194  item->setCheckState(item->checkState() == Qt::Unchecked ?
195  Qt::Checked : Qt::Unchecked);
196  }
197 }
Stores the name of the map and the mod where it came from.
Prompts the user to select a list of maps to insert.
const QStringList selectedMaps()
returns the list of maps that were checked.
void addPaths(const QStringList &paths)
adds the paths to check for maps. Please note that it will immediately start checking them...