modify abstracttool iface
[yamf.git] / yamf / tools / fill / fill.cpp
blob9ee83e6f5ab2b27cb5830e7c52aed5e89357c082
1 /**************************************************************************
2 * Copyright (C) 2005 by David Cuadrado *
3 * krawek@toonka.com *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #include "fill.h"
23 #include <QGraphicsView>
24 #include <QGraphicsSceneMouseEvent>
25 #include <QSet>
26 #include <QHBoxLayout>
27 #include <QRadioButton>
28 #include <QToolBar>
29 #include <QButtonGroup>
31 #include <dcore/global.h>
32 #include <dcore/debug.h>
34 #include <dgui/iconloader.h>
35 #include <dgui/action.h>
37 #include <dgraphics/pathhelper.h>
38 #include <dgraphics/algorithm.h>
40 #include "yamf/drawing/paintarea.h"
41 #include "yamf/drawing/brushmanager.h"
42 #include "yamf/drawing/photogram.h"
43 #include "yamf/item/path.h"
44 #include "yamf/item/proxy.h"
45 #include "yamf/item/converter.h"
47 #include "yamf/model/command/changeitembrush.h"
48 #include "yamf/model/command/changeitempen.h"
50 #include "yamf/model/frame.h"
52 namespace YAMF {
53 namespace Drawing {
54 class BrushManager;
56 namespace Tool {
58 struct Fill::Private {
59 enum Type {
60 Fill,
61 ShapeFill,
62 OutlineFill
65 DGui::Action * action;
66 QWidget *configWidget;
67 QButtonGroup group;
70 Fill::Fill(QObject *parent) : AbstractTool(parent), d(new Private)
72 d->action = new DGui::Action( DGui::IconLoader::self()->load("fill-tool.svg"), tr("Fill"), this);
73 d->action->setShortcut( QKeySequence(tr("Ctrl+B")) );
75 d->configWidget = new QWidget;
76 QHBoxLayout *layout = new QHBoxLayout(d->configWidget);
77 layout->setMargin(0);
78 layout->setSpacing(2);
80 QRadioButton *fill = new QRadioButton(tr("Fill"));
81 fill->setChecked(true);
82 layout->addWidget(fill);
84 // QRadioButton *shapefill = new QRadioButton(tr("Shape fill"));
86 QRadioButton *outlineFill = new QRadioButton(tr("Outline fill"));
87 layout->addWidget(outlineFill);
89 d->group.addButton(fill, Private::Fill);
90 d->group.addButton(outlineFill, Private::OutlineFill);
93 Fill::~Fill()
95 delete d;
98 void Fill::init(Photogram *photogram)
100 foreach(QGraphicsItem *item, photogram->items() )
102 item->setFlag(QGraphicsItem::ItemIsSelectable, true);
103 item->setFlag(QGraphicsItem::ItemIsFocusable, true);
107 QString Fill::id() const
109 return "fill";
112 void Fill::press(const QGraphicsSceneMouseEvent *input)
114 PaintArea *paintArea = this->paintArea();
115 Photogram *photogram = paintArea->photogram();
117 if(input->buttons() == Qt::LeftButton)
119 QGraphicsItem *item = photogram->itemAt(input->scenePos());
121 if( item )
123 if( paintArea->currentFrame()->logicalIndexOf(item) > -1 )
125 while( Item::Proxy *proxy = qgraphicsitem_cast<Item::Proxy *>(item ) )
127 item = proxy->item();
130 if( QAbstractGraphicsShapeItem *shape = qgraphicsitem_cast<QAbstractGraphicsShapeItem *>(item) )
132 QBrush brush = paintArea->brushManager()->mapBrush(item->sceneBoundingRect(), paintArea->brushManager()->penBrush());
134 if( d->group.checkedId() == Private::Fill )
136 Command::ChangeItemBrush *cmd = new Command::ChangeItemBrush(paintArea->currentFrame(), photogram->itemAt(input->scenePos()), brush);
137 paintArea->addCommand(cmd);
139 else if( d->group.checkedId() == Private::OutlineFill)
141 QPen pen = shape->pen();
142 pen.setBrush(brush);
144 Command::ChangeItemPen *cmd = new Command::ChangeItemPen(paintArea->currentFrame(), photogram->itemAt(input->scenePos()), pen);
145 paintArea->addCommand(cmd);
153 void Fill::move(const QGraphicsSceneMouseEvent *input)
158 void Fill::release(const QGraphicsSceneMouseEvent *input)
162 DGui::Action * Fill::action() const
164 return d->action;
167 int Fill::type() const
169 return AbstractTool::Fill;
172 void Fill::aboutToChangeTool()
176 void Fill::setupConfigBar(QToolBar *configBar)
178 configBar->addWidget(d->configWidget)->setVisible(true);
181 QPainterPath Fill::mapPath(const QPainterPath &path, const QPointF &pos)
183 QMatrix tr1;
184 tr1.translate(pos.x(), pos.y());
186 QPainterPath p1 = tr1.map(path);
187 // p1.closeSubpath();
189 return p1;
192 QPainterPath Fill::mapPath(const QGraphicsPathItem *item)
194 return mapPath(item->path(), item->pos());