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