#include "minibutton.h"
#include <QPainter>
#include "config.h"
#include "globalvariables.h"

MiniButton::MiniButton(QString orig_name, QString op, QWidget* parent)
{
	this->orig_name = orig_name;
	q_color.setNamedColor(DEFAULT_BACKGROUND_COLOR);

	this->op = op;
	connect(this, &MiniButton::click, this, &MiniButton::onclick2);
}
MiniButton::MiniButton(ButtonStruct button_struct, QWidget *parent)
	: QPushButton(parent)
{
	ui.setupUi(this);
	q_color.setNamedColor(DEFAULT_COVER_COLOR);
	QString style_sheet = "MiniButton:{background-color:";
	style_sheet += DEFAULT_TEXT_COLOR;
	style_sheet += ";}";
	setStyleSheet(style_sheet);
	connect(this, &MiniButton::click, this, &MiniButton::onclick1);
	this->button_struct = button_struct;
}

MiniButton::~MiniButton()
{}

void MiniButton::paintEvent(QPaintEvent* event)
{
	QPushButton::paintEvent(event);
	setContentsMargins(0, 0, 0, 0);
	QRect rect(0, 0, this->width(), this->height());
	QPainter painter(this);
	
	painter.fillRect(rect, q_color);
	int text_x = (width() - TEXT_SIZE * text().length() * scale * 3) / 2;
	int text_y = (height()- TEXT_SIZE * scale * 3)/2;
	QFont ft;
	ft.setPixelSize(TEXT_SIZE);
	QRectF text_rect(text_x, text_y, TEXT_SIZE * text().length() * scale * 3, TEXT_SIZE * scale * 3);
	painter.setPen(DEFAULT_TEXT_COLOR);
	painter.drawText(text_rect, Qt::AlignCenter, text());
	
}

void MiniButton::mouseReleaseEvent(QMouseEvent* event)
{
	emit click0();
}

void MiniButton::onclick1()
{
	emit click1(button_struct.op, button_struct.func, button_struct.path, button_struct.url);
}

void MiniButton::onclick2()
{
	emit click2(this->orig_name,this->op);
}

