Commit 6b7d2803 authored by roangel's avatar roangel
Browse files

Added QGraphicsView and QGraphicsScene. Can draw rectangles, need to find out...

Added QGraphicsView and QGraphicsScene. Can draw rectangles, need to find out what is wrong with refreshing
parent 85d8fe4c
#include "mainguiwindow.h"
#include "ui_mainguiwindow.h"
#include <QDoubleSpinBox>
#include <QTextEdit>
#include <QString>
......@@ -27,6 +28,7 @@ MainGUIWindow::MainGUIWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainGUIWindow)
{
ui->setupUi(this);
_init();
}
......@@ -52,6 +54,14 @@ void MainGUIWindow::_refresh_tabs()
void MainGUIWindow::_init()
{
scene = new myGraphicsScene(this);
ui->graphicsView->setScene(scene);
QRect rect(10, 20, 80, 60);
scene->addText("Hello world!");
scene->addRect(rect, QPen(Qt::black), QBrush(Qt::blue));
ui->spinBoxNumCrazyflies->setMaximum(N_MAX_CRAZYFLIES);
_refresh_tabs();
}
......@@ -930,10 +940,15 @@ void CSetpointQueue::print()
#endif // DEBUG_GUI
void MainGUIWindow::on_spinBoxNumCrazyflies_valueChanged(int arg1)
{
_refresh_tabs();
}
void MainGUIWindow::on_spinBoxNumCrazyflies_editingFinished()
{
_refresh_tabs();
}
void MainGUIWindow::on_graphicsView_rubberBandChanged(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint)
{
}
......@@ -5,6 +5,7 @@
#include <QMainWindow>
#include <QTimer>
#include <QGridLayout>
#ifndef DEBUG_GUI
#include "ros/callback_queue.h"
......@@ -12,7 +13,7 @@
#include "CrazyFlieTypes.h"
#endif
#include "ui_mainguiwindow.h"
#include "myGraphicsScene.h"
namespace Ui {
class MainGUIWindow;
......@@ -130,9 +131,14 @@ private slots:
void on_spinBoxNumCrazyflies_editingFinished();
void on_graphicsView_rubberBandChanged(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint);
private:
Ui::MainGUIWindow *ui;
myGraphicsScene* scene; //TODO: make a subclass from QGraphicScene class, mouse events
void _init();
void _refresh_tabs();
......
......@@ -46,13 +46,13 @@
</property>
</widget>
</widget>
<widget class="QFrame" name="frame_2">
<widget class="QFrame" name="frame_drawing">
<property name="geometry">
<rect>
<x>40</x>
<y>10</y>
<width>571</width>
<height>481</height>
<x>30</x>
<y>20</y>
<width>582</width>
<height>469</height>
</rect>
</property>
<property name="frameShape">
......@@ -61,14 +61,27 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>531</width>
<height>430</height>
</rect>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QFrame" name="frame_3">
<property name="geometry">
<rect>
<x>630</x>
<y>10</y>
<width>791</width>
<height>481</height>
<x>629</x>
<y>21</y>
<width>581</width>
<height>469</height>
</rect>
</property>
<property name="frameShape">
......
......@@ -19,8 +19,8 @@
QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_MainGUIWindow_t {
QByteArrayData data[5];
char stringdata0[97];
QByteArrayData data[9];
char stringdata0[172];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
......@@ -33,11 +33,17 @@ QT_MOC_LITERAL(0, 0, 13), // "MainGUIWindow"
QT_MOC_LITERAL(1, 14, 36), // "on_spinBoxNumCrazyflies_value..."
QT_MOC_LITERAL(2, 51, 0), // ""
QT_MOC_LITERAL(3, 52, 4), // "arg1"
QT_MOC_LITERAL(4, 57, 39) // "on_spinBoxNumCrazyflies_editi..."
QT_MOC_LITERAL(4, 57, 39), // "on_spinBoxNumCrazyflies_editi..."
QT_MOC_LITERAL(5, 97, 33), // "on_graphicsView_rubberBandCha..."
QT_MOC_LITERAL(6, 131, 12), // "viewportRect"
QT_MOC_LITERAL(7, 144, 14), // "fromScenePoint"
QT_MOC_LITERAL(8, 159, 12) // "toScenePoint"
},
"MainGUIWindow\0on_spinBoxNumCrazyflies_valueChanged\0"
"\0arg1\0on_spinBoxNumCrazyflies_editingFinished"
"\0arg1\0on_spinBoxNumCrazyflies_editingFinished\0"
"on_graphicsView_rubberBandChanged\0"
"viewportRect\0fromScenePoint\0toScenePoint"
};
#undef QT_MOC_LITERAL
......@@ -47,7 +53,7 @@ static const uint qt_meta_data_MainGUIWindow[] = {
7, // revision
0, // classname
0, 0, // classinfo
2, 14, // methods
3, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
......@@ -55,12 +61,14 @@ static const uint qt_meta_data_MainGUIWindow[] = {
0, // signalCount
// slots: name, argc, parameters, tag, flags
1, 1, 24, 2, 0x08 /* Private */,
4, 0, 27, 2, 0x08 /* Private */,
1, 1, 29, 2, 0x08 /* Private */,
4, 0, 32, 2, 0x08 /* Private */,
5, 3, 33, 2, 0x08 /* Private */,
// slots: parameters
QMetaType::Void, QMetaType::Int, 3,
QMetaType::Void,
QMetaType::Void, QMetaType::QRect, QMetaType::QPointF, QMetaType::QPointF, 6, 7, 8,
0 // eod
};
......@@ -73,6 +81,7 @@ void MainGUIWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _i
switch (_id) {
case 0: _t->on_spinBoxNumCrazyflies_valueChanged((*reinterpret_cast< int(*)>(_a[1]))); break;
case 1: _t->on_spinBoxNumCrazyflies_editingFinished(); break;
case 2: _t->on_graphicsView_rubberBandChanged((*reinterpret_cast< const QRect(*)>(_a[1])),(*reinterpret_cast< const QPointF(*)>(_a[2])),(*reinterpret_cast< const QPointF(*)>(_a[3]))); break;
default: ;
}
}
......@@ -103,13 +112,13 @@ int MainGUIWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 2)
if (_id < 3)
qt_static_metacall(this, _c, _id, _a);
_id -= 2;
_id -= 3;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 2)
if (_id < 3)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 2;
_id -= 3;
}
return _id;
}
......
/****************************************************************************
** Meta object code from reading C++ file 'myGraphicsScene.h'
**
** Created by: The Qt Meta Object Compiler version 67 (Qt 5.7.0)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
#include "myGraphicsScene.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'myGraphicsScene.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.7.0. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_myGraphicsScene_t {
QByteArrayData data[1];
char stringdata0[16];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_myGraphicsScene_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_myGraphicsScene_t qt_meta_stringdata_myGraphicsScene = {
{
QT_MOC_LITERAL(0, 0, 15) // "myGraphicsScene"
},
"myGraphicsScene"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_myGraphicsScene[] = {
// content:
7, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
0 // eod
};
void myGraphicsScene::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
Q_UNUSED(_o);
Q_UNUSED(_id);
Q_UNUSED(_c);
Q_UNUSED(_a);
}
const QMetaObject myGraphicsScene::staticMetaObject = {
{ &QGraphicsScene::staticMetaObject, qt_meta_stringdata_myGraphicsScene.data,
qt_meta_data_myGraphicsScene, qt_static_metacall, Q_NULLPTR, Q_NULLPTR}
};
const QMetaObject *myGraphicsScene::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *myGraphicsScene::qt_metacast(const char *_clname)
{
if (!_clname) return Q_NULLPTR;
if (!strcmp(_clname, qt_meta_stringdata_myGraphicsScene.stringdata0))
return static_cast<void*>(const_cast< myGraphicsScene*>(this));
return QGraphicsScene::qt_metacast(_clname);
}
int myGraphicsScene::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QGraphicsScene::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
return _id;
}
QT_END_MOC_NAMESPACE
/****************************************************************************
** Meta object code from reading C++ file 'renderarea.h'
**
** Created by: The Qt Meta Object Compiler version 67 (Qt 5.7.0)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
#include "renderarea.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'renderarea.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.7.0. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_RenderArea_t {
QByteArrayData data[13];
char stringdata0[113];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_RenderArea_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_RenderArea_t qt_meta_stringdata_RenderArea = {
{
QT_MOC_LITERAL(0, 0, 10), // "RenderArea"
QT_MOC_LITERAL(1, 11, 8), // "setShape"
QT_MOC_LITERAL(2, 20, 0), // ""
QT_MOC_LITERAL(3, 21, 5), // "Shape"
QT_MOC_LITERAL(4, 27, 5), // "shape"
QT_MOC_LITERAL(5, 33, 6), // "setPen"
QT_MOC_LITERAL(6, 40, 3), // "pen"
QT_MOC_LITERAL(7, 44, 8), // "setBrush"
QT_MOC_LITERAL(8, 53, 5), // "brush"
QT_MOC_LITERAL(9, 59, 14), // "setAntialiased"
QT_MOC_LITERAL(10, 74, 11), // "antialiased"
QT_MOC_LITERAL(11, 86, 14), // "setTransformed"
QT_MOC_LITERAL(12, 101, 11) // "transformed"
},
"RenderArea\0setShape\0\0Shape\0shape\0"
"setPen\0pen\0setBrush\0brush\0setAntialiased\0"
"antialiased\0setTransformed\0transformed"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_RenderArea[] = {
// content:
7, // revision
0, // classname
0, 0, // classinfo
5, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
// slots: name, argc, parameters, tag, flags
1, 1, 39, 2, 0x0a /* Public */,
5, 1, 42, 2, 0x0a /* Public */,
7, 1, 45, 2, 0x0a /* Public */,
9, 1, 48, 2, 0x0a /* Public */,
11, 1, 51, 2, 0x0a /* Public */,
// slots: parameters
QMetaType::Void, 0x80000000 | 3, 4,
QMetaType::Void, QMetaType::QPen, 6,
QMetaType::Void, QMetaType::QBrush, 8,
QMetaType::Void, QMetaType::Bool, 10,
QMetaType::Void, QMetaType::Bool, 12,
0 // eod
};
void RenderArea::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
RenderArea *_t = static_cast<RenderArea *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->setShape((*reinterpret_cast< Shape(*)>(_a[1]))); break;
case 1: _t->setPen((*reinterpret_cast< const QPen(*)>(_a[1]))); break;
case 2: _t->setBrush((*reinterpret_cast< const QBrush(*)>(_a[1]))); break;
case 3: _t->setAntialiased((*reinterpret_cast< bool(*)>(_a[1]))); break;
case 4: _t->setTransformed((*reinterpret_cast< bool(*)>(_a[1]))); break;
default: ;
}
}
}
const QMetaObject RenderArea::staticMetaObject = {
{ &QWidget::staticMetaObject, qt_meta_stringdata_RenderArea.data,
qt_meta_data_RenderArea, qt_static_metacall, Q_NULLPTR, Q_NULLPTR}
};
const QMetaObject *RenderArea::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *RenderArea::qt_metacast(const char *_clname)
{
if (!_clname) return Q_NULLPTR;
if (!strcmp(_clname, qt_meta_stringdata_RenderArea.stringdata0))
return static_cast<void*>(const_cast< RenderArea*>(this));
return QWidget::qt_metacast(_clname);
}
int RenderArea::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QWidget::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 5)
qt_static_metacall(this, _c, _id, _a);
_id -= 5;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 5)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 5;
}
return _id;
}
QT_END_MOC_NAMESPACE
#include "myGraphicsScene.h"
#include <QGraphicsSceneMouseEvent>
#include <QRect>
myGraphicsScene::myGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
pen = new QPen(Qt::black);
brush = new QBrush(Qt::blue);
rect = 0;
// startedRect = false;
firstClick = true;
}
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// rect = new QRect((mouseEvent->scenePos()).toPoint(), (mouseEvent->scenePos()).toPoint());
// addRect(*rect, *pen, *brush);
// startedRect = true;
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);
}
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());
// }
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
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;
}
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
#ifndef DIAGRAMSCENE_H
#define DIAGRAMSCENE_H
#include <QGraphicsScene>
class QGraphicsSceneMouseEvent;
class QPointF;
class QColor;
class myGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit myGraphicsScene(QObject *parent = 0);
public slots:
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
private:
QPen* pen;
QBrush* brush;
QRect* rect;
QPoint* p1;
QPoint* p2;
bool firstClick;
// bool startedRect;
};
#endif
......@@ -14,6 +14,7 @@
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QFrame>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
......@@ -31,7 +32,8 @@ public:
QWidget *centralWidget;
QFrame *frame;
QTabWidget *tabWidget;
QFrame *frame_2;
QFrame *frame_drawing;
QGraphicsView *graphicsView;
QFrame *frame_3;
QSpinBox *spinBoxNumCrazyflies;
QMenuBar *menuBar;
...