1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* Copyright 2015-2017 Milian Wolff <mail@milianw.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "histogramwidget.h"
#include <QSortFilterProxyModel>
#include <QVBoxLayout>
#include <KChartBarDiagram>
#include <KChartChart>
#include <KChartBackgroundAttributes>
#include <KChartCartesianCoordinatePlane>
#include <KChartDataValueAttributes>
#include <KChartFrameAttributes.h>
#include <KChartGridAttributes>
#include <KChartHeaderFooter>
#include <KChartLegend>
#include <KColorScheme>
#include <KFormat>
#include <KLocalizedString>
#include "histogrammodel.h"
using namespace KChart;
namespace {
class SizeAxis : public CartesianAxis
{
Q_OBJECT
public:
explicit SizeAxis(AbstractCartesianDiagram* diagram = nullptr)
: CartesianAxis(diagram)
{
}
const QString customizedLabel(const QString& label) const override
{
KFormat format(QLocale::system());
return format.formatByteSize(label.toDouble(), 1, KFormat::MetricBinaryDialect);
}
};
class HistogramProxy : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit HistogramProxy(bool showTotal, QObject* parent = nullptr)
: QSortFilterProxyModel(parent)
, m_showTotal(showTotal)
{
}
virtual ~HistogramProxy() = default;
protected:
bool filterAcceptsColumn(int sourceColumn, const QModelIndex& /*sourceParent*/) const override
{
if (m_showTotal) {
return sourceColumn == 0;
} else {
return sourceColumn != 0;
}
}
private:
bool m_showTotal;
};
}
HistogramWidget::HistogramWidget(QWidget* parent)
: QWidget(parent)
, m_chart(new KChart::Chart(this))
, m_total(new BarDiagram(this))
, m_detailed(new BarDiagram(this))
{
auto layout = new QVBoxLayout(this);
layout->addWidget(m_chart);
setLayout(layout);
auto* coordinatePlane = dynamic_cast<CartesianCoordinatePlane*>(m_chart->coordinatePlane());
Q_ASSERT(coordinatePlane);
{
m_total->setAntiAliasing(true);
KColorScheme scheme(QPalette::Active, KColorScheme::Window);
QPen foreground(scheme.foreground().color());
auto bottomAxis = new CartesianAxis(m_total);
auto axisTextAttributes = bottomAxis->textAttributes();
axisTextAttributes.setPen(foreground);
bottomAxis->setTextAttributes(axisTextAttributes);
auto axisTitleTextAttributes = bottomAxis->titleTextAttributes();
axisTitleTextAttributes.setPen(foreground);
bottomAxis->setTitleTextAttributes(axisTitleTextAttributes);
bottomAxis->setPosition(KChart::CartesianAxis::Bottom);
bottomAxis->setTitleText(i18n("Requested Allocation Size"));
m_total->addAxis(bottomAxis);
auto* rightAxis = new CartesianAxis(m_total);
rightAxis->setTextAttributes(axisTextAttributes);
rightAxis->setTitleTextAttributes(axisTitleTextAttributes);
rightAxis->setTitleText(i18n("Number of Allocations"));
rightAxis->setPosition(CartesianAxis::Right);
m_total->addAxis(rightAxis);
coordinatePlane->addDiagram(m_total);
m_total->setType(BarDiagram::Normal);
}
{
m_detailed->setAntiAliasing(true);
coordinatePlane->addDiagram(m_detailed);
m_detailed->setType(BarDiagram::Stacked);
}
}
HistogramWidget::~HistogramWidget() = default;
void HistogramWidget::setModel(QAbstractItemModel* model)
{
{
auto proxy = new HistogramProxy(true, this);
proxy->setSourceModel(model);
m_total->setModel(proxy);
}
{
auto proxy = new HistogramProxy(false, this);
proxy->setSourceModel(model);
m_detailed->setModel(proxy);
}
}
#include "histogramwidget.moc"
|