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.

323 lines
10 KiB

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