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