Guest User

Untitled

a guest
Jul 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. #include <QDebug>
  2. #include <QGraphicsScene>
  3. #include <QWheelEvent>
  4. #include <QGraphicsItem>
  5. #include <QGenericMatrix>
  6. #include <QApplication>
  7. #include <math.h>
  8.  
  9. #include "MyGraphicsView.h"
  10.  
  11. MyGraphicsView::MyGraphicsView(QWidget* iQWidget):
  12. QGraphicsView(iQWidget),
  13. Mode(SELECTION),
  14. ZoomBox(NULL),
  15. Panning(false),
  16. MaxZoomFactor(100)
  17. {
  18. Q_INIT_RESOURCE(MyGraphicsView);
  19. Scene = new QGraphicsScene(this);
  20. Scene->setItemIndexMethod(QGraphicsScene::NoIndex);
  21. setScene(Scene);
  22.  
  23. setCacheMode(CacheBackground);
  24. setViewportUpdateMode(BoundingRectViewportUpdate);
  25. setRenderHint(QPainter::Antialiasing);
  26. setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  27. setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  28.  
  29. setResizeAnchor(AnchorViewCenter);
  30.  
  31. setSceneRect(0,0,500,500);
  32.  
  33. scale(qreal(1.0), qreal(1.0));
  34. setWindowTitle(tr("QT Graphics View Test"));
  35.  
  36. //Add Control Buttons
  37. ControlPanelBackground = scene()->addRect(QRectF(size().width() - 100,0,100,5),QPen(),QBrush(QColor(255,255,255, 0)));
  38. ControlPanelBackground->setZValue(1);
  39. QWidget *ControlFrame = new QWidget();
  40. ControlFrame->setMinimumWidth(60);
  41. ControlFrame->setMinimumHeight(28);
  42. ModeButtonGroup = new QButtonGroup(ControlFrame);
  43.  
  44. ControlFrame->setLayout(new QHBoxLayout());
  45. ControlFrame->layout()->setSpacing(0);
  46. ControlFrame->layout()->setMargin(0);
  47.  
  48. ButtonSelect = new QPushButton(ControlFrame);
  49. ButtonSelect->setText("Select");
  50. ButtonSelect->setMinimumHeight(24);
  51. ButtonSelect->setCheckable(true);
  52. ButtonSelect->setChecked(true);
  53. ControlFrame->layout()->addWidget(ButtonSelect);
  54. ModeButtonGroup->addButton(ButtonSelect);
  55. ButtonZoom = new QPushButton(ControlFrame);
  56. ButtonZoom->setText("Zoom");
  57. ButtonZoom->setMinimumHeight(24);
  58. ButtonZoom->setCheckable(true);
  59. ControlFrame->layout()->addWidget(ButtonZoom);
  60. ModeButtonGroup->addButton(ButtonZoom);
  61.  
  62. ((QHBoxLayout *)ControlFrame->layout())->addSpacing(6);
  63.  
  64. ButtonFitToView = new QPushButton(ControlFrame);
  65. ButtonFitToView->setText("Zoom to Max");
  66. ButtonFitToView->setMinimumHeight(24);
  67. ControlFrame->layout()->addWidget(ButtonFitToView);
  68.  
  69. FrameGraphics = scene()->addWidget(ControlFrame);
  70. FrameGraphics->setParentItem(ControlPanelBackground);
  71. ControlPanelBackground->setRect(FrameGraphics->boundingRect());
  72.  
  73. connect(ButtonSelect, SIGNAL(clicked()), this, SLOT(vModeSelected()));
  74. connect(ButtonZoom, SIGNAL(clicked()), this, SLOT(vModeSelected()));
  75. connect(ButtonFitToView, SIGNAL(clicked()), this, SLOT(vFullExtents()));
  76.  
  77. PixmapItem = Scene->addPixmap(QPixmap(":/new/prefix1/apple.png"));
  78. PixmapItem->setPos(100, 100);
  79.  
  80. scaleView(1.0);
  81. }
  82.  
  83. MyGraphicsView::~MyGraphicsView()
  84. {
  85. }
  86.  
  87. void MyGraphicsView::vModeSelected()
  88. {
  89. QAbstractButton *_pButton = ModeButtonGroup->checkedButton();
  90. if (_pButton == ButtonSelect)
  91. {
  92. vSetMode(SELECTION);
  93. }
  94. else if (_pButton == ButtonZoom)
  95. {
  96. vSetMode(ZOOM_BOX);
  97. }
  98.  
  99. emit vOnModeChanged(getMode());
  100. }
  101.  
  102. // SETTING THE CURSORS FOR EACH MODE
  103. // THIS IS TRIGGERED FROM THE BUTTONS INSIDE THE GRAPHICS VIEW
  104. void MyGraphicsView::vSetMode(ModeEnum a_Mode)
  105. {
  106. Mode = a_Mode;
  107.  
  108. // MUST BE CALLED SO THAT THE CURSOR CAN BE CHANGED BACK TO THE ARROW ONCE WE'VE CHANGED
  109. // IT TO THE CUSTOM CURSOR
  110. this->unsetCursor();
  111.  
  112. if (a_Mode == SELECTION)
  113. {
  114. viewport()->setCursor(Qt::ArrowCursor);
  115. ButtonSelect->setChecked(true);
  116. }
  117. else if (a_Mode == ZOOM_BOX)
  118. {
  119. QCursor *_pCursor = new QCursor(QPixmap(":/new/prefix1/zoom.png"));
  120. viewport()->setCursor(*_pCursor);
  121. setCursor(*_pCursor);
  122. ButtonZoom->setChecked(true);
  123. }
  124.  
  125. // NEEDED TO ENSURE THAT THE CURSOR IS ACTUALLY CHANGED
  126. setDragMode(QGraphicsView::ScrollHandDrag);
  127. setDragMode(QGraphicsView::NoDrag);
  128. }
  129.  
  130. // SETTING THE CURSOR FROM A TRIGGER OUTSIDE THE GRAPHICS VIEW
  131. // THIS IS CALLED FROM THE BUTTON ABOVE THE VIEW
  132. void MyGraphicsView::vStartHiddenMode()
  133. {
  134. TempMode = Mode;
  135.  
  136. vSetMode(QUERY);
  137. QCursor *_pCursor = new QCursor(QPixmap(":/new/prefix1/pencil.png"));
  138. viewport()->setCursor(*_pCursor);
  139. setCursor(*_pCursor);
  140. setFocus();
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. void MyGraphicsView::vFullExtents()
  156. {
  157. QRectF unity = matrix().mapRect(QRectF(0, 0, 1, 1));
  158. scaleView(1 / unity.width());
  159. }
  160.  
  161. void MyGraphicsView::wheelEvent(QWheelEvent *event)
  162. {
  163. scaleView(pow((double)2, event->delta() / 240.0));
  164. }
  165.  
  166. void MyGraphicsView::resizeEvent(QResizeEvent *event)
  167. {
  168. setSceneRect(0,0, this->width(), this->height());
  169. this->setBackgroundBrush(QBrush(QColor(26, 51, 71)));
  170.  
  171. int wNewSizeWidth = event->size().width();
  172. int wNewSizeHeight = event->size().height();
  173.  
  174. int _width = size().width();
  175. QPoint wPointScreen(_width <= 250 ? 0 : _width- 250,5);
  176. QPointF wPointScene = mapToScene(wPointScreen);
  177.  
  178. qreal ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
  179.  
  180. if(ControlPanelBackground)
  181. {
  182. ControlPanelBackground->setPos(wPointScene);
  183. ControlPanelBackground->setScale(1/ActualFactor);
  184. }
  185. }
  186.  
  187. void MyGraphicsView::mousePressEvent(QMouseEvent *a_MouseEvent)
  188. {
  189. // Since the map should drag on the middle mouse, we make QGraphicsView believe that left button is pressed
  190. if(a_MouseEvent->button() == Qt::MidButton)
  191. {
  192. Panning = true;
  193. setDragMode(QGraphicsView::ScrollHandDrag);
  194. QMouseEvent* _Event = new QMouseEvent(a_MouseEvent->type(),a_MouseEvent->pos(),Qt::LeftButton,(a_MouseEvent->buttons() | Qt::MidButton),a_MouseEvent->modifiers());
  195. QGraphicsView::mousePressEvent(_Event);
  196. return;
  197. }
  198.  
  199. Panning = false;
  200. if(a_MouseEvent->button() == Qt::LeftButton && (Mode == ZOOM_BOX))
  201. {
  202. if(ZoomBox == NULL)
  203. {
  204. QPen wPen;
  205. wPen.setColor(QColor("Red"));
  206. wPen.setStyle(Qt::DashLine);
  207. ZoomBox = Scene->addRect(0,0,0,0,wPen);
  208. }
  209.  
  210. ZoomX = a_MouseEvent->x();
  211. ZoomY = a_MouseEvent->y();
  212.  
  213. ZoomBox->setRect(ZoomX, ZoomY,0,0);
  214. }
  215. else if(a_MouseEvent->button() == Qt::LeftButton && Mode == QUERY)
  216. {
  217. double _Lat, _Lon;
  218. int _ScreenX = a_MouseEvent->x();
  219. int _ScreenY = a_MouseEvent->y();
  220. }
  221.  
  222. QGraphicsView::mousePressEvent(a_MouseEvent);
  223. }
  224.  
  225. void MyGraphicsView::mouseReleaseEvent(QMouseEvent *a_MouseEvent)
  226. {
  227. // Since the view should drag on the middle mouse, we make QGraphicsView believe that left button is released
  228. if(a_MouseEvent->button() == Qt::MidButton)
  229. {
  230. Panning = false;
  231. QMouseEvent* _Event = new QMouseEvent(a_MouseEvent->type(),a_MouseEvent->pos(),Qt::LeftButton,(a_MouseEvent->buttons() | Qt::MidButton),a_MouseEvent->modifiers());
  232. QGraphicsView::mouseReleaseEvent(_Event);
  233. setDragMode(QGraphicsView::NoDrag);
  234. return;
  235. }
  236.  
  237. // Once we release the mouse in the query mode we turn it off and return to the previous mode.
  238. if (a_MouseEvent->button() == Qt::LeftButton && Mode == QUERY)
  239. {
  240. vSetMode(TempMode);
  241. }
  242.  
  243. if(ZoomBox && Mode == ZOOM_BOX)
  244. {
  245. Scene->removeItem(ZoomBox);
  246. delete ZoomBox;
  247. ZoomBox = NULL;
  248. }
  249. setDragMode(QGraphicsView::NoDrag);
  250. QGraphicsView::mouseReleaseEvent(a_MouseEvent);
  251. }
  252.  
  253. void MyGraphicsView::mouseMoveEvent(QMouseEvent * a_MouseEvent)
  254. {
  255. if(ZoomBox && (Mode == ZOOM_BOX))
  256. {
  257. int _ScreenX = a_MouseEvent->x();
  258. int _ScreenY = a_MouseEvent->y();
  259.  
  260. double _x = ZoomX;
  261. double _y = ZoomY;
  262. double _x2 = _x;
  263. double _y2 = _y;
  264. double _width, _height;
  265. _height = _ScreenY - _y;
  266. _width = _ScreenX - _x;
  267.  
  268. ZoomBox->setRect(_x2,_y2,_width, _height);
  269. }
  270.  
  271. QGraphicsView::mouseMoveEvent(a_MouseEvent);
  272. }
  273.  
  274.  
  275. void MyGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
  276. {
  277. if(Mode != SELECTION)
  278. {
  279. mousePressEvent(event);
  280. }
  281. else
  282. {
  283. QGraphicsView::mouseDoubleClickEvent(event);
  284. }
  285. }
  286.  
  287. void MyGraphicsView::scaleView(qreal scaleFactor)
  288. {
  289. QRectF _Rect = matrix().mapRect(QRectF(0, 0, 1, 1));
  290. qreal _ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
  291. qreal _NewFactor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  292.  
  293. // Limit zoom factor to 50...
  294. if(_NewFactor>MaxZoomFactor)
  295. {
  296. return;
  297. }
  298.  
  299. scale(scaleFactor, scaleFactor);
  300.  
  301. int _width = size().width();
  302. QPoint wPointScreen(_width <= 250 ? 0 : _width- 250,5);
  303. QPointF wPointScene = mapToScene(wPointScreen);
  304.  
  305. qreal ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
  306.  
  307. if(ControlPanelBackground)
  308. {
  309. ControlPanelBackground->setPos(wPointScene);
  310. ControlPanelBackground->setScale(1/ActualFactor);
  311. }
  312. }
Add Comment
Please, Sign In to add comment