diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/CMakeLists.txt | 41 | ||||
-rw-r--r-- | src/gui/kconfiggroupgui.cpp | 197 | ||||
-rw-r--r-- | src/gui/kconfiggui.cpp | 51 | ||||
-rw-r--r-- | src/gui/kconfiggui.h | 58 | ||||
-rw-r--r-- | src/gui/kconfigloader.cpp | 442 | ||||
-rw-r--r-- | src/gui/kconfigloader.h | 176 | ||||
-rw-r--r-- | src/gui/kconfigloader_p.h | 222 | ||||
-rw-r--r-- | src/gui/kconfigloaderhandler_p.h | 68 | ||||
-rw-r--r-- | src/gui/kconfigskeleton.cpp | 121 | ||||
-rw-r--r-- | src/gui/kconfigskeleton.h | 140 | ||||
-rw-r--r-- | src/gui/kstandardshortcut.cpp | 377 | ||||
-rw-r--r-- | src/gui/kstandardshortcut.h | 482 | ||||
-rw-r--r-- | src/gui/kwindowconfig.cpp | 83 | ||||
-rw-r--r-- | src/gui/kwindowconfig.h | 58 |
14 files changed, 2516 insertions, 0 deletions
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt new file mode 100644 index 00000000..974b05cc --- /dev/null +++ b/src/gui/CMakeLists.txt @@ -0,0 +1,41 @@ + +find_package(Qt5Widgets 5.2.0 REQUIRED NO_MODULE) +find_package(Qt5Xml 5.2.0 REQUIRED NO_MODULE) + +set(libkconfiggui_SRCS + kconfiggui.cpp + kconfiggroupgui.cpp + kconfigloader.cpp + kconfigskeleton.cpp + kstandardshortcut.cpp + kwindowconfig.cpp +) + +add_library(KF5ConfigGui ${libkconfiggui_SRCS}) +generate_export_header(KF5ConfigGui BASE_NAME KConfigGui) +add_library(KF5::ConfigGui ALIAS KF5ConfigGui) + +target_link_libraries(KF5ConfigGui PUBLIC Qt5::Gui Qt5::Xml KF5::ConfigCore) + +if(IS_ABSOLUTE "${INCLUDE_INSTALL_DIR}") + target_include_directories(KF5ConfigGui INTERFACE "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>" ) +else() + target_include_directories(KF5ConfigGui INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${INCLUDE_INSTALL_DIR}>" ) +endif() + +set_target_properties(KF5ConfigGui PROPERTIES VERSION ${KCONFIG_VERSION_STRING} + SOVERSION ${KCONFIG_SOVERSION} + EXPORT_NAME ConfigGui +) + +install(TARGETS KF5ConfigGui EXPORT KF5ConfigTargets ${INSTALL_TARGETS_DEFAULT_ARGS}) + +install( FILES + ${CMAKE_CURRENT_BINARY_DIR}/kconfiggui_export.h + kconfiggui.h + kconfigloader.h + kconfigskeleton.h + kstandardshortcut.h + kwindowconfig.h + DESTINATION ${INCLUDE_INSTALL_DIR} COMPONENT Devel +) diff --git a/src/gui/kconfiggroupgui.cpp b/src/gui/kconfiggroupgui.cpp new file mode 100644 index 00000000..22706e77 --- /dev/null +++ b/src/gui/kconfiggroupgui.cpp @@ -0,0 +1,197 @@ +/* + This file is part of the KDE libraries + Copyright (c) 2007 Thiago Macieira <thiago@kde.org> + + 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 <kconfiggroup.h> + +#include <QtCore/QMutableStringListIterator> +#include <QColor> +#include <QDebug> +#include <QFont> + +#include <kconfiggroup_p.h> + +/** + * Try to read a GUI type from config group @p cg at key @p key. + * @p input is the default value and also indicates the type to be read. + * @p output is to be set with the value that has been read. + * + * @returns true if something was handled (even if output was set to clear or default) + * or false if nothing was handled (e.g., Core type) + */ +static bool readEntryGui(const QByteArray& data, const char* key, const QVariant &input, + QVariant &output) +{ + const QString errString = QString::fromLatin1("\"%1\" - conversion from \"%3\" to %2 failed") + .arg(QLatin1String(key)) + .arg(QLatin1String(QVariant::typeToName(input.type()))) + .arg(QLatin1String(data.constData())); + const QString formatError = QString::fromLatin1(" (wrong format: expected '%1' items, read '%2')"); + + // set in case of failure + output = input; + + switch (input.type()) { + case QVariant::Color: { + if (data.isEmpty() || data == "invalid") { + output = QColor(); // return what was stored + return true; + } else if (data.at(0) == '#') { + QColor col; + col.setNamedColor(QString::fromUtf8(data.constData(), data.length())); + output = col; + return true; + } else if (!data.contains(',')) { + QColor col; + col.setNamedColor(QString::fromUtf8(data.constData(), data.length())); + if (!col.isValid()) + qCritical() << qPrintable(errString); + output = col; + return true; + } else { + const QList<QByteArray> list = data.split(','); + const int count = list.count(); + + if (count != 3 && count != 4) { + qCritical() << qPrintable(errString) << qPrintable(formatError.arg(QLatin1String("3' or '4")).arg(count)); + return true; // return default + } + + int temp[4]; + // bounds check components + for(int i = 0; i < count; i++) { + bool ok; + const int j = temp[i] = list.at(i).toInt(&ok); + if (!ok) { // failed to convert to int + qCritical() << qPrintable(errString) << " (integer conversion failed)"; + return true; // return default + } + if (j < 0 || j > 255) { + static const char *const components[6] = { + "red", "green", "blue", "alpha" + }; + const QString boundsError = QLatin1String(" (bounds error: %1 component %2)"); + qCritical() << qPrintable(errString) + << qPrintable(boundsError.arg(QLatin1String(components[i])).arg(j < 0? QLatin1String("< 0"): QLatin1String("> 255"))); + return true; // return default + } + } + QColor aColor(temp[0], temp[1], temp[2]); + if (count == 4) + aColor.setAlpha(temp[3]); + + if (aColor.isValid()) + output = aColor; + else + qCritical() << qPrintable(errString); + return true; + } + } + + case QVariant::Font: { + QVariant tmp = QString::fromUtf8(data.constData(), data.length()); + if (tmp.convert(QVariant::Font)) + output = tmp; + else + qCritical() << qPrintable(errString); + return true; + } + case QVariant::Pixmap: + case QVariant::Image: + case QVariant::Brush: + case QVariant::Palette: + case QVariant::Icon: + case QVariant::Region: + case QVariant::Bitmap: + case QVariant::Cursor: + case QVariant::SizePolicy: + case QVariant::Pen: + // we may want to handle these in the future + + default: + break; + } + + return false; // not handled +} + +/** + * Try to write a GUI type @p prop to config group @p cg at key @p key. + * + * @returns true if something was handled (even if an empty value was written) + * or false if nothing was handled (e.g., Core type) + */ +static bool writeEntryGui(KConfigGroup *cg, const char* key, const QVariant &prop, + KConfigGroup::WriteConfigFlags pFlags) +{ + switch (prop.type()) { + case QVariant::Color: { + const QColor rColor = prop.value<QColor>(); + + if (!rColor.isValid()) { + cg->writeEntry(key, "invalid", pFlags); + return true; + } + + QList<int> list; + list.insert(0, rColor.red()); + list.insert(1, rColor.green()); + list.insert(2, rColor.blue()); + if (rColor.alpha() != 255) + list.insert(3, rColor.alpha()); + + cg->writeEntry( key, list, pFlags ); + return true; + } + case QVariant::Font: + cg->writeEntry( key, prop.toString().toUtf8(), pFlags ); + return true; + + case QVariant::Pixmap: + case QVariant::Image: + case QVariant::Brush: + case QVariant::Palette: + case QVariant::Icon: + case QVariant::Region: + case QVariant::Bitmap: + case QVariant::Cursor: + case QVariant::SizePolicy: + case QVariant::Pen: + // we may want to handle one of these in the future + break; + + default: + break; + } + + return false; +} + +static int initKConfigGroupGui() +{ + _kde_internal_KConfigGroupGui.readEntryGui = readEntryGui; + _kde_internal_KConfigGroupGui.writeEntryGui = writeEntryGui; + return 42; // because 42 is nicer than 1 or 0 +} + +#ifdef Q_CONSTRUCTOR_FUNCTION +Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui) +#else +static int dummyKConfigGroupGui = initKConfigGroupGui(); +#endif diff --git a/src/gui/kconfiggui.cpp b/src/gui/kconfiggui.cpp new file mode 100644 index 00000000..88da6b56 --- /dev/null +++ b/src/gui/kconfiggui.cpp @@ -0,0 +1,51 @@ +/* + This file is part of the KDE libraries + Copyright (c) 1999 Matthias Ettrich <ettrich@kde.org> + + 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 "kconfiggui.h" + +#include <QGuiApplication> + +#include <kconfig.h> + +static KConfig* s_sessionConfig = 0; + +KConfig* KConfigGui::sessionConfig() +{ + if (!s_sessionConfig) // create an instance specific config object + s_sessionConfig = new KConfig( sessionConfigName(), KConfig::SimpleConfig ); + return s_sessionConfig; +} + +bool KConfigGui::hasSessionConfig() +{ + return s_sessionConfig != 0; +} + +QString KConfigGui::sessionConfigName() +{ +#ifdef QT_NO_SESSIONMANAGER +#error QT_NO_SESSIONMANAGER was set, this will not compile. Reconfigure Qt with Session management support. +#endif + const QString sessionKey = qApp->sessionKey(); + const QString sessionId = qApp->sessionId(); + return QString(QLatin1String("session/%1_%2_%3")).arg(QGuiApplication::applicationName()).arg(sessionId).arg(sessionKey); +} + diff --git a/src/gui/kconfiggui.h b/src/gui/kconfiggui.h new file mode 100644 index 00000000..4e2313f3 --- /dev/null +++ b/src/gui/kconfiggui.h @@ -0,0 +1,58 @@ +/* + This file is part of the KDE libraries + Copyright (c) 1999 Matthias Ettrich <ettrich@kde.org> + + 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/>. +*/ + +#ifndef KCONFIGGUI_H +#define KCONFIGGUI_H + +#include <kconfiggui_export.h> + +#include <QString> + +class KConfig; + +namespace KConfigGui +{ + /** + * Returns the application session config object. + * + * @return A pointer to the application's instance specific + * KConfig object. + * @see KConfig + */ + KCONFIGGUI_EXPORT KConfig* sessionConfig(); + + /** + * Indicates if a session config has been created for that application + * (ie. if sessionConfig() got called at least once) + * + * @return true if a sessionConfig object was created, false otherwise + */ + KCONFIGGUI_EXPORT bool hasSessionConfig(); + + /** + * Returns the name of the application session + * + * @return the application session name + */ + KCONFIGGUI_EXPORT QString sessionConfigName(); +} + +#endif // KCONFIGGUI_H diff --git a/src/gui/kconfigloader.cpp b/src/gui/kconfigloader.cpp new file mode 100644 index 00000000..150c6b69 --- /dev/null +++ b/src/gui/kconfigloader.cpp @@ -0,0 +1,442 @@ +/* + * Copyright 2007 Aaron Seigo <aseigo@kde.org> + * + * This program 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, or + * (at your option) any later version. + * + * This program 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "kconfigloader.h" +#include "kconfigloader_p.h" +#include "kconfigloaderhandler_p.h" + +#include <QColor> +#include <QFont> +#include <QHash> +#include <QXmlContentHandler> +#include <QXmlInputSource> +#include <QXmlSimpleReader> +#include <QUrl> + +#include <QDebug> + +void ConfigLoaderPrivate::parse(KConfigLoader *loader, QIODevice *xml) +{ + clearData(); + loader->clearItems(); + + if (xml) { + QXmlInputSource source(xml); + QXmlSimpleReader reader; + ConfigLoaderHandler handler(loader, this); + reader.setContentHandler(&handler); + reader.parse(&source, false); + } +} + +ConfigLoaderHandler::ConfigLoaderHandler(KConfigLoader *config, ConfigLoaderPrivate *d) + : QXmlDefaultHandler(), + m_config(config), + d(d) +{ + resetState(); +} + +bool ConfigLoaderHandler::startElement(const QString &namespaceURI, const QString &localName, + const QString &qName, const QXmlAttributes &attrs) +{ + Q_UNUSED(namespaceURI) + Q_UNUSED(qName) + +// qDebug() << "ConfigLoaderHandler::startElement(" << localName << qName; + int numAttrs = attrs.count(); + QString tag = localName.toLower(); + if (tag == QStringLiteral("group")) { + QString group; + for (int i = 0; i < numAttrs; ++i) { + QString name = attrs.localName(i).toLower(); + if (name == QStringLiteral("name")) { + //qDebug() << "set group to" << attrs.value(i); + group = attrs.value(i); + } + } + if (group.isEmpty()) { + group = d->baseGroup; + } else { + d->groups.append(group); + if (!d->baseGroup.isEmpty()) { + group = d->baseGroup + QStringLiteral("\x1d") + group; + } + } + m_currentGroup = group; + if (m_config) { + m_config->setCurrentGroup(group); + } + } else if (tag == QStringLiteral("entry")) { + for (int i = 0; i < numAttrs; ++i) { + QString name = attrs.localName(i).toLower(); + if (name == QStringLiteral("name")) { + m_name = attrs.value(i).trimmed(); + } else if (name == QStringLiteral("type")) { + m_type = attrs.value(i).toLower(); + } else if (name == QStringLiteral("key")) { + m_key = attrs.value(i).trimmed(); + } + } + } else if (tag == QStringLiteral("choice")) { + m_choice.name.clear(); + m_choice.label.clear(); + m_choice.whatsThis.clear(); + for (int i = 0; i < numAttrs; ++i) { + QString name = attrs.localName(i).toLower(); + if (name == QStringLiteral("name")) { + m_choice.name = attrs.value(i); + } + } + m_inChoice = true; + } + + return true; +} + +bool ConfigLoaderHandler::characters(const QString &ch) +{ + m_cdata.append(ch); + return true; +} + +QString ConfigLoaderHandler::name() const +{ + return m_name; +} + +void ConfigLoaderHandler::setName(const QString &name) +{ + m_name = name; +} + +QString ConfigLoaderHandler::key() const +{ + return m_key; +} + +void ConfigLoaderHandler::setKey(const QString &key) +{ + m_key = key; +} + +QString ConfigLoaderHandler::type() const +{ + return m_type; +} + +QString ConfigLoaderHandler::currentGroup() const +{ + return m_currentGroup; +} + +QString ConfigLoaderHandler::defaultValue() const +{ + return m_default; +} + +bool ConfigLoaderHandler::endElement(const QString &namespaceURI, + const QString &localName, const QString &qName) +{ + Q_UNUSED(namespaceURI) + Q_UNUSED(qName) + +// qDebug() << "ConfigLoaderHandler::endElement(" << localName << qName; + const QString tag = localName.toLower(); + if (tag == QStringLiteral("entry")) { + addItem(); + resetState(); + } else if (tag == QStringLiteral("label")) { + if (m_inChoice) { + m_choice.label = m_cdata.trimmed(); + } else { + m_label = m_cdata.trimmed(); + } + } else if (tag == QStringLiteral("whatsthis")) { + if (m_inChoice) { + m_choice.whatsThis = m_cdata.trimmed(); + } else { + m_whatsThis = m_cdata.trimmed(); + } + } else if (tag == QStringLiteral("default")) { + m_default = m_cdata.trimmed(); + } else if (tag == QStringLiteral("min")) { + m_min = m_cdata.toInt(&m_haveMin); + } else if (tag == QStringLiteral("max")) { + m_max = m_cdata.toInt(&m_haveMax); + } else if (tag == QStringLiteral("choice")) { + m_enumChoices.append(m_choice); + m_inChoice = false; + } + + m_cdata.clear(); + return true; +} + +void ConfigLoaderHandler::addItem() +{ + if (m_name.isEmpty()) { + if (m_key.isEmpty()) { + return; + } + + m_name = m_key; + } + + m_name.remove(QStringLiteral(" ")); + + KConfigSkeletonItem *item = 0; + + if (m_type == QStringLiteral("bool")) { + bool defaultValue = m_default.toLower() == QStringLiteral("true"); + item = m_config->addItemBool(m_name, *d->newBool(), defaultValue, m_key); + } else if (m_type == QStringLiteral("color")) { + item = m_config->addItemColor(m_name, *d->newColor(), QColor(m_default), m_key); + } else if (m_type == QStringLiteral("datetime")) { + item = m_config->addItemDateTime(m_name, *d->newDateTime(), + QDateTime::fromString(m_default), m_key); + } else if (m_type == QStringLiteral("enum")) { + m_key = (m_key.isEmpty()) ? m_name : m_key; + KConfigSkeleton::ItemEnum *enumItem = + new KConfigSkeleton::ItemEnum(m_config->currentGroup(), + m_key, *d->newInt(), + m_enumChoices, + m_default.toUInt()); + m_config->addItem(enumItem, m_name); + item = enumItem; + } else if (m_type == QStringLiteral("font")) { + item = m_config->addItemFont(m_name, *d->newFont(), QFont(m_default), m_key); + } else if (m_type == QStringLiteral("int")) { + KConfigSkeleton::ItemInt *intItem = m_config->addItemInt(m_name, *d->newInt(), + m_default.toInt(), m_key); + + if (m_haveMin) { + intItem->setMinValue(m_min); + } + + if (m_haveMax) { + intItem->setMaxValue(m_max); + } + + item = intItem; + } else if (m_type == QStringLiteral("password")) { + item = m_config->addItemPassword(m_name, *d->newString(), m_default, m_key); + } else if (m_type == QStringLiteral("path")) { + item = m_config->addItemPath(m_name, *d->newString(), m_default, m_key); + } else if (m_type == QStringLiteral("string")) { + item = m_config->addItemString(m_name, *d->newString(), m_default, m_key); + } else if (m_type == QStringLiteral("stringlist")) { + //FIXME: the split() is naive and will break on lists with ,'s in them + item = m_config->addItemStringList(m_name, *d->newStringList(), + m_default.split(QStringLiteral(",")), m_key); + } else if (m_type == QStringLiteral("uint")) { + KConfigSkeleton::ItemUInt *uintItem = + m_config->addItemUInt(m_name, *d->newUint(), m_default.toUInt(), m_key); + if (m_haveMin) { + uintItem->setMinValue(m_min); + } + if (m_haveMax) { + uintItem->setMaxValue(m_max); + } + item = uintItem; + } else if (m_type == QStringLiteral("url")) { + m_key = (m_key.isEmpty()) ? m_name : m_key; + KConfigSkeleton::ItemUrl *urlItem = + new KConfigSkeleton::ItemUrl(m_config->currentGroup(), + m_key, *d->newUrl(), + QUrl::fromUserInput(m_default)); + m_config->addItem(urlItem, m_name); + item = urlItem; + } else if (m_type == QStringLiteral("double")) { + KConfigSkeleton::ItemDouble *doubleItem = m_config->addItemDouble(m_name, + *d->newDouble(), m_default.toDouble(), m_key); + if (m_haveMin) { + doubleItem->setMinValue(m_min); + } + if (m_haveMax) { + doubleItem->setMaxValue(m_max); + } + item = doubleItem; + } else if (m_type == QStringLiteral("intlist")) { + QStringList tmpList = m_default.split(QStringLiteral(",")); + QList<int> defaultList; + foreach (const QString &tmp, tmpList) { + defaultList.append(tmp.toInt()); + } + item = m_config->addItemIntList(m_name, *d->newIntList(), defaultList, m_key); + } else if (m_type == QStringLiteral("longlong")) { + KConfigSkeleton::ItemLongLong *longlongItem = m_config->addItemLongLong(m_name, + *d->newLongLong(), m_default.toLongLong(), m_key); + if (m_haveMin) { + longlongItem->setMinValue(m_min); + } + if (m_haveMax) { + longlongItem->setMaxValue(m_max); + } + item = longlongItem; + /* No addItemPathList in KConfigSkeleton ? + } else if (m_type == "PathList") { + //FIXME: the split() is naive and will break on lists with ,'s in them + item = m_config->addItemPathList(m_name, *d->newStringList(), m_default.split(","), m_key); + */ + } else if (m_type == QStringLiteral("point")) { + QPoint defaultPoint; + QStringList tmpList = m_default.split(QStringLiteral(",")); + if (tmpList.size() >= 2) { + defaultPoint.setX(tmpList[0].toInt()); + defaultPoint.setY(tmpList[1].toInt()); + } + item = m_config->addItemPoint(m_name, *d->newPoint(), defaultPoint, m_key); + } else if (m_type == QStringLiteral("rect")) { + QRect defaultRect; + QStringList tmpList = m_default.split(QStringLiteral(",")); + if (tmpList.size() >= 4) { + defaultRect.setCoords(tmpList[0].toInt(), tmpList[1].toInt(), + tmpList[2].toInt(), tmpList[3].toInt()); + } + item = m_config->addItemRect(m_name, *d->newRect(), defaultRect, m_key); + } else if (m_type == QStringLiteral("size")) { + QSize defaultSize; + QStringList tmpList = m_default.split(QStringLiteral(",")); + if (tmpList.size() >= 2) { + defaultSize.setWidth(tmpList[0].toInt()); + defaultSize.setHeight(tmpList[1].toInt()); + } + item = m_config->addItemSize(m_name, *d->newSize(), defaultSize, m_key); + } else if (m_type == QStringLiteral("ulonglong")) { + KConfigSkeleton::ItemULongLong *ulonglongItem = + m_config->addItemULongLong(m_name, *d->newULongLong(), m_default.toULongLong(), m_key); + if (m_haveMin) { + ulonglongItem->setMinValue(m_min); + } + if (m_haveMax) { + ulonglongItem->setMaxValue(m_max); + } + item = ulonglongItem; + /* No addItemUrlList in KConfigSkeleton ? + } else if (m_type == "urllist") { + //FIXME: the split() is naive and will break on lists with ,'s in them + QStringList tmpList = m_default.split(","); + QList<QUrl> defaultList; + foreach (const QString& tmp, tmpList) { + defaultList.append(QUrl(tmp)); + } + item = m_config->addItemUrlList(m_name, *d->newUrlList(), defaultList, m_key);*/ + } + + if (item) { + item->setLabel(m_label); + item->setWhatsThis(m_whatsThis); + d->keysToNames.insert(item->group() + item->key(), item->name()); + } +} + +void ConfigLoaderHandler::resetState() +{ + m_haveMin = false; + m_min = 0; + m_haveMax = false; + m_max = 0; + m_name.clear(); + m_type.clear(); + m_label.clear(); + m_default.clear(); + m_key.clear(); + m_whatsThis.clear(); + m_enumChoices.clear(); + m_inChoice = false; +} + +KConfigLoader::KConfigLoader(const QString &configFile, QIODevice *xml, QObject *parent) + : KConfigSkeleton(configFile, parent), + d(new ConfigLoaderPrivate) +{ + d->parse(this, xml); +} + +KConfigLoader::KConfigLoader(KSharedConfigPtr config, QIODevice *xml, QObject *parent) + : KConfigSkeleton(config, parent), + d(new ConfigLoaderPrivate) +{ + d->parse(this, xml); +} + +//FIXME: obviously this is broken and should be using the group as the root, +// but KConfigSkeleton does not currently support this. it will eventually though, +// at which point this can be addressed properly +KConfigLoader::KConfigLoader(const KConfigGroup &config, QIODevice *xml, QObject *parent) + : KConfigSkeleton(KSharedConfig::openConfig(config.config()->name()), parent), + d(new ConfigLoaderPrivate) +{ + KConfigGroup group = config.parent(); + d->baseGroup = config.name(); + while (group.isValid() && group.name() != QStringLiteral("<default>")) { + d->baseGroup = group.name() + QStringLiteral("\x1d") + d->baseGroup; + group = group.parent(); + } + d->parse(this, xml); +} + +KConfigLoader::~KConfigLoader() +{ + delete d; +} + +KConfigSkeletonItem *KConfigLoader::findItem(const QString &group, const QString &key) const +{ + return KConfigSkeleton::findItem(d->keysToNames[group + key]); +} + +KConfigSkeletonItem *KConfigLoader::findItemByName(const QString &name) const +{ + return KConfigSkeleton::findItem(name); +} + +QVariant KConfigLoader::property(const QString &name) const +{ + KConfigSkeletonItem *item = KConfigSkeleton::findItem(name); + + if (item) { + return item->property(); + } + + return QVariant(); +} + +bool KConfigLoader::hasGroup(const QString &group) const +{ + return d->groups.contains(group); +} + +QStringList KConfigLoader::groupList() const +{ + return d->groups; +} + +bool KConfigLoader::usrWriteConfig() +{ + if (d->saveDefaults) { + KConfigSkeletonItem::List itemList = items(); + for(int i = 0; i < itemList.size(); i++) { + KConfigGroup cg(config(), itemList.at(i)->group()); + cg.writeEntry(itemList.at(i)->key(), ""); + } + } + return true; +} diff --git a/src/gui/kconfigloader.h b/src/gui/kconfigloader.h new file mode 100644 index 00000000..df38ce79 --- /dev/null +++ b/src/gui/kconfigloader.h @@ -0,0 +1,176 @@ +/* + * Copyright 2007 Aaron Seigo <aseigo@kde.org> + * + * This program 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, or + * (at your option) any later version. + * + * This program 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef KCONFIGLOADER_H +#define KCONFIGLOADER_H + +#include <kconfiggroup.h> +#include <kconfigskeleton.h> +#include <ksharedconfig.h> + +#include <kconfiggui_export.h> + +class ConfigLoaderPrivate; + +/** + * @class KConfigLoader kconfigloader.h <KConfigLoader> + * + * @short A KConfigSkeleton that populates itself based on KConfigXT XML + * + * This class allows one to ship an XML file and reconstitute it into a + * KConfigSkeleton object at runtime. Common usage might look like this: + * + * \code + * QFile file(xmlFilePath); + * KConfigLoader appletConfig(configFilePath, &file); + * \endcode + * + * Alternatively, any QIODevice may be used in place of QFile in the + * example above. + * + * KConfigLoader is useful if it is not possible to use compiled code + * and by that the kconfig compiler cannot be used. Common examples are + * scripted plugins which want to provide a configuration interface. + * With the help of KConfigLoader a dynamically loaded ui file can be + * populated with the stored values and also stored back to the config + * file. + * + * An example for populating a QDialog with a dynamically populated UI + * with the help of a KConfigDialogManager: + * \code + * QDialog *dialog = new QDialog(); + * QFile xmlFile("path/to/kconfigxt.xml"); + * KConfigGroup cg = KSharedConfig::openConfig()->group(QString()); + * KConfigLoader *configLoader = new KConfigLoader(cg, &xmlFile, this); + * + * // load the ui file + * QUiLoader *loader = new QUiLoader(this); + * QFile uiFile("path/to/userinterface.ui"); + * uiFile.open(QFile::ReadOnly); + * QWidget *customConfigForm = loader->load(&uiFile, dialog); + * uiFile.close(); + * + * KConfigDialogManager *manager = new KConfigDialogManager(customConfigForm, configLoader); + * if (dialog->exec() == QDialog::Accepted) { + * manager->updateSettings(); + * } + * \endcode + * + * Currently the following data types are supported: + * + * @li bools + * @li colors + * @li datetimes + * @li enumerations + * @li fonts + * @li ints + * @li passwords + * @li paths + * @li strings + * @li stringlists + * @li uints + * @li urls + * @li doubles + * @li int lists + * @li longlongs + * @li path lists + * @li points + * @li rects + * @li sizes + * @li ulonglongs + * @li url lists + **/ +class KCONFIGGUI_EXPORT KConfigLoader : public KConfigSkeleton +{ +public: + /** + * Creates a KConfigSkeleton populated using the definition found in + * the XML data passed in. + * + * @param configFile path to the configuration file to use + * @param xml the xml data; must be valid KConfigXT data + * @param parent optional QObject parent + **/ + KConfigLoader(const QString &configFile, QIODevice *xml, QObject *parent = 0); + + /** + * Creates a KConfigSkeleton populated using the definition found in + * the XML data passed in. + * + * @param config the configuration object to use + * @param xml the xml data; must be valid KConfigXT data + * @param parent optional QObject parent + **/ + KConfigLoader(KSharedConfigPtr config, QIODevice *xml, QObject *parent = 0); + + /** + * Creates a KConfigSkeleton populated using the definition found in + * the XML data passed in. + * + * @param config the group to use as the root for configuration items + * @param xml the xml data; must be valid KConfigXT data + * @param parent optional QObject parent + **/ + KConfigLoader(const KConfigGroup &config, QIODevice *xml, QObject *parent = 0); + + ~KConfigLoader(); + + /** + * Finds the item for the given group and key. + * + * @param group the group in the config file to look in + * @param key the configuration key to find + * @return the associated KConfigSkeletonItem, or 0 if none + */ + KConfigSkeletonItem *findItem(const QString &group, const QString &key) const; + + /** + * Finds an item by its name + */ + KConfigSkeletonItem *findItemByName(const QString &name) const; + + /** + * Returns the property (variantized value) of the named item + */ + QVariant property(const QString &name) const; + + /** + * Check to see if a group exists + * + * @param group the name of the group to check for + * @return true if the group exists, or false if it does not + */ + bool hasGroup(const QString &group) const; + + /** + * @return the list of groups defined by the XML + */ + QStringList groupList() const; + +protected: + /** + * Hack used to force writing when no default exists in config file. + */ + bool usrWriteConfig(); + +private: + ConfigLoaderPrivate * const d; +}; + +#endif //multiple inclusion guard diff --git a/src/gui/kconfigloader_p.h b/src/gui/kconfigloader_p.h new file mode 100644 index 00000000..f9aa9191 --- /dev/null +++ b/src/gui/kconfigloader_p.h @@ -0,0 +1,222 @@ +/* + * Copyright 2007-2008 Aaron Seigo <aseigo@kde.org> + * + * This program 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, or + * (at your option) any later version. + * + * This program 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef KCONFIGLOADER_P_H +#define KCONFIGLOADER_P_H + +#include <QUrl> + +class ConfigLoaderPrivate +{ + public: + ConfigLoaderPrivate() + : saveDefaults(false) + { + } + + ~ConfigLoaderPrivate() + { + clearData(); + } + + void clearData() + { + qDeleteAll(bools); + qDeleteAll(strings); + qDeleteAll(stringlists); + qDeleteAll(colors); + qDeleteAll(fonts); + qDeleteAll(ints); + qDeleteAll(uints); + qDeleteAll(urls); + qDeleteAll(dateTimes); + qDeleteAll(doubles); + qDeleteAll(intlists); + qDeleteAll(longlongs); + qDeleteAll(points); + qDeleteAll(rects); + qDeleteAll(sizes); + qDeleteAll(ulonglongs); + qDeleteAll(urllists); + } + + bool *newBool() + { + bool *v = new bool; + bools.append(v); + return v; + } + + QString *newString() + { + QString *v = new QString; + strings.append(v); + return v; + } + + QStringList *newStringList() + { + QStringList *v = new QStringList; + stringlists.append(v); + return v; + } + + QColor *newColor() + { + QColor *v = new QColor; + colors.append(v); + return v; + } + + QFont *newFont() + { + QFont *v = new QFont; + fonts.append(v); + return v; + } + + qint32 *newInt() + { + qint32 *v = new qint32; + ints.append(v); + return v; + } + + quint32 *newUint() + { + quint32 *v = new quint32; + uints.append(v); + return v; + } + + QUrl *newUrl() + { + QUrl *v = new QUrl; + urls.append(v); + return v; + } + + QDateTime *newDateTime() + { + QDateTime *v = new QDateTime; + dateTimes.append(v); + return v; + } + + double *newDouble() + { + double *v = new double; + doubles.append(v); + return v; + } + + QList<qint32>* newIntList() + { + QList<qint32> *v = new QList<qint32>; + intlists.append(v); + return v; + } + + qint64 *newLongLong() + { + qint64 *v = new qint64; + longlongs.append(v); + return v; + } + + QPoint *newPoint() + { + QPoint *v = new QPoint; + points.append(v); + return v; + } + + QRect *newRect() + { + QRect *v = new QRect; + rects.append(v); + return v; + } + + QSize *newSize() + { + QSize *v = new QSize; + sizes.append(v); + return v; + } + + quint64 *newULongLong() + { + quint64 *v = new quint64; + ulonglongs.append(v); + return v; + } + + QList<QUrl> *newUrlList() + { + QList<QUrl> *v = new QList<QUrl>(); + urllists.append(v); + return v; + } + + void parse(KConfigLoader *loader, QIODevice *xml); + + /** + * Whether or not to write out default values. + * + * @param writeDefaults true if defaults should be written out + */ + void setWriteDefaults(bool writeDefaults) + { + saveDefaults = writeDefaults; + } + + /** + * @return true if default values will also be written out + */ + bool writeDefaults() const + { + return saveDefaults; + } + + + QList<bool *> bools; + QList<QString *> strings; + QList<QStringList *> stringlists; + QList<QColor *> colors; + QList<QFont *> fonts; + QList<qint32 *> ints; + QList<quint32 *> uints; + QList<QUrl *> urls; + QList<QDateTime *> dateTimes; + QList<double *> doubles; + QList<QList<qint32> *> intlists; + QList<qint64 *> longlongs; + QList<QPoint *> points; + QList<QRect *> rects; + QList<QSize *> sizes; + QList<quint64 *> ulonglongs; + QList<QList<QUrl> *> urllists; + QString baseGroup; + QStringList groups; + QHash<QString, QString> keysToNames; + bool saveDefaults; +}; + +#endif diff --git a/src/gui/kconfigloaderhandler_p.h b/src/gui/kconfigloaderhandler_p.h new file mode 100644 index 00000000..f0766346 --- /dev/null +++ b/src/gui/kconfigloaderhandler_p.h @@ -0,0 +1,68 @@ +/* + * Copyright 2007-2008 Aaron Seigo <aseigo@kde.org> + * Copyright 2013 Marco Martin <mart@kde.org> + * + * This program 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, or + * (at your option) any later version. + * + * This program 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef KCONFIGLOADERHANDLER_P_H +#define KCONFIGLOADERHANDLER_P_H + +#include <QXmlDefaultHandler> + +class ConfigLoaderHandler : public QXmlDefaultHandler +{ +public: + ConfigLoaderHandler(KConfigLoader *config, ConfigLoaderPrivate *d); + bool startElement(const QString &namespaceURI, const QString &localName, + const QString &qName, const QXmlAttributes &atts); + bool endElement(const QString &namespaceURI, const QString &localName, + const QString &qName); + bool characters(const QString &ch); + + QString name() const; + void setName(const QString &name); + QString key() const; + void setKey(const QString &name); + QString type() const; + QString currentGroup() const; + QString defaultValue() const; + +private: + virtual void addItem(); + void resetState(); + + KConfigLoader *m_config; + ConfigLoaderPrivate *d; + int m_min; + int m_max; + QString m_currentGroup; + QString m_name; + QString m_key; + QString m_type; + QString m_label; + QString m_default; + QString m_cdata; + QString m_whatsThis; + KConfigSkeleton::ItemEnum::Choice m_choice; + QList<KConfigSkeleton::ItemEnum::Choice> m_enumChoices; + bool m_haveMin; + bool m_haveMax; + bool m_inChoice; +}; + +#endif + diff --git a/src/gui/kconfigskeleton.cpp b/src/gui/kconfigskeleton.cpp new file mode 100644 index 00000000..7704d36e --- /dev/null +++ b/src/gui/kconfigskeleton.cpp @@ -0,0 +1,121 @@ +/* + This file is part of KOrganizer. + Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org> + Copyright (c) 2003 Waldo Bastian <bastian@kde.org> + + 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 "kconfigskeleton.h" + +#include <kcoreconfigskeleton_p.h> + +KConfigSkeleton::KConfigSkeleton(const QString &configname, QObject* parent) + : KCoreConfigSkeleton(configname, parent) +{ +} + +KConfigSkeleton::KConfigSkeleton(KSharedConfig::Ptr pConfig, QObject* parent) + : KCoreConfigSkeleton(pConfig, parent) +{ +} + + +KConfigSkeleton::ItemColor::ItemColor( const QString &_group, const QString &_key, + QColor &reference, + const QColor &defaultValue ) + : KConfigSkeletonGenericItem<QColor>( _group, _key, reference, defaultValue ) +{ +} + +void KConfigSkeleton::ItemColor::readConfig( KConfig *config ) +{ + KConfigGroup cg(config, mGroup ); + mReference = cg.readEntry( mKey, mDefault ); + mLoadedValue = mReference; + + readImmutability( cg ); +} + +void KConfigSkeleton::ItemColor::setProperty(const QVariant & p) +{ + mReference = qvariant_cast<QColor>(p); +} + +bool KConfigSkeleton::ItemColor::isEqual(const QVariant &v) const +{ + return mReference == qvariant_cast<QColor>(v); +} + +QVariant KConfigSkeleton::ItemColor::property() const +{ + return QVariant(mReference); +} + + +KConfigSkeleton::ItemFont::ItemFont( const QString &_group, const QString &_key, + QFont &reference, + const QFont &defaultValue ) + : KConfigSkeletonGenericItem<QFont>( _group, _key, reference, defaultValue ) +{ +} + +void KConfigSkeleton::ItemFont::readConfig( KConfig *config ) +{ + KConfigGroup cg(config, mGroup ); + mReference = cg.readEntry( mKey, mDefault ); + mLoadedValue = mReference; + + readImmutability( cg ); +} + +void KConfigSkeleton::ItemFont::setProperty(const QVariant & p) +{ + mReference = qvariant_cast<QFont>(p); +} + +bool KConfigSkeleton::ItemFont::isEqual(const QVariant &v) const +{ + return mReference == qvariant_cast<QFont>(v); +} + +QVariant KConfigSkeleton::ItemFont::property() const +{ + return QVariant(mReference); +} + + +KConfigSkeleton::ItemColor *KConfigSkeleton::addItemColor( const QString &name, QColor &reference, + const QColor &defaultValue, const QString &key ) +{ + KConfigSkeleton::ItemColor *item; + item = new KConfigSkeleton::ItemColor( d->mCurrentGroup, key.isNull() ? name : key, + reference, defaultValue ); + addItem( item, name ); + return item; +} + +KConfigSkeleton::ItemFont *KConfigSkeleton::addItemFont( const QString &name, QFont &reference, + const QFont &defaultValue, const QString &key ) +{ + KConfigSkeleton::ItemFont *item; + item = new KConfigSkeleton::ItemFont( d->mCurrentGroup, key.isNull() ? name : key, + reference, defaultValue ); + addItem( item, name ); + return item; +} + + diff --git a/src/gui/kconfigskeleton.h b/src/gui/kconfigskeleton.h new file mode 100644 index 00000000..e62e60b4 --- /dev/null +++ b/src/gui/kconfigskeleton.h @@ -0,0 +1,140 @@ +/* + * This file is part of KDE. + * + * Copyright (c) 2001,2002,2003 Cornelius Schumacher <schumacher@kde.org> + * Copyright (c) 2003 Waldo Bastian <bastian@kde.org> + * + * 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. + */ + +#ifndef KCONFIGSKELETON_H +#define KCONFIGSKELETON_H + +#include <kconfiggui_export.h> + +#include <kcoreconfigskeleton.h> + +#include <QColor> +#include <QFont> + +/** + * @short Class for handling preferences settings for an application. + * @author Cornelius Schumacher + * + * This class extends KCoreConfigSkeleton by support for GUI types. + * + */ +class KCONFIGGUI_EXPORT KConfigSkeleton : public KCoreConfigSkeleton +{ + Q_OBJECT +public: + /** + * Class for handling a color preferences item. + */ + class KCONFIGGUI_EXPORT ItemColor:public KConfigSkeletonGenericItem < QColor > + { + public: + /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ + ItemColor(const QString & _group, const QString & _key, + QColor & reference, + const QColor & defaultValue = QColor(128, 128, 128)); + + /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ + void readConfig(KConfig * config); + + /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ + void setProperty(const QVariant & p); + + /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) */ + bool isEqual(const QVariant &p) const; + + /** @copydoc KConfigSkeletonItem::property() */ + QVariant property() const; + }; + + + /** + * Class for handling a font preferences item. + */ + class KCONFIGGUI_EXPORT ItemFont:public KConfigSkeletonGenericItem < QFont > + { + public: + /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ + ItemFont(const QString & _group, const QString & _key, QFont & reference, + const QFont & defaultValue = QFont()); + + /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ + void readConfig(KConfig * config); + + /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ + void setProperty(const QVariant & p); + + /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) */ + bool isEqual(const QVariant &p) const; + + /** @copydoc KConfigSkeletonItem::property() */ + QVariant property() const; + }; + +public: + /** + * Constructor. + * + * @param configname name of config file. If no name is given, the default + * config file as returned by KSharedConfig::openConfig() is used. + */ + explicit KConfigSkeleton(const QString & configname = QString(), QObject* parent = 0); + + /** + * Constructor. + * + * @param config configuration object to use. + */ + explicit KConfigSkeleton(KSharedConfig::Ptr config, QObject* parent = 0); + + /** + * Register an item of type QColor. + * + * @param name Name used to identify this setting. Names must be unique. + * @param reference Pointer to the variable, which is set by readConfig() + * calls and read by writeConfig() calls. + * @param defaultValue Default value, which is used when the config file + * does not yet contain the key of this item. + * @param key Key used in config file. If key is null, name is used as key. + * @return The created item + */ + ItemColor *addItemColor(const QString & name, QColor & reference, + const QColor & defaultValue = QColor(128, 128, 128), + const QString & key = QString()); + + /** + * Register an item of type QFont. + * + * @param name Name used to identify this setting. Names must be unique. + * @param reference Pointer to the variable, which is set by readConfig() + * calls and read by writeConfig() calls. + * @param defaultValue Default value, which is used when the config file + * does not yet contain the key of this item. + * @param key Key used in config file. If key is null, name is used as key. + * @return The created item + */ + ItemFont *addItemFont(const QString & name, QFont & reference, + const QFont & defaultValue = QFont(), + const QString & key = QString()); + +}; + +#endif diff --git a/src/gui/kstandardshortcut.cpp b/src/gui/kstandardshortcut.cpp new file mode 100644 index 00000000..a377ff0f --- /dev/null +++ b/src/gui/kstandardshortcut.cpp @@ -0,0 +1,377 @@ +/* This file is part of the KDE libraries + Copyright (C) 1997 Stefan Taferner (taferner@alpin.or.at) + Copyright (C) 2000 Nicolas Hadacek (haadcek@kde.org) + Copyright (C) 2001,2002 Ellis Whitehead (ellis@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + 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 "kstandardshortcut.h" + +#include "kconfig.h" +#include "ksharedconfig.h" +#include <kconfiggroup.h> + +#include <QCoreApplication> +#include <QDebug> +#include <QKeySequence> + +namespace KStandardShortcut +{ + +struct KStandardShortcutInfo +{ + //! The standard shortcut id. @see StandardShortcut + StandardShortcut id; + + /** + * Unique name for the given accel. The name is used to save the user + * settings. It's not representable. Use description for that. + * @warning NEVER EVER CHANGE IT OR TRANSLATE IT! + */ + const char* name; + + //! Context for the translation + const char* translation_context; + + //! Localized label for user-visible display + const char* description; + + //! The keys for this shortcut + int cutDefault, cutDefault2; + + //! A shortcut that is created with @a cutDefault and @cutDefault2 + QList<QKeySequence> cut; + + //! If this struct is initialized. If not initialized @cut is not valid + bool isInitialized; +}; + +//! We need to remember the context to get the correct translation. +#undef I18N_NOOP2 +#define I18N_NOOP2(comment,x) comment, x + +#define CTRL(x) Qt::CTRL+Qt::Key_##x +#define SHIFT(x) Qt::SHIFT+Qt::Key_##x +#define CTRLSHIFT(x) Qt::CTRL+Qt::SHIFT+Qt::Key_##x +#define ALT(x) Qt::ALT+Qt::Key_##x +#define ALTSHIFT(x) Qt::ALT+Qt::SHIFT+Qt::Key_##x + +/** Array of predefined KStandardShortcutInfo objects, which cover all + the "standard" accelerators. Each enum value from StandardShortcut + should appear in this table. +*/ +// STUFF WILL BREAK IF YOU DON'T READ THIS!!! +// Read the comments of the big enum in kstandardshortcut.h before you change anything! +static KStandardShortcutInfo g_infoStandardShortcut[] = +{ +//Group File, + {AccelNone, 0 , 0 , 0 , 0 , 0 , QList<QKeySequence>(), false }, + { Open , "Open" , I18N_NOOP2("@action", "Open") , CTRL(O), 0 , QList<QKeySequence>(), false } , + { New , "New" , I18N_NOOP2("@action", "New") , CTRL(N), 0 , QList<QKeySequence>(), false } , + { Close , "Close", I18N_NOOP2("@action", "Close"), CTRL(W), CTRL(Escape), QList<QKeySequence>(), false } , + { Save , "Save" , I18N_NOOP2("@action", "Save") , CTRL(S), 0 , QList<QKeySequence>(), false } , + { Print , "Print", I18N_NOOP2("@action", "Print"), CTRL(P), 0 , QList<QKeySequence>(), false } , + { Quit , "Quit" , I18N_NOOP2("@action", "Quit") , CTRL(Q), 0 , QList<QKeySequence>(), false } , + +//Group Edit + { Undo , "Undo" , I18N_NOOP2("@action", "Undo") , CTRL(Z) , 0 , QList<QKeySequence>(), false }, + { Redo , "Redo" , I18N_NOOP2("@action", "Redo") , CTRLSHIFT(Z) , 0 , QList<QKeySequence>(), false }, + { Cut , "Cut" , I18N_NOOP2("@action", "Cut") , CTRL(X) , SHIFT(Delete), QList<QKeySequence>(), false }, + { Copy , "Copy" , I18N_NOOP2("@action", "Copy") , CTRL(C) , CTRL(Insert) , QList<QKeySequence>(), false }, + { Paste , "Paste" , I18N_NOOP2("@action", "Paste") , CTRL(V) , SHIFT(Insert), QList<QKeySequence>(), false }, + { PasteSelection , "Paste Selection" , I18N_NOOP2("@action", "Paste Selection") , CTRLSHIFT(Insert), 0 , QList<QKeySequence>(), false }, + + { SelectAll , "SelectAll" , I18N_NOOP2("@action", "Select All") , CTRL(A) , 0 , QList<QKeySequence>(), false }, + { Deselect , "Deselect" , I18N_NOOP2("@action", "Deselect") , CTRLSHIFT(A) , 0 , QList<QKeySequence>(), false }, + { DeleteWordBack , "DeleteWordBack" , I18N_NOOP2("@action", "Delete Word Backwards"), CTRL(Backspace) , 0 , QList<QKeySequence>(), false }, + { DeleteWordForward, "DeleteWordForward", I18N_NOOP2("@action", "Delete Word Forward") , CTRL(Delete) , 0 , QList<QKeySequence>(), false }, + + { Find , "Find" , I18N_NOOP2("@action", "Find") , CTRL(F) , 0 , QList<QKeySequence>(), false }, + { FindNext , "FindNext" , I18N_NOOP2("@action", "Find Next") , Qt::Key_F3 , 0 , QList<QKeySequence>(), false }, + { FindPrev , "FindPrev" , I18N_NOOP2("@action", "Find Prev") , SHIFT(F3) , 0 , QList<QKeySequence>(), false }, + { Replace , "Replace" , I18N_NOOP2("@action", "Replace") , CTRL(R) , 0 , QList<QKeySequence>(), false }, + +//Group Navigation + { Home , "Home" , I18N_NOOP2("@action Go to main page" , "Home") , ALT(Home) , Qt::Key_HomePage , QList<QKeySequence>(), false }, + { Begin , "Begin" , I18N_NOOP2("@action Beginning of document", "Begin") , CTRL(Home) , 0 , QList<QKeySequence>(), false }, + { End , "End" , I18N_NOOP2("@action End of document" , "End") , CTRL(End) , 0 , QList<QKeySequence>(), false }, + { Prior , "Prior" , I18N_NOOP2("@action" , "Prior") , Qt::Key_PageUp , 0 , QList<QKeySequence>(), false }, + { Next , "Next" , I18N_NOOP2("@action Opposite to Prior" , "Next") , Qt::Key_PageDown, 0 , QList<QKeySequence>(), false }, + + { Up , "Up" , I18N_NOOP2("@action" , "Up") , ALT(Up) , 0 , QList<QKeySequence>(), false }, + { Back , "Back" , I18N_NOOP2("@action" , "Back") , ALT(Left) , Qt::Key_Back , QList<QKeySequence>(), false }, + { Forward , "Forward" , I18N_NOOP2("@action" , "Forward") , ALT(Right) , Qt::Key_Forward , QList<QKeySequence>(), false }, + { Reload , "Reload" , I18N_NOOP2("@action" , "Reload") , Qt::Key_F5 , Qt::Key_Refresh , QList<QKeySequence>(), false }, + + { BeginningOfLine, "BeginningOfLine" , I18N_NOOP2("@action" , "Beginning of Line") , Qt::Key_Home , 0 , QList<QKeySequence>(), false }, + { EndOfLine , "EndOfLine" , I18N_NOOP2("@action" , "End of Line") , Qt::Key_End , 0 , QList<QKeySequence>(), false }, + { GotoLine , "GotoLine" , I18N_NOOP2("@action" , "Go to Line") , CTRL(G) , 0 , QList<QKeySequence>(), false }, + { BackwardWord , "BackwardWord" , I18N_NOOP2("@action" , "Backward Word") , CTRL(Left) , 0 , QList<QKeySequence>(), false }, + { ForwardWord , "ForwardWord" , I18N_NOOP2("@action" , "Forward Word") , CTRL(Right) , 0 , QList<QKeySequence>(), false }, + + { AddBookmark , "AddBookmark" , I18N_NOOP2("@action" , "Add Bookmark") , CTRL(B) , 0 , QList<QKeySequence>(), false }, + { ZoomIn , "ZoomIn" , I18N_NOOP2("@action" , "Zoom In") , CTRL(Plus) , CTRL(Equal) , QList<QKeySequence>(), false }, + { ZoomOut , "ZoomOut" , I18N_NOOP2("@action" , "Zoom Out") , CTRL(Minus) , 0 , QList<QKeySequence>(), false }, + { FullScreen , "FullScreen" , I18N_NOOP2("@action" , "Full Screen Mode") , CTRLSHIFT(F) , 0 , QList<QKeySequence>(), false }, + + { ShowMenubar , "ShowMenubar" , I18N_NOOP2("@action" , "Show Menu Bar") , CTRL(M) , 0 , QList<QKeySequence>(), false }, + { TabNext , "Activate Next Tab" , I18N_NOOP2("@action" , "Activate Next Tab") , CTRL(Period) , CTRL(BracketRight), QList<QKeySequence>(), false }, + { TabPrev , "Activate Previous Tab", I18N_NOOP2("@action" , "Activate Previous Tab"), CTRL(Comma) , CTRL(BracketLeft) , QList<QKeySequence>(), false }, + + //Group Help + { Help , "Help" , I18N_NOOP2("@action" , "Help") , Qt::Key_F1 , 0 , QList<QKeySequence>(), false }, + { WhatsThis , "WhatsThis" , I18N_NOOP2("@action" , "What's This") , SHIFT(F1) , 0 , QList<QKeySequence>(), false }, + +//Group TextCompletion + { TextCompletion , "TextCompletion" , I18N_NOOP2("@action", "Text Completion") , CTRL(E) , 0, QList<QKeySequence>(), false }, + { PrevCompletion , "PrevCompletion" , I18N_NOOP2("@action", "Previous Completion Match"), CTRL(Up) , 0, QList<QKeySequence>(), false }, + { NextCompletion , "NextCompletion" , I18N_NOOP2("@action", "Next Completion Match") , CTRL(Down) , 0, QList<QKeySequence>(), false }, + { SubstringCompletion , "SubstringCompletion" , I18N_NOOP2("@action", "Substring Completion") , CTRL(T) , 0, QList<QKeySequence>(), false }, + + { RotateUp , "RotateUp" , I18N_NOOP2("@action", "Previous Item in List") , Qt::Key_Up , 0, QList<QKeySequence>(), false }, + { RotateDown , "RotateDown" , I18N_NOOP2("@action", "Next Item in List") , Qt::Key_Down, 0, QList<QKeySequence>(), false }, + + { OpenRecent , "OpenRecent" , I18N_NOOP2("@action", "Open Recent") , 0 , 0, QList<QKeySequence>(), false }, + { SaveAs , "SaveAs" , I18N_NOOP2("@action", "Save As") , CTRLSHIFT(S), 0, QList<QKeySequence>(), false }, + { Revert , "Revert" , I18N_NOOP2("@action", "Revert") , 0 , 0, QList<QKeySequence>(), false }, + { PrintPreview , "PrintPreview" , I18N_NOOP2("@action", "Print Preview") , 0 , 0, QList<QKeySequence>(), false }, + { Mail , "Mail" , I18N_NOOP2("@action", "Mail") , 0 , 0, QList<QKeySequence>(), false }, + { Clear , "Clear" , I18N_NOOP2("@action", "Clear") , 0 , 0, QList<QKeySequence>(), false }, + { ActualSize , "ActualSize" , I18N_NOOP2("@action", "Actual Size") , 0 , 0, QList<QKeySequence>(), false }, + { FitToPage , "FitToPage" , I18N_NOOP2("@action", "Fit To Page") , 0 , 0, QList<QKeySequence>(), false }, + { FitToWidth , "FitToWidth" , I18N_NOOP2("@action", "Fit To Width") , 0 , 0, QList<QKeySequence>(), false }, + { FitToHeight , "FitToHeight" , I18N_NOOP2("@action", "Fit To Height") , 0 , 0, QList<QKeySequence>(), false }, + { Zoom , "Zoom" , I18N_NOOP2("@action", "Zoom") , 0 , 0, QList<QKeySequence>(), false }, + { Goto , "Goto" , I18N_NOOP2("@action", "Goto") , 0 , 0, QList<QKeySequence>(), false }, + { GotoPage , "GotoPage" , I18N_NOOP2("@action", "Goto Page") , 0 , 0, QList<QKeySequence>(), false }, + { DocumentBack , "DocumentBack" , I18N_NOOP2("@action", "Document Back") , ALTSHIFT(Left), 0, QList<QKeySequence>(), false }, + { DocumentForward , "DocumentForward" , I18N_NOOP2("@action", "Document Forward") , ALTSHIFT(Right), 0, QList<QKeySequence>(), false }, + { EditBookmarks , "EditBookmarks" , I18N_NOOP2("@action", "Edit Bookmarks") , 0 , 0, QList<QKeySequence>(), false }, + { Spelling , "Spelling" , I18N_NOOP2("@action", "Spelling") , 0 , 0, QList<QKeySequence>(), false }, + { ShowToolbar , "ShowToolbar" , I18N_NOOP2("@action", "Show Toolbar") , 0 , 0, QList<QKeySequence>(), false }, + { ShowStatusbar , "ShowStatusbar" , I18N_NOOP2("@action", "Show Statusbar") , 0 , 0, QList<QKeySequence>(), false }, + { SaveOptions , "SaveOptions" , I18N_NOOP2("@action", "Save Options") , 0 , 0, QList<QKeySequence>(), false }, + { KeyBindings , "KeyBindings" , I18N_NOOP2("@action", "Key Bindings") , 0 , 0, QList<QKeySequence>(), false }, + { Preferences , "Preferences" , I18N_NOOP2("@action", "Preferences") , 0 , 0, QList<QKeySequence>(), false }, + { ConfigureToolbars , "ConfigureToolbars" , I18N_NOOP2("@action", "Configure Toolbars") , 0 , 0, QList<QKeySequence>(), false }, + { ConfigureNotifications , "ConfigureNotifications" , I18N_NOOP2("@action", "Configure Notifications") , 0 , 0, QList<QKeySequence>(), false }, + { TipofDay , "TipofDay" , I18N_NOOP2("@action", "Tip Of Day") , 0 , 0, QList<QKeySequence>(), false }, + { ReportBug , "ReportBug" , I18N_NOOP2("@action", "Report Bug") , 0 , 0, QList<QKeySequence>(), false }, + { SwitchApplicationLanguage, "SwitchApplicationLanguage", I18N_NOOP2("@action", "Switch Application Language"), 0 , 0, QList<QKeySequence>(), false }, + { AboutApp , "AboutApp" , I18N_NOOP2("@action", "About Application") , 0 , 0, QList<QKeySequence>(), false }, + { AboutKDE , "AboutKDE" , I18N_NOOP2("@action", "About KDE") , 0 , 0, QList<QKeySequence>(), false }, + + //dummy entry to catch simple off-by-one errors. Insert new entries before this line. + { AccelNone , 0 , 0 , 0 , 0, 0, QList<QKeySequence>(), false } +}; + + +/** Search for the KStandardShortcutInfo object associated with the given @p id. + Return a dummy entry with no name and an empty shortcut if @p id is invalid. +*/ +static KStandardShortcutInfo *guardedStandardShortcutInfo(StandardShortcut id) +{ + if (id >= static_cast<int>(sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo)) || + id < 0) { + qWarning() << "KStandardShortcut: id not found!"; + return &g_infoStandardShortcut[AccelNone]; + } else + return &g_infoStandardShortcut[id]; +} + +/** Initialize the accelerator @p id by checking if it is overridden + in the configuration file (and if it isn't, use the default). + On X11, if QApplication was initialized with GUI disabled, + the default will always be used. +*/ +static void initialize(StandardShortcut id) +{ + KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); + + // All three are needed. + if (info->id!=AccelNone) { + Q_ASSERT(info->description); + Q_ASSERT(info->translation_context); + Q_ASSERT(info->name); + } + + KConfigGroup cg(KSharedConfig::openConfig(), "Shortcuts"); + + if (cg.hasKey(info->name)) { + QString s = cg.readEntry(info->name); + if (s != QLatin1String("none")) + info->cut = QKeySequence::listFromString(s); + else + info->cut = QList<QKeySequence>(); + } else { + info->cut = hardcodedDefaultShortcut(id); + } + + info->isInitialized = true; +} + +void saveShortcut(StandardShortcut id, const QList<QKeySequence> &newShortcut) +{ + KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); + // If the action has no standard shortcut associated there is nothing to + // save + if(info->id == AccelNone) + return; + + KConfigGroup cg(KSharedConfig::openConfig(), "Shortcuts"); + + info->cut = newShortcut; + bool sameAsDefault = (newShortcut == hardcodedDefaultShortcut(id)); + + if (sameAsDefault) { + // If the shortcut is the equal to the hardcoded one we remove it from + // kdeglobal if necessary and return. + if(cg.hasKey(info->name)) + cg.deleteEntry(info->name, KConfig::Global|KConfig::Persistent); + + return; + } + + // Write the changed shortcut to kdeglobals + cg.writeEntry(info->name, QKeySequence::listToString(info->cut), KConfig::Global|KConfig::Persistent); +} + +QString name(StandardShortcut id) +{ + return QString::fromLatin1(guardedStandardShortcutInfo(id)->name); +} + +QString label(StandardShortcut id) +{ + KStandardShortcutInfo *info = guardedStandardShortcutInfo( id ); + return QCoreApplication::translate("KStandardShortcut", + info->description, + info->translation_context); +} + +// TODO: Add psWhatsThis entry to KStandardShortcutInfo +QString whatsThis( StandardShortcut /*id*/ ) +{ +// KStandardShortcutInfo* info = guardedStandardShortcutInfo( id ); +// if( info && info->whatsThis ) +// return i18n(info->whatsThis); +// else + return QString(); +} + +const QList<QKeySequence> &shortcut(StandardShortcut id) +{ + KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); + + if(!info->isInitialized) + initialize(id); + + return info->cut; +} + +StandardShortcut find(const QKeySequence &seq) +{ + if( !seq.isEmpty() ) { + for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) { + StandardShortcut id = g_infoStandardShortcut[i].id; + if( id != AccelNone ) { + if(!g_infoStandardShortcut[i].isInitialized) + initialize(id); + if(g_infoStandardShortcut[i].cut.contains(seq)) + return id; + } + } + } + return AccelNone; +} + +StandardShortcut find(const char *keyName) +{ + for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) + if (qstrcmp(g_infoStandardShortcut[i].name, keyName)) + return g_infoStandardShortcut[i].id; + + return AccelNone; +} + +QList<QKeySequence> hardcodedDefaultShortcut(StandardShortcut id) +{ + QList<QKeySequence> cut; + KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); + + if (info->cutDefault != 0) + cut << info->cutDefault; + + if (info->cutDefault2 != 0) { + if (cut.isEmpty()) + cut << QKeySequence(); + + cut << info->cutDefault2; + } + + return cut; +} + +const QList<QKeySequence> &open() { return shortcut( Open ); } +const QList<QKeySequence> &openNew() { return shortcut( New ); } +const QList<QKeySequence> &close() { return shortcut( Close ); } +const QList<QKeySequence> &save() { return shortcut( Save ); } +const QList<QKeySequence> &print() { return shortcut( Print ); } +const QList<QKeySequence> &quit() { return shortcut( Quit ); } +const QList<QKeySequence> &cut() { return shortcut( Cut ); } +const QList<QKeySequence> ©() { return shortcut( Copy ); } +const QList<QKeySequence> &paste() { return shortcut( Paste ); } +const QList<QKeySequence> &pasteSelection() { return shortcut( PasteSelection ); } +const QList<QKeySequence> &deleteWordBack() { return shortcut( DeleteWordBack ); } +const QList<QKeySequence> &deleteWordForward() { return shortcut( DeleteWordForward ); } +const QList<QKeySequence> &undo() { return shortcut( Undo ); } +const QList<QKeySequence> &redo() { return shortcut( Redo ); } +const QList<QKeySequence> &find() { return shortcut( Find ); } +const QList<QKeySequence> &findNext() { return shortcut( FindNext ); } +const QList<QKeySequence> &findPrev() { return shortcut( FindPrev ); } +const QList<QKeySequence> &replace() { return shortcut( Replace ); } +const QList<QKeySequence> &home() { return shortcut( Home ); } +const QList<QKeySequence> &begin() { return shortcut( Begin ); } +const QList<QKeySequence> &end() { return shortcut( End ); } +const QList<QKeySequence> &beginningOfLine() { return shortcut( BeginningOfLine ); } +const QList<QKeySequence> &endOfLine() { return shortcut( EndOfLine ); } +const QList<QKeySequence> &prior() { return shortcut( Prior ); } +const QList<QKeySequence> &next() { return shortcut( Next ); } +const QList<QKeySequence> &backwardWord() { return shortcut( BackwardWord ); } +const QList<QKeySequence> &forwardWord() { return shortcut( ForwardWord ); } +const QList<QKeySequence> &gotoLine() { return shortcut( GotoLine ); } +const QList<QKeySequence> &addBookmark() { return shortcut( AddBookmark ); } +const QList<QKeySequence> &tabNext() { return shortcut( TabNext ); } +const QList<QKeySequence> &tabPrev() { return shortcut( TabPrev ); } +const QList<QKeySequence> &fullScreen() { return shortcut( FullScreen ); } +const QList<QKeySequence> &zoomIn() { return shortcut( ZoomIn ); } +const QList<QKeySequence> &zoomOut() { return shortcut( ZoomOut ); } +const QList<QKeySequence> &help() { return shortcut( Help ); } +const QList<QKeySequence> &completion() { return shortcut( TextCompletion ); } +const QList<QKeySequence> &prevCompletion() { return shortcut( PrevCompletion ); } +const QList<QKeySequence> &nextCompletion() { return shortcut( NextCompletion ); } +const QList<QKeySequence> &rotateUp() { return shortcut( RotateUp ); } +const QList<QKeySequence> &rotateDown() { return shortcut( RotateDown ); } +const QList<QKeySequence> &substringCompletion() { return shortcut( SubstringCompletion ); } +const QList<QKeySequence> &whatsThis() { return shortcut( WhatsThis ); } +const QList<QKeySequence> &reload() { return shortcut( Reload ); } +const QList<QKeySequence> &selectAll() { return shortcut( SelectAll ); } +const QList<QKeySequence> &up() { return shortcut( Up ); } +const QList<QKeySequence> &back() { return shortcut( Back ); } +const QList<QKeySequence> &forward() { return shortcut( Forward ); } +const QList<QKeySequence> &showMenubar() { return shortcut( ShowMenubar ); } + +} diff --git a/src/gui/kstandardshortcut.h b/src/gui/kstandardshortcut.h new file mode 100644 index 00000000..b02a6ebf --- /dev/null +++ b/src/gui/kstandardshortcut.h @@ -0,0 +1,482 @@ +/* This file is part of the KDE libraries + Copyright (C) 1997 Stefan Taferner (taferner@kde.org) + Copyright (C) 2000 Nicolas Hadacek (hadacek@kde.org) + Copyright (C) 2001,2002 Ellis Whitehead (ellis@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + 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. +*/ +#ifndef KSTANDARDSHORTCUT_H +#define KSTANDARDSHORTCUT_H + +#include <QtCore/QString> +#include <QKeySequence> + +#include <kconfiggui_export.h> + +/** + * \namespace KStandardShortcut + * Convenient methods for access to the common accelerator keys in + * the key configuration. These are the standard keybindings that should + * be used in all KDE applications. They will be configurable, + * so do not hardcode the default behavior. + */ +namespace KStandardShortcut +{ // STUFF WILL BREAK IF YOU DON'T READ THIS!!! + /* + *Always add new std-accels to the end of this enum, never in the middle! + *Don't forget to add the corresponding entries in g_infoStandardShortcut[] in kstandardshortcut.cpp, too. + *Values of elements here and positions of the corresponding entries in + *the big array g_infoStandardShortcut[] ABSOLUTELY MUST BE THE SAME. + * !!! !!!! !!!!! !!!! + * !!!! !!! !!!! !!!! + * Remember to also update kdoctools/genshortcutents.cpp. + * + * Other Rules: + * + * - Never change the name of an existing shortcut + * - Never translate the name of a shortcut + */ + + /** + * Defines the identifier of all standard accelerators. + */ + enum StandardShortcut { + //C++ requires that the value of an enum symbol be one more than the previous one. + //This means that everything will be well-ordered from here on. + AccelNone=0, + // File menu + Open, New, Close, Save, + // The Print item + Print, + Quit, + // Edit menu + Undo, Redo, Cut, Copy, Paste, PasteSelection, + SelectAll, Deselect, DeleteWordBack, DeleteWordForward, + Find, FindNext, FindPrev, Replace, + // Navigation + Home, Begin, End, Prior, Next, + Up, Back, Forward, Reload, + // Text Navigation + BeginningOfLine, EndOfLine, GotoLine, + BackwardWord, ForwardWord, + // View parameters + AddBookmark, ZoomIn, ZoomOut, FullScreen, + ShowMenubar, + // Tabular navigation + TabNext, TabPrev, + // Help menu + Help, WhatsThis, + // Text completion + TextCompletion, PrevCompletion, NextCompletion, SubstringCompletion, + + RotateUp, RotateDown, + + OpenRecent, + SaveAs, + Revert, + PrintPreview, + Mail, + Clear, + ActualSize, + FitToPage, + FitToWidth, + FitToHeight, + Zoom, + Goto, + GotoPage, + DocumentBack, + DocumentForward, + EditBookmarks, + Spelling, + ShowToolbar, + ShowStatusbar, + SaveOptions, + KeyBindings, + Preferences, + ConfigureToolbars, + ConfigureNotifications, + TipofDay, + ReportBug, + SwitchApplicationLanguage, + AboutApp, + AboutKDE, + + // Insert new items here! + + StandardShortcutCount // number of standard shortcuts + }; + + /** + * Returns the keybinding for @p accel. + * On X11, if QApplication was initialized with GUI disabled, the + * default keybinding will always be returned. + * @param id the id of the accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &shortcut(StandardShortcut id); + + /** + * Returns a unique name for the given accel. + * @param id the id of the accelerator + * @return the unique name of the accelerator + */ + KCONFIGGUI_EXPORT QString name(StandardShortcut id); + + /** + * Returns a localized label for user-visible display. + * @param id the id of the accelerator + * @return a localized label for the accelerator + */ + KCONFIGGUI_EXPORT QString label(StandardShortcut id); + + /** + * Returns an extended WhatsThis description for the given accelerator. + * @param id the id of the accelerator + * @return a localized description of the accelerator + */ + KCONFIGGUI_EXPORT QString whatsThis(StandardShortcut id); + + /** + * Return the StandardShortcut id of the standard accel action which + * uses this key sequence, or AccelNone if none of them do. + * This is used by class KKeyChooser. + * @param keySeq the key sequence to search + * @return the id of the standard accelerator, or AccelNone if there + * is none + */ + KCONFIGGUI_EXPORT StandardShortcut find(const QKeySequence &keySeq); + + /** + * Return the StandardShortcut id of the standard accel action which + * has \a keyName as its name, or AccelNone if none of them do. + * This is used by class KKeyChooser. + * @param keyName the key sequence to search + * @return the id of the standard accelerator, or AccelNone if there + * is none + */ + KCONFIGGUI_EXPORT StandardShortcut find(const char *keyName); + + /** + * Returns the hardcoded default shortcut for @p id. + * This does not take into account the user's configuration. + * @param id the id of the accelerator + * @return the default shortcut of the accelerator + */ + KCONFIGGUI_EXPORT QList<QKeySequence> hardcodedDefaultShortcut(StandardShortcut id); + + /** + * Saves the new shortcut \a cut for standard accel \a id. + */ + KCONFIGGUI_EXPORT void saveShortcut(StandardShortcut id, const QList<QKeySequence> &newShortcut); + + /** + * Open file. Default: Ctrl-o + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &open(); + + /** + * Create a new document (or whatever). Default: Ctrl-n + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &openNew(); + + /** + * Close current document. Default: Ctrl-w + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &close(); + + /** + * Save current document. Default: Ctrl-s + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &save(); + + /** + * Print current document. Default: Ctrl-p + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &print(); + + /** + * Quit the program. Default: Ctrl-q + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &quit(); + + /** + * Undo last operation. Default: Ctrl-z + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &undo(); + + /** + * Redo. Default: Shift-Ctrl-z + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &redo(); + + /** + * Cut selected area and store it in the clipboard. Default: Ctrl-x + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &cut(); + + /** + * Copy selected area into the clipboard. Default: Ctrl-c + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> ©(); + + /** + * Paste contents of clipboard at mouse/cursor position. Default: Ctrl-v + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &paste(); + + /** + * Paste the selection at mouse/cursor position. Default: Ctrl-Shift-Insert + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &pasteSelection(); + + /** + * Select all. Default: Ctrl-A + * @return the shortcut of the standard accelerator + **/ + KCONFIGGUI_EXPORT const QList<QKeySequence> &selectAll(); + + /** + * Delete a word back from mouse/cursor position. Default: Ctrl-Backspace + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &deleteWordBack(); + + /** + * Delete a word forward from mouse/cursor position. Default: Ctrl-Delete + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &deleteWordForward(); + + /** + * Find, search. Default: Ctrl-f + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &find(); + + /** + * Find/search next. Default: F3 + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &findNext(); + + /** + * Find/search previous. Default: Shift-F3 + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &findPrev(); + + /** + * Find and replace matches. Default: Ctrl-r + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &replace(); + + /** + * Zoom in. Default: Ctrl-Plus + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &zoomIn(); + + /** + * Zoom out. Default: Ctrl-Minus + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &zoomOut(); + + /** + * Toggle insert/overwrite (with visual feedback, e.g. in the statusbar). Default: Insert + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &insert(); + + /** + * Goto home page. Default: Alt-Home + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &home(); + + /** + * Goto beginning of the document. Default: Ctrl-Home + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &begin(); + + /** + * Goto end of the document. Default: Ctrl-End + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &end(); + + /** + * Goto beginning of current line. Default: Home + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &beginningOfLine(); + + /** + * Goto end of current line. Default: End + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &endOfLine(); + + /** + * Scroll up one page. Default: Prior + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &prior(); + + /** + * Scroll down one page. Default: Next + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &next(); + + /** + * Go to line. Default: Ctrl+G + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &gotoLine(); + + /** + * Add current page to bookmarks. Default: Ctrl+B + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &addBookmark(); + + /** + * Next Tab. Default: Ctrl-< + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &tabNext(); + + /** + * Previous Tab. Default: Ctrl-> + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &tabPrev(); + + /** + * Full Screen Mode. Default: Ctrl+Shift+F + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &fullScreen(); + + /** + * Help the user in the current situation. Default: F1 + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &help(); + + /** + * Complete text in input widgets. Default Ctrl+E + * @return the shortcut of the standard accelerator + **/ + KCONFIGGUI_EXPORT const QList<QKeySequence> &completion(); + + /** + * Iterate through a list when completion returns + * multiple items. Default: Ctrl+Up + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &prevCompletion(); + + /** + * Iterate through a list when completion returns + * multiple items. Default: Ctrl+Down + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &nextCompletion(); + + /** + * Find a string within another string or list of strings. + * Default: Ctrl-T + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &substringCompletion(); + + /** + * Help users iterate through a list of entries. Default: Up + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &rotateUp(); + + /** + * Help users iterate through a list of entries. Default: Down + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &rotateDown(); + + /** + * What's This button. Default: Shift+F1 + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &whatsThis(); + + /** + * Reload. Default: F5 + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &reload(); + + /** + * Up. Default: Alt+Up + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &up(); + + /** + * Back. Default: Alt+Left + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &back(); + + /** + * Forward. Default: ALT+Right + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &forward(); + + /** + * BackwardWord. Default: Ctrl+Left + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &backwardWord(); + + /** + * ForwardWord. Default: Ctrl+Right + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &forwardWord(); + + /** + * Show Menu Bar. Default: Ctrl-M + * @return the shortcut of the standard accelerator + */ + KCONFIGGUI_EXPORT const QList<QKeySequence> &showMenubar(); + +} + +#endif // KSTANDARDSHORTCUT_H diff --git a/src/gui/kwindowconfig.cpp b/src/gui/kwindowconfig.cpp new file mode 100644 index 00000000..6b7ae5f0 --- /dev/null +++ b/src/gui/kwindowconfig.cpp @@ -0,0 +1,83 @@ +/* + This file is part of the KDE libraries + Copyright (c) 2012 Benjamin Port <benjamin.port@ben2367.fr> + + 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 "kwindowconfig.h" + +#include <QScreen> +#include <QWindow> + +static const char* s_initialSizePropertyName = "_kconfig_initial_size"; +static const char* s_initialScreenSizePropertyName = "_kconfig_initial_screen_size"; + +void KWindowConfig::saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options) +{ + if (!window) + return; + const QRect desk = window->screen()->geometry(); + + const QSize sizeToSave = window->size(); + const bool isMaximized = window->windowState() & Qt::WindowMaximized; + + const QString screenMaximizedString(QString::fromLatin1("Window-Maximized %1x%2").arg(desk.height()).arg(desk.width())); + // Save size only if window is not maximized + if (!isMaximized) { + const QSize defaultSize(window->property(s_initialSizePropertyName).toSize()); + const QSize defaultScreenSize(window->property(s_initialScreenSizePropertyName).toSize()); + const bool sizeValid = defaultSize.isValid() && defaultScreenSize.isValid(); + if (!sizeValid || (sizeValid && (defaultSize != sizeToSave || defaultScreenSize != desk.size()))) { + const QString wString(QString::fromLatin1("Width %1").arg(desk.width())); + const QString hString(QString::fromLatin1("Height %1").arg(desk.height())); + config.writeEntry(wString, sizeToSave.width(), options); + config.writeEntry(hString, sizeToSave.height(), options); + } + } + if ( (isMaximized == false) && !config.hasDefault(screenMaximizedString) ) + config.revertToDefault(screenMaximizedString); + else + config.writeEntry(screenMaximizedString, isMaximized, options); + +} + +void KWindowConfig::restoreWindowSize(QWindow* window, const KConfigGroup& config) +{ + if (!window) + return; + + const QRect desk = window->screen()->geometry(); + + const int width = config.readEntry(QString::fromLatin1("Width %1").arg(desk.width()), window->size().width()); + const int height = config.readEntry(QString::fromLatin1("Height %1").arg(desk.height()), window->size().height()); + const bool isMaximized = config.readEntry(QString::fromLatin1("Window-Maximized %1x%2").arg(desk.height()).arg(desk.width()), false); + + // Check default size + const QSize defaultSize(window->property(s_initialSizePropertyName).toSize()); + const QSize defaultScreenSize(window->property(s_initialScreenSizePropertyName).toSize()); + if (!defaultSize.isValid() || !defaultScreenSize.isValid()) { + window->setProperty(s_initialSizePropertyName, window->size()); + window->setProperty(s_initialScreenSizePropertyName, desk.size()); + } + + // If window is maximized set maximized state and in all case set the size + window->resize(width, height); + if (isMaximized) { + window->setWindowState(Qt::WindowMaximized); + } +} diff --git a/src/gui/kwindowconfig.h b/src/gui/kwindowconfig.h new file mode 100644 index 00000000..2c70571d --- /dev/null +++ b/src/gui/kwindowconfig.h @@ -0,0 +1,58 @@ +/* + This file is part of the KDE libraries + Copyright (c) 2012 Benjamin Port <benjamin.port@ben2367.fr> + + 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/>. +*/ + +#ifndef KWINDOWCONFIG_H +#define KWINDOWCONFIG_H + +#include <kconfiggroup.h> +#include <kconfiggui_export.h> + +class QWindow; + +namespace KWindowConfig +{ + /** + * Saves the window's size dependent on the screen dimension either to the + * global or application config file. + * + * @note the group must be set before calling + * + * @param window The window to save size. + * @param config The config group to read from. + * @param options passed to KConfigGroup::writeEntry() + * @since 5.0 + */ + KCONFIGGUI_EXPORT void saveWindowSize( const QWindow* window, KConfigGroup& config, KConfigGroup::WriteConfigFlags options = KConfigGroup::Normal ); + + /** + * Restores the dialog's size from the configuration according to + * the screen size. + * + * @note the group must be set before calling + * + * @param dialog The dialog to restore size. + * @param config The config group to read from. + * @since 5.0. + */ + KCONFIGGUI_EXPORT void restoreWindowSize( QWindow* window, const KConfigGroup& config ); +} + +#endif // KWINDOWCONFIG_H |