Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 )));
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
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 }