/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero 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 Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include "VerticalLabel.h"
using Qt::WindowFlags;
using drn::desktop_ui::VerticalLabel;
using drn::foundation::ObserverPtr;

#include <QPainter>
using ::QPainter;
#include <QSize>
using ::QSize;

#include <stdexcept>
using std::logic_error;


VerticalLabel::VerticalLabel(const ObserverPtr<QWidget>& parent) :
	QLabel{{}, parent.observed()}
{}

void VerticalLabel::adjustToVertical(QPainter& painter)
{
	if (this->layoutDirection() == Qt::LeftToRight)
	{
		painter.translate(this->sizeHint().width(), 0);
		painter.rotate(90);
	}
	else if (this->layoutDirection() == Qt::RightToLeft)
	{
		painter.translate(0, this->sizeHint().height());
		painter.rotate(270);
	}
	else
	{
		throw logic_error{
			"The Qt::LayoutDirectionAuto text content detection is not implemented."
		};
	}
}

void VerticalLabel::paintEvent(QPaintEvent*)
{
	QPainter painter{this};
	this->adjustToVertical(painter);
	painter.drawText(0, 0, this->height(), this->width(), this->alignment(), this->text());
}

QSize VerticalLabel::minimumSizeHint() const
{
	const QSize s{QLabel::minimumSizeHint()};
	return QSize{s.height(), s.width()};
}

QSize VerticalLabel::sizeHint() const
{
	const QSize s{QLabel::sizeHint()};
	return QSize{s.height(), s.width()};
}
