New functions with QT GUI:

- save/load window parameters implemented !
This commit is contained in:
Yannick Verdie 2010-06-29 22:14:24 +00:00
parent 6f10d9cea8
commit 2f4d396506
5 changed files with 416 additions and 301 deletions

View File

@ -67,6 +67,8 @@ CV_EXPORTS double getWindowProperty(const string& winname, int prop_id);//YV
//Only for QT
CV_EXPORTS void displayOverlay(const string& winname, const string& text, int delayms);
CV_EXPORTS void displayStatusBar(const string& winname, const string& text, int delayms);
CV_EXPORTS void saveWindowParameters(const string& windowName);
CV_EXPORTS void loadWindowParameters(const string& windowName);
CV_EXPORTS int startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]);
CV_EXPORTS void stopLoop();

View File

@ -60,6 +60,8 @@ extern "C" {
//-----------New for QT
CVAPI(void) cvDisplayOverlay(const char* name, const char* text, int delayms);
CVAPI(void) cvDisplayStatusBar(const char* name, const char* text, int delayms);
CVAPI(void) cvSaveWindowParameters(const char* name);
CVAPI(void) cvLoadWindowParameters(const char* name);
CVAPI(int) cvStartLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]);
CVAPI(void) cvStopLoop();
//----------------------

View File

@ -160,6 +160,16 @@ int waitKey(int delay)
return cvWaitKey(delay);
}
void saveWindowParameters(const string& windowName)
{
cvSaveWindowParameters(windowName.c_str());
}
void loadWindowParameters(const string& windowName)
{
cvLoadWindowParameters(windowName.c_str());
}
int createTrackbar(const string& trackbarName, const string& winName,
int* value, int count, TrackbarCallback callback,
void* userdata)

View File

