Skip to content

Commit 8a01af3

Browse files
MinyazevRzhitm
andauthored
Add directory picker (#1889)
* Add directory picker * Add Russian translation for dirPicker * Rename the class according to the conventions --------- Co-authored-by: zhitm <zhtinuhina.maria@gmail.com>
1 parent 8c56cf9 commit 8a01af3

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

qrtranslations/fr/qrutils_fr.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,19 @@
447447
<translation type="unfinished"></translation>
448448
</message>
449449
</context>
450+
<context>
451+
<name>qReal::ui::DirPicker</name>
452+
<message>
453+
<location filename="../../qrutils/widgets/dirPicker.cpp" line="+33"/>
454+
<source>Browse...</source>
455+
<translation type="unfinished"></translation>
456+
</message>
457+
<message>
458+
<location line="+35"/>
459+
<source>Select directory</source>
460+
<translation type="unfinished"></translation>
461+
</message>
462+
</context>
450463
<context>
451464
<name>qReal::ui::ImagePicker</name>
452465
<message>

qrtranslations/ru/qrutils_ru.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,19 @@
559559
<translation>Очистить консоль</translation>
560560
</message>
561561
</context>
562+
<context>
563+
<name>qReal::ui::DirPicker</name>
564+
<message>
565+
<location filename="../../qrutils/widgets/dirPicker.cpp" line="+33"/>
566+
<source>Browse...</source>
567+
<translation>Обзор...</translation>
568+
</message>
569+
<message>
570+
<location line="+35"/>
571+
<source>Select directory</source>
572+
<translation>Выберите директорию</translation>
573+
</message>
574+
</context>
562575
<context>
563576
<name>qReal::ui::ImagePicker</name>
564577
<message>

qrutils/widgets/dirPicker.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright 2025 CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#include "dirPicker.h"
16+
#include <QtWidgets/QHBoxLayout>
17+
#include <QtWidgets/QLabel>
18+
#include <QtWidgets/QLineEdit>
19+
#include <QtWidgets/QPushButton>
20+
#include <QtWidgets/QStyle>
21+
22+
#include <qrkernel/settingsManager.h>
23+
#include <qrutils/widgets/qRealFileDialog.h>
24+
25+
using namespace qReal::ui;
26+
27+
28+
DirPicker::DirPicker(QWidget *parent)
29+
: QWidget(parent)
30+
, mLabel(new QLabel(this))
31+
, mPathEditor(new QLineEdit(this))
32+
{
33+
QPushButton *button = new QPushButton(style()->standardIcon(QStyle::SP_DirIcon), tr("Browse..."), this);
34+
QHBoxLayout *layout = new QHBoxLayout(this);
35+
layout->addWidget(mLabel);
36+
layout->addWidget(mPathEditor);
37+
layout->addWidget(button);
38+
connect(button, &QPushButton::clicked, this, &DirPicker::pick);
39+
}
40+
41+
void DirPicker::configure(const QString &settingsKey, const QString &title)
42+
{
43+
mSettingsKey = settingsKey;
44+
mLabel->setText(title);
45+
}
46+
47+
bool DirPicker::isSavedDirExist()
48+
{
49+
return QDir(SettingsManager::value(mSettingsKey).toString()).exists();
50+
}
51+
52+
void DirPicker::save() const
53+
{
54+
if (!mPathEditor->text().isEmpty() && !mSettingsKey.isEmpty()) {
55+
SettingsManager::setValue(mSettingsKey, mPathEditor->text());
56+
}
57+
}
58+
59+
void DirPicker::restore()
60+
{
61+
if (!mSettingsKey.isEmpty()) {
62+
mPathEditor->setText(SettingsManager::value(mSettingsKey).toString());
63+
}
64+
}
65+
66+
void DirPicker::pick()
67+
{
68+
QDir dirPath = QFileDialog::getExistingDirectory(this, tr("Select directory"));
69+
SettingsManager::setValue(mSettingsKey, dirPath.absolutePath());
70+
mPathEditor->setText(dirPath.absolutePath());
71+
}

qrutils/widgets/dirPicker.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2025 CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <QtWidgets/QWidget>
18+
#include <qrutils/utilsDeclSpec.h>
19+
20+
class QLabel;
21+
class QLineEdit;
22+
23+
namespace qReal {
24+
namespace ui {
25+
26+
/// Picks some image from disk, saves into settings.
27+
class QRUTILS_EXPORT DirPicker : public QWidget
28+
{
29+
Q_OBJECT
30+
31+
public:
32+
explicit DirPicker(QWidget *parent = nullptr);
33+
34+
/// Sets parameters of the image picker.
35+
void configure(const QString &settingsKey, const QString &title);
36+
37+
/// Saves picked location into settings.
38+
void save() const;
39+
40+
/// Restores last picked value.
41+
void restore();
42+
43+
/// Determines whether the picked location exists or not
44+
bool isSavedDirExist();
45+
46+
47+
private slots:
48+
void pick();
49+
50+
private:
51+
QString mSettingsKey;
52+
QLabel *mLabel;
53+
QLineEdit *mPathEditor;
54+
};
55+
56+
}
57+
}

qrutils/widgets/widgets.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
HEADERS += \
1616
$$PWD/colorListEditor.h \
17+
$$PWD/dirPicker.h \
1718
$$PWD/paintWidget.h \
1819
$$PWD/painterInterface.h \
1920
$$PWD/searchLineEdit.h \
@@ -26,6 +27,7 @@ HEADERS += \
2627

2728
SOURCES += \
2829
$$PWD/colorListEditor.cpp \
30+
$$PWD/dirPicker.cpp \
2931
$$PWD/paintWidget.cpp \
3032
$$PWD/searchLineEdit.cpp \
3133
$$PWD/consoleDock.cpp \

0 commit comments

Comments
 (0)