RectItem.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 "RectItem.h"
29 
30 #include <QtGui/QPen>
31 #include <QtGui/QBrush>
32 #include <QGraphicsScene>
33 
34 namespace find_object {
35 
36 RectItem::RectItem(int id, const QRectF &rect, QGraphicsItem * parent) :
37  QGraphicsRectItem(rect, parent),
38  placeHolder_(0),
39  id_(id)
40 {
41  this->setAcceptHoverEvents(true);
42  this->setFlag(QGraphicsItem::ItemIsFocusable, true);
43  this->setFlag(QGraphicsItem::ItemIsSelectable, true);
44 }
45 
47 {
48 }
49 
50 void RectItem::setColor(const QColor & color)
51 {
52  this->setPen(QPen(color));
53  this->setBrush(QBrush(color));
54  if(placeHolder_)
55  {
56  QList<QGraphicsItem *> items = placeHolder_->childItems();
57  if(items.size())
58  {
59  ((QGraphicsTextItem *)items.front())->setDefaultTextColor(this->pen().color().rgb());
60  }
61  }
62 }
63 
65 {
66  if(!placeHolder_ || !placeHolder_->isVisible())
67  {
68  if(!placeHolder_)
69  {
70  placeHolder_ = new QGraphicsRectItem();
71  placeHolder_->setVisible(false);
72  this->scene()->addItem(placeHolder_);
73  placeHolder_->setBrush(QBrush(QColor ( 0, 0, 0, 170 ))); // Black transparent background
74  QGraphicsTextItem * text = new QGraphicsTextItem(placeHolder_);
75  text->setDefaultTextColor(this->pen().color().rgb());
76  QTransform t = this->transform();
77  QPolygonF rectH = this->mapToScene(this->rect());
78  float angle = 90.0f;
79  for(int a=0; a<rectH.size(); ++a)
80  {
81  // Find the smaller angle
82  QLineF ab(rectH.at(a).x(), rectH.at(a).y(), rectH.at((a+1)%4).x(), rectH.at((a+1)%4).y());
83  QLineF cb(rectH.at((a+1)%4).x(), rectH.at((a+1)%4).y(), rectH.at((a+2)%4).x(), rectH.at((a+2)%4).y());
84  float angleTmp = ab.angle(cb);
85  if(angleTmp > 90.0f)
86  {
87  angleTmp = 180.0f - angleTmp;
88  }
89  if(angleTmp < angle)
90  {
91  angle = angleTmp;
92  }
93  }
94  text->setPlainText(tr(
95  "Object=%1\n"
96  "Homography= [\n"
97  " %2 %3 %4\n"
98  " %5 %6 %7\n"
99  " %8 %9 %10]\n"
100  "Angle=%11").arg(id_)
101  .arg(t.m11()).arg(t.m12()).arg(t.m13())
102  .arg(t.m21()).arg(t.m22()).arg(t.m23())
103  .arg(t.m31()).arg(t.m32()).arg(t.m33())
104  .arg(angle));
105  placeHolder_->setRect(text->boundingRect());
106  }
107 
108 
109  QPen pen = this->pen();
110  this->setPen(QPen(pen.color(), pen.width()*2));
111  placeHolder_->setZValue(this->zValue()+1);
112  placeHolder_->setPos(0,0);
113  placeHolder_->setVisible(true);
114 
115  Q_EMIT hovered(id_);
116  }
117 }
118 
120 {
121  if(placeHolder_ && placeHolder_->isVisible())
122  {
123  placeHolder_->setVisible(false);
124  this->setPen(QPen(pen().color(), pen().width()/2));
125  }
126 }
127 
128 void RectItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
129 {
130  this->showDescription();
131  QGraphicsRectItem::hoverEnterEvent(event);
132 }
133 
134 void RectItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
135 {
136  if(!this->hasFocus())
137  {
138  this->hideDescription();
139  }
140  QGraphicsRectItem::hoverEnterEvent(event);
141 }
142 
143 void RectItem::focusInEvent ( QFocusEvent * event )
144 {
145  this->showDescription();
146  QGraphicsRectItem::focusInEvent(event);
147 }
148 
149 void RectItem::focusOutEvent ( QFocusEvent * event )
150 {
151  this->hideDescription();
152  QGraphicsRectItem::focusOutEvent(event);
153 }
154 
155 } // namespace find_object
f
virtual void focusOutEvent(QFocusEvent *event)
Definition: RectItem.cpp:149
void setColor(const QColor &color)
Definition: RectItem.cpp:50
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: RectItem.cpp:134
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
virtual void focusInEvent(QFocusEvent *event)
Definition: RectItem.cpp:143
QGraphicsRectItem * placeHolder_
Definition: RectItem.h:63
RectItem(int id, const QRectF &rect, QGraphicsItem *parent=0)
Definition: RectItem.cpp:36
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: RectItem.cpp:128


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