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 "filefilter.h"
26 #include "ini/inisection.h"
27 #include "ini/inivariable.h"
28 #include "pathfinder/pathfind.h"
29 #include "serverapi/gamefile.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  return;
75  QString path = PathFind::findGameFile(d->neighbourStrategy->neighbours(), d->file);
76  if (!path.isEmpty())
77  {
78  d->lePath->setText(path);
79  emit pathChanged();
80  }
81  else
82  emit findFailed();
83 }
84 
85 bool FilePickWidget::isEmpty() const
86 {
87  return path().trimmed().isEmpty();
88 }
89 
90 void FilePickWidget::setFile(const GameFile &file)
91 {
92  d->file = file;
93  d->lblTitle->setText(tr("Path to %1 executable:").arg(file.niceName()));
94  d->btnFind->setVisible(canSearch());
95 }
96 
97 void FilePickWidget::setNeighbourStrategy(QSharedPointer<NeighbourStrategy> strategy)
98 {
99  d->neighbourStrategy = strategy;
100 }
101 
102 QString FilePickWidget::path() const
103 {
104  return d->lePath->text().trimmed();
105 }
106 
107 bool FilePickWidget::canSearch() const
108 {
109  return !d->file.fileName().isEmpty();
110 }
111 
112 void FilePickWidget::emitPathChangedIfChanged()
113 {
114  if (d->changed)
115  {
116  d->changed = false;
117  emit pathChanged();
118  }
119 }
120 
121 void FilePickWidget::trackEdit()
122 {
123  d->changed = true;
124 }
125 
126 void FilePickWidget::load(const IniSection &cfg)
127 {
128  d->lePath->setText(cfg[d->file.configName()].valueString());
129 }
130 
131 void FilePickWidget::save(IniSection &cfg)
132 {
133  cfg[d->file.configName()].setValue(d->lePath->text());
134 }
135 
136 bool FilePickWidget::validate()
137 {
138  QString error;
139 
140  QFileInfo fileInfo(path());
141  if (!path().isEmpty())
142  {
143  if (error.isEmpty() && !fileInfo.exists())
144  error = tr("File doesn't exist.");
145 
146  if (error.isEmpty() && fileInfo.isDir() && !fileInfo.isBundle())
147  error = tr("This is a directory.");
148  }
149 
150  d->lblWarning->setVisible(!error.isEmpty());
151  d->lblWarning->setToolTip(error);
152  return error.isEmpty();
153 }