From 034e5a4a62f69ff3f92af2186ba5316f2adac0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dutkiewicz?= Date: Wed, 23 Sep 2009 18:42:19 +0000 Subject: [PATCH] Support for using custom native widgets with ComboBox, LineEdit and TextEdit svn path=/trunk/KDE/kdelibs/; revision=1027285 --- widgets/combobox.cpp | 33 +++++++++++++++++++++++---------- widgets/combobox.h | 12 ++++++++++-- widgets/lineedit.cpp | 31 +++++++++++++++++++++---------- widgets/lineedit.h | 10 +++++++++- widgets/textedit.cpp | 29 ++++++++++++++++++++--------- widgets/textedit.h | 10 +++++++++- 6 files changed, 92 insertions(+), 33 deletions(-) diff --git a/widgets/combobox.cpp b/widgets/combobox.cpp index 08166727d..0d4a51960 100644 --- a/widgets/combobox.cpp +++ b/widgets/combobox.cpp @@ -132,13 +132,6 @@ ComboBox::ComboBox(QGraphicsWidget *parent) : QGraphicsProxyWidget(parent), d(new ComboBoxPrivate(this)) { - KComboBox *native = new KComboBox; - connect(native, SIGNAL(activated(const QString &)), this, SIGNAL(activated(const QString &))); - connect(native, SIGNAL(currentIndexChanged(const QString &)), - this, SIGNAL(textChanged(const QString &))); - setWidget(native); - native->setAttribute(Qt::WA_NoSystemBackground); - d->background = new FrameSvg(this); d->background->setImagePath("widgets/button"); d->background->setCacheAllRenderedFrames(true); @@ -147,12 +140,14 @@ ComboBox::ComboBox(QGraphicsWidget *parent) d->lineEditBackground->setImagePath("widgets/lineedit"); d->lineEditBackground->setCacheAllRenderedFrames(true); - d->syncBorders(); setAcceptHoverEvents(true); + + d->style = Style::sharedStyle(); + + setNativeWidget(new KComboBox); + connect(Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders())); connect(Plasma::Animator::self(), SIGNAL(customAnimationFinished(int)), this, SLOT(animationFinished(int))); - d->style = Style::sharedStyle(); - native->setStyle(d->style.data()); } ComboBox::~ComboBox() @@ -176,6 +171,24 @@ QString ComboBox::styleSheet() return widget()->styleSheet(); } +void ComboBox::setNativeWidget(KComboBox *nativeWidget) +{ + if (widget()) { + widget()->deleteLater(); + } + + connect(nativeWidget, SIGNAL(activated(const QString &)), this, SIGNAL(activated(const QString &))); + connect(nativeWidget, SIGNAL(currentIndexChanged(const QString &)), + this, SIGNAL(textChanged(const QString &))); + + setWidget(nativeWidget); + + nativeWidget->setAttribute(Qt::WA_NoSystemBackground); + nativeWidget->setStyle(d->style.data()); + + d->syncBorders(); +} + KComboBox *ComboBox::nativeWidget() const { return static_cast(widget()); diff --git a/widgets/combobox.h b/widgets/combobox.h index f3d467944..b7d3be8d3 100644 --- a/widgets/combobox.h +++ b/widgets/combobox.h @@ -43,7 +43,7 @@ class PLASMA_EXPORT ComboBox : public QGraphicsProxyWidget Q_PROPERTY(QGraphicsWidget *parentWidget READ parentWidget) Q_PROPERTY(QString text READ text NOTIFY textChanged) Q_PROPERTY(QString styleSheet READ styleSheet WRITE setStyleSheet) - Q_PROPERTY(KComboBox *nativeWidget READ nativeWidget) + Q_PROPERTY(KComboBox *nativeWidget READ nativeWidget WRITE setNativeWidget) public: explicit ComboBox(QGraphicsWidget *parent = 0); @@ -66,13 +66,21 @@ public: */ QString styleSheet(); + /** + * Sets the combo box wrapped by this ComboBox (widget must inherit KComboBox), ownership is transferred to the ComboBox + * + * @arg combo box that will be wrapped by this ComboBox + * @since KDE4.4 + */ + void setNativeWidget(KComboBox *nativeWidget); + /** * @return the native widget wrapped by this ComboBox */ KComboBox *nativeWidget() const; /** - * Adds an item to the combobox with the given text. The + * Adds an item to the combo box with the given text. The * item is appended to the list of existing items. */ void addItem(const QString &text); diff --git a/widgets/lineedit.cpp b/widgets/lineedit.cpp index 91803a6f1..de41d828d 100644 --- a/widgets/lineedit.cpp +++ b/widgets/lineedit.cpp @@ -72,22 +72,13 @@ LineEdit::LineEdit(QGraphicsWidget *parent) : QGraphicsProxyWidget(parent), d(new LineEditPrivate(this)) { - KLineEdit *native = new KLineEdit; d->style = Plasma::Style::sharedStyle(); d->background = new Plasma::FrameSvg(this); d->background->setImagePath("widgets/lineedit"); d->background->setCacheAllRenderedFrames(true); - native->setStyle(d->style.data()); - native->setAttribute(Qt::WA_NoSystemBackground); - setWidget(native); + setNativeWidget(new KLineEdit); - connect(native, SIGNAL(editingFinished()), this, SIGNAL(editingFinished())); - connect(native, SIGNAL(returnPressed()), this, SIGNAL(returnPressed())); - connect(native, SIGNAL(textEdited(const QString&)), this, SIGNAL(textEdited(const QString&))); - connect(native, SIGNAL(textChanged(const QString&)), this, SIGNAL(textChanged(const QString&))); - - d->setPalette(); connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(setPalette())); } @@ -127,6 +118,26 @@ QString LineEdit::styleSheet() return widget()->styleSheet(); } +void LineEdit::setNativeWidget(KLineEdit *nativeWidget) +{ + if (widget()) { + widget()->deleteLater(); + } + + connect(nativeWidget, SIGNAL(editingFinished()), this, SIGNAL(editingFinished())); + connect(nativeWidget, SIGNAL(returnPressed()), this, SIGNAL(returnPressed())); + connect(nativeWidget, SIGNAL(textEdited(const QString&)), this, SIGNAL(textEdited(const QString&))); + connect(nativeWidget, SIGNAL(textChanged(const QString&)), this, SIGNAL(textChanged(const QString&))); + + + setWidget(nativeWidget); + + nativeWidget->setAttribute(Qt::WA_NoSystemBackground); + nativeWidget->setStyle(d->style.data()); + + d->setPalette(); +} + KLineEdit *LineEdit::nativeWidget() const { return static_cast(widget()); diff --git a/widgets/lineedit.h b/widgets/lineedit.h index 669f9baf6..e6e1c11a1 100644 --- a/widgets/lineedit.h +++ b/widgets/lineedit.h @@ -44,7 +44,7 @@ class PLASMA_EXPORT LineEdit : public QGraphicsProxyWidget Q_PROPERTY(QString text READ text WRITE setText NOTIFY textEdited) Q_PROPERTY(bool isClearButtonShown READ isClearButtonShown WRITE setClearButtonShown) Q_PROPERTY(QString styleSheet READ styleSheet WRITE setStyleSheet) - Q_PROPERTY(KLineEdit *nativeWidget READ nativeWidget) + Q_PROPERTY(KLineEdit *nativeWidget READ nativeWidget WRITE setNativeWidget) public: explicit LineEdit(QGraphicsWidget *parent = 0); @@ -86,6 +86,14 @@ public: */ QString styleSheet(); + /** + * Sets the line edit wrapped by this LineEdit (widget must inherit KLineEdit), ownership is transferred to the LineEdit + * + * @arg text edit that will be wrapped by this LineEdit + * @since KDE4.4 + */ + void setNativeWidget(KLineEdit *nativeWidget); + /** * @return the native widget wrapped by this LineEdit */ diff --git a/widgets/textedit.cpp b/widgets/textedit.cpp index e94a326ab..da8eb5dab 100644 --- a/widgets/textedit.cpp +++ b/widgets/textedit.cpp @@ -73,16 +73,9 @@ TextEdit::TextEdit(QGraphicsWidget *parent) : QGraphicsProxyWidget(parent), d(new TextEditPrivate(this)) { - KTextEdit *native = new KTextEdit; - connect(native, SIGNAL(textChanged()), this, SIGNAL(textChanged())); - setWidget(native); - native->setAttribute(Qt::WA_NoSystemBackground); - native->setFrameShape( QFrame::NoFrame ); - native->setTextBackgroundColor( Qt::transparent ); - native->viewport()->setAutoFillBackground( false ); d->style = Plasma::Style::sharedStyle(); - native->verticalScrollBar()->setStyle(d->style.data()); - native->horizontalScrollBar()->setStyle(d->style.data()); + + setNativeWidget(new KTextEdit); connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(setPalette())); } @@ -114,6 +107,24 @@ QString TextEdit::styleSheet() return widget()->styleSheet(); } +void TextEdit::setNativeWidget(KTextEdit *nativeWidget) +{ + if (widget()) { + widget()->deleteLater(); + } + + connect(nativeWidget, SIGNAL(textChanged()), this, SIGNAL(textChanged())); + + setWidget(nativeWidget); + + nativeWidget->setAttribute(Qt::WA_NoSystemBackground); + nativeWidget->setFrameShape(QFrame::NoFrame); + nativeWidget->setTextBackgroundColor(Qt::transparent); + nativeWidget->viewport()->setAutoFillBackground(false); + nativeWidget->verticalScrollBar()->setStyle(d->style.data()); + nativeWidget->horizontalScrollBar()->setStyle(d->style.data()); +} + KTextEdit *TextEdit::nativeWidget() const { return static_cast(widget()); diff --git a/widgets/textedit.h b/widgets/textedit.h index 418ed0e59..dbffbe727 100644 --- a/widgets/textedit.h +++ b/widgets/textedit.h @@ -44,7 +44,7 @@ class PLASMA_EXPORT TextEdit : public QGraphicsProxyWidget Q_PROPERTY(QGraphicsWidget *parentWidget READ parentWidget) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet) - Q_PROPERTY(KTextEdit *nativeWidget READ nativeWidget) + Q_PROPERTY(KTextEdit *nativeWidget READ nativeWidget WRITE setNativeWidget) public: explicit TextEdit(QGraphicsWidget *parent = 0); @@ -74,6 +74,14 @@ public: */ QString styleSheet(); + /** + * Sets the text edit wrapped by this TextEdit (widget must inherit KTextEdit), ownership is transferred to the TextEdit + * + * @arg text edit that will be wrapped by this TextEdit + * @since KDE4.4 + */ + void setNativeWidget(KTextEdit *nativeWidget); + /** * @return the native widget wrapped by this TextEdit */