From a2b973d34e28675a35a10a528313a3abe23a4159 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Sat, 29 Apr 2017 17:35:42 +0200 Subject: [PATCH] This commit contains fixes and a broken attempt to make drawing. Segfaults are real on this one. Basically, when I call this virtual method on a field which containts a pointer to a derived class from a pure virtual one the program segfaults. Please help. --- KShare.pro | 6 +- cropeditor/cropeditor.cpp | 1 + cropeditor/cropscene.cpp | 117 +++++++++++++++++++++++--------- cropeditor/cropscene.hpp | 12 ++++ cropeditor/cropview.cpp | 2 +- cropeditor/drawing/dotitem.cpp | 18 +++++ cropeditor/drawing/dotitem.hpp | 20 ++++++ cropeditor/drawing/drawitem.hpp | 8 ++- mainwindow.cpp | 4 +- 9 files changed, 148 insertions(+), 40 deletions(-) create mode 100644 cropeditor/drawing/dotitem.cpp create mode 100644 cropeditor/drawing/dotitem.hpp diff --git a/KShare.pro b/KShare.pro index 9db0ea3..43373a7 100644 --- a/KShare.pro +++ b/KShare.pro @@ -38,7 +38,8 @@ SOURCES += main.cpp\ formatter.cpp \ uploaders/customuploader.cpp \ notifications.cpp \ - hotkeying.cpp + hotkeying.cpp \ + cropeditor/drawing/dotitem.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -56,7 +57,8 @@ HEADERS += mainwindow.hpp \ uploaders/customuploader.hpp \ notifications.hpp \ hotkeying.hpp \ - cropeditor/drawing/drawitem.hpp + cropeditor/drawing/drawitem.hpp \ + cropeditor/drawing/dotitem.hpp FORMS += mainwindow.ui diff --git a/cropeditor/cropeditor.cpp b/cropeditor/cropeditor.cpp index 9273280..2f6a8f6 100644 --- a/cropeditor/cropeditor.cpp +++ b/cropeditor/cropeditor.cpp @@ -1,5 +1,6 @@ #include "cropeditor.hpp" +#include "cropscene.hpp" #include "cropview.hpp" #include #include diff --git a/cropeditor/cropscene.cpp b/cropeditor/cropscene.cpp index 9f1316a..98e090a 100644 --- a/cropeditor/cropscene.cpp +++ b/cropeditor/cropscene.cpp @@ -1,10 +1,12 @@ #include "cropscene.hpp" +#include #include #include #include #include #include #include +#include CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt::NoButton) { @@ -21,51 +23,73 @@ CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt:: }); } +QPen CropScene::pen() +{ + return _pen; +} + +QBrush CropScene::brush() +{ + return _brush; +} + +void CropScene::setDrawingSelection(DrawItem *drawAction) +{ + drawingSelection = drawAction; +} + void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) { auto buttons = e->buttons(); if (buttons == Qt::LeftButton || prevButtons == Qt::NoButton) { - QPointF p = e->scenePos(); - if (rect == nullptr) + if (drawingSelection != 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); + drawingSelection->mouseDragEvent(e, this); } else { - if (prevButtons == Qt::NoButton) + QPointF p = e->scenePos(); + if (rect == nullptr) { + rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1); initPos = p; - rect->setRect(p.x(), p.y(), 1, 1); + QPen pen(Qt::NoBrush, 1); + pen.setColor(Qt::cyan); + rect->setPen(pen); + addItem(rect); } else { - rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), - qAbs(initPos.y() - p.y()))); + if (prevButtons == Qt::NoButton) + { + initPos = p; + rect->setRect(p.x(), p.y(), 1, 1); + } + else + { + rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), + qAbs(initPos.y() - p.y()))); + } } - } - 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(); + 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(); + this->polyItem->setPolygon(poly); + e->accept(); + } } else QGraphicsScene::mouseMoveEvent(e); @@ -74,6 +98,11 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { + if (drawingSelection != nullptr) + { + drawingSelection->mouseDragEndEvent(e, this); + drawingSelection = nullptr; + } prevButtons = Qt::NoButton; QGraphicsScene::mouseReleaseEvent(e); } @@ -83,11 +112,31 @@ void CropScene::keyReleaseEvent(QKeyEvent *event) if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); } +void CropScene::addDrawingAction(QMenu &menu, DrawItem *item) +{ + QAction *action = new QAction; + action->setText(item->name()); + QObject::connect(action, &QAction::triggered, + [&](bool) { QTimer::singleShot(0, [&] { setDrawingSelection(item); }); }); + menu.addAction(action); +} + void CropScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) { - QMenu menu(e->widget()); + // QMenu menu(e->widget()); - menu.exec(e->screenPos()); + // addDrawingAction(menu, new DotItem); + + // menu.addSeparator(); + // QAction *bColorAction = new QAction; + // bColorAction->setText("Select brush color"); + // connect(bColorAction, &QAction::triggered, [&] { _brush.setColor(QColorDialog::getColor(_brush.color())); }); + // QAction *pColorAction = new QAction; + // pColorAction->setText("Select pen color"); + // connect(pColorAction, &QAction::triggered, [&] { _pen.setColor(QColorDialog::getColor(_pen.color())); }); + // menu.addActions({ pColorAction, bColorAction }); + + // menu.exec(e->screenPos()); e->accept(); } @@ -99,5 +148,9 @@ void CropScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) void CropScene::done() { - if (rect) emit closedWithRect(rect->rect().toRect()); + if (rect) + { + rect->setPen(QPen(Qt::NoPen)); + emit closedWithRect(rect->rect().toRect()); + } } diff --git a/cropeditor/cropscene.hpp b/cropeditor/cropscene.hpp index e1060ae..58bf3ff 100644 --- a/cropeditor/cropscene.hpp +++ b/cropeditor/cropscene.hpp @@ -7,11 +7,19 @@ #include #include +class CropScene; + +#include + class CropScene : public QGraphicsScene { Q_OBJECT public: CropScene(QObject *parent); + QPen pen(); + QBrush brush(); + void setDrawingSelection(DrawItem *drawAction); + signals: void closedWithRect(QRect rect); @@ -24,11 +32,15 @@ class CropScene : public QGraphicsScene void keyReleaseEvent(QKeyEvent *e) override; private: + void addDrawingAction(QMenu &menu, DrawItem *item); void done(); QFlags prevButtons; QGraphicsRectItem *rect = nullptr; QPointF initPos; + QPen _pen; + QBrush _brush; QGraphicsPolygonItem *polyItem = nullptr; + DrawItem *drawingSelection = nullptr; }; #endif // CROPSCENE_HPP diff --git a/cropeditor/cropview.cpp b/cropeditor/cropview.cpp index 5f383bd..e01a12e 100644 --- a/cropeditor/cropview.cpp +++ b/cropeditor/cropview.cpp @@ -5,7 +5,7 @@ CropView::CropView(QGraphicsScene *scene) : QGraphicsView(scene) setFrameShape(QFrame::NoFrame); // Time taken to solve: A george99g and 38 minutes. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setWindowFlags(Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint); + // setWindowFlags(Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint); } void CropView::keyPressEvent(QKeyEvent *e) diff --git a/cropeditor/drawing/dotitem.cpp b/cropeditor/drawing/dotitem.cpp new file mode 100644 index 0000000..dbf8693 --- /dev/null +++ b/cropeditor/drawing/dotitem.cpp @@ -0,0 +1,18 @@ +#include "dotitem.hpp" + +DotItem::DotItem() +{ +} + +DotItem::~DotItem() +{ +} + +void DotItem::mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) +{ + scene->addEllipse(e->pos().x() - 1.5, e->pos().y() - 1.5, 3, 3, scene->pen(), scene->brush()); +} + +void DotItem::mouseDragEndEvent(QGraphicsSceneMouseEvent *, CropScene *) +{ +} diff --git a/cropeditor/drawing/dotitem.hpp b/cropeditor/drawing/dotitem.hpp new file mode 100644 index 0000000..e95b4ad --- /dev/null +++ b/cropeditor/drawing/dotitem.hpp @@ -0,0 +1,20 @@ +#ifndef DOTITEM_HPP +#define DOTITEM_HPP + +#include "../cropscene.hpp" +#include "drawitem.hpp" + +class DotItem : public DrawItem +{ + public: + DotItem(); + ~DotItem(); + QString name() + { + return "Dots (drag to add)"; + } + void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene); + void mouseDragEndEvent(QGraphicsSceneMouseEvent *e, CropScene *scene); +}; + +#endif // DOTITEM_HPP diff --git a/cropeditor/drawing/drawitem.hpp b/cropeditor/drawing/drawitem.hpp index 101d31d..114e59a 100644 --- a/cropeditor/drawing/drawitem.hpp +++ b/cropeditor/drawing/drawitem.hpp @@ -7,10 +7,12 @@ class DrawItem { public: + virtual ~DrawItem() + { + } virtual QString name() = 0; - virtual void render(QPixmap *pixmap) = 0; - virtual void makeItem(CropScene *scene) = 0; - virtual void mouseDragEvent(QGraphicsSceneMoveEvent *e, CropScene *scene) = 0; + virtual void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) = 0; + virtual void mouseDragEndEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) = 0; }; #endif // DRAWITEM_HPP diff --git a/mainwindow.cpp b/mainwindow.cpp index 4a09142..ddf34c8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -50,8 +50,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi connect(tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::DoubleClick) toggleVisible(); }); - connect(fullscreen, &QAction::triggered, this, [] { screenshotter::area(); }); - connect(area, &QAction::triggered, this, [] { screenshotter::area(); }); + connect(fullscreen, &QAction::triggered, this, [] { screenshotter::fullscreenDelayed(); }); + connect(area, &QAction::triggered, this, [] { screenshotter::areaDelayed(); }); tray->setContextMenu(menu); ui->uploaderList->setSelectionBehavior(QAbstractItemView::SelectRows);