~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to libs/plasma/widgets/lineedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2007 by Alexander Wiedenbruch <mail@wiedenbruch.de>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "lineedit.h"
 
20
 
 
21
#include <QStyleOptionFrameV2>
 
22
#include <QTextDocument>
 
23
#include <QKeyEvent>
 
24
#include <QPainter>
 
25
 
 
26
#include <KDebug>
 
27
 
 
28
namespace Plasma
 
29
{
 
30
 
 
31
class LineEdit::Private
 
32
{
 
33
    public:
 
34
        Private()
 
35
             : styled(true), multiline(false) {}
 
36
 
 
37
        QString defaultText;
 
38
        QString oldText;
 
39
        QString defaultTextPlain;
 
40
 
 
41
        bool styled;
 
42
        bool multiline;
 
43
 
 
44
        bool shouldPaint(QPainter *painter, const QTransform &transform); 
 
45
};
 
46
 
 
47
bool LineEdit::Private::shouldPaint(QPainter *painter, const QTransform &transform)
 
48
{
 
49
    qreal zoomLevel = painter->transform().m11() / transform.m11();
 
50
    //return zoomLevel == scalingFactor(Plasma::DesktopZoom);
 
51
    return true;
 
52
}
 
53
 
 
54
LineEdit::LineEdit(QGraphicsItem *parent, QGraphicsScene *scene)
 
55
    : QGraphicsTextItem(parent, scene),
 
56
      d(new Private())
 
57
{
 
58
    setTextInteractionFlags(Qt::TextEditorInteraction);
 
59
    setCursor(Qt::IBeamCursor);
 
60
    setFlag(QGraphicsItem::ItemIsSelectable);
 
61
}
 
62
 
 
63
LineEdit::~LineEdit()
 
64
{
 
65
    delete d;
 
66
}
 
67
 
 
68
void LineEdit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 
69
{
 
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);
 
73
    }
 
74
    return;
 
75
}
 
76
 
 
77
void LineEdit::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 
78
{
 
79
    QStyleOptionFrameV2 panel;
 
80
    panel.initFrom(widget);
 
81
    panel.state = option->state;
 
82
    panel.rect = boundingRect().toRect();
 
83
 
 
84
    if(d->styled) {
 
85
        widget->style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, painter, widget);
 
86
        widget->style()->drawPrimitive(QStyle::PE_FrameLineEdit, &panel, painter, widget);
 
87
    }
 
88
 
 
89
    // QGraphicsTextItem paints a black frame when it has focus
 
90
    // and is selected. We want to use our own frame, so we
 
91
    // clear these flags.
 
92
    QStyleOptionGraphicsItem *style = const_cast<QStyleOptionGraphicsItem*>(option);
 
93
    style->state &= ~(QStyle::State_Selected | QStyle::State_HasFocus);
 
94
 
 
95
    QGraphicsTextItem::paint(painter, style, widget);
 
96
}
 
97
 
 
98
void LineEdit::updated(const QString&, const DataEngine::Data& data)
 
99
{
 
100
    DataEngine::DataIterator it(data);
 
101
 
 
102
    QString text;
 
103
    while (it.hasNext()) {
 
104
        it.next();
 
105
        if (it.value().canConvert(QVariant::String)) {
 
106
            text.append(QString("<p><b>%1</b>: %2</p>").arg(it.key(), it.value().toString()));
 
107
        }
 
108
    }
 
109
    setHtml(text);
 
110
}
 
111
 
 
112
void LineEdit::setMultiLine(bool multi)
 
113
{
 
114
   d->multiline = multi;
 
115
}
 
116
 
 
117
bool LineEdit::multiLine() const
 
118
{
 
119
    return d->multiline;
 
120
}
 
121
 
 
122
 
 
123
void LineEdit::setStyled(bool style)
 
124
{
 
125
    d->styled = style;
 
126
}
 
127
 
 
128
bool LineEdit::styled() const
 
129
{
 
130
    return d->styled;
 
131
}
 
132
 
 
133
Qt::Orientations LineEdit::expandingDirections() const
 
