Skip to content

Commit

Permalink
Drawing works!
Browse files Browse the repository at this point in the history
Closes #20. AnimationFrameView is pretty much functional right now.
Further modifications will be scheduled in different issues. Also it
fugures that Painter needs adjusting the bounding rect for pen size.
  • Loading branch information
mkatch committed Jun 10, 2014
1 parent 6d25ae8 commit 379a72a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/core/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ void Painter::drawLine(const QPoint &p1, const QPoint &p2)
void Painter::drawRect(const QRect &rect)
{
painter.drawRect(rect);
m_boundingBox |= rect;
int d = -pen().width() / 2;
m_boundingBox |= rect.adjusted(d, d, pen().width() + d, pen().width() + d);
}

/*!
Expand Down
12 changes: 12 additions & 0 deletions src/core/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class Painter

const QBrush &brush() const;
const QPen &pen() const;
QPainter::CompositionMode compositionMode() const;

void setBrush(const QBrush &brush);
void setPen(const QPen &pen);
void setCompositionMode(QPainter::CompositionMode mode);

void drawImage(QString fileName);
void drawPoint(const QPoint &p);
Expand Down Expand Up @@ -46,6 +48,11 @@ inline const QPen &Painter::pen() const
return painter.pen();
}

inline QPainter::CompositionMode Painter::compositionMode() const
{
return painter.compositionMode();
}

inline void Painter::setBrush(const QBrush &brush)
{
painter.setBrush(brush);
Expand All @@ -56,6 +63,11 @@ inline void Painter::setPen(const QPen &pen)
painter.setPen(pen);
}

inline void Painter::setCompositionMode(QPainter::CompositionMode mode)
{
painter.setCompositionMode(mode);
}

inline void Painter::drawPoint(int x, int y)
{
drawPoint(QPoint(x, y));
Expand Down
25 changes: 19 additions & 6 deletions src/tools/dragtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <QDebug>

#include <cstdlib>

DragTool::DragTool(QObject *parent) :
Tool(parent)
{
Expand All @@ -10,9 +12,15 @@ DragTool::DragTool(QObject *parent) :

void DragTool::mouseMoveEvent(ToolMouseMoveEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
if (event->buttons() == Qt::LeftButton) {
view()->translate(event->dViewPos());
event->accept();
} else {
lastPoint = event->pos().toPoint();
if (event->buttons() == Qt::RightButton)
commit();
else
preview();
}
}

Expand All @@ -25,13 +33,18 @@ void DragTool::wheelEvent(ToolWheelEvent *event)

void DragTool::mousePressEvent(ToolMouseEvent *event)
{
lastPoint = event->pos().toPoint();
preview();
event->accept();
if (event->button() == Qt::RightButton) {
lastPoint = event->pos().toPoint();
commit();
event->accept();
}
}

void DragTool::paint(Painter *painter, bool preview)
{
Q_UNUSED(preview)
painter->drawRect(lastPoint.x(), lastPoint.y(), 10, 10);
if (preview)
painter->setPen(QPen(Qt::black, 3));
else
painter->setPen(QPen(QColor(rand() % 256, rand() % 256, rand() % 256), 3));
painter->drawRect(lastPoint.x(), lastPoint.y(), 30, 30);
}
21 changes: 15 additions & 6 deletions src/view/animationframeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ AnimationFrameView::AnimationFrameView(QWidget *parent) :
scene(new QGraphicsScene(this)),
frameItem(new GraphicsAnimationFrameViewItem(this)),
m_frame(nullptr),
m_activeLayer(nullptr)
m_activeLayer(nullptr),
m_tool(nullptr)
{
this->setMouseTracking(true);
scene->addItem(frameItem);
graphicsView->setScene(scene);
graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Expand Down Expand Up @@ -366,13 +368,14 @@ void AnimationFrameView::layOut()

void AnimationFrameView::revertBackBuffer()
{
qDebug() << lastBackBufferChange;
Painter painter(backBuffer);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.drawCanvas(
lastBackBufferChange.topLeft(),
activeLayer()->canvas(),
lastBackBufferChange
);
frameItem->update(lastBackBufferChange);
lastBackBufferChange = QRect();
}

Expand All @@ -382,7 +385,7 @@ void AnimationFrameView::toolPreview()
Painter painter(backBuffer);
m_tool->paint(&painter, true);
lastBackBufferChange = painter.boundingBox();
update();
frameItem->update(lastBackBufferChange);
}

void AnimationFrameView::toolCommit()
Expand All @@ -392,10 +395,15 @@ void AnimationFrameView::toolCommit()
m_tool->paint(&backPainter, false);

Painter *frontPainter = m_activeLayer->newPainter();
frontPainter->drawCanvas(lastBackBufferChange.topLeft(), backBuffer, backPainter.boundingBox());
frontPainter->drawCanvas(
backPainter.boundingBox().topLeft(),
backBuffer,
backPainter.boundingBox()
);
delete frontPainter;

update();
lastBackBufferChange = QRect();
frameItem->update(backPainter.boundingBox());
}

GraphicsAnimationFrameViewItem::GraphicsAnimationFrameViewItem(
Expand Down Expand Up @@ -423,7 +431,8 @@ void GraphicsAnimationFrameViewItem::paint(
Q_UNUSED(widget)

if (view->frame() != nullptr) {
foreach (Layer *layer, view->frame()->layers()) {
for (int i = view->frame()->layerCount() - 1; i >= 0; --i) {
const Layer *layer = view->frame()->layer(i);
const Canvas& canvas = (layer != view->activeLayer())
? layer->canvas().pixmap()
: view->backBuffer;
Expand Down

0 comments on commit 379a72a

Please sign in to comment.