cfgwadalias.cpp
1 //------------------------------------------------------------------------------
2 // cfgwadalias.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 "cfgwadalias.h"
24 #include "ui_cfgwadalias.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "gui/commongui.h"
28 #include "pathfinder/filealias.h"
29 #include <QTimer>
30 
31 DClass<CFGWadAlias> : public Ui::CFGWadAlias
32 {
33 public:
34  enum Columns
35  {
36  ColWad,
37  ColAliases,
38  ColMatchType
39  };
40 
41  QString matchTypeHelp;
42  QTimer resizeTimer;
43 };
44 
45 DPointeredNoCopy(CFGWadAlias)
46 
47 CFGWadAlias::CFGWadAlias(QWidget *parent)
48 : ConfigPage(parent)
49 {
50  d->setupUi(this);
51 
52  d->resizeTimer.setSingleShot(true);
53  connect(&d->resizeTimer, SIGNAL(timeout()), d->table, SLOT(resizeRowsToContents()));
54 
55  QHeaderView *header = d->table->horizontalHeader();
56  header->resizeSection(PrivData<CFGWadAlias>::ColWad, 150);
57 #if QT_VERSION >= 0x050000
58  header->setSectionResizeMode(PrivData<CFGWadAlias>::ColAliases, QHeaderView::Stretch);
59 #else
60  header->setResizeMode(PrivData<CFGWadAlias>::ColAliases, QHeaderView::Stretch);
61 #endif
62  header->resizeSection(PrivData<CFGWadAlias>::ColMatchType, 110);
63 
64  d->table->sortByColumn(PrivData<CFGWadAlias>::ColWad, Qt::AscendingOrder);
65 
66  d->matchTypeHelp = CFGWadAlias::tr("Left-to-Right will use the alias files instead of "
67  "the main file but not vice-versa.");
68  d->matchTypeHelp += "\n\n";
69  d->matchTypeHelp += CFGWadAlias::tr("All Equal will treat all files as equal and try to "
70  "match them in any combination.");
71 }
72 
73 CFGWadAlias::~CFGWadAlias()
74 {
75 }
76 
77 void CFGWadAlias::addAliasToTable(const FileAlias &alias)
78 {
79  bool wasSortingEnabled = d->table->isSortingEnabled();
80  d->table->setSortingEnabled(false);
81 
82  int row = findRowWithWad(alias.name());
83  if (row < 0)
84  {
85  row = d->table->rowCount();
86  addNewEntry();
87  applyAliasToRow(row, alias);
88  }
89  else
90  {
91  // Merge.
92  FileAlias existingAlias = aliasFromRow(row);
93  existingAlias.addAliases(alias.aliases());
94  applyAliasToRow(row, existingAlias);
95  }
96 
97  d->table->setSortingEnabled(wasSortingEnabled);
98  resizeRowsToContents();
99 }
100 
101 void CFGWadAlias::addDefaults()
102 {
103  QList<FileAlias> aliases = FileAlias::standardWadAliases();
104  foreach (const FileAlias& alias, aliases)
105  {
106  addAliasToTable(alias);
107  }
108 }
109 
110 void CFGWadAlias::addNewEntry()
111 {
112  bool wasSortingEnabled = d->table->isSortingEnabled();
113  d->table->setSortingEnabled(false);
114 
115  int row = d->table->rowCount();
116  d->table->insertRow(row);
117  d->table->setItem(row, PrivData<CFGWadAlias>::ColWad, new QTableWidgetItem());
118  d->table->setItem(row, PrivData<CFGWadAlias>::ColAliases, new QTableWidgetItem());
119  mkMatchTypeComboBox(row);
120 
121  d->table->setSortingEnabled(wasSortingEnabled);
122  resizeRowsToContents();
123 }
124 
125 QList<FileAlias> CFGWadAlias::aliases() const
126 {
127  QList<FileAlias> aliases;
128  for (int row = 0; row < d->table->rowCount(); ++row)
129  {
130  FileAlias alias = aliasFromRow(row);
131  if (alias.isValid())
132  {
133  aliases << alias;
134  }
135  }
136  return FileAliasList::mergeDuplicates(aliases);
137 }
138 
139 FileAlias CFGWadAlias::aliasFromRow(int row) const
140 {
141  FileAlias alias;
142  alias.setName(d->table->item(row, PrivData<CFGWadAlias>::ColWad)->text().trimmed());
143  QStringList candidateAliases = d->table->item(row, PrivData<CFGWadAlias>::ColAliases)->text().split(";");
144  foreach (const QString &candidateAlias, candidateAliases)
145  {
146  if (!candidateAlias.trimmed().isEmpty())
147  {
148  alias.addAlias(candidateAlias.trimmed());
149  }
150  }
151  QComboBox *cboMatchType = qobject_cast<QComboBox*>(d->table->cellWidget(
153  alias.setMatchType(static_cast<FileAlias::MatchType>(
154  cboMatchType->itemData(cboMatchType->currentIndex()).toInt()));
155  return alias;
156 }
157 
158 void CFGWadAlias::applyAliasToRow(int row, const FileAlias &alias)
159 {
160  d->table->setItem(row, PrivData<CFGWadAlias>::ColWad, toolTipItem(alias.name()));
161  d->table->setItem(row, PrivData<CFGWadAlias>::ColAliases, toolTipItem(alias.aliases().join("; ")));
162 
163  QComboBox *cboMatchType = qobject_cast<QComboBox*>(
164  d->table->cellWidget(row, PrivData<CFGWadAlias>::ColMatchType));
165  int matchTypeIdx = qMax(0, cboMatchType->findData(alias.matchType()));
166  cboMatchType->setCurrentIndex(matchTypeIdx);
167 }
168 
169 int CFGWadAlias::findRowWithWad(const QString &wadName)
170 {
171  for (int row = 0; row < d->table->rowCount(); ++row)
172  {
173  if (d->table->item(row, PrivData<CFGWadAlias>::ColWad)->text().trimmed().compare(
174  wadName.trimmed(), Qt::CaseInsensitive) == 0)
175  {
176  return row;
177  }
178  }
179  return -1;
180 }
181 
182 QIcon CFGWadAlias::icon() const
183 {
184  return QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon);
185 }
186 
187 QComboBox *CFGWadAlias::mkMatchTypeComboBox(int row)
188 {
189  QComboBox *cboMatchType = new QComboBox();
190  cboMatchType->setToolTip(d->matchTypeHelp);
191  cboMatchType->addItem(tr("Left-to-Right"), FileAlias::LeftToRight);
192  cboMatchType->addItem(tr("All Equal"), FileAlias::AllEqual);
193  d->table->setCellWidget(row, PrivData<CFGWadAlias>::ColMatchType, cboMatchType);
194  return cboMatchType;
195 }
196 
198 {
199  bool wasSortingEnabled = d->table->isSortingEnabled();
200  d->table->setSortingEnabled(false);
201 
202  while (d->table->rowCount() > 0)
203  {
204  d->table->removeRow(0);
205  }
206  // Aliases from configuration are guaranteed to be unique.
207  QList<FileAlias> aliases = gConfig.doomseeker.wadAliases();
208  foreach (const FileAlias &alias, aliases)
209  {
210  if (alias.isValid())
211  {
212  addAliasToTable(alias);
213  }
214  }
215 
216  d->table->setSortingEnabled(wasSortingEnabled);
217 }
218 
219 void CFGWadAlias::removeSelected()
220 {
221  CommonGUI::removeSelectedRowsFromQTableWidget(d->table);
222 }
223 
224 void CFGWadAlias::resizeRowsToContents()
225 {
226  d->resizeTimer.start(0);
227 }
228 
230 {
231  gConfig.doomseeker.setWadAliases(aliases());
232 }
233 
234 void CFGWadAlias::showEvent(QShowEvent *event)
235 {
236  resizeRowsToContents();
237 }
238 
239 QTableWidgetItem *CFGWadAlias::toolTipItem(const QString &contents)
240 {
241  QTableWidgetItem *item = new QTableWidgetItem(contents);
242  item->setToolTip(contents);
243  return item;
244 }
bool isValid() const
Valid FileAlias has a name and at least one alias.
Definition: filealias.cpp:145
void addAliases(const QStringList &val)
Will ensure unique values.
Definition: filealias.cpp:46
static QList< FileAlias > standardWadAliases()
Standard/default aliases for configuration init.
Definition: filealias.cpp:170
Definition: dptr.h:31
void readSettings()
Reimplement this to read settings from config into widgets.
void saveSettings()
Reimplement this to write settings to config from widgets.
void addAlias(const QString &val)
Will ensure unique values.
Definition: filealias.cpp:38
QIcon icon() const
Reimplement this to return a displayable icon for the ConfigPage.
Base class for configuration pages.
Definition: configpage.h:44