Skip to content
Snippets Groups Projects
Commit 94abccb9 authored by roangel's avatar roangel
Browse files

Added resize, deletion, selection according with tabs, zoom, ...

parent 24f75cf7
No related branches found
No related tags found
No related merge requests found
Showing
with 807 additions and 173 deletions
This diff is collapsed.
......@@ -31,7 +31,7 @@ public:
qreal mouseDownY;
enum {kMouseReleased=0, kMouseDown, kMouseMoving}; ///< define the mouse states
enum {bottomLeft, topLeft, topRight, bottomRight};
enum {noCorner, bottomLeft, topLeft, topRight, bottomRight};
private:
......
No preview for this file type
#include "mainguiwindow.h"
#include "ui_mainguiwindow.h"
#include <QObject>
#include <QDoubleSpinBox>
#include <QTextEdit>
#include <QString>
......@@ -38,11 +39,11 @@ MainGUIWindow::~MainGUIWindow()
delete ui;
}
void MainGUIWindow::_refresh_tabs()
void MainGUIWindow::set_tabs(int n)
{
ui->tabWidget->clear();
std::string str;
for (int i = 0; i < ui->spinBoxNumCrazyflies->value(); i++)
for (int i = 0; i < n; i++)
{
str = "CrazyFly ";
str += std::to_string(i+1);
......@@ -58,16 +59,13 @@ void MainGUIWindow::_init()
//scene->setSceneRect(QRectF(QPointF(-100, 100), QSizeF(200, 200)));
ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
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->addItem(item1);
ui->graphicsView->setScene(scene);
ui->spinBoxNumCrazyflies->setMaximum(N_MAX_CRAZYFLIES);
_refresh_tabs();
QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), scene, SLOT(removeRectangle(int)));
QObject::connect(scene, SIGNAL(numRectanglesChanged(int)), this, SLOT(set_tabs(int)));
QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), scene, SLOT(setSelectedRectangle(int)));
QObject::connect(scene, SIGNAL(rectangleSelected(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
}
#ifndef DEBUG_GUI
......@@ -133,6 +131,8 @@ void MainGUIWindow::init()
//refreshScreen();
}
// void MainGUIWindow::refreshScreen()
// {
// // for(int i=0;i<countPIDControllers;i++)
......@@ -942,17 +942,3 @@ void CSetpointQueue::print()
// }
#endif // DEBUG_GUI
void MainGUIWindow::on_spinBoxNumCrazyflies_valueChanged(int arg1)
{
_refresh_tabs();
}
void MainGUIWindow::on_spinBoxNumCrazyflies_editingFinished()
{
}
void MainGUIWindow::on_graphicsView_rubberBandChanged(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint)
{
}
......@@ -67,6 +67,7 @@ public:
#ifndef DEBUG_GUI
void init();
#endif
public slots:
#ifndef DEBUG_GUI
void runCallbacks();
......@@ -128,11 +129,8 @@ private slots:
// void on_buttonStop_2_clicked();
#endif
void on_spinBoxNumCrazyflies_valueChanged(int arg1);
void on_spinBoxNumCrazyflies_editingFinished();
void set_tabs(int n);
void on_graphicsView_rubberBandChanged(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint);
private:
......@@ -141,7 +139,6 @@ private:
QGraphicsRectItem* item1;
void _init();
void _refresh_tabs();
#ifndef DEBUG_GUI
// void refreshScreen();
......
No preview for this file type
......@@ -44,14 +44,17 @@
<property name="currentIndex">
<number>-1</number>
</property>
<property name="tabsClosable">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QFrame" name="frame_drawing">
<property name="geometry">
<rect>
<x>30</x>
<x>20</x>
<y>20</y>
<width>582</width>
<width>641</width>
<height>469</height>
</rect>
</property>
......@@ -61,13 +64,13 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QGraphicsView" name="graphicsView">
<widget class="myGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>531</width>
<height>430</height>
<x>10</x>
<y>9</y>
<width>621</width>
<height>451</height>
</rect>
</property>
<property name="mouseTracking">
......@@ -78,9 +81,9 @@
<widget class="QFrame" name="frame_3">
<property name="geometry">
<rect>
<x>629</x>
<x>669</x>
<y>21</y>
<width>581</width>
<width>541</width>
<height>469</height>
</rect>
</property>
......@@ -91,25 +94,6 @@
<enum>QFrame::Raised</enum>
</property>
</widget>
<widget class="QSpinBox" name="spinBoxNumCrazyflies">
<property name="geometry">
<rect>
<x>1340</x>
<y>560</y>
<width>47</width>
<height>23</height>
</rect>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
<property name="accelerated">
<bool>false</bool>
</property>
<property name="maximum">
<number>6</number>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
......@@ -132,6 +116,13 @@
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>myGraphicsView</class>
<extends>QGraphicsView</extends>
<header>myGraphicsView.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
......@@ -19,8 +19,8 @@
QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_MainGUIWindow_t {
QByteArrayData data[9];
char stringdata0[172];
QByteArrayData data[4];
char stringdata0[26];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
......@@ -30,20 +30,12 @@ struct qt_meta_stringdata_MainGUIWindow_t {
static const qt_meta_stringdata_MainGUIWindow_t qt_meta_stringdata_MainGUIWindow = {
{
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(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"
QT_MOC_LITERAL(1, 14, 8), // "set_tabs"
QT_MOC_LITERAL(2, 23, 0), // ""
QT_MOC_LITERAL(3, 24, 1) // "n"
},
"MainGUIWindow\0on_spinBoxNumCrazyflies_valueChanged\0"
"\0arg1\0on_spinBoxNumCrazyflies_editingFinished\0"
"on_graphicsView_rubberBandChanged\0"
"viewportRect\0fromScenePoint\0toScenePoint"
"MainGUIWindow\0set_tabs\0\0n"
};
#undef QT_MOC_LITERAL
......@@ -53,7 +45,7 @@ static const uint qt_meta_data_MainGUIWindow[] = {
7, // revision
0, // classname
0, 0, // classinfo
3, 14, // methods
1, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
......@@ -61,14 +53,10 @@ static const uint qt_meta_data_MainGUIWindow[] = {
0, // signalCount
// slots: name, argc, parameters, tag, flags
1, 1, 29, 2, 0x08 /* Private */,
4, 0, 32, 2, 0x08 /* Private */,
5, 3, 33, 2, 0x08 /* Private */,
1, 1, 19, 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
};
......@@ -79,9 +67,7 @@ void MainGUIWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _i
MainGUIWindow *_t = static_cast<MainGUIWindow *>(_o);
Q_UNUSED(_t)
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;
case 0: _t->set_tabs((*reinterpret_cast< int(*)>(_a[1]))); break;
default: ;
}
}
......@@ -112,13 +98,13 @@ int MainGUIWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 3)
if (_id < 1)
qt_static_metacall(this, _c, _id, _a);
_id -= 3;
_id -= 1;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 3)
if (_id < 1)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 3;
_id -= 1;
}
return _id;
}
......
No preview for this file type
......@@ -19,8 +19,8 @@
QT_BEGIN_MOC_NAMESPACE
struct qt_meta_stringdata_myGraphicsScene_t {
QByteArrayData data[1];
char stringdata0[16];
QByteArrayData data[8];
char stringdata0[106];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
......@@ -29,10 +29,19 @@ struct qt_meta_stringdata_myGraphicsScene_t {
)
static const qt_meta_stringdata_myGraphicsScene_t qt_meta_stringdata_myGraphicsScene = {
{
QT_MOC_LITERAL(0, 0, 15) // "myGraphicsScene"
QT_MOC_LITERAL(0, 0, 15), // "myGraphicsScene"
QT_MOC_LITERAL(1, 16, 20), // "numRectanglesChanged"
QT_MOC_LITERAL(2, 37, 0), // ""
QT_MOC_LITERAL(3, 38, 6), // "newNum"
QT_MOC_LITERAL(4, 45, 17), // "rectangleSelected"
QT_MOC_LITERAL(5, 63, 5), // "index"
QT_MOC_LITERAL(6, 69, 15), // "removeRectangle"
QT_MOC_LITERAL(7, 85, 20) // "setSelectedRectangle"
},
"myGraphicsScene"
"myGraphicsScene\0numRectanglesChanged\0"
"\0newNum\0rectangleSelected\0index\0"
"removeRectangle\0setSelectedRectangle"
};
#undef QT_MOC_LITERAL
......@@ -42,22 +51,62 @@ static const uint qt_meta_data_myGraphicsScene[] = {
7, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
4, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
2, // signalCount
// signals: name, argc, parameters, tag, flags
1, 1, 34, 2, 0x06 /* Public */,
4, 1, 37, 2, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags
6, 1, 40, 2, 0x0a /* Public */,
7, 1, 43, 2, 0x0a /* Public */,
// signals: parameters
QMetaType::Void, QMetaType::Int, 3,
QMetaType::Void, QMetaType::Int, 5,
// slots: parameters
QMetaType::Void, QMetaType::Int, 5,
QMetaType::Void, QMetaType::Int, 5,
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);
if (_c == QMetaObject::InvokeMetaMethod) {
myGraphicsScene *_t = static_cast<myGraphicsScene *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->numRectanglesChanged((*reinterpret_cast< int(*)>(_a[1]))); break;
case 1: _t->rectangleSelected((*reinterpret_cast< int(*)>(_a[1]))); break;
case 2: _t->removeRectangle((*reinterpret_cast< int(*)>(_a[1]))); break;
case 3: _t->setSelectedRectangle((*reinterpret_cast< int(*)>(_a[1]))); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (myGraphicsScene::*_t)(int );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&myGraphicsScene::numRectanglesChanged)) {
*result = 0;
return;
}
}
{
typedef void (myGraphicsScene::*_t)(int );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&myGraphicsScene::rectangleSelected)) {
*result = 1;
return;
}
}
}
}
const QMetaObject myGraphicsScene::staticMetaObject = {
......@@ -84,6 +133,29 @@ int myGraphicsScene::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
_id = QGraphicsScene::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 4)
qt_static_metacall(this, _c, _id, _a);
_id -= 4;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 4)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 4;
}
return _id;
}
// SIGNAL 0
void myGraphicsScene::numRectanglesChanged(int _t1)
{
void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
// SIGNAL 1
void myGraphicsScene::rectangleSelected(int _t1)
{
void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
QT_END_MOC_NAMESPACE
No preview for this file type
/****************************************************************************
** Meta object code from reading C++ file 'myGraphicsView.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 "myGraphicsView.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'myGraphicsView.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_myGraphicsView_t {
QByteArrayData data[1];
char stringdata0[15];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
qptrdiff(offsetof(qt_meta_stringdata_myGraphicsView_t, stringdata0) + ofs \
- idx * sizeof(QByteArrayData)) \
)
static const qt_meta_stringdata_myGraphicsView_t qt_meta_stringdata_myGraphicsView = {
{
QT_MOC_LITERAL(0, 0, 14) // "myGraphicsView"
},
"myGraphicsView"
};
#undef QT_MOC_LITERAL
static const uint qt_meta_data_myGraphicsView[] = {
// 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 myGraphicsView::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 myGraphicsView::staticMetaObject = {
{ &QGraphicsView::staticMetaObject, qt_meta_stringdata_myGraphicsView.data,
qt_meta_data_myGraphicsView, qt_static_metacall, Q_NULLPTR, Q_NULLPTR}
};
const QMetaObject *myGraphicsView::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}
void *myGraphicsView::qt_metacast(const char *_clname)
{
if (!_clname) return Q_NULLPTR;
if (!strcmp(_clname, qt_meta_stringdata_myGraphicsView.stringdata0))
return static_cast<void*>(const_cast< myGraphicsView*>(this));
return QGraphicsView::qt_metacast(_clname);
}
int myGraphicsView::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QGraphicsView::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
return _id;
}
QT_END_MOC_NAMESPACE
File added
......@@ -12,11 +12,13 @@ myGraphicsRectItem::myGraphicsRectItem(const QRectF & rect, QGraphicsItem * pare
{
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setFlag(QGraphicsItem::ItemIsMovable);
this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
pen = new QPen(Qt::red);
brush = new QBrush(Qt::red);
tmp_rect = 0;
_grabbers_created = false;
resize_mode = false;
// this->setAcceptHoverEvents(true);
}
......@@ -49,6 +51,20 @@ QVariant myGraphicsRectItem::itemChange(GraphicsItemChange change, const QVarian
deleteGrabbers();
}
}
if (change == QGraphicsItem::ItemPositionChange && scene()) // with this, block movement of object when we are hovering through cornergrabbers
{
if(checkCornerGrabbers() == CornerGrabber::noCorner)
{
qDebug("move now!");
return QPointF(value.toPointF().x(), value.toPointF().y());
}
else
{
qDebug("dont move now!");
return QPointF(pos().x(), pos().y());
}
}
return QGraphicsItem::itemChange(change, value);
}
......@@ -58,7 +74,7 @@ bool myGraphicsRectItem::grabbersAreCreated()
return _grabbers_created;
}
void myGraphicsRectItem::setCornerPositions()
void myGraphicsRectItem::setCornerPositions() //need to call this function whenever we chnge the size of the rectangle
{
QRectF rect = this->rect();
......@@ -101,7 +117,15 @@ int myGraphicsRectItem::checkCornerGrabbers()
else if( _bottomRight_corner->isActive())
return CornerGrabber::bottomRight;
else
return 20;
return CornerGrabber::noCorner; //0 is none
}
bool myGraphicsRectItem::anyGrabber()
{
if(checkCornerGrabbers() != CornerGrabber::noCorner)
return true;
else
return false;
}
void myGraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
......@@ -109,38 +133,64 @@ void myGraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
if (mouseEvent->button() != Qt::LeftButton)
return;
createGrabbers(); //This is just in case they have not been created by now.
createGrabbers(); //This is just in case they have not been created by now. We have a creator guardian anyhow
switch(checkCornerGrabbers())
if(anyGrabber())
{
case CornerGrabber::bottomLeft:
qDebug("bottomLeft");
mouseEvent->setAccepted(true);
break;
case CornerGrabber::topLeft:
qDebug("topLeft");
mouseEvent->setAccepted(true);
break;
case CornerGrabber::topRight:
qDebug("topRight");
mouseEvent->setAccepted(true);
break;
case CornerGrabber::bottomRight:
qDebug("bottomRight");
mouseEvent->setAccepted(true);
break;
default:
qDebug("No corner");
// Normal case here, mouse not in corner grabbers
QGraphicsRectItem::mousePressEvent(mouseEvent);
break;
resize_mode = true;
}
// TODO: check if over handlers, if, resize mode
QGraphicsRectItem::mousePressEvent(mouseEvent);
}
void myGraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
this->prepareGeometryChange();
if(resize_mode)
{
switch(checkCornerGrabbers())
{
case CornerGrabber::bottomLeft:
{
qDebug("bottomLeft");
QRectF resize_rect = this->rect();
resize_rect.setBottomLeft(mouseEvent->pos());
this->setRect(resize_rect.normalized());
setCornerPositions();
break;
}
case CornerGrabber::topLeft:
{
qDebug("topLeft");
QRectF resize_rect = this->rect();
resize_rect.setTopLeft(mouseEvent->pos());
this->setRect(resize_rect.normalized());
setCornerPositions();
break;
}
case CornerGrabber::topRight:
{
qDebug("topRight");
QRectF resize_rect = this->rect();
resize_rect.setTopRight(mouseEvent->pos());
this->setRect(resize_rect.normalized());
setCornerPositions();
break;
}
case CornerGrabber::bottomRight:
{
qDebug("bottomRight");
QRectF resize_rect = this->rect();
resize_rect.setBottomRight(mouseEvent->pos());
this->setRect(resize_rect.normalized());
setCornerPositions();
break;
}
case CornerGrabber::noCorner:
qDebug("No corner. Should never enter here");
default:
break;
}
}
QGraphicsRectItem::mouseMoveEvent(mouseEvent);
}
......@@ -150,5 +200,9 @@ void myGraphicsRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
if (mouseEvent->button() != Qt::LeftButton)
return;
// TODO: stop resize mode
if(resize_mode)
{
resize_mode = false;
}
QGraphicsRectItem::mouseReleaseEvent(mouseEvent);
}
......@@ -28,6 +28,7 @@ protected:
private:
void setCornerPositions();
bool anyGrabber();
int checkCornerGrabbers();
void createGrabbers();
bool grabbersAreCreated();
......@@ -46,6 +47,8 @@ private:
CornerGrabber* _bottomRight_corner;
bool _grabbers_created;
bool resize_mode;
};
#endif
No preview for this file type
......@@ -3,6 +3,7 @@
#include <QGraphicsSceneMouseEvent>
#include <QRect>
#include <QApplication>
#include <QKeyEvent>
myGraphicsScene::myGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
......@@ -15,6 +16,49 @@ myGraphicsScene::myGraphicsScene(QObject *parent)
// firstClick = true;
}
void myGraphicsScene::keyPressEvent(QKeyEvent * keyEvent)
{
if(keyEvent->key() == Qt::Key_Delete)
{
qDebug("del key pressed");
for(unsigned int i = 0; i < rectangles.size(); i++)
{
if(rectangles[i]->isSelected())
{
qDebug("selectedRectangle: %d", i);
removeRectangle(i);
}
}
}
QGraphicsScene::keyPressEvent(keyEvent);
}
void myGraphicsScene::setSelectedRectangle(int index)
{
for(unsigned int i = 0; i < rectangles.size(); i++)
{
rectangles[i]->setSelected(false);
if(index == i)
{
rectangles[index]->setSelected(true);
}
}
}
int myGraphicsScene::checkSelectedRectangle()
{
for(unsigned int i = 0; i < rectangles.size(); i++)
{
if(rectangles[i]->isSelected())
{
qDebug("rectangle selected index = %d", i);
return i;
}
}
return -1;
}
void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
......@@ -29,6 +73,7 @@ void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
addItem(tmp_rect_item);
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
......@@ -43,27 +88,37 @@ void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void myGraphicsScene::addRectangleToVector(myGraphicsRectItem* rect)
{
rectangles.push_back(rect);
emit numRectanglesChanged(rectangles.size());
}
void myGraphicsScene::removeRectangle(int index)
{
this->removeItem(rectangles[index]);
rectangles.erase(rectangles.begin() + index);
emit numRectanglesChanged(rectangles.size());
}
void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if(startedRect)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
// Drag and drop approach:
// TODO: If too small, etc etc, dont add it to the container and remove it
tmp_rect_item->setRect(tmp_rect_item->rect().normalized());
rectangles.push_back(tmp_rect_item);
addRectangleToVector(tmp_rect_item);
tmp_rect = 0;
startedRect = false;
// update();
}
int selected_rect = checkSelectedRectangle();
if(selected_rect != -1)
{
emit rectangleSelected(selected_rect);
}
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
......@@ -17,19 +17,29 @@ class myGraphicsScene : public QGraphicsScene
Q_OBJECT
public:
explicit myGraphicsScene(QObject *parent = 0);
std::vector<myGraphicsRectItem*> rectangles;
public slots:
void removeRectangle(int index);
void setSelectedRectangle(int index);
signals:
void numRectanglesChanged(int newNum);
void rectangleSelected(int index);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
void keyPressEvent(QKeyEvent * keyEvent) override;
private:
void addRectangleToVector(myGraphicsRectItem* rect);
int checkSelectedRectangle();
QPen* pen;
QBrush* brush;
QRectF* tmp_rect;
......@@ -37,8 +47,6 @@ private:
QPointF* p1;
QPointF* p2;
// bool firstClick;
bool startedRect;
};
......
No preview for this file type
#include "myGraphicsView.h"
#include <QApplication>
#include <QMouseEvent>
myGraphicsView::myGraphicsView(QWidget *parent)
: QGraphicsView(parent)
{
translation_mode = false;
}
void myGraphicsView::wheelEvent(QWheelEvent *event)
{
if(Qt::ControlModifier == QApplication::keyboardModifiers())
{
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// Scale the view / do the zoom
double scaleFactor = 1.15;
if(event->delta() > 0) {
// Zoom in
this-> scale(scaleFactor, scaleFactor);
} else {
// Zooming out
this->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
}
else
{
QGraphicsView::wheelEvent(event); // dont propagate if we are zooming. If we propagate, we will also scroll
}
}
void myGraphicsView::mousePressEvent(QMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::MiddleButton)
{
translation_mode = true;
tmp_point = new QPointF(mouseEvent->localPos());
}
QGraphicsView::mousePressEvent(mouseEvent);
}
void myGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent)
{
if(translation_mode)
{
translate_dx = mouseEvent->localPos().x() - tmp_point->x();
translate_dy = mouseEvent->localPos().y() - tmp_point->y();
this->translate(translate_dx, translate_dy);
}
QGraphicsView::mouseMoveEvent(mouseEvent);
}
void myGraphicsView::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
translation_mode = false;
QGraphicsView::mouseReleaseEvent(mouseEvent);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment