RedPanda-CPP/RedPandaIDE/settingsdialog/settingswidget.cpp

201 lines
6.7 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-04-16 22:04:48 +08:00
#include "settingswidget.h"
2021-04-17 22:38:46 +08:00
#include <QAbstractItemView>
2021-04-16 22:04:48 +08:00
#include <QCheckBox>
#include <QComboBox>
2021-09-04 21:20:30 +08:00
#include <QGroupBox>
2021-04-16 22:04:48 +08:00
#include <QLineEdit>
2021-04-17 22:38:46 +08:00
#include <QListView>
2021-10-22 07:42:51 +08:00
#include <QMessageBox>
2021-04-16 22:04:48 +08:00
#include <QPlainTextEdit>
#include <QListWidget>
2021-08-30 19:52:38 +08:00
#include <QRadioButton>
2021-06-07 11:02:03 +08:00
#include <QSpinBox>
#include "../widgets/coloredit.h"
2021-10-22 07:42:51 +08:00
#include "../utils.h"
#include "../iconsmanager.h"
2021-04-16 22:04:48 +08:00
SettingsWidget::SettingsWidget(const QString &name, const QString &group, QWidget *parent):
QWidget(parent),
mSettingsChanged(false),
2022-07-04 11:39:06 +08:00
mGroup(group),
mName(name)
2021-04-16 22:04:48 +08:00
{
}
void SettingsWidget::init()
{
connect(pIconsManager,&IconsManager::actionIconsUpdated,
this, &SettingsWidget::onUpdateIcons);
onUpdateIcons();
2021-04-16 22:04:48 +08:00
load();
2021-04-17 22:38:46 +08:00
connectInputs();
}
void SettingsWidget::load()
{
2021-10-22 07:42:51 +08:00
try {
doLoad();
clearSettingsChanged();
} catch (FileError & e) {
QMessageBox::warning(nullptr,
tr("Load Error"),
e.reason());
}
2021-04-17 22:38:46 +08:00
}
void SettingsWidget::save()
{
2021-10-22 07:42:51 +08:00
try {
doSave();
clearSettingsChanged();
} catch (FileError & e) {
QMessageBox::warning(nullptr,
tr("Save Error"),
e.reason());
}
2021-04-18 11:25:40 +08:00
}
void SettingsWidget::connectAbstractItemView(QAbstractItemView *pView)
{
connect(pView->model(),&QAbstractItemModel::rowsInserted,this,&SettingsWidget::setSettingsChanged);
connect(pView->model(),&QAbstractItemModel::rowsMoved,this,&SettingsWidget::setSettingsChanged);
connect(pView->model(),&QAbstractItemModel::rowsRemoved,this,&SettingsWidget::setSettingsChanged);
connect(pView->model(),&QAbstractItemModel::dataChanged,this,&SettingsWidget::setSettingsChanged);
connect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged);
}
void SettingsWidget::disconnectAbstractItemView(QAbstractItemView *pView)
{
disconnect(pView->model(),&QAbstractItemModel::rowsInserted,this,&SettingsWidget::setSettingsChanged);
disconnect(pView->model(),&QAbstractItemModel::rowsMoved,this,&SettingsWidget::setSettingsChanged);
disconnect(pView->model(),&QAbstractItemModel::rowsRemoved,this,&SettingsWidget::setSettingsChanged);
disconnect(pView->model(),&QAbstractItemModel::dataChanged,this,&SettingsWidget::setSettingsChanged);
disconnect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged);
2021-04-17 22:38:46 +08:00
}
void SettingsWidget::updateIcons(const QSize &)
{
}
2021-04-17 22:38:46 +08:00
void SettingsWidget::connectInputs()
{
2021-04-16 22:04:48 +08:00
for (QLineEdit* p:findChildren<QLineEdit*>()) {
connect(p, &QLineEdit::textChanged, this, &SettingsWidget::setSettingsChanged);
}
for (QCheckBox* p:findChildren<QCheckBox*>()) {
2021-08-30 19:52:38 +08:00
connect(p, &QCheckBox::toggled, this, &SettingsWidget::setSettingsChanged);
}
for (QRadioButton* p:findChildren<QRadioButton*>()) {
connect(p, &QRadioButton::toggled, this, &SettingsWidget::setSettingsChanged);
2021-04-16 22:04:48 +08:00
}
for (QPlainTextEdit* p:findChildren<QPlainTextEdit*>()) {
connect(p, &QPlainTextEdit::textChanged, this, &SettingsWidget::setSettingsChanged);
}
2021-06-07 11:02:03 +08:00
for (QSpinBox* p:findChildren<QSpinBox*>()) {
connect(p, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsWidget::setSettingsChanged);
}
for (ColorEdit* p:findChildren<ColorEdit*>()) {
connect(p, &ColorEdit::colorChanged, this, &SettingsWidget::setSettingsChanged);
}
for (QComboBox* p: findChildren<QComboBox*>()) {
connect(p, QOverload<int>::of(&QComboBox::currentIndexChanged) ,this, &SettingsWidget::setSettingsChanged);
}
2021-04-17 22:38:46 +08:00
for (QAbstractItemView* p: findChildren<QAbstractItemView*>()) {
2021-04-18 11:25:40 +08:00
connectAbstractItemView(p);
2021-04-17 22:38:46 +08:00
}
for (QListWidget* p:findChildren<QListWidget*>()) {
connect(p, QOverload<int>::of(&QListWidget::currentRowChanged) ,this, &SettingsWidget::setSettingsChanged);
}
2021-09-04 21:20:30 +08:00
for (QGroupBox* p: findChildren<QGroupBox*>()) {
connect(p, &QGroupBox::toggled,this, &SettingsWidget::setSettingsChanged);
}
2021-04-16 22:04:48 +08:00
}
2021-04-18 11:25:40 +08:00
void SettingsWidget::disconnectInputs()
{
for (QLineEdit* p:findChildren<QLineEdit*>()) {
disconnect(p, &QLineEdit::textChanged, this, &SettingsWidget::setSettingsChanged);
}
for (QCheckBox* p:findChildren<QCheckBox*>()) {
disconnect(p, &QCheckBox::stateChanged, this, &SettingsWidget::setSettingsChanged);
}
for (QRadioButton* p:findChildren<QRadioButton*>()) {
disconnect(p, &QRadioButton::toggled, this, &SettingsWidget::setSettingsChanged);
}
2021-04-18 11:25:40 +08:00
for (QPlainTextEdit* p:findChildren<QPlainTextEdit*>()) {
disconnect(p, &QPlainTextEdit::textChanged, this, &SettingsWidget::setSettingsChanged);
}
for (QSpinBox* p:findChildren<QSpinBox*>()) {
disconnect(p, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsWidget::setSettingsChanged);
}
for (ColorEdit* p:findChildren<ColorEdit*>()) {
disconnect(p, &ColorEdit::colorChanged, this, &SettingsWidget::setSettingsChanged);
}
2021-04-18 11:25:40 +08:00
for (QComboBox* p: findChildren<QComboBox*>()) {
disconnect(p, QOverload<int>::of(&QComboBox::currentIndexChanged) ,this, &SettingsWidget::setSettingsChanged);
}
for (QAbstractItemView* p: findChildren<QAbstractItemView*>()) {
disconnectAbstractItemView(p);
}
for (QListWidget* p:findChildren<QListWidget*>()) {
disconnect(p, QOverload<int>::of(&QListWidget::currentRowChanged) ,this, &SettingsWidget::setSettingsChanged);
}
for (QGroupBox* p: findChildren<QGroupBox*>()) {
disconnect(p, &QGroupBox::toggled,this, &SettingsWidget::setSettingsChanged);
}
2021-04-18 11:25:40 +08:00
}
2021-04-16 22:04:48 +08:00
const QString &SettingsWidget::group()
{
return mGroup;
}
const QString &SettingsWidget::name()
{
return mName;
}
bool SettingsWidget::isSettingsChanged()
2021-04-16 22:04:48 +08:00
{
return mSettingsChanged;
}
void SettingsWidget::setSettingsChanged()
{
mSettingsChanged = true;
emit settingsChanged(true);
2021-04-16 22:04:48 +08:00
}
void SettingsWidget::clearSettingsChanged()
{
mSettingsChanged = false;
emit settingsChanged(false);
2021-04-16 22:04:48 +08:00
}
void SettingsWidget::onUpdateIcons()
{
updateIcons(pIconsManager->actionIconSize());
}