summaryrefslogtreecommitdiffstats
path: root/examples/charts/gallery/contentwidget.cpp
blob: 06ea8314c09a0f101a1ef2c54dc565b36e23dcd3 (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "contentwidget.h"

#include <QChart>
#include <QChartView>
#include <QFont>
#include <QLabel>
#include <QVBoxLayout>

ContentWidget::ContentWidget(QWidget *parent)
    : QWidget(parent)
{
}

void ContentWidget::load()
{
    if (m_loaded || layout())
        return;

    if (!doLoad()) {
        auto errorLabel = new QLabel(this);
        auto errorLayout = new QVBoxLayout(this);
        errorLabel->setText(tr("Error loading the example:\n%1").arg(m_loadError));
        QFont font = errorLabel->font();
        font.setPointSize(20);
        errorLabel->setFont(font);
        errorLabel->setAlignment(Qt::AlignCenter);
        errorLayout->addWidget(errorLabel);
        setLayout(errorLayout);
    }

    m_loaded = true;
}

bool ContentWidget::doLoad()
{
    // Most examples do their initialization in constructor.
    // Only those that can fail and show error message need to reimplement this method.
    return true;
}

void ContentWidget::resizeEvent(QResizeEvent *)
{
    if (m_defaultChartView)
        m_defaultChartView->resize(size());
}

// Most examples are simple and need only basic chart view widget, so provide it in this base class
// to avoid duplicating code
void ContentWidget::createDefaultChartView(QChart *chart)
{
    m_defaultChartView = new QChartView(chart, this);
    m_defaultChartView->setRenderHint(QPainter::Antialiasing);
}

void ContentWidget::setDefaultChartView(QChartView *view)
{
    m_defaultChartView = view;
    m_defaultChartView->setRenderHint(QPainter::Antialiasing);
}