Header includes cleanup
[GPXSee.git] / src / GUI / fileselectwidget.cpp
blob36a3e2da4338dd934edbc883fb30648206164602
1 #include <QPushButton>
2 #include <QToolButton>
3 #include <QFileDialog>
4 #include <QHBoxLayout>
5 #include <QFileInfo>
6 #include <QApplication>
7 #include <QFontMetrics>
8 #include "fileselectwidget.h"
11 FileSelectWidget::FileSelectWidget(QWidget *parent) : QWidget(parent)
13 _edit = new QLineEdit();
14 #ifdef Q_OS_ANDROID
15 _edit->setDisabled(true);
16 #else // Q_OS_ANDROID
17 QFontMetrics fm(QApplication::font());
18 _edit->setMinimumWidth(fm.averageCharWidth() * (QDir::homePath().length()
19 + 12));
20 #endif // Q_OS_ANDROID
21 #ifdef Q_OS_WIN32
22 _button = new QPushButton("...");
23 _button->setMaximumWidth(_button->sizeHint().width() / 2);
24 #else // Q_OS_WIN32
25 _button = new QToolButton();
26 _button->setText("...");
27 #endif // Q_OS_WIN32
28 connect(_button, &QToolButton::clicked, this, &FileSelectWidget::browse);
30 QHBoxLayout *layout = new QHBoxLayout();
31 layout->setContentsMargins(QMargins());
32 layout->addWidget(_edit);
33 layout->addWidget(_button);
34 setLayout(layout);
36 QSizePolicy p(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
37 setSizePolicy(p);
40 void FileSelectWidget::browse()
42 #ifdef Q_OS_ANDROID
43 _fileName = QFileDialog::getSaveFileName(this, tr("Select file"));
44 if (!_fileName.isEmpty())
45 _edit->setText(Util::displayName(_fileName));
46 #else // Q_OS_ANDROID
47 QFileInfo fi(_edit->text());
48 QString fileName = QFileDialog::getSaveFileName(this, tr("Select file"),
49 fi.dir().absolutePath(), _filter);
50 if (!fileName.isEmpty())
51 _edit->setText(fileName);
52 #endif // Q_OS_ANDROID
55 bool FileSelectWidget::checkFile(QString &error) const
57 if (file().isEmpty()) {
58 error = tr("No output file selected.");
59 return false;
62 QFile f(file());
63 QFileInfo fi(f);
64 bool exists = fi.exists();
65 bool opened = false;
67 if (exists && fi.isDir()) {
68 error = tr("%1 is a directory.").arg(f.fileName());
69 return false;
70 } else if ((exists && !fi.isWritable())
71 || !(opened = f.open(QFile::Append))) {
72 error = tr("%1 is not writable.").arg(f.fileName());
73 return false;
76 if (opened) {
77 f.close();
78 if (!exists)
79 f.remove();
82 return true;