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.
This commit is contained in:
ArsenArsen 2017-04-29 17:35:42 +02:00
parent 3c92607727
commit a2b973d34e
9 changed files with 148 additions and 40 deletions

View File

@ -38,7 +38,8 @@ SOURCES += main.cpp\
formatter.cpp \ formatter.cpp \
uploaders/customuploader.cpp \ uploaders/customuploader.cpp \
notifications.cpp \ notifications.cpp \
hotkeying.cpp hotkeying.cpp \
cropeditor/drawing/dotitem.cpp
HEADERS += mainwindow.hpp \ HEADERS += mainwindow.hpp \
cropeditor/cropeditor.hpp \ cropeditor/cropeditor.hpp \
@ -56,7 +57,8 @@ HEADERS += mainwindow.hpp \
uploaders/customuploader.hpp \ uploaders/customuploader.hpp \
notifications.hpp \ notifications.hpp \
hotkeying.hpp \ hotkeying.hpp \
cropeditor/drawing/drawitem.hpp cropeditor/drawing/drawitem.hpp \
cropeditor/drawing/dotitem.hpp
FORMS += mainwindow.ui FORMS += mainwindow.ui

View File

@ -1,5 +1,6 @@
#include "cropeditor.hpp" #include "cropeditor.hpp"
#include "cropscene.hpp"
#include "cropview.hpp" #include "cropview.hpp"
#include <QDebug> #include <QDebug>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>

View File

@ -1,10 +1,12 @@
#include "cropscene.hpp" #include "cropscene.hpp"
#include <QColorDialog>
#include <QDebug> #include <QDebug>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsView> #include <QGraphicsView>
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QTimer>
#include <cropeditor/drawing/dotitem.hpp>
CropScene::CropScene(QObject *parent) : QGraphicsScene(parent), prevButtons(Qt::NoButton) 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) 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)
{ {
QPointF p = e->scenePos(); if (drawingSelection != nullptr)
if (rect == nullptr)
{ {
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1); drawingSelection->mouseDragEvent(e, this);
initPos = p;
QPen pen(Qt::NoBrush, 1);
pen.setColor(Qt::cyan);
rect->setPen(pen);
addItem(rect);
} }
else else
{ {
if (prevButtons == Qt::NoButton) QPointF p = e->scenePos();
if (rect == nullptr)
{ {
rect = new QGraphicsRectItem(p.x(), p.y(), 1, 1);
initPos = p; 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 else
{ {
rect->setRect(QRect(qMin(initPos.x(), p.x()), qMin(initPos.y(), p.y()), qAbs(initPos.x() - p.x()), if (prevButtons == Qt::NoButton)
qAbs(initPos.y() - p.y()))); {
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;
QPolygonF poly; QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom());
QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom()); poly << sceneRect().topLeft();
poly << sceneRect().topLeft(); poly << sceneRect().topRight();
poly << sceneRect().topRight(); poly << sceneRect().bottomRight();
poly << sceneRect().bottomRight(); poly << theMagicWikipediaPoint;
poly << theMagicWikipediaPoint; poly << rect->rect().bottomRight();
poly << rect->rect().bottomRight(); poly << rect->rect().topRight();
poly << rect->rect().topRight(); poly << rect->rect().topLeft();
poly << rect->rect().topLeft(); poly << rect->rect().bottomLeft();
poly << rect->rect().bottomLeft(); poly << rect->rect().bottomRight();
poly << rect->rect().bottomRight(); poly << theMagicWikipediaPoint;
poly << theMagicWikipediaPoint; poly << sceneRect().bottomLeft();
poly << sceneRect().bottomLeft(); poly << sceneRect().topLeft();
poly << sceneRect().topLeft();
this->polyItem->setPolygon(poly); this->polyItem->setPolygon(poly);
e->accept(); e->accept();
}
} }
else else
QGraphicsScene::mouseMoveEvent(e); QGraphicsScene::mouseMoveEvent(e);
@ -74,6 +98,11 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{ {
if (drawingSelection != nullptr)
{
drawingSelection->mouseDragEndEvent(e, this);
drawingSelection = nullptr;
}
prevButtons = Qt::NoButton; prevButtons = Qt::NoButton;
QGraphicsScene::mouseReleaseEvent(e); QGraphicsScene::mouseReleaseEvent(e);
} }
@ -83,11 +112,31 @@ void CropScene::keyReleaseEvent(QKeyEvent *event)
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) done(); 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) 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(); e->accept();
} }
@ -99,5 +148,9 @@ void CropScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e)
void CropScene::done() void CropScene::done()
{ {
if (rect) emit closedWithRect(rect->rect().toRect()); if (rect)
{
rect->setPen(QPen(Qt::NoPen));
emit closedWithRect(rect->rect().toRect());
}
} }

