1 /***************************************************************************
2 * Copyright (C) 2006 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 ***************************************************************************/
22 #include <dgui/action.h>
24 #include <drawing/photogram.h>
25 #include <drawing/paintarea.h>
26 #include <drawing/brushmanager.h>
28 #include <item/path.h>
29 #include <item/proxy.h>
30 #include <item/serializer.h>
32 #include <model/command/convertitem.h>
33 #include <model/command/editnodesitem.h>
34 #include <model/frame.h>
36 #include <QGraphicsItem>
37 #include <QGraphicsSceneMouseEvent>
39 #include <dgui/iconloader.h>
40 #include <dcore/debug.h>
41 #include "private/contoureditor.h"
50 struct Contour::Private
53 QList
<Drawing::Tool::Private::ContourEditor
*> editors
;
58 * Construye la herramienta para editar los nodos de un grafico.
60 Contour::Contour(QObject
*parent
) : Drawing::AbstractTool(parent
), d(new Private
)
62 d
->action
= new DGui::Action( DGui::IconLoader::self()->load("edit-path.svg"), tr("Contour selection"), this);
76 * Inicializa la herramienta, permite que los items dentro de el fotograma se puedan seleccionar o pero no mover.
78 void Contour::init(Photogram
*photogram
)
80 qDeleteAll(d
->editors
);
83 foreach(QGraphicsView
* view
, photogram
->views())
85 foreach(QGraphicsItem
*item
, view
->scene()->items())
87 item
->setFlags(QGraphicsItem::ItemIsSelectable
);
88 item
->setFlag(QGraphicsItem::ItemIsMovable
, false);
95 * Retorna el id de la herramienta.
97 QString
Contour::id() const
104 * Función sobrecargada para implementar la edición de nodos.
106 void Contour::press(const QGraphicsSceneMouseEvent
*input
)
109 PaintArea
*paintArea
= this->paintArea();
110 aboutToChangePhotogram(paintArea
->photogram());
115 * Función sobrecargada para implementar la edición de nodos.
117 void Contour::move(const QGraphicsSceneMouseEvent
*input
)
124 * Función sobrecargada para implementar la edición de nodos.
126 void Contour::release(const QGraphicsSceneMouseEvent
*input
)
129 PaintArea
*paintArea
= this->paintArea();
131 if(paintArea
->scene()->selectedItems().count() > 0)
133 QList
<QGraphicsItem
*> selecteds
= paintArea
->scene()->selectedItems();
134 QList
<Drawing::Tool::Private::ContourEditor
*>::iterator it
= d
->editors
.begin();
135 QList
<Drawing::Tool::Private::ContourEditor
*>::iterator itEnd
= d
->editors
.end();
139 int parentIndex
= paintArea
->scene()->selectedItems().indexOf((*it
)->item() );
140 if(parentIndex
!= -1 )
142 selecteds
.removeAt(parentIndex
);
146 delete d
->editors
.takeAt(d
->editors
.indexOf((*it
)));
151 foreach(QGraphicsItem
*item
, selecteds
)
155 int index
= paintArea
->currentFrame()->logicalIndexOf(item
);
158 item
->setFlag(QGraphicsItem::ItemIsMovable
, false);
159 if(Item::Path
*path
= qgraphicsitem_cast
<Item::Path
*>(item
) )
161 d
->editors
<< new Drawing::Tool::Private::ContourEditor(path
, paintArea
->scene());
165 if(item
->type() != QGraphicsItemGroup::Type
)
167 Command::ConvertItem
*convertItem
= new Command::ConvertItem(item
, Item::Path::Type
, paintArea
->currentFrame());
168 paintArea
->addCommand(convertItem
);
170 if(convertItem
->isValid())
172 if(Item::Path
*path
= qgraphicsitem_cast
<Item::Path
*>( paintArea
->currentFrame()->item(index
) ) )
174 d
->editors
<< new Drawing::Tool::Private::ContourEditor(path
, paintArea
->photogram());
176 else if(Item::Proxy
*proxy
= qgraphicsitem_cast
<Item::Proxy
*>( paintArea
->currentFrame()->item(index
) ))
178 if(Item::Path
*path
= qgraphicsitem_cast
<Item::Path
*>( proxy
->item() ) )
180 d
->editors
<< new Drawing::Tool::Private::ContourEditor(path
, paintArea
->photogram());
181 d
->editors
.last()->setProxyItem(proxy
);
191 Model::Frame
*currentFrame
= paintArea
->currentFrame();
193 foreach(Drawing::Tool::Private::ContourEditor
*editor
, d
->editors
)
195 if(editor
->isModified())
197 if(currentFrame
->logicalIndexOf(editor
->item()) > -1)
199 QPainterPath path
= editor
->item()->path();
200 // editor->restore();
202 paintArea
->addCommand(new Command::EditNodesItem(editor
->item(), editor
->oldPath(), paintArea
->currentFrame()));
211 qDeleteAll(d
->editors
);
218 * Elimina un nodo si se recibe que se presiono la conbinación de teclas Control+supr.
220 void Contour::keyPressEvent(QKeyEvent
*event
)
222 if(event
->key() == Qt::Key_Delete
& Qt::Key_Control
)
224 foreach(Drawing::Tool::Private::ContourEditor
*editor
, d
->editors
)
226 editor
->removeSelectedNodes();
233 * Función reimplemenatada para evita que se des-aparescan los nodos del fotograma.
235 void Contour::photogramChanged(Photogram
*const photogram
)
237 if(photogram
->selectedItems().count() > 0)
239 foreach(Drawing::Tool::Private::ContourEditor
*editor
, d
->editors
)
246 qDeleteAll(d
->editors
);
253 * Retorna la acción que representa la herramienta.
255 DGui::Action
*Contour::action() const
262 * Retorna que la herramienta es de tipo Selection.
264 int Contour::type() const
266 return AbstractTool::Selection
;
271 * Elimina los nodos cuando se cambia de herramienta.
273 void Contour::aboutToChangeTool()
275 qDeleteAll(d
->editors
);