2007-03-04 21:35:37 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 by Alexander Wiedenbruch <mail@wiedenbruch.de>
|
2007-03-04 21:35:37 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
2007-03-04 21:35:37 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lineedit.h"
|
|
|
|
|
2008-02-22 20:40:46 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include <QStyleOptionFrameV2>
|
2007-05-28 07:52:56 +02:00
|
|
|
#include <QTextDocument>
|
2007-06-20 21:49:31 +02:00
|
|
|
#include <QKeyEvent>
|
2007-08-01 22:51:27 +02:00
|
|
|
#include <QPainter>
|
|
|
|
|
2007-06-21 17:42:05 +02:00
|
|
|
#include <KDebug>
|
2008-01-20 21:59:34 +01:00
|
|
|
#include <KColorScheme>
|
|
|
|
#include <KApplication>
|
2007-05-21 16:28:03 +02:00
|
|
|
|
2008-01-20 21:59:34 +01:00
|
|
|
#include <plasma/theme.h>
|
2007-11-20 00:45:56 +01:00
|
|
|
#include "plasma/layouts/layout.h"
|
|
|
|
|
2007-03-04 21:35:37 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-05-24 21:36:05 +02:00
|
|
|
class LineEdit::Private
|
|
|
|
{
|
2007-06-21 20:47:04 +02:00
|
|
|
public:
|
2007-07-17 01:56:57 +02:00
|
|
|
Private()
|
2008-03-29 23:22:44 +01:00
|
|
|
: showingDefaultText(true), styled(true), multiline(false) {}
|
2007-07-17 01:56:57 +02:00
|
|
|
|
2007-06-21 20:47:04 +02:00
|
|
|
QString defaultText;
|
|
|
|
QString oldText;
|
2007-07-17 01:56:57 +02:00
|
|
|
|
2008-03-29 23:22:44 +01:00
|
|
|
bool showingDefaultText;
|
2007-07-17 01:56:57 +02:00
|
|
|
bool styled;
|
2007-07-22 15:41:34 +02:00
|
|
|
bool multiline;
|
2007-07-17 01:56:57 +02:00
|
|
|
|
2007-05-24 21:36:05 +02:00
|
|
|
};
|
|
|
|
|
2007-11-04 06:36:10 +01:00
|
|
|
LineEdit::LineEdit(QGraphicsItem *parent)
|
|
|
|
: QGraphicsTextItem(parent),
|
2007-05-24 21:36:05 +02:00
|
|
|
d(new Private())
|
2007-03-04 21:35:37 +01:00
|
|
|
{
|
|
|
|
setTextInteractionFlags(Qt::TextEditorInteraction);
|
2007-07-10 15:16:33 +02:00
|
|
|
setCursor(Qt::IBeamCursor);
|
2007-07-22 22:46:23 +02:00
|
|
|
setFlag(QGraphicsItem::ItemIsSelectable);
|
2007-03-04 21:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LineEdit::~LineEdit()
|
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2007-03-04 21:35:37 +01:00
|
|
|
}
|
|
|
|
|
2008-02-11 21:50:13 +01:00
|
|
|
void LineEdit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-03-04 21:35:37 +01:00
|
|
|
{
|
2008-02-11 21:50:13 +01:00
|
|
|
if (d->styled && widget) {
|
|
|
|
QStyleOptionFrameV2 panel;
|
|
|
|
panel.initFrom(widget);
|
|
|
|
panel.state = option->state;
|
|
|
|
panel.rect = boundingRect().toRect();
|
2007-03-04 21:35:37 +01:00
|
|
|
|
2007-07-17 01:56:57 +02:00
|
|
|
widget->style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, painter, widget);
|
|
|
|
widget->style()->drawPrimitive(QStyle::PE_FrameLineEdit, &panel, painter, widget);
|
|
|
|
}
|
2007-03-04 21:35:37 +01:00
|
|
|
|
|
|
|
// QGraphicsTextItem paints a black frame when it has focus
|
|
|
|
// and is selected. We want to use our own frame, so we
|
|
|
|
// clear these flags.
|
|
|
|
QStyleOptionGraphicsItem *style = const_cast<QStyleOptionGraphicsItem*>(option);
|
|
|
|
style->state &= ~(QStyle::State_Selected | QStyle::State_HasFocus);
|
|
|
|
|
|
|
|
QGraphicsTextItem::paint(painter, style, widget);
|
|
|
|
}
|
|
|
|
|
2007-11-06 08:20:08 +01:00
|
|
|
void LineEdit::dataUpdated(const QString&, const DataEngine::Data& data)
|
2007-03-04 21:35:37 +01:00
|
|
|
{
|
2007-05-21 06:29:00 +02:00
|
|
|
DataEngine::DataIterator it(data);
|
|
|
|
|
2007-05-28 07:52:56 +02:00
|
|
|
QString text;
|
2007-05-21 06:29:00 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().canConvert(QVariant::String)) {
|
2007-05-28 07:52:56 +02:00
|
|
|
text.append(QString("<p><b>%1</b>: %2</p>").arg(it.key(), it.value().toString()));
|
2007-05-21 06:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-28 07:52:56 +02:00
|
|
|
setHtml(text);
|
|
|
|
}
|
|
|
|
|
2007-07-22 15:41:34 +02:00
|
|
|
void LineEdit::setMultiLine(bool multi)
|
|
|
|
{
|
|
|
|
d->multiline = multi;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LineEdit::multiLine() const
|
|
|
|
{
|
|
|
|
return d->multiline;
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:56:57 +02:00
|
|
|
|
|
|
|
void LineEdit::setStyled(bool style)
|
|
|
|
{
|
|
|
|
d->styled = style;
|
2008-01-20 21:59:34 +01:00
|
|
|
|
|
|
|
if (style) {
|
|
|
|
setDefaultTextColor(kapp->palette().color(QPalette::Text));
|
2008-02-11 21:50:13 +01:00
|
|
|
} else {
|
|
|
|
setDefaultTextColor(Plasma::Theme::self()->textColor());
|
2008-01-20 21:59:34 +01:00
|
|
|
}
|
2007-07-17 01:56:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LineEdit::styled() const
|
|
|
|
{
|
|
|
|
return d->styled;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Qt::Orientations LineEdit::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2007-05-28 07:52:56 +02:00
|
|
|
QSizeF LineEdit::minimumSize() const
|
|
|
|
{
|
2008-02-13 02:48:18 +01:00
|
|
|
QSizeF sh = document()->size();
|
|
|
|
sh.setWidth(textWidth());
|
|
|
|
return sh;
|
2007-05-28 07:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF LineEdit::maximumSize() const
|
|
|
|
{
|
2008-02-22 20:40:46 +01:00
|
|
|
//TODO: well, this is useless, isn't it? ;) when ported to WoC, remove it
|
|
|
|
return QSizeF(std::numeric_limits<qreal>::infinity(),
|
|
|
|
std::numeric_limits<qreal>::infinity());
|
2007-05-28 07:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LineEdit::hasHeightForWidth() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal LineEdit::heightForWidth(qreal w) const
|
|
|
|
{
|
|
|
|
QTextDocument* doc = document();
|
|
|
|
doc->setTextWidth(w);
|
|
|
|
qreal height = doc->size().height();
|
2008-02-22 20:40:46 +01:00
|
|
|
//kDebug() << "LineEdit::heightForWidth(" << w << ") is " << height;
|
2007-05-28 07:52:56 +02:00
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LineEdit::hasWidthForHeight() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal LineEdit::widthForHeight(qreal h) const
|
|
|
|
{
|
2007-11-22 08:19:43 +01:00
|
|
|
Q_UNUSED(h);
|
2007-05-28 07:52:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF LineEdit::geometry() const
|
|
|
|
{
|
2008-02-13 02:48:18 +01:00
|
|
|
return QRectF(pos(), boundingRect().size());
|
2007-05-28 07:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setGeometry(const QRectF& geometry)
|
|
|
|
{
|
2007-05-29 09:58:05 +02:00
|
|
|
prepareGeometryChange();
|
2008-02-13 02:48:18 +01:00
|
|
|
QRectF geom(geometry.topLeft(),
|
|
|
|
geometry.size().boundedTo(maximumSize()).expandedTo(minimumSize()));
|
|
|
|
setTextWidth(geom.width());
|
|
|
|
setPos(geom.topLeft());
|
2007-05-29 09:58:05 +02:00
|
|
|
update();
|
2007-05-28 07:52:56 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void LineEdit::updateGeometry()
|
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
if (managingLayout()) {
|
|
|
|
managingLayout()->invalidate();
|
|
|
|
} else {
|
|
|
|
setGeometry(QRectF(pos(), sizeHint()));
|
|
|
|
}
|
2007-11-20 00:45:56 +01:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QSizeF LineEdit::sizeHint() const
|
2007-05-28 07:52:56 +02:00
|
|
|
{
|
2008-02-13 02:48:18 +01:00
|
|
|
QSizeF sh = document()->size();
|
|
|
|
|
|
|
|
if (sh.width() < textWidth()) {
|
|
|
|
sh.setWidth(textWidth());
|
|
|
|
}
|
|
|
|
|
|
|
|
return sh;
|
2007-03-04 21:35:37 +01:00
|
|
|
}
|
|
|
|
|
2007-07-20 10:06:27 +02:00
|
|
|
void LineEdit::setDefaultText(const QString &text)
|
2007-06-21 17:42:05 +02:00
|
|
|
{
|
2007-06-21 20:47:04 +02:00
|
|
|
d->defaultText = text.simplified();
|
2008-03-28 22:11:57 +01:00
|
|
|
//FIXME hardcoded colours aren't nice
|
2007-06-21 21:18:08 +02:00
|
|
|
d->defaultText = QString("<font color=\"gray\">") + d->defaultText + QString("</font>");
|
2008-03-29 23:22:44 +01:00
|
|
|
if (d->showingDefaultText) {
|
|
|
|
QGraphicsTextItem::setHtml(d->defaultText);
|
|
|
|
}
|
2007-06-21 17:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString LineEdit::toHtml()
|
|
|
|
{
|
2008-03-29 23:22:44 +01:00
|
|
|
if (d->showingDefaultText) {
|
2007-06-21 20:47:04 +02:00
|
|
|
return QString();
|
|
|
|
} else {
|
2007-06-21 17:42:05 +02:00
|
|
|
return QGraphicsTextItem::toHtml();
|
2007-06-21 20:47:04 +02:00
|
|
|
}
|
2007-06-21 17:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString LineEdit::toPlainText()
|
|
|
|
{
|
2008-03-29 23:22:44 +01:00
|
|
|
if (d->showingDefaultText) {
|
2007-06-21 20:47:04 +02:00
|
|
|
return QString();
|
|
|
|
} else {
|
2007-06-21 17:42:05 +02:00
|
|
|
return QGraphicsTextItem::toPlainText();
|
2007-06-21 20:47:04 +02:00
|
|
|
}
|
2007-06-21 17:42:05 +02:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:22:44 +01:00
|
|
|
void LineEdit::setHtml(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
d->showingDefaultText = true;
|
|
|
|
QGraphicsTextItem::setHtml(d->defaultText);
|
|
|
|
} else {
|
|
|
|
d->showingDefaultText = false;
|
|
|
|
QGraphicsTextItem::setHtml(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::setPlainText(const QString &text)
|
|
|
|
{
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
d->showingDefaultText = true;
|
|
|
|
QGraphicsTextItem::setHtml(d->defaultText);
|
|
|
|
} else {
|
|
|
|
d->showingDefaultText = false;
|
|
|
|
QGraphicsTextItem::setPlainText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-21 17:42:05 +02:00
|
|
|
void LineEdit::keyPressEvent(QKeyEvent *event)
|
2007-06-20 21:49:31 +02:00
|
|
|
{
|
2007-07-22 15:41:34 +02:00
|
|
|
if ( !d->multiline && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
|
2007-06-20 22:28:55 +02:00
|
|
|
emit editingFinished();
|
2007-06-21 20:47:04 +02:00
|
|
|
event->accept();
|
2007-06-20 22:28:55 +02:00
|
|
|
} else {
|
|
|
|
QGraphicsTextItem::keyPressEvent(event); //let QT handle other keypresses
|
|
|
|
}
|
2007-06-21 20:47:04 +02:00
|
|
|
|
|
|
|
if (QGraphicsTextItem::toHtml() != d->oldText) {
|
|
|
|
d->oldText = QGraphicsTextItem::toHtml();
|
|
|
|
emit textChanged(QGraphicsTextItem::toHtml());
|
2007-06-20 22:28:55 +02:00
|
|
|
}
|
2007-06-24 14:50:58 +02:00
|
|
|
// if (QGraphicsTextItem::toPlainText().simplified().isEmtpy())
|
2007-06-21 20:47:04 +02:00
|
|
|
// QGraphicsTextItem::setHtml(d->defaultText);
|
2007-06-21 17:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::focusInEvent(QFocusEvent *event)
|
|
|
|
{
|
2008-03-29 23:22:44 +01:00
|
|
|
if (d->showingDefaultText) {
|
2007-06-23 01:36:38 +02:00
|
|
|
QGraphicsTextItem::setHtml(QString());
|
2008-03-29 23:22:44 +01:00
|
|
|
d->showingDefaultText = false;
|
2007-06-21 20:47:04 +02:00
|
|
|
}
|
2007-06-21 17:42:05 +02:00
|
|
|
QGraphicsTextItem::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineEdit::focusOutEvent(QFocusEvent *event)
|
|
|
|
{
|
2007-06-21 20:47:04 +02:00
|
|
|
if (QGraphicsTextItem::toPlainText().isEmpty()) {
|
|
|
|
QGraphicsTextItem::setHtml(d->defaultText);
|
2008-03-29 23:22:44 +01:00
|
|
|
d->showingDefaultText = true;
|
2007-06-21 20:47:04 +02:00
|
|
|
}
|
2007-06-21 17:42:05 +02:00
|
|
|
QGraphicsTextItem::focusOutEvent(event);
|
2007-06-20 21:49:31 +02:00
|
|
|
}
|
|
|
|
|
2008-02-13 02:48:18 +01:00
|
|
|
QPointF LineEdit::position() const
|
|
|
|
{
|
|
|
|
return pos();
|
|
|
|
}
|
|
|
|
|
2007-03-04 21:35:37 +01:00
|
|
|
} // namespace Plasma
|
2007-05-23 05:08:47 +02:00
|
|
|
|
|
|
|
#include "lineedit.moc"
|
|
|
|
|