/* * Copyright (C) 2011, Mathieu Labbe - IntRoLab - Universite de Sherbrooke */ #include "RectItem.h" #include #include #include RectItem::RectItem(int id, const QRectF &rect, QGraphicsItem * parent) : QGraphicsRectItem(rect, parent), placeHolder_(0), id_(id) { this->setAcceptsHoverEvents(true); this->setFlag(QGraphicsItem::ItemIsFocusable, true); this->setFlag(QGraphicsItem::ItemIsSelectable, true); } RectItem::~RectItem() { } void RectItem::setColor(const QColor & color) { this->setPen(QPen(color)); this->setBrush(QBrush(color)); if(placeHolder_) { QList items = placeHolder_->children(); if(items.size()) { ((QGraphicsTextItem *)items.front())->setDefaultTextColor(this->pen().color().rgb()); } } } void RectItem::showDescription() { if(!placeHolder_ || !placeHolder_->isVisible()) { if(!placeHolder_) { placeHolder_ = new QGraphicsRectItem(); placeHolder_->setVisible(false); this->scene()->addItem(placeHolder_); placeHolder_->setBrush(QBrush(QColor ( 0, 0, 0, 170 ))); // Black transparent background QGraphicsTextItem * text = new QGraphicsTextItem(placeHolder_); text->setDefaultTextColor(this->pen().color().rgb()); QTransform t = this->transform(); QPolygonF rectH = this->mapToScene(this->rect()); float angle = 90.0f; for(int a=0; a 90.0f) { angleTmp = 180.0f - angleTmp; } if(angleTmp < angle) { angle = angleTmp; } } text->setPlainText(tr( "Object=%1\n" "Homography= [\n" " %2 %3 %4\n" " %5 %6 %7\n" " %8 %9 %10]\n" "Angle=%11").arg(id_) .arg(t.m11()).arg(t.m12()).arg(t.m13()) .arg(t.m21()).arg(t.m22()).arg(t.m23()) .arg(t.m31()).arg(t.m32()).arg(t.m33()) .arg(angle)); placeHolder_->setRect(text->boundingRect()); } QPen pen = this->pen(); this->setPen(QPen(pen.color(), pen.width()*2)); placeHolder_->setZValue(this->zValue()+1); placeHolder_->setPos(0,0); placeHolder_->setVisible(true); Q_EMIT hovered(id_); } } void RectItem::hideDescription() { if(placeHolder_ && placeHolder_->isVisible()) { placeHolder_->setVisible(false); this->setPen(QPen(pen().color(), pen().width()/2)); } } void RectItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) { this->showDescription(); QGraphicsRectItem::hoverEnterEvent(event); } void RectItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) { if(!this->hasFocus()) { this->hideDescription(); } QGraphicsRectItem::hoverEnterEvent(event); } void RectItem::focusInEvent ( QFocusEvent * event ) { this->showDescription(); QGraphicsRectItem::focusInEvent(event); } void RectItem::focusOutEvent ( QFocusEvent * event ) { this->hideDescription(); QGraphicsRectItem::focusOutEvent(event); }