wadspicker.cpp
1 //------------------------------------------------------------------------------
2 // wadspicker.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "wadspicker.h"
24 #include "ui_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  d->setupUi(this);
40  d->lstAdditionalFiles->setModel(new QStandardItemModel(this));
41 }
42 
43 WadsPicker::~WadsPicker()
44 {
45 }
46 
47 void WadsPicker::addWadPath(const QString &wadPath, bool required)
48 {
49  if (wadPath.isEmpty())
50  {
51  return;
52  }
53  QFileInfo fileInfo(wadPath);
54  if (!fileInfo.isFile())
55  {
56  return;
57  }
58 
59  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstAdditionalFiles->model());
60 
61  // Check if this path exists already, if so - do nothing.
62  for(int i = 0; i < model->rowCount(); ++i)
63  {
64  QStandardItem* item = model->item(i);
65  QString dir = item->text();
66  Qt::CaseSensitivity cs;
67 
68  #ifdef Q_OS_WIN32
69  cs = Qt::CaseInsensitive;
70  #else
71  cs = Qt::CaseSensitive;
72  #endif
73 
74  if (dir.compare(wadPath, cs) == 0)
75  {
76  return;
77  }
78  }
79 
80  QStandardItem* it = new QStandardItem(wadPath);
81 
82  it->setDragEnabled(true);
83  it->setDropEnabled(false);
84  it->setToolTip(wadPath);
85  it->setCheckable(true);
86  it->setCheckState(required ? Qt::Checked : Qt::Unchecked);
87 
88  model->appendRow(it);
89 }
90 
91 void WadsPicker::browseAndAdd()
92 {
93  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
94  QStringList filesNames = QFileDialog::getOpenFileNames(this,
95  tr("Doomseeker - Add file(s)"), dialogDir);
96 
97  if (!filesNames.isEmpty())
98  {
99  // Remember the directory of the first file. This directory will be
100  // restored the next time this dialog is opened.
101  QFileInfo fi(filesNames[0]);
102  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
103 
104  foreach (const QString& strFile, filesNames)
105  {
106  addWadPath(strFile);
107  }
108  }
109 }
110 
111 QList<bool> WadsPicker::fileOptional() const
112 {
113  QList<bool> checked = CommonGUI::listViewStandardItemsToBoolList(d->lstAdditionalFiles);
114  // We check required WADs and want to return optional wads.
115  for(int i = 0;i < checked.size();++i)
116  checked[i] = !checked[i];
117  return checked;
118 }
119 
120 QStringList WadsPicker::filePaths() const
121 {
122  return CommonGUI::listViewStandardItemsToStringList(d->lstAdditionalFiles);
123 }
124 
125 void WadsPicker::setFilePaths(const QStringList &paths, const QList<bool> &optionals)
126 {
127  removeAll();
128  for(int i = 0;i < paths.size();++i)
129  {
130  addWadPath(paths[i], i >= optionals.size() || !optionals[i]);
131  }
132 }
133 
134 void WadsPicker::removeAll()
135 {
136  QStandardItemModel* pModel = (QStandardItemModel*)d->lstAdditionalFiles->model();
137  pModel->clear();
138 }
139 
140 void WadsPicker::removeSelected()
141 {
142  const bool bSelectNextLowest = true;
143  CommonGUI::removeSelectedRowsFromStandardItemView(d->lstAdditionalFiles, bSelectNextLowest);
144 }
static QStringList listViewStandardItemsToStringList(QListView *listview)
Reads items from a QListView that uses QStandardItemModel and puts texts of these items into a list o...
Definition: commongui.cpp:51
static void removeSelectedRowsFromStandardItemView(QAbstractItemView *view, bool bSelectNextItem=false)
Removes all selected rows from a QAbstractItemView.
Definition: commongui.cpp:84
static QList< bool > listViewStandardItemsToBoolList(QListView *listview)
Definition: commongui.cpp:38