KShare/cropeditor/cropscene.cpp

172 lines
6.0 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "cropscene.hpp"
#include <QColorDialog>
2017-04-23 15:05:48 +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>
#include <cropeditor/drawing/dotitem.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-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)
: QGraphicsScene(parent), drawingSelectionMaker([] { return nullptr; }), prevButtons(Qt::NoButton),
_font(settings::settings().value("font", QFont()).value<QFont>()) {
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; });
QAction *reset = menu.addAction("Reset");
connect(reset, &QAction::triggered, [&] { setDrawingSelection("None", [] { return nullptr; }); });
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);
connect(menu.addAction("Set Font"), &QAction::triggered, this, &CropScene::fontAsk);
_pixmap = pixmap;
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
}
CropScene::~CropScene() {
delete drawingSelection;
}
2017-05-02 19:49:33 +02:00
QPen &CropScene::pen() {
return _pen;
}
QBrush &CropScene::brush() {
return _brush;
}
QFont &CropScene::font() {
return _font;
}
2017-05-05 23:59:39 +02:00
void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> drawAction) {
2017-05-11 14:49:00 +02:00
this->setFocus();
drawingSelectionMaker = drawAction;
drawingSelection = drawAction();
drawingName = name;
if (drawingSelection)
if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; });
}
2017-05-05 23:59:39 +02:00
void CropScene::fontAsk() {
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) {
auto buttons = e->buttons();
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) {
if (drawingSelection) {
drawingSelection->mouseDragEvent(e, this);
2017-05-05 23:59:39 +02:00
} else {
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 {
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), qAbs(initPos.y() - p.y())));
}
}
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-23 15:05:48 +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) {
qDebug() << "release";
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();
prevButtons = Qt::NoButton;
2017-04-23 15:05:48 +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-05-05 23:59:39 +02:00
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) {
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) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); // Segfault
2017-04-23 15:05:48 +02:00
}
2017-05-05 23:59:39 +02:00
void CropScene::done() {
if (rect) {
rect->setPen(QPen(Qt::NoPen));
emit closedWithRect(rect->rect().toRect());
}
2017-04-23 15:05:48 +02:00
}