##// END OF EJS Templates
Fix explicitly set default pen/brush/font getting overridden by theme...
Miikka Heikkinen -
r2516:567bb8091928
parent child
Show More
@@ -331,6 +331,8 bool QAreaSeries::pointsVisible() const
331 331
332 332 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries, QAreaSeries *q)
333 333 : QAbstractSeriesPrivate(q),
334 m_brush(QChartPrivate::defaultBrush()),
335 m_pen(QChartPrivate::defaultPen()),
334 336 m_upperSeries(upperSeries),
335 337 m_lowerSeries(lowerSeries),
336 338 m_pointsVisible(false)
@@ -427,19 +429,18 QAbstractAxis* QAreaSeriesPrivate::createDefaultAxis(Qt::Orientation orientation
427 429 void QAreaSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
428 430 {
429 431 Q_Q(QAreaSeries);
430 QPen pen;
431 QBrush brush;
432 432
433 433 const QList<QGradient> gradients = theme->seriesGradients();
434 434 const QList<QColor> colors = theme->seriesColors();
435 435
436 if (forced || pen == m_pen) {
436 if (forced || QChartPrivate::defaultPen() == m_pen) {
437 QPen pen;
437 438 pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
438 439 pen.setWidthF(2);
439 440 q->setPen(pen);
440 441 }
441 442
442 if (forced || brush == m_brush) {
443 if (forced || QChartPrivate::defaultBrush() == m_brush) {
443 444 QBrush brush(colors.at(index % colors.size()));
444 445 q->setBrush(brush);
445 446 }
@@ -22,6 +22,7
22 22 #include "qabstractaxis_p.h"
23 23 #include "chartdataset_p.h"
24 24 #include "charttheme_p.h"
25 #include "qchart_p.h"
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28
@@ -876,16 +877,25 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis *q)
876 877 m_orientation(Qt::Orientation(0)),
877 878 m_visible(true),
878 879 m_arrowVisible(true),
880 m_axisPen(QChartPrivate::defaultPen()),
881 m_axisBrush(QChartPrivate::defaultBrush()),
879 882 m_gridLineVisible(true),
883 m_gridLinePen(QChartPrivate::defaultPen()),
880 884 m_labelsVisible(true),
885 m_labelsPen(QChartPrivate::defaultPen()),
886 m_labelsBrush(QChartPrivate::defaultBrush()),
887 m_labelsFont(QChartPrivate::defaultFont()),
881 888 m_labelsAngle(0),
882 889 m_titleVisible(true),
890 m_titlePen(QChartPrivate::defaultPen()),
891 m_titleBrush(QChartPrivate::defaultBrush()),
892 m_titleFont(QChartPrivate::defaultFont()),
883 893 m_shadesVisible(false),
884 m_shadesBrush(Qt::SolidPattern),
894 m_shadesBrush(QChartPrivate::defaultBrush()),
895 m_shadesPen(QChartPrivate::defaultPen()),
885 896 m_shadesOpacity(1.0),
886 897 m_dirty(false)
887 898 {
888
889 899 }
890 900
891 901 QAbstractAxisPrivate::~QAbstractAxisPrivate()
@@ -912,61 +922,42 void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment)
912 922
913 923 void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced)
914 924 {
915 QPen pen;
916 QBrush brush;
917 QFont font;
918
919 bool axisX = m_orientation == Qt::Horizontal;
920
921 925 //TODO: introduce axis brush
922 if (m_visible) {
923 if (m_arrowVisible) {
924 if (forced || pen == m_axisPen) {
926 if (forced || QChartPrivate::defaultPen() == m_axisPen)
925 927 q_ptr->setLinePen(theme->axisLinePen());
926 }
927 }
928 if (m_gridLineVisible) {
929 if (forced || pen == m_gridLinePen) {
928
929 if (forced || QChartPrivate::defaultPen() == m_gridLinePen)
930 930 q_ptr->setGridLinePen(theme->girdLinePen());
931 }
932 }
933 if (m_labelsVisible) {
934 if (forced || brush == m_labelsBrush){
931
932 if (forced || QChartPrivate::defaultBrush() == m_labelsBrush)
935 933 q_ptr->setLabelsBrush(theme->labelBrush());
936 }
937 if (forced || pen == m_labelsPen){
934 if (forced || QChartPrivate::defaultPen() == m_labelsPen)
938 935 q_ptr->setLabelsPen(Qt::NoPen);// NoPen for performance reasons
939 }
940 if (forced || font == m_labelsFont){
936 if (forced || QChartPrivate::defaultFont() == m_labelsFont)
941 937 q_ptr->setLabelsFont(theme->labelFont());
942 }
943 }
944 if (m_titleVisible) {
945 if (forced || brush == m_titleBrush){
938
939 if (forced || QChartPrivate::defaultBrush() == m_titleBrush)
946 940 q_ptr->setTitleBrush(theme->labelBrush());
947 }
948 if (forced || pen == m_titlePen){
949 q_ptr->setTitlePen(Qt::NoPen);// Noen for performance reasons
950 }
951 if (forced || font == m_titleFont){
941 if (forced || QChartPrivate::defaultPen() == m_titlePen)
942 q_ptr->setTitlePen(Qt::NoPen); // NoPen for performance reasons
943 if (forced || QChartPrivate::defaultFont() == m_titleFont) {
952 944 QFont font(m_labelsFont);
953 945 font.setBold(true);
954 946 q_ptr->setTitleFont(font);
955 947 }
956 }
957 if (forced || m_shadesVisible) {
958 if (forced || brush == m_shadesBrush){
948
949 if (forced || QChartPrivate::defaultBrush() == m_shadesBrush)
959 950 q_ptr->setShadesBrush(theme->backgroundShadesBrush());
960 }
961 if (forced || pen == m_shadesPen){
951 if (forced || QChartPrivate::defaultPen() == m_shadesPen)
962 952 q_ptr->setShadesPen(theme->backgroundShadesPen());
963 }
953
954 bool axisX = m_orientation == Qt::Horizontal;
964 955 if (forced && (theme->backgroundShades() == ChartTheme::BackgroundShadesBoth
965 956 || (theme->backgroundShades() == ChartTheme::BackgroundShadesVertical && axisX)
966 957 || (theme->backgroundShades() == ChartTheme::BackgroundShadesHorizontal && !axisX))) {
967 958 q_ptr->setShadesVisible(true);
968 }
969 }
959 } else if (forced) {
960 q_ptr->setShadesVisible(false);
970 961 }
971 962 }
972 963
@@ -85,6 +85,7 protected:
85 85 QAbstractAxis *q_ptr;
86 86 QChart *m_chart;
87 87 QScopedPointer<ChartAxisElement> m_item;
88
88 89 private:
89 90 QList<QAbstractSeries*> m_series;
90 91
@@ -30,6 +30,7
30 30 #include "qbarlegendmarker.h"
31 31 #include "baranimation_p.h"
32 32 #include "abstractbarchartitem_p.h"
33 #include "qchart_p.h"
33 34
34 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 36
@@ -842,9 +843,6 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bo
842 843 {
843 844 const QList<QGradient> gradients = theme->seriesGradients();
844 845
845 QBrush brush;
846 QPen pen;
847
848 846 qreal takeAtPos = 0.5;
849 847 qreal step = 0.2;
850 848 if (m_barSets.count() > 1) {
@@ -864,19 +862,19 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bo
864 862 takeAtPos += step;
865 863 takeAtPos -= (int) takeAtPos;
866 864 }
867 if (forced || brush == m_barSets.at(i)->brush())
865 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->brush())
868 866 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
869 867
870 868 // Pick label color from the opposite end of the gradient.
871 869 // 0.3 as a boundary seems to work well.
872 if (forced || brush == m_barSets.at(i)->labelBrush()) {
870 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->labelBrush()) {
873 871 if (takeAtPos < 0.3)
874 872 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
875 873 else
876 874 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
877 875 }
878 876
879 if (forced || pen == m_barSets.at(i)->pen()) {
877 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->pen()) {
880 878 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
881 879 m_barSets.at(i)->setPen(c);
882 880 }
@@ -21,6 +21,7
21 21 #include "qbarset.h"
22 22 #include "qbarset_p.h"
23 23 #include "charthelpers_p.h"
24 #include "qchart_p.h"
24 25
25 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 27
@@ -546,7 +547,10 void QBarSet::setLabelColor(QColor color)
546 547
547 548 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
548 549 q_ptr(parent),
549 m_label(label)
550 m_label(label),
551 m_pen(QChartPrivate::defaultPen()),
552 m_brush(QChartPrivate::defaultBrush()),
553 m_labelBrush(QChartPrivate::defaultBrush())
550 554 {
551 555 }
552 556
@@ -152,7 +152,8 void ChartPresenter::createBackgroundItem()
152 152 {
153 153 if (!m_background) {
154 154 m_background = new ChartBackground(rootItem());
155 m_background->setPen(Qt::NoPen);
155 m_background->setPen(Qt::NoPen); // Theme doesn't touch pen so don't use default
156 m_background->setBrush(QChartPrivate::defaultBrush());
156 157 m_background->setZValue(ChartPresenter::BackgroundZValue);
157 158 }
158 159 }
@@ -41,8 +41,7 ChartThemeManager::ChartThemeManager(QChart* chart) :
41 41
42 42 void ChartThemeManager::setTheme(QChart::ChartTheme theme)
43 43 {
44 if(m_theme.isNull() || theme != m_theme->id())
45 {
44 if (m_theme.isNull() || theme != m_theme->id()) {
46 45 switch (theme) {
47 46 case QChart::ChartThemeLight:
48 47 m_theme.reset(new ChartThemeLight());
@@ -70,57 +69,39 void ChartThemeManager::setTheme(QChart::ChartTheme theme)
70 69 break;
71 70 }
72 71
73 if(!m_theme.isNull())
74 {
75 decorateChart(m_chart,m_theme.data(),true);
76 decorateLegend(m_chart->legend(),m_theme.data(),true);
77 foreach(QAbstractAxis* axis, m_axisList) {
72 if (!m_theme.isNull()) {
73 decorateChart(m_chart,m_theme.data());
74 decorateLegend(m_chart->legend(),m_theme.data());
75 foreach (QAbstractAxis* axis, m_axisList)
78 76 axis->d_ptr->initializeTheme(m_theme.data(),true);
79 }
80 foreach(QAbstractSeries* series, m_seriesMap.keys()) {
77 foreach (QAbstractSeries* series, m_seriesMap.keys())
81 78 series->d_ptr->initializeTheme(m_seriesMap[series],m_theme.data(),true);
82 79 }
83
84 }
85 80 }
86 81 }
87 82
88 void ChartThemeManager::decorateChart(QChart *chart,ChartTheme* theme,bool force) const
83 // decorateChart is only called when theme is forcibly initialized
84 void ChartThemeManager::decorateChart(QChart *chart, ChartTheme *theme) const
89 85 {
90 QBrush brush;
91
92 if (force || brush == chart->backgroundBrush())
93 86 chart->setBackgroundBrush(theme->chartBackgroundGradient());
94 87
95 if (force) {
96 // Always clear plotArea brush when forced update, do not touch otherwise
97 88 QPen pen(Qt::transparent);
89 QBrush brush;
98 90 chart->setPlotAreaBackgroundBrush(brush);
99 91 chart->setPlotAreaBackgroundPen(pen);
100 92 chart->setPlotAreaBackgroundVisible(false);
101 }
102 93
103 94 chart->setTitleFont(theme->masterFont());
104 95 chart->setTitleBrush(theme->labelBrush());
105 96 chart->setDropShadowEnabled(theme->isBackgroundDropShadowEnabled());
106 97 }
107 98
108 void ChartThemeManager::decorateLegend(QLegend *legend, ChartTheme* theme, bool force) const
99 // decorateLegend is only called when theme is forcibly initialized
100 void ChartThemeManager::decorateLegend(QLegend *legend, ChartTheme *theme) const
109 101 {
110 QPen pen;
111 QBrush brush;
112 QFont font;
113
114 if (force || pen == legend->pen())
115 102 legend->setPen(theme->axisLinePen());
116
117 if (force || brush == legend->brush())
118 103 legend->setBrush(theme->chartBackgroundGradient());
119
120 if (force || font == legend->font())
121 104 legend->setFont(theme->labelFont());
122
123 if (force || brush == legend->labelBrush())
124 105 legend->setLabelBrush(theme->labelBrush());
125 106 }
126 107
@@ -53,8 +53,8 public:
53 53 explicit ChartThemeManager(QChart* chart);
54 54 void setTheme(QChart::ChartTheme theme);
55 55 ChartTheme* theme() const { return m_theme.data(); }
56 void decorateChart(QChart *chart, ChartTheme* theme, bool force = false) const;
57 void decorateLegend(QLegend *legend, ChartTheme* theme, bool force = false) const;
56 void decorateChart(QChart *chart, ChartTheme* theme) const;
57 void decorateLegend(QLegend *legend, ChartTheme* theme) const;
58 58 void updateSeries(QAbstractSeries *series);
59 59
60 60 public:
@@ -23,7 +23,7
23 23 #include "linechartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26
26 #include "qchart_p.h"
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
@@ -149,8 +149,8 void QLineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forc
149 149 Q_Q(QLineSeries);
150 150 const QList<QColor> colors = theme->seriesColors();
151 151
152 if (forced || QChartPrivate::defaultPen() == m_pen) {
152 153 QPen pen;
153 if (forced || pen == m_pen) {
154 154 pen.setColor(colors.at(index % colors.size()));
155 155 pen.setWidthF(2);
156 156 q->setPen(pen);
@@ -687,6 +687,36 QChartPrivate::~QChartPrivate()
687 687 {
688 688 }
689 689
690 // Hackish solution to the problem of explicitly assigning the default pen/brush/font
691 // to a series or axis and having theme override it:
692 // Initialize pens, brushes, and fonts to something nobody is likely to ever use,
693 // so that default theme initialization will always set these properly.
694 QPen &QChartPrivate::defaultPen()
695 {
696 static QPen *defaultPen = 0;
697 if (!defaultPen)
698 defaultPen = new QPen(QColor(1, 2, 0), 0.93247536);
699 return *defaultPen;
700 }
701
702 QBrush &QChartPrivate::defaultBrush()
703 {
704 static QBrush *defaultBrush = 0;
705 if (!defaultBrush)
706 defaultBrush = new QBrush(QColor(1, 2, 0), Qt::Dense7Pattern);
707 return *defaultBrush;
708 }
709
710 QFont &QChartPrivate::defaultFont()
711 {
712 static QFont *defaultFont = 0;
713 if (!defaultFont) {
714 defaultFont = new QFont();
715 defaultFont->setPointSizeF(8.34563465);
716 }
717 return *defaultFont;
718 }
719
690 720 void QChartPrivate::init()
691 721 {
692 722 m_legend = new LegendScroller(q_ptr);
@@ -53,6 +53,10 public:
53 53 ChartThemeManager *m_themeManager;
54 54 QChart::ChartType m_type;
55 55
56 static QPen &defaultPen();
57 static QBrush &defaultBrush();
58 static QFont &defaultFont();
59
56 60 void init();
57 61 void zoomIn(qreal factor);
58 62 void zoomOut(qreal factor);
@@ -24,6 +24,7
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "scatteranimation_p.h"
27 #include "qchart_p.h"
27 28
28 29 /*!
29 30 \class QScatterSeries
@@ -266,18 +267,17 void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
266 267 void QScatterSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
267 268 {
268 269 Q_Q(QScatterSeries);
269 QPen pen;
270 QBrush brush;
271 270 const QList<QColor> colors = theme->seriesColors();
272 271 const QList<QGradient> gradients = theme->seriesGradients();
273 272
274 if (forced || pen == m_pen) {
273 if (forced || QChartPrivate::defaultPen() == m_pen) {
274 QPen pen;
275 275 pen.setColor(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0));
276 276 pen.setWidthF(2);
277 277 q->setPen(pen);
278 278 }
279 279
280 if (forced || brush == m_brush) {
280 if (forced || QChartPrivate::defaultBrush() == m_brush) {
281 281 QBrush brush(colors.at(index % colors.size()));
282 282 q->setBrush(brush);
283 283 }
@@ -24,6 +24,7
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "splineanimation_p.h"
27 #include "qchart_p.h"
27 28
28 29 /*!
29 30 \class QSplineSeries
@@ -126,8 +127,8 void QSplineSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool fo
126 127 Q_Q(QSplineSeries);
127 128 const QList<QColor> colors = theme->seriesColors();
128 129
130 if (forced || QChartPrivate::defaultPen() == m_pen) {
129 131 QPen pen;
130 if (forced || pen == m_pen) {
131 132 pen.setColor(colors.at(index % colors.size()));
132 133 pen.setWidthF(2);
133 134 q->setPen(pen);
@@ -50,7 +50,9 public:
50 50 protected:
51 51 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeLight):m_id(id),
52 52 m_backgroundShades(BackgroundShadesNone),
53 m_backgroundDropShadowEnabled(false){};
53 m_backgroundShadesBrush(Qt::SolidPattern),
54 m_backgroundDropShadowEnabled(false)
55 {};
54 56 public:
55 57 QChart::ChartTheme id() const { return m_id; }
56 58 QList<QGradient> seriesGradients() const { return m_seriesGradients; }
@@ -25,6 +25,7
25 25 #include "xychart_p.h"
26 26 #include "qxylegendmarker.h"
27 27 #include "charthelpers_p.h"
28 #include "qchart_p.h"
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
@@ -511,6 +512,8 QXYSeries &QXYSeries::operator<< (const QList<QPointF>& points)
511 512
512 513 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q)
513 514 : QAbstractSeriesPrivate(q),
515 m_pen(QChartPrivate::defaultPen()),
516 m_brush(QChartPrivate::defaultBrush()),
514 517 m_pointsVisible(false)
515 518 {
516 519 }
General Comments 0
You need to be logged in to leave comments. Login now