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