| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "filebrowsewidget.h" |
| 7 |
|
| 8 |
#include <QFileSystemModel> |
| 9 |
#include <QtWidgets/QApplication> |
| 10 |
#include <QtWidgets/QCompleter> |
| 11 |
#include <QtWidgets/QFileDialog> |
| 12 |
#include <QtWidgets/QHBoxLayout> |
| 13 |
#include <QtWidgets/QLineEdit> |
| 14 |
#include <QtWidgets/QPushButton> |
| 15 |
|
| 16 |
#include <QFileInfo> |
| 17 |
#include <QProcessEnvironment> |
| 18 |
|
| 19 |
namespace Avogadro::QtGui { |
| 20 |
|
| 21 |
FileBrowseWidget::FileBrowseWidget(QWidget* theParent) |
| 22 |
: QWidget(theParent), m_mode(), // use the setter to initialize filters. |
| 23 |
m_valid(false), m_fileSystemModel(new QFileSystemModel(this)), |
| 24 |
m_button(new QPushButton(tr("Browse"))), m_edit(new QLineEdit) |
| 25 |
{ |
| 26 |
auto* hbox = new QHBoxLayout; |
| 27 |
hbox->addWidget(m_edit); |
| 28 |
hbox->addWidget(m_button); |
| 29 |
setLayout(hbox); |
| 30 |
|
| 31 |
// Focus config |
| 32 |
setFocusPolicy(Qt::StrongFocus); |
| 33 |
setFocusProxy(m_edit); |
| 34 |
setTabOrder(m_edit, m_button); |
| 35 |
|
| 36 |
// Setup completion |
| 37 |
m_fileSystemModel->setRootPath(QDir::rootPath()); |
| 38 |
auto* fsCompleter = new QCompleter(m_fileSystemModel, this); |
| 39 |
m_edit->setCompleter(fsCompleter); |
| 40 |
|
| 41 |
// Connections: |
| 42 |
connect(m_button, SIGNAL(clicked()), SLOT(browse())); |
| 43 |
connect(m_edit, SIGNAL(textChanged(QString)), SLOT(testFileName())); |
| 44 |
connect(m_edit, SIGNAL(textChanged(QString)), |
| 45 |
SIGNAL(fileNameChanged(QString))); |
| 46 |
|
| 47 |
setMode(ExistingFile); |
| 48 |
} |
| 49 |
|
| 50 |
FileBrowseWidget::~FileBrowseWidget() {} |
| 51 |
|
| 52 |
QString FileBrowseWidget::fileName() const |
| 53 |
{ |
| 54 |
return m_edit->text(); |
| 55 |
} |
| 56 |
|
| 57 |
QPushButton* FileBrowseWidget::browseButton() const |
| 58 |
{ |
| 59 |
return m_button; |
| 60 |
} |
| 61 |
|
| 62 |
QLineEdit* FileBrowseWidget::lineEdit() const |
| 63 |
{ |
| 64 |
return m_edit; |
| 65 |
} |
| 66 |
|
| 67 |
void FileBrowseWidget::setFileName(const QString& fname) |
| 68 |
{ |
| 69 |
m_edit->setText(fname); |
| 70 |
} |
| 71 |
|
| 72 |
void FileBrowseWidget::browse() |
| 73 |
{ |
| 74 |
QString fname(fileName()); |
| 75 |
QFileInfo info(fname); |
| 76 |
QString initialFilePath; |
| 77 |
|
| 78 |
if (info.isAbsolute()) { |
| 79 |
initialFilePath = info.absolutePath(); |
| 80 |
} else if (m_mode == ExecutableFile) { |
| 81 |
initialFilePath = searchSystemPathForFile(fname); |
| 82 |
if (!initialFilePath.isEmpty()) |
| 83 |
initialFilePath = QFileInfo(initialFilePath).absolutePath(); |
| 84 |
} |
| 85 |
|
| 86 |
if (initialFilePath.isEmpty()) |
| 87 |
initialFilePath = QDir::homePath(); |
| 88 |
|
| 89 |
initialFilePath += "/" + info.fileName(); |
| 90 |
|
| 91 |
info = QFileInfo(initialFilePath); |
| 92 |
|
| 93 |
QFileDialog dlg(this); |
| 94 |
switch (m_mode) { |
| 95 |
default: |
| 96 |
case ExistingFile: |
| 97 |
dlg.setWindowTitle(tr("Select file:")); |
| 98 |
break; |
| 99 |
case ExecutableFile: |
| 100 |
dlg.setWindowTitle(tr("Select executable:")); |
| 101 |
dlg.setFilter(QDir::Executable); |
| 102 |
break; |
| 103 |
} |
| 104 |
dlg.setFileMode(QFileDialog::ExistingFile); |
| 105 |
dlg.setDirectory(info.absolutePath()); |
| 106 |
dlg.selectFile(info.fileName()); |
| 107 |
if (static_cast<QFileDialog::DialogCode>(dlg.exec()) == |
| 108 |
QFileDialog::Accepted && |
| 109 |
!dlg.selectedFiles().isEmpty()) |
| 110 |
setFileName(dlg.selectedFiles().first()); |
| 111 |
} |
| 112 |
|
| 113 |
void FileBrowseWidget::testFileName() |
| 114 |
{ |
| 115 |
QFileInfo info(fileName()); |
| 116 |
if (info.isAbsolute()) { |
| 117 |
if (info.exists()) { |
| 118 |
if (m_mode != ExecutableFile || info.isExecutable()) { |
| 119 |
fileNameMatch(); |
| 120 |
return; |
| 121 |
} |
| 122 |
} |
| 123 |
} else if (m_mode == ExecutableFile) { |
| 124 |
// for non-absolute executables, search PATH |
| 125 |
QString absoluteFilePath = searchSystemPathForFile(fileName()); |
| 126 |
if (!absoluteFilePath.isNull()) { |
| 127 |
fileNameMatch(); |
| 128 |
return; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
fileNameNoMatch(); |
| 133 |
} |
| 134 |
|
| 135 |
void FileBrowseWidget::fileNameMatch() |
| 136 |
{ |
| 137 |
QPalette pal; |
| 138 |
// get the default window text color |
| 139 |
// works for both dark theme and light theme |
| 140 |
QColor text = qApp->palette().color(QPalette::WindowText); |
| 141 |
pal.setColor(QPalette::Text, text); |
| 142 |
m_edit->setPalette(pal); |
| 143 |
m_valid = true; |
| 144 |
} |
| 145 |
|
| 146 |
void FileBrowseWidget::fileNameNoMatch() |
| 147 |
{ |
| 148 |
QPalette pal; |
| 149 |
pal.setColor(QPalette::Text, Qt::red); |
| 150 |
m_edit->setPalette(pal); |
| 151 |
m_valid = false; |
| 152 |
} |
| 153 |
|
| 154 |
QStringList FileBrowseWidget::searchSystemPathForFiles(const QStringList& execs) |
| 155 |
{ |
| 156 |
QStringList result; |
| 157 |
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 158 |
if (!env.contains(QStringLiteral("PATH"))) |
| 159 |
return result; |
| 160 |
|
| 161 |
static QString pathSplitter = |
| 162 |
#ifdef Q_OS_WIN32 |
| 163 |
";" |
| 164 |
#else // WIN32 |
| 165 |
":" |
| 166 |
#endif // WIN32 |
| 167 |
; |
| 168 |
QStringList paths = |
| 169 |
env.value(QStringLiteral("PATH")).split(pathSplitter, Qt::SkipEmptyParts); |
| 170 |
|
| 171 |
foreach (const QString& exec, execs) { |
| 172 |
foreach (const QString& path, paths) { |
| 173 |
QFileInfo info(path + "/" + exec); |
| 174 |
if (!info.exists() || !info.isFile()) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
result << info.absoluteFilePath(); |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
QString FileBrowseWidget::searchSystemPathForFile(const QString& exec) |
| 186 |
{ |
| 187 |
QString result; |
| 188 |
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 189 |
if (!env.contains(QStringLiteral("PATH"))) |
| 190 |
return result; |
| 191 |
|
| 192 |
static QString pathSplitter = |
| 193 |
#ifdef Q_OS_WIN32 |
| 194 |
";" |
| 195 |
#else // WIN32 |
| 196 |
":" |
| 197 |
#endif // WIN32 |
| 198 |
; |
| 199 |
QStringList paths = |
| 200 |
env.value(QStringLiteral("PATH")).split(pathSplitter, Qt::SkipEmptyParts); |
| 201 |
|
| 202 |
foreach (const QString& path, paths) { |
| 203 |
QFileInfo info(path + "/" + exec); |
| 204 |
if (!info.exists() || !info.isFile()) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
result = info.absoluteFilePath(); |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
return result; |
| 212 |
} |
| 213 |
|
| 214 |
void FileBrowseWidget::setMode(FileBrowseWidget::Mode m) |
| 215 |
{ |
| 216 |
m_mode = m; |
| 217 |
QDir::Filters modelFilters = |
| 218 |
QDir::Files | QDir::AllDirs | QDir::NoDot | QDir::Drives; |
| 219 |
|
| 220 |
// This should go here, but unfortunately this also filters out a ton of |
| 221 |
// directories as well... |
| 222 |
// if (m_mode == ExecutableFile) |
| 223 |
// modelFilters |= QDir::Executable; |
| 224 |
|
| 225 |
m_fileSystemModel->setFilter(modelFilters); |
| 226 |
testFileName(); |
| 227 |
} |
| 228 |
|
| 229 |
FileBrowseWidget::Mode FileBrowseWidget::mode() const |
| 230 |
{ |
| 231 |
return m_mode; |
| 232 |
} |
| 233 |
|
| 234 |
} // namespace Avogadro::QtGui |