|
| 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 | +} |
0 commit comments