1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "serializer.h"
26 #include <QFocusEvent>
29 #include <QGraphicsSceneMouseEvent>
31 #include <dcore/debug.h>
36 struct Text::Private
{
37 Private() : isEditable(false) {}
39 GraphicsItemFlags flags
;
45 * Construye un texto con un �tem "padre" @p parent y dentro de la escena @p scene.
47 Text::Text(QGraphicsItem
* parent
, QGraphicsScene
* scene
)
48 : QGraphicsTextItem(parent
, scene
), d(new Private
)
51 setOpenExternalLinks(true);
66 * Toma los valores dentro del documento xml @p xml y se las asigna a las propiedades del texto.
68 void Text::fromXml(const QString
&xml
)
75 * Crea un elemento del documento xml @p doc con las propiedades actuales del texto.
77 QDomElement
Text::toXml(QDomDocument
&doc
) const
79 QDomElement root
= doc
.createElement("text");
80 root
.setAttribute("width", textWidth() );
81 root
.setAttribute("text-color", defaultTextColor().name() );
83 root
.setAttribute("data", toPlainText() );
85 root
.appendChild( Serializer::properties( this, doc
));
86 QFont font
= this->font();
87 root
.appendChild( Serializer::font( font
, doc
) );
94 * Si @p editable es verdadero permitira editar el texto, de lo contrario no.
96 void Text::setEditable(bool editable
)
98 d
->isEditable
= editable
;
102 d
->flags
= flags(); // save flags
103 setTextInteractionFlags(Qt::TextEditorInteraction
);
104 setFlags( QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable
);
105 setFocus(Qt::MouseFocusReason
);
109 setTextInteractionFlags(Qt::NoTextInteraction
/*Qt::TextBrowserInteraction*/);
110 // setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable ); // restore flags
115 void Text::paint( QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
* widget
)
117 if(toPlainText().isEmpty())
119 painter
->drawRect(boundingRect());
121 QGraphicsTextItem::paint(painter
, option
, widget
);
126 * Si el item es editable lo cambia a no editable, y si es no editable lo cambia a editable.
128 void Text::toggleEditable()
130 setEditable( !d
->isEditable
);
135 * Desactiva la edicion del texto cuando pierde el foco.
137 void Text::focusOutEvent(QFocusEvent
* event
)
139 QGraphicsTextItem::focusOutEvent(event
);
140 if ( textInteractionFlags() & Qt::TextEditorInteraction
&& d
->isEditable
)
142 QTimer::singleShot( 0, this, SLOT(toggleEditable()));
149 * Activa la edicion del texto cuando el texto recibe un evento de doble click.
151 void Text::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent
* event
)
156 void Text::mousePressEvent ( QGraphicsSceneMouseEvent
* event
)
158 QGraphicsTextItem::mousePressEvent( event
);
159 // event->setAccepted( false );
160 // dfDebug << event->isAccepted();