1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/GUI/fileselectwidget.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2016-10-12 22:37:40 +02:00
#include <QPushButton>
2016-10-11 03:17:30 +02:00
#include <QToolButton>
2016-05-24 03:01:22 +02:00
#include <QFileDialog>
#include <QHBoxLayout>
2016-05-28 07:39:40 +02:00
#include <QFileInfo>
#include <QApplication>
#include <QFontMetrics>
2016-05-24 03:01:22 +02:00
#include "fileselectwidget.h"
FileSelectWidget::FileSelectWidget(QWidget *parent) : QWidget(parent)
{
2016-05-28 07:39:40 +02:00
QFontMetrics fm(QApplication::font());
2016-05-24 03:01:22 +02:00
_edit = new QLineEdit();
2020-10-07 08:57:24 +02:00
_edit->setMinimumWidth(fm.averageCharWidth() * (QDir::homePath().length()
+ 12));
2016-10-12 22:37:40 +02:00
#ifdef Q_OS_WIN32
_button = new QPushButton("...");
_button->setMaximumWidth(_button->sizeHint().width() / 2);
#else // Q_OS_WIN32
2016-10-11 03:17:30 +02:00
_button = new QToolButton();
_button->setText("...");
2016-10-12 22:37:40 +02:00
#endif // Q_OS_WIN32
2016-05-24 03:01:22 +02:00
connect(_button, SIGNAL(clicked()), this, SLOT(browse()));
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins(QMargins());
2016-05-24 03:01:22 +02:00
layout->addWidget(_edit);
layout->addWidget(_button);
setLayout(layout);
2016-10-12 22:37:40 +02:00
QSizePolicy p(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
setSizePolicy(p);
2016-05-24 03:01:22 +02:00
}
void FileSelectWidget::browse()
{
2016-05-28 07:39:40 +02:00
QFileInfo fi(_edit->text());
2016-05-24 03:01:22 +02:00
QString fileName = QFileDialog::getSaveFileName(this, tr("Select file"),
2016-05-28 07:39:40 +02:00
fi.dir().absolutePath(), _filter);
2016-05-24 03:01:22 +02:00
if (!fileName.isEmpty())
_edit->setText(fileName);
}
2020-09-27 00:34:38 +02:00
bool FileSelectWidget::checkFile(QString &error) const
{
if (_edit->text().isEmpty()) {
error = tr("No output file selected.");
return false;
}
QFile file(_edit->text());
QFileInfo fi(file);
bool exists = fi.exists();
bool opened = false;
if (exists && fi.isDir()) {
error = tr("%1 is a directory.").arg(file.fileName());
return false;
} else if ((exists && !fi.isWritable())
|| !(opened = file.open(QFile::Append))) {
error = tr("%1 is not writable.").arg(file.fileName());
return false;
}
if (opened) {
file.close();
if (!exists)
file.remove();
}
return true;
}