Skip to content

Commit

Permalink
Add directory picker
Browse files Browse the repository at this point in the history
  • Loading branch information
zhitm authored and iakov committed Dec 25, 2024
1 parent 81ed49b commit dccad23
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
70 changes: 70 additions & 0 deletions qrutils/widgets/dirPicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright 2017 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, "Get Any File");
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 2017 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();

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 dccad23

Please sign in to comment.