diff --git a/src/kstyle/CMakeLists.txt b/src/kstyle/CMakeLists.txt index 1495cee..45f9c3a 100644 --- a/src/kstyle/CMakeLists.txt +++ b/src/kstyle/CMakeLists.txt @@ -1,41 +1,39 @@ # create a Config.cmake and a ConfigVersion.cmake file and install them set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KF5Style") -configure_file(config-kstyle.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kstyle.h ) - remove_definitions(-DQT_NO_CAST_FROM_ASCII) add_library(KF5Style kstyle.cpp) generate_export_header(KF5Style BASE_NAME KStyle) add_library(KF5::Style ALIAS KF5Style) target_include_directories(KF5Style INTERFACE "$") target_link_libraries(KF5Style PUBLIC Qt5::Widgets PRIVATE KF5::ConfigWidgets KF5::IconThemes ) set_target_properties(KF5Style PROPERTIES VERSION ${FRAMEWORKINTEGRATION_VERSION_STRING} SOVERSION ${FRAMEWORKINTEGRATION_SOVERSION} EXPORT_NAME Style ) ecm_generate_headers(KStyle_HEADERS HEADER_NAMES KStyle REQUIRED_HEADERS KStyle_HEADERS ) install(TARGETS KF5Style EXPORT KF5FrameworkIntegrationTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kstyle_export.h ${KStyle_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KStyle COMPONENT Devel) diff --git a/src/kstyle/config-kstyle.h.cmake b/src/kstyle/config-kstyle.h.cmake deleted file mode 100644 index 89858d1..0000000 --- a/src/kstyle/config-kstyle.h.cmake +++ /dev/null @@ -1 +0,0 @@ -#cmakedefine01 HAVE_X11 diff --git a/src/kstyle/kstyle.cpp b/src/kstyle/kstyle.cpp index bbe6e8f..290b2e7 100644 --- a/src/kstyle/kstyle.cpp +++ b/src/kstyle/kstyle.cpp @@ -1,461 +1,456 @@ /** * KStyle for KDE4 * Copyright (C) 2004-2005 Maksim Orlovich * Copyright (C) 2005,2006 Sandro Giessl * * Based in part on the following software: * KStyle for KDE3 * Copyright (C) 2001-2002 Karol Szwed * Portions (C) 1998-2000 TrollTech AS * Keramik for KDE3, * Copyright (C) 2002 Malte Starostik * (C) 2002-2003 Maksim Orlovich * Portions (C) 2001-2002 Karol Szwed * (C) 2001-2002 Fredrik Höglund * (C) 2000 Daniel M. Duley * (C) 2000 Dirk Mueller * (C) 2001 Martijn Klingens * (C) 2003 Sandro Giessl * 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. * * Many thanks to Bradley T. Hughes for the 3 button scrollbar code. * * 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 "kstyle.h" #include #include #include #include #include #include #include #include #include #include #include #include -#include -#if HAVE_X11 -#include -#include -#endif // ---------------------------------------------------------------------------- static const QStyle::StyleHint SH_KCustomStyleElement = (QStyle::StyleHint)0xff000001; static const int X_KdeBase = 0xff000000; class KStylePrivate { public: KStylePrivate(); QHash styleElements; int hintCounter, controlCounter, subElementCounter; }; KStylePrivate::KStylePrivate() { controlCounter = subElementCounter = X_KdeBase; hintCounter = X_KdeBase + 1; //sic! X_KdeBase is covered by SH_KCustomStyleElement } /* The functions called by widgets that request custom element support, passed to the effective style. Collected in a static inline function due to similarity. */ static inline int customStyleElement(QStyle::StyleHint type, const QString &element, QWidget *widget) { if (!widget || widget->style()->metaObject()->indexOfClassInfo("X-KDE-CustomElements") < 0) { return 0; } const QString originalName = widget->objectName(); widget->setObjectName(element); const int id = widget->style()->styleHint(type, nullptr, widget); widget->setObjectName(originalName); return id; } QStyle::StyleHint KStyle::customStyleHint(const QString &element, const QWidget *widget) { return (StyleHint) customStyleElement(SH_KCustomStyleElement, element, const_cast(widget)); } QStyle::ControlElement KStyle::customControlElement(const QString &element, const QWidget *widget) { return (ControlElement) customStyleElement(SH_KCustomStyleElement, element, const_cast(widget)); } QStyle::SubElement KStyle::customSubElement(const QString &element, const QWidget *widget) { return (SubElement) customStyleElement(SH_KCustomStyleElement, element, const_cast(widget)); } KStyle::KStyle() : d(new KStylePrivate) { } KStyle::~KStyle() { delete d; } /* Custom Style Element runtime extension: We reserve one StyleHint to let the effective style inform widgets whether it supports certain string based style elements. As this could lead to number conflicts (i.e. an app utilizing one of the hints itself for other purposes) there're various safety mechanisms to rule out such interference. 1) It's most unlikely that a widget in some 3rd party app will accidentally call a general QStyle/KStyle styleHint() or draw*() and (unconditionally) expect a valid return, however: a. The StyleHint is not directly above Qt's custom base, assuming most 3rd party apps would - in case - make use of such b. In order to be accepted, the StyleHint query must pass a widget with a perfectly matching name, containing the typical element prefix ("CE_", etc.) and being supported by the current style c. Instead using Qt's fragile qstyleoption_cast on the QStyleOption provided to the StyleHint query, try to dump out a string and hope for the best, we now manipulate the widgets objectName(). Plain Qt dependent widgets can do that themselves and if a widget uses KStyle's convenience access functions, it won't notice this at all 2) The key problem is that a common KDE widget will run into an apps custom style which will then falsely respond to the styleHint() call with an invalid value. To prevent this, supporting styles *must* set a Q_CLASSINFO "X-KDE-CustomElements". 3) If any of the above traps snaps, the returned id is 0 - the QStyle default, indicating that this element is not supported by the current style. Obviously, this contains the "diminished clean" action to (temporarily) manipulate the objectName() of a const QWidget* - but this happens completely inside KStyle or the widget, if it does not make use of KStyles static convenience functions. My biggest worry here would be, that in a multithreaded environment a thread (usually not being owner of the widget) does something crucially relying on the widgets name property... This however would also have to happen during the widget construction or stylechanges, when the functions in doubt will typically be called. So this is imho unlikely causing any trouble, ever. */ /* The functions called by the real style implementation to add support for a certain element. Checks for well-formed string (containing the element prefix) and returns 0 otherwise. Checks whether the element is already supported or inserts it otherwise; Returns the proper id NOTICE: We could check for "X-KDE-CustomElements", but this would bloat style start up times (if they e.g. register 100 elements or so) */ static inline int newStyleElement(const QString &element, const char *check, int &counter, QHash *elements) { if (!element.contains(check)) { return 0; } int id = elements->value(element, 0); if (!id) { ++counter; id = counter; elements->insert(element, id); } return id; } QStyle::StyleHint KStyle::newStyleHint(const QString &element) { return (StyleHint)newStyleElement(element, "SH_", d->hintCounter, &d->styleElements); } QStyle::ControlElement KStyle::newControlElement(const QString &element) { return (ControlElement)newStyleElement(element, "CE_", d->controlCounter, &d->styleElements); } KStyle::SubElement KStyle::newSubElement(const QString &element) { return (SubElement)newStyleElement(element, "SE_", d->subElementCounter, &d->styleElements); } void KStyle::polish(QWidget *w) { // Enable hover effects in all itemviews if (QAbstractItemView *itemView = qobject_cast(w)) { itemView->viewport()->setAttribute(Qt::WA_Hover); } if (QDialogButtonBox *box = qobject_cast(w)) { QPushButton *button = box->button(QDialogButtonBox::Ok); if (button) { auto shortcut = new QShortcut(Qt::CTRL | Qt::Key_Return, button); QObject::connect(shortcut, &QShortcut::activated, button, &QPushButton::click); } } QCommonStyle::polish(w); } QPalette KStyle::standardPalette() const { return KColorScheme::createApplicationPalette(KSharedConfig::openConfig()); } QIcon KStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const { switch (standardIcon) { case QStyle::SP_DesktopIcon: return QIcon::fromTheme(QStringLiteral("user-desktop")); case QStyle::SP_TrashIcon: return QIcon::fromTheme(QStringLiteral("user-trash")); case QStyle::SP_ComputerIcon: return QIcon::fromTheme(QStringLiteral("computer")); case QStyle::SP_DriveFDIcon: return QIcon::fromTheme(QStringLiteral("media-floppy")); case QStyle::SP_DriveHDIcon: return QIcon::fromTheme(QStringLiteral("drive-harddisk")); case QStyle::SP_DriveCDIcon: case QStyle::SP_DriveDVDIcon: return QIcon::fromTheme(QStringLiteral("drive-optical")); case QStyle::SP_DriveNetIcon: return QIcon::fromTheme(QStringLiteral("folder-remote")); case QStyle::SP_DirHomeIcon: return QIcon::fromTheme(QStringLiteral("user-home")); case QStyle::SP_DirOpenIcon: return QIcon::fromTheme(QStringLiteral("document-open-folder")); case QStyle::SP_DirClosedIcon: return QIcon::fromTheme(QStringLiteral("folder")); case QStyle::SP_DirIcon: return QIcon::fromTheme(QStringLiteral("folder")); case QStyle::SP_DirLinkIcon: return QIcon::fromTheme(QStringLiteral("folder")); //TODO: generate (!?) folder with link emblem case QStyle::SP_FileIcon: return QIcon::fromTheme(QStringLiteral("text-plain")); //TODO: look for a better icon case QStyle::SP_FileLinkIcon: return QIcon::fromTheme(QStringLiteral("text-plain")); //TODO: generate (!?) file with link emblem case QStyle::SP_FileDialogStart: return QIcon::fromTheme(QStringLiteral("media-playback-start")); //TODO: find correct icon case QStyle::SP_FileDialogEnd: return QIcon::fromTheme(QStringLiteral("media-playback-stop")); //TODO: find correct icon case QStyle::SP_FileDialogToParent: return QIcon::fromTheme(QStringLiteral("go-up")); case QStyle::SP_FileDialogNewFolder: return QIcon::fromTheme(QStringLiteral("folder-new")); case QStyle::SP_FileDialogDetailedView: return QIcon::fromTheme(QStringLiteral("view-list-details")); case QStyle::SP_FileDialogInfoView: return QIcon::fromTheme(QStringLiteral("document-properties")); case QStyle::SP_FileDialogContentsView: return QIcon::fromTheme(QStringLiteral("view-list-icons")); case QStyle::SP_FileDialogListView: return QIcon::fromTheme(QStringLiteral("view-list-text")); case QStyle::SP_FileDialogBack: return QIcon::fromTheme(QStringLiteral("go-previous")); case QStyle::SP_MessageBoxInformation: return QIcon::fromTheme(QStringLiteral("dialog-information")); case QStyle::SP_MessageBoxWarning: return QIcon::fromTheme(QStringLiteral("dialog-warning")); case QStyle::SP_MessageBoxCritical: return QIcon::fromTheme(QStringLiteral("dialog-error")); case QStyle::SP_MessageBoxQuestion: // This used to be dialog-information for a long time, so keep it as a fallback return QIcon::fromTheme(QStringLiteral("dialog-question"), QIcon::fromTheme(QStringLiteral("dialog-information"))); case QStyle::SP_DialogOkButton: return QIcon::fromTheme(QStringLiteral("dialog-ok")); case QStyle::SP_DialogCancelButton: return QIcon::fromTheme(QStringLiteral("dialog-cancel")); case QStyle::SP_DialogHelpButton: return QIcon::fromTheme(QStringLiteral("help-contents")); case QStyle::SP_DialogOpenButton: return QIcon::fromTheme(QStringLiteral("document-open")); case QStyle::SP_DialogSaveButton: return QIcon::fromTheme(QStringLiteral("document-save")); case QStyle::SP_DialogCloseButton: return QIcon::fromTheme(QStringLiteral("dialog-close")); case QStyle::SP_DialogApplyButton: return QIcon::fromTheme(QStringLiteral("dialog-ok-apply")); case QStyle::SP_DialogResetButton: return QIcon::fromTheme(QStringLiteral("edit-undo")); case QStyle::SP_DialogDiscardButton: return QIcon::fromTheme(QStringLiteral("edit-delete")); case QStyle::SP_DialogYesButton: return QIcon::fromTheme(QStringLiteral("dialog-ok-apply")); case QStyle::SP_DialogNoButton: return QIcon::fromTheme(QStringLiteral("dialog-cancel")); case QStyle::SP_ArrowUp: return QIcon::fromTheme(QStringLiteral("go-up")); case QStyle::SP_ArrowDown: return QIcon::fromTheme(QStringLiteral("go-down")); case QStyle::SP_ArrowLeft: return QIcon::fromTheme(QStringLiteral("go-previous-view")); case QStyle::SP_ArrowRight: return QIcon::fromTheme(QStringLiteral("go-next-view")); case QStyle::SP_ArrowBack: return QIcon::fromTheme(QStringLiteral("go-previous")); case QStyle::SP_ArrowForward: return QIcon::fromTheme(QStringLiteral("go-next")); case QStyle::SP_BrowserReload: return QIcon::fromTheme(QStringLiteral("view-refresh")); case QStyle::SP_BrowserStop: return QIcon::fromTheme(QStringLiteral("process-stop")); case QStyle::SP_MediaPlay: return QIcon::fromTheme(QStringLiteral("media-playback-start")); case QStyle::SP_MediaStop: return QIcon::fromTheme(QStringLiteral("media-playback-stop")); case QStyle::SP_MediaPause: return QIcon::fromTheme(QStringLiteral("media-playback-pause")); case QStyle::SP_MediaSkipForward: return QIcon::fromTheme(QStringLiteral("media-skip-forward")); case QStyle::SP_MediaSkipBackward: return QIcon::fromTheme(QStringLiteral("media-skip-backward")); case QStyle::SP_MediaSeekForward: return QIcon::fromTheme(QStringLiteral("media-seek-forward")); case QStyle::SP_MediaSeekBackward: return QIcon::fromTheme(QStringLiteral("media-seek-backward")); case QStyle::SP_MediaVolume: return QIcon::fromTheme(QStringLiteral("audio-volume-medium")); case QStyle::SP_MediaVolumeMuted: return QIcon::fromTheme(QStringLiteral("audio-volume-muted")); case SP_LineEditClearButton: { const bool rtl = (option && option->direction == Qt::RightToLeft) || (!option && QApplication::isRightToLeft()); const QString directionalThemeName = rtl ? QStringLiteral("edit-clear-locationbar-ltr") : QStringLiteral("edit-clear-locationbar-rtl"); return QIcon::fromTheme(directionalThemeName, QIcon::fromTheme(QStringLiteral("edit-clear"))); } #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) case QStyle::SP_DialogYesToAllButton: return QIcon::fromTheme(QStringLiteral("dialog-ok")); case QStyle::SP_DialogNoToAllButton: return QIcon::fromTheme(QStringLiteral("dialog-cancel")); case QStyle::SP_DialogSaveAllButton: return QIcon::fromTheme(QStringLiteral("document-save-all")); case QStyle::SP_DialogAbortButton: return QIcon::fromTheme(QStringLiteral("dialog-cancel")); case QStyle::SP_DialogRetryButton: return QIcon::fromTheme(QStringLiteral("view-refresh")); case QStyle::SP_DialogIgnoreButton: return QIcon::fromTheme(QStringLiteral("dialog-cancel")); case QStyle::SP_RestoreDefaultsButton: return QIcon::fromTheme(QStringLiteral("document-revert")); #endif default: break; } return QCommonStyle::standardIcon(standardIcon, option, widget); } int KStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const { switch (hint) { case SH_DialogButtonBox_ButtonsHaveIcons: { // was KGlobalSettings::showIconsOnPushButtons() : KConfigGroup g(KSharedConfig::openConfig(), "KDE"); return g.readEntry("ShowIconsOnPushButtons", true); } case SH_ItemView_ArrowKeysNavigateIntoChildren: return true; case SH_Widget_Animate: { KConfigGroup g(KSharedConfig::openConfig(), "KDE-Global GUI Settings"); return g.readEntry("GraphicEffectsLevel", true); } case QStyle::SH_Menu_SubMenuSloppyCloseTimeout: return 300; case SH_ToolButtonStyle: { KConfigGroup g(KSharedConfig::openConfig(), "Toolbar style"); bool useOthertoolbars = false; const QWidget *parent = widget ? widget->parentWidget() : nullptr; //If the widget parent is a QToolBar and the magic property is set if (parent && qobject_cast< const QToolBar * >(parent)) { if (parent->property("otherToolbar").isValid()) { useOthertoolbars = true; } } QString buttonStyle; if (useOthertoolbars) { buttonStyle = g.readEntry("ToolButtonStyleOtherToolbars", "NoText").toLower(); } else { buttonStyle = g.readEntry("ToolButtonStyle", "TextBesideIcon").toLower(); } return buttonStyle == QLatin1String("textbesideicon") ? Qt::ToolButtonTextBesideIcon : buttonStyle == QLatin1String("icontextright") ? Qt::ToolButtonTextBesideIcon : buttonStyle == QLatin1String("textundericon") ? Qt::ToolButtonTextUnderIcon : buttonStyle == QLatin1String("icontextbottom") ? Qt::ToolButtonTextUnderIcon : buttonStyle == QLatin1String("textonly") ? Qt::ToolButtonTextOnly : Qt::ToolButtonIconOnly; } case SH_KCustomStyleElement: if (!widget) { return 0; } return d->styleElements.value(widget->objectName(), 0); case SH_ScrollBar_LeftClickAbsolutePosition: { KConfigGroup g(KSharedConfig::openConfig(), "KDE"); return !g.readEntry("ScrollbarLeftClickNavigatesByPage", true); } default: break; }; return QCommonStyle::styleHint(hint, option, widget, returnData); } int KStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const { switch (metric) { case PM_SmallIconSize: case PM_ButtonIconSize: return KIconLoader::global()->currentSize(KIconLoader::Small); case PM_ToolBarIconSize: return KIconLoader::global()->currentSize(KIconLoader::Toolbar); case PM_LargeIconSize: return KIconLoader::global()->currentSize(KIconLoader::Dialog); case PM_MessageBoxIconSize: // TODO return KIconLoader::global()->currentSize(KIconLoader::MessageBox); return KIconLoader::SizeHuge; default: break; } return QCommonStyle::pixelMetric(metric, option, widget); }