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/pathitem.hpp>
#include <cropeditor/settings/brushpenselection.hpp>
#include <functional>
#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().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; });
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<DrawItem *()> 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<DrawItem *()> 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();
}

View File

@ -6,7 +6,8 @@
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMenu>
#include <functional>
class CropScene;
#include <cropeditor/drawing/drawitem.hpp>
@ -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<DrawItem *()> 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<DrawItem *()> item);
void done();
QPixmap *_pixmap;
QFlags<Qt::MouseButton> prevButtons;
@ -44,7 +46,11 @@ class CropScene : public QGraphicsScene
QPen _pen;
QBrush _brush;
QGraphicsPolygonItem *polyItem = nullptr;
std::function<DrawItem *()> drawingSelectionMaker;
DrawItem *drawingSelection = nullptr;
QMenu menu;
QString drawingName = "None";
QAction *display;
};
#endif // CROPSCENE_HPP

View File

@ -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);
}