1 /*
2     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "screenshandler.h"
7 
8 // local
9 #include <coretypes.h>
10 #include "ui_screensdialog.h"
11 #include "screensdialog.h"
12 #include "screensmodel.h"
13 #include "delegates/checkboxdelegate.h"
14 #include "../../lattecorona.h"
15 #include "../../screenpool.h"
16 
17 // KDE
18 #include <KMessageBox>
19 
20 namespace Latte {
21 namespace Settings {
22 namespace Handler {
23 
ScreensHandler(Dialog::ScreensDialog * dialog)24 ScreensHandler::ScreensHandler(Dialog::ScreensDialog *dialog)
25     : Generic(dialog),
26       m_dialog(dialog),
27       m_ui(m_dialog->ui()),
28       m_screensModel(new Model::Screens(this))
29 {
30     init();
31 }
32 
~ScreensHandler()33 ScreensHandler::~ScreensHandler()
34 {
35 }
36 
init()37 void ScreensHandler::init()
38 {
39     m_ui->screensTable->horizontalHeader()->setSectionsClickable(false);
40     m_ui->screensTable->verticalHeader()->setVisible(false);
41 
42     //! Data Changed
43     connect(m_screensModel, &Settings::Model::Screens::screenDataChanged, this, &ScreensHandler::dataChanged);
44 
45     //! Screens Proxy Model
46     m_screensProxyModel = new QSortFilterProxyModel(this);
47     m_screensProxyModel->setSourceModel(m_screensModel);
48 
49     m_ui->screensTable->setModel(m_screensProxyModel);
50 
51     m_screensProxyModel->setSortRole(Model::Screens::SORTINGROLE);
52     m_screensProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
53 
54     m_ui->screensTable->sortByColumn(Model::Screens::SCREENCOLUMN, Qt::DescendingOrder);
55 
56     //! Delegates
57     m_ui->screensTable->setItemDelegateForColumn(Model::Screens::SCREENCOLUMN, new Settings::Screens::Delegate::CheckBox(this));
58 
59     //! Buttons
60     connect(m_ui->deselectAllBtn, &QPushButton::clicked, this, &ScreensHandler::deselectAll);
61 
62     //! load data
63     m_screensModel->setData(m_dialog->layoutsController()->screensData());
64 
65     //! signals
66     connect(m_dialog->removeNowButton(), &QPushButton::clicked, this, &ScreensHandler::onRemoveNow);
67     connect(m_screensModel, &Settings::Model::Screens::screenDataChanged, this, &ScreensHandler::onScreenDataChanged);
68 
69     onScreenDataChanged();
70 }
71 
hasChangedData() const72 bool ScreensHandler::hasChangedData() const
73 {
74     return m_screensModel->hasChangedData();
75 }
76 
inDefaultValues() const77 bool ScreensHandler::inDefaultValues() const
78 {
79     return m_screensModel->inDefaultValues();
80 }
81 
deselectAll()82 void ScreensHandler::deselectAll()
83 {
84     m_screensModel->deselectAll();
85 }
86 
reset()87 void ScreensHandler::reset()
88 {
89     if (m_screensModel->hasChangedData()) {
90         m_screensModel->reset();
91     }
92 }
93 
resetDefaults()94 void ScreensHandler::resetDefaults()
95 {
96     reset();
97 }
98 
onRemoveNow()99 void ScreensHandler::onRemoveNow()
100 {
101     if (!m_screensModel->hasChecked()) {
102         return;
103     }
104 
105     Data::ScreensTable checkedscreens = m_screensModel->checkedScreens();
106 
107     if (removalConfirmation(checkedscreens.names())) {
108         save();
109     }
110 }
111 
onScreenDataChanged()112 void ScreensHandler::onScreenDataChanged()
113 {
114     m_dialog->removeNowButton()->setEnabled(m_screensModel->hasChecked());
115 }
116 
117 
save()118 void ScreensHandler::save()
119 {
120     if (!m_screensModel->hasChecked()) {
121         return;
122     }
123 
124     m_dialog->corona()->screenPool()->removeScreens(m_screensModel->checkedScreens());
125 
126     //! reload data
127     m_screensModel->setData(m_dialog->layoutsController()->screensData());
128 }
129 
removalConfirmation(const QStringList & screens) const130 bool ScreensHandler::removalConfirmation(const QStringList &screens) const
131 {
132     return (KMessageBox::warningYesNo(m_dialog,
133                                      i18np("You are going to <b>remove %2</b> reference completely.<br/>Would you like to continue?",
134                                            "You are going to <b>remove %2</b> references completely.<br/>Would you like to continue?",
135                                            screens.count(),
136                                            screens.join(", ")),
137                                      i18n("Approve Removal")) == KMessageBox::Yes);
138 }
139 
140 }
141 }
142 }
143