ImageHolder.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 <QDebug>
  14. ImageHolder::ImageHolder(QWidget *aParent)
  15. : QLabel(aParent)
  16. {
  17. repaint_needed_ = 0;
  18. tool_ = NoTool;
  19. state_ = StandBy;
  20. keyboard_modifier_ = Qt::NoModifier;
  21. focused_selection_ = -1;
  22. focused_selection_type_ = NoTool;
  23. list_bounding_box_ = 0;
  24. main_label_ = 0;
  25. //list_bounding_box_ = new QList< QRect >;
  26. scale_ = 1;
  27. setScaledContents(true);
  28. }
  29. ImageHolder::~ImageHolder()
  30. {
  31. //list_bounding_box_->clear();
  32. //delete list_bounding_box_;
  33. }
  34. void
  35. ImageHolder::paintEvent(QPaintEvent *anEvent)
  36. {
  37. QLabel::paintEvent(anEvent);
  38. QPainter painter(this);
  39. painter.setRenderHint(QPainter::Antialiasing);
  40. //painter.setRenderHint(QPainter::SmoothPixmapTransform);
  41. QPen pen;
  42. if (NoTool != tool_) {
  43. pen.setWidth(1);
  44. pen.setColor(QColor(Qt::black));
  45. pen.setStyle(Qt::DashLine);
  46. painter.setPen(pen);
  47. switch(tool_) {
  48. case BoundingBoxTool:
  49. painter.drawRect(bounding_box_.rect);
  50. break;
  51. case PolygonTool:
  52. painter.drawPolygon(polygon_.poly);
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. /* drawing bounding boxes */
  59. drawBoundingBoxes(&painter, &pen);
  60. drawPolygons(&painter, &pen);
  61. }
  62. void
  63. ImageHolder::drawBoundingBoxes(
  64. QPainter *aPainter,
  65. QPen *aPen
  66. )
  67. {
  68. /* FIXME: hardcoded colors */
  69. if (0 == list_bounding_box_)
  70. {
  71. return;
  72. /* NOTREACHED */
  73. }
  74. Qt::PenStyle penStyle = Qt::SolidLine;
  75. int width = 1;
  76. /* confirmed boxes */
  77. for (int i = 0; i < list_bounding_box_->size(); i++) {
  78. int labelID = list_bounding_box_->at(i).label_ID_;
  79. aPen->setColor(QColor(list_label_color_->at(labelID)));
  80. /* checking whether labeled area is of main object or not */
  81. if (labelID == *main_label_)
  82. width = 3;
  83. else
  84. width = 1;
  85. if (BoundingBoxTool == focused_selection_type_ &&
  86. focused_selection_ == i) {
  87. penStyle = Qt::DotLine;
  88. width = 2;
  89. }
  90. aPen->setWidth(width);
  91. aPen->setStyle(penStyle);
  92. aPainter->setPen(*aPen);
  93. aPainter->drawRect(list_bounding_box_->at(i).rect);
  94. /* drawing label ids of these boxes */
  95. QString labelIDText =
  96. QString("%1").arg(labelID);
  97. QRect rect = list_bounding_box_->at(i).rect.normalized();
  98. aPainter->drawText(
  99. rect.left() + 5,
  100. rect.top() + 5,
  101. 20,
  102. 20,
  103. Qt::AlignLeft,
  104. labelIDText
  105. );
  106. }
  107. }
  108. void
  109. ImageHolder::drawPolygons(
  110. QPainter *aPainter,
  111. QPen *aPen
  112. )
  113. {
  114. /* FIXME: hardcoded colors */
  115. if (0 == list_polygon_)
  116. {
  117. return;
  118. /* NOTREACHED */
  119. }
  120. Qt::PenStyle penStyle = Qt::SolidLine;
  121. int width = 1;
  122. /* confirmed polygons */
  123. for (int i = 0; i < list_polygon_->size(); i++) {
  124. penStyle = Qt::SolidLine;
  125. int labelID = list_polygon_->at(i).label_ID_;
  126. aPen->setColor(QColor(list_label_color_->at(labelID)));
  127. /* checking whether labeled area is of main object or not */
  128. if (labelID == *main_label_)
  129. width = 3;
  130. else
  131. width = 1;
  132. if (PolygonTool == focused_selection_type_ &&
  133. focused_selection_ == i) {
  134. penStyle = Qt::DotLine;
  135. width = 2;
  136. }
  137. aPen->setWidth(width);
  138. aPen->setStyle(penStyle);
  139. aPainter->setPen(*aPen);
  140. aPainter->drawPolygon(list_polygon_->at(i).poly);
  141. /* drawing label ids of these polys */
  142. QString labelIDText =
  143. QString("%1").arg(labelID);
  144. QRect rect = list_polygon_->at(i).poly.boundingRect();
  145. int x = rect.center().x();
  146. int y = rect.center().y();
  147. aPainter->drawText(
  148. x,
  149. y,
  150. 20,
  151. 20,
  152. Qt::AlignHCenter,
  153. labelIDText
  154. );
  155. }
  156. }
  157. void
  158. ImageHolder::triggerBoundBox(
  159. const QPoint &aNewPos,
  160. const QPoint &anOldPos,
  161. QRect *aNewRect
  162. )
  163. {
  164. aNewRect->setCoords(
  165. anOldPos.x(),
  166. anOldPos.y(),
  167. aNewPos.x(),
  168. aNewPos.y()
  169. );
  170. state_ = NewSelection;
  171. repaint_needed_ = 1;
  172. }
  173. void
  174. ImageHolder::triggerPolygon(
  175. const QPoint &aPoint,
  176. QPolygon *aNewPoly
  177. )
  178. {
  179. *aNewPoly << aPoint;
  180. repaint_needed_ = 1;
  181. }
  182. void
  183. ImageHolder::focusOnArea(QListWidgetItem *anItem)
  184. {
  185. QString text = anItem->text();
  186. Tool tool = NoTool;
  187. if (-1 != text.indexOf("BBox"))
  188. tool = BoundingBoxTool;
  189. else if (-1 != text.indexOf("Poly"))
  190. tool = PolygonTool;
  191. /* looking for a number of selected area */
  192. if (NoTool != tool) {
  193. bool ok = 0;
  194. focused_selection_ = getNumFromString(&text, "#", ";", &ok);
  195. if (!ok) {
  196. focused_selection_ = -1;
  197. focused_selection_type_ = NoTool;
  198. return;
  199. /* NOTREACHED */
  200. }
  201. switch (tool) {
  202. case BoundingBoxTool:
  203. focused_selection_type_ = BoundingBoxTool;
  204. break;
  205. case PolygonTool:
  206. focused_selection_type_ = PolygonTool;
  207. break;
  208. default:
  209. focused_selection_type_ = NoTool;
  210. break;
  211. }
  212. }
  213. update();
  214. }
  215. void
  216. ImageHolder::scaleImage(
  217. ZoomDirection aDirection,
  218. double scaleFactor
  219. )
  220. {
  221. QSize size = pixmap()->size();
  222. /* zoomin */
  223. if (ZoomIn == aDirection) {
  224. size *= scaleFactor;
  225. scale_ *= scaleFactor;
  226. }
  227. /* zoomout */
  228. else if (ZoomOut == aDirection) {
  229. size /= scaleFactor;
  230. scale_ /= scaleFactor;
  231. }
  232. setPixmap(
  233. pixmap()->scaled(
  234. size,
  235. Qt::IgnoreAspectRatio,
  236. Qt::SmoothTransformation
  237. )
  238. );
  239. //resize(scaleFactor * pixmap()->size());
  240. //emit imageScaled();
  241. }
  242. void
  243. ImageHolder::clearFocusOnArea()
  244. {
  245. focused_selection_ = -1;
  246. focused_selection_type_ = NoTool;
  247. }
  248. void
  249. ImageHolder::setTool(Tool aTool)
  250. {
  251. switch(aTool) {
  252. case BoundingBoxTool:
  253. tool_ = BoundingBoxTool;
  254. break;
  255. case PolygonTool:
  256. tool_ = PolygonTool;
  257. break;
  258. case TaggingTool:
  259. tool_ = TaggingTool;
  260. break;
  261. default:
  262. tool_ = NoTool;
  263. break;
  264. }
  265. }
  266. void
  267. ImageHolder::setBoundingBoxList(QList< BoundingBox > *aBBoxList)
  268. {
  269. if (0 == aBBoxList) {
  270. return;
  271. /* NOTREACHED */
  272. }
  273. list_bounding_box_ = aBBoxList;
  274. }
  275. void
  276. ImageHolder::setPolygonList(QList< Polygon > *aPolygonList)
  277. {
  278. if (0 == aPolygonList) {
  279. return;
  280. /* NOTREACHED */
  281. }
  282. list_polygon_ = aPolygonList;
  283. }
  284. void
  285. ImageHolder::setLabelColorList(QList< uint > *aLabelColorList)
  286. {
  287. if (0 == aLabelColorList) {
  288. return;
  289. /* NOTREACHED */
  290. }
  291. list_label_color_ = aLabelColorList;
  292. }
  293. void
  294. ImageHolder::setMainLabelNum(int *aNum)
  295. {
  296. if (0 == aNum) {
  297. return;
  298. /* NOTREACHED */
  299. }
  300. main_label_ = aNum;
  301. }
  302. void
  303. ImageHolder::clearAll()
  304. {
  305. //list_bounding_box_->clear();
  306. bounding_box_.rect.setRect(-1, -1, 0, 0);
  307. //list_polygon_->clear();
  308. polygon_.poly.clear();
  309. clearFocusOnArea();
  310. state_ = StandBy;
  311. update();
  312. }
  313. void
  314. ImageHolder::clearLast()
  315. {
  316. switch (tool_) {
  317. case BoundingBoxTool:
  318. bounding_box_.rect.setRect(-1, -1, 0, 0);
  319. break;
  320. case PolygonTool:
  321. polygon_.poly.clear();
  322. break;
  323. case TaggingTool:
  324. break;
  325. default:
  326. break;
  327. }
  328. bounding_box_.rect.setRect(-1, -1, 0, 0);
  329. state_ = StandBy;
  330. update();
  331. }
  332. void
  333. ImageHolder::confirmSelection()
  334. {
  335. if (NewSelection != state_ || NoTool == tool_) {
  336. return;
  337. /* NOTREACHED */
  338. }
  339. switch (tool_) {
  340. case BoundingBoxTool:
  341. list_bounding_box_->append(bounding_box_);
  342. bounding_box_.rect.setRect(-1, -1, 0, 0);
  343. break;
  344. case PolygonTool:
  345. list_polygon_->append(polygon_);
  346. polygon_.poly.clear();
  347. break;
  348. default:
  349. tool_ = NoTool;
  350. break;
  351. }
  352. state_ = StandBy;
  353. update();
  354. }
  355. ImageHolder::State
  356. ImageHolder::state()
  357. {
  358. return state_;
  359. }
  360. ImageHolder::Tool
  361. ImageHolder::tool()
  362. {
  363. return tool_;
  364. }
  365. void
  366. ImageHolder::keyPressEvent(QKeyEvent *anEvent)
  367. {
  368. QLabel::keyPressEvent(anEvent);
  369. //keyboard_modifier_ = anEvent->modifiers();
  370. if (repaint_needed_) {
  371. update();
  372. repaint_needed_ = 0;
  373. }
  374. }
  375. void
  376. ImageHolder::mouseMoveEvent(QMouseEvent *anEvent)
  377. {
  378. QPoint pos = anEvent->pos();
  379. if (anEvent->pos().x() < 0)
  380. pos.setX(0);
  381. if (width() < anEvent->pos().x())
  382. pos.setX(width() - 1);
  383. if (anEvent->pos().y() < 0)
  384. pos.setY(0);
  385. if (height() < anEvent->pos().y())
  386. pos.setY(height() - 1);
  387. if ((anEvent->buttons() & Qt::LeftButton) &&
  388. BoundingBoxTool == tool_ &&
  389. NewSelection == state_ &&
  390. Qt::NoModifier == keyboard_modifier_)
  391. {
  392. triggerBoundBox(pos, prev_cursor_pos_, &(bounding_box_.rect));
  393. }
  394. if (PolygonTool == tool_ &&
  395. NewSelection == state_) {
  396. polygon_.poly.setPoint(polygon_.poly.count() - 1, pos);
  397. repaint_needed_ = 1;
  398. }
  399. if (repaint_needed_) {
  400. update();
  401. repaint_needed_ = 0;
  402. }
  403. }
  404. void
  405. ImageHolder::mousePressEvent(QMouseEvent *anEvent)
  406. {
  407. /* remembering coordinates of the click */
  408. if ((anEvent->buttons() & Qt::LeftButton) ||
  409. (anEvent->buttons() & Qt::RightButton))
  410. {
  411. prev_cursor_pos_ = anEvent->pos();
  412. }
  413. if (anEvent->buttons() & Qt::LeftButton) {
  414. /* clearing the selected area if it is not confirmed */
  415. if (NewSelection == state_ && BoundingBoxTool == tool_) {
  416. bounding_box_.rect.setRect(-1, -1, 0, 0);
  417. state_ = StandBy;
  418. }
  419. /* making new points for poly */
  420. if (PolygonTool == tool_ &&
  421. NewSelection == state_ &&
  422. Qt::NoModifier == keyboard_modifier_)
  423. {
  424. triggerPolygon(anEvent->pos(), &(polygon_.poly));
  425. }
  426. /* starting new selection by click */
  427. if (StandBy == state_ && NoTool != tool_) {
  428. state_ = NewSelection;
  429. emit selectionStarted();
  430. polygon_.poly.clear();
  431. if (PolygonTool == tool_) {
  432. polygon_.poly << prev_cursor_pos_;
  433. }
  434. }
  435. }
  436. if (repaint_needed_) {
  437. update();
  438. repaint_needed_ = 0;
  439. }
  440. }
  441. void
  442. ImageHolder::mouseReleaseEvent(QMouseEvent *anEvent)
  443. {
  444. Q_UNUSED(anEvent)
  445. }
  446. /*
  447. *
  448. */