@ -42,7 +42,6 @@
#ifdef HAVE_QT
#include <window_QT.h>
#include <QVarLengthArray>
//Static and global first
static GuiReceiver guiMainThread;
@ -132,6 +131,22 @@ CV_IMPL void cvDisplayOverlay(const char* name, const char* text, int delayms)
}
CV_IMPL void cvSaveWindowParameters(const char* name)
{
QMetaObject::invokeMethod(&guiMainThread,
"saveWindowParameters",
Qt::AutoConnection,
Q_ARG(QString, QString(name)));
}
CV_IMPL void cvLoadWindowParameters(const char* name)
{
QMetaObject::invokeMethod(&guiMainThread,
"loadWindowParameters",
Qt::AutoConnection,
Q_ARG(QString, QString(name)));
}
CV_IMPL void cvDisplayStatusBar(const char* name, const char* text, int delayms)
{
@ -235,7 +250,7 @@ CV_IMPL CvWindow* icvFindWindowByName( const char* arg )
foreach (QWidget *widget, QApplication::topLevelWidgets())
{
w = (CvWindow*) widget;
if (w->name==name)
if (w->param_name==name)
{
window = w;
break;
@ -258,8 +273,9 @@ CvTrackbar* icvFindTrackbarByName( const char* name_trackbar, const char* name_w
QString nameQt = QString(name_trackbar);
QPointer<CvTrackbar> t;
//Warning ---- , asume the location 0 is myview and max-1 the status bar
for (int i = 1; i < w->layout->layout()->count()-2; ++i)
//Warning ---- , asume the location 0 is myview and max-1 the status bar
//done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar
for (int i = 1; i < w->layout->layout()->count()-1; ++i)
{
t = (CvTrackbar*) w->layout->layout()->itemAt(i);
@ -467,6 +483,22 @@ GuiReceiver::GuiReceiver() : _bTimeOut(false)
qApp->setQuitOnLastWindowClosed ( false );//maybe the user would like to access this setting
}
void GuiReceiver::saveWindowParameters(QString name)
{
QPointer<CvWindow> w = icvFindWindowByName( name.toLatin1().data() );
if (w)
w->writeSettings();
}
void GuiReceiver::loadWindowParameters(QString name)
{
QPointer<CvWindow> w = icvFindWindowByName( name.toLatin1().data() );
if (w)
w->readSettings();
}
double GuiReceiver::getRatioWindow(QString name)
{
QPointer<CvWindow> w = icvFindWindowByName( name.toLatin1().data() );
@ -504,7 +536,7 @@ double GuiReceiver::getPropWindow(QString name)
if (!w)
return -1;
return (double)w->flags;
return (double)w->param_flags;
}
void GuiReceiver::setPropWindow(QString name, double arg2 )
@ -516,7 +548,7 @@ void GuiReceiver::setPropWindow(QString name, double arg2 )
int flags = (int) arg2;
if (w->flags == flags)//nothing to do
if (w->param_flags == flags)//nothing to do
return;
@ -524,11 +556,11 @@ void GuiReceiver::setPropWindow(QString name, double arg2 )
{
case CV_WINDOW_NORMAL:
w->layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
w->flags = flags;
w->param_flags = flags;
break;
case CV_WINDOW_AUTOSIZE:
w->layout->setSizeConstraint(QLayout::SetFixedSize);
w->flags = flags;
w->param_flags = flags;
break;
default:;
}
@ -757,16 +789,16 @@ void CvTrackbar::createDialog()
int max = slider->maximum();
int i = QInputDialog::getInt(this->parentWidget(),
tr("Slider %1").arg(trackbar_name),
tr("New value:"),
value,
min,
max,
step,
&ok);
tr("Slider %1").arg(trackbar_name),
tr("New value:"),
value,
min,
max,
step,
&ok);
if (ok)
slider->setValue(i);
slider->setValue(i);
}
@ -776,7 +808,7 @@ void CvTrackbar::update(int myvalue)
*dataSlider = myvalue;
if (callback)
callback(myvalue);
callback(myvalue);
}
void CvTrackbar::setLabel(int myvalue)
@ -796,13 +828,13 @@ CvTrackbar::~CvTrackbar()
CvWindow::CvWindow(QString arg, int arg2)
{
moveToThread(qApp->instance()->thread());
name = arg;
flags = arg2;
param_name = arg;
param_flags = arg2;
setAttribute(Qt::WA_DeleteOnClose);//in other case, does not release memory
setContentsMargins(0,0,0,0);
setWindowTitle(name);
setObjectName(name);
setWindowTitle(param_name);
setObjectName(param_name);
resize(400,300);
@ -811,22 +843,20 @@ CvWindow::CvWindow(QString arg, int arg2)
myview->setAlignment(Qt::AlignHCenter);
shortcut_r_Zoom = new QShortcut(Qt::CTRL + Qt::Key_P, this);
QObject::connect( shortcut_r_Zoom, SIGNAL( activated ()),myview, SLOT( resetZoom( ) ));
shortcut_imgRegion = new QShortcut(Qt::CTRL + Qt::Key_O, this);
QObject::connect( shortcut_imgRegion, SIGNAL( activated ()),myview, SLOT( imgRegion( ) ));
shortcut_Plus = new QShortcut(QKeySequence(QKeySequence::ZoomIn), this);
QObject::connect( shortcut_Plus, SIGNAL( activated ()),myview, SLOT( ZoomIn() ));
shortcut_Minus = new QShortcut(QKeySequence(QKeySequence::ZoomOut), this);
QObject::connect(shortcut_Minus, SIGNAL( activated ()),myview, SLOT( ZoomOut() ));
shortcut_Left = new QShortcut(Qt::CTRL + Qt::Key_Left, this);
QObject::connect( shortcut_Left, SIGNAL( activated ()),myview, SLOT( siftWindowOnLeft() ));
shortcut_Right = new QShortcut(Qt::CTRL + Qt::Key_Right, this);
QObject::connect( shortcut_Right, SIGNAL( activated ()),myview, SLOT( siftWindowOnRight() ));
shortcut_Up = new QShortcut(Qt::CTRL + Qt::Key_Up, this);
QObject::connect(shortcut_Up, SIGNAL( activated ()),myview, SLOT( siftWindowOnUp() ));
shortcut_Down = new QShortcut(Qt::CTRL + Qt::Key_Down, this);
QObject::connect(shortcut_Down, SIGNAL( activated ()),myview, SLOT( siftWindowOnDown() ));
shortcutZ = new QShortcut(Qt::CTRL + Qt::Key_P, this);
QObject::connect( shortcutZ, SIGNAL( activated ()),myview, SLOT( resetZoom( ) ));
shortcutPlus = new QShortcut(QKeySequence(QKeySequence::ZoomIn), this);
QObject::connect( shortcutPlus, SIGNAL( activated ()),myview, SLOT( ZoomIn() ));
shortcutMinus = new QShortcut(QKeySequence(QKeySequence::ZoomOut), this);
QObject::connect(shortcutMinus, SIGNAL( activated ()),myview, SLOT( ZoomOut() ));
shortcutLeft = new QShortcut(Qt::CTRL + Qt::Key_Left, this);
QObject::connect( shortcutLeft, SIGNAL( activated ()),myview, SLOT( siftWindowOnLeft() ));
shortcutRight = new QShortcut(Qt::CTRL + Qt::Key_Right, this);
QObject::connect( shortcutRight, SIGNAL( activated ()),myview, SLOT( siftWindowOnRight() ));
shortcutUp = new QShortcut(Qt::CTRL + Qt::Key_Up, this);
QObject::connect(shortcutUp, SIGNAL( activated ()),myview, SLOT( siftWindowOnUp() ));
shortcutDown = new QShortcut(Qt::CTRL + Qt::Key_Down, this);
QObject::connect(shortcutDown, SIGNAL( activated ()),myview, SLOT( siftWindowOnDown() ));
layout = new QBoxLayout(QBoxLayout::TopToBottom);
//layout = new CustomLayout;
@ -837,8 +867,8 @@ CvWindow::CvWindow(QString arg, int arg2)
layout->addWidget(myview,Qt::AlignCenter);
//layout->addStretch(0);
if (flags == CV_WINDOW_AUTOSIZE)
layout->setSizeConstraint(QLayout::SetFixedSize);
if (param_flags == CV_WINDOW_AUTOSIZE)
layout->setSizeConstraint(QLayout::SetFixedSize);
//now status bar
myBar = new QStatusBar;
@ -864,29 +894,28 @@ CvWindow::~CvWindow()
if (layout)
{
while ((child = layout->takeAt(0)) != 0)
delete child;
while ((child = layout->takeAt(0)) != 0)
delete child;
delete layout;
delete layout;
}
delete myBar;
delete myBar_msg;
delete shortcut_r_Zoom;
delete shortcut_imgRegion;
delete shortcut_Plus;
delete shortcut_Minus;
delete shortcut_Left;
delete shortcut_Right;
delete shortcut_Up;
delete shortcut_Down;
delete shortcutZ;
delete shortcutPlus;
delete shortcutMinus;
delete shortcutLeft;
delete shortcutRight;
delete shortcutUp;
delete shortcutDown;
}
ViewPort* CvWindow::getView()
{
return myview;
return myview;
}
void CvWindow::displayInfo(QString text,int delayms)
@ -925,53 +954,128 @@ void CvWindow::keyPressEvent(QKeyEvent *event)
if (key>=20 && key<=255 )
{
key = (int)event->text().toLocal8Bit().at(0);
goodKey = true;
key = (int)event->text().toLocal8Bit().at(0);
goodKey = true;
}
if (key == Qt::Key_Escape)
{
key = 27;
goodKey = true;
key = 27;
goodKey = true;
}
//control plus (Z, +, -, up, down, left, right) are used for zoom/panning functions
if (event->modifiers() != Qt::ControlModifier && goodKey)
{
mutexKey.lock();
last_key = key;
//last_key = event->nativeVirtualKey ();
mutexKey.unlock();
key_pressed.wakeAll();
//event->accept();
mutexKey.lock();
last_key = key;
//last_key = event->nativeVirtualKey ();
mutexKey.unlock();
key_pressed.wakeAll();
//event->accept();
}
QWidget::keyPressEvent(event);
}
void CvWindow::readSettings()//not tested
void CvWindow::readSettings()
{
QSettings settings("Trolltech", "Application Example");
//organisation and application's name
QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName());
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
param_name = settings.value("name_window",param_name).toString();
param_flags = settings.value("mode_resize",param_flags).toInt();
qreal m11 = settings.value("matrix_view.m11",myview->param_matrixWorld.m11()).toReal();
qreal m12 = settings.value("matrix_view.m12",myview->param_matrixWorld.m12()).toReal();
qreal m13 = settings.value("matrix_view.m13",myview->param_matrixWorld.m13()).toReal();
qreal m21 = settings.value("matrix_view.m21",myview->param_matrixWorld.m21()).toReal();
qreal m22 = settings.value("matrix_view.m22",myview->param_matrixWorld.m22()).toReal();
qreal m23 = settings.value("matrix_view.m23",myview->param_matrixWorld.m23()).toReal();
qreal m31 = settings.value("matrix_view.m31",myview->param_matrixWorld.m31()).toReal();
qreal m32 = settings.value("matrix_view.m32",myview->param_matrixWorld.m32()).toReal();
qreal m33 = settings.value("matrix_view.m33",myview->param_matrixWorld.m33()).toReal();
myview->param_matrixWorld = QTransform(m11,m12,m13,m21,m22,m23,m31,m32,m33);
//trackbar here
icvLoadTrackbars(&settings);
resize(size);
move(pos);
}
void CvWindow::writeSettings()//not tested
void CvWindow::writeSettings()
{
QSettings settings("Trolltech", "Application Example");
//organisation and application's name
QSettings settings("OpenCV2", QFileInfo(QApplication::applicationFilePath()).fileName());
settings.setValue("name_window",param_name);
settings.setValue("pos", pos());
settings.setValue("size", size());
settings.setValue("mode_resize",param_flags);
settings.setValue("view_aspectRatio",myview->param_keepRatio);
settings.setValue("matrix_view.m11",myview->param_matrixWorld.m11());
settings.setValue("matrix_view.m12",myview->param_matrixWorld.m12());
settings.setValue("matrix_view.m13",myview->param_matrixWorld.m13());
settings.setValue("matrix_view.m21",myview->param_matrixWorld.m21());
settings.setValue("matrix_view.m22",myview->param_matrixWorld.m22());
settings.setValue("matrix_view.m23",myview->param_matrixWorld.m23());
settings.setValue("matrix_view.m31",myview->param_matrixWorld.m31());
settings.setValue("matrix_view.m32",myview->param_matrixWorld.m32());
settings.setValue("matrix_view.m33",myview->param_matrixWorld.m33());
icvSaveTrackbars(&settings);
}
void CvWindow::icvLoadTrackbars(QSettings *settings)
{
int size = settings->beginReadArray("trackbars");
QPointer<CvTrackbar> t;
//Warning ---- , asume the location 0 is myview and max-1 the status bar
//done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar
//trackbar are saved in the same order, so no need to use icvFindTrackbarByName
if (layout->layout()->count()-2 == size)//if not the same number, the window saved and loaded is not the same (nb trackbar not equal)
for (int i = 0; i < size; ++i)
{
settings->setArrayIndex(i);
t = (CvTrackbar*) layout->layout()->itemAt(i+1);//+1 because index 0 is myview (see Warning)
if (t->trackbar_name == settings->value("name").toString())
{
t->slider->setValue(settings->value("value").toInt());
}
}
settings->endArray();
}
void CvWindow::icvSaveTrackbars(QSettings *settings)
{
QPointer<CvTrackbar> t;
//Warning ---- , asume the location 0 is myview and max-1 the status bar
//done three times in the code, in loadtrackbars, savetrackbar and in findtrackbar
settings->beginWriteArray("trackbars");
for (int i = 0; i < layout->layout()->count()-2; ++i) {
t = (CvTrackbar*) layout->layout()->itemAt(i+1);//+1 because index 0 is myview (see Warning)
settings->setArrayIndex(i);
settings->setValue("name", t->trackbar_name);
settings->setValue("value", t->slider->value());
}
settings->endArray();
}
//Here is ViewPort
ViewPort::ViewPort(CvWindow* arg, int arg2, int arg3)
{
centralWidget = arg,
mode = arg2;
keepRatio = arg3;
mode_display = arg2;
param_keepRatio = arg3;
setupViewport(centralWidget);
setContentsMargins(0,0,0,0);
@ -988,7 +1092,7 @@ ViewPort::ViewPort(CvWindow* arg, int arg2, int arg3)
#if defined(OPENCV_GL)
if (mode == CV_MODE_OPENGL)
if ( mode_display == CV_MODE_OPENGL)
{
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
initGL();
@ -1013,62 +1117,57 @@ ViewPort::~ViewPort()
void ViewPort::setRatio(int flags)
{
keepRatio = flags;
param_keepRatio = flags;
updateGeometry();
viewport()->update();
}
int ViewPort::getRatio()
{
return keepRatio;
return param_keepRatio;
}
void ViewPort::resetZoom()
{
matrixWorld.reset();
param_matrixWorld.reset();
controlImagePosition();
}
void ViewPort::imgRegion( )
{
scaleView(threshold_zoom_img_region/matrixWorld.m11(), QPointF(size().width()/2,size().height()/2),false);//false = do not process the 1st param
}
void ViewPort::ZoomIn()
{
scaleView( 0.5,QPointF(size().width()/2,size().height()/2),true);
scaleView( 0.5,QPointF(size().width()/2,size().height()/2));
}
void ViewPort::ZoomOut()
{
scaleView( -0.5,QPointF(size().width()/2,size().height()/2),true);
scaleView( -0.5,QPointF(size().width()/2,size().height()/2));
}
//Note: move 2 percent of the window
void ViewPort::siftWindowOnLeft()
{
float delta = 2*width()/(100.0*matrixWorld.m11());
float delta = 2*width()/(100.0*param_matrixWorld.m11());
moveView(QPointF(delta,0));
}
//Note: move 2 percent of the window
void ViewPort::siftWindowOnRight()
{
float delta = -2*width()/(100.0*matrixWorld.m11());
float delta = -2*width()/(100.0*param_matrixWorld.m11());
moveView(QPointF(delta,0));
}
//Note: move 2 percent of the window
void ViewPort::siftWindowOnUp()
{
float delta = 2*height()/(100.0*matrixWorld.m11());
float delta = 2*height()/(100.0*param_matrixWorld.m11());
moveView(QPointF(0,delta));
}
//Note: move 2 percent of the window
void ViewPort::siftWindowOnDown()
{
float delta = -2*height()/(100.0*matrixWorld.m11());
float delta = -2*height()/(100.0*param_matrixWorld.m11());
moveView(QPointF(0,delta));
}
@ -1125,30 +1224,30 @@ void ViewPort::controlImagePosition()
qreal left, top, right, bottom;
//after check top-left, bottom right corner to avoid getting "out" during zoom/panning
matrixWorld.map(0,0,&left,&top);
param_matrixWorld.map(0,0,&left,&top);
if (left > 0)
{
matrixWorld.translate(-left,0);
param_matrixWorld.translate(-left,0);
left = 0;
}
if (top > 0)
{
matrixWorld.translate(0,-top);
param_matrixWorld.translate(0,-top);
top = 0;
}
//-------
QSize sizeImage = size();
matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom);
param_matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom);
if (right < sizeImage.width())
{
matrixWorld.translate(sizeImage.width()-right,0);
param_matrixWorld.translate(sizeImage.width()-right,0);
right = sizeImage.width();
}
if (bottom < sizeImage.height())
{
matrixWorld.translate(0,sizeImage.height()-bottom);
param_matrixWorld.translate(0,sizeImage.height()-bottom);
bottom = sizeImage.height();
}
@ -1156,51 +1255,48 @@ void ViewPort::controlImagePosition()
positionCorners.setTopLeft(QPoint(left,top));
positionCorners.setBottomRight(QPoint(right,bottom));
//save also the inv matrix
matrixWorld_inv = matrixWorld.inverted();
matrixWorld_inv = param_matrixWorld.inverted();
viewport()->update();
}
void ViewPort::moveView(QPointF delta)
{
matrixWorld.translate(delta.x(),delta.y());
param_matrixWorld.translate(delta.x(),delta.y());
controlImagePosition();
}
//factor is -0.5 (zoom out) or 0.5 (zoom in)
void ViewPort::scaleView(qreal factor,QPointF center,bool process_factor)
void ViewPort::scaleView(qreal factor,QPointF center)
{
if (process_factor)
{
factor/=5;//-0.1 <-> 0.1
factor+=1;//0.9 <-> 1.1
}
factor/=5;//-0.1 <-> 0.1
factor+=1;//0.9 <-> 1.1
//limit zoom out ---
if (matrixWorld.m11()==1 && factor < 1)
if (param_matrixWorld.m11()==1 && factor < 1)
return;
if (matrixWorld.m11()*factor<1)
factor = 1/matrixWorld.m11();
if (param_matrixWorld.m11()*factor<1)
factor = 1/param_matrixWorld.m11();
//limit zoom int ---
if (matrixWorld.m11()>100 && factor > 1)
if (param_matrixWorld.m11()>100 && factor > 1)
return;
//inverse the transform
int a, b;
matrixWorld_inv.map(center.x(),center.y(),&a,&b);
matrixWorld.translate(a-factor*a,b-factor*b);
matrixWorld.scale(factor,factor);
param_matrixWorld.translate(a-factor*a,b-factor*b);
param_matrixWorld.scale(factor,factor);
controlImagePosition();
//display new zoom
centralWidget->displayStatusBar(tr("Zoom: %1%").arg(matrixWorld.m11()*100),1000);
centralWidget->displayStatusBar(tr("Zoom: %1%").arg(param_matrixWorld.m11()*100),1000);
if (matrixWorld.m11()>1)
if (param_matrixWorld.m11()>1)
setCursor(Qt::OpenHandCursor);
else
unsetCursor();
@ -1208,7 +1304,7 @@ void ViewPort::scaleView(qreal factor,QPointF center,bool process_factor)
void ViewPort::wheelEvent(QWheelEvent *event)
{
scaleView( -event->delta() / 240.0,event->pos(),true);//true means process the 1st parameter
scaleView( -event->delta() / 240.0,event->pos());
}
void ViewPort::mousePressEvent(QMouseEvent *event)
@ -1220,7 +1316,7 @@ void ViewPort::mousePressEvent(QMouseEvent *event)
icvmouseHandler(event, mouse_down, cv_event, flags);
icvmouseProcessing(QPointF(pt), cv_event, flags);
if (matrixWorld.m11()>1)
if (param_matrixWorld.m11()>1)
{
setCursor(Qt::ClosedHandCursor);
positionGrabbing = event->pos();
@ -1239,7 +1335,7 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
icvmouseHandler(event, mouse_up, cv_event, flags);
icvmouseProcessing(QPointF(pt), cv_event, flags);
if (matrixWorld.m11()>1)
if (param_matrixWorld.m11()>1)
setCursor(Qt::OpenHandCursor);
QWidget::mouseReleaseEvent(event);
@ -1267,9 +1363,9 @@ void ViewPort::mouseMoveEvent(QMouseEvent *event)
icvmouseProcessing(QPointF(pt), cv_event, flags);
if (matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton)
if (param_matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton)
{
QPointF dxy = (pt - positionGrabbing)/matrixWorld.m11();
QPointF dxy = (pt - positionGrabbing)/param_matrixWorld.m11();
positionGrabbing = event->pos();
@ -1357,7 +1453,7 @@ void ViewPort::resizeEvent ( QResizeEvent *event)
ratioX=width()/float(image2Draw_ipl->width);
ratioY=height()/float(image2Draw_ipl->height);
if(keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio
if(param_keepRatio == CV_WINDOW_KEEPRATIO)//to keep the same aspect ratio
{
QSize newSize = QSize(image2Draw_ipl->width,image2Draw_ipl->height);
newSize.scale(event->size(),Qt::KeepAspectRatio);
@ -1382,13 +1478,13 @@ void ViewPort::resizeEvent ( QResizeEvent *event)
void ViewPort::paintEvent(QPaintEvent* event)
{
QPainter myPainter(viewport());
myPainter.setWorldTransform(matrixWorld);
myPainter.setWorldTransform(param_matrixWorld);
draw2D(&myPainter);
#if defined(OPENCV_GL)
if (mode == CV_MODE_OPENGL && false)//disable it for now
if ( mode_display == CV_MODE_OPENGL && false)//disable it for now
{
setGL(this->width(),this->height());
draw3D();
@ -1397,12 +1493,12 @@ void ViewPort::paintEvent(QPaintEvent* event)
#endif
//in mode zoom/panning
if (matrixWorld.m11()>1)
if (param_matrixWorld.m11()>1)
{
myPainter.setWorldMatrixEnabled (false );
if (matrixWorld.m11()>=threshold_zoom_img_region)
if (param_matrixWorld.m11()>=threshold_zoom_img_region)
drawImgRegion(&myPainter);
drawViewOverview(&myPainter);
@ -1463,23 +1559,23 @@ void ViewPort::drawStatusBar()
void ViewPort::drawImgRegion(QPainter *painter)
{
qreal offsetX = matrixWorld.dx()/matrixWorld.m11();
qreal offsetX = param_matrixWorld.dx()/param_matrixWorld.m11();
offsetX = offsetX - floor(offsetX);
qreal offsetY = matrixWorld.dy()/matrixWorld.m11();
qreal offsetY = param_matrixWorld.dy()/param_matrixWorld.m11();
offsetY = offsetY - floor(offsetY);
QSize view = size();
QVarLengthArray<QLineF, 30> linesX;
for (qreal x = offsetX*matrixWorld.m11(); x < view.width(); x += matrixWorld.m11() )
linesX.append(QLineF(x, 0, x, view.height()));
for (qreal x = offsetX*param_matrixWorld.m11(); x < view.width(); x += param_matrixWorld.m11() )
linesX.append(QLineF(x, 0, x, view.height()));
QVarLengthArray<QLineF, 30> linesY;
for (qreal y = offsetY*matrixWorld.m11(); y < view.height(); y += matrixWorld.m11() )
for (qreal y = offsetY*param_matrixWorld.m11(); y < view.height(); y += param_matrixWorld.m11() )
linesY.append(QLineF(0, y, view.width(), y));
QFont f = painter->font();
f.setPointSize(threshold_zoom_img_region/3+(matrixWorld.m11()-threshold_zoom_img_region)/6);
f.setPixelSize(6+(param_matrixWorld.m11()-threshold_zoom_img_region)/5);
//f.setStretch(0);
painter->setFont(f);
QString val;
@ -1489,11 +1585,11 @@ void ViewPort::drawImgRegion(QPainter *painter)
QPointF point2;//idem
for (int j=-1;j<view.height()/matrixWorld.m11();j++)
for (int i=-1;i<view.width()/matrixWorld.m11();i++)
for (int j=-1;j<view.height()/param_matrixWorld.m11();j++)
for (int i=-1;i<view.width()/param_matrixWorld.m11();i++)
{
point1.setX((i+offsetX)*matrixWorld.m11());
point1.setY((j+offsetY)*matrixWorld.m11());
point1.setX((i+offsetX)*param_matrixWorld.m11());
point1.setY((j+offsetY)*param_matrixWorld.m11());
matrixWorld_inv.map(point1.x(),point1.y(),&point2.rx(),&point2.ry());
@ -1502,22 +1598,21 @@ void ViewPort::drawImgRegion(QPainter *painter)
else
rgbValue = qRgb(0,0,0);
const int margin = 1;
if (nbChannelOriginImage==3)
{
val = tr("%1").arg(qRed(rgbValue));
painter->setPen(QPen(Qt::red, 1));
painter->drawText(QRect(point1.x(),point1.y()+margin,matrixWorld.m11(),matrixWorld.m11()/3),
painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()/3),
Qt::AlignCenter, val);
val = tr("%1").arg(qGreen(rgbValue));
painter->setPen(QPen(Qt::green, 1));
painter->drawText(QRect(point1.x(),point1.y()+matrixWorld.m11()/3+margin,matrixWorld.m11(),matrixWorld.m11()/3),
painter->drawText(QRect(point1.x(),point1.y()+param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3),
Qt::AlignCenter, val);
val = tr("%1").arg(qBlue(rgbValue));
painter->setPen(QPen(Qt::blue, 1));
painter->drawText(QRect(point1.x(),point1.y()+2*matrixWorld.m11()/3+margin,matrixWorld.m11(),matrixWorld.m11()/3),
painter->drawText(QRect(point1.x(),point1.y()+2*param_matrixWorld.m11()/3,param_matrixWorld.m11(),param_matrixWorld.m11()/3),
Qt::AlignCenter, val);
}
@ -1525,7 +1620,7 @@ void ViewPort::drawImgRegion(QPainter *painter)
{
val = tr("%1").arg(qRed(rgbValue));
painter->drawText(QRect(point1.x(),point1.y(),matrixWorld.m11(),matrixWorld.m11()),
painter->drawText(QRect(point1.x(),point1.y(),param_matrixWorld.m11(),param_matrixWorld.m11()),
Qt::AlignCenter, val);
}
}
@ -1549,7 +1644,7 @@ void ViewPort::drawViewOverview(QPainter *painter)
painter->drawRect(QRect(width()-viewSize.width()-margin, 0,viewSize.width(),viewSize.height()));
//daw the view's location inside the image
qreal ratioSize = 1/matrixWorld.m11();
qreal ratioSize = 1/param_matrixWorld.m11();
qreal ratioWindow = (qreal)(viewSize.height())/(qreal)(size().height());
painter->setPen(Qt::darkBlue);
painter->drawRect(QRectF(width()-viewSize.width()-positionCorners.left()*ratioSize*ratioWindow-margin,

View File

@ -69,6 +69,7 @@
#include <QIODevice>
#include <QShortcut>
#include <QStatusBar>
#include <QVarLengthArray>
//Macro here
#define CV_MODE_NORMAL 0
@ -108,7 +109,9 @@ public slots:
double getPropWindow(QString name);
void setPropWindow(QString name, double flags );
double getRatioWindow(QString name);
void setRatioWindow(QString name, double arg2 );
void setRatioWindow(QString name, double arg2 );
void saveWindowParameters(QString name);
void loadWindowParameters(QString name);
};
class CvTrackbar : public QHBoxLayout
@ -133,6 +136,7 @@ private:
CvTrackbarCallback callback;
QPointer<CvWindow> parent;
int* dataSlider;
};
class CvWindow : public QWidget
@ -146,37 +150,39 @@ public:
void updateImage(void* arr);
void displayInfo(QString text, int delayms );
void displayStatusBar(QString text, int delayms );
void readSettings();
void writeSettings();
ViewPort* getView();
QString name;
int flags;
QPointer<QBoxLayout> layout;
QPointer<QStatusBar> myBar;
QPointer<QLabel> myBar_msg;
//QPointer<CustomLayout> layout;
//parameters (will be save/load)
QString param_name;
int param_flags;
protected:
void readSettings();
void writeSettings();
virtual void keyPressEvent(QKeyEvent *event);
private:
QPointer<ViewPort> myview;
QPointer<QShortcut> shortcutZ;
QPointer<QShortcut> shortcutPlus;
QPointer<QShortcut> shortcutMinus;
QPointer<QShortcut> shortcutLeft;
QPointer<QShortcut> shortcutRight;
QPointer<QShortcut> shortcutUp;
QPointer<QShortcut> shortcutDown;
int status;//0 normal, 1 fullscreen (YV)
QPointer<QShortcut> shortcut_r_Zoom;
QPointer<QShortcut> shortcut_imgRegion;
QPointer<QShortcut> shortcut_Plus;
QPointer<QShortcut> shortcut_Minus;
QPointer<QShortcut> shortcut_Left;
QPointer<QShortcut> shortcut_Right;
QPointer<QShortcut> shortcut_Up;
QPointer<QShortcut> shortcut_Down;
void icvLoadTrackbars(QSettings *settings);
void icvSaveTrackbars(QSettings *settings);
};
enum type_mouse_event {mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3};
static const int tableMouseButtons[][3]={
@ -186,6 +192,7 @@ static const int tableMouseButtons[][3]={
{CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE} //mouse_move
};
class ViewPort : public QGraphicsView
{
Q_OBJECT
@ -195,20 +202,24 @@ public:
void updateImage(void* arr);
void startDisplayInfo(QString text, int delayms);
void setMouseCallBack(CvMouseCallback m, void* param);
int getRatio();
void setRatio(int arg);
//parameters (will be save/load)
QTransform param_matrixWorld;
int param_keepRatio;
IplImage* image2Draw_ipl;
QImage image2Draw_qt;
int mode_display;//opengl or native
int nbChannelOriginImage;
void setRatio(int flags);
int getRatio();
public slots:
//reference:
//http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
//http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html
void scaleView(qreal scaleFactor, QPointF center, bool process1stParam);
void imgRegion( );
void scaleView(qreal scaleFactor, QPointF center);
void moveView(QPointF delta);
void resetZoom();
void ZoomIn();
@ -220,21 +231,14 @@ public slots:
void resizeEvent ( QResizeEvent * );
private:
QPoint deltaOffset;
QPoint computeOffset();
QPoint mouseCoordinate;
QPointF positionGrabbing;
QRect positionCorners;
QTransform matrixWorld;
QRect positionCorners;
QTransform matrixWorld_inv;
float ratioX, ratioY;
CvMouseCallback on_mouse;
void* on_mouse_param;
int mode;
int keepRatio;
bool isSameSize(IplImage* img1,IplImage* img2);
QSize sizeHint() const;
QPointer<CvWindow> centralWidget;
@ -270,64 +274,66 @@ private slots:
void stopDisplayInfo();
};
//here css for trackbar
/* from http://thesmithfam.org/blog/2010/03/10/fancy-qslider-stylesheet */
static const QString str_Trackbar_css = QString("")
+ "QSlider::groove:horizontal {"
+ "border: 1px solid #bbb;"
+ "background: white;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::groove:horizontal {"
+ "border: 1px solid #bbb;"
+ "background: white;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::sub-page:horizontal {"
+ "background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
+ "stop: 0 #66e, stop: 1 #bbf);"
+ "background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,"
+ "stop: 0 #bbf, stop: 1 #55f);"
+ "border: 1px solid #777;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::sub-page:horizontal {"
+ "background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
+ "stop: 0 #66e, stop: 1 #bbf);"
+ "background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,"
+ "stop: 0 #bbf, stop: 1 #55f);"
+ "border: 1px solid #777;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::add-page:horizontal {"
+ "background: #fff;"
+ "border: 1px solid #777;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::add-page:horizontal {"
+ "background: #fff;"
+ "border: 1px solid #777;"
+ "height: 10px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::handle:horizontal {"
+ "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
+ "stop:0 #eee, stop:1 #ccc);"
+ "border: 1px solid #777;"
+ "width: 13px;"
+ "margin-top: -2px;"
+ "margin-bottom: -2px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::handle:horizontal {"
+ "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
+ "stop:0 #eee, stop:1 #ccc);"
+ "border: 1px solid #777;"
+ "width: 13px;"
+ "margin-top: -2px;"
+ "margin-bottom: -2px;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::handle:horizontal:hover {"
+ "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
+ "stop:0 #fff, stop:1 #ddd);"
+ "border: 1px solid #444;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::handle:horizontal:hover {"
+ "background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
+ "stop:0 #fff, stop:1 #ddd);"
+ "border: 1px solid #444;"
+ "border-radius: 4px;"
+ "}"
+ "QSlider::sub-page:horizontal:disabled {"
+ "background: #bbb;"
+ "border-color: #999;"
+ "}"
+ "QSlider::sub-page:horizontal:disabled {"
+ "background: #bbb;"
+ "border-color: #999;"
+ "}"
+ "QSlider::add-page:horizontal:disabled {"
+ "background: #eee;"
+ "border-color: #999;"
+ "}"
+ "QSlider::add-page:horizontal:disabled {"
+ "background: #eee;"
+ "border-color: #999;"
+ "}"
+ "QSlider::handle:horizontal:disabled {"
+ "background: #eee;"
+ "border: 1px solid #aaa;"
+ "border-radius: 4px;"
+ "}";
+ "QSlider::handle:horizontal:disabled {"
+ "background: #eee;"
+ "border: 1px solid #aaa;"
+ "border-radius: 4px;"
+ "}";
#endif