cfgfilepaths.cpp
1 //------------------------------------------------------------------------------
2 // cfgfilepaths.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) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgfilepaths.h"
24 #include "ui_cfgfilepaths.h"
25 #include "configuration/doomseekerconfig.h"
26 #include "pathfinder/filesearchpath.h"
27 #include <QFileDialog>
28 #include <QStandardItem>
29 
30 const int COL_PATH = 0;
31 const int COL_RECURSE = 1;
32 
33 DClass<CFGFilePaths> : public Ui::CFGFilePaths
34 {
35 };
36 
37 DPointered(CFGFilePaths)
38 
39 CFGFilePaths::CFGFilePaths(QWidget* parent)
40 : ConfigPage(parent)
41 {
42  d->setupUi(this);
43 
44  QStandardItemModel* model = new QStandardItemModel(this);
45  d->lstIwadAndPwadPaths->setModel(model);
46 
47  QStringList labels;
48  labels << tr("Path") << tr("Recurse");
49  model->setHorizontalHeaderLabels(labels);
50 
51  QHeaderView* header = d->lstIwadAndPwadPaths->horizontalHeader();
52 #if QT_VERSION >= 0x050000
53  header->setSectionResizeMode(COL_PATH, QHeaderView::Stretch);
54  header->setSectionResizeMode(COL_RECURSE, QHeaderView::ResizeToContents);
55 #else
56  header->setResizeMode(COL_PATH, QHeaderView::Stretch);
57  header->setResizeMode(COL_RECURSE, QHeaderView::ResizeToContents);
58 #endif
59 
60  connect(d->btnAddWadPath, SIGNAL( clicked() ), this, SLOT( btnAddWadPath_Click()) );
61  connect(d->btnRemoveWadPath, SIGNAL( clicked() ), this, SLOT( btnRemoveWadPath_Click()) );
62  this->connect(d->lstIwadAndPwadPaths->itemDelegate(),
63  SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)),
64  SIGNAL(validationRequested()));
65 }
66 
67 CFGFilePaths::~CFGFilePaths()
68 {
69 }
70 
71 void CFGFilePaths::addPath(const FileSearchPath& fileSearchPath)
72 {
73  if (fileSearchPath.isValid())
74  {
75  return;
76  }
77 
78  QStandardItemModel *model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
79 
80  if (!isPathAlreadyDefined(fileSearchPath.path()))
81  {
82  QStandardItem *path = new QStandardItem(fileSearchPath.path());
83  path->setData(fileSearchPath.path(), Qt::ToolTipRole);
84  QStandardItem *recurse = new QStandardItem();
85  recurse->setCheckable(true);
86  recurse->setCheckState(fileSearchPath.isRecursive() ? Qt::Checked : Qt::Unchecked);
87  recurse->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
88  QList<QStandardItem*> items;
89  items << path;
90  items << recurse;
91  model->appendRow(items);
92  d->lstIwadAndPwadPaths->resizeRowsToContents();
93  }
94 }
95 
96 void CFGFilePaths::btnAddWadPath_Click()
97 {
98  QString strDir = QFileDialog::getExistingDirectory(this, tr("Doomseeker - Add wad path"));
99  addPath(strDir);
100  emit validationRequested();
101 }
102 
103 void CFGFilePaths::btnRemoveWadPath_Click()
104 {
105  QItemSelectionModel* selModel = d->lstIwadAndPwadPaths->selectionModel();
106  QModelIndexList indexList = selModel->selectedRows();
107  selModel->clear();
108 
109  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
110  QList<QStandardItem*> itemList;
111  for (int i = 0; i < indexList.count(); ++i)
112  {
113  itemList << model->itemFromIndex(indexList[i]);
114  }
115 
116  for (int i = 0; i < itemList.count(); ++i)
117  {
118  QModelIndex index = model->indexFromItem(itemList[i]);
119  model->removeRow(index.row());
120  }
121  emit validationRequested();
122 }
123 
124 QIcon CFGFilePaths::icon() const
125 {
126  return QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon);
127 }
128 
129 bool CFGFilePaths::isPathAlreadyDefined(const QString& path)
130 {
131  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
132 
133  Qt::CaseSensitivity caseSensitivity;
134 
135  #ifdef Q_OS_WIN32
136  caseSensitivity = Qt::CaseInsensitive;
137  #else
138  caseSensitivity = Qt::CaseSensitive;
139  #endif
140 
141  for(int i = 0; i < model->rowCount(); ++i)
142  {
143  QStandardItem* item = model->item(i);
144  QString dir = item->text();
145 
146  if (dir.compare(path, caseSensitivity) == 0)
147  {
148  return true;
149  }
150  }
151 
152  return false;
153 }
154 
156 {
157  const QList<FileSearchPath>& wadPaths = gConfig.doomseeker.wadPaths;
158  for (int i = 0; i < wadPaths.count(); ++i)
159  {
160  addPath(wadPaths[i]);
161  }
162 
163  d->cbTellMeWhereAreMyWads->setChecked(gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn);
164 }
165 
167 {
168  QList<FileSearchPath> wadPaths;
169 
170  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
171  for(int i = 0; i < model->rowCount(); ++i)
172  {
173  QStandardItem* itemPath = model->item(i, COL_PATH);
174  QStandardItem* itemRecurse = model->item(i, COL_RECURSE);
175  FileSearchPath fileSearchPath(itemPath->text());
176  fileSearchPath.setRecursive(itemRecurse->checkState() == Qt::Checked);
177  wadPaths << fileSearchPath;
178  }
179 
180  gConfig.doomseeker.wadPaths = wadPaths;
181  gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn = d->cbTellMeWhereAreMyWads->isChecked();
182 }
183 
185 {
186  bool allPathsValid = true;
187  QStandardItemModel *model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
188  for (int i = 0; i < model->rowCount(); ++i)
189  {
190  QStandardItem *itemPath = model->item(i, COL_PATH);
191 
192  QString validationError = validatePath(itemPath->text());
193  bool valid = validationError.isEmpty();
194  allPathsValid = allPathsValid && valid;
195 
196  itemPath->setIcon(valid ? QIcon() : QIcon(":/icons/exclamation_16.png"));
197  itemPath->setToolTip(validationError);
198  }
199  return allPathsValid ? VALIDATION_OK : VALIDATION_ERROR;
200 }
201 
202 QString CFGFilePaths::validatePath(const QString &path) const
203 {
204  if (path.trimmed().isEmpty())
205  {
206  return tr("No path specified.");
207  }
208 
209  QFileInfo fileInfo(path.trimmed());
210  if (!fileInfo.exists())
211  {
212  return tr("Path doesn't exist.");
213  }
214 
215  if (!fileInfo.isDir())
216  {
217  return tr("Path is not a directory.");
218  }
219 
220  return QString();
221 }
Validation validate()
Validate settings on this page.
Validation
Result of validate()
Definition: configpage.h:49
void saveSettings()
Reimplement this to write settings to config from widgets.
void validationRequested()
Request that the page should be (re-)validated.
QIcon icon() const
Reimplement this to return a displayable icon for the ConfigPage.
Validation detected no problems.
Definition: configpage.h:52
void readSettings()
Reimplement this to read settings from config into widgets.
Validation detected at least one problem.
Definition: configpage.h:54
Base class for configuration pages.
Definition: configpage.h:43