23#include <QGraphicsView>
26#include <kiconeffect.h>
27#include <kiconloader.h>
32#include "private/style_p.h"
33#include "private/focusindicator_p.h"
34#include "private/themedwidgetinterface_p.h"
40class ComboBoxPrivate :
public ThemedWidgetInterface<ComboBox>
43 ComboBoxPrivate(ComboBox *comboBox)
44 : ThemedWidgetInterface<ComboBox>(comboBox),
54 void syncActiveRect();
58 FrameSvg *lineEditBackground;
59 FocusIndicator *focusIndicator;
67void ComboBoxPrivate::syncActiveRect()
69 background->setElementPrefix(
"normal");
71 qreal left, top, right, bottom;
72 background->getMargins(left, top, right, bottom);
74 background->setElementPrefix(
"active");
75 qreal activeLeft, activeTop, activeRight, activeBottom;
76 background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
78 activeRect = QRectF(QPointF(0, 0), q->size());
79 activeRect.adjust(left - activeLeft, top - activeTop,
80 -(right - activeRight), -(bottom - activeBottom));
82 background->setElementPrefix(
"normal");
85void ComboBoxPrivate::syncBorders()
88 qreal left, top, right, bottom;
90 background->setElementPrefix(
"normal");
91 background->getMargins(left, top, right, bottom);
92 q->setContentsMargins(left, top, right, bottom);
98 q->setFont(q->font());
100 q->setFont(Theme::defaultTheme()->font(Theme::DefaultFont));
104 if (q->nativeWidget()->isEditable()) {
105 focusIndicator->setFrameSvg(lineEditBackground);
107 focusIndicator->setFrameSvg(background);
109 focusIndicator->setFlag(QGraphicsItem::ItemStacksBehindParent, !q->nativeWidget()->isEditable() || !lineEditBackground->hasElement(
"hint-focus-over-base"));
115 d(new ComboBoxPrivate(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);
126 setAcceptHoverEvents(
true);
128 d->style = Style::sharedStyle();
130 d->focusIndicator =
new FocusIndicator(
this, d->background);
132 connect(d->background, SIGNAL(repaintNeeded()), SLOT(syncBorders()));
139 Style::doneWithSharedStyle();
142QString ComboBox::text()
const
144 return static_cast<KComboBox*
>(widget())->currentText();
147void ComboBox::setStyleSheet(
const QString &stylesheet)
149 widget()->setStyleSheet(stylesheet);
152QString ComboBox::styleSheet()
154 return widget()->styleSheet();
157void ComboBox::setNativeWidget(KComboBox *nativeWidget)
160 widget()->deleteLater();
178KComboBox *ComboBox::nativeWidget()
const
180 return static_cast<KComboBox*
>(widget());
183void ComboBox::addItem(
const QString &text)
188void ComboBox::clear()
190 static_cast<KComboBox*
>(widget())->
clear();
193void ComboBox::resizeEvent(QGraphicsSceneResizeEvent *event)
199 d->background->setElementPrefix(
"focus");
200 d->background->resizeFrame(size());
202 d->background->setElementPrefix(
"active");
203 d->background->resizeFrame(d->activeRect.size());
205 d->background->setElementPrefix(
"normal");
206 d->background->resizeFrame(size());
209 QGraphicsProxyWidget::resizeEvent(event);
212void ComboBox::paint(QPainter *painter,
219 QGraphicsProxyWidget::paint(painter, option, widget);
224 QGraphicsProxyWidget::paint(painter, option, widget);
228 QPixmap bufferPixmap;
232 d->background->setElementPrefix(
"normal");
234 d->background->paintFrame(painter);
237 bufferPixmap = QPixmap(rect().size().toSize());
238 bufferPixmap.fill(Qt::transparent);
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));
245 painter->drawPixmap(0, 0, bufferPixmap);
250 QStyleOptionComboBox comboOpt;
254 comboOpt.palette.setColor(
260 comboOpt.editable =
false;
263 QStyle::CE_ComboBoxLabel, &comboOpt, painter,
nativeWidget());
264 comboOpt.rect =
nativeWidget()->style()->subControlRect(
265 QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxArrow,
nativeWidget());
267 QStyle::PE_IndicatorArrowDown, &comboOpt, painter,
nativeWidget());
270void ComboBox::focusInEvent(QFocusEvent *event)
272 QGraphicsProxyWidget::focusInEvent(event);
275void ComboBox::focusOutEvent(QFocusEvent *event)
280 while (!applet && widget) {
281 widget = widget->parentWidget();
282 applet = qobject_cast<Plasma::Applet *>(widget);
290 QEvent closeEvent(QEvent::CloseSoftwareInputPanel);
292 if (
QGraphicsView *view = qobject_cast<QGraphicsView*>(qApp->focusWidget())) {
293 if (view->scene() && view->scene() == scene()) {
294 QApplication::sendEvent(view, &closeEvent);
300 QGraphicsProxyWidget::focusOutEvent(event);
303void ComboBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
305 d->underMouse =
true;
306 QGraphicsProxyWidget::hoverEnterEvent(event);
309void ComboBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
311 d->underMouse =
false;
312 QGraphicsProxyWidget::hoverLeaveEvent(event);
315void ComboBox::changeEvent(QEvent *event)
317 d->changeEvent(event);
318 QGraphicsProxyWidget::changeEvent(event);
321void ComboBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
326 while (!applet && widget) {
327 widget = widget->parentWidget();
328 applet = qobject_cast<Plasma::Applet *>(widget);
334 QGraphicsProxyWidget::mousePressEvent(event);
337int ComboBox::count()
const
342int ComboBox::currentIndex()
const
347void ComboBox::setCurrentIndex(
int index)
354#include <combobox.moc>
void setStatus(const ItemStatus stat)
sets the status for this applet
void activated(const QString &text)
This signal is sent when the user chooses an item in the combobox.
void setNativeWidget(KComboBox *nativeWidget)
Sets the combo box wrapped by this ComboBox (widget must inherit KComboBox), ownership is transferred...
Q_INVOKABLE void addItem(const QString &text)
Adds an item to the combo box with the given text.
void currentIndexChanged(int index)
This signal is sent whenever the currentIndex in the combobox changes either through user interaction...
QGraphicsWidget * parentWidget
void textChanged(const QString &text)
This signal is sent whenever the currentIndex in the combobox changes either through user interaction...
Provides an SVG with borders.
static Theme * defaultTheme()
Singleton pattern accessor.
Namespace for everything in libplasma.
@ AcceptingInputStatus
The Item is accepting input.
@ UnknownStatus
The status is unknown.