Commit af27decc authored by roangel's avatar roangel
Browse files

Added support for drag and drop way of creating rectangles

parent da168dca
......@@ -55,12 +55,15 @@ void MainGUIWindow::_refresh_tabs()
void MainGUIWindow::_init()
{
scene = new myGraphicsScene(this);
scene = new myGraphicsScene(ui->frame_drawing);
//scene->setSceneRect(QRectF(QPointF(-100, 100), QSizeF(200, 200)));
ui->graphicsView->setScene(scene);
QRect rect(10, 20, 80, 60);
item1 = new QGraphicsRectItem(0,0,100,100);
item1->setBrush(QBrush(Qt::blue));
scene->addText("Hello world!");
scene->addRect(rect, QPen(Qt::black), QBrush(Qt::blue));
// scene->addRect(rect, QPen(Qt::black), QBrush(Qt::blue));
scene->addItem(item1);
ui->spinBoxNumCrazyflies->setMaximum(N_MAX_CRAZYFLIES);
_refresh_tabs();
......
......@@ -6,6 +6,7 @@
#include <QMainWindow>
#include <QTimer>
#include <QGridLayout>
#include <QGraphicsRectItem>
#ifndef DEBUG_GUI
#include "ros/callback_queue.h"
......@@ -137,7 +138,7 @@ private:
Ui::MainGUIWindow *ui;
myGraphicsScene* scene; //TODO: make a subclass from QGraphicScene class, mouse events
QGraphicsRectItem* item1;
void _init();
void _refresh_tabs();
......
......@@ -2,15 +2,17 @@
#include <QGraphicsSceneMouseEvent>
#include <QRect>
#include <QGraphicsRectItem>
myGraphicsScene::myGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
pen = new QPen(Qt::black);
brush = new QBrush(Qt::blue);
rect = 0;
// startedRect = false;
firstClick = true;
tmp_rect = 0;
startedRect = false;
// firstClick = true;
}
......@@ -20,33 +22,43 @@ void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
return;
// rect = new QRect((mouseEvent->scenePos()).toPoint(), (mouseEvent->scenePos()).toPoint());
// addRect(*rect, *pen, *brush);
// startedRect = true;
// Drag and drop approach
if(firstClick)
{
p1 = new QPoint((mouseEvent->scenePos()).toPoint());
QRect tmp_rect(*p1, *p1);
addRect(tmp_rect, *pen, *brush);
}
else
{
p2 = new QPoint((mouseEvent->scenePos()).toPoint());
QRect tmp_rect(*p2, *p2);
addRect(tmp_rect, *pen, *brush);
}
startedRect = true;
p1 = new QPointF(mouseEvent->scenePos());
tmp_rect = new QRectF(*p1, *p1);
// addRect(*tmp_rect, *pen, *brush);
tmp_rect_item = new QGraphicsRectItem(*tmp_rect);
rectangles.push_back(tmp_rect_item);
addItem(rectangles.back());
// Two-clicks approach
// if(firstClick)
// {
// p1 = new QPointF(mouseEvent->scenePos());
// tmp_rect_item = addRect(QRect(p1->toPoint(), p1->toPoint()), *pen, *brush); //save it to remove it after
// }
// else
// {
// p2 = new QPointF(mouseEvent->scenePos());
// // QRect tmp_rect(*p2, *p2);
// // addRect(tmp_rect, *pen, *brush);
// }
update();
QGraphicsScene::mousePressEvent(mouseEvent);
}
void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
// if(startedRect)
// {
// rect->moveBottomRight((mouseEvent->scenePos()).toPoint());
// qDebug("Mouse Position: %d, %d", (mouseEvent->scenePos()).toPoint().x(), (mouseEvent->scenePos()).toPoint().y());
// qDebug("Rectangle BottomRight Position: %d, %d", rect->bottomRight().x(), rect->bottomRight().y());
// }
if(startedRect)
{
tmp_rect_item->setRect(QRectF(*p1, mouseEvent->scenePos()));
qDebug("Mouse Position: %d, %d", (mouseEvent->scenePos()).toPoint().x(), (mouseEvent->scenePos()).toPoint().y());
qDebug("Rectangle BottomRight Position: %d, %d", tmp_rect->bottomRight().x(), tmp_rect->bottomRight().y());
update();
}
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
......@@ -55,20 +67,33 @@ void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
if (mouseEvent->button() != Qt::LeftButton)
return;
// rect = 0;
// startedRect = false;
if(firstClick)
{
firstClick = false;
}
else
{
rect = new QRect(*p1, *p2);
addRect(*rect, *pen, *brush);
p1 = 0;
p2 = 0;
rect = 0;
firstClick = true;
}
// Drag and drop approach:
tmp_rect = 0;
startedRect = false;
// Two-clicks approach
// if(firstClick)
// {
// firstClick = false;
// }
// else
// {
// removeItem(tmp_rect_item);
// tmp_rect_item = new QGraphicsRectItem(QRectF(*p1, *p2));
// // *tmp_rect, *pen, *brush);
// rectangles.push_back(tmp_rect_item);
// addItem(rectangles.back());
// p1 = 0;
// p2 = 0;
// tmp_rect = 0;
// firstClick = true;
// }
update();
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
#ifndef DIAGRAMSCENE_H
#define DIAGRAMSCENE_H
#ifndef MYGRAPHICSSCENE_H
#define MYGRAPHICSSCENE_H
#include <vector>
#include <QGraphicsScene>
......@@ -14,6 +16,7 @@ class myGraphicsScene : public QGraphicsScene
public:
explicit myGraphicsScene(QObject *parent = 0);
std::vector<QGraphicsRectItem*> rectangles;
public slots:
......@@ -27,13 +30,14 @@ protected:
private:
QPen* pen;
QBrush* brush;
QRect* rect;
QPoint* p1;
QPoint* p2;
QRectF* tmp_rect;
QGraphicsRectItem* tmp_rect_item;
QPointF* p1;
QPointF* p2;
bool firstClick;
// bool firstClick;
// bool startedRect;
bool startedRect;
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment