ImageHolder.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * ImageHolder.cpp
  3. *
  4. * Created on: Oct 5, 2011
  5. * Author: gapchich
  6. */
  7. #include "ImageHolder.h"
  8. #include "functions.h"
  9. #include <QKeyEvent>
  10. #include <QMouseEvent>
  11. #include <QPainter>
  12. #include <QListWidgetItem>
  13. #include <qmath.h>
  14. #include <QDebug>
  15. //! A constructor initializing some variables
  16. ImageHolder::ImageHolder(QWidget *aParent)
  17. : QLabel(aParent)
  18. {
  19. repaint_needed_ = 0;
  20. tool_ = NoTool;
  21. state_ = StandBy;
  22. keyboard_modifier_ = Qt::NoModifier;
  23. focused_selection_ = -1;
  24. focused_selection_type_ = NoFigure;
  25. hovered_point_.figure = NoFigure;
  26. hovered_point_.figureID = -1;
  27. hovered_point_.pointID = -1;
  28. list_bounding_box_ = 0;
  29. main_label_ = 0;
  30. //list_bounding_box_ = new QList< QRect >;
  31. scale_ = 1;
  32. point_radius_ = 6;
  33. setScaledContents(true);
  34. setMouseTracking(true);
  35. }
  36. //! An empty destructor
  37. ImageHolder::~ImageHolder()
  38. {
  39. }
  40. //! An event which being automatically called after any change of the widget
  41. /*!
  42. * \see drawBoundingBoxes(QPainter *aPainter, QPen *aPen)
  43. * \see drawPolygons(QPainter *aPainter, QPen *aPen)
  44. *
  45. * It contains drawing of the confirmed and not confirmed selections either.
  46. */
  47. void
  48. ImageHolder::paintEvent(QPaintEvent *anEvent)
  49. {
  50. QLabel::paintEvent(anEvent);
  51. QPainter painter(this);
  52. painter.setRenderHint(QPainter::Antialiasing);
  53. //painter.setRenderHint(QPainter::SmoothPixmapTransform);
  54. QPen pen;
  55. if (NoTool != tool_) {
  56. pen.setWidth(1);
  57. pen.setColor(QColor(Qt::black));
  58. pen.setStyle(Qt::DashLine);
  59. painter.setPen(pen);
  60. if (BoundingBoxTool == tool_) {
  61. /* scaling */
  62. QRect bbox = bounding_box_.rect;
  63. QPoint bboxTopLeft = bbox.topLeft() * scale_;
  64. QPoint bboxBottomRight = bbox.bottomRight() * scale_;
  65. bbox.setTopLeft(bboxTopLeft);
  66. bbox.setBottomRight(bboxBottomRight);
  67. painter.drawRect(bbox);
  68. }
  69. else if (PolygonTool == tool_) {
  70. /* scaling */
  71. QPoint point;
  72. QPolygon poly = polygon_.poly;
  73. for (int i = 0; i < poly.size(); i++) {
  74. point.setX(poly.at(i).x());
  75. point.setY(poly.at(i).y());
  76. point *= scale_;
  77. poly.remove(i);
  78. poly.insert(i, point);
  79. }
  80. painter.drawPolygon(poly);
  81. }
  82. }
  83. /* drawing bounding boxes */
  84. drawBoundingBoxes(&painter, &pen);
  85. drawPolygons(&painter, &pen);
  86. }
  87. //! draws only confirmed bounding boxes
  88. /*!
  89. * parameters of bboxes may vary depending on whether bbox is selected or not or
  90. * whether it's label is main or not.
  91. */
  92. void
  93. ImageHolder::drawBoundingBoxes(
  94. QPainter *aPainter,
  95. QPen *aPen
  96. )
  97. {
  98. if (0 == list_bounding_box_)
  99. {
  100. return;
  101. /* NOTREACHED */
  102. }
  103. Qt::PenStyle penStyle;
  104. /* default width is hardcoded */
  105. int width = 2;
  106. /* drawing all the bboxes */
  107. for (int i = 0; i < list_bounding_box_->size(); i++) {
  108. penStyle = Qt::SolidLine;
  109. int labelID = list_bounding_box_->at(i)->label_ID_;
  110. /* setting color for the label of current bbox */
  111. if (labelID < list_label_color_->count())
  112. aPen->setColor(QColor(list_label_color_->at(labelID)));
  113. /* in case there is no color for such label */
  114. else
  115. aPen->setColor(QColor(Qt::white));
  116. /* checking whether labeled area is of main label or not */
  117. if (labelID == *main_label_)
  118. width = 3;
  119. else
  120. width = 2;
  121. /* changing the line style and width if current area is selected(focused) */
  122. if (RectFigure == focused_selection_type_ &&
  123. focused_selection_ == i) {
  124. penStyle = Qt::DotLine;
  125. width = 3;
  126. }
  127. /* scaling */
  128. QRect rect = list_bounding_box_->at(i)->rect.normalized();
  129. QPoint topLeft = rect.topLeft() * scale_;
  130. QPoint bottomRight = rect.bottomRight() * scale_;
  131. rect.setTopLeft(topLeft);
  132. rect.setBottomRight(bottomRight);
  133. if (focused_selection_ == i &&
  134. focused_selection_type_ == RectFigure) {
  135. QPen circPen;
  136. circPen.setWidth(2);
  137. circPen.setStyle(Qt::SolidLine);
  138. circPen.setColor(aPen->color());
  139. aPainter->setPen(circPen);
  140. for (int j = 0; j < 4; j++) {
  141. QPoint point;
  142. /* getting the number of point mouse pointer hovered on */
  143. if (!j) {
  144. point = rect.topLeft();
  145. }
  146. else if (1 == j)
  147. {
  148. point = rect.topRight();
  149. }
  150. else if (2 == j)
  151. {
  152. point = rect.bottomRight();
  153. }
  154. else if (3 == j)
  155. {
  156. point = rect.bottomLeft();
  157. }
  158. /* if current point is hovered then fill it */
  159. if (i == hovered_point_.figureID &&
  160. j == hovered_point_.pointID &&
  161. RectFigure == hovered_point_.figure) {
  162. QBrush brush;
  163. brush.setColor(aPen->color());
  164. brush.setStyle(Qt::SolidPattern);
  165. aPainter->setBrush(brush);
  166. }
  167. aPainter->drawEllipse(point, point_radius_, point_radius_);
  168. aPainter->setBrush(Qt::NoBrush);
  169. }
  170. }
  171. aPen->setWidth(width);
  172. aPen->setStyle(penStyle);
  173. aPainter->setPen(*aPen);
  174. aPainter->drawRect(rect);
  175. /* drawing label ids of these boxes */
  176. QString labelIDText =
  177. QString("%1").arg(labelID);
  178. aPainter->drawText(
  179. rect.left() + 5,
  180. rect.top() + 5,
  181. 20,
  182. 20,
  183. Qt::AlignLeft,
  184. labelIDText
  185. );
  186. }
  187. }
  188. //! draws only confirmed polygons
  189. /*!
  190. * parameters of polygons may vary depending on whether poly is selected or not or
  191. * whether it's label is main or not.
  192. */
  193. void
  194. ImageHolder::drawPolygons(
  195. QPainter *aPainter,
  196. QPen *aPen
  197. )
  198. {
  199. if (0 == list_polygon_)
  200. {
  201. return;
  202. /* NOTREACHED */
  203. }
  204. Qt::PenStyle penStyle = Qt::SolidLine;
  205. /* default width is hardcoded */
  206. int width = 2;
  207. /* drawing all the polygons */
  208. for (int i = 0; i < list_polygon_->size(); i++) {
  209. penStyle = Qt::SolidLine;
  210. int labelID = list_polygon_->at(i)->label_ID_;
  211. /* setting color for the label of current bbox */
  212. if (labelID < list_label_color_->count())
  213. aPen->setColor(QColor(list_label_color_->at(labelID)));
  214. /* in case there is no color for such label */
  215. else
  216. aPen->setColor(QColor(Qt::white));
  217. /* checking whether labeled area is of main object or not */
  218. if (labelID == *main_label_)
  219. width = 3;
  220. else
  221. width = 2;
  222. /* changing the line style and width if current area is selected(focused) */
  223. if (PolyFigure == focused_selection_type_ &&
  224. focused_selection_ == i) {
  225. penStyle = Qt::DotLine;
  226. width = 3;
  227. }
  228. QPoint point;
  229. QPolygon poly = list_polygon_->at(i)->poly;
  230. for (int j = 0; j < poly.size(); j++) {
  231. point.setX(poly.at(j).x());
  232. point.setY(poly.at(j).y());
  233. /* scaling */
  234. point *= scale_;
  235. poly.remove(j);
  236. poly.insert(j, point);
  237. /* in case if it's focused */
  238. if (focused_selection_ == i &&
  239. focused_selection_type_ == PolyFigure) {
  240. QPen circPen;
  241. circPen.setWidth(2);
  242. circPen.setStyle(Qt::SolidLine);
  243. circPen.setColor(aPen->color());
  244. aPainter->setPen(circPen);
  245. /* filling the point if it is hovered */
  246. if (j == hovered_point_.pointID &&
  247. i == hovered_point_.figureID &&
  248. PolyFigure == hovered_point_.figure) {
  249. QBrush brush;
  250. brush.setColor(aPen->color());
  251. brush.setStyle(Qt::SolidPattern);
  252. aPainter->setBrush(brush);
  253. }
  254. aPainter->drawEllipse(point, point_radius_, point_radius_);
  255. aPainter->setBrush(Qt::NoBrush);
  256. }
  257. }
  258. aPen->setWidth(width);
  259. aPen->setStyle(penStyle);
  260. aPainter->setPen(*aPen);
  261. aPainter->drawPolygon(poly);
  262. /* drawing label IDs of these polygons */
  263. QString labelIDText =
  264. QString("%1").arg(labelID);
  265. QRect rect = poly.boundingRect();
  266. int x = rect.center().x();
  267. int y = rect.center().y();
  268. aPainter->drawText(
  269. x,
  270. y,
  271. 20,
  272. 20,
  273. Qt::AlignHCenter,
  274. labelIDText
  275. );
  276. }
  277. }
  278. //! Changes current state and setting new coordinates for the bbox
  279. /*!
  280. * \see mouseMoveEvent(QMouseEvent *anEvent)
  281. * \param[in] aNewPos a second point for the bbox(rect)
  282. * \param[in] anOldPos a first point for the bbox
  283. * \param[in,out] aNewRect a pointer to the rectangle
  284. * which is being currently changed
  285. */
  286. void
  287. ImageHolder::triggerBoundBox(
  288. const QPoint &aNewPos,
  289. const QPoint &anOldPos,
  290. QRect *aNewRect
  291. )
  292. {
  293. aNewRect->setCoords(
  294. anOldPos.x(),
  295. anOldPos.y(),
  296. aNewPos.x(),
  297. aNewPos.y()
  298. );
  299. state_ = NewSelection;
  300. repaint_needed_ = 1;
  301. }
  302. //! \brief Changes current state and adding a new point to the
  303. //! current not confirmed polygon
  304. /*!
  305. * \see mouseMoveEvent(QMouseEvent *anEvent)
  306. * \param[in] aPoint a point going to be added to the polygon
  307. * \param[in,out] aNewPoly a pointer to the polygon
  308. * which is being currently changed
  309. */
  310. void
  311. ImageHolder::triggerPolygon(
  312. const QPoint &aPoint,
  313. QPolygon *aNewPoly
  314. )
  315. {
  316. *aNewPoly << aPoint;
  317. repaint_needed_ = 1;
  318. }
  319. //! \brief Puts focus on some of the selections(selected areas)
  320. /*!
  321. * \param[in] anItem should be a pointer to the item of list_areas_ which
  322. * has a string with certain format
  323. * \see ImageLabeler::addPoly(Polygon *poly)
  324. * \see ImageLabeler::list_areas_
  325. * \see focused_selection_
  326. * \see focused_selection_type_
  327. * Parses the string from anItem and gets the selected object out of it
  328. */
  329. void
  330. ImageHolder::focusOnArea(QListWidgetItem *anItem)
  331. {
  332. QString text = anItem->text();
  333. Tool tool = NoTool;
  334. if (-1 != text.indexOf("BBox"))
  335. tool = BoundingBoxTool;
  336. else if (-1 != text.indexOf("Poly"))
  337. tool = PolygonTool;
  338. /* looking for a number of selected area */
  339. if (NoTool != tool) {
  340. bool ok = 0;
  341. focused_selection_ = getNumFromString(&text, "#", ";", &ok);
  342. if (!ok) {
  343. focused_selection_ = -1;
  344. focused_selection_type_ = NoFigure;
  345. return;
  346. /* NOTREACHED */
  347. }
  348. switch (tool) {
  349. case BoundingBoxTool:
  350. focused_selection_type_ = RectFigure;
  351. break;
  352. case PolygonTool:
  353. focused_selection_type_ = PolyFigure;
  354. break;
  355. default:
  356. focused_selection_type_ = NoFigure;
  357. break;
  358. }
  359. }
  360. update();
  361. }
  362. //! Changes scale_ variable depending on zoom direction and scale factor
  363. /*!
  364. * \param[in] aDirection an enum indicating the zoom direction
  365. * \param[in] scaleFactor a speed of zooming
  366. */
  367. void
  368. ImageHolder::scaleImage(
  369. ZoomDirection aDirection,
  370. double scaleFactor
  371. )
  372. {
  373. QSize size = this->size();
  374. /* zoomin */
  375. if (ZoomIn == aDirection) {
  376. size *= scaleFactor;
  377. scale_ *= scaleFactor;
  378. }
  379. /* zoomout */
  380. else if (ZoomOut == aDirection) {
  381. size /= scaleFactor;
  382. scale_ /= scaleFactor;
  383. }
  384. this->resize(size);
  385. }
  386. //! Removes the focus on an object
  387. /*!
  388. * \see paintEvent(QPaintEvent *)
  389. */
  390. void
  391. ImageHolder::clearFocusOnArea()
  392. {
  393. focused_selection_ = -1;
  394. focused_selection_type_ = NoFigure;
  395. }
  396. //! Clears the data of hovered_point_
  397. void
  398. ImageHolder::clearHoveredPoint()
  399. {
  400. hovered_point_.figure = NoFigure;
  401. hovered_point_.figureID = -1;
  402. hovered_point_.pointID = -1;
  403. }
  404. //! Sets current tool
  405. void
  406. ImageHolder::setTool(Tool aTool)
  407. {
  408. switch(aTool) {
  409. case BoundingBoxTool:
  410. tool_ = BoundingBoxTool;
  411. break;
  412. case PolygonTool:
  413. tool_ = PolygonTool;
  414. break;
  415. default:
  416. tool_ = NoTool;
  417. break;
  418. }
  419. }
  420. //! Sets a pointer on the ImageLabeler::list_bounding_box_
  421. void
  422. ImageHolder::setBoundingBoxList(QList< BoundingBox * > *aBBoxList)
  423. {
  424. if (0 == aBBoxList) {
  425. return;
  426. /* NOTREACHED */
  427. }
  428. list_bounding_box_ = aBBoxList;
  429. }
  430. //! Sets a pointer on the ImageLabeler::list_polygon_
  431. void
  432. ImageHolder::setPolygonList(QList< Polygon * > *aPolygonList)
  433. {
  434. if (0 == aPolygonList) {
  435. return;
  436. /* NOTREACHED */
  437. }
  438. list_polygon_ = aPolygonList;
  439. }
  440. //! Sets a pointer on the ImageLabeler::list_label_color
  441. void
  442. ImageHolder::setLabelColorList(QList< uint > *aLabelColorList)
  443. {
  444. if (0 == aLabelColorList) {
  445. return;
  446. /* NOTREACHED */
  447. }
  448. list_label_color_ = aLabelColorList;
  449. }
  450. //! Sets a pointer on the ImageLabeler::main_label_
  451. void
  452. ImageHolder::setMainLabelNum(int *aNum)
  453. {
  454. if (0 == aNum) {
  455. return;
  456. /* NOTREACHED */
  457. }
  458. main_label_ = aNum;
  459. }
  460. //! Sets a pointer on the ImageLabeler::image_
  461. void
  462. ImageHolder::setImage(QPixmap *anImage)
  463. {
  464. if (0 == anImage) {
  465. return;
  466. /* NOTREACHED */
  467. }
  468. image_ = anImage;
  469. }
  470. //! Clears scale, state, bounding_box_ and polygon_
  471. void
  472. ImageHolder::clearAll()
  473. {
  474. //list_bounding_box_->clear();
  475. bounding_box_.rect.setRect(-1, -1, 0, 0);
  476. //list_polygon_->clear();
  477. polygon_.poly.clear();
  478. clearFocusOnArea();
  479. state_ = StandBy;
  480. scale_ = 1;
  481. update();
  482. }
  483. //! Clears las selection(selected area)
  484. void
  485. ImageHolder::clearLast()
  486. {
  487. switch (tool_) {
  488. case BoundingBoxTool:
  489. bounding_box_.rect.setRect(-1, -1, 0, 0);
  490. break;
  491. case PolygonTool:
  492. polygon_.poly.clear();
  493. break;
  494. default:
  495. break;
  496. }
  497. bounding_box_.rect.setRect(-1, -1, 0, 0);
  498. state_ = StandBy;
  499. update();
  500. }
  501. //! Moves bounding_box_ or polygon_ to the corresponding list
  502. /*!
  503. * \see ImageLabeler::confirmSelection()
  504. */
  505. void
  506. ImageHolder::confirmSelection()
  507. {
  508. if (NewSelection != state_ || NoTool == tool_) {
  509. return;
  510. /* NOTREACHED */
  511. }
  512. if (BoundingBoxTool == tool_) {
  513. BoundingBox *bbox = new BoundingBox;
  514. bounding_box_.rect = bounding_box_.rect.normalized();
  515. *bbox = bounding_box_;
  516. list_bounding_box_->append(bbox);
  517. bounding_box_.rect.setRect(-1, -1, 0, 0);
  518. }
  519. else if (PolygonTool == tool_) {
  520. Polygon *poly = new Polygon;
  521. *poly = polygon_;
  522. list_polygon_->append(poly);
  523. polygon_.poly.clear();
  524. }
  525. list_poly_history_.clear();
  526. state_ = StandBy;
  527. update();
  528. }
  529. //! returns state
  530. ImageHolder::State
  531. ImageHolder::state()
  532. {
  533. return state_;
  534. }
  535. //! returns tool
  536. ImageHolder::Tool
  537. ImageHolder::tool()
  538. {
  539. return tool_;
  540. }
  541. //! returns focused_selection_
  542. int
  543. ImageHolder::focusedSelection()
  544. {
  545. return focused_selection_;
  546. }
  547. //! returns focused_selection_type_
  548. Figure
  549. ImageHolder::focusedSelectionType()
  550. {
  551. return focused_selection_type_;
  552. }
  553. //! removes last added point form the polygon_
  554. void
  555. ImageHolder::undo()
  556. {
  557. if (PolygonTool == tool_ &&
  558. NewSelection == state_ &&
  559. !polygon_.poly.isEmpty())
  560. {
  561. list_poly_history_.append(polygon_.poly.last());
  562. polygon_.poly.pop_back();
  563. repaint_needed_ = 1;
  564. }
  565. if (repaint_needed_) {
  566. update();
  567. repaint_needed_ = 0;
  568. }
  569. }
  570. //! Brings back the last removed by undo() point
  571. void
  572. ImageHolder::redo()
  573. {
  574. if (PolygonTool == tool_ &&
  575. NewSelection == state_ &&
  576. !list_poly_history_.isEmpty())
  577. {
  578. polygon_.poly.append(list_poly_history_.last());
  579. list_poly_history_.pop_back();
  580. repaint_needed_ = 1;
  581. }
  582. if (repaint_needed_) {
  583. update();
  584. repaint_needed_ = 0;
  585. }
  586. }
  587. //! Checks whether mouse pointer is on some point of any object or not
  588. /*!
  589. * \see hovered_point_
  590. * \see drawBoundingBoxes(QPainter *aPainter, QPen *aPen)
  591. * \see drawPolygons(QPainter *aPainter, QPen *aPen)
  592. *
  593. * It simply checks all the points of all objects if mouse
  594. * pointer is hovered above any of them
  595. */
  596. void
  597. ImageHolder::checkForPoints(QPoint *aPos)
  598. {
  599. if ((!list_polygon_->count() &&
  600. !list_bounding_box_->count()) ||
  601. !aPos) {
  602. return;
  603. /* NOTREACHED */
  604. }
  605. int newRadius = 0;
  606. int x = aPos->x();
  607. int y = aPos->y();
  608. /* center coordinates */
  609. int xc = 0;
  610. int yc = 0;
  611. for (int i = 0; i < list_polygon_->count(); i++) {
  612. QPolygon poly = list_polygon_->at(i)->poly;
  613. for (int j = 0; j < poly.count(); j++) {
  614. xc = poly.at(j).x();
  615. yc = poly.at(j).y();
  616. newRadius = qSqrt(qPow(x - xc, 2) + qPow(y - yc, 2));
  617. if (newRadius <= point_radius_) {
  618. hovered_point_.figure = PolyFigure;
  619. hovered_point_.figureID = i;
  620. hovered_point_.pointID = j;
  621. repaint_needed_ = 1;
  622. return;
  623. /* NOTREACHED */
  624. }
  625. }
  626. }
  627. for (int i = 0; i < list_bounding_box_->count(); i++) {
  628. QRect rect = list_bounding_box_->at(i)->rect;
  629. for (int j = 0; j < 4; j++) {
  630. if (!j) {
  631. xc = rect.left();
  632. yc = rect.top();
  633. }
  634. else if (1 == j)
  635. {
  636. xc = rect.right();
  637. yc = rect.top();
  638. }
  639. else if (2 == j)
  640. {
  641. xc = rect.right();
  642. yc = rect.bottom();
  643. }
  644. else if (3 == j)
  645. {
  646. xc = rect.left();
  647. yc = rect.bottom();
  648. }
  649. newRadius = qSqrt(qPow(x - xc, 2) + qPow(y - yc, 2));
  650. if (newRadius <= point_radius_) {
  651. hovered_point_.figure = RectFigure;
  652. hovered_point_.figureID = i;
  653. hovered_point_.pointID = j;
  654. repaint_needed_ = 1;
  655. return;
  656. /* NOTREACHED */
  657. }
  658. }
  659. }
  660. hovered_point_.figure = NoFigure;
  661. hovered_point_.figureID = -1;
  662. hovered_point_.pointID = -1;
  663. repaint_needed_ = 1;
  664. }
  665. void
  666. ImageHolder::keyPressEvent(QKeyEvent *anEvent)
  667. {
  668. QLabel::keyPressEvent(anEvent);
  669. }
  670. //! Event is automatically called on every mouse move
  671. /*!
  672. * With the help of this event all the mouse movements are being tracked.
  673. */
  674. void
  675. ImageHolder::mouseMoveEvent(QMouseEvent *anEvent)
  676. {
  677. QPoint pos = anEvent->pos() / scale_;
  678. if (anEvent->pos().x() < 0)
  679. pos.setX(0);
  680. if (width() < anEvent->pos().x())
  681. pos.setX(width() - 1);
  682. if (anEvent->pos().y() < 0)
  683. pos.setY(0);
  684. if (height() < anEvent->pos().y())
  685. pos.setY(height() - 1);
  686. if ((anEvent->buttons() & Qt::LeftButton) &&
  687. BoundingBoxTool == tool_ &&
  688. NewSelection == state_ &&
  689. Qt::NoModifier == keyboard_modifier_)
  690. {
  691. triggerBoundBox(pos, prev_cursor_pos_, &(bounding_box_.rect));
  692. }
  693. if (PolygonTool == tool_ &&
  694. NewSelection == state_ &&
  695. (anEvent->buttons() & Qt::LeftButton))
  696. {
  697. polygon_.poly.setPoint(polygon_.poly.count() - 1, pos);
  698. //triggerPolygon(pos, &(polygon_.poly));
  699. repaint_needed_ = 1;
  700. }
  701. if (-1 != focused_selection_ &&
  702. !(anEvent->buttons() & Qt::LeftButton)) {
  703. checkForPoints(&pos);
  704. }
  705. /* editing polygons */
  706. if (-1 != hovered_point_.figureID &&
  707. !list_polygon_->isEmpty() &&
  708. PolyFigure == hovered_point_.figure &&
  709. (anEvent->buttons() & Qt::LeftButton) &&
  710. hovered_point_.figureID == focused_selection_)
  711. {
  712. Polygon *poly = list_polygon_->at(hovered_point_.figureID);
  713. poly->poly.setPoint(hovered_point_.pointID, pos);
  714. repaint_needed_ = 1;
  715. }
  716. /* editing bounding boxes */
  717. if (-1 != hovered_point_.figureID &&
  718. !list_bounding_box_->isEmpty() &&
  719. RectFigure == hovered_point_.figure &&
  720. (anEvent->buttons() & Qt::LeftButton))
  721. {
  722. BoundingBox *rect = list_bounding_box_->at(hovered_point_.figureID);
  723. if (0 == hovered_point_.pointID)
  724. rect->rect.setTopLeft(pos);
  725. else if (1 == hovered_point_.pointID)
  726. rect->rect.setTopRight(pos);
  727. else if (2 == hovered_point_.pointID)
  728. rect->rect.setBottomRight(pos);
  729. else if (3 == hovered_point_.pointID)
  730. rect->rect.setBottomLeft(pos);
  731. repaint_needed_ = 1;
  732. }
  733. if (repaint_needed_) {
  734. update();
  735. repaint_needed_ = 0;
  736. }
  737. }
  738. //! Event is automatically called on every mouse click
  739. /*!
  740. * Every time mouse was clicked it's position is stored in the prev_cursor_pos_
  741. * Depending on current state and selected tool it creates new points or
  742. * starts new rects
  743. */
  744. void
  745. ImageHolder::mousePressEvent(QMouseEvent *anEvent)
  746. {
  747. /* remembering coordinates of the click */
  748. if ((anEvent->buttons() & Qt::LeftButton) ||
  749. (anEvent->buttons() & Qt::RightButton))
  750. {
  751. prev_cursor_pos_ = anEvent->pos() / scale_;
  752. }
  753. QPoint pos = anEvent->pos() / scale_;
  754. if (anEvent->buttons() & Qt::LeftButton) {
  755. /* clearing the selected area if it is not confirmed */
  756. if (NewSelection == state_ && BoundingBoxTool == tool_) {
  757. bounding_box_.rect.setRect(-1, -1, 0, 0);
  758. state_ = StandBy;
  759. }
  760. /* making new points for poly */
  761. if (PolygonTool == tool_ &&
  762. NewSelection == state_ &&
  763. Qt::NoModifier == keyboard_modifier_)
  764. {
  765. triggerPolygon(pos, &(polygon_.poly));
  766. }
  767. /* starting new selection by click */
  768. if (StandBy == state_ && NoTool != tool_ &&
  769. -1 == focused_selection_) {
  770. state_ = NewSelection;
  771. emit selectionStarted();
  772. polygon_.poly.clear();
  773. if (PolygonTool == tool_) {
  774. polygon_.poly << prev_cursor_pos_;
  775. }
  776. }
  777. }
  778. if (repaint_needed_) {
  779. update();
  780. repaint_needed_ = 0;
  781. }
  782. }
  783. //! Event is automatically called on every mouse release
  784. void
  785. ImageHolder::mouseReleaseEvent(QMouseEvent *anEvent)
  786. {
  787. Q_UNUSED(anEvent)
  788. if (-1 != hovered_point_.figureID)
  789. emit areaEdited();
  790. if (RectFigure == hovered_point_.figure &&
  791. -1 != hovered_point_.figureID &&
  792. !list_bounding_box_->
  793. at(hovered_point_.figureID)->
  794. rect.isValid()
  795. )
  796. {
  797. BoundingBox *rect = list_bounding_box_->at(hovered_point_.figureID);
  798. rect->rect = rect->rect.normalized();
  799. }
  800. }
  801. /*
  802. *
  803. */