wadspicker.cpp
1 //------------------------------------------------------------------------------
2 // wadspicker.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 "ui_wadspicker.h"
24 #include "wadspicker.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "datapaths.h"
28 #include "gui/commongui.h"
29 #include "gui/icons.h"
30 #include "templatedpathresolver.h"
31 #include <QApplication>
32 #include <QKeyEvent>
33 #include <QFileDialog>
34 #include <QStandardItemModel>
35 #include <QStyle>
36 
37 DClass<WadsPicker> : public Ui::WadsPicker
38 {
39 };
40 
41 DPointered(WadsPicker)
42 
43 WadsPicker::WadsPicker(QWidget *parent)
44 {
45  Q_UNUSED(parent);
46  d->setupUi(this);
47  d->lstAdditionalFiles->setModel(new QStandardItemModel(this));
48  d->lstAdditionalFiles->installEventFilter(this);
49 
50  d->btnBrowsePwad->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon));
51  d->btnClearPwadList->setIcon(Icons::clear());
52 }
53 
54 WadsPicker::~WadsPicker()
55 {
56 }
57 
58 void WadsPicker::addEmptyPath()
59 {
60  addPathToTable(QString(), true);
61 }
62 
63 void WadsPicker::addWadPath(const QString &wadPath, bool required)
64 {
65  if (wadPath.isEmpty())
66  return;
67  QFileInfo fileInfo(gDoomseekerTemplatedPathResolver().resolve(wadPath));
68  if (!fileInfo.isFile())
69  return;
70 
71  auto model = static_cast<QStandardItemModel *>(d->lstAdditionalFiles->model());
72 
73  // Check if this path exists already, if so - do nothing.
74  for (int i = 0; i < model->rowCount(); ++i)
75  {
76  QStandardItem *item = model->item(i);
77  QString existing = item->text();
78  Qt::CaseSensitivity cs;
79 
80  #ifdef Q_OS_WIN32
81  cs = Qt::CaseInsensitive;
82  #else
83  cs = Qt::CaseSensitive;
84  #endif
85 
86  if (existing.compare(wadPath, cs) == 0)
87  return;
88  }
89  addPathToTable(wadPath, required);
90 }
91 
92 void WadsPicker::addPathToTable(const QString &path, bool required)
93 {
94  auto it = new QStandardItem(path);
95 
96  it->setDragEnabled(true);
97  it->setDropEnabled(false);
98  it->setToolTip(path);
99  it->setCheckable(true);
100  it->setCheckState(required ? Qt::Checked : Qt::Unchecked);
101 
102  auto model = static_cast<QStandardItemModel *>(d->lstAdditionalFiles->model());
103  model->appendRow(it);
104 }
105 
106 void WadsPicker::browseAndAdd()
107 {
108  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
109  QStringList filesNames = QFileDialog::getOpenFileNames(this,
110  tr("Doomseeker - Add file(s)"), dialogDir);
111 
112  if (!filesNames.isEmpty())
113  {
114  // Remember the directory of the first file. This directory will be
115  // restored the next time this dialog is opened.
116  QFileInfo fi(filesNames[0]);
117  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
118 
119  for (const QString &strFile : filesNames)
120  {
121  addWadPath(gDefaultDataPaths->portablizePath(strFile));
122  }
123  }
124 }
125 
126 bool WadsPicker::eventFilter(QObject *obj, QEvent *event)
127 {
128  if (obj == d->lstAdditionalFiles)
129  {
130  if (event->type() == QEvent::KeyPress)
131  {
132  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
133  switch (keyEvent->key())
134  {
135  case Qt::Key_Delete:
136  case Qt::Key_Minus:
137  removeSelected();
138  return true;
139  case Qt::Key_Plus:
140  addEmptyPath();
141  return true;
142  default:
143  // no-op
144  break;
145  }
146  }
147  }
148  return false;
149 }
150 
151 QList<bool> WadsPicker::fileOptional() const
152 {
153  QList<bool> checked = CommonGUI::listViewStandardItemsToBoolList(d->lstAdditionalFiles);
154  // We check required WADs and want to return optional wads.
155  for (bool &i : checked)
156  i = !i;
157  return checked;
158 }
159 
160 QStringList WadsPicker::filePaths() const
161 {
162  return CommonGUI::listViewStandardItemsToStringList(d->lstAdditionalFiles);
163 }
164 
165 void WadsPicker::setFilePaths(const QStringList &paths, const QList<bool> &optionals)
166 {
167  removeAll();
168  for (int i = 0; i < paths.size(); ++i)
169  addWadPath(paths[i], i >= optionals.size() || !optionals[i]);
170 }
171 
172 void WadsPicker::removeAll()
173 {
174  auto pModel = (QStandardItemModel *)d->lstAdditionalFiles->model();
175  pModel->clear();
176 }
177 
178 void WadsPicker::removeSelected()
179 {
180  const bool bSelectNextLowest = true;
181  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstAdditionalFiles, bSelectNextLowest);
182 }