KProperty

coloredit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2010-2016 Jarosław Staniek <staniek@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "coloredit.h"
21#include "combobox.h"
22#include "KPropertyUtils_p.h"
23
24#include <QEvent>
25#include <QHBoxLayout>
26
27#include <KColorCombo>
28#include <KColorCollection>
29
30Q_GLOBAL_STATIC_WITH_ARGS(KColorCollection, g_oxygenColors, (QLatin1String("Oxygen.colors")))
31
32static QString colorToName(const QColor &color, const QLocale &locale)
33{
34 if (!color.isValid()) {
35 return locale.language() == QLocale::C ? QString::fromLatin1("#invalid")
36 : QObject::tr("#invalid", "Invalid color");
37 }
38 return color.alpha() == 255 ? color.name(QColor::HexRgb) : color.name(QColor::HexArgb);
39}
40
41//! @brief Paints color code
42//! Useful for caching the font.
43//! @internal
44class ColorCodePainter
45{
46public:
47 explicit ColorCodePainter(const QFont &font)
48 : m_font(font)
49 {
50 m_font.setFamily(QLatin1String("courier"));
51 }
52 void paint(QPainter *painter, const QRect &rect, const QColor &color) {
53 painter->setPen(KPropertyUtilsPrivate::contrastColor(color));
54 painter->setFont(m_font);
55 painter->drawText(rect, Qt::AlignCenter, colorToName(color, QLocale()));
56 }
57
58private:
59 QFont m_font;
60};
61
62// -------------------
63
64class Q_DECL_HIDDEN KPropertyColorComboEditor::Private
65{
66public:
67 Private() {}
68 KColorCombo *combo;
69 QWidget *colorCodeOverlay;
70 QScopedPointer<ColorCodePainter> colorCodePainter;
71};
72
73KPropertyColorComboEditor::KPropertyColorComboEditor(QWidget *parent)
74 : QWidget(parent)
75 , d(new Private)
76{
77 installEventFilter(this); // handle size of the combo
78 d->colorCodePainter.reset(new ColorCodePainter(font()));
79 d->combo = new KColorCombo(this);
80 connect(d->combo, SIGNAL(activated(QColor)), this, SLOT(slotValueChanged(QColor)));
81 d->combo->installEventFilter(this); // handle size of the overlay
82 d->colorCodeOverlay = new QWidget(d->combo, Qt::CustomizeWindowHint | Qt::WindowTransparentForInput);
83 d->colorCodeOverlay->raise();
84 d->colorCodeOverlay->installEventFilter(this); // handle painting of the overlay
85 QList< QColor > colors;
86 const int oxygenColorsCount = g_oxygenColors->count();
87 for (int i = 0; i < oxygenColorsCount; i++) {
88 colors += g_oxygenColors->color(i);
89 }
90 d->combo->setColors(colors);
91 setFocusProxy(d->combo);
92
93 int paddingTop = 1;
94 if (!KPropertyUtilsPrivate::gridLineColor(this).isValid()) {
95 d->combo->setFrame(false);
96 paddingTop = 0;
97 }
98 QString styleSheet = QString::fromLatin1("KPropertyColorComboEditor { \
99 %1; \
100 padding-top: %2px; padding-left: 1px; }")
101 .arg(KPropertyComboBoxEditor::borderSheet(this))
102 .arg(paddingTop);
103 setStyleSheet(styleSheet);
104}
105
106KPropertyColorComboEditor::~KPropertyColorComboEditor()
107{
108 delete d;
109}
110
111QVariant KPropertyColorComboEditor::value() const
112{
113 return d->combo->color();
114}
115
116void KPropertyColorComboEditor::setValue(const QVariant &value)
117{
118 d->combo->setColor(value.value<QColor>());
119}
120
121void KPropertyColorComboEditor::slotValueChanged(const QColor&)
122{
123 emit commitData(this);
124}
125
126bool KPropertyColorComboEditor::eventFilter(QObject *o, QEvent *e)
127{
128 const bool result = QWidget::eventFilter(o, e);
129 if (o == d->colorCodeOverlay) {
130 if (e->type() == QEvent::Paint) {
131 QPainter painter(d->colorCodeOverlay);
132 d->colorCodePainter->paint(&painter, d->colorCodeOverlay->rect(), d->combo->color());
133 }
134 } else if (o == d->combo) {
135 if (e->type() == QEvent::Resize) {
136 d->colorCodeOverlay->setGeometry(0, 0, d->combo->width(), d->combo->height());
137 }
138 } else if (o == this) {
139 if (e->type() == QEvent::Resize) {
140 d->combo->setGeometry(0, 0, width(), height()+1);
141 }
142 }
143 return result;
144}
145
146// -------------------
147
148class Q_DECL_HIDDEN KPropertyColorComboDelegate::Private
149{
150public:
151 Private() {}
152 QScopedPointer<ColorCodePainter> colorCodePainter;
153};
154
155KPropertyColorComboDelegate::KPropertyColorComboDelegate()
156 : d(new Private)
157{
158}
159
160KPropertyColorComboDelegate::~KPropertyColorComboDelegate()
161{
162 delete d;
163}
164
165QWidget * KPropertyColorComboDelegate::createEditor(int type, QWidget *parent,
166 const QStyleOptionViewItem &option, const QModelIndex &index) const
167{
168 Q_UNUSED(type)
169 Q_UNUSED(option)
170 Q_UNUSED(index)
171 return new KPropertyColorComboEditor(parent);
172}
173
174void KPropertyColorComboDelegate::paint( QPainter * painter,
175 const QStyleOptionViewItem & option, const QModelIndex & index ) const
176{
177 const KPropertyUtilsPrivate::PainterSaver saver(painter);
178 const QBrush b(index.data(Qt::EditRole).value<QColor>());
179 painter->setBrush(b);
180 painter->setPen(QPen(Qt::NoPen));
181 painter->drawRect(option.rect);
182 if (!d->colorCodePainter) {
183 d->colorCodePainter.reset(new ColorCodePainter(option.font));
184 }
185 d->colorCodePainter->paint(painter, option.rect, b.color());
186}
187
188QString KPropertyColorComboDelegate::valueToString(const QVariant& value, const QLocale &locale) const
189{
190 return colorToName(value.value<QColor>(), locale);
191}
void setColor(const QColor &col)
Color combo box.
Definition coloredit.h:30
bool isValid(QStringView ifopt)
Type type() const const
void setFamily(const QString &family)
qsizetype count() const const
Language language() const const
QVariant data(int role) const const
virtual bool eventFilter(QObject *watched, QEvent *event)
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
void setBrush(Qt::BrushStyle style)
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
void reset(T *other)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
AlignCenter
EditRole
CustomizeWindowHint
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
T value() const const
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.