Plasma
lineedit.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2008 Aaron Seigo <aseigo@kde.org> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "lineedit.h" 00021 00022 #include <QGraphicsSceneResizeEvent> 00023 #include <QIcon> 00024 #include <QPainter> 00025 00026 #include <klineedit.h> 00027 #include <kmimetype.h> 00028 00029 #include "applet.h" 00030 #include "framesvg.h" 00031 #include "private/style_p.h" 00032 #include "private/focusindicator_p.h" 00033 #include "private/themedwidgetinterface_p.h" 00034 #include "theme.h" 00035 00036 namespace Plasma 00037 { 00038 00039 class LineEditPrivate : public ThemedWidgetInterface<LineEdit> 00040 { 00041 public: 00042 LineEditPrivate(LineEdit *lineEdit) 00043 : ThemedWidgetInterface<LineEdit>(lineEdit) 00044 { 00045 buttonColorForText = true; 00046 } 00047 00048 ~LineEditPrivate() 00049 { 00050 } 00051 00052 LineEdit *q; 00053 Plasma::Style::Ptr style; 00054 Plasma::FrameSvg *background; 00055 }; 00056 00057 LineEdit::LineEdit(QGraphicsWidget *parent) 00058 : QGraphicsProxyWidget(parent), 00059 d(new LineEditPrivate(this)) 00060 { 00061 d->style = Plasma::Style::sharedStyle(); 00062 d->background = new Plasma::FrameSvg(this); 00063 d->background->setImagePath("widgets/lineedit"); 00064 d->background->setCacheAllRenderedFrames(true); 00065 00066 new FocusIndicator(this, d->background); 00067 setNativeWidget(new KLineEdit); 00068 } 00069 00070 LineEdit::~LineEdit() 00071 { 00072 delete d; 00073 Plasma::Style::doneWithSharedStyle(); 00074 } 00075 00076 void LineEdit::setText(const QString &text) 00077 { 00078 static_cast<KLineEdit*>(widget())->setText(text); 00079 } 00080 00081 QString LineEdit::text() const 00082 { 00083 return static_cast<KLineEdit*>(widget())->text(); 00084 } 00085 00086 void LineEdit::setClearButtonShown(bool show) 00087 { 00088 nativeWidget()->setClearButtonShown(show); 00089 } 00090 00091 bool LineEdit::isClearButtonShown() const 00092 { 00093 return nativeWidget()->isClearButtonShown(); 00094 } 00095 00096 void LineEdit::setClickMessage(const QString &message) 00097 { 00098 nativeWidget()->setClickMessage(message); 00099 } 00100 00101 QString LineEdit::clickMessage() const 00102 { 00103 return nativeWidget()->clickMessage(); 00104 } 00105 00106 void LineEdit::setStyleSheet(const QString &stylesheet) 00107 { 00108 widget()->setStyleSheet(stylesheet); 00109 } 00110 00111 QString LineEdit::styleSheet() 00112 { 00113 return widget()->styleSheet(); 00114 } 00115 00116 void LineEdit::setNativeWidget(KLineEdit *nativeWidget) 00117 { 00118 if (widget()) { 00119 widget()->deleteLater(); 00120 } 00121 00122 connect(nativeWidget, SIGNAL(editingFinished()), this, SIGNAL(editingFinished())); 00123 connect(nativeWidget, SIGNAL(returnPressed()), this, SIGNAL(returnPressed())); 00124 connect(nativeWidget, SIGNAL(textEdited(const QString&)), this, SIGNAL(textEdited(const QString&))); 00125 connect(nativeWidget, SIGNAL(textChanged(const QString&)), this, SIGNAL(textChanged(const QString&))); 00126 00127 00128 nativeWidget->setWindowFlags(nativeWidget->windowFlags()|Qt::BypassGraphicsProxyWidget); 00129 setWidget(nativeWidget); 00130 nativeWidget->setWindowIcon(QIcon()); 00131 00132 nativeWidget->setAttribute(Qt::WA_NoSystemBackground); 00133 nativeWidget->setStyle(d->style.data()); 00134 d->initTheming(); 00135 } 00136 00137 KLineEdit *LineEdit::nativeWidget() const 00138 { 00139 return static_cast<KLineEdit*>(widget()); 00140 } 00141 00142 void LineEdit::hoverEnterEvent(QGraphicsSceneHoverEvent *event) 00143 { 00144 Q_UNUSED(event) 00145 update(); 00146 } 00147 00148 void LineEdit::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) 00149 { 00150 Q_UNUSED(event) 00151 update(); 00152 } 00153 00154 void LineEdit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 00155 { 00156 Q_UNUSED(option) 00157 Q_UNUSED(widget) 00158 00159 nativeWidget()->render(painter, QPoint(0, 0), QRegion(), QWidget::DrawChildren|QWidget::IgnoreMask); 00160 } 00161 00162 void LineEdit::changeEvent(QEvent *event) 00163 { 00164 d->changeEvent(event); 00165 QGraphicsProxyWidget::changeEvent(event); 00166 } 00167 00168 void LineEdit::mousePressEvent(QGraphicsSceneMouseEvent *event) 00169 { 00170 QGraphicsWidget *widget = parentWidget(); 00171 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget); 00172 00173 while (!applet && widget) { 00174 widget = widget->parentWidget(); 00175 applet = qobject_cast<Plasma::Applet *>(widget); 00176 } 00177 00178 if (applet) { 00179 applet->setStatus(Plasma::AcceptingInputStatus); 00180 } 00181 QGraphicsProxyWidget::mousePressEvent(event); 00182 } 00183 00184 void LineEdit::focusInEvent(QFocusEvent *event) 00185 { 00186 QGraphicsProxyWidget::focusInEvent(event); 00187 if (!nativeWidget()->hasFocus()) { 00188 // as of Qt 4.7, apparently we have a bug here in QGraphicsProxyWidget 00189 nativeWidget()->setFocus(event->reason()); 00190 } 00191 } 00192 00193 void LineEdit::focusOutEvent(QFocusEvent *event) 00194 { 00195 QGraphicsWidget *widget = parentWidget(); 00196 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget); 00197 00198 while (!applet && widget) { 00199 widget = widget->parentWidget(); 00200 applet = qobject_cast<Plasma::Applet *>(widget); 00201 } 00202 00203 if (applet) { 00204 applet->setStatus(Plasma::UnknownStatus); 00205 } 00206 QGraphicsProxyWidget::focusOutEvent(event); 00207 } 00208 00209 } // namespace Plasma 00210 00211 #include <lineedit.moc> 00212
KDE 4.6 API Reference