1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* This file is part of KDevelop
* Copyright 2014 Alex Richardson <arichardson.kde@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "configdialog.h"
#include "debug.h"
#include <QCloseEvent>
#include <QPushButton>
#include <QPointer>
#include <KMessageBox>
#include <KLocalizedString>
#include <iplugin.h>
#include <configpage.h>
#include <icore.h>
#include <iplugincontroller.h>
using namespace KDevelop;
//FIXME: unit test this code!
ConfigDialog::ConfigDialog(QWidget* parent)
: KPageDialog(parent)
{
setWindowTitle(i18nc("@title:window", "Configure"));
setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
button(QDialogButtonBox::Apply)->setEnabled(false);
setObjectName(QStringLiteral("configdialog"));
auto onApplyClicked = [this] {
auto page = qobject_cast<ConfigPage*>(currentPage()->widget());
Q_ASSERT(page);
applyChanges(page);
};
connect(button(QDialogButtonBox::Apply), &QPushButton::clicked, onApplyClicked);
connect(button(QDialogButtonBox::Ok), &QPushButton::clicked, onApplyClicked);
connect(button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, [this]() {
auto page = qobject_cast<ConfigPage*>(currentPage()->widget());
Q_ASSERT(page);
page->defaults();
});
connect(this, &KPageDialog::currentPageChanged, this, &ConfigDialog::checkForUnsavedChanges);
// make sure we don't keep any entries for unloaded plugins
connect(ICore::self()->pluginController(), &IPluginController::unloadingPlugin,
this, &ConfigDialog::removePagesForPlugin);
}
KPageWidgetItem* ConfigDialog::itemForPage(ConfigPage* page) const
{
for (auto& item : m_pages) {
if (item->widget() == page) {
return item;
}
}
return nullptr;
}
int ConfigDialog::checkForUnsavedChanges(KPageWidgetItem* current, KPageWidgetItem* before)
{
Q_UNUSED(current);
if (!m_currentPageHasChanges) {
return KMessageBox::Yes;
}
// before must be non-null, because if we change from nothing to a new page m_currentPageHasChanges must also be false!
Q_ASSERT(before);
auto oldPage = qobject_cast<ConfigPage*>(before->widget());
Q_ASSERT(oldPage);
auto dialogResult = KMessageBox::warningYesNoCancel(this, i18n("The settings of the current module have changed.\n"
"Do you want to apply the changes or discard them?"), i18nc("@title:window", "Apply Settings"), KStandardGuiItem::apply(),
KStandardGuiItem::discard(), KStandardGuiItem::cancel());
if (dialogResult == KMessageBox::No) {
oldPage->reset();
m_currentPageHasChanges = false;
button(QDialogButtonBox::Apply)->setEnabled(false);
} else if (dialogResult == KMessageBox::Yes) {
applyChanges(oldPage);
} else if (dialogResult == KMessageBox::Cancel) {
// restore old state
QSignalBlocker block(this); // prevent recursion
setCurrentPage(before);
}
return dialogResult;
}
void ConfigDialog::closeEvent(QCloseEvent* event)
{
if (checkForUnsavedChanges(currentPage(), currentPage()) == KMessageBox::Cancel) {
// if the user clicked cancel he wants to continue editing the current page -> don't close
event->ignore();
} else {
event->accept();
}
}
void ConfigDialog::removeConfigPage(ConfigPage* page)
{
auto item = itemForPage(page);
Q_ASSERT(item);
removePage(item);
m_pages.removeAll(QPointer<KPageWidgetItem>(item));
// also remove all items that were deleted because a parent KPageWidgetItem was removed
m_pages.removeAll(QPointer<KPageWidgetItem>());
}
void ConfigDialog::removePagesForPlugin(IPlugin* plugin)
{
Q_ASSERT(plugin);
const auto oldPages = m_pages;
for (auto&& item : oldPages) {
if (!item) {
continue;
}
auto page = qobject_cast<ConfigPage*>(item->widget());
if (page && page->plugin() == plugin) {
removePage(item); // this also deletes the config page -> QPointer is set to null
}
};
// also remove all items that were deleted because a parent KPageWidgetItem was removed
m_pages.removeAll(QPointer<KPageWidgetItem>());
}
void ConfigDialog::appendConfigPage(ConfigPage* page)
{
addConfigPageInternal(addPage(page, page->name()), page);
}
void ConfigDialog::insertConfigPage(ConfigPage* before, ConfigPage* page)
{
Q_ASSERT(before);
auto beforeItem = itemForPage(before);
Q_ASSERT(beforeItem);
addConfigPageInternal(insertPage(beforeItem, page, page->name()), page);
}
void ConfigDialog::appendSubConfigPage(ConfigPage* parentPage, ConfigPage* page)
{
auto item = itemForPage(parentPage);
Q_ASSERT(item);
addConfigPageInternal(addSubPage(item, page, page->name()), page);
}
void ConfigDialog::addConfigPageInternal(KPageWidgetItem* item, ConfigPage* page)
{
item->setHeader(page->fullName());
item->setIcon(page->icon());
page->initConfigManager();
page->reset(); // make sure all widgets are in the correct state
// make sure that we only connect to changed after calling reset()
connect(page, &ConfigPage::changed, this, &ConfigDialog::onPageChanged);
m_pages.append(item);
for (int i = 0; i < page->childPages(); ++i) {
auto child = page->childPage(i);
appendSubConfigPage(page, child);
}
}
void ConfigDialog::onPageChanged()
{
QObject* from = sender();
if (from && from != currentPage()->widget()) {
qCWarning(SHELL) << "Settings in config page" << from << "changed, while" << currentPage()->widget() << "is currently selected. This case is not implemented yet.";
return;
// TODO: add a QHash<ConfigPage*, bool> as a member to make sure the apply button is always correct
// TODO: when pressing okay show confirm dialog if other pages have changed or just silently apply every page? "Items on other pages have changed, do you wish to review those changes? + list with changed pages."
}
if (!m_currentlyApplyingChanges) {
// e.g. PluginPreferences emits changed() from its apply method, better fix this here than having to
// ensure that no plugin emits changed() from apply()
// together with KPageDialog emitting currentPageChanged("Plugins", nullptr) this could cause a crash
// when we dereference before
m_currentPageHasChanges = true;
button(QDialogButtonBox::Apply)->setEnabled(true);
}
}
void ConfigDialog::applyChanges(ConfigPage* page)
{
// must set this to false before calling apply, otherwise we get the confirmation dialog
// whenever we enable/disable plugins.
// This is because KPageWidget then emits currentPageChanged("Plugins", nullptr), which seems like a bug to me,
// it should rather emit currentPageChanged("Plugins", "Plugins") or even better nothing at all, since the current
// page did not actually change!
// TODO: fix KPageWidget
m_currentPageHasChanges = false;
m_currentlyApplyingChanges = true;
page->apply();
m_currentlyApplyingChanges = false;
Q_ASSERT(!m_currentPageHasChanges);
button(QDialogButtonBox::Apply)->setEnabled(false);
emit configSaved(page);
}
|