2017-04-23 15:05:48 +02:00
|
|
|
#include "cropscene.hpp"
|
2017-07-25 01:33:01 +02:00
|
|
|
#include <QApplication>
|
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-07-24 20:17:10 +02:00
|
|
|
#include <QGraphicsProxyWidget>
|
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-07-24 20:17:10 +02:00
|
|
|
#include <QMenuBar>
|
2017-07-25 01:33:01 +02:00
|
|
|
#include <QScreen>
|
2017-04-26 22:00:13 +02:00
|
|
|
#include <QTimer>
|
2017-06-29 16:28:26 +02:00
|
|
|
#include <cropeditor/drawing/arrowitem.hpp>
|
2017-05-01 11:28:54 +02:00
|
|
|
#include <cropeditor/drawing/bluritem.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-07-02 20:51:15 +02:00
|
|
|
CropScene::CropScene(QObject *parent, QPixmap pixmap)
|
2017-07-08 21:40:09 +02:00
|
|
|
: QGraphicsScene(parent), cursorPos(0, 0), drawingSelectionMaker([] { return nullptr; }), prevButtons(Qt::NoButton),
|
2017-06-28 15:56:59 +02:00
|
|
|
_brush(Qt::SolidPattern), _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());
|
2017-07-24 20:17:10 +02:00
|
|
|
pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toReal());
|
2017-05-09 17:26:00 +02:00
|
|
|
brush().setColor(settings::settings().value("brushColor", brush().color()).value<QColor>());
|
2017-07-24 20:17:10 +02:00
|
|
|
brush().setStyle(
|
|
|
|
static_cast<Qt::BrushStyle>(settings::settings().value("brushStyle", static_cast<int>(Qt::SolidPattern)).toInt()));
|
2017-08-05 21:20:24 +02:00
|
|
|
_highlight = settings::settings().value("highlightColor", QColor(Qt::cyan)).value<QColor>();
|
2017-05-09 17:26:00 +02:00
|
|
|
|
2017-07-24 20:17:10 +02:00
|
|
|
menu = new QMenuBar;
|
2017-07-29 17:22:17 +02:00
|
|
|
addDrawingAction(menu, tr("Free draw"), ":/icons/pencil.png", [] { return new PathItem; });
|
|
|
|
addDrawingAction(menu, tr("Blur"), ":/icons/blur.png", [] { return new BlurItem; });
|
|
|
|
addDrawingAction(menu, tr("Straight line"), ":/icons/line.png", [] { return new LineItem; });
|
|
|
|
addDrawingAction(menu, tr("Text"), ":/icons/text.png", [] { return new TextItem; });
|
|
|
|
addDrawingAction(menu, tr("Rectangle"), ":/icons/rectangle.png", [] { return new RectItem; });
|
|
|
|
addDrawingAction(menu, tr("Ellipse"), ":/icons/circle.png", [] { return new EllipseItem; });
|
|
|
|
addDrawingAction(menu, tr("Arrow"), ":/icons/arrow.png", [] { return new ArrowItem; });
|
2017-06-26 15:43:12 +02:00
|
|
|
|
2017-07-24 20:17:10 +02:00
|
|
|
menu->addSeparator();
|
2017-07-29 17:22:17 +02:00
|
|
|
addDrawingAction(menu, tr("Eraser"), ":/icons/erase.png", [] { return new EraserItem; });
|
2017-07-25 01:16:56 +02:00
|
|
|
QAction *clear = menu->addAction("");
|
2017-07-29 17:22:17 +02:00
|
|
|
clear->setToolTip(tr("Clear all drawing"));
|
2017-07-26 22:19:44 +02:00
|
|
|
clear->setIcon(QIcon(":/icons/delete.png"));
|
2017-06-26 15:43:12 +02:00
|
|
|
connect(clear, &QAction::triggered, [&] {
|
|
|
|
auto its = items();
|
|
|
|
for (auto i : its) {
|
2017-07-24 20:17:10 +02:00
|
|
|
if (i != polyItem && i != rect && i != cursorItem && i->zValue() != -1 && i != proxyMenu && i != hint
|
|
|
|
&& i != magnifier && i != magnifierBox && i != magnifierHint && i != magnifierHintBox
|
|
|
|
&& gridRectsX.contains(dynamic_cast<QGraphicsRectItem *>(i))
|
|
|
|
&& gridRectsY.contains(dynamic_cast<QGraphicsRectItem *>(i))) {
|
|
|
|
removeItem(i);
|
|
|
|
}
|
2017-06-26 15:43:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-19 00:29:08 +02:00
|
|
|
addDrawingAction(menu, tr("None"), ":/icons/crop.png", [] { return nullptr; });
|
2017-06-26 16:28:45 +02:00
|
|
|
|
2017-07-24 20:17:10 +02:00
|
|
|
menu->addSeparator();
|
2017-07-25 10:50:57 +02:00
|
|
|
QAction *settings = menu->addAction("");
|
2017-07-29 17:22:17 +02:00
|
|
|
settings->setToolTip(tr("Settings"));
|
2017-07-26 22:19:44 +02:00
|
|
|
settings->setIcon(QIcon(":/icons/settings.png"));
|
2017-07-25 10:50:57 +02:00
|
|
|
connect(settings, &QAction::triggered, [&] {
|
2017-06-29 19:09:39 +02:00
|
|
|
hide();
|
|
|
|
BrushPenSelection(this).exec();
|
|
|
|
show();
|
|
|
|
});
|
2017-07-25 10:50:57 +02:00
|
|
|
menu->addSeparator();
|
|
|
|
display = menu->addAction(drawingName);
|
|
|
|
display->setDisabled(true);
|
2017-07-25 01:16:56 +02:00
|
|
|
|
2017-07-25 10:50:57 +02:00
|
|
|
QAction *fonts = menu->addAction("");
|
2017-07-26 22:19:44 +02:00
|
|
|
fonts->setIcon(QIcon(":/icons/fontsettings.png"));
|
2017-07-25 10:50:57 +02:00
|
|
|
connect(fonts, &QAction::triggered, this, &CropScene::fontAsk);
|
2017-07-25 01:16:56 +02:00
|
|
|
|
2017-07-25 10:50:57 +02:00
|
|
|
menu->addAction(fonts);
|
2017-07-25 01:16:56 +02:00
|
|
|
menu->addSeparator();
|
2017-07-25 10:50:57 +02:00
|
|
|
QAction *confirm = menu->addAction("");
|
2017-07-29 17:22:17 +02:00
|
|
|
confirm->setToolTip(tr("Confirm"));
|
2017-07-26 22:19:44 +02:00
|
|
|
confirm->setIcon(QIcon(":/icons/accept.png"));
|
2017-07-25 10:50:57 +02:00
|
|
|
connect(confirm, &QAction::triggered, [this] { done(true); });
|
|
|
|
menu->addAction(confirm);
|
2017-07-25 01:16:56 +02:00
|
|
|
|
2017-07-25 10:50:57 +02:00
|
|
|
QAction *cancel = menu->addAction("");
|
2017-07-29 17:22:17 +02:00
|
|
|
cancel->setToolTip(tr("Cancel"));
|
2017-07-26 22:19:44 +02:00
|
|
|
cancel->setIcon(QIcon(":/icons/cancel.png"));
|
2017-07-25 10:50:57 +02:00
|
|
|
connect(cancel, &QAction::triggered, [this] { done(false); });
|
|
|
|
menu->addAction(cancel);
|
2017-07-25 01:16:56 +02:00
|
|
|
|
2017-07-08 21:40:09 +02:00
|
|
|
QPolygonF cursorPoly;
|
|
|
|
cursorPoly << QPoint(-10, 0) //
|
|
|
|
<< QPoint(10, 0) //
|
|
|
|
<< QPoint(0, 0) //
|
|
|
|
<< QPoint(0, 10) //
|
|
|
|
<< QPoint(0, -10) //
|
|
|
|
<< QPoint(0, 0);
|
|
|
|
|
|
|
|
cursorItem = addPolygon(cursorPoly, QPen(Qt::white));
|
|
|
|
cursorItem->setZValue(3);
|
|
|
|
|
|
|
|
magnifier = addPixmap(QPixmap(110, 110));
|
2017-08-05 21:20:24 +02:00
|
|
|
magnifierBox = addRect(magnifier->boundingRect(), QPen(_highlight));
|
2017-07-08 21:40:09 +02:00
|
|
|
magnifier->setZValue(3);
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifierBox->setZValue(1.1);
|
|
|
|
magnifierBox->setParentItem(magnifier);
|
2017-07-08 21:40:09 +02:00
|
|
|
magnifierHint = addText("ptr: (0, 0)\nsel: (-1, -1, 0, 0)");
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifierHint->setParentItem(magnifier);
|
|
|
|
magnifierHint->setY(magnifier->boundingRect().height());
|
2017-08-05 21:20:24 +02:00
|
|
|
QColor c(_highlight);
|
2017-06-27 00:02:27 +02:00
|
|
|
c.setAlphaF(.25);
|
|
|
|
magnifierHintBox = addRect(magnifierHint->boundingRect(), Qt::NoPen, c);
|
|
|
|
magnifierHintBox->setParentItem(magnifierHint);
|
|
|
|
magnifierHintBox->setZValue(1);
|
|
|
|
magnifierHint->setZValue(1.1);
|
2017-07-08 21:40:09 +02:00
|
|
|
updateMag();
|
2017-06-27 00:02:27 +02:00
|
|
|
|
2017-07-09 21:04:21 +02:00
|
|
|
addItem(hint);
|
|
|
|
hint->setPos(5, 5);
|
|
|
|
hint->setZValue(2);
|
|
|
|
hint->setVisible(settings::settings().value("crophint", true).toBool());
|
2017-05-09 17:26:00 +02:00
|
|
|
|
2017-07-08 21:40:09 +02:00
|
|
|
QPolygonF poly;
|
|
|
|
QRect prect = pixmap.rect();
|
|
|
|
poly.append(prect.topLeft());
|
|
|
|
poly.append(prect.topRight());
|
|
|
|
poly.append(prect.bottomRight());
|
|
|
|
poly.append(prect.bottomLeft());
|
|
|
|
polyItem = new QGraphicsPolygonItem(poly);
|
|
|
|
polyItem->setBrush(QBrush(QColor(0, 0, 0, 191)));
|
|
|
|
polyItem->setPen(QPen(Qt::NoPen));
|
2017-07-09 14:23:52 +02:00
|
|
|
polyItem->setZValue(1);
|
2017-07-08 21:40:09 +02:00
|
|
|
addItem(polyItem);
|
2017-07-24 20:17:10 +02:00
|
|
|
|
|
|
|
auto widget = addWidget(menu);
|
2017-07-25 17:26:57 +02:00
|
|
|
widget->setFlag(QGraphicsItem::ItemIsMovable, true);
|
2017-07-24 20:17:10 +02:00
|
|
|
widget->setZValue(100);
|
|
|
|
widget->setPos(100, 100);
|
|
|
|
proxyMenu = widget;
|
|
|
|
|
2017-07-25 01:33:01 +02:00
|
|
|
QTimer::singleShot(0, [&, widget] {
|
2017-07-08 21:40:09 +02:00
|
|
|
auto pf = views()[0]->mapFromGlobal(QCursor::pos());
|
|
|
|
cursorPos = QPoint(pf.x(), pf.y());
|
|
|
|
cursorItem->setPos(cursorPos);
|
|
|
|
updateMag();
|
2017-07-26 23:29:26 +02:00
|
|
|
auto screen = QApplication::primaryScreen();
|
|
|
|
int w = screen->geometry().width();
|
2017-07-30 00:49:15 +02:00
|
|
|
widget->setPos(views()[0]->mapToScene(
|
|
|
|
QPoint(screen->geometry().x() + (w - widget->boundingRect().width()) / 2, screen->geometry().y() + 100)));
|
2017-08-05 21:20:24 +02:00
|
|
|
setGrid(settings::settings().value("gridEnabled", true).toBool());
|
2017-05-09 17:26:00 +02:00
|
|
|
});
|
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-08-05 21:20:24 +02:00
|
|
|
void CropScene::setHighlight(QColor highlight) {
|
|
|
|
_highlight = highlight;
|
|
|
|
QColor c = highlight;
|
|
|
|
c.setAlphaF(.4);
|
|
|
|
magnifierHintBox->setBrush(c);
|
|
|
|
if (grid()) setGrid(true);
|
|
|
|
if (rect) rect->setPen(highlight);
|
|
|
|
int i = settings::settings().value("magnifierPixelCount", 11).toInt() / 2;
|
|
|
|
if (gridRectsX.isEmpty() || gridRectsY.isEmpty()) return;
|
|
|
|
gridRectsX[i]->setBrush(c);
|
|
|
|
gridRectsY[i]->setBrush(c);
|
|
|
|
}
|
|
|
|
|
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;
|
2017-07-25 01:16:56 +02:00
|
|
|
display->setText(drawingName);
|
2017-05-09 17:26:00 +02:00
|
|
|
if (drawingSelection)
|
|
|
|
if (!drawingSelection->init(this)) setDrawingSelection("None", [] { return nullptr; });
|
2017-04-29 17:35:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-12 00:59:38 +02:00
|
|
|
QGraphicsItem *CropScene::whichItem(QPointF scenePos) {
|
|
|
|
for (auto item : items()) {
|
|
|
|
if (item->sceneBoundingRect().contains(scenePos))
|
2017-07-25 01:16:56 +02:00
|
|
|
if (item != polyItem && item != rect && item != cursorItem && item->zValue() != -1) return item;
|
2017-07-12 00:59:38 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:09:39 +02:00
|
|
|
void CropScene::hide() {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::show() {
|
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::setVisible(bool visible) {
|
|
|
|
for (auto view : views()) {
|
|
|
|
view->setVisible(visible);
|
2017-07-11 16:58:05 +02:00
|
|
|
if (visible) {
|
2017-08-03 22:00:15 +02:00
|
|
|
if (QApplication::screens().size() == 1) view->showFullScreen();
|
2017-07-11 16:58:05 +02:00
|
|
|
QPoint p = screenshotutil::smallestScreenCoordinate() + QPoint(settings::settings().value("cropx", 0).toInt(),
|
|
|
|
settings::settings().value("cropy", 0).toInt());
|
|
|
|
view->move(p.x(), p.y());
|
2017-07-29 17:22:17 +02:00
|
|
|
view->setWindowTitle(tr("KShare Crop Editor"));
|
2017-07-11 16:58:05 +02:00
|
|
|
view->activateWindow();
|
|
|
|
}
|
2017-06-29 19:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::fontAsk() {
|
2017-06-29 19:09:39 +02:00
|
|
|
hide();
|
2017-05-09 17:26:00 +02:00
|
|
|
bool ok = false;
|
2017-07-05 19:24:53 +02:00
|
|
|
QFont font = QFontDialog::getFont(&ok, this->font(), this->views()[0], "Font to use");
|
2017-05-09 17:26:00 +02:00
|
|
|
if (ok) _font = font;
|
2017-06-29 19:09:39 +02:00
|
|
|
show();
|
2017-05-05 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
|
2017-07-08 21:40:09 +02:00
|
|
|
QPointF delta = e->scenePos() - cursorPos;
|
|
|
|
if (e->modifiers() & Qt::ShiftModifier) {
|
|
|
|
cursorPos += delta / 2;
|
|
|
|
QCursor::setPos(views()[0]->mapToGlobal(cursorPos.toPoint()));
|
|
|
|
} else
|
|
|
|
cursorPos = e->scenePos();
|
2017-08-05 21:20:24 +02:00
|
|
|
hint->setVisible(settings::settings().value("crophint").toBool() && !hint->sceneBoundingRect().contains(cursorPos));
|
2017-07-08 21:40:09 +02:00
|
|
|
cursorItem->setPos(cursorPos);
|
|
|
|
updateMag();
|
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) {
|
2017-07-12 00:59:38 +02:00
|
|
|
auto item = whichItem(cursorPos);
|
|
|
|
if (item) item->moveBy(delta.x(), delta.y());
|
2017-06-26 17:58:36 +02:00
|
|
|
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-07-08 21:40:09 +02:00
|
|
|
QPointF p = cursorPos;
|
2017-05-09 17:26:00 +02:00
|
|
|
if (rect == nullptr) {
|
|
|
|
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
|
|
|
|
initPos = p;
|
|
|
|
QPen pen(Qt::NoBrush, 1);
|
2017-08-05 21:20:24 +02:00
|
|
|
pen.setColor(_highlight);
|
2017-05-09 17:26:00 +02:00
|
|
|
rect->setPen(pen);
|
2017-07-08 21:40:09 +02:00
|
|
|
rect->setZValue(1);
|
2017-05-09 17:26:00 +02:00
|
|
|
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-07-24 20:17:10 +02:00
|
|
|
|
|
|
|
QGraphicsScene::mouseMoveEvent(e);
|
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())
|
2017-07-03 15:43:28 +02:00
|
|
|
done(true);
|
2017-05-09 17:26:00 +02:00
|
|
|
prevButtons = Qt::NoButton;
|
2017-07-24 20:17:10 +02:00
|
|
|
|
2017-07-25 17:26:57 +02:00
|
|
|
if (!(e->modifiers() & Qt::ControlModifier)) QGraphicsScene::mouseReleaseEvent(e);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 22:26:18 +02:00
|
|
|
void CropScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
|
|
|
|
if (e->modifiers() & Qt::AltModifier) {
|
2017-07-12 00:59:38 +02:00
|
|
|
auto item = whichItem(cursorItem->scenePos());
|
2017-07-25 01:16:56 +02:00
|
|
|
if (item && item != proxyMenu) removeItem(item);
|
2017-06-28 22:26:18 +02:00
|
|
|
}
|
2017-07-24 20:17:10 +02:00
|
|
|
|
2017-07-25 17:26:57 +02:00
|
|
|
if (!(e->modifiers() & Qt::ControlModifier)) QGraphicsScene::mousePressEvent(e);
|
2017-06-28 22:26:18 +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();
|
|
|
|
|
2017-08-05 21:20:24 +02:00
|
|
|
if (grid()) initMagnifierGrid();
|
2017-07-08 21:40:09 +02:00
|
|
|
updateMag();
|
2017-06-27 00:27:04 +02:00
|
|
|
|
2017-07-25 17:26:57 +02:00
|
|
|
if (!(event->modifiers() & Qt::ControlModifier)) QGraphicsScene::wheelEvent(event);
|
2017-04-29 17:35:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 01:16:56 +02:00
|
|
|
void CropScene::addDrawingAction(QMenuBar *menu, QString name, QString icon, std::function<DrawItem *()> item) {
|
|
|
|
QAction *action = menu->addAction("");
|
|
|
|
action->setToolTip(name);
|
|
|
|
action->setIcon(QIcon(icon));
|
|
|
|
connect(action, &QAction::triggered, [this, menu, action, item, name](bool) { setDrawingSelection(name, item); });
|
2017-04-29 12:08:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:59:39 +02:00
|
|
|
void CropScene::keyReleaseEvent(QKeyEvent *event) {
|
2017-07-08 21:40:09 +02:00
|
|
|
if (((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !drawingSelection) || event->key() == Qt::Key_Escape)
|
2017-07-03 15:43:28 +02:00
|
|
|
done(event->key() != Qt::Key_Escape);
|
2017-07-09 21:04:21 +02:00
|
|
|
else if (event->key() == Qt::Key_F1) {
|
|
|
|
bool enabled = !settings::settings().value("crophint", true).toBool();
|
|
|
|
hint->setVisible(enabled);
|
|
|
|
settings::settings().setValue("crophint", enabled);
|
|
|
|
}
|
2017-07-25 10:21:31 +02:00
|
|
|
|
2017-07-25 17:26:57 +02:00
|
|
|
if (!(event->modifiers() & Qt::ControlModifier)) QGraphicsScene::keyReleaseEvent(event);
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 21:40:09 +02:00
|
|
|
void CropScene::updateMag() {
|
|
|
|
QString rectStr("(-1, -1, 0, 0)");
|
2017-06-27 00:27:04 +02:00
|
|
|
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());
|
|
|
|
}
|
2017-07-08 21:40:09 +02:00
|
|
|
magnifierHint->setPlainText(QString("ptr: (%0, %1)\nsel: %2").arg(qRound(cursorPos.x())).arg(qRound(cursorPos.y())).arg(rectStr));
|
2017-06-27 00:27:04 +02:00
|
|
|
magnifierHintBox->setRect(magnifierHint->boundingRect());
|
|
|
|
|
|
|
|
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
|
|
|
|
if (pixCnt % 2 == 0) pixCnt++;
|
2017-07-08 21:40:09 +02:00
|
|
|
QPointF magnifierTopLeft = cursorPos - QPointF(pixCnt / 2., pixCnt / 2.);
|
|
|
|
QPointF magnifierPos = cursorPos + QPointF(5, 5);
|
2017-06-27 00:27:04 +02:00
|
|
|
|
|
|
|
magnifier->setPos(magnifierPos);
|
2017-07-02 20:51:15 +02:00
|
|
|
magnifier->setPixmap(_pixmap.copy(magnifierTopLeft.x(), magnifierTopLeft.y(), pixCnt, pixCnt).scaled(110, 110));
|
2017-06-27 00:27:04 +02:00
|
|
|
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() {
|
2017-08-05 21:20:24 +02:00
|
|
|
if (!gridRectsX.isEmpty() || !gridRectsY.isEmpty()) return;
|
|
|
|
|
|
|
|
QColor c(_highlight);
|
2017-06-27 00:27:04 +02:00
|
|
|
c.setAlphaF(.25);
|
|
|
|
int pixCnt = settings::settings().value("magnifierPixelCount", 11).toInt();
|
|
|
|
if (pixCnt % 2 == 0) pixCnt++;
|
|
|
|
for (int i = 0; i < pixCnt; i++) {
|
2017-06-28 22:31:12 +02:00
|
|
|
auto gridRectX = addRect(0, i * 110. / pixCnt, 110, 110. / pixCnt, QPen(Qt::black, 0.5));
|
|
|
|
auto gridRectY = addRect(i * 110. / pixCnt, 0, 110. / pixCnt, 110, QPen(Qt::black, 0.5));
|
2017-06-27 00:27:04 +02:00
|
|
|
gridRectX->setParentItem(magnifierBox);
|
|
|
|
gridRectY->setParentItem(magnifierBox);
|
2017-08-05 21:20:24 +02:00
|
|
|
gridRectX->setZValue(5);
|
|
|
|
gridRectY->setZValue(5);
|
2017-06-27 00:27:04 +02:00
|
|
|
gridRectsX.append(gridRectX);
|
|
|
|
gridRectsY.append(gridRectY);
|
|
|
|
if (i == (pixCnt / 2)) {
|
|
|
|
gridRectX->setBrush(c);
|
|
|
|
gridRectY->setBrush(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 15:43:28 +02:00
|
|
|
void CropScene::done(bool notEsc) {
|
|
|
|
if (notEsc && rect) {
|
2017-07-09 21:04:21 +02:00
|
|
|
hint->setVisible(false);
|
2017-05-09 17:26:00 +02:00
|
|
|
rect->setPen(QPen(Qt::NoPen));
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifier->setVisible(false);
|
2017-07-24 20:19:30 +02:00
|
|
|
proxyMenu->setVisible(false);
|
2017-07-08 21:40:09 +02:00
|
|
|
cursorItem->setVisible(false);
|
2017-06-27 00:02:27 +02:00
|
|
|
magnifierBox->setVisible(false);
|
|
|
|
magnifierHint->setVisible(false);
|
|
|
|
magnifierHintBox->setVisible(false);
|
2017-05-09 17:26:00 +02:00
|
|
|
emit closedWithRect(rect->rect().toRect());
|
2017-07-02 20:51:15 +02:00
|
|
|
} else
|
|
|
|
emit closedWithRect(QRect());
|
2017-04-23 15:05:48 +02:00
|
|
|
}
|