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  this->connect(d->lePath, SIGNAL(editingFinished()), SLOT(emitPathChangedIfChanged()));
55  this->connect(d->lePath, SIGNAL(textEdited(QString)), SLOT(trackEdit()));
56 }
57 
58 FilePickWidget::~FilePickWidget()
59 {
60 }
61 
62 void FilePickWidget::browsePath()
63 {
64  QString filepath = QFileDialog::getOpenFileName(this,
65  tr("Doomseeker - choose executable file"),
66  d->lePath->text(), FileFilter::executableFilesFilter());
67  if (!filepath.isEmpty())
68  {
69  d->lePath->setText(gDefaultDataPaths->portablizePath(filepath));
70  emit pathChanged();
71  }
72 }
73 
74 void FilePickWidget::findPath()
75 {
76  QFileInfo currentFile(gDoomseekerTemplatedPathResolver().resolve(path()));
77  if (currentFile.isFile() && d->file.isSameFile(currentFile.fileName()))
78  return;
79  QString path = PathFind::findGameFile(d->neighbourStrategy->neighbours(), d->file);
80  if (!path.isEmpty())
81  {
82  d->lePath->setText(gDefaultDataPaths->portablizePath(path));
83  emit pathChanged();
84  }
85  else
86  emit findFailed();
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(gDoomseekerTemplatedPathResolver().resolve(path()));
145  if (!path().isEmpty())
146  {
147  if (error.isEmpty() && !fileInfo.exists())
148  error = tr("File doesn't exist.");
149 
150  if (error.isEmpty() && fileInfo.isDir() && !fileInfo.isBundle())
151  error = tr("This is a directory.");
152  }
153 
154  d->lblWarning->setVisible(!error.isEmpty());
155  d->lblWarning->setToolTip(error);
156  return error.isEmpty();
157 }