cfgwadseekergeneral.cpp
1 //------------------------------------------------------------------------------
2 // cfgwadseekergeneral.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) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgwadseekergeneral.h"
24 #include "ui_cfgwadseekergeneral.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "pathfinder/filesearchpath.h"
28 #include <QCompleter>
29 #include <QDebug>
30 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
31 #include <QFileSystemModel>
32 #else
33 #include <QDirModel>
34 #endif
35 #include <QMessageBox>
36 
37 DClass<CFGWadseekerGeneral> : public Ui::CFGWadseekerGeneral
38 {
39 public:
40  bool completerActive;
41 
42  QString targetDirectory() const
43  {
44  return cbTargetDirectory->currentText().trimmed();
45  }
46 };
47 
48 DPointered(CFGWadseekerGeneral)
49 
51  : ConfigPage(parent)
52 {
53  d->setupUi(this);
54  d->completerActive = false;
55 
56  // Settings defined in this widget are ATM unused.
57  d->lblDirectoryWarning->setPixmap(QPixmap(":/icons/exclamation_16.png"));
58  d->lblDirectoryWarning->hide();
59  d->lblDirectoryWarning->setWordWrap(true);
60 
61  this->connect(d->cbTargetDirectory, SIGNAL(editTextChanged(QString)),
62  SIGNAL(validationRequested()));
63 }
64 
65 CFGWadseekerGeneral::~CFGWadseekerGeneral()
66 {
67 }
68 
69 void CFGWadseekerGeneral::activateCompleter()
70 {
71  // Lazy activation of completer prevents floppy drive clicking
72  // on QComboBox::addItem() if you're on Windows and if you
73  // actually have a floppy drive.
74  //
75  // The floppy drive will still click *one time* when user tries to type
76  // anything, but at least it won't click on readSettings() anymore
77  // (so, essentially, when user opens config box).
78  if (!d->completerActive)
79  {
80 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
81  // NOTE: Although QFileSystemModel is available since Qt 4, there were
82  // some issues with it and QCompleter until at least Qt 5.12 (i.e.
83  // QTBUG-38014). We'll conservatively use QFileSystemModel on Qt 6 only.
84  auto completer = new QCompleter(this);
85  auto model = new QFileSystemModel(completer);
86  model->setRootPath(d->targetDirectory());
87  completer->setModel(model);
88  d->cbTargetDirectory->setCompleter(completer);
89 #else
90  d->cbTargetDirectory->setCompleter(new QCompleter(new QDirModel()));
91 #endif
92  d->completerActive = true;
93  }
94 }
95 
96 void CFGWadseekerGeneral::fillTargetDirectoryComboBox()
97 {
98  d->cbTargetDirectory->clear();
99  d->cbTargetDirectory->addItems(gConfig.doomseeker.wadPathsOnly());
100 }
101 
103 {
104  fillTargetDirectoryComboBox();
105 
106  d->cbTargetDirectory->setEditText(gConfig.wadseeker.targetDirectory);
107  d->spinMaxConcurrentSiteSeeks->setValue(gConfig.wadseeker.maxConcurrentSiteDownloads);
108  d->spinMaxConcurrentWadDownloads->setValue(gConfig.wadseeker.maxConcurrentWadDownloads);
109 }
110 
112 {
113  gConfig.wadseeker.targetDirectory = d->targetDirectory();
114  gConfig.wadseeker.maxConcurrentSiteDownloads = d->spinMaxConcurrentSiteSeeks->value();
115  gConfig.wadseeker.maxConcurrentWadDownloads = d->spinMaxConcurrentWadDownloads->value();
116 }
117 
118 void CFGWadseekerGeneral::showEvent(QShowEvent *event)
119 {
120  activateCompleter();
121  ConfigPage::showEvent(event);
122 }
123 
125 {
126  QString error;
127 
128  QFileInfo targetDirectory(d->targetDirectory());
129  if (error.isEmpty() && d->targetDirectory().isEmpty())
130  error = tr("No path specified.");
131 
132  if (error.isEmpty() && !targetDirectory.exists())
133  error = tr("This path doesn't exist.");
134 
135  if (error.isEmpty() && !targetDirectory.isDir())
136  error = tr("This is not a directory.");
137 
138  if (error.isEmpty() && !targetDirectory.isWritable())
139  error = tr("This directory cannot be written to.");
140 
141  d->lblDirectoryWarning->setVisible(!error.isEmpty());
142  d->lblDirectoryWarning->setText(error);
143  return error.isEmpty() ? VALIDATION_OK : VALIDATION_ERROR;
144 }