legendmarkeritem.cpp
189 lines
| 4.7 KiB
| text/x-c
|
CppLexer
sauimone
|
r2164 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** 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> | ||||
#include <QGraphicsSimpleTextItem> | ||||
sauimone
|
r2183 | #include "qlegend.h" | ||
#include "qlegend_p.h" | ||||
#include "qlegendmarker.h" | ||||
sauimone
|
r2167 | #include "qlegendmarker_p.h" | ||
sauimone
|
r2183 | #include "legendmarkeritem_p.h" | ||
sauimone
|
r2167 | |||
sauimone
|
r2164 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
sauimone
|
r2167 | LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) : | ||
sauimone
|
r2164 | QGraphicsObject(parent), | ||
sauimone
|
r2167 | m_marker(marker), | ||
sauimone
|
r2164 | m_markerRect(0,0,10.0,10.0), | ||
sauimone
|
r2170 | m_boundingRect(0,0,0,0), | ||
sauimone
|
r2164 | m_textItem(new QGraphicsSimpleTextItem(this)), | ||
m_rectItem(new QGraphicsRectItem(this)), | ||||
m_margin(4), | ||||
sauimone
|
r2189 | m_space(4), | ||
sauimone
|
r2210 | m_hovering(false), | ||
sauimone
|
r2189 | m_pressPos(0, 0) | ||
sauimone
|
r2164 | { | ||
m_rectItem->setRect(m_markerRect); | ||||
Jani Honkonen
|
r2241 | setAcceptHoverEvents(true); | ||
sauimone
|
r2210 | } | ||
LegendMarkerItem::~LegendMarkerItem() | ||||
{ | ||||
if (m_hovering) { | ||||
emit m_marker->q_ptr->hovered(false); | ||||
} | ||||
sauimone
|
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) | ||||
{ | ||||
sauimone
|
r2174 | m_label = label; | ||
sauimone
|
r2164 | updateGeometry(); | ||
} | ||||
QString LegendMarkerItem::label() const | ||||
{ | ||||
sauimone
|
r2174 | return m_label; | ||
sauimone
|
r2164 | } | ||
void LegendMarkerItem::setLabelBrush(const QBrush &brush) | ||||
{ | ||||
m_textItem->setBrush(brush); | ||||
} | ||||
QBrush LegendMarkerItem::labelBrush() const | ||||
{ | ||||
return m_textItem->brush(); | ||||
} | ||||
sauimone
|
r2193 | void LegendMarkerItem::setGeometry(const QRectF &rect) | ||
sauimone
|
r2164 | { | ||
sauimone
|
r2168 | QFontMetrics fn (m_font); | ||
sauimone
|
r2164 | |||
int width = rect.width(); | ||||
qreal x = m_margin + m_markerRect.width() + m_space + m_margin; | ||||
qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin); | ||||
sauimone
|
r2174 | if (fn.boundingRect(m_label).width() + x > width) | ||
sauimone
|
r2164 | { | ||
sauimone
|
r2174 | QString string = m_label + "..."; | ||
sauimone
|
r2164 | while(fn.boundingRect(string).width() + x > width && string.length() > 3) | ||
string.remove(string.length() - 4, 1); | ||||
m_textItem->setText(string); | ||||
} | ||||
else | ||||
sauimone
|
r2174 | m_textItem->setText(m_label); | ||
sauimone
|
r2164 | |||
sauimone
|
r2193 | const QRectF &textRect = m_textItem->boundingRect(); | ||
sauimone
|
r2164 | |||
m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2); | ||||
m_rectItem->setRect(m_markerRect); | ||||
m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2); | ||||
prepareGeometryChange(); | ||||
m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); | ||||
} | ||||
sauimone
|
r2168 | QRectF LegendMarkerItem::boundingRect() const | ||
{ | ||||
return m_boundingRect; | ||||
} | ||||
sauimone
|
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) | ||||
QFontMetrics fn(m_textItem->font()); | ||||
QSizeF sh; | ||||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | ||||
break; | ||||
case Qt::PreferredSize: | ||||
sauimone
|
r2174 | sh = QSizeF(fn.boundingRect(m_label).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | ||
sauimone
|
r2164 | break; | ||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
sauimone
|
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); | ||||
} | ||||
sauimone
|
r2164 | #include "moc_legendmarkeritem_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||