diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fcaca33..24196ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,259 +1,266 @@ # Options # ... set(kpropertycore_LIB_SRCS KProperty.cpp KPropertySet.cpp KPropertyUnit_p.cpp KPropertyFactory.cpp KPropertyCoreUtils.cpp kproperty_debug.cpp ) +set(kpropertycore_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR} +) + +# TODO: see T3992, Split kproperty_qt into kpropertycore_qt & kpropertywidgets_qt +ecm_create_qm_loader(kpropertycore_LIB_SRCS kproperty_qt) + +add_library(KPropertyCore SHARED ${kpropertycore_LIB_SRCS}) +set_coinstallable_lib_version(KPropertyCore) +target_link_libraries(KPropertyCore + PUBLIC + Qt5::Core +) +generate_export_header(KPropertyCore) + +target_include_directories(KPropertyCore + PUBLIC "$" + INTERFACE "$" +) + if(KPROPERTY_WIDGETS) add_subdirectory(editors) set(kpropertywidgets_LIB_SRCS editors/utils.cpp editors/booledit.cpp editors/coloredit.cpp editors/combobox.cpp editors/cursoredit.cpp editors/dateedit.cpp editors/datetimeedit.cpp # editors/dummywidget.cpp editors/fontedit.cpp editors/pixmapedit.cpp editors/pointedit.cpp editors/pointfedit.cpp editors/rectedit.cpp editors/KPropertyRectFEditor.cpp editors/sizeedit.cpp editors/sizefedit.cpp editors/sizepolicyedit.cpp editors/spinbox.cpp editors/KPropertyGenericSelectionEditor.cpp editors/KPropertyMultiLineStringEditor.cpp editors/KPropertyStringEditor.cpp editors/KPropertyUrlEditor.cpp editors/linestyleedit.cpp # editors/stringlistedit.cpp # editors/symbolcombo.cpp editors/timeedit.cpp #TODO editors/urledit.cpp kproperty_debug.cpp # needed here too KPropertyWidgetsFactory.cpp KPropertyWidgetsPluginManager.cpp KDefaultPropertyFactory.cpp KPropertyEditorView.cpp KPropertyEditorDataModel.cpp KPropertyUtils.cpp KPropertyLineStyleSelector_p.cpp KPropertyLineStyleModel_p.cpp KPropertyLineStyleItemDelegate_p.cpp # private: KPropertyUtils_p.h # non-source: Mainpage.dox Messages.sh ) # TODO: reenable once kproperty_qt has been split # ecm_create_qm_loader(kpropertywidgets_LIB_SRCS kpropertywidgets_qt) set(kpropertywidgets_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/editors ) -endif() - -set(kpropertycore_INCLUDE_DIRS - ${CMAKE_CURRENT_SOURCE_DIR} -) - -# TODO: see T3992, Split kproperty_qt into kpropertycore_qt & kpropertywidgets_qt -ecm_create_qm_loader(kpropertycore_LIB_SRCS kproperty_qt) - -add_library(KPropertyCore SHARED ${kpropertycore_LIB_SRCS}) -set_coinstallable_lib_version(KPropertyCore) -target_link_libraries(KPropertyCore - PUBLIC - Qt5::Core -) -generate_export_header(KPropertyCore) - -target_include_directories(KPropertyCore - PUBLIC "$" - INTERFACE "$" -) + if(NOT KPROPERTY_KF) + list(APPEND kpropertywidgets_LIB_SRCS + editors/3rdparty/KColorCombo.cpp + editors/3rdparty/KColorCollection.cpp + ) + list(APPEND kpropertywidgets_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR}/editors/3rdparty + ) + endif() -if(KPROPERTY_WIDGETS) add_library(KPropertyWidgets SHARED ${kpropertywidgets_LIB_SRCS}) set_coinstallable_lib_version(KPropertyWidgets) target_link_libraries(KPropertyWidgets PUBLIC KPropertyCore ) if(KPROPERTY_KF) target_link_libraries(KPropertyWidgets PUBLIC Qt5::Widgets KF5::WidgetsAddons PRIVATE KF5::ConfigGui #KConfigGroup KF5::GuiAddons #KColorCollection ) else() target_link_libraries(KPropertyWidgets PUBLIC Qt5::Widgets ) endif() generate_export_header(KPropertyWidgets) target_include_directories(KPropertyWidgets PUBLIC "$" INTERFACE "$" PRIVATE editors ) endif() # Create a Config.cmake and a ConfigVersion.cmake file and install them set(CMAKECONFIG_CORE_INSTALL_DIR "${CMAKECONFIG_INSTALL_PREFIX}/${KPROPERTYCORE_BASE_NAME}") # A place for KProperty plugins set(KPROPERTY_PLUGIN_INSTALL_DIR ${PLUGIN_INSTALL_DIR}/kproperty${PROJECT_STABLE_VERSION_MAJOR}) ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KPROPERTYCORE VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfigVersion.cmake" ) ecm_configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KPropertyCoreConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfig.cmake" INSTALL_DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyCoreConfigVersion.cmake" DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" COMPONENT Devel) if(SAILFISH) set(KDE_INSTALL_TARGETS_DEFAULT_ARGS LIBRARY DESTINATION lib) endif() install(TARGETS KPropertyCore EXPORT KPropertyCoreTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(EXPORT KPropertyCoreTargets DESTINATION "${CMAKECONFIG_CORE_INSTALL_DIR}" FILE KPropertyCoreTargets.cmake) if(KPROPERTY_WIDGETS) set(CMAKECONFIG_WIDGETS_INSTALL_DIR "${CMAKECONFIG_INSTALL_PREFIX}/${KPROPERTYWIDGETS_BASE_NAME}") ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KPROPERTYWIDGETS VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfigVersion.cmake" ) ecm_configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KPropertyWidgetsConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfig.cmake" INSTALL_DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KPropertyWidgetsConfigVersion.cmake" DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" COMPONENT Devel) install(TARGETS KPropertyWidgets EXPORT KPropertyWidgetsTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(EXPORT KPropertyWidgetsTargets DESTINATION "${CMAKECONFIG_WIDGETS_INSTALL_DIR}" FILE KPropertyWidgetsTargets.cmake) endif() install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) if(KPROPERTY_GENERATE_PRI) ecm_generate_pri_file( BASE_NAME ${KPROPERTYCORE_BASE_NAME} LIB_NAME ${KPROPERTYCORE_BASE_NAME} DEPS "core" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KPROPERTYCORE_INCLUDE_INSTALL_DIR} ) if(KPROPERTY_WIDGETS) ecm_generate_pri_file( BASE_NAME ${KPROPERTYWIDGETS_BASE_NAME} LIB_NAME ${KPROPERTYWIDGETS_BASE_NAME} DEPS "widgets KPropertyCore" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KPROPERTYWIDGETS_INCLUDE_INSTALL_DIR} ) endif() endif() #ecm_install_icons(${DATA_INSTALL_DIR}/kproperty/icons) ecm_generate_headers(kpropertycore_FORWARDING_HEADERS REQUIRED_HEADERS kpropertycore_HEADERS ORIGINAL CAMELCASE HEADER_NAMES KProperty KPropertySet KPropertyFactory ) list(APPEND kpropertycore_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/kproperty_version.h) install( FILES ${kpropertycore_HEADERS} ${kpropertycore_FORWARDING_HEADERS} ${PROJECT_BINARY_DIR}/src/kpropertycore_export.h ${PROJECT_BINARY_DIR}/src/config-kproperty.h DESTINATION ${KPROPERTYCORE_INCLUDE_INSTALL_DIR} COMPONENT Devel ) if(KPROPERTY_WIDGETS) add_subdirectory(pics) ecm_generate_headers(kpropertywidgets_FORWARDING_HEADERS REQUIRED_HEADERS kpropertywidgets_HEADERS ORIGINAL CAMELCASE HEADER_NAMES KPropertyWidgetsFactory KPropertyWidgetsPluginManager KPropertyUtils KPropertyEditorView ) install( FILES ${kpropertywidgets_HEADERS} ${kpropertywidgets_FORWARDING_HEADERS} ${kproperty_editors_HEADERS} ${kproperty_editors_FORWARDING_HEADERS} ${PROJECT_BINARY_DIR}/src/kpropertywidgets_export.h DESTINATION ${KPROPERTYWIDGETS_INCLUDE_INSTALL_DIR} COMPONENT Devel ) endif() enable_testing() configure_file(config-kproperty.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kproperty.h) diff --git a/src/editors/3rdparty/KColorCollection b/src/editors/3rdparty/KColorCollection new file mode 100644 index 0000000..e0770c6 --- /dev/null +++ b/src/editors/3rdparty/KColorCollection @@ -0,0 +1 @@ +#include "KColorCollection.h" diff --git a/src/editors/3rdparty/KColorCollection.cpp b/src/editors/3rdparty/KColorCollection.cpp new file mode 100644 index 0000000..0296a24 --- /dev/null +++ b/src/editors/3rdparty/KColorCollection.cpp @@ -0,0 +1,282 @@ +/* This file is part of the KDE libraries + Copyright (C) 1999 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. + + 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. +*/ +//----------------------------------------------------------------------------- +// KDE color collection + +#include "KColorCollection.h" + +#include +#include +#include +#include +#include + +//BEGIN KColorCollectionPrivate +class KColorCollectionPrivate +{ +public: + KColorCollectionPrivate(const QString &); + KColorCollectionPrivate(const KColorCollectionPrivate &); + ~KColorCollectionPrivate() {} + struct ColorNode { + ColorNode(const QColor &c, const QString &n) + : color(c), name(n) {} + QColor color; + QString name; + }; + QList colorList; + + QString name; + QString desc; + KColorCollection::Editable editable; +}; + +KColorCollectionPrivate::KColorCollectionPrivate(const QString &_name) + : name(_name) +{ + if (name.isEmpty()) { + return; + } + + QString filename = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QLatin1String("colors/") + name); + if (filename.isEmpty()) { + return; + } + + QFile paletteFile(filename); + if (!paletteFile.exists()) { + return; + } + if (!paletteFile.open(QIODevice::ReadOnly)) { + return; + } + + // Read first line + // Expected "GIMP Palette" + QString line = QString::fromLocal8Bit(paletteFile.readLine()); + if (line.contains(QLatin1String(" Palette"))) { + return; + } + + while (!paletteFile.atEnd()) { + line = QString::fromLocal8Bit(paletteFile.readLine()); + if (line[0] == QLatin1Char('#')) { + // This is a comment line + line = line.mid(1); // Strip '#' + line = line.trimmed(); // Strip remaining white space.. + if (!line.isEmpty()) { + desc += line + QLatin1Char('\n'); // Add comment to description + } + } else { + // This is a color line, hopefully + line = line.trimmed(); + if (line.isEmpty()) { + continue; + } + int r, g, b; + int pos = 0; + if (sscanf(line.toLatin1().constData(), "%d %d %d%n", &r, &g, &b, &pos) >= 3) { + r = qBound(0, r, 255); + g = qBound(0, g, 255); + b = qBound(0, b, 255); + QString name = line.mid(pos).trimmed(); + colorList.append(ColorNode(QColor(r, g, b), name)); + } + } + } +} + +KColorCollectionPrivate::KColorCollectionPrivate(const KColorCollectionPrivate &p) + : colorList(p.colorList), name(p.name), desc(p.desc), editable(p.editable) +{ +} +//END KColorCollectionPrivate + +QStringList KColorCollection::installedCollections() +{ + QStringList paletteDirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, + QStringLiteral("colors"), + QStandardPaths::LocateDirectory); + + QStringList paletteList; + Q_FOREACH (const QString &dir, paletteDirs) { + paletteList += QDir(dir).entryList(QDir::Files | QDir::NoDotAndDotDot); + } + paletteList.removeDuplicates(); + + return paletteList; +} + +KColorCollection::KColorCollection(const QString &name) +{ + d = new KColorCollectionPrivate(name); +} + +KColorCollection::KColorCollection(const KColorCollection &p) +{ + d = new KColorCollectionPrivate(*p.d); +} + +KColorCollection::~KColorCollection() +{ + // Need auto-save? + delete d; +} + +bool +KColorCollection::save() +{ + QString filename = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/colors/") + + d->name; + QSaveFile sf(filename); + if (!sf.open(QIODevice::WriteOnly)) { + return false; + } + + QTextStream str(&sf); + + QString description = d->desc.trimmed(); + description = QLatin1Char('#') + description.split(QLatin1Char('\n'), QString::KeepEmptyParts).join(QStringLiteral("\n#")); + + str << QLatin1String("KDE RGB Palette\n"); + str << description << QLatin1Char('\n'); + foreach (const KColorCollectionPrivate::ColorNode &node, d->colorList) { + int r, g, b; + node.color.getRgb(&r, &g, &b); + str << r << " " << g << " " << b << " " << node.name << "\n"; + } + + return sf.commit(); +} + +QString KColorCollection::description() const +{ + return d->desc; +} + +void KColorCollection::setDescription(const QString &desc) +{ + d->desc = desc; +} + +QString KColorCollection::name() const +{ + return d->name; +} + +void KColorCollection::setName(const QString &name) +{ + d->name = name; +} + +KColorCollection::Editable KColorCollection::editable() const +{ + return d->editable; +} + +void KColorCollection::setEditable(Editable editable) +{ + d->editable = editable; +} + +int KColorCollection::count() const +{ + return (int) d->colorList.count(); +} + +KColorCollection & +KColorCollection::operator=(const KColorCollection &p) +{ + if (&p == this) { + return *this; + } + d->colorList = p.d->colorList; + d->name = p.d->name; + d->desc = p.d->desc; + d->editable = p.d->editable; + return *this; +} + +QColor +KColorCollection::color(int index) const +{ + if ((index < 0) || (index >= count())) { + return QColor(); + } + + return d->colorList[index].color; +} + +int +KColorCollection::findColor(const QColor &color) const +{ + for (int i = 0; i < d->colorList.size(); ++i) { + if (d->colorList[i].color == color) { + return i; + } + } + return -1; +} + +QString +KColorCollection::name(int index) const +{ + if ((index < 0) || (index >= count())) { + return QString(); + } + + return d->colorList[index].name; +} + +QString KColorCollection::name(const QColor &color) const +{ + return name(findColor(color)); +} + +int +KColorCollection::addColor(const QColor &newColor, const QString &newColorName) +{ + d->colorList.append(KColorCollectionPrivate::ColorNode(newColor, newColorName)); + return count() - 1; +} + +int +KColorCollection::changeColor(int index, + const QColor &newColor, + const QString &newColorName) +{ + if ((index < 0) || (index >= count())) { + return -1; + } + + KColorCollectionPrivate::ColorNode &node = d->colorList[index]; + node.color = newColor; + node.name = newColorName; + + return index; +} + +int KColorCollection::changeColor(const QColor &oldColor, + const QColor &newColor, + const QString &newColorName) +{ + return changeColor(findColor(oldColor), newColor, newColorName); +} + +#include "KColorCollection.moc" diff --git a/src/editors/3rdparty/KColorCollection.h b/src/editors/3rdparty/KColorCollection.h new file mode 100644 index 0000000..bc53113 --- /dev/null +++ b/src/editors/3rdparty/KColorCollection.h @@ -0,0 +1,203 @@ +/* This file is part of the KDE libraries + Copyright (C) 1999 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; version + 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +//----------------------------------------------------------------------------- +// KDE color collection. + +#ifndef KDELIBS_KCOLORCOLLECTION_H +#define KDELIBS_KCOLORCOLLECTION_H + +#include +#include + +/** + * Class for handling color collections ("palettes"). + * + * This class makes it easy to handle color collections, sometimes referred to + * as "palettes". This class can read and write collections from and to a file. + * + * This class uses the "GIMP" palette file format. + * + * @author Waldo Bastian (bastian@kde.org) + **/ +class KColorCollection +{ +public: + /** + * Query which KDE color collections are installed. + * + * @return A list with installed color collection names. + */ + static QStringList installedCollections(); + + /** + * KColorCollection constructor. Creates a KColorCollection from a file + * the filename is derived from the name. + * @param name The name of collection as returned by installedCollections() + **/ + explicit KColorCollection(const QString &name = QString()); + + /** + * KColorCollection copy constructor. + **/ + KColorCollection(const KColorCollection &); + + /** + * KColorCollection destructor. + **/ + ~KColorCollection(); + + /** + * KColorCollection assignment operator + **/ + KColorCollection &operator=(const KColorCollection &); + + /** + * Save the collection + * + * @return 'true' if successful + **/ + bool save(); + + /** + * Get the description of the collection. + * @return the description of the collection. + **/ + QString description() const; + + /** + * Set the description of the collection. + * @param desc the new description + **/ + void setDescription(const QString &desc); + + /** + * Get the name of the collection. + * @return the name of the collection + **/ + QString name() const; + + /** + * Set the name of the collection. + * @param name the name of the collection + **/ + void setName(const QString &name); + + /** + * Used to specify whether a collection may be edited. + * @see editable() + * @see setEditable() + */ + enum Editable { Yes, ///< Collection may be edited + No, ///< Collection may not be edited + Ask ///< Ask user before editing + }; + + /** + * Returns whether the collection may be edited. + * @return the state of the collection + **/ + Editable editable() const; + + /** + * Change whether the collection may be edited. + * @param editable the state of the collection + **/ + void setEditable(Editable editable); + + /** + * Return the number of colors in the collection. + * @return the number of colors + **/ + int count() const; + + /** + * Find color by index. + * @param index the index of the desired color + * @return The @p index -th color of the collection, null if not found. + **/ + QColor color(int index) const; + + /** + * Find index by @p color. + * @param color the color to find + * @return The index of the color in the collection or -1 if the + * color is not found. + **/ + int findColor(const QColor &color) const; + + /** + * Find color name by @p index. + * @param index the index of the color + * @return The name of the @p index -th color. + * Note that not all collections have named the colors. Null is + * returned if the color does not exist or has no name. + **/ + QString name(int index) const; + + /** + * Find color name by @p color. + * @return The name of color according to this collection. + * Note that not all collections have named the colors. + * Note also that each collection can give the same color + * a different name. + **/ + QString name(const QColor &color) const; + + /** + * Add a color. + * @param newColor The color to add. + * @param newColorName The name of the color, null to remove + * the name. + * @return The index of the added color. + **/ + int addColor(const QColor &newColor, + const QString &newColorName = QString()); + + /** + * Change a color. + * @param index Index of the color to change + * @param newColor The new color. + * @param newColorName The new color name, null to remove + * the name. + * @return The index of the new color or -1 if the color couldn't + * be changed. + **/ + int changeColor(int index, + const QColor &newColor, + const QString &newColorName = QString()); + + /** + * Change a color. + * @param oldColor The original color + * @param newColor The new color. + * @param newColorName The new color name, null to remove + * the name. + * @return The index of the new color or -1 if the color couldn't + * be changed. + **/ + int changeColor(const QColor &oldColor, + const QColor &newColor, + const QString &newColorName = QString()); + +private: + class KColorCollectionPrivate *d; +}; + +#endif // KDELIBS_KCOLORCOLLECTION_H + diff --git a/src/editors/3rdparty/KColorCombo b/src/editors/3rdparty/KColorCombo new file mode 100644 index 0000000..c22febb --- /dev/null +++ b/src/editors/3rdparty/KColorCombo @@ -0,0 +1 @@ +#include "KColorCombo.h" diff --git a/src/editors/3rdparty/KColorCombo.cpp b/src/editors/3rdparty/KColorCombo.cpp new file mode 100644 index 0000000..25c45d6 --- /dev/null +++ b/src/editors/3rdparty/KColorCombo.cpp @@ -0,0 +1,354 @@ +/* This file is part of the KDE libraries + Copyright (C) 1997 Martin Jones (mjones@kde.org) + Copyright (C) 2007 Pino Toscano (pino@kde.org) + Copyright (c) 2007 David Jarvie (software@astrojar.org.uk) + + 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 "KColorCombo.h" + +#include +#include +#include +#include + +class KColorComboDelegate : public QAbstractItemDelegate +{ +public: + enum ItemRoles { + ColorRole = Qt::UserRole + 1 + }; + + enum LayoutMetrics { + FrameMargin = 3 + }; + + KColorComboDelegate(QObject *parent = 0); + virtual ~KColorComboDelegate(); + + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; +}; + +static QBrush k_colorcombodelegate_brush(const QModelIndex &index, int role) +{ + QBrush brush; + QVariant v = index.data(role); + if (v.type() == QVariant::Brush) { + brush = v.value(); + } else if (v.type() == QVariant::Color) { + brush = QBrush(v.value()); + } + return brush; +} + +KColorComboDelegate::KColorComboDelegate(QObject *parent) + : QAbstractItemDelegate(parent) +{ +} + +KColorComboDelegate::~KColorComboDelegate() +{ +} + +void KColorComboDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + // background + QColor innercolor(Qt::white); + bool isSelected = (option.state & QStyle::State_Selected); + bool paletteBrush = (k_colorcombodelegate_brush(index, Qt::BackgroundRole).style() == Qt::NoBrush); + if (isSelected) { + innercolor = option.palette.color(QPalette::Highlight); + } else { + innercolor = option.palette.color(QPalette::Base); + } + // highlight selected item + QStyleOptionViewItem opt(option); + opt.showDecorationSelected = true; + QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); + QRect innerrect = option.rect.adjusted(FrameMargin, FrameMargin, -FrameMargin, -FrameMargin); + // inner color + QVariant cv = index.data(ColorRole); + if (cv.type() == QVariant::Color) { + QColor tmpcolor = cv.value(); + if (tmpcolor.isValid()) { + innercolor = tmpcolor; + paletteBrush = false; + painter->setPen(Qt::transparent); + painter->setBrush(innercolor); + QPainter::RenderHints tmpHint = painter->renderHints(); + painter->setRenderHint(QPainter::Antialiasing); + painter->drawRoundedRect(innerrect, 2, 2); + painter->setRenderHints(tmpHint); + painter->setBrush(Qt::NoBrush); + } + } + // text + QVariant tv = index.data(Qt::DisplayRole); + if (tv.type() == QVariant::String) { + QString text = tv.toString(); + QColor textColor; + if (paletteBrush) { + if (isSelected) { + textColor = option.palette.color(QPalette::HighlightedText); + } else { + textColor = option.palette.color(QPalette::Text); + } + } else { + int unused, v; + innercolor.getHsv(&unused, &unused, &v); + if (v > 128) { + textColor = Qt::black; + } else { + textColor = Qt::white; + } + } + painter->setPen(textColor); + painter->drawText(innerrect.adjusted(1, 1, -1, -1), text); + } +} + +QSize KColorComboDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + Q_UNUSED(index) + + // the width does not matter, as the view will always use the maximum width available + return QSize(100, option.fontMetrics.height() + 2 * FrameMargin); +} + +static const uchar standardPalette[][4] = { + { 255, 255, 255 }, // white + { 192, 192, 192 }, // light gray + { 160, 160, 160 }, // gray + { 128, 128, 128 }, // dark gray + { 0, 0, 0 }, // black + + { 255, 128, 128 }, //light red + { 255, 192, 128 }, //light orange + { 255, 255, 128 }, //light yellow + { 128, 255, 128 }, //light green + { 128, 255, 255 }, //cyan blue + { 128, 128, 255 }, //light blue + { 255, 128, 255 }, //light violet + { 255, 0, 0 }, //red + { 255, 128, 0 }, //orange + { 255, 255, 0 }, //yellow + { 0, 255, 0 }, //green + { 0, 255, 255 }, //light blue + { 0, 0, 255 }, //blue + { 255, 0, 255 }, //violet + { 128, 0, 0 }, //dark red + { 128, 64, 0 }, //dark orange + { 128, 128, 0 }, //dark yellow + { 0, 128, 0 }, //dark green + { 0, 128, 128 }, //dark light blue + { 0, 0, 128 }, //dark blue + { 128, 0, 128 } //dark violet +}; + +#define STANDARD_PALETTE_SIZE (int(sizeof(standardPalette) / sizeof(*standardPalette))) + +static inline QColor standardColor(int i) +{ + const uchar *entry = standardPalette[i]; + return QColor(entry[0], entry[1], entry[2]); +} + +class KColorComboPrivate +{ +public: + KColorComboPrivate(KColorCombo *qq); + + void addColors(); + void setCustomColor(const QColor &color, bool lookupInPresets = true); + + // slots + void _k_slotActivated(int index); + void _k_slotHighlighted(int index); + + KColorCombo *q; + QList colorList; + QColor customColor; + QColor internalcolor; +}; + +KColorComboPrivate::KColorComboPrivate(KColorCombo *qq) + : q(qq), customColor(Qt::white) +{ +} + +void KColorComboPrivate::setCustomColor(const QColor &color, bool lookupInPresets) +{ + if (lookupInPresets) { + if (colorList.isEmpty()) { + for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) { + if (standardColor(i) == color) { + q->setCurrentIndex(i + 1); + internalcolor = color; + return; + } + } + } else { + int i = colorList.indexOf(color); + if (i >= 0) { + q->setCurrentIndex(i + 1); + internalcolor = color; + return; + } + } + } + + internalcolor = color; + customColor = color; + q->setItemData(0, customColor, KColorComboDelegate::ColorRole); +} + +KColorCombo::KColorCombo(QWidget *parent) + : QComboBox(parent), d(new KColorComboPrivate(this)) +{ + setItemDelegate(new KColorComboDelegate(this)); + d->addColors(); + + connect(this, SIGNAL(activated(int)), SLOT(_k_slotActivated(int))); + connect(this, SIGNAL(highlighted(int)), SLOT(_k_slotHighlighted(int))); + + // select the white color + setCurrentIndex(1); + d->_k_slotActivated(1); + + setMaxVisibleItems(13); +} + +KColorCombo::~KColorCombo() +{ + delete d; +} + +void KColorCombo::setColors(const QList &colors) +{ + clear(); + d->colorList = colors; + d->addColors(); +} + +QList KColorCombo::colors() const +{ + if (d->colorList.isEmpty()) { + QList list; + for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) { + list += standardColor(i); + } + return list; + } else { + return d->colorList; + } +} + +void KColorCombo::setColor(const QColor &col) +{ + if (!col.isValid()) { + return; + } + + if (count() == 0) { + d->addColors(); + } + + d->setCustomColor(col, true); +} + +QColor KColorCombo::color() const +{ + return d->internalcolor; +} + +bool KColorCombo::isCustomColor() const +{ + return d->internalcolor == d->customColor; +} + +void KColorCombo::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) + QStylePainter painter(this); + painter.setPen(palette().color(QPalette::Text)); + + QStyleOptionComboBox opt; + initStyleOption(&opt); + painter.drawComplexControl(QStyle::CC_ComboBox, opt); + + QRect frame = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this); + painter.setRenderHint(QPainter::Antialiasing); + painter.setPen(Qt::transparent); + painter.setBrush(QBrush(d->internalcolor)); + painter.drawRoundedRect(frame.adjusted(1, 1, -1, -1), 2, 2); +} + +void KColorCombo::showEmptyList() +{ + clear(); +} + +void KColorComboPrivate::_k_slotActivated(int index) +{ + if (index == 0) { + QColor c = QColorDialog::getColor(customColor, q); + if (c.isValid()) { + customColor = c; + setCustomColor(customColor, false); + } + } else if (colorList.isEmpty()) { + internalcolor = standardColor(index - 1); + } else { + internalcolor = colorList[index - 1]; + } + + emit q->activated(internalcolor); +} + +void KColorComboPrivate::_k_slotHighlighted(int index) +{ + if (index == 0) { + internalcolor = customColor; + } else if (colorList.isEmpty()) { + internalcolor = standardColor(index - 1); + } else { + internalcolor = colorList[index - 1]; + } + + emit q->highlighted(internalcolor); +} + +void KColorComboPrivate::addColors() +{ + q->addItem(KColorCombo::tr("Custom...", "Custom color")); + + if (colorList.isEmpty()) { + for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) { + q->addItem(QString()); + q->setItemData(i + 1, standardColor(i), KColorComboDelegate::ColorRole); + } + } else { + for (int i = 0, count = colorList.count(); i < count; ++i) { + q->addItem(QString()); + q->setItemData(i + 1, colorList[i], KColorComboDelegate::ColorRole); + } + } +} + +#include "moc_KColorCombo.cpp" diff --git a/src/editors/3rdparty/KColorCombo.h b/src/editors/3rdparty/KColorCombo.h new file mode 100644 index 0000000..bd665ec --- /dev/null +++ b/src/editors/3rdparty/KColorCombo.h @@ -0,0 +1,113 @@ +/* This file is part of the KDE libraries + Copyright (C) 1997 Martin Jones (mjones@kde.org) + Copyright (c) 2007 David Jarvie (software@astrojar.org.uk) + + 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. +*/ +//----------------------------------------------------------------------------- +// KDE color selection combo box + +// layout management added Oct 1997 by Mario Weilguni +// + +#ifndef KCOLORCOMBO_H +#define KCOLORCOMBO_H + +#include +#include + +class KColorComboPrivate; + +/** + * Combobox for colors. + * + * The combobox provides some preset colors to be selected, and an entry to + * select a custom color using a color dialog. + * + * \image html kcolorcombo.png "KDE Color Combo Box" + */ +class KColorCombo : public QComboBox +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY activated USER true) + Q_PROPERTY(QList colors READ colors WRITE setColors) + +public: + /** + * Constructs a color combo box. + */ + explicit KColorCombo(QWidget *parent = 0); + ~KColorCombo(); + + /** + * Selects the color @p col. + */ + void setColor(const QColor &col); + + /** + * Returns the currently selected color. + **/ + QColor color() const; + + /** + * Find whether the currently selected color is a custom color selected + * using a color dialog. + **/ + bool isCustomColor() const; + + /** + * Set a custom list of colors to choose from, in place of the standard + * list. + * @param cols list of colors. If empty, the selection list reverts to + * the standard list. + **/ + void setColors(const QList &colors); + + /** + * Return the list of colors available for selection. + * @return list of colors + **/ + QList colors() const; + + /** + * Clear the color list and don't show it, till the next setColor() call + **/ + void showEmptyList(); + +Q_SIGNALS: + /** + * Emitted when a new color box has been selected. + */ + void activated(const QColor &col); + /** + * Emitted when a new item has been highlighted. + */ + void highlighted(const QColor &col); + +protected: + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + +private: + friend class KColorComboPrivate; + KColorComboPrivate *const d; + + Q_DISABLE_COPY(KColorCombo) + + Q_PRIVATE_SLOT(d, void _k_slotActivated(int)) + Q_PRIVATE_SLOT(d, void _k_slotHighlighted(int)) +}; + +#endif // KCOLORCOMBO_H diff --git a/src/editors/coloredit.cpp b/src/editors/coloredit.cpp index a2ce097..0f220f4 100644 --- a/src/editors/coloredit.cpp +++ b/src/editors/coloredit.cpp @@ -1,114 +1,124 @@ /* This file is part of the KDE project Copyright (C) 2010-2015 Jarosław Staniek 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 "coloredit.h" #include "KPropertyUtils_p.h" -#include +#include -#ifdef KPROPERTY_KF +#include #include Q_GLOBAL_STATIC_WITH_ARGS(KColorCollection, g_oxygenColors, (QLatin1String("Oxygen.colors"))) -KPropertyColorComboEditor::KPropertyColorComboEditor(QWidget *parent) - : KColorCombo(parent) +class Q_DECL_HIDDEN KPropertyColorComboEditor::Private { - connect(this, SIGNAL(activated(QColor)), this, SLOT(slotValueChanged(QColor))); +public: + Private() {} + KColorCombo *combo; +}; +KPropertyColorComboEditor::KPropertyColorComboEditor(QWidget *parent) + : QWidget(parent) + , d(new Private) +{ + QHBoxLayout *lyr = new QHBoxLayout(this); + lyr->setMargin(0); + d->combo = new KColorCombo; + connect(d->combo, SIGNAL(activated(QColor)), this, SLOT(slotValueChanged(QColor))); QList< QColor > colors; const int oxygenColorsCount = g_oxygenColors->count(); for (int i = 0; i < oxygenColorsCount; i++) { colors += g_oxygenColors->color(i); } - setColors(colors); + d->combo->setColors(colors); + lyr->addWidget(d->combo); + setFocusProxy(d->combo); int paddingTop = 1; if (!KPropertyUtils::gridLineColor(this).isValid()) { - setFrame(false); + d->combo->setFrame(false); paddingTop = 0; } QString styleSheet = QString::fromLatin1("QComboBox { \ border: 1px; \ padding-top: %1px; padding-left: 1px; }").arg(paddingTop); - setStyleSheet(styleSheet); + d->combo->setStyleSheet(styleSheet); } KPropertyColorComboEditor::~KPropertyColorComboEditor() { + delete d; } QVariant KPropertyColorComboEditor::value() const { - return color(); + return d->combo->color(); } void KPropertyColorComboEditor::setValue(const QVariant &value) { - setColor(value.value()); + d->combo->setColor(value.value()); } void KPropertyColorComboEditor::slotValueChanged(const QColor&) { emit commitData(this); } -#endif // KPROPERTY_KF + +// ------------------- QWidget * KPropertyColorComboDelegate::createEditor(int type, QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(type) Q_UNUSED(option) Q_UNUSED(index) -#ifdef KPROPERTY_KF return new KPropertyColorComboEditor(parent); -#else - return KPropertyEditorCreatorInterface::createEditor(type, parent, option, index); -#endif } static QString colorToName(const QColor &color, const QLocale &locale) { if (!color.isValid()) { return locale.language() == QLocale::C ? QString::fromLatin1("#invalid") : QObject::tr("#invalid", "Invalid color"); } return color.alpha() == 255 ? color.name(QColor::HexRgb) : color.name(QColor::HexArgb); } void KPropertyColorComboDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const { const KPropertyUtils::PainterSaver saver(painter); const QBrush b(index.data(Qt::EditRole).value()); painter->setBrush(b); painter->setPen(QPen(Qt::NoPen)); painter->drawRect(option.rect); painter->setBrush(KPropertyUtils::contrastColor(b.color())); painter->setPen(KPropertyUtils::contrastColor(b.color())); QFont f(option.font); f.setFamily(QLatin1String("courier")); painter->setFont(f); painter->drawText(option.rect, Qt::AlignCenter, colorToName(b.color(), QLocale())); } QString KPropertyColorComboDelegate::valueToString(const QVariant& value, const QLocale &locale) const { return colorToName(value.value(), locale); } diff --git a/src/editors/coloredit.h b/src/editors/coloredit.h index 54a0fe5..68c0d40 100644 --- a/src/editors/coloredit.h +++ b/src/editors/coloredit.h @@ -1,72 +1,71 @@ /* This file is part of the KDE project Copyright (C) 2010-2015 Jarosław Staniek 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 KPROPERTY_COLOREDIT_H #define KPROPERTY_COLOREDIT_H #include "KPropertyWidgetsFactory.h" -#include "config-kproperty.h" - -#ifdef KPROPERTY_KF -#include //! Color combo box //! @todo enable transparency selection //! @todo add transparency option //! @todo reimplement view using KColorCells -class KPROPERTYWIDGETS_EXPORT KPropertyColorComboEditor : public KColorCombo +class KPROPERTYWIDGETS_EXPORT KPropertyColorComboEditor : public QWidget { Q_OBJECT Q_PROPERTY(QVariant value READ value WRITE setValue USER true) public: explicit KPropertyColorComboEditor(QWidget *parent = 0); ~KPropertyColorComboEditor(); QVariant value() const; Q_SIGNALS: void commitData( QWidget * editor ); public Q_SLOTS: void setValue(const QVariant &value); protected Q_SLOTS: void slotValueChanged(const QColor&); + +private: + class Private; + Private * const d; }; -#endif // KPROPERTY_KF class KPROPERTYWIDGETS_EXPORT KPropertyColorComboDelegate : public KPropertyEditorCreatorInterface, public KPropertyValuePainterInterface, public KPropertyValueDisplayInterface { public: KPropertyColorComboDelegate() {} virtual QWidget * createEditor( int type, QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; virtual void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const; virtual QString valueToString(const QVariant& value, const QLocale &locale) const; }; #endif