RectItem.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2014, 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 "RectItem.h"
00029 
00030 #include <QtGui/QPen>
00031 #include <QtGui/QBrush>
00032 #include <QtGui/QGraphicsScene>
00033 
00034 namespace find_object {
00035 
00036 RectItem::RectItem(int id, const QRectF &rect, QGraphicsItem * parent) :
00037         QGraphicsRectItem(rect, parent),
00038         placeHolder_(0),
00039         id_(id)
00040 {
00041         this->setAcceptsHoverEvents(true);
00042         this->setFlag(QGraphicsItem::ItemIsFocusable, true);
00043         this->setFlag(QGraphicsItem::ItemIsSelectable, true);
00044 }
00045 
00046 RectItem::~RectItem()
00047 {
00048 }
00049 
00050 void RectItem::setColor(const QColor & color)
00051 {
00052         this->setPen(QPen(color));
00053         this->setBrush(QBrush(color));
00054         if(placeHolder_)
00055         {
00056                 QList<QGraphicsItem *> items = placeHolder_->children();
00057                 if(items.size())
00058                 {
00059                         ((QGraphicsTextItem *)items.front())->setDefaultTextColor(this->pen().color().rgb());
00060                 }
00061         }
00062 }
00063 
00064 void RectItem::showDescription()
00065 {
00066         if(!placeHolder_ || !placeHolder_->isVisible())
00067         {
00068                 if(!placeHolder_)
00069                 {
00070                         placeHolder_ = new QGraphicsRectItem();
00071                         placeHolder_->setVisible(false);
00072                         this->scene()->addItem(placeHolder_);
00073                         placeHolder_->setBrush(QBrush(QColor ( 0, 0, 0, 170 ))); // Black transparent background
00074                         QGraphicsTextItem * text = new QGraphicsTextItem(placeHolder_);
00075                         text->setDefaultTextColor(this->pen().color().rgb());
00076                         QTransform t = this->transform();
00077                         QPolygonF rectH = this->mapToScene(this->rect());
00078                         float angle = 90.0f;
00079                         for(int a=0; a<rectH.size(); ++a)
00080                         {
00081                                 //  Find the smaller angle
00082                                 QLineF ab(rectH.at(a).x(), rectH.at(a).y(), rectH.at((a+1)%4).x(), rectH.at((a+1)%4).y());
00083                                 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());
00084                                 float angleTmp =  ab.angle(cb);
00085                                 if(angleTmp > 90.0f)
00086                                 {
00087                                         angleTmp  = 180.0f - angleTmp;
00088                                 }
00089                                 if(angleTmp < angle)
00090                                 {
00091                                         angle = angleTmp;
00092                                 }
00093                         }
00094                         text->setPlainText(tr(
00095                                         "Object=%1\n"
00096                                         "Homography= [\n"
00097                                         "            %2 %3 %4\n"
00098                                         "            %5 %6 %7\n"
00099                                         "            %8 %9 %10]\n"
00100                                         "Angle=%11").arg(id_)
00101                                         .arg(t.m11()).arg(t.m12()).arg(t.m13())
00102                                         .arg(t.m21()).arg(t.m22()).arg(t.m23())
00103                                         .arg(t.m31()).arg(t.m32()).arg(t.m33())
00104                                         .arg(angle));
00105                         placeHolder_->setRect(text->boundingRect());
00106                 }
00107 
00108 
00109                 QPen pen = this->pen();
00110                 this->setPen(QPen(pen.color(), pen.width()*2));
00111                 placeHolder_->setZValue(this->zValue()+1);
00112                 placeHolder_->setPos(0,0);
00113                 placeHolder_->setVisible(true);
00114 
00115                 Q_EMIT hovered(id_);
00116         }
00117 }
00118 
00119 void RectItem::hideDescription()
00120 {
00121         if(placeHolder_ && placeHolder_->isVisible())
00122         {
00123                 placeHolder_->setVisible(false);
00124                 this->setPen(QPen(pen().color(), pen().width()/2));
00125         }
00126 }
00127 
00128 void RectItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
00129 {
00130         this->showDescription();
00131         QGraphicsRectItem::hoverEnterEvent(event);
00132 }
00133 
00134 void RectItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
00135 {
00136         if(!this->hasFocus())
00137         {
00138                 this->hideDescription();
00139         }
00140         QGraphicsRectItem::hoverEnterEvent(event);
00141 }
00142 
00143 void RectItem::focusInEvent ( QFocusEvent * event )
00144 {
00145         this->showDescription();
00146         QGraphicsRectItem::focusInEvent(event);
00147 }
00148 
00149 void RectItem::focusOutEvent ( QFocusEvent * event )
00150 {
00151         this->hideDescription();
00152         QGraphicsRectItem::focusOutEvent(event);
00153 }
00154 
00155 } // namespace find_object


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Fri Feb 12 2016 01:18:17