SimpleSelector.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimagedisplay - A library for image and video display
  4. * See file License for license information.
  5. */
  6. #include <core/image/RectangleT.h>
  7. #include <core/image/CrossT.h>
  8. #include <core/imagedisplay/OverlayColors.h>
  9. #include <core/basics/stringutils.h>
  10. #include "SimpleSelector.h"
  11. using namespace std;
  12. namespace NICE {
  13. SimpleSelector::SimpleSelector ( bool drawRectangle,
  14. bool drawCross, int maxColors,
  15. QWidget *parent, const char *name, Qt::WFlags flags)
  16. : ImageDisplay ( parent, name, flags ),
  17. m_markColor (1),
  18. m_maxColors(maxColors),
  19. m_drawRectangle(drawRectangle), m_drawCross(drawCross)
  20. {
  21. connect(this, SIGNAL(rectSelect(float,float,float,float)),
  22. this, SLOT(rectSelect(float,float,float,float)));
  23. setDrawSelectionRect ( m_drawRectangle );
  24. m_crossSize = 10;
  25. }
  26. void SimpleSelector::rectSelect(float left, float top, float right, float bottom)
  27. {
  28. RectT<double> r ( CoordT<float>(left, top), CoordT<float>( right, bottom) );
  29. m_rectangles.push_back ( r );
  30. m_rectanglesColor.push_back ( m_markColor );
  31. updateGL();
  32. }
  33. void SimpleSelector::addExtraMenuItems ( Q3PopupMenu *popupMenu )
  34. {
  35. colorMenuMap.clear();
  36. for ( int color = 1 ; color <= m_maxColors ; color++ )
  37. {
  38. string caption = "Color/Class " + itostr(color);
  39. int id = popupMenu->insertItem ( QString(caption.c_str()), this, SLOT(dummy()) );
  40. colorMenuMap.insert ( pair<int, int> ( id, color ) );
  41. }
  42. connect (popupMenu, SIGNAL(activated(int)), this, SLOT(menuActivated(int)));
  43. popupMenu->insertSeparator();
  44. }
  45. void SimpleSelector::dummy(void)
  46. {
  47. }
  48. void SimpleSelector::menuActivated ( int id )
  49. {
  50. map<int, int>::const_iterator i = colorMenuMap.find(id);
  51. if ( i != colorMenuMap.end() )
  52. setCurrentColor(i->second);
  53. }
  54. void SimpleSelector::setCurrentColor ( int color )
  55. {
  56. m_markColor = color;
  57. }
  58. void SimpleSelector::paintGLObjects(void)
  59. {
  60. int index = 0;
  61. for ( vector<RectT<double> >::const_iterator i = m_rectangles.begin();
  62. i != m_rectangles.end(); i++,index++ )
  63. {
  64. int rColor = m_rectanglesColor[index];
  65. int mark_r = overlayColorTable[ rColor % overlayColorTableNumEntries ][0];
  66. int mark_g = overlayColorTable[ rColor % overlayColorTableNumEntries ][1];
  67. int mark_b = overlayColorTable[ rColor % overlayColorTableNumEntries ][2];
  68. const RectT<double> & r = *i;
  69. double left = r.left * width() / image->width();
  70. double right = r.right() * width() / image->width();
  71. double top = r.top * height() / image->height();
  72. double bottom = r.bottom() * height() / image->height();
  73. if ( m_drawRectangle )
  74. {
  75. glLineWidth(1);
  76. glColor4f(mark_r, mark_g, mark_b, 1);
  77. glBegin(GL_LINE_LOOP);
  78. glVertex2f(left, height() - top);
  79. glVertex2f(left, height() - bottom);
  80. glVertex2f(right, height() - bottom);
  81. glVertex2f(right, height() - top);
  82. glEnd();
  83. glFlush();
  84. }
  85. if ( m_drawCross )
  86. {
  87. glLineWidth(1);
  88. glColor4f(mark_r, mark_g, mark_b, 1);
  89. glBegin(GL_LINES);
  90. glVertex2f(left, height() - top - m_crossSize );
  91. glVertex2f(left, height() - top + m_crossSize );
  92. glVertex2f(left - m_crossSize, height() - top );
  93. glVertex2f(left + m_crossSize, height() - top );
  94. glEnd();
  95. glFlush();
  96. }
  97. }
  98. }
  99. SimpleSelector::~SimpleSelector ()
  100. {
  101. }
  102. void SimpleSelector::addRectangle ( const NICE::RectT<double> & rectangle, int color )
  103. {
  104. m_rectangles.push_back ( rectangle );
  105. m_rectanglesColor.push_back ( color );
  106. }
  107. };