From 8a01af32ef2080dd97c182c48f702fae232f4dfe Mon Sep 17 00:00:00 2001 From: MinyazevR <89993880+MinyazevR@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:18:35 +0300 Subject: [PATCH] Add directory picker (#1889) * Add directory picker * Add Russian translation for dirPicker * Rename the class according to the conventions --------- Co-authored-by: zhitm --- qrtranslations/fr/qrutils_fr.ts | 13 ++++++ qrtranslations/ru/qrutils_ru.ts | 13 ++++++ qrutils/widgets/dirPicker.cpp | 71 +++++++++++++++++++++++++++++++++ qrutils/widgets/dirPicker.h | 57 ++++++++++++++++++++++++++ qrutils/widgets/widgets.pri | 2 + 5 files changed, 156 insertions(+) create mode 100644 qrutils/widgets/dirPicker.cpp create mode 100644 qrutils/widgets/dirPicker.h diff --git a/qrtranslations/fr/qrutils_fr.ts b/qrtranslations/fr/qrutils_fr.ts index d930d61d83..0ac115c262 100644 --- a/qrtranslations/fr/qrutils_fr.ts +++ b/qrtranslations/fr/qrutils_fr.ts @@ -447,6 +447,19 @@ + + qReal::ui::DirPicker + + + Browse... + + + + + Select directory + + + qReal::ui::ImagePicker diff --git a/qrtranslations/ru/qrutils_ru.ts b/qrtranslations/ru/qrutils_ru.ts index 36fc8f0fe9..d565092af3 100644 --- a/qrtranslations/ru/qrutils_ru.ts +++ b/qrtranslations/ru/qrutils_ru.ts @@ -559,6 +559,19 @@ Очистить консоль + + qReal::ui::DirPicker + + + Browse... + Обзор... + + + + Select directory + Выберите директорию + + qReal::ui::ImagePicker diff --git a/qrutils/widgets/dirPicker.cpp b/qrutils/widgets/dirPicker.cpp new file mode 100644 index 0000000000..fb228103a4 --- /dev/null +++ b/qrutils/widgets/dirPicker.cpp @@ -0,0 +1,71 @@ +/* Copyright 2025 CyberTech Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + +#include "dirPicker.h" +#include +#include +#include +#include +#include + +#include +#include + +using namespace qReal::ui; + + +DirPicker::DirPicker(QWidget *parent) + : QWidget(parent) + , mLabel(new QLabel(this)) + , mPathEditor(new QLineEdit(this)) +{ + QPushButton *button = new QPushButton(style()->standardIcon(QStyle::SP_DirIcon), tr("Browse..."), this); + QHBoxLayout *layout = new QHBoxLayout(this); + layout->addWidget(mLabel); + layout->addWidget(mPathEditor); + layout->addWidget(button); + connect(button, &QPushButton::clicked, this, &DirPicker::pick); +} + +void DirPicker::configure(const QString &settingsKey, const QString &title) +{ + mSettingsKey = settingsKey; + mLabel->setText(title); +} + +bool DirPicker::isSavedDirExist() +{ + return QDir(SettingsManager::value(mSettingsKey).toString()).exists(); +} + +void DirPicker::save() const +{ + if (!mPathEditor->text().isEmpty() && !mSettingsKey.isEmpty()) { + SettingsManager::setValue(mSettingsKey, mPathEditor->text()); + } +} + +void DirPicker::restore() +{ + if (!mSettingsKey.isEmpty()) { + mPathEditor->setText(SettingsManager::value(mSettingsKey).toString()); + } +} + +void DirPicker::pick() +{ + QDir dirPath = QFileDialog::getExistingDirectory(this, tr("Select directory")); + SettingsManager::setValue(mSettingsKey, dirPath.absolutePath()); + mPathEditor->setText(dirPath.absolutePath()); +} diff --git a/qrutils/widgets/dirPicker.h b/qrutils/widgets/dirPicker.h new file mode 100644 index 0000000000..fa359bfbed --- /dev/null +++ b/qrutils/widgets/dirPicker.h @@ -0,0 +1,57 @@ +/* Copyright 2025 CyberTech Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + +#pragma once + +#include +#include + +class QLabel; +class QLineEdit; + +namespace qReal { +namespace ui { + +/// Picks some image from disk, saves into settings. +class QRUTILS_EXPORT DirPicker : public QWidget +{ + Q_OBJECT + +public: + explicit DirPicker(QWidget *parent = nullptr); + + /// Sets parameters of the image picker. + void configure(const QString &settingsKey, const QString &title); + + /// Saves picked location into settings. + void save() const; + + /// Restores last picked value. + void restore(); + + /// Determines whether the picked location exists or not + bool isSavedDirExist(); + + +private slots: + void pick(); + +private: + QString mSettingsKey; + QLabel *mLabel; + QLineEdit *mPathEditor; +}; + +} +} diff --git a/qrutils/widgets/widgets.pri b/qrutils/widgets/widgets.pri index c98cbd474e..a7041c2f84 100644 --- a/qrutils/widgets/widgets.pri +++ b/qrutils/widgets/widgets.pri @@ -14,6 +14,7 @@ HEADERS += \ $$PWD/colorListEditor.h \ + $$PWD/dirPicker.h \ $$PWD/paintWidget.h \ $$PWD/painterInterface.h \ $$PWD/searchLineEdit.h \ @@ -26,6 +27,7 @@ HEADERS += \ SOURCES += \ $$PWD/colorListEditor.cpp \ + $$PWD/dirPicker.cpp \ $$PWD/paintWidget.cpp \ $$PWD/searchLineEdit.cpp \ $$PWD/consoleDock.cpp \