134
{
 
135
    return Qt::Vertical;
 
136
}
 
137
 
 
138
QSizeF LineEdit::minimumSize() const
 
139
{
 
140
    return boundingRect().size();
 
141
}
 
142
 
 
143
QSizeF LineEdit::maximumSize() const
 
144
{
 
145
    return minimumSize();
 
146
}
 
147
 
 
148
bool LineEdit::hasHeightForWidth() const
 
149
{
 
150
    return true;
 
151
}
 
152
 
 
153
qreal LineEdit::heightForWidth(qreal w) const
 
154
{
 
155
    QTextDocument* doc = document();
 
156
    doc->setTextWidth(w);
 
157
    qreal height = doc->size().height();
 
158
    kDebug() << "LineEdit::heightForWidth(" << w << ") is " << height;
 
159
    return height;
 
160
}
 
161
 
 
162
bool LineEdit::hasWidthForHeight() const
 
163
{
 
164
    return false;
 
165
}
 
166
 
 
167
qreal LineEdit::widthForHeight(qreal h) const
 
168
{
 
169
    return 0;
 
170
}
 
171
 
 
172
QRectF LineEdit::geometry() const
 
173
{
 
174
    return QRectF(pos(),boundingRect().size());
 
175
}
 
176
 
 
177
void LineEdit::setGeometry(const QRectF& geometry)
 
178
{
 
179
    prepareGeometryChange();
 
180
    setTextWidth(geometry.width());
 
181
    setPos(geometry.topLeft());
 
182
    update();
 
183
}
 
184
 
 
185
QSizeF LineEdit::sizeHint() const
 
186
{
 
187
    kDebug() << "LineEdit::sizeeHint() " << document()->size();
 
188
    return document()->size();
 
189
}
 
190
 
 
191
void LineEdit::setDefaultText(const QString &text)
 
192
{
 
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();
 
197
}
 
198
 
 
199
const QString LineEdit::toHtml()
 
200
{
 
201
    if (QGraphicsTextItem::toHtml() == d->defaultText) {
 
202
        return QString();
 
203
    } else {
 
204
        return QGraphicsTextItem::toHtml();
 
205
    }
 
206
}
 
207
 
 
208
const QString LineEdit::toPlainText()
 
209
{
 
210
    if (QGraphicsTextItem::toPlainText() == d->defaultText) {
 
211
        return QString();
 
212
    } else {
 
213
        return QGraphicsTextItem::toPlainText();
 
214
    }
 
215
}
 
216
 
 
217
void LineEdit::keyPressEvent(QKeyEvent *event)
 
218
{
 
219
    if ( !d->multiline && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
 
220
        emit editingFinished();
 
221
        event->accept();
 
222
    } else {
 
223
        QGraphicsTextItem::keyPressEvent(event); //let QT handle other keypresses
 
224
    }
 
225
 
 
226
    if (QGraphicsTextItem::toHtml() != d->oldText) {
 
227
        d->oldText = QGraphicsTextItem::toHtml();
 
228
        emit textChanged(QGraphicsTextItem::toHtml());
 
229
    }
 
230
//     if (QGraphicsTextItem::toPlainText().simplified().isEmtpy())
 
231
//         QGraphicsTextItem::setHtml(d->defaultText);
 
232
}
 
233
 
 
234
void LineEdit::focusInEvent(QFocusEvent *event)
 
235
{
 
236
    if (QGraphicsTextItem::toPlainText() == d->defaultTextPlain) {
 
237
        QGraphicsTextItem::setHtml(QString());
 
238
    }
 
239
    QGraphicsTextItem::focusInEvent(event);
 
240
}
 
241
 
 
242
void LineEdit::focusOutEvent(QFocusEvent *event)
 
243
{
 
244
    if (QGraphicsTextItem::toPlainText().isEmpty()) {
 
245
        QGraphicsTextItem::setHtml(d->defaultText);
 
246
    }
 
247
    QGraphicsTextItem::focusOutEvent(event);
 
248
}
 
249
 
 
250
} // namespace Plasma
 
251
 
 
252
#include "lineedit.moc"
 
253