KeypointItem.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "KeypointItem.h"
29 
30 #include <QtGui/QPen>
31 #include <QtGui/QBrush>
32 #include <QGraphicsScene>
33 
34 namespace find_object {
35 
36 KeypointItem::KeypointItem(int id, qreal x, qreal y, int r, const cv::KeyPoint & kpt, int wordID, const QColor & color, QGraphicsItem * parent) :
37  QGraphicsEllipseItem(x, y, r, r, parent),
38  placeHolder_(0),
39  id_(id),
40  kpt_(kpt),
41  wordID_(wordID)
42 {
43  this->setPen(QPen(color));
44  this->setBrush(QBrush(color));
45  this->setAcceptHoverEvents(true);
46  this->setFlag(QGraphicsItem::ItemIsFocusable, true);
47  this->setFlag(QGraphicsItem::ItemIsSelectable, true);
48 }
49 
51 {
52 }
53 
54 void KeypointItem::setColor(const QColor & color)
55 {
56  this->setPen(QPen(color));
57  this->setBrush(QBrush(color));
58  if(placeHolder_)
59  {
60  QList<QGraphicsItem *> items = placeHolder_->childItems();
61  if(items.size())
62  {
63  ((QGraphicsTextItem *)items.front())->setDefaultTextColor(this->pen().color().rgb());
64  }
65  }
66 }
67 
69 {
70  if(!placeHolder_ || !placeHolder_->isVisible())
71  {
72  if(!placeHolder_)
73  {
74  QString info = QString( "Keypoint = %1\n"
75  "Word = %2\n"
76  "Response = %3\n"
77  "Angle = %4\n"
78  "X = %5\n"
79  "Y = %6\n"
80  "Size = %7").arg(id_).arg(wordID_).arg(kpt_.response).arg(kpt_.angle).arg(kpt_.pt.x).arg(kpt_.pt.y).arg(kpt_.size);
81 
82  placeHolder_ = new QGraphicsRectItem();
83  placeHolder_->setVisible(false);
84  this->scene()->addItem(placeHolder_);
85  placeHolder_->setBrush(QBrush(QColor ( 0, 0, 0, 170 ))); // Black transparent background
86  QGraphicsTextItem * text = new QGraphicsTextItem(placeHolder_);
87  text->setDefaultTextColor(this->pen().color().rgb());
88  text->setPlainText(info);
89  placeHolder_->setRect(text->boundingRect());
90  }
91 
92 
93  QPen pen = this->pen();
94  this->setPen(QPen(pen.color(), pen.width()+2));
95  placeHolder_->setZValue(this->zValue()+1);
96  placeHolder_->setPos(this->mapToScene(0,0));
97  placeHolder_->setVisible(true);
98  }
99 }
100 
102 {
103  if(placeHolder_ && placeHolder_->isVisible())
104  {
105  placeHolder_->setVisible(false);
106  this->setPen(QPen(pen().color(), pen().width()-2));
107  }
108 }
109 
110 void KeypointItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
111 {
112  this->showDescription();
113  QGraphicsEllipseItem::hoverEnterEvent(event);
114 }
115 
116 void KeypointItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
117 {
118  if(!this->hasFocus())
119  {
120  this->hideDescription();
121  }
122  QGraphicsEllipseItem::hoverEnterEvent(event);
123 }
124 
125 void KeypointItem::focusInEvent ( QFocusEvent * event )
126 {
127  this->showDescription();
128  QGraphicsEllipseItem::focusInEvent(event);
129 }
130 
131 void KeypointItem::focusOutEvent ( QFocusEvent * event )
132 {
133  this->hideDescription();
134  QGraphicsEllipseItem::focusOutEvent(event);
135 }
136 
137 } // namespace find_object
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
void setColor(const QColor &color)
virtual void focusOutEvent(QFocusEvent *event)
KeypointItem(int id, qreal x, qreal y, int r, const cv::KeyPoint &kpt, int wordID=-1, const QColor &color=Qt::green, QGraphicsItem *parent=0)
virtual void focusInEvent(QFocusEvent *event)
QGraphicsRectItem * placeHolder_
Definition: KeypointItem.h:61


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26