ImageView.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #include "rtabmap/gui/ImageView.h"
00029 
00030 #include <QtGui/QWheelEvent>
00031 #include <QtCore/qmath.h>
00032 #include <QMenu>
00033 #include <QFileDialog>
00034 #include <QtCore/QDir>
00035 #include <QAction>
00036 #include <QGraphicsEffect>
00037 #include <QInputDialog>
00038 #include <QVBoxLayout>
00039 #include <QGraphicsRectItem>
00040 #include "rtabmap/utilite/ULogger.h"
00041 #include "rtabmap/gui/KeypointItem.h"
00042 #include "rtabmap/core/util2d.h"
00043 
00044 namespace rtabmap {
00045 
00046 //LineItem
00047 class LineItem : public QGraphicsLineItem
00048 {
00049 public:
00050         LineItem(float x1, float y1, float x2, float y2, const QString & text = QString(), QGraphicsItem * parent = 0) :
00051                 QGraphicsLineItem(x1, y1, x2, y2, parent),
00052                 _text(text),
00053                 _placeHolder(0)
00054         {
00055                 this->setAcceptHoverEvents(true);
00056                 this->setFlag(QGraphicsItem::ItemIsFocusable, true);
00057                 _width = pen().width();
00058         }
00059         virtual ~LineItem()
00060         {
00061                 if(_placeHolder)
00062                 {
00063                         delete _placeHolder;
00064                 }
00065         }
00066 
00067         void setColor(const QColor & color);
00068 
00069 protected:
00070         virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
00071         {
00072                 QGraphicsScene * scene = this->scene();
00073                 if(scene && scene->focusItem() == 0)
00074                 {
00075                         this->showDescription();
00076                 }
00077                 else
00078                 {
00079                         this->setPen(QPen(pen().color(), _width+2));
00080                 }
00081                 QGraphicsLineItem::hoverEnterEvent(event);
00082         }
00083 
00084         virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
00085         {
00086                 if(!this->hasFocus())
00087                 {
00088                         this->hideDescription();
00089                 }
00090                 QGraphicsLineItem::hoverEnterEvent(event);
00091         }
00092 
00093         virtual void focusInEvent ( QFocusEvent * event )
00094         {
00095                 this->showDescription();
00096                 QGraphicsLineItem::focusInEvent(event);
00097         }
00098 
00099         virtual void focusOutEvent ( QFocusEvent * event )
00100         {
00101                 this->hideDescription();
00102                 QGraphicsLineItem::focusOutEvent(event);
00103         }
00104 
00105 private:
00106         void showDescription()
00107         {
00108                 if(!_text.isEmpty())
00109                 {
00110                         if(!_placeHolder)
00111                         {
00112                                 _placeHolder = new QGraphicsRectItem (this);
00113                                 _placeHolder->setVisible(false);
00114                                 _placeHolder->setBrush(QBrush(QColor ( 0, 0, 0, 170 ))); // Black transparent background
00115                                 QGraphicsTextItem * text = new QGraphicsTextItem(_placeHolder);
00116                                 text->setDefaultTextColor(this->pen().color().rgb());
00117                                 text->setPlainText(_text);
00118                                 _placeHolder->setRect(text->boundingRect());
00119                         }
00120 
00121                         if(_placeHolder->parentItem())
00122                         {
00123                                 _placeHolder->setParentItem(0); // Make it a to level item
00124                         }
00125                         _placeHolder->setZValue(this->zValue()+1);
00126                         _placeHolder->setPos(this->mapFromScene(0,0));
00127                         _placeHolder->setVisible(true);
00128                 }
00129                 QPen pen = this->pen();
00130                 this->setPen(QPen(pen.color(), _width+2));
00131         }
00132         void hideDescription()
00133         {
00134                 if(_placeHolder)
00135                 {
00136                         _placeHolder->setVisible(false);
00137                 }
00138                 this->setPen(QPen(pen().color(), _width));
00139         }
00140 
00141 private:
00142         QString _text;
00143         QGraphicsRectItem * _placeHolder;
00144         int _width;
00145 };
00146 
00147 ImageView::ImageView(QWidget * parent) :
00148                 QWidget(parent),
00149                 _savedFileName((QDir::homePath()+ "/") + "picture" + ".png"),
00150                 _alpha(50),
00151                 _imageItem(0),
00152                 _imageDepthItem(0)
00153 {
00154         _graphicsView = new QGraphicsView(this);
00155         _graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
00156         _graphicsView->setScene(new QGraphicsScene(this));
00157         _graphicsView->setVisible(false);
00158 
00159         this->setLayout(new QVBoxLayout(this));
00160         this->layout()->addWidget(_graphicsView);
00161         this->layout()->setContentsMargins(0,0,0,0);
00162 
00163         _menu = new QMenu(tr(""), this);
00164         _showImage = _menu->addAction(tr("Show image"));
00165         _showImage->setCheckable(true);
00166         _showImage->setChecked(true);
00167         _showImageDepth = _menu->addAction(tr("Show image depth"));
00168         _showImageDepth->setCheckable(true);
00169         _showImageDepth->setChecked(false);
00170         _showFeatures = _menu->addAction(tr("Show features"));
00171         _showFeatures->setCheckable(true);
00172         _showFeatures->setChecked(true);
00173         _showLines = _menu->addAction(tr("Show lines"));
00174         _showLines->setCheckable(true);
00175         _showLines->setChecked(true);
00176         _graphicsViewMode = _menu->addAction(tr("Graphics view"));
00177         _graphicsViewMode->setCheckable(true);
00178         _graphicsViewMode->setChecked(false);
00179         _graphicsViewScaled = _menu->addAction(tr("Scale image"));
00180         _graphicsViewScaled->setCheckable(true);
00181         _graphicsViewScaled->setChecked(true);
00182         _graphicsViewScaled->setEnabled(false);
00183         _setAlpha = _menu->addAction(tr("Set transparency..."));
00184         _saveImage = _menu->addAction(tr("Save picture..."));
00185         _saveImage->setEnabled(false);
00186 
00187         connect(_graphicsView->scene(), SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
00188 }
00189 
00190 ImageView::~ImageView() {
00191         clear();
00192 }
00193 
00194 void ImageView::saveSettings(QSettings & settings, const QString & group) const
00195 {
00196         if(!group.isEmpty())
00197         {
00198                 settings.beginGroup(group);
00199         }
00200         settings.setValue("image_shown", this->isImageShown());
00201         settings.setValue("depth_shown", this->isImageDepthShown());
00202         settings.setValue("features_shown", this->isFeaturesShown());
00203         settings.setValue("lines_shown", this->isLinesShown());
00204         settings.setValue("alpha", this->getAlpha());
00205         settings.setValue("graphics_view", this->isGraphicsViewMode());
00206         settings.setValue("graphics_view_scale", this->isGraphicsViewScaled());
00207         if(!group.isEmpty())
00208         {
00209                 settings.endGroup();
00210         }
00211 }
00212 
00213 void ImageView::loadSettings(QSettings & settings, const QString & group)
00214 {
00215         if(!group.isEmpty())
00216         {
00217                 settings.beginGroup(group);
00218         }
00219         this->setImageShown(settings.value("image_shown", this->isImageShown()).toBool());
00220         this->setImageDepthShown(settings.value("depth_shown", this->isImageDepthShown()).toBool());
00221         this->setFeaturesShown(settings.value("features_shown", this->isFeaturesShown()).toBool());
00222         this->setLinesShown(settings.value("lines_shown", this->isLinesShown()).toBool());
00223         this->setAlpha(settings.value("alpha", this->getAlpha()).toInt());
00224         this->setGraphicsViewMode(settings.value("graphics_view", this->isGraphicsViewMode()).toBool());
00225         this->setGraphicsViewScaled(settings.value("graphics_view_scale", this->isGraphicsViewScaled()).toBool());
00226         if(!group.isEmpty())
00227         {
00228                 settings.endGroup();
00229         }
00230 }
00231 
00232 QRectF ImageView::sceneRect() const
00233 {
00234         return _graphicsView->scene()->sceneRect();
00235 }
00236 
00237 bool ImageView::isImageShown() const
00238 {
00239         return _showImage->isChecked();
00240 }
00241 
00242 bool ImageView::isImageDepthShown() const
00243 {
00244         return _showImageDepth->isChecked();
00245 }
00246 
00247 bool ImageView::isFeaturesShown() const
00248 {
00249         return _showFeatures->isChecked();
00250 }
00251 
00252 bool ImageView::isGraphicsViewMode() const
00253 {
00254         return _graphicsViewMode->isChecked();
00255 }
00256 
00257 bool ImageView::isGraphicsViewScaled() const
00258 {
00259         return _graphicsViewScaled->isChecked();
00260 }
00261 
00262 const QColor & ImageView::getBackgroundColor() const
00263 {
00264         return _graphicsView->backgroundBrush().color();
00265 }
00266 
00267 
00268 void ImageView::setFeaturesShown(bool shown)
00269 {
00270         _showFeatures->setChecked(shown);
00271         for(QMultiMap<int, KeypointItem*>::iterator iter=_features.begin(); iter!=_features.end(); ++iter)
00272         {
00273                 iter.value()->setVisible(_showFeatures->isChecked());
00274         }
00275 
00276         if(!_graphicsView->isVisible())
00277         {
00278                 this->update();
00279         }
00280 }
00281 
00282 void ImageView::setImageShown(bool shown)
00283 {
00284         _showImage->setChecked(shown);
00285         if(_imageItem)
00286         {
00287                 _imageItem->setVisible(_showImage->isChecked());
00288                 this->updateOpacity();
00289         }
00290 
00291         if(!_graphicsView->isVisible())
00292         {
00293                 this->update();
00294         }
00295 }
00296 
00297 void ImageView::setImageDepthShown(bool shown)
00298 {
00299         _showImageDepth->setChecked(shown);
00300         if(_imageDepthItem)
00301         {
00302                 _imageDepthItem->setVisible(_showImageDepth->isChecked());
00303                 this->updateOpacity();
00304         }
00305 
00306         if(!_graphicsView->isVisible())
00307         {
00308                 this->update();
00309         }
00310 }
00311 
00312 bool ImageView::isLinesShown() const
00313 {
00314         return _showLines->isChecked();
00315 }
00316 
00317 void ImageView::setLinesShown(bool shown)
00318 {
00319         _showLines->setChecked(shown);
00320         for(int i=0; i<_lines.size(); ++i)
00321         {
00322                 _lines.at(i)->setVisible(_showLines->isChecked());
00323         }
00324 
00325         if(!_graphicsView->isVisible())
00326         {
00327                 this->update();
00328         }
00329 }
00330 
00331 float ImageView::viewScale() const
00332 {
00333         if(_graphicsView->isVisible())
00334         {
00335                 return _graphicsView->transform().m11();
00336         }
00337         else
00338         {
00339                 float scale, offsetX, offsetY;
00340                 computeScaleOffsets(this->rect(), scale, offsetX, offsetY);
00341                 return scale;
00342         }
00343 }
00344 
00345 void ImageView::setGraphicsViewMode(bool on)
00346 {
00347         _graphicsViewMode->setChecked(on);
00348         _graphicsView->setVisible(on);
00349         _graphicsViewScaled->setEnabled(on);
00350 
00351         if(on)
00352         {
00353                 for(QMultiMap<int, KeypointItem*>::iterator iter=_features.begin(); iter!=_features.end(); ++iter)
00354                 {
00355                         _graphicsView->scene()->addItem(iter.value());
00356                 }
00357 
00358                 for(QList<QGraphicsLineItem*>::iterator iter=_lines.begin(); iter!=_lines.end(); ++iter)
00359                 {
00360                         _graphicsView->scene()->addItem(*iter);
00361                 }
00362 
00363                 //update images
00364                 if(_imageItem)
00365                 {
00366                         _imageItem->setPixmap(_image);
00367                 }
00368                 else
00369                 {
00370                         _imageItem = _graphicsView->scene()->addPixmap(_image);
00371                         _imageItem->setVisible(_showImage->isChecked());
00372                 }
00373 
00374                 if(_imageDepthItem)
00375                 {
00376                         _imageDepthItem->setPixmap(_imageDepth);
00377                 }
00378                 else
00379                 {
00380                         _imageDepthItem = _graphicsView->scene()->addPixmap(_imageDepth);
00381                         _imageDepthItem->setVisible(_showImageDepth->isChecked());
00382                 }
00383                 this->updateOpacity();
00384 
00385                 if(_graphicsViewScaled->isChecked())
00386                 {
00387                         _graphicsView->fitInView(_graphicsView->sceneRect(), Qt::KeepAspectRatio);
00388                 }
00389                 else
00390                 {
00391                         _graphicsView->resetTransform();
00392                 }
00393         }
00394         else
00395         {
00396                 this->update();
00397         }
00398 }
00399 
00400 void ImageView::setGraphicsViewScaled(bool scaled)
00401 {
00402         _graphicsViewScaled->setChecked(scaled);
00403 
00404         if(scaled)
00405         {
00406                 _graphicsView->fitInView(_graphicsView->sceneRect(), Qt::KeepAspectRatio);
00407         }
00408         else
00409         {
00410                 _graphicsView->resetTransform();
00411         }
00412 
00413         if(!_graphicsView->isVisible())
00414         {
00415                 this->update();
00416         }
00417 }
00418 
00419 void ImageView::setBackgroundColor(const QColor & color)
00420 {
00421         _graphicsView->setBackgroundBrush(QBrush(color));
00422 
00423         if(!_graphicsView->isVisible())
00424         {
00425                 this->update();
00426         }
00427 }
00428 
00429 void ImageView::computeScaleOffsets(const QRect & targetRect, float & scale, float & offsetX, float & offsetY) const
00430 {
00431         scale = 1.0f;
00432         offsetX = 0.0f;
00433         offsetY = 0.0f;
00434 
00435         if(!_graphicsView->scene()->sceneRect().isNull())
00436         {
00437                 float w = _graphicsView->scene()->width();
00438                 float h = _graphicsView->scene()->height();
00439                 float widthRatio = float(targetRect.width()) / w;
00440                 float heightRatio = float(targetRect.height()) / h;
00441 
00442                 //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
00443                 if(widthRatio < heightRatio)
00444                 {
00445                         scale = widthRatio;
00446                 }
00447                 else
00448                 {
00449                         scale = heightRatio;
00450                 }
00451 
00452                 //printf("ratio=%f\n",ratio);
00453 
00454                 w *= scale;
00455                 h *= scale;
00456 
00457                 if(w < targetRect.width())
00458                 {
00459                         offsetX = (targetRect.width() - w)/2.0f;
00460                 }
00461                 if(h < targetRect.height())
00462                 {
00463                         offsetY = (targetRect.height() - h)/2.0f;
00464                 }
00465                 //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
00466         }
00467 }
00468 
00469 void ImageView::sceneRectChanged(const QRectF & rect)
00470 {
00471         _saveImage->setEnabled(rect.isValid());
00472 }
00473 
00474 void ImageView::paintEvent(QPaintEvent *event)
00475 {
00476         if(_graphicsViewMode->isChecked())
00477         {
00478                 QWidget::paintEvent(event);
00479         }
00480         else
00481         {
00482                 if(!_graphicsView->scene()->sceneRect().isNull())
00483                 {
00484                         //Scale
00485                         float ratio, offsetX, offsetY;
00486                         this->computeScaleOffsets(event->rect(), ratio, offsetX, offsetY);
00487                         QPainter painter(this);
00488 
00489                         //Background
00490                         painter.save();
00491                         painter.setBrush(_graphicsView->backgroundBrush());
00492                         painter.drawRect(event->rect());
00493                         painter.restore();
00494 
00495                         painter.translate(offsetX, offsetY);
00496                         painter.scale(ratio, ratio);
00497 
00498                         painter.save();
00499                         if(_showImage->isChecked() && !_image.isNull() &&
00500                            _showImageDepth->isChecked() && !_imageDepth.isNull())
00501                         {
00502                                 painter.setOpacity(0.5);
00503                         }
00504 
00505                         if(_showImage->isChecked() && !_image.isNull())
00506                         {
00507                                 painter.drawPixmap(QPoint(0,0), _image);
00508                         }
00509 
00510                         if(_showImageDepth->isChecked() && !_imageDepth.isNull())
00511                         {
00512                                 painter.drawPixmap(QPoint(0,0), _imageDepth);
00513                         }
00514                         painter.restore();
00515 
00516                         if(_showFeatures->isChecked())
00517                         {
00518                                 for(QMultiMap<int, rtabmap::KeypointItem *>::iterator iter = _features.begin(); iter != _features.end(); ++iter)
00519                                 {
00520                                         QColor color = iter.value()->pen().color();
00521                                         painter.save();
00522                                         painter.setPen(color);
00523                                         painter.setBrush(color);
00524                                         painter.drawEllipse(iter.value()->rect());
00525                                         painter.restore();
00526                                 }
00527                         }
00528 
00529                         if(_showLines->isChecked())
00530                         {
00531                                 for(QList<QGraphicsLineItem*>::iterator iter = _lines.begin(); iter != _lines.end(); ++iter)
00532                                 {
00533                                         QColor color = (*iter)->pen().color();
00534                                         painter.save();
00535                                         painter.setPen(color);
00536                                         painter.drawLine((*iter)->line());
00537                                         painter.restore();
00538                                 }
00539                         }
00540                 }
00541         }
00542 }
00543 
00544 void ImageView::resizeEvent(QResizeEvent* event)
00545 {
00546         QWidget::resizeEvent(event);
00547         if(_graphicsView->isVisible() && _graphicsViewScaled->isChecked())
00548         {
00549                 _graphicsView->fitInView(_graphicsView->sceneRect(), Qt::KeepAspectRatio);
00550         }
00551 }
00552 
00553 void ImageView::contextMenuEvent(QContextMenuEvent * e)
00554 {
00555         QAction * action = _menu->exec(e->globalPos());
00556         if(action == _saveImage)
00557         {
00558                 if(!_graphicsView->scene()->sceneRect().isNull())
00559                 {
00560                         QString text;
00561 #ifdef QT_SVG_LIB
00562                         text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), _savedFileName, "*.png *.xpm *.jpg *.pdf *.svg");
00563 #else
00564                         text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), _savedFileName, "*.png *.xpm *.jpg *.pdf");
00565 #endif
00566                         if(!text.isEmpty())
00567                         {
00568                                 _savedFileName = text;
00569                                 QImage img(_graphicsView->sceneRect().width(), _graphicsView->sceneRect().height(), QImage::Format_ARGB32_Premultiplied);
00570                                 QPainter p(&img);
00571                                 if(_graphicsView->isVisible())
00572                                 {
00573                                         _graphicsView->scene()->render(&p, _graphicsView->sceneRect(), _graphicsView->sceneRect());
00574                                 }
00575                                 else
00576                                 {
00577                                         this->render(&p, QPoint(), _graphicsView->sceneRect().toRect());
00578                                 }
00579                                 img.save(text);
00580                         }
00581                 }
00582         }
00583         else if(action == _showFeatures)
00584         {
00585                 this->setFeaturesShown(_showFeatures->isChecked());
00586                 emit configChanged();
00587         }
00588         else if(action == _showImage)
00589         {
00590                 this->setImageShown(_showImage->isChecked());
00591                 emit configChanged();
00592         }
00593         else if(action == _showImageDepth)
00594         {
00595                 this->setImageDepthShown(_showImageDepth->isChecked());
00596                 emit configChanged();
00597         }
00598         else if(action == _showLines)
00599         {
00600                 this->setLinesShown(_showLines->isChecked());
00601                 emit configChanged();
00602         }
00603         else if(action == _graphicsViewMode)
00604         {
00605                 this->setGraphicsViewMode(_graphicsViewMode->isChecked());
00606                 emit configChanged();
00607         }
00608         else if(action == _graphicsViewScaled)
00609         {
00610                 this->setGraphicsViewScaled(_graphicsViewScaled->isChecked());
00611                 emit configChanged();
00612         }
00613         else if(action == _setAlpha)
00614         {
00615                 bool ok = false;
00616                 int value = QInputDialog::getInt(this, tr("Set features and lines transparency"), tr("alpha (0-255)"), _alpha, 0, 255, 10, &ok);
00617                 if(ok)
00618                 {
00619                         this->setAlpha(value);
00620                         emit configChanged();
00621                 }
00622         }
00623 
00624         if(action == _showImage || action ==_showImageDepth)
00625         {
00626                 this->updateOpacity();
00627                 emit configChanged();
00628         }
00629 }
00630 
00631 void ImageView::updateOpacity()
00632 {
00633         if(_imageItem && _imageDepthItem)
00634         {
00635                 if(_imageItem->isVisible() && _imageDepthItem->isVisible())
00636                 {
00637                         QGraphicsOpacityEffect * effect = new QGraphicsOpacityEffect();
00638                         effect->setOpacity(0.5);
00639                         _imageDepthItem->setGraphicsEffect(effect);
00640                 }
00641                 else
00642                 {
00643                         _imageDepthItem->setGraphicsEffect(0);
00644                 }
00645         }
00646         else if(_imageDepthItem)
00647         {
00648                 _imageDepthItem->setGraphicsEffect(0);
00649         }
00650 }
00651 
00652 void ImageView::setFeatures(const std::multimap<int, cv::KeyPoint> & refWords, const cv::Mat & depth, const QColor & color)
00653 {
00654         qDeleteAll(_features);
00655         _features.clear();
00656 
00657         for(std::multimap<int, cv::KeyPoint>::const_iterator iter = refWords.begin(); iter != refWords.end(); ++iter )
00658         {
00659                 addFeature(iter->first, iter->second, depth.empty()?0:util2d::getDepth(depth, iter->second.pt.x, iter->second.pt.y, false), color);
00660         }
00661 
00662         if(!_graphicsView->isVisible())
00663         {
00664                 this->update();
00665         }
00666 }
00667 
00668 void ImageView::setFeatures(const std::vector<cv::KeyPoint> & features, const cv::Mat & depth, const QColor & color)
00669 {
00670         qDeleteAll(_features);
00671         _features.clear();
00672 
00673         for(unsigned int i = 0; i< features.size(); ++i )
00674         {
00675                 addFeature(i, features[i], depth.empty()?0:util2d::getDepth(depth, features[i].pt.x, features[i].pt.y, false), color);
00676         }
00677 
00678         if(!_graphicsView->isVisible())
00679         {
00680                 this->update();
00681         }
00682 }
00683 
00684 void ImageView::addFeature(int id, const cv::KeyPoint & kpt, float depth, QColor color)
00685 {
00686         color.setAlpha(this->getAlpha());
00687         rtabmap::KeypointItem * item = new rtabmap::KeypointItem(id, kpt, depth, color);
00688         _features.insert(id, item);
00689         item->setVisible(isFeaturesShown());
00690         item->setZValue(1);
00691 
00692         if(_graphicsView->isVisible())
00693         {
00694                 _graphicsView->scene()->addItem(item);
00695         }
00696 }
00697 
00698 void ImageView::addLine(float x1, float y1, float x2, float y2, QColor color, const QString & text)
00699 {
00700         color.setAlpha(this->getAlpha());
00701         LineItem * item  = new LineItem(x1, y1, x2, y2, text);
00702         item->setPen(QPen(color));
00703         _lines.push_back(item);
00704         item->setVisible(isLinesShown());
00705         item->setZValue(1);
00706 
00707         if(_graphicsView->isVisible())
00708         {
00709                 _graphicsView->scene()->addItem(item);
00710         }
00711 }
00712 
00713 void ImageView::setImage(const QImage & image)
00714 {
00715         _image = QPixmap::fromImage(image);
00716         if(_graphicsView->isVisible())
00717         {
00718                 if(_imageItem)
00719                 {
00720                         _imageItem->setPixmap(_image);
00721                 }
00722                 else
00723                 {
00724                         _imageItem = _graphicsView->scene()->addPixmap(_image);
00725                         _imageItem->setVisible(_showImage->isChecked());
00726                         this->updateOpacity();
00727                 }
00728         }
00729 
00730         if(image.rect().isValid())
00731         {
00732                 this->setSceneRect(image.rect());
00733         }
00734         else if(!_graphicsView->isVisible())
00735         {
00736                 this->update();
00737         }
00738 }
00739 
00740 void ImageView::setImageDepth(const QImage & imageDepth)
00741 {
00742         _imageDepth = QPixmap::fromImage(imageDepth);
00743 
00744         if( _image.width() > 0 &&
00745                 _image.width() > _imageDepth.width() &&
00746                 _image.height() > _imageDepth.height() &&
00747                 _image.width() % _imageDepth.width() == 0 &&
00748                 _image.height() % _imageDepth.height() == 0 &&
00749                 _image.width() / _imageDepth.width() == _image.height() / _imageDepth.height())
00750         {
00751                 // scale depth to rgb
00752                 _imageDepth = _imageDepth.scaledToWidth(_image.width());
00753         }
00754 
00755         if(_graphicsView->isVisible())
00756         {
00757                 if(_imageDepthItem)
00758                 {
00759                         _imageDepthItem->setPixmap(_imageDepth);
00760                 }
00761                 else
00762                 {
00763                         _imageDepthItem = _graphicsView->scene()->addPixmap(_imageDepth);
00764                         _imageDepthItem->setVisible(_showImageDepth->isChecked());
00765                         this->updateOpacity();
00766                 }
00767         }
00768         else
00769         {
00770                 if(_image.isNull())
00771                 {
00772                         this->setSceneRect(imageDepth.rect());
00773                 }
00774                 this->update();
00775         }
00776 }
00777 
00778 void ImageView::setFeatureColor(int id, QColor color)
00779 {
00780         color.setAlpha(getAlpha());
00781         QList<KeypointItem*> items = _features.values(id);
00782         if(items.size())
00783         {
00784                 for(int i=0; i<items.size(); ++i)
00785                 {
00786                         items[i]->setColor(color);
00787                 }
00788         }
00789         else
00790         {
00791                 UWARN("Not found feature %d", id);
00792         }
00793 
00794         if(!_graphicsView->isVisible())
00795         {
00796                 this->update();
00797         }
00798 }
00799 
00800 void ImageView::setFeaturesColor(QColor color)
00801 {
00802         color.setAlpha(getAlpha());
00803         for(QMultiMap<int, KeypointItem*>::iterator iter=_features.begin(); iter!=_features.end(); ++iter)
00804         {
00805                 iter.value()->setColor(color);
00806         }
00807 
00808         if(!_graphicsView->isVisible())
00809         {
00810                 this->update();
00811         }
00812 }
00813 
00814 void ImageView::setAlpha(int alpha)
00815 {
00816         UASSERT(alpha >=0 && alpha <= 255);
00817         _alpha = alpha;
00818         for(QMultiMap<int, KeypointItem*>::iterator iter=_features.begin(); iter!=_features.end(); ++iter)
00819         {
00820                 QColor c = iter.value()->pen().color();
00821                 c.setAlpha(_alpha);
00822                 iter.value()->setPen(QPen(c));
00823                 iter.value()->setBrush(QBrush(c));
00824         }
00825 
00826         for(QList<QGraphicsLineItem*>::iterator iter=_lines.begin(); iter!=_lines.end(); ++iter)
00827         {
00828                 QColor c = (*iter)->pen().color();
00829                 c.setAlpha(_alpha);
00830                 (*iter)->setPen(QPen(c));
00831         }
00832 
00833         if(!_graphicsView->isVisible())
00834         {
00835                 this->update();
00836         }
00837 }
00838 
00839 void ImageView::setSceneRect(const QRectF & rect)
00840 {
00841         _graphicsView->scene()->setSceneRect(rect);
00842 
00843         if(_graphicsViewScaled->isChecked())
00844         {
00845                 _graphicsView->fitInView(_graphicsView->sceneRect(), Qt::KeepAspectRatio);
00846         }
00847         else
00848         {
00849                 _graphicsView->resetTransform();
00850         }
00851 
00852         if(!_graphicsView->isVisible())
00853         {
00854                 this->update();
00855         }
00856 }
00857 
00858 void ImageView::clearLines()
00859 {
00860         qDeleteAll(_lines);
00861         _lines.clear();
00862 
00863         if(!_graphicsView->isVisible())
00864         {
00865                 this->update();
00866         }
00867 }
00868 
00869 void ImageView::clear()
00870 {
00871         qDeleteAll(_features);
00872         _features.clear();
00873 
00874         qDeleteAll(_lines);
00875         _lines.clear();
00876 
00877         if(_imageItem)
00878         {
00879                 _graphicsView->scene()->removeItem(_imageItem);
00880                 delete _imageItem;
00881                 _imageItem = 0;
00882         }
00883         _image = QPixmap();
00884 
00885         if(_imageDepthItem)
00886         {
00887                 _graphicsView->scene()->removeItem(_imageDepthItem);
00888                 delete _imageDepthItem;
00889                 _imageDepthItem = 0;
00890         }
00891         _imageDepth = QPixmap();
00892 
00893         _graphicsView->scene()->setSceneRect(QRectF());
00894         _graphicsView->setScene(_graphicsView->scene());
00895 
00896         if(!_graphicsView->isVisible())
00897         {
00898                 this->update();
00899         }
00900 }
00901 
00902 QSize ImageView::sizeHint() const
00903 {
00904         return _graphicsView->sizeHint();
00905 }
00906 
00907 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:16