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 : ConfigurationBaseBox(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  header->setResizeMode(COL_PATH, QHeaderView::Stretch);
53  header->setResizeMode(COL_RECURSE, QHeaderView::ResizeToContents);
54 
55  connect(d->btnAddWadPath, SIGNAL( clicked() ), this, SLOT( btnAddWadPath_Click()) );
56  connect(d->btnRemoveWadPath, SIGNAL( clicked() ), this, SLOT( btnRemoveWadPath_Click()) );
57 }
58 
59 CFGFilePaths::~CFGFilePaths()
60 {
61 }
62 
63 void CFGFilePaths::addPath(const FileSearchPath& fileSearchPath)
64 {
65  if (fileSearchPath.isValid())
66  {
67  return;
68  }
69 
70  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
71 
72  if (!isPathAlreadyDefined(fileSearchPath.path()))
73  {
74  QStandardItem* path = new QStandardItem(fileSearchPath.path());
75  path->setData(fileSearchPath.path(), Qt::ToolTipRole);
76  QStandardItem* recurse = new QStandardItem();
77  recurse->setCheckable(true);
78  recurse->setCheckState(fileSearchPath.isRecursive() ? Qt::Checked : Qt::Unchecked);
79  recurse->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
80  QList<QStandardItem*> items;
81  items << path;
82  items << recurse;
83  model->appendRow(items);
84  d->lstIwadAndPwadPaths->resizeRowsToContents();
85  }
86 }
87 
88 void CFGFilePaths::btnAddWadPath_Click()
89 {
90  QString strDir = QFileDialog::getExistingDirectory(this, tr("Doomseeker - Add wad path"));
91  addPath(strDir);
92 }
93 
94 void CFGFilePaths::btnRemoveWadPath_Click()
95 {
96  QItemSelectionModel* selModel = d->lstIwadAndPwadPaths->selectionModel();
97  QModelIndexList indexList = selModel->selectedRows();
98  selModel->clear();
99 
100  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
101  QList<QStandardItem*> itemList;
102  for (int i = 0; i < indexList.count(); ++i)
103  {
104  itemList << model->itemFromIndex(indexList[i]);
105  }
106 
107  for (int i = 0; i < itemList.count(); ++i)
108  {
109  QModelIndex index = model->indexFromItem(itemList[i]);
110  model->removeRow(index.row());
111  }
112 }
113 
114 QIcon CFGFilePaths::icon() const
115 {
116  return QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon);
117 }
118 
119 bool CFGFilePaths::isPathAlreadyDefined(const QString& path)
120 {
121  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
122 
123  Qt::CaseSensitivity caseSensitivity;
124 
125  #ifdef Q_OS_WIN32
126  caseSensitivity = Qt::CaseInsensitive;
127  #else
128  caseSensitivity = Qt::CaseSensitive;
129  #endif
130 
131  for(int i = 0; i < model->rowCount(); ++i)
132  {
133  QStandardItem* item = model->item(i);
134  QString dir = item->text();
135 
136  if (dir.compare(path, caseSensitivity) == 0)
137  {
138  return true;
139  }
140  }
141 
142  return false;
143 }
144 
146 {
147  const QList<FileSearchPath>& wadPaths = gConfig.doomseeker.wadPaths;
148  for (int i = 0; i < wadPaths.count(); ++i)
149  {
150  addPath(wadPaths[i]);
151  }
152 
153  d->cbTellMeWhereAreMyWads->setChecked(gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn);
154 }
155 
157 {
158  QList<FileSearchPath> wadPaths;
159 
160  QStandardItemModel* model = static_cast<QStandardItemModel*>(d->lstIwadAndPwadPaths->model());
161  {
162  for(int i = 0; i < model->rowCount(); ++i)
163  {
164  QStandardItem* itemPath = model->item(i, COL_PATH);
165  QStandardItem* itemRecurse = model->item(i, COL_RECURSE);
166  FileSearchPath fileSearchPath(itemPath->text());
167  fileSearchPath.setRecursive(itemRecurse->checkState() == Qt::Checked);
168  wadPaths << fileSearchPath;
169  }
170  }
171 
172  gConfig.doomseeker.wadPaths = wadPaths;
173  gConfig.doomseeker.bTellMeWhereAreTheWADsWhenIHoverCursorOverWADSColumn = d->cbTellMeWhereAreMyWads->isChecked();
174 }
void saveSettings()
Reimplement this to write settings to config from widgets.
QIcon icon() const
Reimplement this to return a displayable icon for the ConfigurationBaseBox.
void readSettings()
Reimplement this to read settings from config into widgets.
Base class for configuration pages.