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 #include <QGraphicsView> 00026 00027 #include <klineedit.h> 00028 #include <kmimetype.h> 00029 00030 #include "applet.h" 00031 #include "framesvg.h" 00032 #include "private/style_p.h" 00033 #include "private/focusindicator_p.h" 00034 #include "private/themedwidgetinterface_p.h" 00035 #include "theme.h" 00036 00037 namespace Plasma 00038 { 00039 00040 class LineEditPrivate : public ThemedWidgetInterface<LineEdit> 00041 { 00042 public: 00043 LineEditPrivate(LineEdit *lineEdit) 00044 : ThemedWidgetInterface<LineEdit>(lineEdit) 00045 { 00046 buttonColorForText = true; 00047 } 00048 00049 ~LineEditPrivate() 00050 { 00051 } 00052 00053 LineEdit *q; 00054 Plasma::Style::Ptr style; 00055 Plasma::FrameSvg *background; 00056 }; 00057 00058 LineEdit::LineEdit(QGraphicsWidget *parent) 00059 : QGraphicsProxyWidget(parent), 00060 d(new LineEditPrivate(this)) 00061 { 00062 d->style = Plasma::Style::sharedStyle(); 00063 d->background = new Plasma::FrameSvg(this); 00064 d->background->setImagePath("widgets/lineedit"); 00065 d->background->setCacheAllRenderedFrames(true); 00066 00067 new FocusIndicator(this, d->background); 00068 setNativeWidget(new KLineEdit); 00069 } 00070 00071 LineEdit::~LineEdit() 00072 { 00073 delete d; 00074 Plasma::Style::doneWithSharedStyle(); 00075 } 00076 00077 void LineEdit::setText(const QString &text) 00078 { 00079 static_cast<KLineEdit*>(widget())->setText(text); 00080 } 00081 00082 QString LineEdit::text() const 00083 { 00084 return static_cast<KLineEdit*>(widget())->text(); 00085 } 00086 00087 void LineEdit::setClearButtonShown(bool show) 00088 { 00089 nativeWidget()->setClearButtonShown(show); 00090 } 00091 00092 bool LineEdit::isClearButtonShown() const 00093 { 00094 return nativeWidget()->isClearButtonShown(); 00095 } 00096 00097 void LineEdit::setClickMessage(const QString &message) 00098 { 00099 nativeWidget()->setClickMessage(message); 00100 } 00101 00102 QString LineEdit::clickMessage() const 00103 { 00104 return nativeWidget()->clickMessage(); 00105 } 00106 00107 void LineEdit::setStyleSheet(const QString &stylesheet) 00108 { 00109 widget()->setStyleSheet(stylesheet); 00110 } 00111 00112 QString LineEdit::styleSheet() 00113 { 00114 return widget()->styleSheet(); 00115 } 00116 00117 void LineEdit::setNativeWidget(KLineEdit *nativeWidget) 00118 { 00119 if (widget()) { 00120 widget()->deleteLater(); 00121 } 00122 00123 connect(nativeWidget, SIGNAL(editingFinished()), this, SIGNAL(editingFinished())); 00124 connect(nativeWidget, SIGNAL(returnPressed()), this, SIGNAL(returnPressed())); 00125 connect(nativeWidget, SIGNAL(textEdited(const QString&)), this, SIGNAL(textEdited(const QString&))); 00126 connect(nativeWidget, SIGNAL(textChanged(const QString&)), this, SIGNAL(textChanged(const QString&))); 00127 00128 00129 nativeWidget->setWindowFlags(nativeWidget->windowFlags()|Qt::BypassGraphicsProxyWidget); 00130 setWidget(nativeWidget); 00131 nativeWidget->setWindowIcon(QIcon()); 00132 00133 nativeWidget->setAttribute(Qt::WA_NoSystemBackground); 00134 nativeWidget->setStyle(d->style.data()); 00135 d->initTheming(); 00136 } 00137 00138 KLineEdit *LineEdit::nativeWidget() const 00139 { 00140 return static_cast<KLineEdit*>(widget()); 00141 } 00142 00143 void LineEdit::hoverEnterEvent(QGraphicsSceneHoverEvent *event) 00144 { 00145 Q_UNUSED(event) 00146 update(); 00147 } 00148 00149 void LineEdit::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) 00150 { 00151 Q_UNUSED(event) 00152 update(); 00153 } 00154 00155 void LineEdit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 00156 { 00157 Q_UNUSED(option) 00158 Q_UNUSED(widget) 00159 00160 nativeWidget()->render(painter, QPoint(0, 0), QRegion(), QWidget::DrawChildren|QWidget::IgnoreMask); 00161 } 00162 00163 void LineEdit::changeEvent(QEvent *event) 00164 { 00165 d->changeEvent(event); 00166 QGraphicsProxyWidget::changeEvent(event); 00167 } 00168 00169 void LineEdit::mousePressEvent(QGraphicsSceneMouseEvent *event) 00170 { 00171 QGraphicsWidget *widget = parentWidget(); 00172 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget); 00173 00174 while (!applet && widget) { 00175 widget = widget->parentWidget(); 00176 applet = qobject_cast<Plasma::Applet *>(widget); 00177 } 00178 00179 if (applet) { 00180 applet->setStatus(Plasma::AcceptingInputStatus); 00181 } 00182 QGraphicsProxyWidget::mousePressEvent(event); 00183 } 00184 00185 void LineEdit::focusInEvent(QFocusEvent *event) 00186 { 00187 QGraphicsProxyWidget::focusInEvent(event); 00188 if (!nativeWidget()->hasFocus()) { 00189 // as of Qt 4.7, apparently we have a bug here in QGraphicsProxyWidget 00190 nativeWidget()->setFocus(event->reason()); 00191 } 00192 00193 emit focusChanged(true); 00194 } 00195 00196 void LineEdit::focusOutEvent(QFocusEvent *event) 00197 { 00198 QGraphicsWidget *widget = parentWidget(); 00199 Plasma::Applet *applet = qobject_cast<Plasma::Applet *>(widget); 00200 00201 while (!applet && widget) { 00202 widget = widget->parentWidget(); 00203 applet = qobject_cast<Plasma::Applet *>(widget); 00204 } 00205 00206 if (applet) { 00207 applet->setStatus(Plasma::UnknownStatus); 00208 } 00209 00210 QEvent closeEvent(QEvent::CloseSoftwareInputPanel); 00211 if (qApp) { 00212 if (QGraphicsView *view = qobject_cast<QGraphicsView*>(qApp->focusWidget())) { 00213 if (view->scene() && view->scene() == scene()) { 00214 QApplication::sendEvent(view, &closeEvent); 00215 } 00216 } 00217 } 00218 00219 QGraphicsProxyWidget::focusOutEvent(event); 00220 00221 emit focusChanged(false); 00222 } 00223 00224 } // namespace Plasma 00225 00226 #include <lineedit.moc> 00227
KDE 4.7 API Reference