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

#include "VideoThumbWidget.h"
#include "VideoSourceChooser.h"
#include "../dialogs/SettingsCameraDialog.h"
#define LOG( X ) m_pLog->Log( "VideoThumbWidget", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "VideoThumbWidget", this, _LOG_ERROR, X )
VideoThumbWidget::VideoThumbWidget(QWidget *parent) :
QWidget(parent),
basicMediaDestination(){
m_bGrayScale = false;
m_bEnhanced = false;
m_bDirectDrawing = true;
m_bUpdateIsPending = false;
m_pSignalParameter = NULL;
m_pSettingsDialog = NULL;
setFixedSize( 240, 180 );
setMouseTracking( true );
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_pPixmap = NULL;
m_pOverlayPixmap = NULL;
m_pGraber = NULL;
m_mFrame = NULL;
m_mOutput = NULL;
m_pTimer = NULL;
m_bMouseActive = false;
m_bSelected = false;
m_bErrorMode = false;
m_pErrorMsg = NULL;
m_nMouseX = 0;
m_nMouseY = 0;
m_pPropertiesProtectorMutex = new basicMutex();
m_pWidgetProtectorMutex = new basicMutex();
m_pPropertiesProtectorMutex->lock();
m_pWidgetProtectorMutex->lock();
//this->setContextMenuPolicy(Qt::CustomContextMenu);
//connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
// this, SLOT(ShowContextMenu(const QPoint &)));
};
void VideoThumbWidget::ShowContextMenu(const QPoint &pos){
QMenu contextMenu("Context menu", this);
QAction action1("Togle Enhanced Mode", this);
action1.setIcon(QIcon(":pictures/appbar.candle.png"));
connect(&action1, &QAction::triggered, this, &VideoThumbWidget::setEnhanced);
QAction action2("Togle Video B/W", this);
action2.setIcon(QIcon(":pictures/appbar.alien.png"));
connect(&action2, &QAction::triggered, this, &VideoThumbWidget::setGrayScale);
QAction action3("Camera Settings", this);
action3.setIcon(QIcon(":pictures/appbar.cog.png"));
connect(&action3, &QAction::triggered, this, &VideoThumbWidget::showCameraSettings);
QAction action4("Remove Camera", this);
action4.setIcon(QIcon(":pictures/appbar.close.png"));
connect(&action4, &QAction::triggered, this, &VideoThumbWidget::removeThisCamera);
contextMenu.addAction(&action1);
contextMenu.addAction(&action2);
contextMenu.addAction(&action3);
contextMenu.addAction(&action4);
contextMenu.exec(mapToGlobal(pos));
};
bool VideoThumbWidget::event(QEvent *event){
if (event->type() == QEvent::UpdateRequest){
m_bUpdateIsPending = false;
renderNow();
return true;
}
if (event->type() == QEvent::MouseButtonRelease){
//m_bSelected = (m_bSelected) ? false : true;
if (((QMouseEvent*)event)->button() == Qt::RightButton){
ShowContextMenu(QPoint(((QMouseEvent*)event)->x(), ((QMouseEvent*)event)->y()));
} else selected( m_pSignalParameter );
return true;
}
if (event->type() == QEvent::MouseButtonDblClick){
m_bEnhanced = (m_bEnhanced) ? false : true;
renderLater();
return true;
}
if (event->type() == QEvent::MouseMove){
m_nMouseX = ((QMouseEvent*)event)->x();
m_nMouseY = ((QMouseEvent*)event)->y();
m_bMouseActive = true;
m_nMenuActivityCounter = 75;// 3 Sec
};
return QWidget::event(event);
}
//! [7]
//! [6]
void VideoThumbWidget::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 VideoThumbWidget::selected(void *P){
((VideoSourceChooser*)m_pParent)->selectForMainPreview(P);
};
void VideoThumbWidget::exposeEvent(QExposeEvent *){
LOG_ERROR("Expose EVT!");
}
//! [2]
void VideoThumbWidget::paintEvent(QPaintEvent *evt){
renderNow();
}
//! [3]
void VideoThumbWidget::renderNow(){
m_pWidgetProtectorMutex->lock();
try{
//if (m_nMenuActivityCounter)m_nMenuActivityCounter--;
//else if (m_bMouseActive)m_bMouseActive = false;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
//painter.begin(this);
render(&painter);
//painter.end();
}catch (std::exception& e){
LOG_ERROR("RenderNow exception!");
};
m_pWidgetProtectorMutex->unlock();
}
//! [3]
//! [4]
void VideoThumbWidget::render(QPainter *painter){
if( painter != NULL ){
try{
//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);
QPen BgPen(Qt::SolidPattern, 3);
BgPen.setColor(Qt::black);
if( m_bErrorMode && m_pOverlayPixmap!= NULL ){
painter->drawPixmap(QRect(0, 0, width(), height()), *m_pOverlayPixmap);
if (m_pErrorMsg != NULL){
QPen FgPen(Qt::SolidPattern, 0);
FgPen.setColor(Qt::yellow);
painter->setPen(BgPen);
painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, m_pErrorMsg);
painter->setPen(FgPen);
painter->drawText(QRect(0, 0, width(), height()), Qt::AlignHCenter | Qt::AlignBaseline, m_pErrorMsg);
};
} else {
painter->drawPixmap(QRect(0, 0, width(), height()), *m_pPixmap);
};
if (m_bSelected){
BgPen.setColor(Qt::blue);
painter->setPen(BgPen);
painter->drawRect(QRect(2, 2, width() - 4, height() - 4));
};
//if (m_bMouseActive){
// if (m_pOverlayPixmap != NULL){
// painter->drawPixmap(QRect(0, 0, width(), height()), *m_pOverlayPixmap);
// };
//};
painter->restore();
}catch (std::exception& e){
LOG_ERROR("Render exception!");
};
};
}
bool VideoThumbWidget::init( void ){
LOG("init.");
m_pPixmap = new QPixmap(width(), height()); //, QImage::Format_RGB888 );
if( m_pPixmap==NULL ) return false;
m_pOverlayPixmap = new QPixmap(":pictures/43a.png", "PNG");
m_pTimer = new QTimer();
if (m_pTimer != NULL){
QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT( update()));
m_pTimer->start(40);
};
m_pPixmap->fill(Qt::black);
m_pPropertiesProtectorMutex->unlock();
m_pWidgetProtectorMutex->unlock();
return false;
};
VideoThumbWidget::~VideoThumbWidget(){
if (m_pTimer){
m_pTimer->stop();
delete(m_pTimer);
};
delete( m_pPropertiesProtectorMutex );
delete( m_pWidgetProtectorMutex );
};
void VideoThumbWidget::setGrayScale(){
m_pPropertiesProtectorMutex->lock();
m_bGrayScale = (m_bGrayScale) ? false : true;
m_pPropertiesProtectorMutex->unlock();
};
void VideoThumbWidget::setEnhanced(){
m_pPropertiesProtectorMutex->lock();
m_bEnhanced = (m_bEnhanced) ? false : true;
m_pPropertiesProtectorMutex->unlock();
};
void VideoThumbWidget::setError(bool AAA, CSTR Message){
m_bErrorMode = AAA;
m_pErrorMsg = Message;
if (AAA) LOG("ERROR TRUE"); else LOG("ERROR FALSE");
};
void VideoThumbWidget::pushFrame(cv::Mat *Frame, void *src){
if (Frame != NULL && m_pPixmap!= NULL){
m_pPropertiesProtectorMutex->lock();
m_eStreamAspectRatio = detectAspectRatio(Frame->cols, Frame->rows);
if (m_bGrayScale) {
cvtColor(*Frame, *Frame, CV_BGR2GRAY);
};
if (m_bEnhanced) {
if (!m_bGrayScale) {
vector<cv::Mat> channels;
cv::Mat img_hist_equalized;
cvtColor(*Frame, img_hist_equalized, CV_BGR2YCrCb); //change the color image from BGR to YCrCb format
cv::split(img_hist_equalized, channels); //split the image into channels
cv::equalizeHist(channels[0], channels[0]); //equalize histogram on the 1st channel (Y)
merge(channels, img_hist_equalized); //merge 3 channels including the modified 1st channel into one image
cvtColor(img_hist_equalized, *Frame, CV_YCrCb2BGR); //change the color image from YCrCb to BGR format (to display image properly)
} else {
equalizeHist(*Frame, *Frame);
};
};
if (!m_bGrayScale){
cvtColor(*Frame, *Frame, CV_BGR2RGB);
} else {
cvtColor(*Frame, *Frame, CV_GRAY2RGB);
};
m_pPropertiesProtectorMutex->unlock();
m_mOutput = Frame;
try{
assert(m_mOutput->isContinuous());
m_pWidgetProtectorMutex->lock();
assert(m_pPixmap);
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());
};
//delete(Frame);
};
};
void VideoThumbWidget::showCameraSettings(){
if (m_pSettingsDialog == NULL){
m_pSettingsDialog = new DialogSettingsCamera(this);
}
m_pSettingsDialog->attachDevice( m_pSignalParameter );
m_pSettingsDialog->show();
};
void VideoThumbWidget::removeThisCamera(){
if( m_pParent != NULL ){
if (m_pSignalParameter != NULL){
m_pParent->removeCamera(m_pSignalParameter);
};
};
};
#undef LOG
#undef LOG_ERROR