Commit d70d0118 authored by Han Young's avatar Han Young Committed by Devin Lin
Browse files

port back old qtcharts code

parent a8764f3b
Pipeline #72969 passed with stage
in 17 seconds
......@@ -43,12 +43,14 @@ find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS
Gui
Svg
QuickControls2
Charts
)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
Config
Kirigami2
I18n
CoreAddons
Notifications
QuickCharts
)
find_package(KF5 0.3.0 REQUIRED COMPONENTS KWeatherCore)
......
......@@ -29,8 +29,10 @@ target_link_libraries(kweather
Qt5::Qml
Qt5::QuickControls2
Qt5::Network
Qt5::Charts
KF5::I18n
KF5::CoreAddons
KF5::Notifications
KF5::QuickCharts
KF5::KWeatherCore
kweatherLib
......
......@@ -8,8 +8,7 @@
import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
// FIXME: version bump
import org.kde.quickcharts 1.0 as Charts
import QtCharts 2.3
import org.kde.kirigami 2.13 as Kirigami
import kweather 1.0
import "backgrounds"
......@@ -21,10 +20,12 @@ Kirigami.ScrollablePage {
property WeatherLocation weatherLocation
property WeatherDay currentDay: weatherLocation.dayListModel.get(dailyListView.currentIndex)
property int maximumContentWidth: Kirigami.Units.gridUnit * 35
onInViewChanged: background.item["inView"] = inView
verticalScrollBarPolicy: ScrollBar.AlwaysOff
background: Loader {
id: background
anchors.fill: parent
......@@ -54,7 +55,7 @@ Kirigami.ScrollablePage {
ColumnLayout {
id: mainLayout
anchors.horizontalCenter: parent.horizontalCenter
width: Math.min(page.width - Kirigami.Units.largeSpacing * 4, Kirigami.Units.gridUnit * 35)
width: Math.min(page.width - Kirigami.Units.largeSpacing * 4, maximumContentWidth)
// separator from top
Item {
......@@ -208,9 +209,11 @@ Kirigami.ScrollablePage {
// temperature chart
Control {
id: tempChartCard
Layout.fillWidth: true
leftPadding: 0; rightPadding: 0; topPadding: 0; bottomPadding: 0;
implicitHeight: Math.round(Kirigami.Units.gridUnit * 8.5)
background: Kirigami.ShadowedRectangle {
color: weatherLocation.cardBackgroundColor
radius: Kirigami.Units.smallSpacing
......@@ -221,62 +224,83 @@ Kirigami.ScrollablePage {
shadow.yOffset: Kirigami.Units.devicePixelRatio * 2
}
contentItem: Flickable {
contentItem: ScrollView {
id: scrollView
Layout.fillWidth: true
contentHeight: chartChild.height
contentWidth: chartChild.width
clip: true
onContentYChanged: contentY = 0
ColumnLayout {
id: chartChild
spacing: Kirigami.Units.largeSpacing * 2
Charts.LineChart {
id: tempChart
Layout.leftMargin: Kirigami.Units.largeSpacing * 2
Layout.rightMargin: Kirigami.Units.largeSpacing * 2
Layout.topMargin: Kirigami.Units.largeSpacing
Layout.preferredHeight: Kirigami.Units.gridUnit * 5
Layout.preferredWidth: tempChartCard.width > Kirigami.Units.gridUnit * 28 ? tempChartCard.width : Kirigami.Units.gridUnit * 28
nameSource: Charts.SingleValueSource { value: i18n("MaxTemperature") }
lineWidth: Kirigami.Settings.isMobile ? 0.5 : 1
smooth: true
pointDelegate: Label {
text: String(Charts.LineChart.value.toFixed(1))
color: weatherLocation.textColor
}
valueSources: [
Charts.ArraySource {
id: tempSource
array: weatherLocation.maxTempList
contentHeight: -1
contentWidth: page.maximumContentWidth
Item {
width: page.maximumContentWidth
height: Math.round(Kirigami.Units.gridUnit * 8.5)
ChartView {
id: chartView
anchors.fill: parent
margins.left: Kirigami.Units.largeSpacing; margins.right: Kirigami.Units.largeSpacing
margins.top: 0; margins.bottom: Kirigami.Units.smallSpacing
legend.visible: false
antialiasing: true
animationOptions: ChartView.NoAnimation
backgroundColor: weatherLocation.cardBackgroundColor
plotAreaColor: weatherLocation.cardBackgroundColor
height: tempChartCard.height
width: Math.max(Kirigami.Units.gridUnit * 25, tempChartCard.width)
SplineSeries {
id: splineSeries
axisX: DateTimeAxis {
id: axisX
tickCount: dailyListView.count
format: "ddd"
labelsColor: weatherLocation.cardTextColor
lineVisible: false
gridLineColor: Qt.rgba(weatherLocation.cardTextColor.r, weatherLocation.cardTextColor.g, weatherLocation.cardTextColor.b, 0.05)
}
]
axisY: ValueAxis {
id: axisY
visible: false
colorSource: Charts.SingleValueSource {
value: Kirigami.ColorUtils.linearInterpolation(weatherLocation.backgroundColor, weatherLocation.textColor, 0.5)
min: weatherLocation.minTempLimit
max: weatherLocation.maxTempLimit
labelsColor: weatherLocation.cardTextColor
}
name: i18n("temperature")
pointLabelsVisible: true
pointLabelsFormat: "@yPoint°"
pointLabelsClipping: false
pointLabelsColor: weatherLocation.cardTextColor
pointLabelsFont.pointSize: Kirigami.Theme.defaultFont.pointSize
}
fillColorSource: Charts.SingleValueSource {
value: weatherLocation.backgroundColor
Component.onCompleted: {
weatherLocation.initAxes(axisX, axisY);
weatherLocation.initSeries(chartView.series(0));
}
}
Charts.AxisLabels {
Layout.fillWidth: true
Layout.leftMargin: Kirigami.Units.smallSpacing
Layout.rightMargin: Kirigami.Units.largeSpacing
delegate: Label {
color: weatherLocation.textColor
text: Charts.AxisLabels.label
}
source: Charts.ArraySource {
array: weatherLocation.xAxisList
// allow continuous mouse scrolling
MouseArea {
anchors.fill: parent
property int scrollDist: Kirigami.Units.gridUnit * 2
onWheel: {
//check if mouse is scrolling up or down
if (wheel.angleDelta.y < 0){
page.flickable.contentY += scrollDist
} else {
page.flickable.contentY -= scrollDist
}
}
onPressed: mouse.accepted = false // forward mouse event
onReleased: mouse.accepted = false // forward mouse event
}
}
}
}
// hourly view header
ColumnLayout {
spacing: Kirigami.Units.smallSpacing
......
......@@ -17,6 +17,10 @@
#include <QQmlEngine>
#include <QTimeZone>
#include <utility>
#include <QAbstractSeries>
#include <QDateTimeAxis>
#include <QSplineSeries>
#include <QValueAxis>
WeatherLocation::WeatherLocation()
{
......@@ -49,6 +53,7 @@ WeatherLocation::WeatherLocation(QString locationId, QString locationName, QStri
if (forecast) {
m_lastUpdated = forecast->createdTime();
determineCurrentForecast();
updateSeries();
}
}
WeatherLocation *WeatherLocation::load(const QString &groupName)
......@@ -104,7 +109,7 @@ void WeatherLocation::updateData(QExplicitlySharedDataPointer<KWeatherCore::Weat
}
determineCurrentForecast();
updateChart();
updateSeries();
m_lastUpdated = forecasts->createdTime();
emit weatherRefresh(m_forecast);
......@@ -177,12 +182,14 @@ void WeatherLocation::determineCurrentForecast()
m_backgroundColor = QStringLiteral("#3daee2");
m_textColor = m_cardTextColor;
m_iconColor = QStringLiteral("#4d4d4d");
m_isDarkTheme = false;
} else {
m_cardBackgroundColor = QStringLiteral("#333333");
m_cardTextColor = QStringLiteral("#eeeeee");
m_backgroundColor = QStringLiteral("#222222");
m_textColor = m_cardTextColor;
m_iconColor = QStringLiteral("white");
m_isDarkTheme = true;
}
emit currentForecastChange();
}
......@@ -200,28 +207,48 @@ void WeatherLocation::update()
});
}
void WeatherLocation::updateChart()
void WeatherLocation::initSeries(QtCharts::QAbstractSeries *series)
{
m_maxTempList.clear();
m_xAxisList.clear();
for (const auto &day : m_forecast->dailyWeatherForecast()) {
const auto tempToUse = Kweather::convertTemp(day.maxTemp());
m_maxTempList.append(tempToUse);
m_xAxisList.append(QLocale::system().toString(day.date(), QStringLiteral("ddd")));
if (series) {
m_series = static_cast<QtCharts::QSplineSeries *>(series);
updateSeries();
}
Q_EMIT chartListChanged();
}
const QVariantList &WeatherLocation::maxTempList()
void WeatherLocation::updateSeries()
{
return m_maxTempList;
if (m_series && !m_forecast->dailyWeatherForecast().empty()) {
m_vector.clear();
double minTemp = std::numeric_limits<double>::max(), maxTemp = std::numeric_limits<double>::min();
for (const auto &d : m_forecast->dailyWeatherForecast()) {
const auto dayMinTemp = Kweather::convertTemp(d.minTemp()), dayMaxTemp = Kweather::convertTemp(d.maxTemp());
m_vector.append(QPointF(d.date().startOfDay().toMSecsSinceEpoch(), dayMaxTemp));
minTemp = std::min<double>(dayMinTemp, minTemp);
maxTemp = std::max<double>(dayMaxTemp, maxTemp);
}
// make enough room for the curve
m_maxTempLimit = maxTemp + 5;
m_minTempLimit = minTemp - 5;
m_series->replace(m_vector);
if (m_axisX) {
m_axisX->setRange(m_forecast->dailyWeatherForecast().front().date().startOfDay(), m_forecast->dailyWeatherForecast().back().date().startOfDay());
}
}
}
const QVariantList &WeatherLocation::xAxisList()
void WeatherLocation::initAxes(QObject *axisX, QObject *axisY)
{
return m_xAxisList;
using namespace QtCharts;
if (axisX && axisY) {
m_axisX = static_cast<QDateTimeAxis *>(axisX);
m_axisY = static_cast<QValueAxis *>(axisY);
updateAxes();
}
}
void WeatherLocation::updateAxes()
{}
void WeatherLocation::updateCurrentDateTime()
{
m_timer->setInterval(60000);
......
......@@ -18,8 +18,18 @@
#include <QTimeZone>
#include <QTimer>
#include <utility>
using SharedForecastPtr = QExplicitlySharedDataPointer<KWeatherCore::WeatherForecast>;
class WeatherDayListModel;
namespace QtCharts
{
class QAbstractSeries;
class QSplineSeries;
class QDateTimeAxis;
class QValueAxis;
}
class WeatherLocation : public QObject
{
Q_OBJECT
......@@ -36,9 +46,12 @@ class WeatherLocation : public QObject
Q_PROPERTY(QColor cardBackgroundColor READ cardBackgroundColor NOTIFY currentForecastChange)
Q_PROPERTY(QColor cardTextColor READ cardTextColor NOTIFY currentForecastChange)
Q_PROPERTY(QColor iconColor READ iconColor NOTIFY currentForecastChange)
Q_PROPERTY(bool darkTheme READ darkTheme NOTIFY currentForecastChange)
Q_PROPERTY(double maxTempLimit READ maxTempLimit NOTIFY currentForecastChange)
Q_PROPERTY(double minTempLimit READ minTempLimit NOTIFY currentForecastChange)
Q_PROPERTY(QVariantList maxTempList READ maxTempList NOTIFY chartListChanged)
Q_PROPERTY(QVariantList xAxisList READ xAxisList NOTIFY chartListChanged)
public:
WeatherLocation();
explicit WeatherLocation(QString locationId,
......@@ -131,8 +144,21 @@ public:
return m_iconColor;
}
const QVariantList &maxTempList();
const QVariantList &xAxisList();
double maxTempLimit() const
{
return m_maxTempLimit;
}
double minTempLimit() const
{
return m_minTempLimit;
}
bool darkTheme() const
{
return m_isDarkTheme;
}
Q_INVOKABLE void initSeries(QtCharts::QAbstractSeries *series);
Q_INVOKABLE void initAxes(QObject *axisX, QObject *axisY);
// for restore order of locations
void saveOrder(int index);
......@@ -155,12 +181,21 @@ private slots:
private:
void determineCurrentForecast();
void updateSeries();
void updateAxes();
KWeatherCore::WeatherForecastSource m_source;
SharedForecastPtr m_forecast;
// chart related fields
QVariantList m_maxTempList, m_xAxisList;
QtCharts::QSplineSeries *m_series = nullptr;
QVector<QPointF> m_vector;
double m_maxTempLimit {100};
double m_minTempLimit {0};
QtCharts::QDateTimeAxis *m_axisX {nullptr};
QtCharts::QValueAxis *m_axisY {nullptr};
// background related fields
QColor m_backgroundColor;
......@@ -169,6 +204,7 @@ private:
QColor m_cardTextColor;
QColor m_iconColor;
QString m_backgroundComponent = QStringLiteral("backgrounds/ClearDay.qml");
bool m_isDarkTheme = false;
QString m_locationName, m_locationId;
QString m_timeZone;
......@@ -178,6 +214,4 @@ private:
WeatherDayListModel *m_weatherDayListModel = nullptr;
WeatherHourListModel *m_weatherHourListModel = nullptr;
void updateChart();
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment