2011-06-05 14:45:39 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010-2011, Mathieu Labbe and IntRoLab - Universite de Sherbrooke
|
|
|
|
|
*
|
|
|
|
|
* This file is part of RTAB-Map.
|
|
|
|
|
*
|
|
|
|
|
* RTAB-Map is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* RTAB-Map is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with RTAB-Map. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-06-24 17:19:34 +00:00
|
|
|
#include "rtabmap/gui/ImageView.h"
|
2011-06-05 14:45:39 +00:00
|
|
|
|
|
|
|
|
#include <QtGui/QWheelEvent>
|
|
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QFileDialog>
|
|
|
|
|
#include <QtCore/QDir>
|
2011-07-23 03:55:05 +00:00
|
|
|
#include <QtGui/QAction>
|
2013-04-30 20:16:01 +00:00
|
|
|
#include "rtabmap/utilite/ULogger.h"
|
2012-06-24 17:19:34 +00:00
|
|
|
#include "rtabmap/gui/KeypointItem.h"
|
2011-06-05 14:45:39 +00:00
|
|
|
|
|
|
|
|
namespace rtabmap {
|
|
|
|
|
|
|
|
|
|
ImageView::ImageView(QWidget * parent) :
|
|
|
|
|
QGraphicsView(parent),
|
|
|
|
|
_zoom(250),
|
|
|
|
|
_minZoom(250),
|
|
|
|
|
_savedFileName((QDir::homePath()+ "/") + "picture" + ".png")
|
|
|
|
|
{
|
|
|
|
|
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
|
|
this->setScene(new QGraphicsScene(this));
|
|
|
|
|
connect(this->scene(), SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(updateZoom()));
|
2011-07-23 03:55:05 +00:00
|
|
|
|
|
|
|
|
_menu = new QMenu(tr(""), this);
|
|
|
|
|
_showImage = _menu->addAction(tr("Show image"));
|
|
|
|
|
_showImage->setCheckable(true);
|
|
|
|
|
_showImage->setChecked(true);
|
|
|
|
|
_showFeatures = _menu->addAction(tr("Show features"));
|
|
|
|
|
_showFeatures->setCheckable(true);
|
|
|
|
|
_showFeatures->setChecked(true);
|
2012-04-07 19:08:54 +00:00
|
|
|
_showLines = _menu->addAction(tr("Show lines"));
|
|
|
|
|
_showLines->setCheckable(true);
|
2013-03-12 15:43:36 +00:00
|
|
|
_showLines->setChecked(true);
|
2011-07-23 03:55:05 +00:00
|
|
|
_saveImage = _menu->addAction(tr("Save picture..."));
|
2011-06-05 14:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ImageView::~ImageView() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::resetZoom()
|
|
|
|
|
{
|
|
|
|
|
_zoom = _minZoom;
|
|
|
|
|
this->setDragMode(QGraphicsView::NoDrag);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-23 03:55:05 +00:00
|
|
|
bool ImageView::isImageShown()
|
|
|
|
|
{
|
|
|
|
|
return _showImage->isChecked();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ImageView::isFeaturesShown()
|
|
|
|
|
{
|
|
|
|
|
return _showFeatures->isChecked();
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-24 17:19:34 +00:00
|
|
|
void ImageView::setFeaturesShown(bool shown)
|
|
|
|
|
{
|
|
|
|
|
_showFeatures->setChecked(shown);
|
|
|
|
|
this->updateItemsShown();
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-07 19:08:54 +00:00
|
|
|
bool ImageView::isLinesShown()
|
|
|
|
|
{
|
|
|
|
|
return _showLines->isChecked();
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-24 17:19:34 +00:00
|
|
|
void ImageView::setLinesShown(bool shown)
|
|
|
|
|
{
|
|
|
|
|
_showLines->setChecked(shown);
|
|
|
|
|
this->updateItemsShown();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-05 14:45:39 +00:00
|
|
|
void ImageView::contextMenuEvent(QContextMenuEvent * e)
|
|
|
|
|
{
|
2011-07-23 03:55:05 +00:00
|
|
|
QAction * action = _menu->exec(e->globalPos());
|
|
|
|
|
if(action == _saveImage)
|
2011-06-05 14:45:39 +00:00
|
|
|
{
|
|
|
|
|
QString text;
|
|
|
|
|
#ifdef QT_SVG_LIB
|
|
|
|
|
text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), _savedFileName, "*.png *.xpm *.jpg *.pdf *.svg");
|
|
|
|
|
#else
|
|
|
|
|
text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), _savedFileName, "*.png *.xpm *.jpg *.pdf");
|
|
|
|
|
#endif
|
|
|
|
|
if(!text.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
_savedFileName = text;
|
|
|
|
|
QImage img(this->sceneRect().width(), this->sceneRect().height(),QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
QPainter p(&img);
|
|
|
|
|
this->scene()->render(&p, this->sceneRect(), this->sceneRect());
|
|
|
|
|
img.save(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-07 19:08:54 +00:00
|
|
|
else if(action == _showFeatures || action == _showImage || action == _showLines)
|
2011-07-23 03:55:05 +00:00
|
|
|
{
|
|
|
|
|
this->updateItemsShown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::updateItemsShown()
|
|
|
|
|
{
|
|
|
|
|
QList<QGraphicsItem*> items = this->scene()->items();
|
|
|
|
|
for(int i=0; i<items.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
|
|
|
|
|
{
|
|
|
|
|
items.at(i)->setVisible(_showFeatures->isChecked());
|
|
|
|
|
}
|
2012-04-07 19:08:54 +00:00
|
|
|
else if( qgraphicsitem_cast<QGraphicsLineItem*>(items.at(i)))
|
|
|
|
|
{
|
|
|
|
|
items.at(i)->setVisible(_showLines->isChecked());
|
|
|
|
|
}
|
2011-07-23 03:55:05 +00:00
|
|
|
else if(qgraphicsitem_cast<QGraphicsPixmapItem*>(items.at(i)))
|
|
|
|
|
{
|
|
|
|
|
items.at(i)->setVisible(_showImage->isChecked());
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-05 14:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::updateZoom()
|
|
|
|
|
{
|
|
|
|
|
qreal scaleRatio = 1;
|
|
|
|
|
if(this->scene())
|
|
|
|
|
{
|
|
|
|
|
scaleRatio = this->geometry().width()/this->sceneRect().width();
|
|
|
|
|
}
|
|
|
|
|
_minZoom = log(scaleRatio)/log(2)*50+250;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageView::wheelEvent(QWheelEvent * e)
|
|
|
|
|
{
|
|
|
|
|
if(e->delta() > 0)
|
|
|
|
|
{
|
|
|
|
|
_zoom += 20;
|
|
|
|
|
this->setDragMode(QGraphicsView::ScrollHandDrag);
|
|
|
|
|
if(_zoom>=500)
|
|
|
|
|
{
|
|
|
|
|
_zoom = 500;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_zoom -= 20;
|
|
|
|
|
if(_zoom<=_minZoom)
|
|
|
|
|
{
|
|
|
|
|
this->setDragMode(QGraphicsView::NoDrag);
|
|
|
|
|
_zoom = _minZoom;
|
|
|
|
|
this->fitInView(this->sceneRect(), Qt::KeepAspectRatio);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qreal scale = qPow(qreal(2), (_zoom - 250) / qreal(50));
|
|
|
|
|
QMatrix matrix;
|
|
|
|
|
matrix.scale(scale, scale);
|
|
|
|
|
this->setMatrix(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|