Warning, file /plasma/latte-dock/app/settings/actionsdialog/actionshandler.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "actionshandler.h" 0007 0008 // local 0009 #include "ui_actionsdialog.h" 0010 #include "actionsdialog.h" 0011 #include "../settingsdialog/tabpreferenceshandler.h" 0012 #include "../../data/contextmenudata.h" 0013 0014 // Qt 0015 #include <QDebug> 0016 0017 // KDE 0018 #include <KActionSelector> 0019 #include <KLocalizedString> 0020 0021 namespace Latte { 0022 namespace Settings { 0023 namespace Handler { 0024 0025 ActionsHandler::ActionsHandler(Dialog::ActionsDialog *dialog) 0026 : Generic(dialog), 0027 m_dialog(dialog), 0028 m_ui(m_dialog->ui()) 0029 { 0030 initItems(); 0031 init(); 0032 } 0033 0034 ActionsHandler::~ActionsHandler() 0035 { 0036 } 0037 0038 void ActionsHandler::init() 0039 { 0040 connect(m_ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &ActionsHandler::onCancel); 0041 connect(m_ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &ActionsHandler::save); 0042 connect(m_ui->buttonBox->button(QDialogButtonBox::Reset), &QPushButton::clicked, this, &ActionsHandler::reset); 0043 connect(m_ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &ActionsHandler::resetDefaults); 0044 0045 connect(m_ui->actionsSelector, &KActionSelector::added, this, [&]() { 0046 updateButtonEnablement(); 0047 }); 0048 0049 connect(m_ui->actionsSelector, &KActionSelector::removed, this, [&]() { 0050 updateButtonEnablement(); 0051 }); 0052 } 0053 0054 void ActionsHandler::initItems() 0055 { 0056 o_alwaysActions = m_dialog->preferencesHandler()->contextMenuAlwaysActions(); 0057 0058 QString itemid = Latte::Data::ContextMenu::LAYOUTSACTION; 0059 int itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0060 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("user-identity"), 0061 i18n("Layouts"), 0062 itemindex, 0063 itemid); 0064 0065 itemid = Latte::Data::ContextMenu::PREFERENCESACTION; 0066 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0067 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("configure"), 0068 i18nc("global settings window", "Configure Latte..."), 0069 itemindex, 0070 itemid); 0071 0072 itemid = Latte::Data::ContextMenu::QUITLATTEACTION; 0073 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0074 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("application-exit"), 0075 i18nc("quit application", "Quit Latte"), 0076 itemindex, 0077 itemid); 0078 0079 itemid = Latte::Data::ContextMenu::SEPARATOR1ACTION; 0080 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0081 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme(""), 0082 i18n(" --- separator --- "), 0083 itemindex, 0084 itemid); 0085 0086 itemid = Latte::Data::ContextMenu::ADDWIDGETSACTION; 0087 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0088 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("list-add"), 0089 i18n("Add Widgets..."), 0090 itemindex, 0091 itemid); 0092 0093 itemid = Latte::Data::ContextMenu::ADDVIEWACTION; 0094 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0095 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("list-add"), 0096 i18n("Add Dock/Panel"), 0097 itemindex, 0098 itemid); 0099 0100 itemid = Latte::Data::ContextMenu::MOVEVIEWACTION; 0101 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0102 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("transform-move-horizontal"), 0103 i18n("Move Dock/Panel To Layout"), 0104 itemindex, 0105 itemid); 0106 0107 itemid = Latte::Data::ContextMenu::EXPORTVIEWTEMPLATEACTION; 0108 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0109 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("document-export"), 0110 i18n("Export Dock/Panel as Template..."), 0111 itemindex, 0112 itemid); 0113 0114 itemid = Latte::Data::ContextMenu::REMOVEVIEWACTION; 0115 itemindex = Latte::Data::ContextMenu::ACTIONSEDITORDER.indexOf(itemid); 0116 m_items[itemid] = new ActionsDialog::ActionListWidgetItem(QIcon::fromTheme("delete"), 0117 i18n("Remove Dock/Panel"), 0118 itemindex, 0119 itemid); 0120 0121 loadItems(m_dialog->preferencesHandler()->contextMenuAlwaysActions()); 0122 } 0123 0124 void ActionsHandler::loadItems(const QStringList &alwaysActions) 0125 { 0126 for(int i=0; i<Latte::Data::ContextMenu::ACTIONSEDITORDER.count(); ++i) { 0127 QString actionname = Latte::Data::ContextMenu::ACTIONSEDITORDER[i]; 0128 bool inalways = alwaysActions.contains(actionname); 0129 0130 int rowinalways = m_ui->actionsSelector->selectedListWidget()->row(m_items[actionname]); 0131 int rowinedit = m_ui->actionsSelector->availableListWidget()->row(m_items[actionname]); 0132 0133 if (inalways && rowinalways == -1) { 0134 if (rowinedit >= 0) { 0135 m_ui->actionsSelector->availableListWidget()->takeItem(rowinedit); 0136 } 0137 m_ui->actionsSelector->selectedListWidget()->addItem(m_items[actionname]); 0138 } else if (!inalways && rowinedit == -1) { 0139 if (rowinalways >= 0) { 0140 m_ui->actionsSelector->selectedListWidget()->takeItem(rowinalways); 0141 } 0142 m_ui->actionsSelector->availableListWidget()->addItem(m_items[actionname]); 0143 } 0144 } 0145 0146 m_ui->actionsSelector->setAvailableInsertionPolicy(KActionSelector::Sorted); 0147 m_ui->actionsSelector->setSelectedInsertionPolicy(KActionSelector::Sorted); 0148 0149 m_ui->actionsSelector->availableListWidget()->sortItems(); 0150 m_ui->actionsSelector->selectedListWidget()->sortItems(); 0151 0152 updateButtonEnablement(); 0153 } 0154 0155 bool ActionsHandler::hasChangedData() const 0156 { 0157 return currentAlwaysData() != o_alwaysActions; 0158 } 0159 0160 bool ActionsHandler::inDefaultValues() const 0161 { 0162 return currentAlwaysData() == Data::ContextMenu::ACTIONSALWAYSVISIBLE; 0163 } 0164 0165 Data::GenericTable<Data::Generic> ActionsHandler::table(const QStringList &ids) 0166 { 0167 Data::GenericTable<Data::Generic> bastable; 0168 0169 for(int i=0; i<ids.count(); ++i) { 0170 bastable << Data::Generic(ids[i], ""); 0171 } 0172 0173 return bastable; 0174 } 0175 0176 QStringList ActionsHandler::currentAlwaysData() const 0177 { 0178 QStringList always; 0179 0180 for(int i=0; i<m_ui->actionsSelector->selectedListWidget()->count(); ++i) { 0181 QListWidgetItem *widgetitem = m_ui->actionsSelector->selectedListWidget()->item(i); 0182 always << widgetitem->data(ActionsDialog::ActionListWidgetItem::IDROLE).toString(); 0183 } 0184 0185 return always; 0186 } 0187 0188 void ActionsHandler::reset() 0189 { 0190 loadItems(o_alwaysActions); 0191 } 0192 0193 void ActionsHandler::resetDefaults() 0194 { 0195 loadItems(Data::ContextMenu::ACTIONSALWAYSVISIBLE); 0196 } 0197 0198 void ActionsHandler::updateButtonEnablement() 0199 { 0200 bool haschanges = hasChangedData(); 0201 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(haschanges); 0202 m_ui->buttonBox->button(QDialogButtonBox::Reset)->setEnabled(haschanges); 0203 m_ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(!inDefaultValues()); 0204 } 0205 0206 void ActionsHandler::onCancel() 0207 { 0208 qDebug() << Q_FUNC_INFO; 0209 m_dialog->close(); 0210 } 0211 0212 void ActionsHandler::save() 0213 { 0214 qDebug() << Q_FUNC_INFO; 0215 m_dialog->preferencesHandler()->setContextMenuAlwaysActions(currentAlwaysData()); 0216 m_dialog->close(); 0217 } 0218 0219 } 0220 } 0221 }