2
* Copyright 2007 by Alexander Wiedenbruch <mail@wiedenbruch.de>
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 version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <QStyleOptionFrameV2>
22
#include <QTextDocument>
31
class LineEdit::Private
35
: styled(true), multiline(false) {}
39
QString defaultTextPlain;
44
bool shouldPaint(QPainter *painter, const QTransform &transform);
47
bool LineEdit::Private::shouldPaint(QPainter *painter, const QTransform &transform)
49
qreal zoomLevel = painter->transform().m11() / transform.m11();
50
//return zoomLevel == scalingFactor(Plasma::DesktopZoom);
54
LineEdit::LineEdit(QGraphicsItem *parent, QGraphicsScene *scene)
55
: QGraphicsTextItem(parent, scene),
58
setTextInteractionFlags(Qt::TextEditorInteraction);
59
setCursor(Qt::IBeamCursor);
60
setFlag(QGraphicsItem::ItemIsSelectable);
68
void LineEdit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70
//TODO: Deal with paint and shouldPaint, since they are just copies of the ones in Widget
71
if (d->shouldPaint(painter, transform())) {
72
paintWidget(painter, option, widget);
77
void LineEdit::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
79
QStyleOptionFrameV2 panel;
80
panel.initFrom(widget);
81
panel.state = option->state;
82
panel.rect = boundingRect().toRect();
85
widget->style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, painter, widget);
86
widget->style()->drawPrimitive(QStyle::PE_FrameLineEdit, &panel, painter, widget);
89
// QGraphicsTextItem paints a black frame when it has focus
90
// and is selected. We want to use our own frame, so we
92
QStyleOptionGraphicsItem *style = const_cast<QStyleOptionGraphicsItem*>(option);
93
style->state &= ~(QStyle::State_Selected | QStyle::State_HasFocus);
95
QGraphicsTextItem::paint(painter, style, widget);
98
void LineEdit::updated(const QString&, const DataEngine::Data& data)
100
DataEngine::DataIterator it(data);
103
while (it.hasNext()) {
105
if (it.value().canConvert(QVariant::String)) {
106
text.append(QString("<p><b>%1</b>: %2</p>").arg(it.key(), it.value().toString()));
112
void LineEdit::setMultiLine(bool multi)
114
d->multiline = multi;
117
bool LineEdit::multiLine() const
123
void LineEdit::setStyled(bool style)
128
bool LineEdit::styled() const
133
Qt::Orientations LineEdit::expandingDirections() const
138
QSizeF LineEdit::minimumSize() const
140
return boundingRect().size();
143
QSizeF LineEdit::maximumSize() const
145
return minimumSize();
148
bool LineEdit::hasHeightForWidth() const
153
qreal LineEdit::heightForWidth(qreal w) const
155
QTextDocument* doc = document();
156
doc->setTextWidth(w);
157
qreal height = doc->size().height();
158
kDebug() << "LineEdit::heightForWidth(" << w << ") is " << height;
162
bool LineEdit::hasWidthForHeight() const
167
qreal LineEdit::widthForHeight(qreal h) const
172
QRectF LineEdit::geometry() const
174
return QRectF(pos(),boundingRect().size());
177
void LineEdit::setGeometry(const QRectF& geometry)
179
prepareGeometryChange();
180
setTextWidth(geometry.width());
181
setPos(geometry.topLeft());
185
QSizeF LineEdit::sizeHint() const
187
kDebug() << "LineEdit::sizeeHint() " << document()->size();
188
return document()->size();
191
void LineEdit::setDefaultText(const QString &text)
193
d->defaultText = text.simplified();
194
d->defaultText = QString("<font color=\"gray\">") + d->defaultText + QString("</font>");
195
QGraphicsTextItem::setHtml(d->defaultText);
196
d->defaultTextPlain = QGraphicsTextItem::toPlainText();
199
const QString LineEdit::toHtml()
201
if (QGraphicsTextItem::toHtml() == d->defaultText) {
204
return QGraphicsTextItem::toHtml();
208
const QString LineEdit::toPlainText()
210
if (QGraphicsTextItem::toPlainText() == d->defaultText) {
213
return QGraphicsTextItem::toPlainText();
217
void LineEdit::keyPressEvent(QKeyEvent *event)
219
if ( !d->multiline && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
220
emit editingFinished();
223
QGraphicsTextItem::keyPressEvent(event); //let QT handle other keypresses
226
if (QGraphicsTextItem::toHtml() != d->oldText) {
227
d->oldText = QGraphicsTextItem::toHtml();
228
emit textChanged(QGraphicsTextItem::toHtml());
230
// if (QGraphicsTextItem::toPlainText().simplified().isEmtpy())
231
// QGraphicsTextItem::setHtml(d->defaultText);
234
void LineEdit::focusInEvent(QFocusEvent *event)
236
if (QGraphicsTextItem::toPlainText() == d->defaultTextPlain) {
237
QGraphicsTextItem::setHtml(QString());
239
QGraphicsTextItem::focusInEvent(event);
242
void LineEdit::focusOutEvent(QFocusEvent *event)
244
if (QGraphicsTextItem::toPlainText().isEmpty()) {
245
QGraphicsTextItem::setHtml(d->defaultText);
247
QGraphicsTextItem::focusOutEvent(event);
250
} // namespace Plasma
252
#include "lineedit.moc"