KShare/cropeditor/cropscene.cpp

179 lines
5.6 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)
2017-05-06 13:21:12 +02:00
: QGraphicsScene(parent), prevButtons(Qt::NoButton),
drawingSelectionMaker([] { return nullptr; }),
_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
}
2017-05-06 13:21:12 +02:00
CropScene::~CropScene() { delete drawingSelection; }
2017-05-02 19:49:33 +02:00
2017-05-06 13:21:12 +02:00
QPen &CropScene::pen() { return _pen; }
2017-05-06 13:21:12 +02:00
QBrush &CropScene::brush() { return _brush; }
2017-05-06 13:21:12 +02:00
QFont &CropScene::font() { return _font; }
2017-05-05 23:59:39 +02:00
2017-05-06 13:21:12 +02:00
void CropScene::setDrawingSelection(QString name,
std::function<DrawItem *()> drawAction) {
drawingSelectionMaker = drawAction;
drawingSelection = drawAction();
drawingName = name;
if (drawingSelection)
drawingSelection->init(this);
}
2017-05-05 23:59:39 +02:00
void CropScene::fontAsk() {
2017-05-06 13:21:12 +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-05-06 13:21:12 +02:00
auto buttons = e->buttons();
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) {
if (drawingSelection) {
drawingSelection->mouseDragEvent(e, this);
} 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);
2017-05-05 23:59:39 +02:00
} else {
2017-05-06 13:21:12 +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-06 13:21:12 +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-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +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-06 13:21:12 +02:00
qDebug() << "release";
if (drawingSelection) {
drawingSelection->mouseDragEndEvent(e, this);
delete drawingSelection;
drawingSelection = drawingSelectionMaker();
if (drawingSelection)
drawingSelection->init(this);
}
prevButtons = Qt::NoButton;
2017-04-23 15:05:48 +02:00
}
2017-05-06 13:21:12 +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) {
2017-05-06 13:21:12 +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-06 13:21:12 +02:00
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() {
2017-05-06 13:21:12 +02:00
if (rect) {
rect->setPen(QPen(Qt::NoPen));
emit closedWithRect(rect->rect().toRect());
}
2017-04-23 15:05:48 +02:00
}