diff --git a/sheets/FunctionModuleRegistry.cpp b/sheets/FunctionModuleRegistry.cpp index 1be76cce793..4029144588c 100644 --- a/sheets/FunctionModuleRegistry.cpp +++ b/sheets/FunctionModuleRegistry.cpp @@ -1,217 +1,218 @@ /* This file is part of the KDE project Copyright 2008 Stefan Nikolaus This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "FunctionModuleRegistry.h" #include "SheetsDebug.h" #include "Function.h" #include "FunctionRepository.h" -#include +#include #include #ifndef SHEETS_NO_PLUGINMODULES #include #include #include #else #include "functions/BitOpsModule.h" #include "functions/ConversionModule.h" #include "functions/DatabaseModule.h" #include "functions/DateTimeModule.h" #include "functions/EngineeringModule.h" #include "functions/FinancialModule.h" #include "functions/InformationModule.h" #include "functions/LogicModule.h" #include "functions/MathModule.h" #include "functions/ReferenceModule.h" #include "functions/StatisticalModule.h" #include "functions/TextModule.h" #include "functions/TrigonometryModule.h" #endif +Q_GLOBAL_STATIC(Calligra::Sheets::FunctionModuleRegistry, s_instance) + using namespace Calligra::Sheets; class Q_DECL_HIDDEN FunctionModuleRegistry::Private { public: void registerFunctionModule(FunctionModule* module); void removeFunctionModule(FunctionModule* module); public: bool repositoryInitialized; }; void FunctionModuleRegistry::Private::registerFunctionModule(FunctionModule* module) { const QList > functions = module->functions(); for (int i = 0; i < functions.count(); ++i) { FunctionRepository::self()->add(functions[i]); } Q_ASSERT(!module->descriptionFileName().isEmpty()); const QString fileName = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("calligrasheets/functions/")+module->descriptionFileName()); if (fileName.isEmpty()) { debugSheetsFormula << module->descriptionFileName() << "not found."; return; } FunctionRepository::self()->loadFunctionDescriptions(fileName); } void FunctionModuleRegistry::Private::removeFunctionModule(FunctionModule* module) { const QList > functions = module->functions(); for (int i = 0; i < functions.count(); ++i) { FunctionRepository::self()->remove(functions[i]); } } FunctionModuleRegistry::FunctionModuleRegistry() : d(new Private) { d->repositoryInitialized = false; } FunctionModuleRegistry::~FunctionModuleRegistry() { foreach(const QString &id, keys()) { get(id)->deleteLater(); } qDeleteAll(doubleEntries()); delete d; } FunctionModuleRegistry* FunctionModuleRegistry::instance() { - K_GLOBAL_STATIC(FunctionModuleRegistry, s_instance) return s_instance; } void FunctionModuleRegistry::loadFunctionModules() { #ifndef SHEETS_NO_PLUGINMODULES const QList offers = KoPluginLoader::pluginLoaders(QStringLiteral("calligrasheets/functions")); debugSheetsFormula << offers.count() << "function modules found."; const KConfigGroup pluginsConfigGroup = KSharedConfig::openConfig()->group("Plugins"); foreach (QPluginLoader *loader, offers) { QJsonObject metaData = loader->metaData().value("MetaData").toObject(); int version = metaData.value("X-CalligraSheets-InterfaceVersion").toInt(); if (version != 0) { debugSheetsFormula << "Skipping" << loader->fileName() << ", because interface version is" << version; continue; } QJsonObject pluginData = metaData.value("KPlugin").toObject(); QString category = pluginData.value("Category").toString(); if (category != "FunctionModule") { debugSheetsFormula << "Skipping" << loader->fileName() << ", because category is " << category; continue; } const QString pluginId = pluginData.value("Id").toString(); const QString pluginConfigEnableKey = pluginId + QLatin1String("Enabled"); const bool isPluginEnabled = pluginsConfigGroup.hasKey(pluginConfigEnableKey) ? pluginsConfigGroup.readEntry(pluginConfigEnableKey, true) : pluginData.value("EnabledByDefault").toBool(true); if (isPluginEnabled) { if(contains(pluginId)) { continue; } // Plugin enabled, but not registered. Add it. KPluginFactory* const factory = qobject_cast(loader->instance()); if (!factory) { debugSheetsFormula << "Unable to create plugin factory for" << loader->fileName(); continue; } FunctionModule* const module = qobject_cast(factory->create()); if (!module) { debugSheetsFormula << "Unable to create function module for" << loader->fileName(); continue; } add(pluginId, module); debugSheetsFormula << "Loaded" << pluginId; // Delays the function registration until the user needs one. if (d->repositoryInitialized) { d->registerFunctionModule(module); } } else { if (!contains(pluginId)) { continue; } // Plugin disabled, but registered. Remove it. FunctionModule* const module = get(pluginId); // Delay the function registration until the user needs one. if (d->repositoryInitialized) { d->removeFunctionModule(module); } remove(pluginId); if (module->isRemovable()) { delete module; KPluginFactory* factory = qobject_cast(loader->instance()); delete factory; loader->unload(); } else { // Put it back in. add(pluginId, module); // Delay the function registration until the user needs one. if (d->repositoryInitialized) { d->registerFunctionModule(module); } } } } qDeleteAll(offers); #else QList modules; QObject *parent = 0; modules << new BitOpsModule(parent); modules << new ConversionModule(parent); modules << new DatabaseModule(parent); modules << new DateTimeModule(parent); modules << new EngineeringModule(parent); modules << new FinancialModule(parent); modules << new InformationModule(parent); modules << new LogicModule(parent); modules << new MathModule(parent); modules << new ReferenceModule(parent); modules << new StatisticalModule(parent); modules << new TextModule(parent); modules << new TrigonometryModule(parent); Q_FOREACH(FunctionModule* module, modules) { add(module->id(), module); d->registerFunctionModule(module); } #endif } void FunctionModuleRegistry::registerFunctions() { d->repositoryInitialized = true; const QList modules = values(); for (int i = 0; i < modules.count(); ++i) { d->registerFunctionModule(modules[i]); } } diff --git a/sheets/FunctionRepository.cpp b/sheets/FunctionRepository.cpp index b02bc3345c3..f0f9e372be7 100644 --- a/sheets/FunctionRepository.cpp +++ b/sheets/FunctionRepository.cpp @@ -1,197 +1,198 @@ /* This file is part of the KDE project Copyright (C) 2003,2004 Ariya Hidayat Copyright (C) 2005 Tomas Mecir This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; only version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ // Local #include "FunctionRepository.h" #include "SheetsDebug.h" #include "Function.h" #include "FunctionDescription.h" #include "FunctionModuleRegistry.h" #include #include #include #include -#include +#include #include +Q_GLOBAL_STATIC(Calligra::Sheets::FunctionRepository, s_instance) + using namespace Calligra::Sheets; class Q_DECL_HIDDEN FunctionRepository::Private { public: QHash > functions; QHash > alternates; QHash descriptions; QStringList groups; bool initialized; }; FunctionRepository* FunctionRepository::self() { - K_GLOBAL_STATIC(FunctionRepository, s_instance) if (!s_instance.exists()) { *s_instance; // creates the global instance // register all existing functions FunctionModuleRegistry::instance()->registerFunctions(); #ifndef NDEBUG debugSheetsUI << "functions registered:" << s_instance->d->functions.count() << "descriptions loaded:" << s_instance->d->descriptions.count(); // Verify, that every function has a description. QStringList missingDescriptions; typedef QHash > Functions; Functions::ConstIterator end = s_instance->d->functions.constEnd(); for (Functions::ConstIterator it = s_instance->d->functions.constBegin(); it != end; ++it) { if (!s_instance->d->descriptions.contains(it.key())) missingDescriptions << it.key(); } if (missingDescriptions.count() > 0) { debugSheetsUI << "No function descriptions found for:"; foreach(const QString& missingDescription, missingDescriptions) { debugSheetsUI << "\t" << missingDescription; } } #endif } return s_instance; } FunctionRepository::FunctionRepository() : d(new Private) { d->initialized = false; } FunctionRepository::~FunctionRepository() { qDeleteAll(d->descriptions); delete d; } void FunctionRepository::add(const QSharedPointer& function) { if (!function) return; d->functions.insert(function->name().toUpper(), function); if (!function->alternateName().isNull()) { d->alternates.insert(function->alternateName().toUpper(), function); } } void FunctionRepository::add(FunctionDescription *desc) { if (!desc) return; if (!d->functions.contains(desc->name())) return; d->descriptions.insert(desc->name(), desc); } void FunctionRepository::remove(const QSharedPointer& function) { const QString functionName = function->name().toUpper(); delete d->descriptions.take(functionName); if (d->functions.contains(functionName)) { QSharedPointer function = d->functions.take(functionName); d->alternates.remove(function->alternateName().toUpper()); } } QSharedPointer FunctionRepository::function(const QString& name) { const QString n = name.toUpper(); QSharedPointer f = d->functions.value(n); return !f.isNull() ? f : d->alternates.value(n); } FunctionDescription *FunctionRepository::functionInfo(const QString& name) { return d->descriptions.value(name.toUpper()); } // returns names of function in certain group QStringList FunctionRepository::functionNames(const QString& group) { QStringList lst; foreach(FunctionDescription* description, d->descriptions) { if (group.isNull() || (description->group() == group)) lst.append(description->name()); } lst.sort(); return lst; } const QStringList& FunctionRepository::groups() const { return d->groups; } void FunctionRepository::addGroup(const QString& groupname) { d->groups.append(groupname); d->groups.sort(); } void FunctionRepository::loadFunctionDescriptions(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly)) return; QDomDocument doc; doc.setContent(&file); file.close(); QString group; QDomNode n = doc.documentElement().firstChild(); for (; !n.isNull(); n = n.nextSibling()) { if (!n.isElement()) continue; QDomElement e = n.toElement(); if (e.tagName() == "Group") { group = i18n(e.namedItem("GroupName").toElement().text().toUtf8()); addGroup(group); QDomNode n2 = e.firstChild(); for (; !n2.isNull(); n2 = n2.nextSibling()) { if (!n2.isElement()) continue; QDomElement e2 = n2.toElement(); if (e2.tagName() == "Function") { FunctionDescription* desc = new FunctionDescription(e2); desc->setGroup(group); if (d->functions.contains(desc->name())) d->descriptions.insert(desc->name(), desc); else { debugSheetsUI << "Description for unknown function" << desc->name() << "found."; delete desc; } } } group.clear(); } } } diff --git a/sheets/dialogs/AutoFormatDialog.cpp b/sheets/dialogs/AutoFormatDialog.cpp index 932bd451d8c..11ee4dc8d7f 100644 --- a/sheets/dialogs/AutoFormatDialog.cpp +++ b/sheets/dialogs/AutoFormatDialog.cpp @@ -1,204 +1,203 @@ /* This file is part of the KDE project Copyright 2007 Stefan Nikolaus Copyright 2002-2003 Norbert Andres Copyright 2002 Ariya Hidayat Copyright 2002 John Dailey Copyright 2001-2002 Philipp Mueller Copyright 2000-2002 Laurent Montel Copyright 2000 Werner Trobin Copyright 1998-2000 Torben Weis This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ // Local #include "AutoFormatDialog.h" #include #include #include #include #include -#include #include #include #include #include #include "Cell.h" #include "CellStorage.h" #include "Localization.h" #include "ui/Selection.h" #include "Sheet.h" #include "Style.h" #include "StyleManager.h" #include "commands/AutoFormatCommand.h" using namespace Calligra::Sheets; struct Entry { QString xml; QString image; QString config; QString name; }; class AutoFormatDialog::Private { public: Selection* selection; KComboBox* combo; QLabel* label; QList entries; QList