src/RelpipeChartMainWindow.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 26 Aug 2022 22:36:12 +0200
branchv_0
changeset 54 259f99938898
parent 49 a3777909fd44
permissions -rw-r--r--
exception: fix typo: getMessge() → getMessage()

/**
 * 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, version 3 of the License.
 *
 * 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/>.
 */
#include <vector>

#include <QPushButton>
#include <QTableView>
#include <QAction>
#include <QHeaderView>

#include "RelpipeChartMainWindow.h"

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

RelpipeChartMainWindow::RelpipeChartMainWindow() {
	widget.setupUi(this);

	int optionsIndex = tabs->addTab(new QPushButton("here will be options", tabs), "Options");
	tabs->setTabIcon(optionsIndex, QIcon::fromTheme("configure"));
	statusBar()->addWidget(status);
	setCentralWidget(tabs);
	setupKeyboardShortcuts();
}

void RelpipeChartMainWindow::setupKeyboardShortcuts() {
	// TODO: less boilerplate code
	QAction* nextTabAction = new QAction(this);
	nextTabAction->setShortcut(Qt::Key_PageDown | Qt::CTRL);
	connect(nextTabAction, &QAction::triggered, this, &RelpipeChartMainWindow::selectNextTab);
	this->addAction(nextTabAction);

	QAction* previousTabAction = new QAction(this);
	previousTabAction->setShortcut(Qt::Key_PageUp | Qt::CTRL);
	connect(previousTabAction, &QAction::triggered, this, &RelpipeChartMainWindow::selectPreviousTab);
	this->addAction(previousTabAction);

	QAction* quitAction = new QAction(this);
	quitAction->setShortcut(Qt::Key_Q | Qt::CTRL);
	connect(quitAction, &QAction::triggered, this, &RelpipeChartMainWindow::close);
	this->addAction(quitAction);
}


RelpipeChartMainWindow::~RelpipeChartMainWindow() {
}

void RelpipeChartMainWindow::selectNextTab() {
	tabs->setCurrentIndex((tabs->currentIndex() + 1) % tabs->count());
}

void RelpipeChartMainWindow::selectPreviousTab() {
	tabs->setCurrentIndex((tabs->count() + tabs->currentIndex() - 1) % tabs->count());
}

void RelpipeChartMainWindow::startRelation(const string_t name, std::vector<AttributeMetadata> attributes) {
	setStatusMessage(L"Reading relation: " + name);

	currentModel = new RelpipeTableModel(attributes, this);
	QTableView* tableView = new QTableView(this);
	tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
	tableView->setModel(currentModel);

	if (currentChartWidget) currentChartWidget->endOfRelation();
	splitter = new QSplitter(Qt::Orientation::Vertical, tabs);
	currentChartWidget = new RelpipeChartWidget(currentModel, splitter);
	splitter->addWidget(currentChartWidget);
	splitter->addWidget(tableView);
	splitter->setSizes({currentChartWidget->hasChartData() ? 1 : 0, 1});
	// splitter->setStretchFactor(currentChartWidget->hasChartData() ? 1 : 0, 1); // FIXME: 50:50 if chart is present
	int index = tabs->addTab(splitter, QString::fromWCharArray(name.c_str()));
	if (tabs->count() == 2) tabs->setCurrentIndex(index); // switch to the first relation (first tab is Options tab)
	tabs->setTabIcon(index, QIcon::fromTheme("application-vnd.oasis.opendocument.spreadsheet"));
}

void RelpipeChartMainWindow::attribute(const string_t value) {
	currentModel->addAttribute(value);
}

void RelpipeChartMainWindow::setStatusMessage(string_t message) {
	status->setText(QString::fromWCharArray(message.c_str()));
}

void RelpipeChartMainWindow::endOfPipe() {
	if (currentChartWidget) currentChartWidget->endOfRelation();
	setStatusMessage(L"Reading successfully finished.");
}