1 /***************************************************************************
2 * Copyright (C) 2007 Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Lesser General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2.1 of the License, or (at your option) any later version. *
10 * This library 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 GNU *
13 * Lesser General Public License for more details. *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with this library; if not, write to the Free Software *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
25 #include <QKeySequence>
28 #include <QGraphicsView>
30 #include <QGraphicsSceneMouseEvent>
31 #include <QGraphicsLineItem>
34 #include <QHBoxLayout>
37 #include <dgraphics/algorithm.h>
38 #include <dcore/algorithm.h>
39 #include <dcore/debug.h>
40 #include <dgui/action.h>
41 #include <dgui/iconloader.h>
43 #include <drawing/photogram.h>
44 #include <drawing/brushmanager.h>
45 #include <drawing/paintarea.h>
47 #include <model/frame.h>
48 #include <model/command/changetext.h>
49 #include <model/command/changetextwidth.h>
50 #include <model/command/changetextfont.h>
52 #include <item/text.h>
54 #include <dgui/fontchooser.h>
65 YAMF::Item::Text
*text
;
66 QGraphicsLineItem
*line
;
70 DGui::FontChooser
*font
;
82 * Construye la herramienta para insertar texto a la animaci�n.
84 Text::Text(QObject
*parent
) : AbstractTool(parent
), d(new Private
)
86 d
->action
= new DGui::Action( DGui::IconLoader::self()->load("draw-text.svg"), tr("Text"), this);
92 d
->font
= new DGui::FontChooser
;
93 connect(d
->font
, SIGNAL(fontChanged()), this, SLOT(onChangeFont()));
111 * Inicializa la herramienta, evita que los items dentro de el fotograma se puedan mover o seleccionar.
113 void Text::init(Photogram
*photogram
)
115 foreach(QGraphicsView
* view
, photogram
->views())
117 view
->setDragMode ( QGraphicsView::NoDrag
);
119 if ( QGraphicsScene
*photogram
= qobject_cast
<QGraphicsScene
*>(view
->scene()) )
121 foreach(QGraphicsItem
*item
, photogram
->items() )
123 item
->setFlag(QGraphicsItem::ItemIsSelectable
, false);
124 item
->setFlag(QGraphicsItem::ItemIsMovable
, false);
127 if(YAMF::Item::Text
*text
= static_cast< YAMF::Item::Text
* >(item
))
129 text
->setFlags( QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable
);
138 * Retorna el id de la herramienta.
140 QString
Text::id() const
147 * Funci�n sobrecargada para implementar la inserci�n de texto
149 void Text::press(const QGraphicsSceneMouseEvent
*input
)
151 PaintArea
*paintArea
= this->paintArea();
152 if(input
->buttons() == Qt::LeftButton
)
154 QList
<QGraphicsItem
* > items
= paintArea
->scene()->selectedItems();
156 foreach(QGraphicsItem
*item
, items
)
158 if(item
/* && item != d->text*/)
160 if( paintArea
->currentFrame()->visualIndexOf(item
) != -1)
162 YAMF::Item::Text
*text
= static_cast< YAMF::Item::Text
* >(item
);
168 d
->text
->setEditable(false);
172 d
->position
= d
->text
->scenePos();
174 d
->oldWidth
= d
->text
->textWidth();
182 d
->text
= new Item::Text();
185 connect(d
->text
, SIGNAL(edited()), this, SLOT(
188 d
->editWidth
= false;
189 d
->text
->setFlags( QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsFocusable
);
190 d
->paintArea
= paintArea
;
192 d
->position
= input
->scenePos();
193 d
->text
->setPos(d
->position
);
194 d
->text
->setFont(d
->font
->currentFont());
196 d
->text
->setDefaultTextColor(paintArea
->brushManager()->penBrush().color());
198 d
->line
= new QGraphicsLineItem
;
199 d
->line
->setPen(QPen(Qt::red
, 2));
200 paintArea
->photogram()->addItem(d
->line
);
206 * Funci�n sobrecargada para implementar la inserci�n de texto
208 void Text::move(const QGraphicsSceneMouseEvent
*input
)
210 if(input
->buttons() == Qt::LeftButton
)
216 double width
= std::abs(d
->position
.x() - input
->scenePos().x());
217 d
->text
->setTextWidth( width
);
221 d
->line
->setLine(QLineF(d
->position
, QPointF(d
->line
->mapFromScene(input
->scenePos()).x(), d
->position
.y())));
229 * Funci�n sobrecargada para implementar la inserci�n de texto
231 void Text::release(const QGraphicsSceneMouseEvent
*input
)
233 PaintArea
*paintArea
= this->paintArea(); // FIXME
237 d
->paintArea
= paintArea
;
244 double width
= std::abs(d
->position
.x() - input
->scenePos().x());
245 d
->text
->setTextWidth( width
);
246 paintArea
->photogram()->removeItem(d
->line
);
248 paintArea
->scene()->addItem(d
->text
);
249 d
->text
->setEditable(true);
251 if(d
->position
.x() > input
->scenePos().x())
252 d
->text
->setPos(d
->text
->scenePos() - QPointF(d
->text
->textWidth(), 0));
259 paintArea
->addCommand(new Command::ChangeTextWidth(paintArea
->currentFrame(), d
->text
, d
->oldWidth
));
260 d
->oldWidth
= d
->text
->textWidth();
266 * Retorna la acci�n que representa la herramienta.
268 DGui::Action
*Text::action() const
275 * Retorna que la herramienta es de tipo Brush.
277 int Text::type() const
279 return AbstractTool::Brush
;
282 void Text::aboutToChangeTool()
284 Q_CHECK_PTR(d
->paintArea
);
285 Q_CHECK_PTR(d
->text
);
286 if(d
->paintArea
&& d
->text
)
288 if(!d
->text
->toPlainText().isEmpty())
290 d
->text
->setEditable(false);
299 * Asigna la interfaz gráfica para configurar el tipo de letra del texto.
301 void Text::setupConfigBar(QToolBar
*configBar
)
303 configBar
->addWidget(d
->font
)->setVisible(true);
308 * Ignora el evento de presionado de teclas para poder escribir el esto.
310 void Text::keyPressEvent(QKeyEvent
*event
)
318 * Cambia el tipo de letra que se seleccione en el configurador.
320 void Text::onChangeFont()
324 QFont oldFont
= d
->text
->font();
326 d
->text
->setFont(d
->font
->currentFont());
328 d
->text
->setEditable(true);
330 d
->text
->setSelected(true);
332 if(d
->added
&& oldFont
!= d
->text
->font())
334 d
->paintArea
->addCommand(new Command::ChangeTextFont( d
->paintArea
->currentFrame(), d
->text
, oldFont
));
339 void Text::onEdited()
341 if(d
->text
&& d
->paintArea
)
343 if(d
->oldText
.isEmpty())
345 d
->paintArea
->currentFrame()->addItem(d
->text
);
350 d
->paintArea
->addCommand(new Command::ChangeText( d
->paintArea
->currentFrame(), d
->text
, d
->oldText
));
352 d
->oldText
= d
->text
->toPlainText();