#include "FSVideoWidget.h" #include "VideoWidget.h" #define LOG( X ) m_pLog->Log( "FSVideoWidget", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "FSVideoWidget", this, _LOG_ERROR, X ) FSVideoWidget::FSVideoWidget(QWidget *parent) : QWidget(parent), basicMediaDestination(){ m_bGrayScale = false; m_bEnhanced = false; m_bDirectDrawing = true; m_bUpdateIsPending = false; //setFixedSize( 640, 480 ); setMouseTracking( true ); //grabKeyboard(); //releaseKeyboard(); m_nKeyCode = 0; m_nOldKeyCode = 0; m_nManualControlMode = CD_AUTO; m_pCamera = NULL; m_pLog = basicLogCollection::getInstance(); m_eStreamAspectRatio = FAR_UNKNOWN; //m_eWidgetAspectRatio = detectAspectRatio(width(), height()); //setGeometry(m_nPosX, m_nPosY, m_nWidth+1, m_nHeight+1); //setMinimumSize(QSize(320, 240)); //setSizeIncrement(QSize(20, 20)); //m_backingStore = new QBackingStore( ); //setBackgroundRole(QPalette::Window); setAutoFillBackground(false); setAttribute(Qt::WA_OpaquePaintEvent); setCursor(QCursor(Qt::PointingHandCursor)); //create(); m_pParentViewer = NULL; m_pPixmap = NULL; //m_pOverlayPixmap = NULL; m_pGraber = NULL; m_mFrame = NULL; m_mOutput = NULL; m_pTimer = NULL; //m_bMouseActive = false; m_nMouseX = 0; m_nMouseY = 0; m_pPropertiesProtectorMutex = new basicMutex(); m_pWidgetProtectorMutex = new basicMutex(); }; void FSVideoWidget::setBindingSize(basicMediaDestination *P, int x, int y){ m_pWidgetProtectorMutex->lock(); m_pParentViewer = P; setFixedSize(x, y); m_eWidgetAspectRatio = detectAspectRatio(width(), height()); if (m_pPixmap != NULL) delete m_pPixmap; m_pPixmap = new QPixmap(width(), height()); m_pPixmap->fill(Qt::black); m_pWidgetProtectorMutex->unlock(); }; void FSVideoWidget::setCameraControl(basicCamera *C, objectCaps CC){ if (m_pCamera != NULL){ #ifndef NO_PTZ_CAMERAS if (m_nCameraCaps & OC_PAN_CONTROLL){ if (m_nManualControlMode&CD_LEFT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan(); if (m_nManualControlMode&CD_RIGHT)((basicCameraPTZ *)m_pCamera)->cmdEndManualPan(); }; if (m_nCameraCaps & OC_TILT_CONTROLL){ if (m_nManualControlMode&CD_UP)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt(); if (m_nManualControlMode&CD_DOWN)((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt(); }; if (m_nCameraCaps & OC_ZOOM_CONTROLL){ if (m_nManualControlMode&CD_TELE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); if (m_nManualControlMode&CD_WIDE)((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); }; #endif m_nManualControlMode = CD_AUTO; }; m_nCameraCaps = CC; m_pCamera = C; }; bool FSVideoWidget::event(QEvent *event){ if( event->type() == QEvent::UpdateRequest ){ m_bUpdateIsPending = false; renderNow(); return true; } if (event->type() == QEvent::MouseButtonRelease){ if (((QMouseEvent*)event)->button() == Qt::LeftButton){ if (m_nManualControlMode & CD_MANUAL){ #ifndef NO_PTZ_CAMERAS if (m_nManualControlMode&(CD_TELE | CD_WIDE)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); if (m_nManualControlMode&(CD_UP | CD_DOWN)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); if (m_nManualControlMode&(CD_LEFT | CD_RIGHT)) ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); #endif m_nManualControlMode = CD_AUTO; } else{ m_nManualControlMode = CD_MANUAL; grabKeyboard(); m_nMenuActivityCounter = 75; }; return true; }; }; if (event->type() == QEvent::MouseButtonDblClick){ ((VideoWidget *)m_pParentViewer)->hideFullScreen(); return true; } if (m_nManualControlMode & CD_MANUAL){ if (event->type() == QEvent::KeyPress){ m_nKeyCode = ((QKeyEvent *)event)->key(); switch (m_nKeyCode){ case 32: ((VideoWidget *)m_pParentViewer)->hideFullScreen(); break; case 87://w if (m_nCameraCaps & OC_ZOOM_CONTROLL){ if ((m_nManualControlMode & CD_TELE) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_TELE); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomTele(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); #endif }; m_nMenuActivityCounter = 75; }; break; case 65://A if (m_nCameraCaps & OC_ZOOM_CONTROLL){ if ((m_nManualControlMode & CD_WIDE) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_TELE)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_WIDE); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginZoomWide(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_WIDE)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualZoom(); #endif }; m_nMenuActivityCounter = 75; }; break; case 0x01000012://Left if (m_nCameraCaps & OC_PAN_CONTROLL){ if ((m_nManualControlMode & CD_LEFT) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_LEFT); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginPanLeft(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan(); #endif }; m_nMenuActivityCounter = 75; }; break; case 0x01000013://Up if (m_nCameraCaps & OC_TILT_CONTROLL){ if ((m_nManualControlMode & CD_UP) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_UP); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltUp(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt(); #endif }; m_nMenuActivityCounter = 75; }; break; case 0x01000014://Right if (m_nCameraCaps & OC_PAN_CONTROLL){ if ((m_nManualControlMode & CD_RIGHT) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_LEFT)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_RIGHT); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginPanRight(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_RIGHT)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualPan(); #endif }; m_nMenuActivityCounter = 75; }; break; case 0x01000015://Down if (m_nCameraCaps & OC_TILT_CONTROLL){ if ((m_nManualControlMode & CD_DOWN) == 0){ m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_UP)); m_nManualControlMode = (ControlDirection)(m_nManualControlMode | CD_DOWN); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdBeginTiltDown(); #endif } else { m_nManualControlMode = (ControlDirection)(m_nManualControlMode & (~CD_DOWN)); #ifndef NO_PTZ_CAMERAS ((basicCameraPTZ *)m_pCamera)->cmdEndManualTilt(); #endif }; m_nMenuActivityCounter = 75; }; break; }; m_nOldKeyCode = m_nKeyCode; return true; }; }; return QWidget::event(event); } //! [7] //! [6] void FSVideoWidget::renderLater(){ if (!m_bUpdateIsPending) { m_bUpdateIsPending = true; QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest)); LOG("Delayed Rendering Request"); } } //! [6] //! [5] //void VideoWidget::resizeEvent(QResizeEvent *resizeEvent){ // //m_backingStore->resize(resizeEvent->size()); // this->backingStore()->resize(resizeEvent->size()); // if( this->isEnabled() ) // renderNow(); //} //! [5] //! [2] void FSVideoWidget::exposeEvent(QExposeEvent *){ LOG_ERROR("Expose EVT!"); } //! [2] void FSVideoWidget::paintEvent(QPaintEvent *evt){ renderNow(); } //! [3] void FSVideoWidget::renderNow(){ m_pWidgetProtectorMutex->lock(); try{ if (m_nMenuActivityCounter)m_nMenuActivityCounter--; else if (m_nManualControlMode & CD_MANUAL){ m_nManualControlMode = CD_AUTO; releaseKeyboard(); }; QPainter painter(this); //painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint((QPainter::RenderHint)7);// QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing //if( !painter.isActive() ) painter.begin(this); render(&painter); //if (pa inter.isActive()) painter.end(); }catch (std::exception& e){ LOG_ERROR("RenderNow exception!"); }; m_pWidgetProtectorMutex->unlock(); } //! [3] //! [4] void FSVideoWidget::render(QPainter *painter){ if( painter != NULL ){ try{ char buffer[64]; sprintf(buffer, "Coords: %d %d Key: %d", m_nMouseX, m_nMouseY, m_nKeyCode); painter->save(); painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap); QPen BgPen(Qt::SolidPattern, 2); BgPen.setColor(Qt::black); QPen FgPen(Qt::SolidPattern, 0); FgPen.setColor(Qt::yellow); painter->setPen(BgPen); painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer); painter->setPen(FgPen); painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer); if (m_pCamera != NULL){ if (m_nManualControlMode & CD_MANUAL){ QRect ManualRect(10, height() - m_pManualMode->height() - 10, m_pManualMode->width(), m_pManualMode->height()); if (m_nCameraCaps & OC_TILT_CONTROLL){ QRect UpRect( (width() - m_pPixUp->width()) / 2, (height() / 2) - m_pPixUp->height() - 10, m_pPixUp->width(), m_pPixUp->height()); QRect DownRect( (width() - m_pPixDown->width()) / 2, (height() / 2) + 10, m_pPixDown->width(), m_pPixDown->height()); if (m_nManualControlMode & CD_UP) { painter->fillRect(UpRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(UpRect); }; if (m_nManualControlMode & CD_DOWN){ painter->fillRect(DownRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(DownRect); }; painter->drawPixmap(UpRect, *m_pPixUp); painter->drawPixmap(DownRect, *m_pPixDown); }; if (m_nCameraCaps & OC_PAN_CONTROLL){ QRect LeftRect( (width() / 2) - m_pPixLeft->width() - 10 - (m_pPixLeft->width() / 2), (height() / 2) - (m_pPixLeft->height() / 2), m_pPixLeft->width(), m_pPixLeft->height()); QRect RightRect( (width() / 2) + 10 + (m_pPixRight->width() / 2), (height() / 2) - (m_pPixRight->height() / 2), m_pPixRight->width(), m_pPixRight->height()); if (m_nManualControlMode & CD_RIGHT){ painter->fillRect(RightRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(RightRect); }; if (m_nManualControlMode & CD_LEFT){ painter->fillRect(LeftRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(LeftRect); }; painter->drawPixmap(LeftRect, *m_pPixLeft); painter->drawPixmap(RightRect, *m_pPixRight); }; if (m_nCameraCaps & OC_ZOOM_CONTROLL){ QRect WideRect( 25, 25, m_pManualMode->width(), m_pManualMode->height()); QRect TeleRect( width() - m_pPixTele->height() - 25, 25, m_pPixTele->width(), m_pPixTele->height()); if (m_nManualControlMode & CD_WIDE){ painter->fillRect(WideRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(WideRect); }; if (m_nManualControlMode & CD_TELE){ painter->fillRect(TeleRect, QColor(255, 255, 255, 63)); } else { painter->drawRect(TeleRect); }; painter->drawPixmap(WideRect, *m_pPixWide); painter->drawPixmap(TeleRect, *m_pPixTele); }; painter->fillRect(ManualRect, QColor(255, 255, 255, 63)); painter->drawPixmap(ManualRect, *m_pManualMode); }; }; //char buffer[64]; //sprintf(buffer, "Coords: %dx%d %d %d ? %d %d", pos().x(), pos().y(), width(), height(), m_nMouseX, m_nMouseY); //painter->save(); //painter->fillRect(QRect(0, 0, width(), height()), Qt::green); //int x, y; //x = width(); //y = height(); //painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap); //QPen BgPen(Qt::SolidPattern,2); //BgPen.setColor(Qt::black); //QPen FgPen(Qt::SolidPattern,0); //FgPen.setColor(Qt::yellow); //painter->setPen(BgPen); //painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, buffer); //painter->setPen(FgPen); //painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline , buffer); painter->restore(); }catch (std::exception& e){ LOG_ERROR("Render exception!"); }; }; } bool FSVideoWidget::init( void ){ LOG("init."); m_pPixmap = new QPixmap(width(), height()); //, QImage::Format_RGB888 ); if( m_pPixmap==NULL ) return false; //m_pOverlayPixmap = new QPixmap("640x480.png", "PNG"); m_pManualMode = new QPixmap(":pictures/appbar.controller.snes.png", "PNG"); m_pPixUp = new QPixmap(":pictures/appbar.chevron.up.png", "PNG"); m_pPixDown = new QPixmap(":pictures/appbar.chevron.down.png", "PNG"); m_pPixLeft = new QPixmap(":pictures/appbar.chevron.left.png", "PNG"); m_pPixRight = new QPixmap(":pictures/appbar.chevron.right.png", "PNG"); m_pPixWide = new QPixmap(":pictures/appbar.arrow.expand.png", "PNG"); m_pPixTele = new QPixmap(":pictures/appbar.arrow.collapsed.png", "PNG"); m_pTimer = new QTimer(); if (m_pTimer != NULL){ connect(m_pTimer, SIGNAL(timeout()), this, SLOT(update())); m_pTimer->start(40); }; m_eWidgetAspectRatio = detectAspectRatio(width(), height()); m_pPixmap->fill(Qt::black); m_pPropertiesProtectorMutex->lock(); m_pPropertiesProtectorMutex->unlock(); return false; }; FSVideoWidget::~FSVideoWidget(){ if (m_pCamera)m_pCamera->setViewer(NULL); if (m_pTimer)m_pTimer->stop(); delete( m_pPropertiesProtectorMutex ); delete( m_pWidgetProtectorMutex ); }; void FSVideoWidget::setGrayScale( bool AAA ){ m_pPropertiesProtectorMutex->lock(); m_bGrayScale = AAA; m_pPropertiesProtectorMutex->unlock(); LOG("GRAYSCALE"); }; void FSVideoWidget::setEnhanced( bool AAA ){ m_pPropertiesProtectorMutex->lock(); m_bEnhanced = AAA; m_pPropertiesProtectorMutex->unlock(); LOG("ENHANCED"); }; void FSVideoWidget::pushFrame(cv::Mat *Frame, void *src){ if( Frame != NULL ){ try{ m_pWidgetProtectorMutex->lock(); m_mOutput = Frame; assert(m_mOutput->isContinuous()); m_eStreamAspectRatio = detectAspectRatio(Frame->cols, Frame->rows); QPainter dbPainter(m_pPixmap); if (m_eStreamAspectRatio != m_eWidgetAspectRatio){ int tOffset; if (m_eStreamAspectRatio > m_eWidgetAspectRatio){ int tHeight; tHeight = (int)((float)m_pPixmap->width() / m_eStreamAspectRatio); tOffset = (m_pPixmap->height() - tHeight) >> 1; dbPainter.drawImage(QRect(0, tOffset, width(), tHeight ), QImage((unsigned char*)m_mOutput->data, m_mOutput->cols, m_mOutput->rows, QImage::Format_RGB888), QRect(0, 0, m_mOutput->cols, m_mOutput->rows )); } else { int tWidth; tWidth = (int)((float)m_pPixmap->height() * m_eStreamAspectRatio); tOffset = (m_pPixmap->width() - tWidth) >> 1; dbPainter.drawImage(QRect(tOffset, 0, tWidth, 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)); }; } else { 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)); }; m_pWidgetProtectorMutex->unlock(); } catch (std::exception& e){ LOG_ERROR("Exception catched:"); LOG_ERROR(e.what()); }; m_pPropertiesProtectorMutex->unlock(); //delete(Frame); }; }; #undef LOG #undef LOG_ERROR