KProperty

coloredit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2010-2016 Jarosław Staniek <[email protected]>
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 
30 Q_GLOBAL_STATIC_WITH_ARGS(KColorCollection, g_oxygenColors, (QLatin1String("Oxygen.colors")))
31 
32 static 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 
44 class ColorCodePainter
45 {
46 public:
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 
58 private:
59  QFont m_font;
60 };
61 
62 // -------------------
63 
64 class Q_DECL_HIDDEN KPropertyColorComboEditor::Private
65 {
66 public:
67  Private() {}
68  KColorCombo *combo;
69  QWidget *colorCodeOverlay;
70  QScopedPointer<ColorCodePainter> colorCodePainter;
71 };
72 
73 KPropertyColorComboEditor::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 
106 KPropertyColorComboEditor::~KPropertyColorComboEditor()
107 {
108  delete d;
109 }
110 
111 QVariant KPropertyColorComboEditor::value() const
112 {
113  return d->combo->color();
114 }
115 
116 void KPropertyColorComboEditor::setValue(const QVariant &value)
117 {
118  d->combo->setColor(value.value<QColor>());
119 }
120 
121 void KPropertyColorComboEditor::slotValueChanged(const QColor&)
122 {
123  emit commitData(this);
124 }
125 
126 bool 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 
148 class Q_DECL_HIDDEN KPropertyColorComboDelegate::Private
149 {
150 public:
151  Private() {}
152  QScopedPointer<ColorCodePainter> colorCodePainter;
153 };
154 
155 KPropertyColorComboDelegate::KPropertyColorComboDelegate()
156  : d(new Private)
157 {
158 }
159 
160 KPropertyColorComboDelegate::~KPropertyColorComboDelegate()
161 {
162  delete d;
163 }
164 
165 QWidget * 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 
174 void 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 
188 QString KPropertyColorComboDelegate::valueToString(const QVariant& value, const QLocale &locale) const
189 {
190  return colorToName(value.value<QColor>(), locale);
191 }
QString styleSheet() const const
QEvent::Type type() const const
T value() const const
Color combo box.
Definition: coloredit.h:29
AlignCenter
int width() const const
void setFont(const QFont &font)
int count(const T &value) const const
void installEventFilter(QObject *filterObj)
Combobox for colors.
Definition: KColorCombo.h:42
void setPen(const QColor &color)
EditRole
void setFocusProxy(QWidget *w)
void activated(int index)
void drawText(const QPointF &position, const QString &text)
virtual bool eventFilter(QObject *watched, QEvent *event)
const QFont & font() const const
Class for handling color collections ("palettes").
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QWidget(QWidget *parent, Qt::WindowFlags f)
QString fromLatin1(const char *str, int size)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
CustomizeWindowHint
int height() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.