2017-04-23 15:05:48 +02:00
|
|
|
#include "cropscene.hpp"
|
2017-04-29 17:35:42 +02:00
|
|
|
#include <QColorDialog>
|
2017-04-23 15:05:48 +02:00
|
|
|
#include <QDebug>
|
2017-04-26 22:00:13 +02:00
|
|
|
#include <QGraphicsPolygonItem>
|
2017-04-29 12:08:02 +02:00
|
|
|
#include <QGraphicsSceneContextMenuEvent>
|
2017-04-23 15:05:48 +02:00
|
|
|
#include <QGraphicsView>
|
2017-04-29 12:08:02 +02:00
|
|
|
#include <QMenu>
|
2017-04-26 22:00:13 +02:00
|
|
|
#include <QTimer>
|
2017-04-29 17:35:42 +02:00
|
|
|
#include <cropeditor/drawing/dotitem.hpp>
|
2017-04-29 23:00:32 +02:00
|
|
|
#include <cropeditor/drawing/lineitem.hpp>
|
|
|
|
#include <cropeditor/settings/brushpenselection.hpp>
|
2017-04-23 15:05:48 +02:00
|
|
|
|
|
|
|
CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt::NoButton)
|
|
|
|
{
|
2017-04-26 22:00:13 +02:00
|
|
|
QTimer::singleShot(0, [&] {
|
|
|
|
QPolygonF poly;
|
|
|
|
poly.append(sceneRect().topLeft());
|
|
|
|
poly.append(sceneRect().topRight());
|
|
|
|
poly.append(sceneRect().bottomRight());
|
|
|
|
poly.append(sceneRect().bottomLeft());
|
|
|
|
polyItem = new QGraphicsPolygonItem(poly);
|
|
|
|
polyItem->setBrush(QBrush(QColor(0, 0, 0, 191)));
|
|
|
|
polyItem->setPen(QPen(Qt::NoPen));
|
|
|
|
addItem(polyItem);
|
|
|
|
});
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-04-29 23:00:32 +02:00
|
|
|
QPen &CropScene::pen()
|
2017-04-29 17:35:42 +02:00
|
|
|
{
|
|
|
|
return _pen;
|
|
|
|
}
|
|
|
|
|
2017-04-29 23:00:32 +02:00
|
|
|
QBrush &CropScene::brush()
|
2017-04-29 17:35:42 +02:00
|
|
|
{
|
|
|
|
return _brush;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::setDrawingSelection(DrawItem *drawAction)
|
|
|
|
{
|
|
|
|
drawingSelection = drawAction;
|
|
|
|
}
|
|
|
|
|
2017-04-23 15:05:48 +02:00
|
|
|
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
|
|
|
|
{
|
|
|
|
auto buttons = e->buttons();
|
|
|
|
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton)
|
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
if (drawingSelection != nullptr)
|
2017-04-23 15:05:48 +02:00
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
drawingSelection->mouseDragEvent(e, this);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
QPointF p = e->scenePos();
|
|
|
|
if (rect == nullptr)
|
2017-04-23 15:05:48 +02:00
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
|
2017-04-23 15:05:48 +02:00
|
|
|
initPos = p;
|
2017-04-29 17:35:42 +02:00
|
|
|
QPen pen(Qt::NoBrush, 1);
|
|
|
|
pen.setColor(Qt::cyan);
|
|
|
|
rect->setPen(pen);
|
|
|
|
addItem(rect);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
if (prevButtons == Qt::NoButton)
|
|
|
|
{
|
|
|
|
initPos = p;
|
|
|
|
rect->setRect(p.x(), p.y(), 1, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()),
|
|
|
|
qAbs(initPos.y() - p.y())));
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
2017-04-29 17:35:42 +02:00
|
|
|
QPolygonF poly;
|
|
|
|
QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom());
|
|
|
|
poly << sceneRect().topLeft();
|
|
|
|
poly << sceneRect().topRight();
|
|
|
|
poly << sceneRect().bottomRight();
|
|
|
|
poly << theMagicWikipediaPoint;
|
|
|
|
poly << rect->rect().bottomRight();
|
|
|
|
poly << rect->rect().topRight();
|
|
|
|
poly << rect->rect().topLeft();
|
|
|
|
poly << rect->rect().bottomLeft();
|
|
|
|
poly << rect->rect().bottomRight();
|
|
|
|
poly << theMagicWikipediaPoint;
|
|
|
|
poly << sceneRect().bottomLeft();
|
|
|
|
poly << sceneRect().topLeft();
|
2017-04-26 22:00:13 +02:00
|
|
|
|
2017-04-29 17:35:42 +02:00
|
|
|
this->polyItem->setPolygon(poly);
|
|
|
|
e->accept();
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
prevButtons = buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
if (drawingSelection != nullptr)
|
|
|
|
{
|
|
|
|
drawingSelection->mouseDragEndEvent(e, this);
|
2017-04-29 23:00:32 +02:00
|
|
|
delete drawingSelection;
|
2017-04-29 17:35:42 +02:00
|
|
|
drawingSelection = nullptr;
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
prevButtons = Qt::NoButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done();
|
|
|
|
}
|
|
|
|
|
2017-04-29 17:35:42 +02:00
|
|
|
void CropScene::addDrawingAction(QMenu &menu, DrawItem *item)
|
|
|
|
{
|
|
|
|
QAction *action = new QAction;
|
|
|
|
action->setText(item->name());
|
2017-04-29 23:00:32 +02:00
|
|
|
QObject::connect(action, &QAction::triggered, [this, item](bool) { setDrawingSelection(item); });
|
2017-04-29 17:35:42 +02:00
|
|
|
menu.addAction(action);
|
|
|
|
}
|
|
|
|
|
2017-04-29 12:08:02 +02:00
|
|
|
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
|
|
|
|
{
|
2017-04-29 23:00:32 +02:00
|
|
|
QMenu menu(e->widget());
|
2017-04-29 17:35:42 +02:00
|
|
|
|
2017-04-29 23:00:32 +02:00
|
|
|
addDrawingAction(menu, new DotItem);
|
|
|
|
addDrawingAction(menu, new LineItem);
|
2017-04-29 17:35:42 +02:00
|
|
|
|
2017-04-29 23:00:32 +02:00
|
|
|
menu.addSeparator();
|
|
|
|
QAction *settings = new QAction;
|
|
|
|
settings->setText("Settings");
|
|
|
|
connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); });
|
|
|
|
menu.addAction(settings);
|
2017-04-29 12:08:02 +02:00
|
|
|
|
2017-04-29 23:00:32 +02:00
|
|
|
menu.exec(e->screenPos());
|
2017-04-29 12:08:02 +02:00
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
2017-04-29 18:11:18 +02:00
|
|
|
void CropScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
|
2017-04-23 15:05:48 +02:00
|
|
|
{
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::done()
|
|
|
|
{
|
2017-04-29 17:35:42 +02:00
|
|
|
if (rect)
|
|
|
|
{
|
|
|
|
rect->setPen(QPen(Qt::NoPen));
|
|
|
|
emit closedWithRect(rect->rect().toRect());
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|