0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-27 09:58:11 +03:00
LimeReport/limereport/items/lrchartitem.h

307 lines
11 KiB
C
Raw Normal View History

2016-11-02 01:34:45 +03:00
#ifndef LRCHARTITEM_H
#define LRCHARTITEM_H
#include "lritemdesignintf.h"
2017-09-27 01:05:22 +03:00
#include "lrglobal.h"
#include "lraxisdata.h"
#include <QtGlobal>
2016-11-02 01:34:45 +03:00
namespace LimeReport{
QColor generateColor();
extern QColor color_map[39];
class IDataSource;
class SeriesItemData : public QObject{
Q_OBJECT
public:
QList<qreal>& values(){ return m_values;}
2022-01-25 21:13:00 +03:00
QList<qreal>& xAxisValues(){ return m_xAxisValues;}
2016-11-02 01:34:45 +03:00
QList<QString>& labels(){ return m_labels;}
QList<QColor>& colors() { return m_colors;}
2019-12-18 23:13:33 +03:00
void clear(){ m_values.clear(); m_labels.clear(); m_colors.clear(); }
2016-11-02 01:34:45 +03:00
private:
2022-01-25 21:13:00 +03:00
QList<qreal> m_values, m_xAxisValues;
2016-11-02 01:34:45 +03:00
QList<QString> m_labels;
QList<QColor> m_colors;
};
class SeriesItem : public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
2021-08-18 05:32:45 +03:00
Q_PROPERTY(QString valuesColumn READ valuesColumn WRITE setValuesColumn)
Q_PROPERTY(QString labelsColumn READ labelsColumn WRITE setLabelsColumn)
2022-01-25 21:13:00 +03:00
Q_PROPERTY(QString xAxisColumn READ xAxisColumn WRITE setXAxisColumn)
2016-11-02 01:34:45 +03:00
Q_PROPERTY(QColor color READ color WRITE setColor)
2017-09-27 01:05:22 +03:00
Q_PROPERTY(SeriesItemPreferredType preferredType READ preferredType WRITE setPreferredType)
2016-11-02 01:34:45 +03:00
public:
2017-09-27 01:05:22 +03:00
enum SeriesItemPreferredType {Bar, Line};
2021-08-24 10:22:30 +03:00
#if QT_VERSION >= 0x050500
Q_ENUM(SeriesItemPreferredType)
#else
2020-03-03 05:19:59 +03:00
Q_ENUMS(SeriesItemPreferredType)
#endif
2017-09-27 01:05:22 +03:00
SeriesItem(QObject* parent = 0) : QObject(parent), m_preferredType(Bar){}
2016-11-02 01:34:45 +03:00
QString name() const;
void setName(const QString &name);
QString valuesColumn() const;
void setValuesColumn(const QString &valuesColumn);
QString labelsColumn() const;
void setLabelsColumn(const QString &labelsColumn);
2022-01-25 21:13:00 +03:00
QString xAxisColumn() const;
void setXAxisColumn(const QString &xAxisColumn);
2016-11-02 01:34:45 +03:00
SeriesItem* clone();
void fillSeriesData(IDataSource* dataSource);
SeriesItemData* data(){ return &m_data;}
QColor color() const;
void setColor(const QColor &color);
2017-09-27 01:05:22 +03:00
SeriesItemPreferredType preferredType() const;
void setPreferredType(const SeriesItemPreferredType& preferredType);
2020-04-06 19:48:16 +03:00
bool isEmpty(){ return m_data.values().isEmpty();}
2016-11-02 01:34:45 +03:00
private:
QString m_name;
QString m_valuesColumn;
QString m_labelsColumn;
2022-01-25 21:13:00 +03:00
QString m_xAxisColumn;
2016-11-02 01:34:45 +03:00
SeriesItemData m_data;
QColor m_color;
2017-09-27 01:05:22 +03:00
SeriesItemPreferredType m_preferredType;
2016-11-02 01:34:45 +03:00
};
class ChartItem;
class AbstractChart {
public:
AbstractChart(ChartItem* chartItem);
virtual ~AbstractChart(){}
virtual void paintChart(QPainter *painter, QRectF rect) = 0;
virtual void paintChartLegend(QPainter *painter, QRectF legendRect) =0;
2022-02-08 01:16:07 +03:00
virtual QSizeF calcChartLegendSize(const QFont &font, qreal maxWidth = 0) = 0;
2016-11-02 01:34:45 +03:00
virtual QRectF calcChartLegendRect(const QFont& font, const QRectF& parentRect, bool takeAllRect, qreal borderMargin, qreal titleOffset);
2022-01-31 18:43:47 +03:00
QFont titleFont();
void setTitleFont(const QFont &value);
2016-11-02 01:34:45 +03:00
protected:
2022-02-08 01:16:07 +03:00
QVector<qreal> legendColumnWidths() const;
2016-11-02 01:34:45 +03:00
virtual void prepareLegendToPaint(QRectF& legendRect, QPainter *painter);
protected:
2022-01-31 18:43:47 +03:00
// Title font must be placed here instead of CharItem, becuase
// it would cause crash when creating CharItem object on embedded
QFont m_titleFont;
2016-11-02 01:34:45 +03:00
ChartItem* m_chartItem;
QList<QString> m_designLabels;
2022-02-08 01:16:07 +03:00
QVector<qreal> m_legendColumnWidths;
2016-11-02 01:34:45 +03:00
};
class AbstractSeriesChart: public AbstractChart{
public:
AbstractSeriesChart(ChartItem* chartItem);
protected:
2022-03-09 01:44:49 +03:00
AxisData &xAxisData() const;
AxisData &yAxisData() const;
2016-11-02 01:34:45 +03:00
qreal maxValue();
qreal minValue();
2022-01-05 14:13:05 +03:00
void updateMinAndMaxValues();
2016-11-02 01:34:45 +03:00
int valuesCount();
int seriesCount();
2020-01-11 02:11:55 +03:00
bool verticalLabels(QPainter* painter, QRectF labelsRect);
2022-02-08 01:16:07 +03:00
QSizeF calcChartLegendSize(const QFont &font, qreal maxWidth);
2016-11-02 01:34:45 +03:00
qreal* designValues(){ return m_designValues;}
2020-01-11 02:11:55 +03:00
virtual qreal hPadding(QRectF chartRect);
virtual qreal vPadding(QRectF chartRect);
virtual void paintHorizontalLabels(QPainter *painter, QRectF labelsRect);
virtual void paintVerticalLabels(QPainter *painter, QRectF labelsRect);
virtual void paintHorizontalGrid(QPainter *painter, QRectF gridRect);
2022-01-25 21:46:14 +03:00
virtual void paintGrid(QPainter *painter, QRectF gridRect);
2020-01-11 02:11:55 +03:00
virtual void paintVerticalGrid(QPainter *painter, QRectF gridRect);
virtual void drawSegment(QPainter *painter, QPoint startPoint, QPoint endPoint, QColor color);
virtual qreal valuesHMargin(QPainter *painter);
virtual qreal valuesVMargin(QPainter *painter);
virtual QFont adaptLabelsFont(QRectF rect, QFont font);
2022-01-30 19:02:24 +03:00
virtual QFont adaptFont(qreal width, QFont font, const AxisData &axisData);
2022-01-25 21:46:14 +03:00
virtual QString axisLabel(int i, const AxisData &axisData);
2020-01-11 02:11:55 +03:00
2016-11-02 01:34:45 +03:00
private:
2022-02-08 01:16:07 +03:00
bool calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm);
bool calculateLegendSingleColumnWidth(qreal &currentRowWidth, int &currentColumn, int &maxColumnCount,
2022-03-05 18:40:12 +03:00
const qreal itemWidth, const qreal maxRowWidth);
2016-11-02 01:34:45 +03:00
qreal m_designValues [9];
};
class AbstractBarChart: public AbstractSeriesChart{
public:
AbstractBarChart(ChartItem* chartItem):AbstractSeriesChart(chartItem){}
void paintChartLegend(QPainter *painter, QRectF legendRect);
protected:
2020-01-11 02:11:55 +03:00
QRectF verticalLabelsRect(QPainter* painter, QRectF horizontalLabelsRect);
virtual QRectF horizontalLabelsRect(QPainter* painter, QRectF horizontalLabelsRect);
2022-02-08 01:16:07 +03:00
private:
void drawVerticalLegendItem(QPainter *painter, int i, const QString &text,
int indicatorSize, const QRectF &indicatorsRect, const QColor &indicatorColor);
void drawHorizontalLegendItem(QPainter *painter, int i, const QString &text,
int indicatorSize, const QRectF &indicatorsRect, const QColor &indicatorColor);
2016-11-02 01:34:45 +03:00
};
class ChartItem : public LimeReport::ItemDesignIntf
{
Q_OBJECT
2022-03-09 01:44:49 +03:00
Q_PROPERTY(QObject* xAxisSettings READ xAxisSettings WRITE setXAxisSettings)
Q_PROPERTY(QObject* yAxisSettings READ yAxisSettings WRITE setYAxisSettings)
Q_PROPERTY(ACollectionProperty series READ fakeCollectionReader WRITE setSeries)
2016-11-02 01:34:45 +03:00
Q_PROPERTY(QString datasource READ datasource WRITE setDatasource)
Q_PROPERTY(QString chartTitle READ chartTitle WRITE setChartTitle)
Q_PROPERTY(bool drawLegendBorder READ drawLegendBorder WRITE setDrawLegendBorder)
Q_PROPERTY(LegendAlign legendAlign READ legendAlign WRITE setLegendAlign)
2022-02-08 01:16:07 +03:00
Q_PROPERTY(LegendStyle legendStyle READ legendStyle WRITE setLegendStyle)
2016-11-02 01:34:45 +03:00
Q_PROPERTY(TitleAlign titleAlign READ titleAlign WRITE setTitleAlign)
Q_PROPERTY(ChartType chartType READ chartType WRITE setChartType)
Q_PROPERTY(QString labelsField READ labelsField WRITE setLabelsField)
2020-04-06 19:48:16 +03:00
Q_PROPERTY(bool showLegend READ showLegend WRITE setShowLegend)
2022-01-31 18:43:47 +03:00
Q_PROPERTY(QFont titleFont READ titleFont WRITE setTitleFont)
Q_PROPERTY(QFont font READ font WRITE setCharItemFont)
//linesChart
Q_PROPERTY(bool drawPoints READ drawPoints WRITE setDrawPoints)
Q_PROPERTY(int seriesLineWidth READ seriesLineWidth WRITE setSeriesLineWidth)
2022-01-25 21:13:00 +03:00
Q_PROPERTY(bool horizontalAxisOnTop READ horizontalAxisOnTop WRITE setHorizontalAxisOnTop)
2022-01-25 22:05:18 +03:00
2022-01-27 21:51:40 +03:00
//gridChart
Q_FLAGS(GridChartLines)
Q_PROPERTY(QString xAxisField READ xAxisField WRITE setXAxisField)
Q_PROPERTY(GridChartLines gridChartLines READ gridChartLines WRITE setGridChartLines)
2016-11-02 01:34:45 +03:00
friend class AbstractChart;
public:
2022-02-08 01:16:07 +03:00
enum LegendAlign{LegendAlignRightTop,LegendAlignRightCenter,LegendAlignRightBottom,
LegendAlignBottomLeft,LegendAlignBottomCenter,LegendAlignBottomRight};
2022-01-25 21:46:14 +03:00
enum LegendStyle{LegendPoints, LegendLines};
2016-11-02 01:34:45 +03:00
enum TitleAlign{TitleAlignLeft, TitleAlignCenter, TitleAlignRight};
2022-01-25 21:46:14 +03:00
enum ChartType{Pie, VerticalBar, HorizontalBar, Lines, GridLines};
2022-01-27 21:51:40 +03:00
enum LineType {
NoLine = 0,
HorizontalLine = 1,
VerticalLine = 2,
AllLines = 3
};
2021-08-24 10:22:30 +03:00
#if QT_VERSION >= 0x050500
Q_ENUM(LegendAlign)
2022-02-08 01:16:07 +03:00
Q_ENUM(LegendStyle)
Q_ENUM(TitleAlign)
Q_ENUM(ChartType)
2022-01-27 21:51:40 +03:00
Q_ENUM(LineType)
#else
Q_ENUMS(LegendAlign)
2022-02-08 01:16:07 +03:00
Q_ENUMS(LegendStyle)
Q_ENUMS(TitleAlign)
Q_ENUMS(ChartType)
2022-01-27 21:51:40 +03:00
Q_ENUMS(LineType)
#endif
2022-01-27 21:51:40 +03:00
Q_DECLARE_FLAGS(GridChartLines, LineType)
2016-11-02 01:34:45 +03:00
ChartItem(QObject* owner, QGraphicsItem* parent);
~ChartItem();
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
2022-03-09 01:44:49 +03:00
QObject* xAxisSettings();
void setYAxisSettings(QObject *axis);
QObject* yAxisSettings();
void setXAxisSettings(QObject *axis);
AxisData *xAxisData();
AxisData *yAxisData();
2022-03-10 22:35:48 +03:00
void showAxisEditorDialog(bool isXAxis);
2016-11-02 01:34:45 +03:00
QList<SeriesItem *> &series();
void setSeries(const QList<SeriesItem *> &series);
bool isSeriesExists(const QString& name);
QString datasource() const;
void setDatasource(const QString &datasource);
QString chartTitle() const;
void setChartTitle(const QString &chartTitle);
bool drawLegendBorder() const;
void setDrawLegendBorder(bool drawLegendBorder);
LegendAlign legendAlign() const;
void setLegendAlign(const LegendAlign &legendAlign);
2022-02-08 01:16:07 +03:00
LegendStyle legendStyle() const;
void setLegendStyle(const LegendStyle &legendStyle);
2016-11-02 01:34:45 +03:00
TitleAlign titleAlign() const;
void setTitleAlign(const TitleAlign &titleAlign);
ChartType chartType() const;
void setChartType(const ChartType &chartType);
QString labelsField() const;
void setLabelsField(const QString &labelsField);
QList<QString> labels() const;
void setLabels(const QList<QString> &labels);
QWidget* defaultEditor();
2016-11-02 01:34:45 +03:00
2020-04-06 19:48:16 +03:00
bool showLegend() const;
void setShowLegend(bool showLegend);
bool drawPoints() const;
void setDrawPoints(bool drawPoints);
int seriesLineWidth() const;
void setSeriesLineWidth(int newSeriesLineWidth);
2022-01-25 21:13:00 +03:00
QString xAxisField() const;
void setXAxisField(const QString &xAxisField);
bool horizontalAxisOnTop() const;
void setHorizontalAxisOnTop(bool horizontalAxisOnTop);
2022-01-27 21:51:40 +03:00
GridChartLines gridChartLines() const;
void setGridChartLines(GridChartLines flags);
2022-01-31 18:43:47 +03:00
QFont titleFont() const;
void setTitleFont(QFont value);
void setCharItemFont(QFont value);
2022-03-09 01:44:49 +03:00
QSettings *settings();
2016-11-02 01:34:45 +03:00
protected:
void paintChartTitle(QPainter* painter, QRectF titleRect);
virtual BaseDesignIntf* createSameTypeItem(QObject *owner, QGraphicsItem *parent);
//ICollectionContainer
2019-12-18 23:13:33 +03:00
QObject* createElement(const QString& collectionName, const QString& elementType);
2016-11-02 01:34:45 +03:00
int elementsCount(const QString& collectionName);
QObject* elementAt(const QString& collectionName,int index);
void collectionLoadFinished(const QString& collectionName){Q_UNUSED(collectionName)}
void updateItemSize(DataSourceManager *dataManager, RenderPass, int);
void fillLabels(IDataSource* dataSource);
2019-11-26 23:40:36 +03:00
bool isNeedUpdateSize(RenderPass pass) const;
void setSeries(ACollectionProperty series){Q_UNUSED(series)}
2016-11-02 01:34:45 +03:00
private:
QList<SeriesItem*> m_series;
QString m_datasource;
QPixmap m_chartImage;
QString m_title;
AbstractChart* m_chart;
bool m_legendBorder;
LegendAlign m_legendAlign;
TitleAlign m_titleAlign;
ChartType m_chartType;
QString m_labelsField;
QList<QString> m_labels;
2020-04-06 19:48:16 +03:00
bool m_isEmpty;
bool m_showLegend;
bool m_drawPoints;
int m_seriesLineWidth;
2022-01-25 21:13:00 +03:00
QString m_xAxisField;
bool m_horizontalAxisOnTop;
2022-01-27 21:51:40 +03:00
GridChartLines m_gridChartLines;
2022-02-08 01:16:07 +03:00
LegendStyle m_legendStyle;
2022-03-09 01:44:49 +03:00
AxisData *m_xAxisData, *m_yAxisData;
2016-11-02 01:34:45 +03:00
};
} //namespace LimeReport
#endif // LRCHARTITEM_H