ImageHolder.cpp 9.1 KB

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