• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
combobox.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "combobox.h"
21
22#include <QPainter>
23#include <QGraphicsView>
24
25#include <kcombobox.h>
26#include <kiconeffect.h>
27#include <kiconloader.h>
28#include <kmimetype.h>
29
30#include "applet.h"
31#include "framesvg.h"
32#include "private/style_p.h"
33#include "private/focusindicator_p.h"
34#include "private/themedwidgetinterface_p.h"
35#include "theme.h"
36
37namespace Plasma
38{
39
40class ComboBoxPrivate : public ThemedWidgetInterface<ComboBox>
41{
42public:
43 ComboBoxPrivate(ComboBox *comboBox)
44 : ThemedWidgetInterface<ComboBox>(comboBox),
45 background(0),
46 underMouse(false)
47 {
48 }
49
50 ~ComboBoxPrivate()
51 {
52 }
53
54 void syncActiveRect();
55 void syncBorders();
56
57 FrameSvg *background;
58 FrameSvg *lineEditBackground;
59 FocusIndicator *focusIndicator;
60 int animId;
61 qreal opacity;
62 QRectF activeRect;
63 Style::Ptr style;
64 bool underMouse;
65};
66
67void ComboBoxPrivate::syncActiveRect()
68{
69 background->setElementPrefix("normal");
70
71 qreal left, top, right, bottom;
72 background->getMargins(left, top, right, bottom);
73
74 background->setElementPrefix("active");
75 qreal activeLeft, activeTop, activeRight, activeBottom;
76 background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
77
78 activeRect = QRectF(QPointF(0, 0), q->size());
79 activeRect.adjust(left - activeLeft, top - activeTop,
80 -(right - activeRight), -(bottom - activeBottom));
81
82 background->setElementPrefix("normal");
83}
84
85void ComboBoxPrivate::syncBorders()
86{
87 //set margins from the normal element
88 qreal left, top, right, bottom;
89
90 background->setElementPrefix("normal");
91 background->getMargins(left, top, right, bottom);
92 q->setContentsMargins(left, top, right, bottom);
93
94 //calc the rect for the over effect
95 syncActiveRect();
96
97 if (customFont) {
98 q->setFont(q->font());
99 } else {
100 q->setFont(Theme::defaultTheme()->font(Theme::DefaultFont));
101 customFont = false;
102 }
103
104 if (q->nativeWidget()->isEditable()) {
105 focusIndicator->setFrameSvg(lineEditBackground);
106 } else {
107 focusIndicator->setFrameSvg(background);
108 }
109 focusIndicator->setFlag(QGraphicsItem::ItemStacksBehindParent, !q->nativeWidget()->isEditable() || !lineEditBackground->hasElement("hint-focus-over-base"));
110}
111
112
113ComboBox::ComboBox(QGraphicsWidget *parent)
114 : QGraphicsProxyWidget(parent),
115 d(new ComboBoxPrivate(this))
116{
117 d->background = new FrameSvg(this);
118 d->background->setImagePath("widgets/button");
119 d->background->setCacheAllRenderedFrames(true);
120 d->background->setElementPrefix("normal");
121 d->lineEditBackground = new FrameSvg(this);
122 d->lineEditBackground->setImagePath("widgets/lineedit");
123 d->lineEditBackground->setCacheAllRenderedFrames(true);
124 setZValue(900);
125
126 setAcceptHoverEvents(true);
127
128 d->style = Style::sharedStyle();
129
130 d->focusIndicator = new FocusIndicator(this, d->background);
131 setNativeWidget(new KComboBox);
132 connect(d->background, SIGNAL(repaintNeeded()), SLOT(syncBorders()));
133 d->initTheming();
134}
135
136ComboBox::~ComboBox()
137{
138 delete d;
139 Style::doneWithSharedStyle();
140}
141
142QString ComboBox::text() const
143{
144 return static_cast<KComboBox*>(widget())->currentText();
145}
146
147void ComboBox::setStyleSheet(const QString &stylesheet)
148{
149 widget()->setStyleSheet(stylesheet);
150}
151
152QString ComboBox::styleSheet()
153{
154 return widget()->styleSheet();
155}
156
157void ComboBox::setNativeWidget(KComboBox *nativeWidget)
158{
159 if (widget()) {
160 widget()->deleteLater();
161 }
162
163 connect(nativeWidget, SIGNAL(activated(QString)), this, SIGNAL(activated(QString)));
164 connect(nativeWidget, SIGNAL(currentIndexChanged(int)),
165 this, SIGNAL(currentIndexChanged(int)));
166 connect(nativeWidget, SIGNAL(currentIndexChanged(QString)),
167 this, SIGNAL(textChanged(QString)));
168
169 d->setWidget(nativeWidget);
170 nativeWidget->setWindowIcon(QIcon());
171
172 nativeWidget->setAttribute(Qt::WA_NoSystemBackground);
173 nativeWidget->setStyle(d->style.data());
174
175 d->syncBorders();
176}
177
178KComboBox *ComboBox::nativeWidget() const
179{
180 return static_cast<KComboBox*>(widget());
181}
182
183void ComboBox::addItem(const QString &text)
184{
185 static_cast<KComboBox*>(widget())->addItem(text);
186}
187
188void ComboBox::clear()
189{
190 static_cast<KComboBox*>(widget())->clear();
191}
192
193void ComboBox::resizeEvent(QGraphicsSceneResizeEvent *event)
194{
195 if (d->background) {
196 //resize needed panels
197 d->syncActiveRect();
198
199 d->background->setElementPrefix("focus");
200 d->background->resizeFrame(size());
201
202 d->background->setElementPrefix("active");
203 d->background->resizeFrame(d->activeRect.size());
204
205 d->background->setElementPrefix("normal");
206 d->background->resizeFrame(size());
207 }
208
209 QGraphicsProxyWidget::resizeEvent(event);
210}
211
212void ComboBox::paint(QPainter *painter,
213 const QStyleOptionGraphicsItem *option,
214 QWidget *widget)
215{
216
217 if (!styleSheet().isNull() ||
218 Theme::defaultTheme()->useNativeWidgetStyle()) {
219 QGraphicsProxyWidget::paint(painter, option, widget);
220 return;
221 }
222
223 if (nativeWidget()->isEditable()) {
224 QGraphicsProxyWidget::paint(painter, option, widget);
225 return;
226 }
227
228 QPixmap bufferPixmap;
229
230 //normal button
231 if (isEnabled()) {
232 d->background->setElementPrefix("normal");
233
234 d->background->paintFrame(painter);
235 //disabled widget
236 } else {
237 bufferPixmap = QPixmap(rect().size().toSize());
238 bufferPixmap.fill(Qt::transparent);
239
240 QPainter buffPainter(&bufferPixmap);
241 d->background->paintFrame(&buffPainter);
242 buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
243 buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 128));
244
245 painter->drawPixmap(0, 0, bufferPixmap);
246 }
247
248 painter->setPen(Theme::defaultTheme()->color(Theme::ButtonTextColor));
249
250 QStyleOptionComboBox comboOpt;
251
252 comboOpt.initFrom(nativeWidget());
253
254 comboOpt.palette.setColor(
255 QPalette::ButtonText, Theme::defaultTheme()->color(Theme::ButtonTextColor));
256 comboOpt.currentIcon = nativeWidget()->itemIcon(
257 nativeWidget()->currentIndex());
258 comboOpt.currentText = nativeWidget()->itemText(
259 nativeWidget()->currentIndex());
260 comboOpt.editable = false;
261
262 nativeWidget()->style()->drawControl(
263 QStyle::CE_ComboBoxLabel, &comboOpt, painter, nativeWidget());
264 comboOpt.rect = nativeWidget()->style()->subControlRect(
265 QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxArrow, nativeWidget());
266 nativeWidget()->style()->drawPrimitive(
267 QStyle::PE_IndicatorArrowDown, &comboOpt, painter, nativeWidget());
268}
269
270void ComboBox::focusInEvent(QFocusEvent *event)
271{
272 QGraphicsProxyWidget::focusInEvent(event);
273}
274
275void ComboBox::focusOutEvent(QFocusEvent *event)
276{
277 QGraphicsWidget *widget = parentWidget();
278 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget);
279
280 while (!applet && widget) {
281 widget = widget->parentWidget();
282 applet = qobject_cast<Plasma::Applet *>(widget);
283 }
284
285 if (applet) {
286 applet->setStatus(Plasma::UnknownStatus);
287 }
288
289 if (nativeWidget()->isEditable()) {
290 QEvent closeEvent(QEvent::CloseSoftwareInputPanel);
291 if (qApp) {
292 if (QGraphicsView *view = qobject_cast<QGraphicsView*>(qApp->focusWidget())) {
293 if (view->scene() && view->scene() == scene()) {
294 QApplication::sendEvent(view, &closeEvent);
295 }
296 }
297 }
298 }
299
300 QGraphicsProxyWidget::focusOutEvent(event);
301}
302
303void ComboBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
304{
305 d->underMouse = true;
306 QGraphicsProxyWidget::hoverEnterEvent(event);
307}
308
309void ComboBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
310{
311 d->underMouse = false;
312 QGraphicsProxyWidget::hoverLeaveEvent(event);
313}
314
315void ComboBox::changeEvent(QEvent *event)
316{
317 d->changeEvent(event);
318 QGraphicsProxyWidget::changeEvent(event);
319}
320
321void ComboBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
322{
323 QGraphicsWidget *widget = parentWidget();
324 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget);
325
326 while (!applet && widget) {
327 widget = widget->parentWidget();
328 applet = qobject_cast<Plasma::Applet *>(widget);
329 }
330
331 if (applet) {
332 applet->setStatus(Plasma::AcceptingInputStatus);
333 }
334 QGraphicsProxyWidget::mousePressEvent(event);
335}
336
337int ComboBox::count() const
338{
339 return nativeWidget()->count();
340}
341
342int ComboBox::currentIndex() const
343{
344 return nativeWidget()->currentIndex();
345}
346
347void ComboBox::setCurrentIndex(int index)
348{
349 nativeWidget()->setCurrentIndex(index);
350}
351
352} // namespace Plasma
353
354#include <combobox.moc>
355
applet.h
Plasma::Applet
The base Applet class.
Definition: applet.h:78
Plasma::Applet::setStatus
void setStatus(const ItemStatus stat)
sets the status for this applet
Definition: applet.cpp:1198
Plasma::ComboBox::activated
void activated(const QString &text)
This signal is sent when the user chooses an item in the combobox.
Plasma::ComboBox::setNativeWidget
void setNativeWidget(KComboBox *nativeWidget)
Sets the combo box wrapped by this ComboBox (widget must inherit KComboBox), ownership is transferred...
Definition: combobox.cpp:157
Plasma::ComboBox::nativeWidget
KComboBox * nativeWidget
Definition: combobox.h:46
Plasma::ComboBox::addItem
Q_INVOKABLE void addItem(const QString &text)
Adds an item to the combo box with the given text.
Definition: combobox.cpp:183
Plasma::ComboBox::text
QString text
Definition: combobox.h:44
Plasma::ComboBox::currentIndexChanged
void currentIndexChanged(int index)
This signal is sent whenever the currentIndex in the combobox changes either through user interaction...
Plasma::ComboBox::styleSheet
QString styleSheet
Definition: combobox.h:45
Plasma::ComboBox::currentIndex
int currentIndex
Definition: combobox.h:48
Plasma::ComboBox::parentWidget
QGraphicsWidget * parentWidget
Definition: combobox.h:43
Plasma::ComboBox::textChanged
void textChanged(const QString &text)
This signal is sent whenever the currentIndex in the combobox changes either through user interaction...
Plasma::ComboBox::clear
void clear()
Definition: combobox.cpp:188
Plasma::FrameSvg
Provides an SVG with borders.
Definition: framesvg.h:77
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::Theme::ButtonTextColor
@ ButtonTextColor
Definition: theme.h:67
QGraphicsProxyWidget
QGraphicsView
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
combobox.h
framesvg.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
Plasma::AcceptingInputStatus
@ AcceptingInputStatus
The Item is accepting input.
Definition: plasma.h:261
Plasma::UnknownStatus
@ UnknownStatus
The status is unknown.
Definition: plasma.h:257
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal