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.

781 lines
27 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "VideoWidget.h"
  2. #define LOG( X ) m_pLog->Log( "VideoWidget", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "VideoWidget", this, _LOG_ERROR, X )
  4. VideoWidget::VideoWidget(QWidget *parent) :
  5. QWidget(parent),
  6. basicMediaDestination(){
  7. m_bGrayScale = false;
  8. m_bEnhanced = false;
  9. m_bDirectDrawing = true;
  10. m_bUpdateIsPending = false;
  11. //setFixedSize( 500, 375 );
  12. setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
  13. setMouseTracking( true );
  14. //grabKeyboard();
  15. m_nKeyCode = 0;
  16. m_nOldKeyCode = 0;
  17. m_nManualControlMode = CD_AUTO;
  18. m_pCamera = NULL;
  19. //setKeyboardTracking( true );
  20. m_eStreamAspectRatio = FAR_UNKNOWN;
  21. m_eWidgetAspectRatio = FAR_4X3;//detectAspectRatio(width(), height());
  22. //setGeometry(m_nPosX, m_nPosY, m_nWidth+1, m_nHeight+1);
  23. //setMinimumSize(QSize(320, 240));
  24. //setSizeIncrement(QSize(20, 20));
  25. //m_backingStore = new QBackingStore( );
  26. //setBackgroundRole(QPalette::Window);
  27. m_pBorder = new QFrame(this);
  28. m_pBorder->setFrameShape(QFrame::Panel);
  29. m_pBorder->setFrameShadow(QFrame::Sunken);
  30. m_pBorder->setGeometry(0, 0, 200, 200);
  31. setAutoFillBackground(false);
  32. setAttribute(Qt::WA_OpaquePaintEvent);
  33. setCursor(QCursor(Qt::PointingHandCursor));
  34. //create();
  35. m_pManualMode = NULL;
  36. m_pPixUp = NULL;
  37. m_pPixDown = NULL;
  38. m_pPixLeft = NULL;
  39. m_pPixRight = NULL;
  40. m_pPixWide = NULL;
  41. m_pPixTele = NULL;
  42. m_pPixmap = NULL;
  43. //m_pOverlayPixmap = NULL;
  44. m_pGraber = NULL;
  45. m_mFrame = NULL;
  46. m_mOutput = NULL;
  47. m_pTimer = NULL;
  48. m_pFullScreenView = NULL;
  49. m_bMouseActive = false;
  50. m_bFullScreenView = false;
  51. m_nMouseX = 0;
  52. m_nMouseY = 0;
  53. m_pPropertiesProtectorMutex = new basicMutex();
  54. m_pWidgetProtectorMutex = new basicMutex();
  55. #ifndef NO_JOYSTICK
  56. m_pJO = NULL;
  57. #endif
  58. };
  59. bool VideoWidget::event(QEvent *event){
  60. if( event->type() == QEvent::UpdateRequest ){
  61. m_bUpdateIsPending = false;
  62. renderNow();
  63. return true;
  64. }
  65. if (event->type() == QEvent::MouseButtonRelease ){
  66. //Beep(1024, 10);
  67. if (((QMouseEvent*)event)->button() == Qt::LeftButton){
  68. if (m_nManualControlMode & CD_MANUAL){
  69. #ifndef NO_PTZ_CAMERAS
  70. if (m_nManualControlMode&(CD_TELE | CD_WIDE)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  71. if (m_nManualControlMode&(CD_UP | CD_DOWN)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  72. if (m_nManualControlMode&(CD_LEFT | CD_RIGHT)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  73. #endif
  74. m_nManualControlMode = CD_AUTO;
  75. releaseKeyboard();
  76. }else{
  77. m_nManualControlMode = CD_MANUAL;
  78. m_nMenuActivityCounter = 75;
  79. grabKeyboard();
  80. };
  81. return true;
  82. };
  83. };
  84. if (event->type() == QEvent::MouseButtonDblClick){
  85. showFullScreen();
  86. return true;
  87. }
  88. if (m_nManualControlMode & CD_MANUAL){
  89. if (event->type() == QEvent::KeyPress){
  90. m_nKeyCode = ((QKeyEvent *)event)->key();
  91. switch (m_nKeyCode){
  92. case 32:
  93. showFullScreen();
  94. break;
  95. case 87://w
  96. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  97. if ((m_nManualControlMode & CD_TELE) == 0){
  98. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
  99. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_TELE);
  100. #ifndef NO_PTZ_CAMERAS
  101. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomTele();
  102. #endif
  103. }
  104. else {
  105. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
  106. #ifndef NO_PTZ_CAMERAS
  107. ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  108. #endif
  109. };
  110. m_nMenuActivityCounter = 75;
  111. };
  112. break;
  113. case 65://A
  114. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  115. if ((m_nManualControlMode & CD_WIDE) == 0){
  116. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
  117. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_WIDE);
  118. #ifndef NO_PTZ_CAMERAS
  119. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomWide();
  120. #endif
  121. }
  122. else {
  123. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
  124. #ifndef NO_PTZ_CAMERAS
  125. ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  126. #endif
  127. };
  128. m_nMenuActivityCounter = 75;
  129. };
  130. break;
  131. case 0x01000012://Left
  132. if (m_nCameraCaps & OC_PAN_CONTROLL){
  133. if ((m_nManualControlMode & CD_LEFT) == 0){
  134. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
  135. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_LEFT);
  136. #ifndef NO_PTZ_CAMERAS
  137. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanLeft();
  138. #endif
  139. } else {
  140. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
  141. #ifndef NO_PTZ_CAMERAS
  142. ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  143. #endif
  144. };
  145. m_nMenuActivityCounter = 75;
  146. };
  147. break;
  148. case 0x01000013://Up
  149. if (m_nCameraCaps & OC_TILT_CONTROLL){
  150. if ((m_nManualControlMode & CD_UP) == 0){
  151. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
  152. m_nManualControlMode = (ControlDirection) (m_nManualControlMode | CD_UP );
  153. #ifndef NO_PTZ_CAMERAS
  154. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltUp();
  155. #endif
  156. } else {
  157. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
  158. #ifndef NO_PTZ_CAMERAS
  159. ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  160. #endif
  161. };
  162. m_nMenuActivityCounter = 75;
  163. };
  164. break;
  165. case 0x01000014://Right
  166. if (m_nCameraCaps & OC_PAN_CONTROLL){
  167. if ((m_nManualControlMode & CD_RIGHT) == 0){
  168. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
  169. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_RIGHT);
  170. #ifndef NO_PTZ_CAMERAS
  171. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanRight();
  172. #endif
  173. } else {
  174. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
  175. #ifndef NO_PTZ_CAMERAS
  176. ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  177. #endif
  178. };
  179. m_nMenuActivityCounter = 75;
  180. };
  181. break;
  182. case 0x01000015://Down
  183. if (m_nCameraCaps & OC_TILT_CONTROLL){
  184. if ((m_nManualControlMode & CD_DOWN) == 0){
  185. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
  186. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_DOWN);
  187. #ifndef NO_PTZ_CAMERAS
  188. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltDown();
  189. #endif
  190. } else {
  191. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
  192. #ifndef NO_PTZ_CAMERAS
  193. ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  194. #endif
  195. };
  196. m_nMenuActivityCounter = 75;
  197. };
  198. break;
  199. };
  200. m_nOldKeyCode = m_nKeyCode;
  201. return true;
  202. };
  203. };
  204. return QWidget::event(event);
  205. }
  206. void VideoWidget::setCameraControl(basicCamera *C, objectCaps CC){
  207. if (m_pCamera != NULL){
  208. #ifndef NO_PTZ_CAMERAS
  209. if (m_nCameraCaps & OC_PAN_CONTROLL){
  210. if (m_nManualControlMode&CD_LEFT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  211. if (m_nManualControlMode&CD_RIGHT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  212. };
  213. if (m_nCameraCaps & OC_TILT_CONTROLL){
  214. if (m_nManualControlMode&CD_UP)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  215. if (m_nManualControlMode&CD_DOWN)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  216. };
  217. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  218. if (m_nManualControlMode&CD_TELE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  219. if (m_nManualControlMode&CD_WIDE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  220. };
  221. #endif
  222. m_nManualControlMode = CD_AUTO;
  223. #ifndef NO_JOYSTICK
  224. m_pJO = joysticObject::getInstance();
  225. #endif
  226. };
  227. m_nCameraCaps = CC;
  228. m_pCamera = C;
  229. };
  230. void VideoWidget::renderLater(){
  231. if (!m_bUpdateIsPending) {
  232. m_bUpdateIsPending = true;
  233. QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
  234. #ifdef DEBUG
  235. LOG("Delayed Rendering Request");
  236. #endif
  237. }
  238. }
  239. //! [6]
  240. //! [5]
  241. //void VideoWidget::resizeEvent(QResizeEvent *resizeEvent){
  242. // //m_backingStore->resize(resizeEvent->size());
  243. // this->backingStore()->resize(resizeEvent->size());
  244. // if( this->isEnabled() )
  245. // renderNow();
  246. //}
  247. //! [5]
  248. //! [2]
  249. void VideoWidget::exposeEvent(QExposeEvent *){
  250. LOG_ERROR("Expose EVT!");
  251. }
  252. //! [2]
  253. void VideoWidget::paintEvent(QPaintEvent *evt){
  254. renderNow();
  255. }
  256. //! [3]
  257. void VideoWidget::renderNow(){
  258. m_pWidgetProtectorMutex->lock();
  259. try{
  260. if ((m_nManualControlMode & CD_JOYSTICK) != 0){
  261. #ifndef NO_JOYSTICK
  262. if (m_pJO != NULL){
  263. #ifndef NO_PTZ_CAMERAS
  264. ((basicCameraPTZ *)m_pCamera)->setDirectSpeedControl(true);
  265. #endif
  266. long tParam;
  267. tParam = m_pJO->getHat();
  268. if (tParam > 200){
  269. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  270. if ((m_nManualControlMode & CD_TELE) == 0){
  271. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE));
  272. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_TELE);
  273. #ifndef NO_PTZ_CAMERAS
  274. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomTele(((float)tParam) / 1000.f);
  275. #endif
  276. };
  277. m_nMenuActivityCounter = 75;
  278. };
  279. } else if (tParam < -200){
  280. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  281. if ((m_nManualControlMode & CD_WIDE) == 0){
  282. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE));
  283. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_WIDE);
  284. #ifndef NO_PTZ_CAMERAS
  285. ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomWide(((float)-tParam) / 1000.f);
  286. #endif
  287. };
  288. m_nMenuActivityCounter = 75;
  289. };
  290. } else {
  291. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  292. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~(CD_TELE | CD_WIDE)));
  293. #ifndef NO_PTZ_CAMERAS
  294. ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom();
  295. #endif
  296. };
  297. };
  298. tParam = m_pJO->getAxisX();
  299. if (tParam < -200){
  300. if (m_nCameraCaps & OC_PAN_CONTROLL){
  301. if ((m_nManualControlMode & CD_LEFT) == 0){
  302. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT));
  303. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_LEFT);
  304. #ifndef NO_PTZ_CAMERAS
  305. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanLeft(((float)-tParam) / 1000.f);
  306. #endif
  307. };
  308. m_nMenuActivityCounter = 75;
  309. };
  310. } else if (tParam > 200){
  311. if (m_nCameraCaps & OC_PAN_CONTROLL){
  312. if ((m_nManualControlMode & CD_RIGHT) == 0){
  313. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT));
  314. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_RIGHT);
  315. #ifndef NO_PTZ_CAMERAS
  316. ((basicCameraPTZ *)m_pCamera)->cmdBeginPanRight(((float)tParam) / 1000.f);
  317. #endif
  318. };
  319. m_nMenuActivityCounter = 75;
  320. };
  321. } else {
  322. if (m_nCameraCaps & OC_PAN_CONTROLL){
  323. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~(CD_LEFT | CD_RIGHT)));
  324. #ifndef NO_PTZ_CAMERAS
  325. ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan();
  326. #endif
  327. };
  328. };
  329. tParam = m_pJO->getAxisY();
  330. if (tParam > 200){
  331. if (m_nCameraCaps & OC_TILT_CONTROLL){
  332. if ((m_nManualControlMode & CD_UP) == 0){
  333. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN));
  334. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_UP);
  335. #ifndef NO_PTZ_CAMERAS
  336. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltUp(((float)tParam) / 1000.f);
  337. #endif
  338. };
  339. m_nMenuActivityCounter = 75;
  340. };
  341. } else if (tParam < -200){//Down
  342. if (m_nCameraCaps & OC_TILT_CONTROLL){
  343. if ((m_nManualControlMode & CD_DOWN) == 0){
  344. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP));
  345. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_DOWN);
  346. #ifndef NO_PTZ_CAMERAS
  347. ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltDown(((float)-tParam) / 1000.f);
  348. #endif
  349. };
  350. m_nMenuActivityCounter = 75;
  351. };
  352. } else {
  353. if (m_nCameraCaps & OC_TILT_CONTROLL){
  354. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~(CD_UP | CD_DOWN)));
  355. #ifndef NO_PTZ_CAMERAS
  356. ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt();
  357. #endif
  358. };
  359. };
  360. } else {
  361. m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_JOYSTICK));
  362. #ifndef NO_PTZ_CAMERAS
  363. ((basicCameraPTZ *)m_pCamera)->setDirectSpeedControl(false);
  364. #endif
  365. };
  366. #endif //#ifndef NO_JOYSTICK
  367. };
  368. if ((m_nManualControlMode & CD_MOVIN ) == 0){
  369. if ( m_nMenuActivityCounter) m_nMenuActivityCounter--;
  370. else if ((m_nManualControlMode & CD_AUTO) == 0){
  371. if (m_nManualControlMode & CD_MANUAL) releaseKeyboard();
  372. #ifndef NO_PTZ_CAMERAS
  373. if(m_pCamera!= NULL)((basicCameraPTZ *)m_pCamera)->setDirectSpeedControl(false);
  374. #endif
  375. m_nManualControlMode = CD_AUTO;
  376. };
  377. };
  378. QPainter painter(this);
  379. //painter.setRenderHint(QPainter::Antialiasing);
  380. painter.setRenderHint((QPainter::RenderHint)7);// QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing
  381. //if( !painter.isActive() ) painter.begin(this);
  382. render(&painter);
  383. //if (pa inter.isActive()) painter.end();
  384. }catch (std::exception& e){
  385. LOG_ERROR("RenderNow exception!");
  386. };
  387. m_pWidgetProtectorMutex->unlock();
  388. }
  389. //! [3]
  390. //! [4]
  391. void VideoWidget::render(QPainter *painter){
  392. if( painter != NULL ){
  393. try{
  394. char buffer[64];
  395. sprintf(buffer, "Coords: %d %d Key: %d PM: %dx%d", m_nMouseX, m_nMouseY, m_nKeyCode, m_pPixmap->width(),m_pPixmap->height() );
  396. painter->save();
  397. painter->drawPixmap(QRect(1, 1, m_pPixmap->width(), m_pPixmap->height()), *m_pPixmap);
  398. QPen BgPen(Qt::SolidPattern,2);
  399. BgPen.setColor(Qt::black);
  400. QPen FgPen(Qt::SolidPattern,0);
  401. FgPen.setColor(Qt::yellow);
  402. painter->setPen(BgPen);
  403. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer);
  404. painter->setPen(FgPen);
  405. painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline , buffer);
  406. if (m_pCamera != NULL){
  407. uint8_t cnt;
  408. if (cnt = m_pCamera->getLabelCount()){
  409. osdLabel *pLabel;
  410. for (uint8_t index = 0; index < cnt; index++){
  411. pLabel = m_pCamera->getLabel(index);
  412. if (pLabel != NULL){
  413. FgPen.setColor(QColor(pLabel->color[0], pLabel->color[1], pLabel->color[2], pLabel->color[3]));
  414. painter->setPen(FgPen);
  415. painter->drawText(pLabel->x, pLabel->y, pLabel->text );
  416. };
  417. };
  418. FgPen.setColor(Qt::yellow);
  419. painter->setPen(FgPen);
  420. };
  421. if ((m_nManualControlMode & CD_AUTO) == 0){
  422. QRect ManualRect(10, height() - m_pManualMode->height() - 10, m_pManualMode->width(), m_pManualMode->height());
  423. if (m_nCameraCaps & OC_TILT_CONTROLL){
  424. QRect UpRect(
  425. (width() - m_pPixUp->width()) / 2,
  426. (height() / 2) - m_pPixUp->height() - 10,
  427. m_pPixUp->width(),
  428. m_pPixUp->height());
  429. QRect DownRect(
  430. (width() - m_pPixDown->width()) / 2,
  431. (height() / 2) + 10,
  432. m_pPixDown->width(),
  433. m_pPixDown->height());
  434. if (m_nManualControlMode & CD_UP) {
  435. painter->fillRect(UpRect, QColor(255, 255, 255, 63));
  436. } else {
  437. painter->drawRect(UpRect);
  438. };
  439. if (m_nManualControlMode & CD_DOWN){
  440. painter->fillRect(DownRect, QColor(255, 255, 255, 63));
  441. } else {
  442. painter->drawRect(DownRect);
  443. };
  444. painter->drawPixmap(UpRect, *m_pPixUp);
  445. painter->drawPixmap(DownRect, *m_pPixDown);
  446. };
  447. if (m_nCameraCaps & OC_PAN_CONTROLL){
  448. QRect LeftRect(
  449. (width() / 2) - m_pPixLeft->width() - 10 - (m_pPixLeft->width() / 2),
  450. (height() / 2) - (m_pPixLeft->height() / 2),
  451. m_pPixLeft->width(),
  452. m_pPixLeft->height());
  453. QRect RightRect(
  454. (width() / 2) + 10 + (m_pPixRight->width() / 2),
  455. (height() / 2) - (m_pPixRight->height() / 2),
  456. m_pPixRight->width(),
  457. m_pPixRight->height());
  458. if (m_nManualControlMode & CD_RIGHT){
  459. painter->fillRect(RightRect, QColor(255, 255, 255, 63));
  460. } else {
  461. painter->drawRect(RightRect);
  462. };
  463. if (m_nManualControlMode & CD_LEFT){
  464. painter->fillRect(LeftRect, QColor(255, 255, 255, 63));
  465. } else {
  466. painter->drawRect(LeftRect);
  467. };
  468. painter->drawPixmap(LeftRect, *m_pPixLeft);
  469. painter->drawPixmap(RightRect, *m_pPixRight);
  470. };
  471. if (m_nCameraCaps & OC_ZOOM_CONTROLL){
  472. QRect WideRect(
  473. 25,
  474. 25,
  475. m_pManualMode->width(),
  476. m_pManualMode->height());
  477. QRect TeleRect(
  478. width() - m_pPixTele->height() - 25,
  479. 25,
  480. m_pPixTele->width(),
  481. m_pPixTele->height());
  482. if (m_nManualControlMode & CD_WIDE){
  483. painter->fillRect(WideRect, QColor(255, 255, 255, 63));
  484. } else {
  485. painter->drawRect(WideRect);
  486. };
  487. if (m_nManualControlMode & CD_TELE){
  488. painter->fillRect(TeleRect, QColor(255, 255, 255, 63));
  489. } else {
  490. painter->drawRect(TeleRect);
  491. };
  492. painter->drawPixmap(WideRect, *m_pPixWide);
  493. painter->drawPixmap(TeleRect, *m_pPixTele);
  494. };
  495. #ifndef NO_JOYSTICK
  496. if( m_pJO!= NULL ){
  497. if( m_pJO->isPresent() ){
  498. if( m_pJO->isActive() ){
  499. m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_JOYSTICK);
  500. painter->fillRect(ManualRect, QColor(255, 255, 255, 63));
  501. m_nMenuActivityCounter = 75;
  502. } else {
  503. //m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_JOYSTICK));
  504. painter->drawRect(ManualRect);
  505. };
  506. } else {
  507. painter->drawRect(ManualRect);
  508. };
  509. } else {
  510. painter->drawRect(ManualRect);
  511. };
  512. #endif
  513. painter->drawPixmap(ManualRect, *m_pManualMode);
  514. };
  515. };
  516. painter->restore();
  517. }catch (std::exception& e){
  518. LOG_ERROR("Render exception!");
  519. };
  520. };
  521. }
  522. QSize VideoWidget::sizeHint() const {
  523. QSize s = size();
  524. //lastHeight = s.height();
  525. s.setWidth((s.height() * 4) / 3);
  526. s.setHeight(s.height());// QGroupBox::sizeHint().height());
  527. return s;
  528. };
  529. void VideoWidget::resizeEvent(QResizeEvent *event){
  530. if (m_pPixmap == NULL) return;
  531. int w, h;
  532. w = width();
  533. h = height();
  534. if ( abs( (width() * 3 ) - (height() * 4) ) > 4 ){
  535. setFixedWidth((4 * height()) / 3);
  536. //event->ignore();
  537. //return;
  538. };
  539. m_pBorder->setGeometry(0, 0, width(), height());
  540. m_pPropertiesProtectorMutex->lock();
  541. delete(m_pPixmap);
  542. m_pPixmap = new QPixmap(width()-2, height()-2);
  543. if( m_mOutput != NULL ){
  544. try{
  545. assert(m_mOutput->isContinuous());
  546. m_pWidgetProtectorMutex->lock();
  547. m_pPixmap->fill(Qt::black);
  548. QPainter dbPainter(m_pPixmap);
  549. if (m_eStreamAspectRatio != m_eWidgetAspectRatio){
  550. int tOffset;
  551. if (m_eStreamAspectRatio > m_eWidgetAspectRatio){
  552. int tHeight;
  553. tHeight = (int)((float)m_pPixmap->width() / m_eStreamAspectRatio);
  554. tOffset = (m_pPixmap->height() - tHeight) >> 1;
  555. dbPainter.drawImage(QRect(0, tOffset, m_pPixmap->width(), tHeight),
  556. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  557. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  558. } else {
  559. int tWidth;
  560. tWidth = (int)((float)m_pPixmap->height() * m_eStreamAspectRatio);
  561. tOffset = (m_pPixmap->width() - tWidth) >> 1;
  562. dbPainter.drawImage(QRect(tOffset, 0, tWidth, m_pPixmap->height()),
  563. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  564. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  565. };
  566. } else {
  567. dbPainter.drawImage(QRect(0, 0, m_pPixmap->width(), m_pPixmap->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));
  568. };
  569. m_pWidgetProtectorMutex->unlock();
  570. } catch (std::exception& e){
  571. LOG_ERROR("Exception catched:");
  572. LOG_ERROR(e.what());
  573. };
  574. } else {
  575. m_pWidgetProtectorMutex->lock();
  576. m_pPixmap->fill(Qt::black);
  577. m_pWidgetProtectorMutex->unlock();
  578. };
  579. m_pPropertiesProtectorMutex->unlock();
  580. event->accept();
  581. };
  582. bool VideoWidget::init( void ){
  583. LOG("init.");
  584. //int a;
  585. //a = height();
  586. //setFixedWidth( ( height() * 4 )/ 3 );
  587. m_pPixmap = new QPixmap(width()-2, height()-2); //, QImage::Format_RGB888 );
  588. if( m_pPixmap==NULL ) return false;
  589. //m_pOverlayPixmap = new QPixmap("640x480.png", "PNG");
  590. m_pManualMode = new QPixmap(":pictures/appbar.controller.snes.png", "PNG");
  591. m_pPixUp = new QPixmap(":pictures/appbar.chevron.up.png", "PNG");
  592. m_pPixDown = new QPixmap(":pictures/appbar.chevron.down.png", "PNG");
  593. m_pPixLeft = new QPixmap(":pictures/appbar.chevron.left.png", "PNG");
  594. m_pPixRight = new QPixmap(":pictures/appbar.chevron.right.png", "PNG");
  595. m_pPixWide = new QPixmap(":pictures/appbar.arrow.expand.png", "PNG");
  596. m_pPixTele = new QPixmap(":pictures/appbar.arrow.collapsed.png", "PNG");
  597. m_pTimer = new QTimer();
  598. if (m_pTimer != NULL){
  599. connect(m_pTimer, SIGNAL(timeout()), this, SLOT(update()));
  600. m_pTimer->start(40);
  601. };
  602. m_pPixmap->fill(Qt::black);
  603. m_pPropertiesProtectorMutex->lock();
  604. m_pPropertiesProtectorMutex->unlock();
  605. return false;
  606. };
  607. VideoWidget::~VideoWidget(){
  608. if (m_pTimer){
  609. m_pTimer->stop();
  610. delete(m_pTimer);
  611. };
  612. delete( m_pPropertiesProtectorMutex );
  613. delete( m_pWidgetProtectorMutex );
  614. };
  615. void VideoWidget::setGrayScale( bool AAA ){
  616. m_pPropertiesProtectorMutex->lock();
  617. m_bGrayScale = AAA;
  618. m_pPropertiesProtectorMutex->unlock();
  619. //LOG("GRAYSCALE");
  620. };
  621. void VideoWidget::setEnhanced( bool AAA ){
  622. m_pPropertiesProtectorMutex->lock();
  623. m_bEnhanced = AAA;
  624. m_pPropertiesProtectorMutex->unlock();
  625. //LOG("ENHANCED");
  626. };
  627. void VideoWidget::showFullScreen(void){
  628. m_pWidgetProtectorMutex->lock();
  629. if (m_pFullScreenView == NULL){
  630. m_pFullScreenView = new QDialog(NULL);
  631. QVBoxLayout *mainLayout = new QVBoxLayout(m_pFullScreenView);
  632. mainLayout->setContentsMargins(0, 0, 0, 0);
  633. mainLayout->setSizeConstraint(QLayout::SizeConstraint::SetMaximumSize);
  634. m_pFullScreenView->setLayout(mainLayout);
  635. #ifdef USE_OPENGL
  636. m_pFSVideoWidget = new FSGLVideoWidget(m_pFullScreenView, 640,480, QColor(200, 150, 11));
  637. #else
  638. m_pFSVideoWidget = new FSVideoWidget(m_pFullScreenView);
  639. #endif
  640. m_pFSVideoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  641. mainLayout->addWidget(m_pFSVideoWidget);
  642. m_pFSVideoWidget->init();
  643. };
  644. releaseKeyboard();
  645. m_pFullScreenView->setModal(true);
  646. const QString session = QString(getenv("DESKTOP_SESSION")).toLower();
  647. if (session == "ubuntu" || session == "mate") {
  648. //m_pFullScreenView->setFixedSize(qApp->desktop()->screenGeometry());
  649. m_pFullScreenView->setWindowFlags(Qt::FramelessWindowHint);
  650. //m_pFullScreenView->setWindowState( m_pFullScreenView->windowState() | Qt::WindowFullScreen);
  651. m_pFullScreenView->showMaximized();
  652. m_pFullScreenView->activateWindow();
  653. QRect R = qApp->desktop()->screenGeometry();
  654. m_pFSVideoWidget->setBindingSize( this, R.width() , R.height());
  655. } else {
  656. m_pFullScreenView->showFullScreen();
  657. int X, Y;
  658. X = m_pFullScreenView->width();
  659. Y = m_pFullScreenView->height();
  660. m_pFSVideoWidget->setBindingSize( this,X , Y);
  661. };
  662. m_pFSVideoWidget->setCameraControl(m_pCamera, m_nCameraCaps);
  663. m_bFullScreenView = true;
  664. m_pWidgetProtectorMutex->unlock();
  665. };
  666. void VideoWidget::hideFullScreen(void){
  667. m_pWidgetProtectorMutex->lock();
  668. if (m_pFullScreenView != NULL){
  669. m_pFullScreenView->hide();
  670. //grabKeyboard();
  671. };
  672. m_bFullScreenView = false;
  673. m_pWidgetProtectorMutex->unlock();
  674. };
  675. /*
  676. QImage Mat2QImage(cv::Mat const& src){
  677. cv::Mat temp; // make the same cv::Mat
  678. cvtColor(src, temp, CV_BGR2RGB); // cvtColor Makes a copt, that what i need
  679. QImage dest((const uchar *)temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
  680. dest.bits(); // enforce deep copy, see documentation
  681. // of QImage::QImage ( const uchar * data, int width, int height, Format format )
  682. return dest;
  683. }
  684. cv::Mat QImage2Mat(QImage const& src){
  685. cv::Mat tmp(src.height(), src.width(), CV_8UC3, (uchar*)src.bits(), src.bytesPerLine());
  686. cv::Mat result; // deep copy just in case (my lack of knowledge with open cv)
  687. cvtColor(tmp, result, CV_BGR2RGB);
  688. return result;
  689. }
  690. */
  691. void VideoWidget::pushFrame(cv::Mat *Frame, void *src){
  692. if( Frame != NULL ){
  693. if (m_bFullScreenView){
  694. m_pFSVideoWidget->pushFrame(Frame);
  695. return;
  696. };
  697. m_eStreamAspectRatio = detectAspectRatio(Frame->cols, Frame->rows);
  698. /*if (m_bGrayScale) {
  699. cvtColor(*Frame, *Frame, CV_BGR2GRAY);
  700. };
  701. if (m_bEnhanced) {
  702. if (!m_bGrayScale) {
  703. vector<cv::Mat> channels;
  704. cv::Mat img_hist_equalized;
  705. cvtColor(*Frame, img_hist_equalized, CV_BGR2YCrCb); //change the color image from BGR to YCrCb format
  706. cv::split(img_hist_equalized, channels); //split the image into channels
  707. cv::equalizeHist(channels[0], channels[0]); //equalize histogram on the 1st channel (Y)
  708. merge(channels, img_hist_equalized); //merge 3 channels including the modified 1st channel into one image
  709. cvtColor(img_hist_equalized, *Frame, CV_YCrCb2BGR); //change the color image from YCrCb to BGR format (to display image properly)
  710. }
  711. else {
  712. equalizeHist(*Frame, *Frame);
  713. };
  714. };*/
  715. //if (!m_bGrayScale) cvtColor(*Frame, *Frame, CV_BGR2RGB);
  716. m_mOutput = Frame;
  717. try{
  718. assert(m_mOutput->isContinuous());
  719. m_pWidgetProtectorMutex->lock();
  720. m_pPixmap->fill(Qt::black);
  721. QPainter dbPainter(m_pPixmap);
  722. if (m_eStreamAspectRatio != m_eWidgetAspectRatio){
  723. int tOffset;
  724. if (m_eStreamAspectRatio > m_eWidgetAspectRatio){
  725. int tHeight;
  726. tHeight = (int)((float)m_pPixmap->width() / m_eStreamAspectRatio);
  727. tOffset = (m_pPixmap->height() - tHeight) >> 1;
  728. dbPainter.drawImage(QRect(0, tOffset, m_pPixmap->width(), tHeight),
  729. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  730. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  731. } else {
  732. int tWidth;
  733. tWidth = (int)((float)m_pPixmap->height() * m_eStreamAspectRatio);
  734. tOffset = (m_pPixmap->width() - tWidth) >> 1;
  735. dbPainter.drawImage(QRect(tOffset, 0, tWidth, m_pPixmap->height()),
  736. QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888),
  737. QRect(0, 0, m_mOutput->cols, m_mOutput->rows));
  738. };
  739. } else {
  740. dbPainter.drawImage(QRect(0, 0, m_pPixmap->width(), m_pPixmap->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));
  741. };
  742. m_pWidgetProtectorMutex->unlock();
  743. }
  744. catch (std::exception& e){
  745. LOG_ERROR("Exception catched:");
  746. LOG_ERROR(e.what());
  747. };
  748. m_pPropertiesProtectorMutex->unlock();
  749. //delete(Frame);
  750. };
  751. };
  752. #undef LOG
  753. #undef LOG_ERROR