filepickwidget.cpp
1 //------------------------------------------------------------------------------
2 // filepickwidget.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) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "filepickwidget.h"
24 
25 #include "serverapi/gamefile.h"
26 #include "ini/inisection.h"
27 #include "ini/inivariable.h"
28 #include "pathfinder/pathfind.h"
29 #include "filefilter.h"
30 #include "ui_filepickwidget.h"
31 #include <QFileDialog>
32 #include <QString>
33 
34 DClass<FilePickWidget> : public Ui::FilePickWidget
35 {
36 public:
37  bool changed;
38  GameFile file;
39  QSharedPointer< ::FilePickWidget::NeighbourStrategy> neighbourStrategy;
40 };
41 DPointered(FilePickWidget)
42 
43 FilePickWidget::FilePickWidget(QWidget *parent)
44 : QWidget(parent)
45 {
46  d->setupUi(this);
47  d->changed = false;
48  d->neighbourStrategy = QSharedPointer<NeighbourStrategy>(new NeighbourStrategy);
49  d->lblWarning->hide();
50  this->connect(d->lePath, SIGNAL(editingFinished()), SLOT(emitPathChangedIfChanged()));
51  this->connect(d->lePath, SIGNAL(textEdited(QString)), SLOT(trackEdit()));
52 }
53 
54 FilePickWidget::~FilePickWidget()
55 {
56 }
57 
58 void FilePickWidget::browsePath()
59 {
60  QString filepath = QFileDialog::getOpenFileName(this,
61  tr("Doomseeker - choose executable file"),
62  d->lePath->text(), FileFilter::executableFilesFilter());
63  if (!filepath.isEmpty())
64  {
65  d->lePath->setText(filepath);
66  emit pathChanged();
67  }
68 }
69 
70 void FilePickWidget::findPath()
71 {
72  QFileInfo currentFile = path();
73  if (currentFile.isFile() && d->file.isSameFile(currentFile.fileName()))
74  {
75  return;
76  }
77  QString path = PathFind::findGameFile(d->neighbourStrategy->neighbours(), d->file);
78  if (!path.isEmpty())
79  {
80  d->lePath->setText(path);
81  emit pathChanged();
82  }
83  else
84  {
85  emit findFailed();
86  }
87 }
88 
89 bool FilePickWidget::isEmpty() const
90 {
91  return path().trimmed().isEmpty();
92 }
93 
94 void FilePickWidget::setFile(const GameFile &file)
95 {
96  d->file = file;
97  d->lblTitle->setText(tr("Path to %1 executable:").arg(file.niceName()));
98  d->btnFind->setVisible(canSearch());
99 }
100 
101 void FilePickWidget::setNeighbourStrategy(QSharedPointer<NeighbourStrategy> strategy)
102 {
103  d->neighbourStrategy = strategy;
104 }
105 
106 QString FilePickWidget::path() const
107 {
108  return d->lePath->text().trimmed();
109 }
110 
111 bool FilePickWidget::canSearch() const
112 {
113  return !d->file.fileName().isEmpty();
114 }
115 
116 void FilePickWidget::emitPathChangedIfChanged()
117 {
118  if (d->changed)
119  {
120  d->changed = false;
121  emit pathChanged();
122  }
123 }
124 
125 void FilePickWidget::trackEdit()
126 {
127  d->changed = true;
128 }
129 
130 void FilePickWidget::load(const IniSection &cfg)
131 {
132  d->lePath->setText(cfg[d->file.configName()].valueString());
133 }
134 
135 void FilePickWidget::save(IniSection &cfg)
136 {
137  cfg[d->file.configName()].setValue(d->lePath->text());
138 }
139 
140 bool FilePickWidget::validate()
141 {
142  QString error;
143 
144  QFileInfo fileInfo = path();
145  if (!path().isEmpty())
146  {
147  if (error.isEmpty() && !fileInfo.exists())
148  {
149  error = tr("File doesn't exist.");
150  }
151 
152  if (error.isEmpty() && fileInfo.isDir())
153  {
154  error = tr("This is a directory.");
155  }
156  }
157 
158  d->lblWarning->setVisible(!error.isEmpty());
159  d->lblWarning->setToolTip(error);
160  return error.isEmpty();
161 }
const QString & niceName() const
Descriptive name, ie. "client executable", "server executable", etc.
Definition: gamefile.cpp:101
void setValue(const QString &key, const QVariant &value)
Sets a variable directly. Omits the IniVariable system.
Definition: inisection.cpp:154
INI section representation.
Definition: inisection.h:40
Game file definition allows to browse this file in configuration box.
Definition: gamefile.h:72