From c721ff340c0b727ba3f472b542e7bc2e8b2edefd Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Tue, 2 May 2017 19:49:33 +0200 Subject: [PATCH] Allow pens to persist --- cropeditor/cropscene.cpp | 62 +++++++++++++++++++++------------ cropeditor/cropscene.hpp | 12 +++++-- uploaders/uploadersingleton.cpp | 4 +++ 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/cropeditor/cropscene.cpp b/cropeditor/cropscene.cpp index e539851..e71ef4e 100644 --- a/cropeditor/cropscene.cpp +++ b/cropeditor/cropscene.cpp @@ -11,15 +11,34 @@ #include #include #include +#include #include -CropScene::CropScene(QObject *parent, QPixmap *pixmap) : QGraphicsScene(parent), prevButtons(Qt::NoButton) +CropScene::CropScene(QObject *parent, QPixmap *pixmap) +: QGraphicsScene(parent), prevButtons(Qt::NoButton), drawingSelectionMaker([] { return nullptr; }) { pen().setColor(settings::settings().value("penColor", pen().color()).value()); 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()); + 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; }); + + 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); + _pixmap = pixmap; QTimer::singleShot(0, [&] { QPolygonF poly; @@ -34,6 +53,11 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap) : QGraphicsScene(parent), }); } +CropScene::~CropScene() +{ + delete drawingSelection; +} + QPen &CropScene::pen() { return _pen; @@ -44,10 +68,12 @@ QBrush &CropScene::brush() return _brush; } -void CropScene::setDrawingSelection(DrawItem *drawAction) +void CropScene::setDrawingSelection(QString name, std::function drawAction) { - drawingSelection = drawAction; - drawAction->init(this); + drawingSelectionMaker = drawAction; + drawingSelection = drawAction(); + drawingName = name; + if (drawingSelection) drawingSelection->init(this); } void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) @@ -55,7 +81,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) auto buttons = e->buttons(); if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) { - if (drawingSelection != nullptr) + if (drawingSelection) { drawingSelection->mouseDragEvent(e, this); } @@ -108,38 +134,28 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { - if (drawingSelection != nullptr) + qDebug() << "release"; + if (drawingSelection) { drawingSelection->mouseDragEndEvent(e, this); delete drawingSelection; - drawingSelection = nullptr; + drawingSelection = drawingSelectionMaker(); + if (drawingSelection) drawingSelection->init(this); } prevButtons = Qt::NoButton; } -void CropScene::addDrawingAction(QMenu &menu, DrawItem *item) +void CropScene::addDrawingAction(QMenu &menu, QString name, std::function item) { QAction *action = new QAction; - action->setText(item->name()); - connect(action, &QAction::triggered, [this, item](bool) { setDrawingSelection(item); }); + action->setText(name); + connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); }); menu.addAction(action); } void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) { - QMenu menu(e->widget()); - - addDrawingAction(menu, new DotItem); - addDrawingAction(menu, new PathItem); - addDrawingAction(menu, new BlurItem); - addDrawingAction(menu, new LineItem); - - menu.addSeparator(); - QAction *settings = new QAction; - settings->setText("Settings"); - connect(settings, &QAction::triggered, [&] { BrushPenSelection(this).exec(); }); - menu.addAction(settings); - + display->setText(drawingName); menu.exec(e->screenPos()); e->accept(); } diff --git a/cropeditor/cropscene.hpp b/cropeditor/cropscene.hpp index ded499e..504046d 100644 --- a/cropeditor/cropscene.hpp +++ b/cropeditor/cropscene.hpp @@ -6,7 +6,8 @@ #include #include #include - +#include +#include class CropScene; #include @@ -16,9 +17,10 @@ class CropScene : public QGraphicsScene Q_OBJECT public: CropScene(QObject *parent, QPixmap *pixmap); + ~CropScene(); QPen &pen(); QBrush &brush(); - void setDrawingSelection(DrawItem *drawAction); + void setDrawingSelection(QString name, std::function drawAction); QPixmap *pixmap() { return _pixmap; @@ -35,7 +37,7 @@ class CropScene : public QGraphicsScene void keyReleaseEvent(QKeyEvent *e) override; private: - void addDrawingAction(QMenu &menu, DrawItem *item); + void addDrawingAction(QMenu &menu, QString name, std::function item); void done(); QPixmap *_pixmap; QFlags prevButtons; @@ -44,7 +46,11 @@ class CropScene : public QGraphicsScene QPen _pen; QBrush _brush; QGraphicsPolygonItem *polyItem = nullptr; + std::function drawingSelectionMaker; DrawItem *drawingSelection = nullptr; + QMenu menu; + QString drawingName = "None"; + QAction *display; }; #endif // CROPSCENE_HPP diff --git a/uploaders/uploadersingleton.cpp b/uploaders/uploadersingleton.cpp index 3f22db5..6b5d5ba 100644 --- a/uploaders/uploadersingleton.cpp +++ b/uploaders/uploadersingleton.cpp @@ -45,6 +45,10 @@ UploaderSingleton::UploaderSingleton() : QObject() void UploaderSingleton::registerUploader(Uploader *uploader) { + if (uploaders.contains(uploader->name())) + { + throw std::runtime_error(("Ambigious uploader " + uploader->name()).toStdString()); + } uploaders.insert(uploader->name(), uploader); emit newUploader(uploader); }