Skip to content

Commit

Permalink
Add directory picker (#1889)
Browse files Browse the repository at this point in the history
* Add directory picker
* Add Russian translation for dirPicker
* Rename the class according to the conventions

---------

Co-authored-by: zhitm <[email protected]>
  • Loading branch information
MinyazevR and zhitm authored Jan 11, 2025
1 parent 8c56cf9 commit 8a01af3
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
13 changes: 13 additions & 0 deletions qrtranslations/fr/qrutils_fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,19 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>qReal::ui::DirPicker</name>
<message>
<location filename="../../qrutils/widgets/dirPicker.cpp" line="+33"/>
<source>Browse...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location line="+35"/>
<source>Select directory</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>qReal::ui::ImagePicker</name>
<message>
Expand Down
13 changes: 13 additions & 0 deletions qrtranslations/ru/qrutils_ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,19 @@
<translation>Очистить консоль</translation>
</message>
</context>
<context>
<name>qReal::ui::DirPicker</name>
<message>
<location filename="../../qrutils/widgets/dirPicker.cpp" line="+33"/>
<source>Browse...</source>
<translation>Обзор...</translation>
</message>
<message>
<location line="+35"/>
<source>Select directory</source>
<translation>Выберите директорию</translation>
</message>
</context>
<context>
<name>qReal::ui::ImagePicker</name>
<message>
Expand Down
71 changes: 71 additions & 0 deletions qrutils/widgets/dirPicker.cpp
Original file line number Diff line number Diff line change
@@ -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 <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStyle>

#include <qrkernel/settingsManager.h>
#include <qrutils/widgets/qRealFileDialog.h>

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());
}
57 changes: 57 additions & 0 deletions qrutils/widgets/dirPicker.h
Original file line number Diff line number Diff line change
@@ -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 <QtWidgets/QWidget>
#include <qrutils/utilsDeclSpec.h>

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;
};

}
}
2 changes: 2 additions & 0 deletions qrutils/widgets/widgets.pri
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

HEADERS += \
$$PWD/colorListEditor.h \
$$PWD/dirPicker.h \
$$PWD/paintWidget.h \
$$PWD/painterInterface.h \
$$PWD/searchLineEdit.h \
Expand All @@ -26,6 +27,7 @@ HEADERS += \

SOURCES += \
$$PWD/colorListEditor.cpp \
$$PWD/dirPicker.cpp \
$$PWD/paintWidget.cpp \
$$PWD/searchLineEdit.cpp \
$$PWD/consoleDock.cpp \
Expand Down

0 comments on commit 8a01af3

Please sign in to comment.