From 7d038c735476b2b3071b2b2df0e391838565b4af Mon Sep 17 00:00:00 2001 From: Matthieu Bruel Date: Fri, 27 Nov 2020 13:24:54 +0100 Subject: [PATCH] Missing filechooserwidget files... --- src/ui/filechooserwidget.cpp | 106 +++++++++++++++++++++++++++++++++++ src/ui/filechooserwidget.h | 41 ++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/ui/filechooserwidget.cpp create mode 100644 src/ui/filechooserwidget.h diff --git a/src/ui/filechooserwidget.cpp b/src/ui/filechooserwidget.cpp new file mode 100644 index 000000000..cdab2edb9 --- /dev/null +++ b/src/ui/filechooserwidget.cpp @@ -0,0 +1,106 @@ +#include "filechooserwidget.h" + +#include +#include +#include +#include +#include +#include +FileChooserWidget::FileChooserWidget(QWidget* parent) + : QWidget(parent), + layout_(new QHBoxLayout(this)), + path_edit_(new QLineEdit(this)), + mode_(Mode::Directory), + filter_(""), + open_dir_path_("") { + Init(""); +} + +FileChooserWidget::FileChooserWidget(Mode mode, const QString& initial_path, + QWidget* parent) + : QWidget(parent), + layout_(new QHBoxLayout(this)), + path_edit_(new QLineEdit(this)), + mode_(mode), + filter_(""), + open_dir_path_("") { + Init(initial_path); +} + +FileChooserWidget::FileChooserWidget(Mode mode, const QString& label, + const QString& initial_path, + QWidget* parent) + : QWidget(parent), + layout_(new QHBoxLayout(this)), + path_edit_(new QLineEdit(this)), + mode_(mode), + filter_(""), + open_dir_path_("") { + QLabel* lbl = new QLabel(label, this); + layout_->addWidget(lbl); + Init(initial_path); +} + +void FileChooserWidget::SetFileFilter(const QString& filter) { + filter_ = filter; +} + +void FileChooserWidget::SetPath(const QString& path) { + QFileInfo fi(path); + if (fi.exists()) { + path_edit_->setText(path); + open_dir_path_ = fi.absolutePath(); + } +} + +QString FileChooserWidget::Path() const { + QString path(path_edit_->text()); + QFileInfo fi(path); + if (!fi.exists()) return ""; + if (mode_ == Mode::File) { + if (!fi.isFile()) return ""; + } else { + if (!fi.isDir()) return ""; + } + return path; +} + +void FileChooserWidget::Init(const QString& initialPath) { + QFileInfo fi(initialPath); + if (fi.exists()) { + path_edit_->setText(initialPath); + open_dir_path_ = fi.absolutePath(); + } + layout_->addWidget(path_edit_); + + QPushButton* changePath = new QPushButton(QLatin1String("..."), this); + connect(changePath, &QAbstractButton::clicked, this, + &FileChooserWidget::ChooseFile); + changePath->setFixedWidth( + 2 * changePath->fontMetrics().width(QLatin1String(" ... "))); + + layout_->addWidget(changePath); + layout_->setContentsMargins(2, 0, 2, 0); + + setFocusProxy(path_edit_); +} + +void FileChooserWidget::ChooseFile() { + QString new_path; + + if (mode_ == Mode::File) + new_path = QFileDialog::getOpenFileName(this, tr("Select a file"), + open_dir_path_, filter_); + else + new_path = QFileDialog::getExistingDirectory(this, tr("Select a directory"), + open_dir_path_); + + if (!new_path.isEmpty()) { + QFileInfo fi(new_path); + open_dir_path_ = fi.absolutePath(); + if (mode_ == Mode::File) + path_edit_->setText(fi.absoluteFilePath()); + else + path_edit_->setText(fi.absoluteFilePath() + "/"); + } +} diff --git a/src/ui/filechooserwidget.h b/src/ui/filechooserwidget.h new file mode 100644 index 000000000..49bcb8f00 --- /dev/null +++ b/src/ui/filechooserwidget.h @@ -0,0 +1,41 @@ +#ifndef FILECHOOSERWIDGET_H +#define FILECHOOSERWIDGET_H + +#include +class QLineEdit; +class QHBoxLayout; + +class FileChooserWidget : public QWidget { + Q_OBJECT + public: + enum class Mode { File, Directory }; + + private: + QHBoxLayout* layout_; + QLineEdit* path_edit_; + const Mode mode_; + QString filter_; + QString open_dir_path_; + + public: + FileChooserWidget(QWidget* parent); + FileChooserWidget(Mode mode, const QString& initial_path = "", + QWidget* parent = nullptr); + FileChooserWidget(Mode mode, const QString& label, + const QString& initial_path = "", + QWidget* parent = nullptr); + ~FileChooserWidget() = default; + + void SetFileFilter(const QString& filter); + + void SetPath(const QString& path); + QString Path() const; + + public slots: + void ChooseFile(); + + private: + void Init(const QString& initialPath); +}; + +#endif // FILECHOOSERWIDGET_H