Skip to content

Commit 5d86691

Browse files
author
zhitm
committed
Add directory picker
1 parent 4e580f6 commit 5d86691

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

qrutils/widgets/dirPicker.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright 2017 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+
{
31+
mLabel = new QLabel(this);
32+
mPathEditor = new QLineEdit(this);
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+
return QDir(SettingsManager::value(mSettingsKey).toString()).exists();
49+
}
50+
51+
void dirPicker::save() const
52+
{
53+
if (!mPathEditor->text().isEmpty() && !mSettingsKey.isEmpty()) {
54+
SettingsManager::setValue(mSettingsKey, mPathEditor->text());
55+
}
56+
}
57+
58+
void dirPicker::restore()
59+
{
60+
if (!mSettingsKey.isEmpty()) {
61+
mPathEditor->setText(SettingsManager::value(mSettingsKey).toString());
62+
}
63+
}
64+
65+
void dirPicker::pick()
66+
{
67+
QDir dirPath=QFileDialog::getExistingDirectory(this, "Get Any File");
68+
SettingsManager::setValue(mSettingsKey, dirPath.absolutePath());
69+
mPathEditor->setText(dirPath.absolutePath());
70+
}

qrutils/widgets/dirPicker.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2017 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+
19+
#include <qrutils/utilsDeclSpec.h>
20+
21+
class QLabel;
22+
class QLineEdit;
23+
24+
namespace qReal {
25+
namespace ui {
26+
27+
/// Picks some image from disk, saves into settings.
28+
class QRUTILS_EXPORT dirPicker : public QWidget
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
explicit dirPicker(QWidget *parent = nullptr);
34+
35+
/// Sets parameters of the image picker.
36+
void configure(const QString &settingsKey, const QString &title);
37+
38+
/// Saves picked location into settings.
39+
void save() const;
40+
41+
/// Restores last picked value.
42+
void restore();
43+
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)