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 "gui/commongui.h"
28 #include <QFileDialog>
29 #include <QStandardItemModel>
30 
31 DClass<WadsPicker> : public Ui::WadsPicker
32 {
33 };
34 
35 DPointered(WadsPicker)
36 
37 WadsPicker::WadsPicker(QWidget *parent)
38 {
39  Q_UNUSED(parent);
40  d->setupUi(this);
41  d->lstAdditionalFiles->setModel(new QStandardItemModel(this));
42 }
43 
44 WadsPicker::~WadsPicker()
45 {
46 }
47 
48 void WadsPicker::addWadPath(const QString &wadPath, bool required)
49 {
50  if (wadPath.isEmpty())
51  return;
52  QFileInfo fileInfo(wadPath);
53  if (!fileInfo.isFile())
54  return;
55 
56  auto model = static_cast<QStandardItemModel *>(d->lstAdditionalFiles->model());
57 
58  // Check if this path exists already, if so - do nothing.
59  for (int i = 0; i < model->rowCount(); ++i)
60  {
61  QStandardItem *item = model->item(i);
62  QString dir = item->text();
63  Qt::CaseSensitivity cs;
64 
65  #ifdef Q_OS_WIN32
66  cs = Qt::CaseInsensitive;
67  #else
68  cs = Qt::CaseSensitive;
69  #endif
70 
71  if (dir.compare(wadPath, cs) == 0)
72  return;
73  }
74 
75  auto it = new QStandardItem(wadPath);
76 
77  it->setDragEnabled(true);
78  it->setDropEnabled(false);
79  it->setToolTip(wadPath);
80  it->setCheckable(true);
81  it->setCheckState(required ? Qt::Checked : Qt::Unchecked);
82 
83  model->appendRow(it);
84 }
85 
86 void WadsPicker::browseAndAdd()
87 {
88  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
89  QStringList filesNames = QFileDialog::getOpenFileNames(this,
90  tr("Doomseeker - Add file(s)"), dialogDir);
91 
92  if (!filesNames.isEmpty())
93  {
94  // Remember the directory of the first file. This directory will be
95  // restored the next time this dialog is opened.
96  QFileInfo fi(filesNames[0]);
97  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
98 
99  for (const QString &strFile : filesNames)
100  {
101  addWadPath(strFile);
102  }
103  }
104 }
105 
106 QList<bool> WadsPicker::fileOptional() const
107 {
108  QList<bool> checked = CommonGUI::listViewStandardItemsToBoolList(d->lstAdditionalFiles);
109  // We check required WADs and want to return optional wads.
110  for (bool &i : checked)
111  i = !i;
112  return checked;
113 }
114 
115 QStringList WadsPicker::filePaths() const
116 {
117  return CommonGUI::listViewStandardItemsToStringList(d->lstAdditionalFiles);
118 }
119 
120 void WadsPicker::setFilePaths(const QStringList &paths, const QList<bool> &optionals)
121 {
122  removeAll();
123  for (int i = 0; i < paths.size(); ++i)
124  addWadPath(paths[i], i >= optionals.size() || !optionals[i]);
125 }
126 
127 void WadsPicker::removeAll()
128 {
129  auto pModel = (QStandardItemModel *)d->lstAdditionalFiles->model();
130  pModel->clear();
131 }
132 
133 void WadsPicker::removeSelected()
134 {
135  const bool bSelectNextLowest = true;
136  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstAdditionalFiles, bSelectNextLowest);
137 }