Browse Source

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.
tags/v5.0.0
ArsenArsen 5 years ago
parent
commit
a2b973d34e
  1. 6
      KShare.pro
  2. 1
      cropeditor/cropeditor.cpp
  3. 117
      cropeditor/cropscene.cpp
  4. 12
      cropeditor/cropscene.hpp
  5. 2
      cropeditor/cropview.cpp
  6. 18
      cropeditor/drawing/dotitem.cpp
  7. 20
      cropeditor/drawing/dotitem.hpp
  8. 8
      cropeditor/drawing/drawitem.hpp
  9. 4
      mainwindow.cpp

6
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

1
cropeditor/cropeditor.cpp

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

117
cropeditor/cropscene.cpp

@ -1,10 +1,12 @@
#include "cropscene.hpp"
#include <QColorDialog>
#include <QDebug>
#include <QGraphicsPolygonItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsView>
#include <QMenu>
#include <QTimer>
#include <cropeditor/drawing/dotitem.hpp>
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());
// 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());
// 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());
}
}

12
cropeditor/cropscene.hpp

@ -7,11 +7,19 @@
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
class CropScene;
#include <cropeditor/drawing/drawitem.hpp>
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<Qt::MouseButton> prevButtons;
QGraphicsRectItem *rect = nullptr;
QPointF initPos;
QPen _pen;
QBrush _brush;
QGraphicsPolygonItem *polyItem = nullptr;
DrawItem *drawingSelection = nullptr;
};
#endif // CROPSCENE_HPP

2
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)

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

20
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

8
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

4
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);

Loading…
Cancel
Save