File indexing completed on 2024-12-08 08:10:25

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *   Copyright (C) 2020 by Pino Toscano                                    *
0005  *   pino@kde.org                                                          *
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or modify  *
0008  *   it under the terms of the GNU General Public License as published by  *
0009  *   the Free Software Foundation; either version 2 of the License, or     *
0010  *   (at your option) any later version.                                   *
0011  ***************************************************************************/
0012 
0013 #include "colorutils.h"
0014 
0015 #include <KColorCombo>
0016 
0017 namespace ColorUtils
0018 {
0019 
0020 /**
0021 Internal implementation based on KColorCombo, with the sole purpose
0022 of making setColor() available as slot.
0023 */
0024 class ColorCombo : public KColorCombo
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     ColorCombo(QWidget *parent = nullptr)
0030         : KColorCombo(parent)
0031     {
0032     }
0033 
0034 public Q_SLOTS:
0035     void setColor(const QColor &col)
0036     {
0037         KColorCombo::setColor(col);
0038     }
0039 };
0040 
0041 QColor palette_qtstandard[] = {
0042     Qt::red,
0043     Qt::green,
0044     Qt::blue,
0045     Qt::cyan,
0046     Qt::magenta,
0047     Qt::yellow,
0048     Qt::darkRed,
0049     Qt::darkGreen,
0050     Qt::darkBlue,
0051     Qt::darkCyan,
0052     Qt::darkMagenta,
0053     Qt::darkYellow,
0054     Qt::white,
0055     Qt::lightGray,
0056     Qt::gray,
0057     Qt::darkGray,
0058     Qt::black
0059 };
0060 size_t palette_qtstandard_len = sizeof(palette_qtstandard) / sizeof(QColor);
0061 QColor palette_led[] = {
0062     "#f62a2a",
0063     "#ff7733",
0064     "#ffbb33",
0065     "#eeee22",
0066     "#4cc308",
0067     "#22aaee"
0068 };
0069 size_t palette_led_len = sizeof(palette_led) / sizeof(QColor);
0070 
0071 KColorCombo *createColorCombo(ColorScheme colorScheme, QWidget *parent)
0072 {
0073     QColor *palette = nullptr;
0074     size_t palette_len;
0075 
0076     switch (colorScheme) {
0077     case QtStandard:
0078         palette = palette_qtstandard;
0079         palette_len = palette_qtstandard_len;
0080         break;
0081     case LED:
0082         palette = palette_led;
0083         palette_len = palette_led_len;
0084         break;
0085     }
0086 
0087     if (!palette)
0088         return nullptr;
0089 
0090     ColorCombo *combo = new ColorCombo(parent);
0091 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
0092     combo->setColors(QList<QColor>(palette, palette + palette_len));
0093 #else
0094     QList<QColor> colorList;
0095     for (size_t colorNr = 0; colorNr < palette_len; ++colorNr) {
0096         colorList.push_back(palette[colorNr]);
0097     }
0098     combo->setColors(colorList);
0099 #endif
0100     return combo;
0101 }
0102 
0103 }
0104 
0105 #include "colorutils.moc"