30 #include <QtGui/QWheelEvent> 31 #include <QtCore/qmath.h> 33 #include <QFileDialog> 34 #include <QtCore/QDir> 36 #include <QGraphicsEffect> 37 #include <QInputDialog> 38 #include <QVBoxLayout> 39 #include <QGraphicsRectItem> 51 LineItem(
float x1,
float y1,
float x2,
float y2,
const QString & text = QString(), QGraphicsItem * parent = 0) :
52 QGraphicsLineItem(x1, y1, x2, y2, parent),
56 this->setAcceptHoverEvents(
true);
57 this->setFlag(QGraphicsItem::ItemIsFocusable,
true);
70 QGraphicsScene * scene = this->scene();
71 if(scene && scene->focusItem() == 0)
77 this->setPen(QPen(pen().color(),
_width+2));
79 QGraphicsLineItem::hoverEnterEvent(event);
88 QGraphicsLineItem::hoverEnterEvent(event);
94 QGraphicsLineItem::focusInEvent(event);
100 QGraphicsLineItem::focusOutEvent(event);
112 if(qGray(pen().color().rgb() > 255/2))
118 _placeHolder->setBrush(QBrush(QColor ( 255, 255, 255, 170 )));
120 QGraphicsTextItem * text =
new QGraphicsTextItem(
_placeHolder);
121 text->setDefaultTextColor(this->pen().color().rgb());
122 text->setPlainText(
_text);
134 QPen pen = this->pen();
135 this->setPen(QPen(pen.color(),
_width+2));
143 this->setPen(QPen(pen().color(),
_width));
154 _savedFileName((QDir::homePath()+
"/") +
"picture" +
".png"),
157 _defaultBgColor(Qt::black),
162 _graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
166 this->setLayout(
new QVBoxLayout(
this));
168 this->layout()->setContentsMargins(0,0,0,0);
170 _menu =
new QMenu(tr(
""),
this);
198 QActionGroup * group =
new QActionGroup(
this);
202 QMenu * colorMap =
_menu->addMenu(
"Depth color map");
215 group =
new QActionGroup(
this);
235 settings.beginGroup(group);
242 settings.setValue(
"alpha", this->
getAlpha());
258 settings.beginGroup(group);
260 this->
setImageShown(settings.value(
"image_shown", this->isImageShown()).toBool());
261 this->
setImageDepthShown(settings.value(
"depth_shown", this->isImageDepthShown()).toBool());
262 this->
setFeaturesShown(settings.value(
"features_shown", this->isFeaturesShown()).toBool());
263 this->
setFeaturesSize(settings.value(
"features_size", this->getFeaturesSize()).toInt());
264 this->
setLinesShown(settings.value(
"lines_shown", this->isLinesShown()).toBool());
265 this->
setAlpha(settings.value(
"alpha", this->getAlpha()).toInt());
267 this->
setGraphicsViewMode(settings.value(
"graphics_view", this->isGraphicsViewMode()).toBool());
268 this->
setGraphicsViewScaled(settings.value(
"graphics_view_scale", this->isGraphicsViewScaled()).toBool());
270 int colorMap = settings.value(
"colormap", 0).toInt();
330 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
379 for(
int i=0; i<
_lines.size(); ++i)
398 float scale, offsetX, offsetY;
412 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
417 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
531 float widthRatio = float(targetRect.width()) / w;
532 float heightRatio = float(targetRect.height()) / h;
535 if(widthRatio < heightRatio)
549 if(w < targetRect.width())
551 offsetX = (targetRect.width() - w)/2.0
f;
553 if(h < targetRect.height())
555 offsetY = (targetRect.height() - h)/2.0
f;
570 QWidget::paintEvent(event);
577 float ratio, offsetX, offsetY;
579 QPainter painter(
this);
584 painter.drawRect(event->rect());
587 painter.translate(offsetX, offsetY);
588 painter.scale(ratio, ratio);
594 painter.setOpacity(0.5);
599 painter.drawPixmap(QPoint(0,0),
_image);
610 for(QMultiMap<int, rtabmap::KeypointItem *>::iterator iter =
_features.begin(); iter !=
_features.end(); ++iter)
612 QColor color = iter.value()->pen().color();
614 painter.setPen(color);
615 painter.setBrush(color);
616 painter.drawEllipse(iter.value()->rect());
623 for(QList<QGraphicsLineItem*>::iterator iter =
_lines.begin(); iter !=
_lines.end(); ++iter)
625 QColor color = (*iter)->pen().color();
627 painter.setPen(color);
628 painter.drawLine((*iter)->line());
638 QWidget::resizeEvent(event);
656 QAction * action =
_menu->exec(e->globalPos());
663 text = QFileDialog::getSaveFileName(
this, tr(
"Save figure to ..."),
_savedFileName,
"*.png *.xpm *.jpg *.pdf *.svg");
665 text = QFileDialog::getSaveFileName(
this, tr(
"Save figure to ..."),
_savedFileName,
"*.png *.xpm *.jpg *.pdf");
678 this->render(&p, QPoint(),
_graphicsView->sceneRect().toRect());
728 int value = QInputDialog::getInt(
this, tr(
"Set features and lines transparency"), tr(
"alpha (0-255)"),
_alpha, 0, 255, 10, &ok);
738 int value = QInputDialog::getInt(
this, tr(
"Set features size"), tr(
"Size (0 means actual keypoint size)"),
_featuresSize, 0, 999, 1, &ok);
759 QGraphicsOpacityEffect * effect =
new QGraphicsOpacityEffect();
760 effect->setOpacity(0.5);
774 void ImageView::setFeatures(
const std::multimap<int, cv::KeyPoint> & refWords,
const cv::Mat & depth,
const QColor & color)
780 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
782 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
783 xRatio = float(depth.cols)/float(this->
sceneRect().width());
784 yRatio = float(depth.rows)/float(this->
sceneRect().height());
785 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
788 for(std::multimap<int, cv::KeyPoint>::const_iterator iter = refWords.begin(); iter != refWords.end(); ++iter )
790 if (xRatio > 0 && yRatio > 0)
792 addFeature(iter->first, iter->second,
util2d::getDepth(depth, iter->second.pt.x*xRatio, iter->second.pt.y*yRatio,
false), color);
796 addFeature(iter->first, iter->second, 0, color);
812 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
814 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
815 xRatio = float(depth.cols) / float(this->
sceneRect().width());
816 yRatio = float(depth.rows) / float(this->
sceneRect().height());
817 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
820 for(
unsigned int i = 0; i< features.size(); ++i )
822 if(xRatio > 0 && yRatio > 0)
860 item->setPen(QPen(color));
873 _image = QPixmap::fromImage(image);
888 if(image.rect().isValid())
959 QList<KeypointItem*> items =
_features.values(
id);
962 for(
int i=0; i<items.size(); ++i)
964 items[i]->setColor(color);
969 UWARN(
"Not found feature %d",
id);
981 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
983 iter.value()->setColor(color);
994 UASSERT(alpha >=0 && alpha <= 255);
996 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
998 QColor c = iter.value()->pen().color();
1000 iter.value()->setPen(QPen(c));
1001 iter.value()->setBrush(QBrush(c));
1004 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
1006 QColor c = (*iter)->pen().color();
1008 (*iter)->setPen(QPen(c));
1020 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1022 const cv::KeyPoint & kpt = iter.value()->keypoint();
1025 size = kpt.size==0?3:kpt.size;
1028 iter.value()->setRect(kpt.pt.x-sizef/2.0f, kpt.pt.y-sizef/2.0f, sizef, sizef);
void addLine(float x1, float y1, float x2, float y2, QColor color, const QString &text=QString())
QMultiMap< int, rtabmap::KeypointItem * > _features
bool isImageDepthShown() const
QAction * _showImageDepth
bool isLinesShown() const
void setFeaturesColor(QColor color)
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
bool isImageShown() const
bool isGraphicsViewScaledToHeight() const
void setFeatureColor(int id, QColor color)
QAction * _setFeaturesSize
void setLinesShown(bool shown)
void computeScaleOffsets(const QRect &targetRect, float &scale, float &offsetX, float &offsetY) const
QList< QGraphicsLineItem * > _lines
bool isGraphicsViewScaled() const
GLM_FUNC_DECL genType e()
void setFeaturesSize(int size)
QAction * _graphicsViewScaledToHeight
void setImage(const QImage &image)
LineItem(float x1, float y1, float x2, float y2, const QString &text=QString(), QGraphicsItem *parent=0)
QAction * _colorMapRedToBlue
const QColor & getDefaultBackgroundColor() const
#define UASSERT(condition)
QGraphicsView * _graphicsView
QAction * _graphicsViewMode
void loadSettings(QSettings &settings, const QString &group="")
void setSceneRect(const QRectF &rect)
void setImageShown(bool shown)
void setBackgroundColor(const QColor &color)
QGraphicsRectItem * _placeHolder
QGraphicsPixmapItem * _imageItem
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack)
void setGraphicsViewScaledToHeight(bool scaled)
QAction * _colorMapWhiteToBlack
void setImageDepth(const cv::Mat &imageDepth)
void setFeaturesShown(bool shown)
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
virtual void focusOutEvent(QFocusEvent *event)
virtual QSize sizeHint() const
ImageView(QWidget *parent=0)
void addFeature(int id, const cv::KeyPoint &kpt, float depth, QColor color)
bool isGraphicsViewMode() const
void setGraphicsViewMode(bool on)
void setColor(const QColor &color)
const QColor & getBackgroundColor() const
virtual void focusInEvent(QFocusEvent *event)
void setGraphicsViewScaled(bool scaled)
QAction * _colorMapBlueToRed
virtual void resizeEvent(QResizeEvent *event)
virtual void paintEvent(QPaintEvent *event)
void sceneRectChanged(const QRectF &rect)
ULogger class and convenient macros.
QAction * _colorMapBlackToWhite
bool isFeaturesShown() const
float RTABMAP_EXP getDepth(const cv::Mat &depthImage, float x, float y, bool smoothing, float depthErrorRatio=0.02f, bool estWithNeighborsIfNull=false)
virtual void contextMenuEvent(QContextMenuEvent *e)
QAction * _graphicsViewNoScaling
void setFeatures(const std::multimap< int, cv::KeyPoint > &refWords, const cv::Mat &depth=cv::Mat(), const QColor &color=Qt::yellow)
int getFeaturesSize() const
void setDefaultBackgroundColor(const QColor &color)
void saveSettings(QSettings &settings, const QString &group="") const
QAction * _graphicsViewScaled
void setImageDepthShown(bool shown)
QGraphicsPixmapItem * _imageDepthItem