legendmarkeritem.cpp
186 lines
| 4.7 KiB
| text/x-c
|
CppLexer
|
r2164 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r2164 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r2164 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r2164 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include <QPainter> | ||||
#include <QGraphicsSceneEvent> | ||||
|
r2539 | #include <QGraphicsTextItem> | ||
|
r2164 | |||
|
r2183 | #include "qlegend.h" | ||
#include "qlegend_p.h" | ||||
#include "qlegendmarker.h" | ||||
|
r2167 | #include "qlegendmarker_p.h" | ||
|
r2183 | #include "legendmarkeritem_p.h" | ||
|
r2539 | #include "chartpresenter_p.h" | ||
|
r2167 | |||
|
r2164 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r2167 | LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) : | ||
|
r2164 | QGraphicsObject(parent), | ||
|
r2167 | m_marker(marker), | ||
|
r2164 | m_markerRect(0,0,10.0,10.0), | ||
|
r2170 | m_boundingRect(0,0,0,0), | ||
|
r2539 | m_textItem(new QGraphicsTextItem(this)), | ||
|
r2164 | m_rectItem(new QGraphicsRectItem(this)), | ||
|
r2569 | m_margin(1), | ||
m_space(2), | ||||
|
r2210 | m_hovering(false), | ||
|
r2189 | m_pressPos(0, 0) | ||
|
r2164 | { | ||
m_rectItem->setRect(m_markerRect); | ||||
|
r2241 | setAcceptHoverEvents(true); | ||
|
r2210 | } | ||
LegendMarkerItem::~LegendMarkerItem() | ||||
{ | ||||
if (m_hovering) { | ||||
emit m_marker->q_ptr->hovered(false); | ||||
} | ||||
|
r2164 | } | ||
void LegendMarkerItem::setPen(const QPen &pen) | ||||
{ | ||||
m_rectItem->setPen(pen); | ||||
} | ||||
QPen LegendMarkerItem::pen() const | ||||
{ | ||||
return m_rectItem->pen(); | ||||
} | ||||
void LegendMarkerItem::setBrush(const QBrush &brush) | ||||
{ | ||||
m_rectItem->setBrush(brush); | ||||
} | ||||
QBrush LegendMarkerItem::brush() const | ||||
{ | ||||
return m_rectItem->brush(); | ||||
} | ||||
void LegendMarkerItem::setFont(const QFont &font) | ||||
{ | ||||
m_textItem->setFont(font); | ||||
QFontMetrics fn(font); | ||||
m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); | ||||
updateGeometry(); | ||||
} | ||||
QFont LegendMarkerItem::font() const | ||||
{ | ||||
return m_textItem->font(); | ||||
} | ||||
void LegendMarkerItem::setLabel(const QString label) | ||||
{ | ||||
|
r2174 | m_label = label; | ||
|
r2164 | updateGeometry(); | ||
} | ||||
QString LegendMarkerItem::label() const | ||||
{ | ||||
|
r2174 | return m_label; | ||
|
r2164 | } | ||
void LegendMarkerItem::setLabelBrush(const QBrush &brush) | ||||
{ | ||||
|
r2539 | m_textItem->setDefaultTextColor(brush.color()); | ||
|
r2164 | } | ||
QBrush LegendMarkerItem::labelBrush() const | ||||
{ | ||||
|
r2539 | return QBrush(m_textItem->defaultTextColor()); | ||
|
r2164 | } | ||
|
r2193 | void LegendMarkerItem::setGeometry(const QRectF &rect) | ||
|
r2164 | { | ||
|
r2544 | qreal width = rect.width(); | ||
|
r2539 | qreal x = m_margin + m_markerRect.width() + m_space + m_margin; | ||
QRectF truncatedRect; | ||||
|
r2164 | |||
|
r2544 | m_textItem->setHtml(ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0), width - x, Qt::Horizontal, truncatedRect)); | ||
|
r2164 | |||
|
r2539 | qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin); | ||
const QRectF &textRect = m_textItem->boundingRect(); | ||||
|
r2164 | |||
|
r2539 | m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2); | ||
|
r2164 | m_rectItem->setRect(m_markerRect); | ||
|
r2539 | m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2); | ||
|
r2164 | |||
prepareGeometryChange(); | ||||
|
r2539 | m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y); | ||
|
r2164 | } | ||
|
r2168 | QRectF LegendMarkerItem::boundingRect() const | ||
{ | ||||
return m_boundingRect; | ||||
} | ||||
|
r2164 | void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
Q_UNUSED(option) | ||||
Q_UNUSED(widget) | ||||
Q_UNUSED(painter) | ||||
} | ||||
QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | ||||
{ | ||||
Q_UNUSED(constraint) | ||||
QSizeF sh; | ||||
|
r2544 | switch (which) { | ||
case Qt::MinimumSize: { | ||||
QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), "..."); | ||||
sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(), | ||||
qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin)); | ||||
break; | ||||
} | ||||
case Qt::PreferredSize: { | ||||
QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), m_label); | ||||
sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(), | ||||
qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin)); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r2164 | } | ||
|
r2210 | void LegendMarkerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||
{ | ||||
Q_UNUSED(event) | ||||
m_hovering = true; | ||||
emit m_marker->q_ptr->hovered(true); | ||||
} | ||||
void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
Q_UNUSED(event) | ||||
m_hovering = false; | ||||
emit m_marker->q_ptr->hovered(false); | ||||
} | ||||
|
r2164 | #include "moc_legendmarkeritem_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||