0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 08:34:38 +03:00

Add fonts to charts

This commit is contained in:
Emil Sawicki 2022-01-31 16:43:47 +01:00
parent da9c422f74
commit 981c90edfd
2 changed files with 70 additions and 11 deletions

View File

@ -148,6 +148,7 @@ ChartItem::ChartItem(QObject *owner, QGraphicsItem *parent)
{ {
m_labels<<"First"<<"Second"<<"Thrid"; m_labels<<"First"<<"Second"<<"Thrid";
m_chart = new PieChart(this); m_chart = new PieChart(this);
m_chart->setTitleFont(font());
} }
ChartItem::~ChartItem() ChartItem::~ChartItem()
@ -182,15 +183,18 @@ void ChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->setRenderHint(QPainter::Antialiasing,true); painter->setRenderHint(QPainter::Antialiasing,true);
painter->setRenderHint(QPainter::TextAntialiasing,true); painter->setRenderHint(QPainter::TextAntialiasing,true);
qreal borderMargin = (rect().height()*0.01>10)?(10):(rect().height()*0.01); qreal borderMargin = (rect().height()*0.01>10)?(10):(rect().height()*0.01);
qreal maxTitleHeight = rect().height()*0.2; qreal maxTitleHeight = rect().height()*0.5;
QFont tmpFont = painter->font(); QFont tmpFont = painter->font();
qreal titleOffset = !m_title.isEmpty()?(((painter->fontMetrics().height()+borderMargin*2)<maxTitleHeight)? qreal titleOffset = 0;
(painter->fontMetrics().height()+borderMargin*2): if (!m_title.isEmpty()) {
(maxTitleHeight)):0; QFontMetrics fm(titleFont());
const qreal titleHeight = fm.boundingRect(rect().toRect(), Qt::TextWordWrap,chartTitle()).height() + borderMargin * 2;
titleOffset = std::min(titleHeight, maxTitleHeight);
}
QRectF titleRect = QRectF(borderMargin,borderMargin,rect().width()-borderMargin*2,titleOffset); const QRectF titleRect = QRectF(borderMargin,borderMargin,rect().width()-borderMargin*2,titleOffset);
QRectF legendRect = QRectF(0,0,0,0); QRectF legendRect = QRectF(0,0,0,0);
if (m_showLegend) if (m_showLegend)
legendRect = m_chart->calcChartLegendRect(painter->font(), rect(), false, borderMargin, titleOffset); legendRect = m_chart->calcChartLegendRect(painter->font(), rect(), false, borderMargin, titleOffset);
@ -329,6 +333,7 @@ void ChartItem::setChartType(const ChartType &chartType)
if (m_chartType != chartType){ if (m_chartType != chartType){
ChartType oldValue = m_chartType; ChartType oldValue = m_chartType;
m_chartType = chartType; m_chartType = chartType;
QFont oldTitleFont = m_chart->titleFont();
delete m_chart; delete m_chart;
switch (m_chartType) { switch (m_chartType) {
case Pie: case Pie:
@ -347,6 +352,7 @@ void ChartItem::setChartType(const ChartType &chartType)
m_chart = new GridLinesChart(this); m_chart = new GridLinesChart(this);
break; break;
} }
m_chart->setTitleFont(oldTitleFont);
notify("chartType",oldValue,m_chartType); notify("chartType",oldValue,m_chartType);
update(); update();
} }
@ -365,13 +371,16 @@ void ChartItem::setDatasource(const QString &datasource)
void ChartItem::paintChartTitle(QPainter *painter, QRectF titleRect) void ChartItem::paintChartTitle(QPainter *painter, QRectF titleRect)
{ {
painter->save(); painter->save();
QFont tmpFont = painter->font(); QFont tmpFont = transformToSceneFont(titleFont());
QFontMetrics fm(tmpFont); QRect titleBoundingRect = QFontMetrics(tmpFont).boundingRect(rect().toRect(), Qt::TextWordWrap, chartTitle());
while ((fm.height()>titleRect.height() || fm.boundingRect(m_title).width()>titleRect.width())
&& tmpFont.pixelSize()>1) { while ((titleBoundingRect.height() > titleRect.height() || titleBoundingRect.width() > titleRect.width())
&& tmpFont.pixelSize() > 1)
{
tmpFont.setPixelSize(tmpFont.pixelSize()-1); tmpFont.setPixelSize(tmpFont.pixelSize()-1);
fm = QFontMetrics(tmpFont); titleBoundingRect = QFontMetrics(tmpFont).boundingRect(rect().toRect(), Qt::TextWordWrap, chartTitle());
} }
painter->setFont(tmpFont); painter->setFont(tmpFont);
Qt::AlignmentFlag align = Qt::AlignCenter; Qt::AlignmentFlag align = Qt::AlignCenter;
switch (m_titleAlign) { switch (m_titleAlign) {
@ -385,7 +394,7 @@ void ChartItem::paintChartTitle(QPainter *painter, QRectF titleRect)
align = Qt::AlignRight; align = Qt::AlignRight;
break; break;
} }
painter->drawText(titleRect, align, m_title); painter->drawText(titleRect, align | Qt::TextWordWrap, m_title);
painter->restore(); painter->restore();
} }
@ -527,6 +536,33 @@ void ChartItem::setGridChartLines(GridChartLines flags)
notify("gridChartLines",QVariant(oldValue),QVariant(flags)); notify("gridChartLines",QVariant(oldValue),QVariant(flags));
} }
void ChartItem::setCharItemFont(QFont value)
{
if (font() == value) {
return;
}
QFont oldValue = font();
setFont(value);
if (!isLoading()) update();
notify("font",oldValue,value);
}
QFont ChartItem::titleFont() const
{
return m_chart->titleFont();
}
void ChartItem::setTitleFont(QFont value)
{
if (m_chart->titleFont() == value){
return;
}
QFont oldValue = value;
m_chart->setTitleFont(value);
if (!isLoading()) update();
notify("titleFont", oldValue, value);
}
AbstractChart::AbstractChart(ChartItem *chartItem) AbstractChart::AbstractChart(ChartItem *chartItem)
:m_chartItem(chartItem) :m_chartItem(chartItem)
{ {
@ -568,6 +604,17 @@ QRectF AbstractChart::calcChartLegendRect(const QFont &font, const QRectF &paren
return legendRect; return legendRect;
} }
QFont AbstractChart::titleFont()
{
return m_titleFont;
}
void AbstractChart::setTitleFont(const QFont &value)
{
m_titleFont = value;
}
void AbstractChart::prepareLegendToPaint(QRectF &legendRect, QPainter *painter) void AbstractChart::prepareLegendToPaint(QRectF &legendRect, QPainter *painter)
{ {
QFont tmpFont = painter->font(); QFont tmpFont = painter->font();

View File

@ -78,9 +78,15 @@ public:
virtual void paintChartLegend(QPainter *painter, QRectF legendRect) =0; virtual void paintChartLegend(QPainter *painter, QRectF legendRect) =0;
virtual QSizeF calcChartLegendSize(const QFont &font) = 0; virtual QSizeF calcChartLegendSize(const QFont &font) = 0;
virtual QRectF calcChartLegendRect(const QFont& font, const QRectF& parentRect, bool takeAllRect, qreal borderMargin, qreal titleOffset); virtual QRectF calcChartLegendRect(const QFont& font, const QRectF& parentRect, bool takeAllRect, qreal borderMargin, qreal titleOffset);
QFont titleFont();
void setTitleFont(const QFont &value);
protected: protected:
virtual void prepareLegendToPaint(QRectF& legendRect, QPainter *painter); virtual void prepareLegendToPaint(QRectF& legendRect, QPainter *painter);
protected: protected:
// Title font must be placed here instead of CharItem, becuase
// it would cause crash when creating CharItem object on embedded
QFont m_titleFont;
ChartItem* m_chartItem; ChartItem* m_chartItem;
QList<QString> m_designLabels; QList<QString> m_designLabels;
}; };
@ -139,6 +145,8 @@ class ChartItem : public LimeReport::ItemDesignIntf
Q_PROPERTY(ChartType chartType READ chartType WRITE setChartType) Q_PROPERTY(ChartType chartType READ chartType WRITE setChartType)
Q_PROPERTY(QString labelsField READ labelsField WRITE setLabelsField) Q_PROPERTY(QString labelsField READ labelsField WRITE setLabelsField)
Q_PROPERTY(bool showLegend READ showLegend WRITE setShowLegend) Q_PROPERTY(bool showLegend READ showLegend WRITE setShowLegend)
Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
Q_PROPERTY(QFont font READ font WRITE setCharItemFont)
//linesChart //linesChart
Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints) Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints)
@ -226,6 +234,10 @@ public:
GridChartLines gridChartLines() const; GridChartLines gridChartLines() const;
void setGridChartLines(GridChartLines flags); void setGridChartLines(GridChartLines flags);
QFont titleFont() const;
void setTitleFont(QFont value);
void setCharItemFont(QFont value);
protected: protected:
void paintChartTitle(QPainter* painter, QRectF titleRect); void paintChartTitle(QPainter* painter, QRectF titleRect);
virtual BaseDesignIntf* createSameTypeItem(QObject *owner, QGraphicsItem *parent); virtual BaseDesignIntf* createSameTypeItem(QObject *owner, QGraphicsItem *parent);