src/RelpipeChartWidget.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 20 Feb 2019 11:30:49 +0100
branchv_0
changeset 37 d905557eda62
parent 29 0f9f7d6564cd
child 42 707a6734f364
permissions -rw-r--r--
Added tag v0.10 for changeset e54814c9a54d

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <iostream>

#include <QWidget>
#include <QGridLayout>
#include <QLabel>

#include <QChart>
#include <QChartView>
#include <QStackedBarSeries>
#include <QBarCategoryAxis>
#include <QVBarModelMapper>


#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include "RelpipeTableModel.h"

using namespace relpipe::reader;
using namespace relpipe::reader::handlers;

QT_CHARTS_USE_NAMESPACE

class RelpipeChartWidget : public QWidget {
	Q_OBJECT
private:
	RelpipeTableModel* model;

	QGridLayout* layout;
	QChart* chart;
	QChartView* chartView;
	QVBarModelMapper* mapper;
	QStackedBarSeries* series;
	QBarCategoryAxis* axis;

	boolean_t isNumeric(TypeId typeId) {
		return typeId == TypeId::INTEGER;
	}

	int firstValueColumn() {
		int first = lastValueColumn();
		for (int i = first; i > 0; i--) if (isNumeric(model->attributeType(i))) first = i; else break;
		return first;
	}

	int lastValueColumn() {
		return model->columnCount() - 1;
	}

public:

	RelpipeChartWidget(RelpipeTableModel* model, QWidget* parent = Q_NULLPTR) : model(model), QWidget(parent) {
		layout = new QGridLayout();
		setLayout(layout);
	}

	void endOfRelation() {
		if (hasChartData()) {
			chart = new QChart();
			chartView = new QChartView(chart, this);
			mapper = new QVBarModelMapper(chartView);
			series = new QStackedBarSeries(mapper);
			mapper->setFirstBarSetColumn(firstValueColumn());
			mapper->setLastBarSetColumn(lastValueColumn());
			mapper->setSeries(series);
			mapper->setModel(model);


			axis = new QBarCategoryAxis(chartView);
			for (int i = 0; i < model->rowCount(); i++) axis->append(model->data(model->index(i, 0)).toString());
			chart->addSeries(series);
			chart->createDefaultAxes();
			chart->setAxisX(axis, series);
			layout->addWidget(chartView);
		} else {
			layout->addWidget(new QLabel("This relation can't be displayed as a chart.", this));
		}
	}

	/**
	 * @return whether drawing a chart makes sense for this relation
	 */
	boolean_t hasChartData() {
		return model->columnCount() >= 2 // first = category, last = numeric value
				&& isNumeric(model->attributeType(lastValueColumn())); // at least the last attribute must be numeric
	}
};