1 /***************************************************************************
2 * Copyright (C) 2005 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 ***************************************************************************/
24 #include <QKeySequence>
25 #include <QGraphicsPathItem>
26 #include <QPainterPath>
28 #include <QGraphicsLineItem>
29 #include <QGraphicsView>
31 #include <QGraphicsSceneMouseEvent>
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>
48 #include <model/frame.h>
49 #include <item/path.h>
57 Private() : item(0), exactness(0), configWidget(0) {}
65 YAMF::Item::Path
*item
;
67 QDoubleSpinBox
*exactness
;
68 QWidget
*configWidget
;
73 * Construye la herramienta.
75 Brush::Brush(QObject
*parent
) : AbstractTool(parent
), d(new Private
)
77 d
->action
= new DGui::Action( DGui::IconLoader::self()->load("draw-freehand.svg"), tr("Pencil"), this);
78 d
->action
->setShortcut( QKeySequence(tr("Ctrl+B")) );
80 d
->exactness
= new QDoubleSpinBox
;
81 d
->exactness
->setValue(3.0);
84 d
->configWidget
= new QWidget
;
85 QHBoxLayout
*layout
= new QHBoxLayout(d
->configWidget
);
87 layout
->setSpacing(2);
88 layout
->addWidget(new QLabel(tr("Smoothness")));
89 layout
->addWidget(d
->exactness
);
100 d
->configWidget
->setParent(0);
101 delete d
->configWidget
;
108 * Inicializa la herramienta, evita que los items dentro de el fotograma se puedan mover o seleccionar
110 void Brush::init(Photogram
*photogram
)
112 foreach(QGraphicsView
* view
, photogram
->views())
114 view
->setDragMode ( QGraphicsView::NoDrag
);
116 if ( QGraphicsScene
*photogram
= qobject_cast
<QGraphicsScene
*>(view
->scene()) )
118 foreach(QGraphicsItem
*item
, photogram
->items() )
120 item
->setFlag(QGraphicsItem::ItemIsSelectable
, false);
121 item
->setFlag(QGraphicsItem::ItemIsMovable
, false);
129 * Retorna el id de la herramienta
131 QString
Brush::id() const
138 * Función sobrecargada para implementar el trazo de lineas.
140 void Brush::press(const QGraphicsSceneMouseEvent
*input
)
142 PaintArea
*paintArea
= this->paintArea();
144 d
->firstPoint
= input
->scenePos();
145 d
->path
= QPainterPath();
146 d
->path
.moveTo(d
->firstPoint
);
147 d
->oldPos
= input
->scenePos();
148 d
->item
= new Item::Path();
149 d
->item
->setPen(paintArea
->brushManager()->pen());
150 paintArea
->photogram()->addItem( d
->item
);
151 d
->item
->setZValue(1000);
156 * Función sobrecargada para implementar el trazo de lineas.
158 void Brush::move(const QGraphicsSceneMouseEvent
*input
)
160 PaintArea
*paintArea
= this->paintArea();
162 if(input
->buttons() == Qt::LeftButton
)
164 QPointF lastPoint
= input
->scenePos();
166 foreach(QGraphicsView
* view
, paintArea
->photogram()->views())
168 // view->setDragMode (QGraphicsView::NoDrag);
171 // d->path.moveTo( d->oldPos);
172 d
->path
.lineTo( lastPoint
);
174 d
->item
->setPath(d
->path
);
175 paintArea
->brushManager()->map(d
->item
);
177 d
->oldPos
= lastPoint
;
183 * Función sobrecargada para implementar el trazo de lineas.
185 void Brush::release(const QGraphicsSceneMouseEvent
*input
)
187 PaintArea
*paintArea
= this->paintArea();
188 BrushManager
*brushManager
= paintArea
->brushManager();
190 double smoothness
= 0.1;
193 smoothness
= d
->exactness
->value();
196 if ( d
->firstPoint
== input
->pos() && d
->path
.elementCount() == 1)
199 d
->path
.addEllipse(input
->pos().x(), input
->pos().y(), brushManager
->pen().width(), brushManager
->pen().width());
202 smoothPath( d
->path
, smoothness
);
204 d
->firstPoint
= QPoint(0,0);
205 d
->item
->setBrush( brushManager
->brush() );
206 d
->item
->setPath(d
->path
);
207 paintArea
->brushManager()->map(d
->item
);
210 paintArea
->currentFrame()->addItem(d
->item
);
216 * Suaviza el trazo hecho por el usuario.
218 void Brush::smoothPath(QPainterPath
&path
, double smoothness
, int from
, int to
)
222 QPolygonF polygon
= path
.toFillPolygon();
223 if(!polygon
.isEmpty() && polygon
.isClosed())
226 path
= DGraphics::Algorithm::smooth(polygon
, smoothness
, from
, to
);
232 * Retorna la acción que representa la herramienta.
234 DGui::Action
* Brush::action() const
241 * Retorna que la herramienta es de tipo Brush.
243 int Brush::type() const
245 return AbstractTool::Brush
;
248 void Brush::aboutToChangeTool()
254 * Asigna la interfaz gráfica para configurar el grado de suavizado.
256 void Brush::setupConfigBar(QToolBar
*configBar
)
258 configBar
->addWidget(d
->configWidget
)->setVisible(true);