2017-04-23 15:05:48 +02:00
|
|
|
#include "cropscene.hpp"
|
2017-04-29 17:35:42 +02:00
|
|
|
#include <QColorDialog>
|
2017-06-26 17:58:36 +02:00
|
|
|
#include <QDebug>
|
2017-05-05 23:59:39 +02:00
|
|
|
#include <QFontDialog>
|
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-05-01 11:28:54 +02:00
|
|
|
#include <cropeditor/drawing/bluritem.hpp>
|
2017-04-29 17:35:42 +02:00
|
|
|
#include <cropeditor/drawing/dotitem.hpp>
|
2017-06-26 18:27:38 +02:00
|
|
|
#include <cropeditor/drawing/ellipseitem.hpp>
|
2017-06-26 15:43:12 +02:00
|
|
|
#include <cropeditor/drawing/eraseritem.hpp>
|
2017-04-29 23:00:32 +02:00
|
|
|
#include <cropeditor/drawing/lineitem.hpp>
|
2017-05-01 18:43:54 +02:00
|
|
|
#include <cropeditor/drawing/pathitem.hpp>
|
2017-06-26 15:43:12 +02:00
|
|
|
#include <cropeditor/drawing/rectitem.hpp>
|
2017-05-05 23:59:39 +02:00
|
|
|
#include <cropeditor/drawing/textitem.hpp>
|
2017-04-29 23:00:32 +02:00
|
|
|
#include <cropeditor/settings/brushpenselection.hpp>
|
2017-05-02 19:49:33 +02:00
|
|
|
#include <functional>
|
2017-05-02 15:47:08 +02:00
|
|
|
#include <settings.hpp>
|
2017-04-23 15:05:48 +02:00
|
|
|
|
2017-05-02 19:49:33 +02:00
|
|
|
CropScene::CropScene(QObject *parent, QPixmap *pixmap)
|
2017-05-09 17:26:00 +02:00
|
|
|
: QGraphicsScene(parent), drawingSelectionMaker([] { return nullptr; }), prevButtons(Qt::NoButton),
|
|
|
|
_font(settings::settings().value("font", QFont()).value<QFont>()) {
|
2017-06-27 00:27:04 +02:00
|
|
|
_pixmap = pixmap;
|
2017-05-09 17:26:00 +02:00
|
|
|
pen().setColor(settings::settings().value("penColor", pen().color()).value<QColor>());
|
|
|
|
pen().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool());
|
|
|
|
pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toFloat());
|
|
|
|
brush().setColor(settings::settings().value("brushColor", brush().color()).value<QColor>());
|
|
|
|
|
|
|
|
addDrawingAction(menu, "Dot", [] { return new DotItem; });
|
|
|
|
addDrawingAction(menu, "Path", [] { return new PathItem; });
|
|
|
|
addDrawingAction(menu, "Blur", [] { return new BlurItem; });
|
|
|
|
addDrawingAction(menu, "Straight line", [] { return new LineItem; });
|
|
|
|
addDrawingAction(menu, "Text", [] { return new TextItem; });
|
2017-06-26 15:43:12 +02:00
|
|
|
addDrawingAction(menu, "Rectangle", [] { return new RectItem; });
|
2017-06-26 18:27:38 +02:00
|
|
|
addDrawingAction(menu, "Ellipse", [] { return new EllipseItem; });
|
2017-06-26 15:43:12 +02:00
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
addDrawingAction(menu, "Eraser", [] { return new EraserItem; });
|
2017-06-26 21:20:17 +02:00
|
|
|
QAction *clear = menu.addAction("Clear all drawing");
|
2017-06-26 15:43:12 +02:00
|
|
|
connect(clear, &QAction::triggered, [&] {
|
|
|
|
auto its = items();
|
|
|
|
for (auto i : its) {
|
|
|
|
if (i != rect && i != polyItem && i->zValue() != -1) removeItem(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-26 16:28:45 +02:00
|
|
|
QAction *reset = menu.addAction("Reset pen selection");
|
|
|
|
connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); });
|
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
menu.addSeparator();
|
|
|
|
QAction *settings = new QAction;
|
|
|
|
settings->setText("Settings");
|
|
|
|
menu.addSeparator();
|
|
|
|
display = menu.addAction(drawingName);
|
|
|
|
display->setDisabled(true);
|
|
|
|
connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); });
|
|
|
|
menu.addAction(settings);
|
|
|
|
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifier = addPixmap(pixmap->copy(0, 0, 11, 11).scaled(110, 110));
|
|
|
|
magnifierBox = addRect(magnifier->boundingRect(), QPen(Qt::cyan));
|
|
|
|
magnifier->setZValue(1);
|
|
|
|
magnifierBox->setZValue(1.1);
|
|
|
|
magnifierBox->setParentItem(magnifier);
|
|
|
|
magnifierHint = addText("ptr: (0, 0)\nsel: (0, 0, 0, 0)");
|
|
|
|
magnifierHint->setParentItem(magnifier);
|
|
|
|
magnifierHint->setY(magnifier->boundingRect().height());
|
|
|
|
QColor c(Qt::cyan);
|
|
|
|
c.setAlphaF(.25);
|
|
|
|
magnifierHintBox = addRect(magnifierHint->boundingRect(), Qt::NoPen, c);
|
|
|
|
magnifierHintBox->setParentItem(magnifierHint);
|
|
|
|
magnifierHintBox->setZValue(1);
|
|
|
|
magnifierHint->setZValue(1.1);
|
2017-06-27 00:27:04 +02:00
|
|
|
initMagnifierGrid();
|
|
|
|
updateMag(QPointF(0, 0));
|
2017-06-27 00:02:27 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
|
|
|
|
|
|
|
|
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-05-09 17:26:00 +02:00
|
|
|
CropScene::~CropScene() {
|
|
|
|
delete drawingSelection;
|
|
|
|
}
|
2017-05-02 19:49:33 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QPen &CropScene::pen() {
|
|
|
|
return _pen;
|
|
|
|
}
|
2017-04-29 17:35:42 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QBrush &CropScene::brush() {
|
|
|
|
return _brush;
|
|
|
|
}
|
2017-04-29 17:35:42 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
QFont &CropScene::font() {
|
|
|
|
return _font;
|
|
|
|
}
|
2017-05-05 23:59:39 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> drawAction) {
|
2017-05-11 14:49:00 +02:00
|
|
|
this->setFocus();
|
2017-05-09 17:26:00 +02:00
|
|
|
drawingSelectionMaker = drawAction;
|
|
|
|
drawingSelection = drawAction();
|
|
|
|
drawingName = name;
|
|
|
|
if (drawingSelection)
|
|
|
|
if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; });
|
2017-04-29 17:35:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::fontAsk() {
|
2017-05-09 17:26:00 +02:00
|
|
|
bool ok = false;
|
|
|
|
QFont font = QFontDialog::getFont(&ok, this->font(), nullptr, "Font to use");
|
|
|
|
if (ok) _font = font;
|
2017-05-05 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
|
2017-06-27 00:27:04 +02:00
|
|
|
updateMag(e->scenePos());
|
2017-06-27 00:02:27 +02:00
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
auto buttons = e->buttons();
|
2017-06-26 17:58:36 +02:00
|
|
|
if (e->modifiers() & Qt::ControlModifier && buttons == Qt::LeftButton) {
|
|
|
|
QTransform stupidThing = views()[0]->transform();
|
|
|
|
auto item = itemAt(e->screenPos(), stupidThing);
|
|
|
|
if (item && item != polyItem && item != rect && item->zValue() != -1) {
|
|
|
|
QPointF delta = e->scenePos() - e->lastScenePos();
|
|
|
|
item->moveBy(delta.x(), delta.y());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-06-27 00:02:27 +02:00
|
|
|
if (buttons == Qt::LeftButton || (prevButtons == Qt::NoButton && prevButtons != buttons)) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (drawingSelection) {
|
|
|
|
drawingSelection->mouseDragEvent(e, this);
|
2017-05-05 23:59:39 +02:00
|
|
|
} else {
|
2017-05-09 17:26:00 +02:00
|
|
|
QPointF p = e->scenePos();
|
|
|
|
if (rect == nullptr) {
|
|
|
|
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
|
|
|
|
initPos = p;
|
|
|
|
QPen pen(Qt::NoBrush, 1);
|
|
|
|
pen.setColor(Qt::cyan);
|
|
|
|
rect->setPen(pen);
|
|
|
|
addItem(rect);
|
|
|
|
} else {
|
|
|
|
if (prevButtons == Qt::NoButton) {
|
|
|
|
initPos = p;
|
|
|
|
rect->setRect(p.x(), p.y(), 1, 1);
|
|
|
|
} else {
|
2017-06-13 12:59:58 +02:00
|
|
|
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()),
|
|
|
|
qAbs(initPos.y() - p.y())));
|
2017-05-09 17:26:00 +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();
|
|
|
|
|
|
|
|
this->polyItem->setPolygon(poly);
|
|
|
|
e->accept();
|
2017-04-29 17:35:42 +02:00
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
2017-05-09 17:26:00 +02:00
|
|
|
prevButtons = buttons;
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (drawingSelection) {
|
|
|
|
drawingSelection->mouseDragEndEvent(e, this);
|
|
|
|
delete drawingSelection;
|
|
|
|
drawingSelection = drawingSelectionMaker();
|
|
|
|
if (drawingSelection)
|
|
|
|
if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; });
|
2017-05-13 19:19:56 +02:00
|
|
|
} else if (settings::settings().value("quickMode", false).toBool())
|
|
|
|
done();
|
2017-05-09 17:26:00 +02:00
|
|
|
prevButtons = Qt::NoButton;
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 00:27:04 +02:00
|
|
|
void CropScene::wheelEvent(QGraphicsSceneWheelEvent *event) {
|
|
|
|
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
|
|
|
|
if (pixCnt % 2 == 0) pixCnt++;
|
|
|
|
if (pixCnt > 20) return;
|
|
|
|
if (event->delta() > 0 && pixCnt < 19)
|
|
|
|
settings::settings().setValue("magnifierPixelCount", pixCnt += 2);
|
|
|
|
else if (pixCnt > 1)
|
|
|
|
settings::settings().setValue("magnifierPixelCount", pixCnt -= 2);
|
|
|
|
|
|
|
|
for (auto item : gridRectsX) delete item;
|
|
|
|
gridRectsX.clear();
|
|
|
|
for (auto item : gridRectsY) delete item;
|
|
|
|
gridRectsY.clear();
|
|
|
|
|
|
|
|
initMagnifierGrid();
|
|
|
|
updateMag(event->scenePos());
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:26:00 +02:00
|
|
|
void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item) {
|
|
|
|
QAction *action = new QAction;
|
|
|
|
action->setText(name);
|
|
|
|
connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); });
|
|
|
|
menu.addAction(action);
|
2017-04-29 17:35:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
|
2017-05-09 17:26:00 +02:00
|
|
|
display->setText(drawingName);
|
|
|
|
menu.exec(e->screenPos());
|
|
|
|
e->accept();
|
2017-04-29 12:08:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::keyReleaseEvent(QKeyEvent *event) {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); // Segfault
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 00:27:04 +02:00
|
|
|
void CropScene::updateMag(QPointF scenePos) {
|
|
|
|
QString rectStr("(0, 0, 0, 0)");
|
|
|
|
if (rect) {
|
|
|
|
rectStr = "(%0, %1, %2, %3)";
|
|
|
|
rectStr = rectStr.arg(rect->rect().x()).arg(rect->rect().y()).arg(rect->rect().width()).arg(rect->rect().height());
|
|
|
|
}
|
|
|
|
magnifierHint->setPlainText(QString("ptr: (%0, %1)\nsel: %2").arg(scenePos.x()).arg(scenePos.y()).arg(rectStr));
|
|
|
|
magnifierHintBox->setRect(magnifierHint->boundingRect());
|
|
|
|
|
|
|
|
QPointF magnifierTopLeft = scenePos - QPointF(5.5, 5.5);
|
|
|
|
QPointF magnifierPos = scenePos + QPointF(11, 11);
|
|
|
|
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
|
|
|
|
if (pixCnt % 2 == 0) pixCnt++;
|
|
|
|
|
|
|
|
magnifier->setPos(magnifierPos);
|
|
|
|
magnifier->setPixmap(_pixmap->copy(magnifierTopLeft.x(), magnifierTopLeft.y(), pixCnt, pixCnt).scaled(110, 110));
|
|
|
|
QPointF bottomRight = magnifierHintBox->sceneBoundingRect().bottomRight();
|
|
|
|
if (magnifier->sceneBoundingRect().bottom() > bottomRight.y())
|
|
|
|
bottomRight.setY(magnifier->sceneBoundingRect().bottom());
|
|
|
|
|
|
|
|
if (magnifier->sceneBoundingRect().right() > bottomRight.x())
|
|
|
|
bottomRight.setX(magnifier->sceneBoundingRect().right());
|
|
|
|
|
|
|
|
if (bottomRight.x() > sceneRect().right())
|
|
|
|
magnifierPos -= QPointF(qMax(130., magnifierHintBox->boundingRect().width()), 0);
|
|
|
|
if (bottomRight.y() > sceneRect().bottom())
|
|
|
|
magnifierPos -= QPointF(0, 130 + magnifierHintBox->boundingRect().height());
|
|
|
|
magnifier->setPos(magnifierPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::initMagnifierGrid() {
|
|
|
|
QColor c(Qt::cyan);
|
|
|
|
c.setAlphaF(.25);
|
|
|
|
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
|
|
|
|
if (pixCnt % 2 == 0) pixCnt++;
|
|
|
|
for (int i = 0; i < pixCnt; i++) {
|
|
|
|
auto gridRectX = addRect(0, i * 110. / pixCnt, 110, 110. / pixCnt, QPen(Qt::black));
|
|
|
|
auto gridRectY = addRect(i * 110. / pixCnt, 0, 110. / pixCnt, 110, QPen(Qt::black));
|
|
|
|
gridRectX->setParentItem(magnifierBox);
|
|
|
|
gridRectY->setParentItem(magnifierBox);
|
|
|
|
gridRectX->setZValue(1);
|
|
|
|
gridRectY->setZValue(1);
|
|
|
|
gridRectsX.append(gridRectX);
|
|
|
|
gridRectsY.append(gridRectY);
|
|
|
|
if (i == (pixCnt / 2)) {
|
|
|
|
gridRectX->setBrush(c);
|
|
|
|
gridRectY->setBrush(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::done() {
|
2017-05-09 17:26:00 +02:00
|
|
|
if (rect) {
|
|
|
|
rect->setPen(QPen(Qt::NoPen));
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifier->setVisible(false);
|
|
|
|
magnifierBox->setVisible(false);
|
|
|
|
magnifierHint->setVisible(false);
|
|
|
|
magnifierHintBox->setVisible(false);
|
2017-05-09 17:26:00 +02:00
|
|
|
emit closedWithRect(rect->rect().toRect());
|
|
|
|
}
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|