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 <QColorDialog> 41 #include <QGraphicsRectItem> 47 #if QT_VERSION >= 0x050000 48 #include <QStandardPaths> 57 LineItem(
float x1,
float y1,
float x2,
float y2,
const QString & text = QString(), QGraphicsItem * parent = 0) :
58 QGraphicsLineItem(x1, y1, x2, y2, parent),
62 this->setAcceptHoverEvents(
true);
63 this->setFlag(QGraphicsItem::ItemIsFocusable,
true);
74 this->setPen(QPen(pen().color(),
_width));
80 QGraphicsScene * scene = this->scene();
81 if(scene && scene->focusItem() == 0)
87 this->setPen(QPen(pen().color(),
_width+2));
89 QGraphicsLineItem::hoverEnterEvent(event);
98 QGraphicsLineItem::hoverEnterEvent(event);
104 QGraphicsLineItem::focusInEvent(event);
110 QGraphicsLineItem::focusOutEvent(event);
122 if(qGray(pen().color().rgb() > 255/2))
128 _placeHolder->setBrush(QBrush(QColor ( 255, 255, 255, 170 )));
130 QGraphicsTextItem * text =
new QGraphicsTextItem(
_placeHolder);
131 text->setDefaultTextColor(this->pen().color().rgb());
132 text->setPlainText(
_text);
144 QPen pen = this->pen();
145 this->setPen(QPen(pen.color(),
_width+2));
153 this->setPen(QPen(pen().color(),
_width));
164 QPixmap pixmap(50, 50);
166 return QIcon(pixmap);
174 _defaultBgColor(Qt::black),
175 _defaultFeatureColor(Qt::yellow),
176 _defaultMatchingFeatureColor(Qt::magenta),
177 _defaultMatchingLineColor(Qt::cyan),
178 _depthColorMapRange(0),
182 #if QT_VERSION >= 0x050000 183 _savedFileName = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
196 _graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
200 this->setLayout(
new QVBoxLayout(
this));
202 this->layout()->setContentsMargins(0,0,0,0);
204 _menu =
new QMenu(tr(
""),
this);
244 QActionGroup * group =
new QActionGroup(
this);
248 QMenu * colorMap =
_menu->addMenu(
"Depth color map");
262 group =
new QActionGroup(
this);
282 settings.beginGroup(group);
290 settings.setValue(
"alpha", this->
getAlpha());
310 settings.beginGroup(group);
312 this->
setImageShown(settings.value(
"image_shown", this->isImageShown()).toBool());
313 this->
setImageDepthShown(settings.value(
"depth_shown", this->isImageDepthShown()).toBool());
314 this->
setFeaturesShown(settings.value(
"features_shown", this->isFeaturesShown()).toBool());
315 this->
setFeaturesSize(settings.value(
"features_size", this->getFeaturesSize()).toInt());
316 this->
setLinesShown(settings.value(
"lines_shown", this->isLinesShown()).toBool());
317 this->
setLinesWidth(settings.value(
"lines_width", this->getLinesWidth()).toInt());
318 this->
setAlpha(settings.value(
"alpha", this->getAlpha()).toInt());
320 this->
setDefaultFeatureColor(settings.value(
"feature_color", this->getDefaultFeatureColor()).value<QColor>());
323 this->
setGraphicsViewMode(settings.value(
"graphics_view", this->isGraphicsViewMode()).toBool());
324 this->
setGraphicsViewScaled(settings.value(
"graphics_view_scale", this->isGraphicsViewScaled()).toBool());
326 int colorMap = settings.value(
"colormap", 2).toInt();
331 this->
setDepthColorMapRange(settings.value(
"colormap_range", this->getDepthColorMapRange()).toFloat());
422 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
471 for(
int i=0; i<
_lines.size(); ++i)
490 float scale, offsetX, offsetY;
504 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
512 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
613 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
615 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
619 iter.value()->setPen(QPen(c));
620 iter.value()->setBrush(QBrush(c));
632 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
634 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
638 iter.value()->setPen(QPen(c));
639 iter.value()->setBrush(QBrush(c));
643 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
645 if(QColor((*iter)->pen().color().rgb()) == previousColor)
649 (*iter)->setPen(QPen(c));
661 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
663 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
667 iter.value()->setPen(QPen(c));
668 iter.value()->setBrush(QBrush(c));
672 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
674 if(QColor((*iter)->pen().color().rgb()) == previousColor)
678 (*iter)->setPen(QPen(c));
713 float widthRatio = float(targetRect.width()) / w;
714 float heightRatio = float(targetRect.height()) / h;
717 if(widthRatio < heightRatio)
731 if(w < targetRect.width())
733 offsetX = (targetRect.width() - w)/2.0
f;
735 if(h < targetRect.height())
737 offsetY = (targetRect.height() - h)/2.0
f;
752 QWidget::paintEvent(event);
759 float ratio, offsetX, offsetY;
761 QPainter painter(
this);
766 painter.drawRect(event->rect());
769 painter.translate(offsetX, offsetY);
770 painter.scale(ratio, ratio);
776 painter.setOpacity(0.5);
781 painter.drawPixmap(QPoint(0,0),
_image);
792 for(QMultiMap<int, rtabmap::KeypointItem *>::iterator iter =
_features.begin(); iter !=
_features.end(); ++iter)
794 QColor color = iter.value()->pen().color();
796 painter.setPen(color);
797 painter.setBrush(color);
798 painter.drawEllipse(iter.value()->rect());
805 for(QList<QGraphicsLineItem*>::iterator iter =
_lines.begin(); iter !=
_lines.end(); ++iter)
808 painter.setPen(QPen((*iter)->pen().color(),
_linesWidth));
809 painter.drawLine((*iter)->line());
819 QWidget::resizeEvent(event);
844 QAction * action =
_menu->exec(e->globalPos());
850 QString extensions =
"*.png *.xpm *.jpg";
853 extensions +=
" *.pdf";
856 extensions +=
" *.svg";
858 text = QFileDialog::getSaveFileName(
this, tr(
"Save figure to ..."),
_savedFileName, extensions);
862 if(QFileInfo(text).suffix() ==
"")
869 if(QFileInfo(text).suffix().compare(
"pdf") == 0)
871 QPrinter printer(QPrinter::HighResolution);
872 printer.setOrientation(QPrinter::Portrait);
873 printer.setOutputFileName( text );
874 QPainter p(&printer);
876 double xscale = printer.pageRect().width()/double(
_graphicsView->sceneRect().width());
877 double yscale = printer.pageRect().height()/double(
_graphicsView->sceneRect().height());
878 double scale = qMin(xscale, yscale);
879 p.scale(scale, scale);
893 this->render(&p, QPoint(),
_graphicsView->sceneRect().toRect());
922 color = QColorDialog::getColor(color,
this);
979 double value = QInputDialog::getDouble(
this, tr(
"Set depth colormap max range"), tr(
"Range (m), 0=no limit"),
_depthColorMapRange, 0, 9999, 1, &ok);
989 int value = QInputDialog::getInt(
this, tr(
"Set features and lines transparency"), tr(
"alpha (0-255)"),
_alpha, 0, 255, 10, &ok);
999 int value = QInputDialog::getInt(
this, tr(
"Set features size"), tr(
"Size (0 means actual keypoint size)"),
_featuresSize, 0, 999, 1, &ok);
1009 int value = QInputDialog::getInt(
this, tr(
"Set lines width"), tr(
"Width"),
_linesWidth, 0, 999, 1, &ok);
1030 QGraphicsOpacityEffect * effect =
new QGraphicsOpacityEffect();
1031 effect->setOpacity(0.5);
1051 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
1053 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
1054 xRatio = float(depth.cols)/float(this->
sceneRect().width());
1055 yRatio = float(depth.rows)/float(this->
sceneRect().height());
1056 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
1059 for(std::multimap<int, cv::KeyPoint>::const_iterator iter = refWords.begin(); iter != refWords.end(); ++iter )
1061 if (xRatio > 0 && yRatio > 0)
1063 addFeature(iter->first, iter->second,
util2d::getDepth(depth, iter->second.pt.x*xRatio, iter->second.pt.y*yRatio,
false), color);
1067 addFeature(iter->first, iter->second, 0, color);
1083 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
1085 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
1086 xRatio = float(depth.cols) / float(this->
sceneRect().width());
1087 yRatio = float(depth.rows) / float(this->
sceneRect().height());
1088 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
1091 for(
unsigned int i = 0; i< features.size(); ++i )
1093 if(xRatio > 0 && yRatio > 0)
1131 item->setPen(QPen(color));
1145 _image = QPixmap::fromImage(image);
1160 if(image.rect().isValid())
1182 if(
_image.width() > 0 &&
1216 QList<KeypointItem*> items =
_features.values(
id);
1219 for(
int i=0; i<items.size(); ++i)
1221 items[i]->setColor(color);
1226 UWARN(
"Not found feature %d",
id);
1238 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1240 iter.value()->setColor(color);
1251 UASSERT(alpha >=0 && alpha <= 255);
1253 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1255 QColor c = iter.value()->pen().color();
1257 iter.value()->setPen(QPen(c));
1258 iter.value()->setBrush(QBrush(c));
1261 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
1263 QColor c = (*iter)->pen().color();
1265 (*iter)->setPen(QPen(c));
1277 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1279 const cv::KeyPoint & kpt = iter.value()->keypoint();
1282 size = kpt.size==0?3:kpt.size;
1285 iter.value()->setRect(kpt.pt.x-sizef/2.0f, kpt.pt.y-sizef/2.0f, sizef, sizef);
1297 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
1299 if(dynamic_cast<LineItem*>(*iter))
void setDefaultFeatureColor(const QColor &color)
void addLine(float x1, float y1, float x2, float y2, QColor color, const QString &text=QString())
QMultiMap< int, rtabmap::KeypointItem * > _features
QAction * _showImageDepth
int getLinesWidth() const
void setDefaultMatchingLineColor(const QColor &color)
float getDepthColorMapRange() const
void setFeaturesColor(QColor color)
bool isLinesShown() const
const QColor & getBackgroundColor() const
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
void saveSettings(QSettings &settings, const QString &group="") const
void setFeatureColor(int id, QColor color)
QAction * _setFeaturesSize
bool isGraphicsViewScaledToHeight() const
void setLinesShown(bool shown)
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack, float depthMin=0, float depthMax=0)
QList< QGraphicsLineItem * > _lines
GLM_FUNC_DECL genType e()
const QColor & getDefaultFeatureColor() const
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)
bool isGraphicsViewMode() const
bool isGraphicsViewScaled() const
QAction * _colorMapRedToBlue
QAction * _setMatchingFeatureColor
#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
void setGraphicsViewScaledToHeight(bool scaled)
void setDefaultMatchingFeatureColor(const QColor &color)
bool isImageShown() const
QAction * _colorMapWhiteToBlack
QColor _defaultMatchingFeatureColor
void setImageDepth(const cv::Mat &imageDepth)
void setFeaturesShown(bool shown)
QColor _defaultFeatureColor
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
virtual void focusOutEvent(QFocusEvent *event)
bool isImageDepthShown() const
ImageView(QWidget *parent=0)
void addFeature(int id, const cv::KeyPoint &kpt, float depth, QColor color)
QAction * _setMatchingLineColor
int getFeaturesSize() const
void setGraphicsViewMode(bool on)
virtual void focusInEvent(QFocusEvent *event)
void setLinesWidth(int width)
void setGraphicsViewScaled(bool scaled)
QAction * _colorMapBlueToRed
virtual void resizeEvent(QResizeEvent *event)
const QColor & getDefaultMatchingFeatureColor() const
QAction * _setFeatureColor
QColor _defaultMatchingLineColor
virtual void paintEvent(QPaintEvent *event)
void sceneRectChanged(const QRectF &rect)
QIcon createIcon(const QColor &color)
ULogger class and convenient macros.
QAction * _colorMapBlackToWhite
void computeScaleOffsets(const QRect &targetRect, float &scale, float &offsetX, float &offsetY) 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)
virtual QSize sizeHint() const
float _depthColorMapRange
QAction * _graphicsViewNoScaling
const QColor & getDefaultBackgroundColor() const
void setFeatures(const std::multimap< int, cv::KeyPoint > &refWords, const cv::Mat &depth=cv::Mat(), const QColor &color=Qt::yellow)
void setDepthColorMapRange(float value)
uCvQtDepthColorMap getDepthColorMap() const
const QColor & getDefaultMatchingLineColor() const
void setDefaultBackgroundColor(const QColor &color)
QAction * _graphicsViewScaled
bool isFeaturesShown() const
void setImageDepthShown(bool shown)
QGraphicsPixmapItem * _imageDepthItem