KeypointItem.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/KeypointItem.h"
00029 
00030 #include <QtGui/QPen>
00031 #include <QtGui/QBrush>
00032 #include <QGraphicsScene>
00033 #include "rtabmap/utilite/ULogger.h"
00034 
00035 namespace rtabmap {
00036 
00037 KeypointItem::KeypointItem(int id, const cv::KeyPoint & kpt, float depth, const QColor & color, QGraphicsItem * parent) :
00038         QGraphicsEllipseItem(kpt.pt.x-(kpt.size==0?3.0f:kpt.size)/2.0f, kpt.pt.y-(kpt.size==0?3.0f:kpt.size)/2.0f, kpt.size==0?3.0f:kpt.size, kpt.size==0?3.0f:kpt.size, parent),
00039         _id(id),
00040         _kpt(kpt),
00041         _placeHolder(0),
00042         _depth(depth)
00043 {
00044         this->setColor(color);
00045         this->setAcceptHoverEvents(true);
00046         this->setFlag(QGraphicsItem::ItemIsFocusable, true);
00047         _width = pen().width();
00048 }
00049 
00050 KeypointItem::~KeypointItem()
00051 {
00052         delete _placeHolder;
00053 }
00054 
00055 void KeypointItem::setColor(const QColor & color)
00056 {
00057         this->setPen(QPen(color));
00058         this->setBrush(QBrush(color));
00059 }
00060 
00061 void KeypointItem::showDescription()
00062 {
00063         if(!_placeHolder)
00064         {
00065                 _placeHolder = new QGraphicsRectItem (this);
00066                 _placeHolder->setVisible(false);
00067                 if(qGray(pen().color().rgb() > 255/2))
00068                 {
00069                         _placeHolder->setBrush(QBrush(QColor ( 0,0,0, 170 )));
00070                 }
00071                 else
00072                 {
00073                         _placeHolder->setBrush(QBrush(QColor ( 255, 255, 255, 170 )));
00074                 }
00075                 QGraphicsTextItem * text = new QGraphicsTextItem(_placeHolder);
00076                 text->setDefaultTextColor(this->pen().color().rgb());
00077                 if(_depth <= 0)
00078                 {
00079                         text->setPlainText(QString( "Id = %1\n"
00080                                         "Dir = %3\n"
00081                                         "Hessian = %4\n"
00082                                         "X = %5\n"
00083                                         "Y = %6\n"
00084                                         "Size = %7\n"
00085                                         "Octave = %8").arg(_id).arg(_kpt.angle).arg(_kpt.response).arg(_kpt.pt.x).arg(_kpt.pt.y).arg(_kpt.size).arg(_kpt.octave));
00086                 }
00087                 else
00088                 {
00089                         text->setPlainText(QString( "Id = %1\n"
00090                                         "Dir = %3\n"
00091                                         "Hessian = %4\n"
00092                                         "X = %5\n"
00093                                         "Y = %6\n"
00094                                         "Size = %7\n"
00095                                         "Octave = %8\n"
00096                                         "Depth = %9 m").arg(_id).arg(_kpt.angle).arg(_kpt.response).arg(_kpt.pt.x).arg(_kpt.pt.y).arg(_kpt.size).arg(_kpt.octave).arg(_depth));
00097                 }
00098                 _placeHolder->setRect(text->boundingRect());
00099         }
00100 
00101 
00102         if(_placeHolder->parentItem())
00103         {
00104                 _placeHolder->setParentItem(0); // Make it a to level item
00105         }
00106         QPen pen = this->pen();
00107         this->setPen(QPen(pen.color(), _width+2));
00108         _placeHolder->setZValue(this->zValue()+1);
00109         _placeHolder->setPos(this->mapFromScene(0,0));
00110         _placeHolder->setVisible(true);
00111 }
00112 
00113 void KeypointItem::hideDescription()
00114 {
00115         if(_placeHolder)
00116         {
00117                 _placeHolder->setVisible(false);
00118         }
00119         this->setPen(QPen(pen().color(), _width));
00120 }
00121 
00122 void KeypointItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
00123 {
00124         QGraphicsScene * scene = this->scene();
00125         if(scene && scene->focusItem() == 0)
00126         {
00127                 this->showDescription();
00128         }
00129         else
00130         {
00131                 this->setPen(QPen(pen().color(), _width+2));
00132         }
00133         QGraphicsEllipseItem::hoverEnterEvent(event);
00134 }
00135 
00136 void KeypointItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
00137 {
00138         if(!this->hasFocus())
00139         {
00140                 this->hideDescription();
00141         }
00142         QGraphicsEllipseItem::hoverEnterEvent(event);
00143 }
00144 
00145 void KeypointItem::focusInEvent ( QFocusEvent * event )
00146 {
00147         this->showDescription();
00148         QGraphicsEllipseItem::focusInEvent(event);
00149 }
00150 
00151 void KeypointItem::focusOutEvent ( QFocusEvent * event )
00152 {
00153         this->hideDescription();
00154         QGraphicsEllipseItem::focusOutEvent(event);
00155 }
00156 
00157 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:20