Allow pens to persist

This commit is contained in:
ArsenArsen 2017-05-02 19:49:33 +02:00
parent c3f1c6bfaf
commit c721ff340c
3 changed files with 52 additions and 26 deletions

View File

@ -11,15 +11,34 @@
#include <cropeditor/drawing/lineitem.hpp> #include <cropeditor/drawing/lineitem.hpp>
#include <cropeditor/drawing/pathitem.hpp> #include <cropeditor/drawing/pathitem.hpp>
#include <cropeditor/settings/brushpenselection.hpp> #include <cropeditor/settings/brushpenselection.hpp>
#include <functional>
#include <settings.hpp> #include <settings.hpp>
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<QColor>()); pen().setColor(settings::settings().value("penColor", pen().color()).value<QColor>());
pen().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool()); pen().setCosmetic(settings::settings().value("penCosmetic", pen().isCosmetic()).toBool());
pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toFloat()); pen().setWidthF(settings::settings().value("penWidth", pen().widthF()).toFloat());
brush().setColor(settings::settings().value("brushColor", brush().color()).value<QColor>()); 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; });
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; _pixmap = pixmap;
QTimer::singleShot(0, [&] { QTimer::singleShot(0, [&] {
QPolygonF poly; QPolygonF poly;
@ -34,6 +53,11 @@ CropScene::CropScene(QObject *parent, QPixmap *pixmap) : QGraphicsScene(parent),
}); });
} }
CropScene::~CropScene()
{
delete drawingSelection;
}
QPen &CropScene::pen() QPen &CropScene::pen()
{ {
return _pen; return _pen;
@ -44,10 +68,12 @@ QBrush &CropScene::brush()
return _brush; return _brush;
} }
void CropScene::setDrawingSelection(DrawItem *drawAction) void CropScene::setDrawingSelection(QString name, std::function<DrawItem *()> drawAction)
{ {
drawingSelection = drawAction; drawingSelectionMaker = drawAction;
drawAction->init(this); drawingSelection = drawAction();
drawingName = name;
if (drawingSelection) drawingSelection->init(this);
} }
void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
@ -55,7 +81,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
auto buttons = e->buttons(); auto buttons = e->buttons();
if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton)
{ {
if (drawingSelection != nullptr) if (drawingSelection)
{ {
drawingSelection->mouseDragEvent(e, this); drawingSelection->mouseDragEvent(e, this);
} }
@ -108,38 +134,28 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{ {
if (drawingSelection != nullptr) qDebug() << "release";
if (drawingSelection)
{ {
drawingSelection->mouseDragEndEvent(e, this); drawingSelection->mouseDragEndEvent(e, this);
delete drawingSelection; delete drawingSelection;
drawingSelection = nullptr; drawingSelection = drawingSelectionMaker();
if (drawingSelection) drawingSelection->init(this);
} }
prevButtons = Qt::NoButton; prevButtons = Qt::NoButton;
} }
void CropScene::addDrawingAction(QMenu &menu, DrawItem *item) void CropScene::addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item)
{ {
QAction *action = new QAction; QAction *action = new QAction;
action->setText(item->name()); action->setText(name);
connect(action, &QAction::triggered, [this, item](bool) { setDrawingSelection(item); }); connect(action, &QAction::triggered, [this, item, name](bool) { setDrawingSelection(name, item); });
menu.addAction(action); menu.addAction(action);
} }
void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
{ {
QMenu menu(e->widget()); display->setText(drawingName);
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);
menu.exec(e->screenPos()); menu.exec(e->screenPos());
e->accept(); e->accept();
} }

View File

@ -6,7 +6,8 @@
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMenu>
#include <functional>
class CropScene; class CropScene;
#include <cropeditor/drawing/drawitem.hpp> #include <cropeditor/drawing/drawitem.hpp>
@ -16,9 +17,10 @@ class CropScene : public QGraphicsScene
Q_OBJECT Q_OBJECT
public: public:
CropScene(QObject *parent, QPixmap *pixmap); CropScene(QObject *parent, QPixmap *pixmap);
~CropScene();
QPen &pen(); QPen &pen();
QBrush &brush(); QBrush &brush();
void setDrawingSelection(DrawItem *drawAction); void setDrawingSelection(QString name, std::function<DrawItem *()> drawAction);
QPixmap *pixmap() QPixmap *pixmap()
{ {
return _pixmap; return _pixmap;
@ -35,7 +37,7 @@ class CropScene : public QGraphicsScene
void keyReleaseEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override;
private: private:
void addDrawingAction(QMenu &menu, DrawItem *item); void addDrawingAction(QMenu &menu, QString name, std::function<DrawItem *()> item);
void done(); void done();
QPixmap *_pixmap; QPixmap *_pixmap;
QFlags<Qt::MouseButton> prevButtons; QFlags<Qt::MouseButton> prevButtons;
@ -44,7 +46,11 @@ class CropScene : public QGraphicsScene
QPen _pen; QPen _pen;
QBrush _brush; QBrush _brush;
QGraphicsPolygonItem *polyItem = nullptr; QGraphicsPolygonItem *polyItem = nullptr;
std::function<DrawItem *()> drawingSelectionMaker;
DrawItem *drawingSelection = nullptr; DrawItem *drawingSelection = nullptr;
QMenu menu;
QString drawingName = "None";
QAction *display;
}; };
#endif // CROPSCENE_HPP #endif // CROPSCENE_HPP

View File

@ -45,6 +45,10 @@ UploaderSingleton::UploaderSingleton() : QObject()
void UploaderSingleton::registerUploader(Uploader *uploader) void UploaderSingleton::registerUploader(Uploader *uploader)
{ {
if (uploaders.contains(uploader->name()))
{
throw std::runtime_error(("Ambigious uploader " + uploader->name()).toStdString());
}
uploaders.insert(uploader->name(), uploader); uploaders.insert(uploader->name(), uploader);
emit newUploader(uploader); emit newUploader(uploader);
} }