You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
10 KiB

7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
  1. #include "VideoThumbWidget.h"
  2. #include "VideoSourceChooser.h"
  3. #include "../dialogs/SettingsCameraDialog.h"
  4. #define LOG( X ) m_pLog->Log( "VideoThumbWidget", this, X )
  5. #define LOG_ERROR( X ) m_pLog->Log( "VideoThumbWidget", this, _LOG_ERROR, X )
  6. VideoThumbWidget::VideoThumbWidget(QWidget *parent) :
  7. QWidget(parent),
  8. basicMediaDestination(){
  9. m_bGrayScale = false;
  10. m_bEnhanced = false;
  11. m_bDirectDrawing = true;
  12. m_bUpdateIsPending = false;
  13. m_pSignalParameter = NULL;
  14. m_pSettingsDialog = NULL;
  15. setFixedSize( 240, 180 );
  16. setMouseTracking( true );
  17. m_eStreamAspectRatio = FAR_UNKNOWN;
  18. m_eWidgetAspectRatio = detectAspectRatio(width(), height());
  19. //setGeometry(m_nPosX, m_nPosY, m_nWidth+1, m_nHeight+1);
  20. //setMinimumSize(QSize(320, 240));
  21. //setSizeIncrement(QSize(20, 20));
  22. //m_backingStore = new QBackingStore( );
  23. //setBackgroundRole(QPalette::Window);
  24. setAutoFillBackground(false);
  25. setAttribute(Qt::WA_OpaquePaintEvent);
  26. setCursor(QCursor(Qt::PointingHandCursor));
  27. //create();
  28. m_pPixmap = NULL;
  29. m_pOverlayPixmap = NULL;
  30. m_pGraber = NULL;
  31. m_mFrame = NULL;
  32. m_mOutput = NULL;
  33. m_pTimer = NULL;
  34. m_bMouseActive = false;
  35. m_bSelected = false;
  36. m_bErrorMode = false;
  37. m_pErrorMsg = NULL;
  38. m_nMouseX = 0;
  39. m_nMouseY = 0;
  40. m_pPropertiesProtectorMutex = new basicMutex();
  41. m_pWidgetProtectorMutex = new basicMutex();
  42. m_pPropertiesProtectorMutex->lock();
  43. m_pWidgetProtectorMutex->lock();
  44. //this->setContextMenuPolicy(Qt::CustomContextMenu);
  45. //connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
  46. // this, SLOT(ShowContextMenu(const QPoint &)));
  47. };
  48. void VideoThumbWidget::ShowContextMenu(const QPoint &pos){
  49. QMenu contextMenu("Context menu", this);
  50. QAction action1("Togle Enhanced Mode", this);
  51. action1.setIcon(QIcon(":pictures/appbar.candle.png"));
  52. connect(&action1, &QAction::triggered, this, &VideoThumbWidget::setEnhanced);
  53. QAction action2("Togle Video B/W", this);
  54. action2.setIcon(QIcon(":pictures/appbar.alien.png"));
  55. connect(&action2, &QAction::triggered, this, &VideoThumbWidget::setGrayScale);
  56. QAction action3("Camera Settings", this);
  57. action3.setIcon(QIcon(":pictures/appbar.cog.png"));
  58. connect(&action3, &QAction::triggered, this, &VideoThumbWidget::showCameraSettings);
  59. QAction action4("Remove Camera", this);
  60. action4.setIcon(QIcon(":pictures/appbar.close.png"));
  61. connect(&action4, &QAction::triggered, this, &VideoThumbWidget::removeThisCamera);
  62. contextMenu.addAction(&action1);
  63. contextMenu.addAction(&action2);
  64. contextMenu.addAction(&action3);
  65. contextMenu.addAction(&action4);
  66. contextMenu.exec(mapToGlobal(pos));
  67. };
  68. bool VideoThumbWidget::event(QEvent *event){
  69. if (event->type() == QEvent::UpdateRequest){
  70. m_bUpdateIsPending = false;
  71. renderNow();
  72. return true;
  73. }
  74. if (event->type() == QEvent::MouseButtonRelease){
  75. //m_bSelected = (m_bSelected) ? false : true;
  76. if (((QMouseEvent*)event)->button() == Qt::RightButton){
  77. ShowContextMenu(QPoint(((QMouseEvent*)event)->x(), ((QMouseEvent*)event)->y()));
  78. } else selected( m_pSignalParameter );
  79. return true;
  80. }
  81. if (event->type() == QEvent::MouseButtonDblClick){
  82. m_bEnhanced = (m_bEnhanced) ? false : true;
  83. renderLater();
  84. return true;
  85. }
  86. if (event->type() == QEvent::MouseMove){
  87. m_nMouseX = ((QMouseEvent*)event)->x();
  88. m_nMouseY = ((QMouseEvent*)event)->y();
  89. m_bMouseActive = true;
  90. m_nMenuActivityCounter = 75;// 3 Sec
  91. };
  92. return QWidget::event(event);
  93. }
  94. //! [7]
  95. //! [6]
  96. void VideoThumbWidget::renderLater(){
  97. if (!m_bUpdateIsPending) {
  98. m_bUpdateIsPending = true;
  99. QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
  100. LOG("Delayed Rendering Request");
  101. }
  102. }
  103. //! [6]
  104. //! [5]
  105. //void VideoWidget::resizeEvent(QResizeEvent *resizeEvent){
  106. // //m_backingStore->resize(resizeEvent->size());
  107. // this->backingStore()->resize(resizeEvent->size());
  108. // if( this->isEnabled() )
  109. // renderNow();
  110. //}
  111. //! [5]
  112. //! [2]
  113. void VideoThumbWidget::selected(void *P){
  114. ((VideoSourceChooser*)m_pParent)->selectForMainPreview(P);
  115. };
  116. void VideoThumbWidget::exposeEvent(QExposeEvent *){
  117. LOG_ERROR("Expose EVT!");
  118. }
  119. //! [2]
  120. void VideoThumbWidget::paintEvent(QPaintEvent *evt){
  121. renderNow();
  122. }
  123. //! [3]
  124. void VideoThumbWidget::renderNow(){
  125. m_pWidgetProtectorMutex->lock();
  126. try{
  127. //if (m_nMenuActivityCounter)m_nMenuActivityCounter--;
  128. //else if (m_bMouseActive)m_bMouseActive = false;
  129. QPainter painter(this);
  130. painter.setRenderHint(QPainter::Antialiasing);
  131. //painter.begin(this);
  132. render(&painter);
  133. //painter.end();
  134. }catch (std::exception& e){
  135. LOG_ERROR("RenderNow exception!");
  136. };
  137. m_pWidgetProtectorMutex->unlock();
  138. }
  139. //! [3]
  140. //! [4]
  141. void VideoThumbWidget::render(QPainter *painter){
  142. if( painter != NULL ){
  143. try{
  144. //char buffer[64];
  145. //sprintf(buffer, "Coords: %dx%d %d %d ? %d %d", pos().x(), pos().y(), width(), height(), m_nMouseX, m_nMouseY);
  146. painter->save();
  147. //painter->fillRect(QRect(0, 0, width(), height()), Qt::green);
  148. QPen BgPen(Qt::SolidPattern, 3);
  149. BgPen.setColor(Qt::black);
  150. if( m_bErrorMode && m_pOverlayPixmap!= NULL ){
  151. painter->drawPixmap(QRect(0, 0, width(), height()), *m_pOverlayPixmap);
  152. if (m_pErrorMsg != NULL){
  153. QPen FgPen(Qt::SolidPattern, 0);
  154. FgPen.setColor(Qt::yellow);
  155. painter->setPen(BgPen);
  156. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, m_pErrorMsg);
  157. painter->setPen(FgPen);
  158. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, m_pErrorMsg);
  159. };
  160. } else {
  161. painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
  162. };
  163. if (m_bSelected){
  164. BgPen.setColor(Qt::blue);
  165. painter->setPen(BgPen);
  166. painter->drawRect(QRect(2, 2, width() - 4, height() - 4));
  167. };
  168. //if (m_bMouseActive){
  169. // if (m_pOverlayPixmap != NULL){
  170. // painter->drawPixmap(QRect(0, 0, width(), height()), *m_pOverlayPixmap);
  171. // };
  172. //};
  173. painter->restore();
  174. }catch (std::exception& e){
  175. LOG_ERROR("Render exception!");
  176. };
  177. };
  178. }
  179. bool VideoThumbWidget::init( void ){
  180. LOG("init.");
  181. m_pPixmap = new QPixmap(width(), height()); //, QImage::Format_RGB888 );
  182. if( m_pPixmap==NULL ) return false;
  183. m_pOverlayPixmap = new QPixmap(":pictures/43a.png", "PNG");
  184. m_pTimer = new QTimer();
  185. if (m_pTimer != NULL){
  186. QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT( update()));
  187. m_pTimer->start(40);
  188. };
  189. m_pPixmap->fill(Qt::black);
  190. m_pPropertiesProtectorMutex->unlock();
  191. m_pWidgetProtectorMutex->unlock();
  192. return false;
  193. };
  194. VideoThumbWidget::~VideoThumbWidget(){
  195. if (m_pTimer){
  196. m_pTimer->stop();
  197. delete(m_pTimer);
  198. };
  199. delete( m_pPropertiesProtectorMutex );
  200. delete( m_pWidgetProtectorMutex );
  201. };
  202. void VideoThumbWidget::setGrayScale(){
  203. m_pPropertiesProtectorMutex->lock();
  204. m_bGrayScale = (m_bGrayScale) ? false : true;
  205. m_pPropertiesProtectorMutex->unlock();
  206. };
  207. void VideoThumbWidget::setEnhanced(){
  208. m_pPropertiesProtectorMutex->lock();
  209. m_bEnhanced = (m_bEnhanced) ? false : true;
  210. m_pPropertiesProtectorMutex->unlock();
  211. };
  212. void VideoThumbWidget::setError(bool AAA, CSTR Message){
  213. m_bErrorMode = AAA;
  214. m_pErrorMsg = Message;
  215. if (AAA) LOG("ERROR TRUE"); else LOG("ERROR FALSE");
  216. };
  217. void VideoThumbWidget::pushFrame(cv::Mat *Frame, void *src){
  218. if (Frame != NULL && m_pPixmap!= NULL){
  219. m_pPropertiesProtectorMutex->lock();
  220. m_eStreamAspectRatio = detectAspectRatio(Frame->cols, Frame->rows);
  221. if (m_bGrayScale) {
  222. cvtColor(*Frame, *Frame, CV_BGR2GRAY);
  223. };
  224. if (m_bEnhanced) {
  225. if (!m_bGrayScale) {
  226. vector<cv::Mat> channels;
  227. cv::Mat img_hist_equalized;
  228. cvtColor(*Frame, img_hist_equalized, CV_BGR2YCrCb); //change the color image from BGR to YCrCb format
  229. cv::split(img_hist_equalized, channels); //split the image into channels
  230. cv::equalizeHist(channels[0], channels[0]); //equalize histogram on the 1st channel (Y)
  231. merge(channels, img_hist_equalized); //merge 3 channels including the modified 1st channel into one image
  232. cvtColor(img_hist_equalized, *Frame, CV_YCrCb2BGR); //change the color image from YCrCb to BGR format (to display image properly)
  233. } else {
  234. equalizeHist(*Frame, *Frame);
  235. };
  236. };
  237. if (!m_bGrayScale){
  238. cvtColor(*Frame, *Frame, CV_BGR2RGB);
  239. } else {
  240. cvtColor(*Frame, *Frame, CV_GRAY2RGB);
  241. };
  242. m_pPropertiesProtectorMutex->unlock();
  243. m_mOutput = Frame;
  244. try{
  245. assert(m_mOutput->isContinuous());
  246. m_pWidgetProtectorMutex->lock();
  247. assert(m_pPixmap);
  248. QPainter dbPainter(m_pPixmap);
  249. if (m_eStreamAspectRatio != m_eWidgetAspectRatio){
  250. int tOffset;
  251. if (m_eStreamAspectRatio > m_eWidgetAspectRatio){
  252. int tHeight;
  253. tHeight = (int)((float)m_pPixmap->width() / m_eStreamAspectRatio);
  254. tOffset = (m_pPixmap->height() - tHeight) >> 1;
  255. dbPainter.drawImage(QRect(0, tOffset, width(), tHeight),
  256. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  257. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  258. } else {
  259. int tWidth;
  260. tWidth = (int)((float)m_pPixmap->height() * m_eStreamAspectRatio);
  261. tOffset = (m_pPixmap->width() - tWidth) >> 1;
  262. dbPainter.drawImage(QRect(tOffset, 0, tWidth, height()),
  263. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  264. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  265. };
  266. } else {
  267. dbPainter.drawImage(QRect(0, 0, width(), height()), QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888), QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  268. };
  269. m_pWidgetProtectorMutex->unlock();
  270. } catch (std::exception& e){
  271. LOG_ERROR("Exception catched:");
  272. LOG_ERROR(e.what());
  273. };
  274. //delete(Frame);
  275. };
  276. };
  277. void VideoThumbWidget::showCameraSettings(){
  278. if (m_pSettingsDialog == NULL){
  279. m_pSettingsDialog = new DialogSettingsCamera(this);
  280. }
  281. m_pSettingsDialog->attachDevice( m_pSignalParameter );
  282. m_pSettingsDialog->show();
  283. };
  284. void VideoThumbWidget::removeThisCamera(){
  285. if( m_pParent != NULL ){
  286. if (m_pSignalParameter != NULL){
  287. m_pParent->removeCamera(m_pSignalParameter);
  288. };
  289. };
  290. };
  291. #undef LOG
  292. #undef LOG_ERROR