mirror of
https://github.com/KDE/latte-dock.git
synced 2025-01-11 13:18:13 +03:00
settings:provide views handler
This commit is contained in:
parent
b25492a6fb
commit
714b90e4a7
@ -1,6 +1,7 @@
|
||||
set(lattedock-app_SRCS
|
||||
${lattedock-app_SRCS}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/viewsdialog.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/viewshandler.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
// local
|
||||
#include "ui_viewsdialog.h"
|
||||
#include "viewshandler.h"
|
||||
#include "../settingsdialog/layoutscontroller.h"
|
||||
|
||||
namespace Latte {
|
||||
@ -36,6 +37,11 @@ ViewsDialog::ViewsDialog(SettingsDialog *parent, Controller::Layouts *controller
|
||||
{
|
||||
//! first we need to setup the ui
|
||||
m_ui->setupUi(this);
|
||||
//! we must create handlers after creating/adjusting the ui
|
||||
m_handler = new Handler::ViewsHandler(this);
|
||||
|
||||
connect(m_handler, &Handler::ViewsHandler::currentLayoutChanged, this, &ViewsDialog::updateApplyButtonsState);
|
||||
connect(m_handler, &Handler::ViewsHandler::dataChanged, this, &ViewsDialog::updateApplyButtonsState);
|
||||
|
||||
connect(m_ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked,
|
||||
this, &ViewsDialog::onOk);
|
||||
|
@ -38,6 +38,9 @@ namespace Settings {
|
||||
namespace Controller {
|
||||
class Layouts;
|
||||
}
|
||||
namespace Handler {
|
||||
class ViewsHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,6 +75,8 @@ private:
|
||||
SettingsDialog *m_parentDlg{nullptr};
|
||||
Ui::ViewsDialog *m_ui;
|
||||
Controller::Layouts *m_layoutsController{nullptr};
|
||||
|
||||
Handler::ViewsHandler *m_handler;
|
||||
};
|
||||
|
||||
}
|
||||
|
172
app/settings/viewsdialog/viewshandler.cpp
Normal file
172
app/settings/viewsdialog/viewshandler.cpp
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2021 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* This file is part of Latte-Dock
|
||||
*
|
||||
* Latte-Dock 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 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* Latte-Dock 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "viewshandler.h"
|
||||
|
||||
// local
|
||||
#include "ui_viewsdialog.h"
|
||||
#include "viewsdialog.h"
|
||||
#include "../settingsdialog/layoutscontroller.h"
|
||||
#include "../settingsdialog/layoutsmodel.h"
|
||||
#include "../settingsdialog/delegates/layoutcmbitemdelegate.h"
|
||||
#include "../../data/layoutstable.h"
|
||||
#include "../../layout/abstractlayout.h"
|
||||
|
||||
// Qt
|
||||
#include <QMessageBox>
|
||||
|
||||
namespace Latte {
|
||||
namespace Settings {
|
||||
namespace Handler {
|
||||
|
||||
ViewsHandler::ViewsHandler(Dialog::ViewsDialog *dialog)
|
||||
: Generic(dialog),
|
||||
m_dialog(dialog),
|
||||
m_ui(m_dialog->ui())
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
ViewsHandler::~ViewsHandler()
|
||||
{
|
||||
}
|
||||
|
||||
void ViewsHandler::init()
|
||||
{
|
||||
//! Layouts
|
||||
m_layoutsProxyModel = new QSortFilterProxyModel(this);
|
||||
m_layoutsProxyModel->setSourceModel(m_dialog->layoutsController()->baseModel());
|
||||
m_layoutsProxyModel->setSortRole(Model::Layouts::SORTINGROLE);
|
||||
m_layoutsProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
||||
m_layoutsProxyModel->sort(Model::Layouts::NAMECOLUMN, Qt::AscendingOrder);
|
||||
|
||||
m_ui->layoutsCmb->setModel(m_layoutsProxyModel);
|
||||
m_ui->layoutsCmb->setModelColumn(Model::Layouts::NAMECOLUMN);
|
||||
m_ui->layoutsCmb->setItemDelegate(new Settings::Layout::Delegate::LayoutCmbItemDelegate(this));
|
||||
|
||||
connect(this, &ViewsHandler::currentLayoutChanged, this, &ViewsHandler::reload);
|
||||
|
||||
reload();
|
||||
|
||||
//! connect layout combobox after the selected layout has been loaded
|
||||
connect(m_ui->layoutsCmb, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ViewsHandler::onCurrentLayoutIndexChanged);
|
||||
|
||||
//! data were changed
|
||||
connect(this, &ViewsHandler::dataChanged, this, [&]() {
|
||||
loadLayout(c_data);
|
||||
});
|
||||
}
|
||||
|
||||
void ViewsHandler::reload()
|
||||
{
|
||||
o_data = m_dialog->layoutsController()->selectedLayoutCurrentData();
|
||||
c_data = o_data;
|
||||
|
||||
m_ui->layoutsCmb->setCurrentText(o_data.name);
|
||||
|
||||
loadLayout(c_data);
|
||||
}
|
||||
|
||||
void ViewsHandler::loadLayout(const Latte::Data::Layout &data)
|
||||
{
|
||||
updateWindowTitle();
|
||||
}
|
||||
|
||||
Latte::Data::Layout ViewsHandler::currentData() const
|
||||
{
|
||||
return c_data;
|
||||
}
|
||||
|
||||
bool ViewsHandler::hasChangedData() const
|
||||
{
|
||||
return o_data != c_data;
|
||||
}
|
||||
|
||||
bool ViewsHandler::inDefaultValues() const
|
||||
{
|
||||
//nothing special
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ViewsHandler::reset()
|
||||
{
|
||||
c_data = o_data;
|
||||
emit currentLayoutChanged();
|
||||
}
|
||||
|
||||
void ViewsHandler::resetDefaults()
|
||||
{
|
||||
//do nothing
|
||||
}
|
||||
|
||||
void ViewsHandler::save()
|
||||
{
|
||||
m_dialog->layoutsController()->setLayoutProperties(currentData());
|
||||
}
|
||||
|
||||
void ViewsHandler::onCurrentLayoutIndexChanged(int row)
|
||||
{
|
||||
bool switchtonewlayout{true};
|
||||
|
||||
if (hasChangedData()) {
|
||||
int result = saveChanges();
|
||||
|
||||
if (result == QMessageBox::Apply) {
|
||||
save();
|
||||
} else if (result == QMessageBox::Discard) {
|
||||
//do nothing
|
||||
} else if (result == QMessageBox::Cancel) {
|
||||
switchtonewlayout = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (switchtonewlayout) {
|
||||
QString layoutId = m_layoutsProxyModel->data(m_layoutsProxyModel->index(row, Model::Layouts::IDCOLUMN), Qt::UserRole).toString();
|
||||
m_dialog->layoutsController()->selectRow(layoutId);
|
||||
reload();
|
||||
|
||||
emit currentLayoutChanged();
|
||||
} else {
|
||||
//! reset combobox index
|
||||
m_ui->layoutsCmb->setCurrentText(c_data.name);
|
||||
}
|
||||
}
|
||||
|
||||
void ViewsHandler::updateWindowTitle()
|
||||
{
|
||||
m_dialog->setWindowTitle(m_ui->layoutsCmb->currentText());
|
||||
}
|
||||
|
||||
int ViewsHandler::saveChanges()
|
||||
{
|
||||
if (hasChangedData()) {
|
||||
QString layoutName = c_data.name;
|
||||
QString saveChangesText = i18n("The settings of <b>%0</b> layout have changed. Do you want to apply the changes or discard them?").arg(layoutName);
|
||||
|
||||
return m_dialog->saveChangesConfirmation(saveChangesText);
|
||||
}
|
||||
|
||||
return QMessageBox::Cancel;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
101
app/settings/viewsdialog/viewshandler.h
Normal file
101
app/settings/viewsdialog/viewshandler.h
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2021 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* This file is part of Latte-Dock
|
||||
*
|
||||
* Latte-Dock 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 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* Latte-Dock 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef VIEWSDIALOGHANDLER_H
|
||||
#define VIEWSDIALOGHANDLER_H
|
||||
|
||||
// local
|
||||
#include "../generic/generichandler.h"
|
||||
#include "../../data/layoutdata.h"
|
||||
#include "../../layout/abstractlayout.h"
|
||||
|
||||
// Qt
|
||||
#include <QButtonGroup>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
namespace Ui {
|
||||
class ViewsDialog;
|
||||
}
|
||||
|
||||
namespace Latte{
|
||||
namespace Settings{
|
||||
namespace Dialog{
|
||||
class ViewsDialog;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace Latte {
|
||||
namespace Settings {
|
||||
namespace Handler {
|
||||
|
||||
//! Handlers are objects to handle the UI elements that semantically associate with specific
|
||||
//! ui::tabs or different windows. They are responsible also to handle the user interaction
|
||||
//! between controllers and views
|
||||
|
||||
class ViewsHandler : public Generic
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ViewsHandler(Dialog::ViewsDialog *dialog);
|
||||
~ViewsHandler();
|
||||
|
||||
bool hasChangedData() const override;
|
||||
bool inDefaultValues() const override;
|
||||
|
||||
Latte::Data::Layout currentData() const;
|
||||
|
||||
public slots:
|
||||
void reset() override;
|
||||
void resetDefaults() override;
|
||||
void save() override;
|
||||
|
||||
signals:
|
||||
void currentLayoutChanged();
|
||||
|
||||
private slots:
|
||||
void onCurrentLayoutIndexChanged(int row);
|
||||
void updateWindowTitle();
|
||||
|
||||
private:
|
||||
void init();
|
||||
void reload();
|
||||
|
||||
void loadLayout(const Latte::Data::Layout &data);
|
||||
|
||||
int saveChanges();
|
||||
|
||||
private:
|
||||
Dialog::ViewsDialog *m_dialog{nullptr};
|
||||
Ui::ViewsDialog *m_ui{nullptr};
|
||||
|
||||
QSortFilterProxyModel *m_layoutsProxyModel{nullptr};
|
||||
|
||||
//! current data
|
||||
Latte::Data::Layout o_data;
|
||||
Latte::Data::Layout c_data;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user