SimpleSelector.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ( QMenu *popupMenu )
  34. {
  35. colorMenuMap.clear();
  36. for ( int color = 1 ; color <= m_maxColors ; color++ )
  37. {
  38. string caption = "Color/Class " + itostr(color);
  39. QAction* action = popupMenu->addAction ( QString(caption.c_str()), this, SLOT(dummy()) );
  40. colorMenuMap.insert ( pair<QAction*, int> ( action, color ) );
  41. }
  42. connect (popupMenu, SIGNAL(activated(int)), this, SLOT(menuActivated(int)));
  43. popupMenu->addSeparator();
  44. }
  45. void SimpleSelector::dummy(void)
  46. {
  47. }
  48. void SimpleSelector::menuActivated (QAction* action)
  49. {
  50. map<QAction*, int>::const_iterator i = colorMenuMap.find(action);
  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. #ifdef NICE_USELIB_OPENGL
  61. int index = 0;
  62. for ( vector<RectT<double> >::const_iterator i = m_rectangles.begin();
  63. i != m_rectangles.end(); i++,index++ )
  64. {
  65. int rColor = m_rectanglesColor[index];
  66. int mark_r = overlayColorTable[ rColor % overlayColorTableNumEntries ][0];
  67. int mark_g = overlayColorTable[ rColor % overlayColorTableNumEntries ][1];
  68. int mark_b = overlayColorTable[ rColor % overlayColorTableNumEntries ][2];
  69. const RectT<double> & r = *i;
  70. double left = r.left * width() / image->width();
  71. double right = r.right() * width() / image->width();
  72. double top = r.top * height() / image->height();
  73. double bottom = r.bottom() * height() / image->height();
  74. if ( m_drawRectangle )
  75. {
  76. glLineWidth(1);
  77. glColor4f(mark_r, mark_g, mark_b, 1);
  78. glBegin(GL_LINE_LOOP);
  79. glVertex2f(left, height() - top);
  80. glVertex2f(left, height() - bottom);
  81. glVertex2f(right, height() - bottom);
  82. glVertex2f(right, height() - top);
  83. glEnd();
  84. glFlush();
  85. }
  86. if ( m_drawCross )
  87. {
  88. glLineWidth(1);
  89. glColor4f(mark_r, mark_g, mark_b, 1);
  90. glBegin(GL_LINES);
  91. glVertex2f(left, height() - top - m_crossSize );
  92. glVertex2f(left, height() - top + m_crossSize );
  93. glVertex2f(left - m_crossSize, height() - top );
  94. glVertex2f(left + m_crossSize, height() - top );
  95. glEnd();
  96. glFlush();
  97. }
  98. }
  99. #else
  100. fthrow(Exception,"OpenGL lib not availabe, recompile using OpenGL!");
  101. #endif
  102. }
  103. SimpleSelector::~SimpleSelector ()
  104. {
  105. }
  106. void SimpleSelector::addRectangle ( const NICE::RectT<double> & rectangle, int color )
  107. {
  108. m_rectangles.push_back ( rectangle );
  109. m_rectanglesColor.push_back ( color );
  110. }
  111. };