View File

@ -7,11 +7,19 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent> #include <QKeyEvent>
class CropScene;
#include <cropeditor/drawing/drawitem.hpp>
class CropScene : public QGraphicsScene class CropScene : public QGraphicsScene
{ {
Q_OBJECT Q_OBJECT
public: public:
CropScene(QObject *parent); CropScene(QObject *parent);
QPen pen();
QBrush brush();
void setDrawingSelection(DrawItem *drawAction);
signals: signals:
void closedWithRect(QRect rect); void closedWithRect(QRect rect);
@ -24,11 +32,15 @@ class CropScene : public QGraphicsScene
void keyReleaseEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override;
private: private:
void addDrawingAction(QMenu &menu, DrawItem *item);
void done(); void done();
QFlags<Qt::MouseButton> prevButtons; QFlags<Qt::MouseButton> prevButtons;
QGraphicsRectItem *rect = nullptr; QGraphicsRectItem *rect = nullptr;
QPointF initPos; QPointF initPos;
QPen _pen;
QBrush _brush;
QGraphicsPolygonItem *polyItem = nullptr; QGraphicsPolygonItem *polyItem = nullptr;
DrawItem *drawingSelection = nullptr;
}; };
#endif // CROPSCENE_HPP #endif // CROPSCENE_HPP

View File

@ -5,7 +5,7 @@ CropView::CropView(QGraphicsScene *scene) : QGraphicsView(scene)
setFrameShape(QFrame::NoFrame); // Time taken to solve: A george99g and 38 minutes. setFrameShape(QFrame::NoFrame); // Time taken to solve: A george99g and 38 minutes.
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint); // setWindowFlags(Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
} }
void CropView::keyPressEvent(QKeyEvent *e) void CropView::keyPressEvent(QKeyEvent *e)

View File

@ -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 *)
{
}

View File

@ -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

View File

@ -7,10 +7,12 @@
class DrawItem class DrawItem
{ {
public: public:
virtual ~DrawItem()
{
}
virtual QString name() = 0; virtual QString name() = 0;
virtual void render(QPixmap *pixmap) = 0; virtual void mouseDragEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) = 0;
virtual void makeItem(CropScene *scene) = 0; virtual void mouseDragEndEvent(QGraphicsSceneMouseEvent *e, CropScene *scene) = 0;
virtual void mouseDragEvent(QGraphicsSceneMoveEvent *e, CropScene *scene) = 0;
}; };
#endif // DRAWITEM_HPP #endif // DRAWITEM_HPP

View File

@ -50,8 +50,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
connect(tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { connect(tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::DoubleClick) toggleVisible(); if (reason == QSystemTrayIcon::DoubleClick) toggleVisible();
}); });
connect(fullscreen, &QAction::triggered, this, [] { screenshotter::area(); }); connect(fullscreen, &QAction::triggered, this, [] { screenshotter::fullscreenDelayed(); });
connect(area, &QAction::triggered, this, [] { screenshotter::area(); }); connect(area, &QAction::triggered, this, [] { screenshotter::areaDelayed(); });
tray->setContextMenu(menu); tray->setContextMenu(menu);
ui->uploaderList->setSelectionBehavior(QAbstractItemView::SelectRows); ui->uploaderList->setSelectionBehavior(QAbstractItemView::SelectRows);