#include "customlefttabbarbutton.h"

CustomLeftTabBarButton::CustomLeftTabBarButton(QWidget* parent) : QPushButton(parent)
{
	QIcon i = this->icon();
    QPixmap p = i.pixmap(5, 5);
    m_pixmap = this->icon().pixmap(this->icon().actualSize(QSize(5, 5)));
}

CustomLeftTabBarButton::~CustomLeftTabBarButton()
{
}

QSize CustomLeftTabBarButton::sizeHint() const
{
	const auto parentHint = QPushButton::sizeHint();
	// add margins here if needed
    return QSize
       (
        parentHint.width() + m_pixmap.width() ,
        std::max(parentHint.height(), m_pixmap.height())
        );
}

void CustomLeftTabBarButton::setPixmap(const QPixmap& pixmap)
{
	m_pixmap = pixmap;
}

void CustomLeftTabBarButton::setPos(const QSize& pos)
{
	m_pixmapPos = pos;
}

void CustomLeftTabBarButton::setIconStringForButton(QString iconString)
{
    QIcon i = QIcon(iconString); //":/res/lock-icon-closed.png");
    int sz = 20;
    QSize size (sz , sz);
            //= iconSize();
    i.actualSize(size);
	QPixmap p = i.pixmap(sz);
    m_pixmap = p;
	this->setPixmap(p);
}


void CustomLeftTabBarButton::paintEvent(QPaintEvent* e)
{
	// Draw the button first
	QPushButton::paintEvent(e);

	if (!m_pixmap.isNull())
	{
        bool isTextEmpty = (text()=="");

		{
            //const int y = (height() - m_pixmap.height()) / 2; // add margin if needed
            int h_btn_half = height() / 2;
            int h_icon_half = m_pixmap.height() / 2;
            int w_btn_half = width() / 2;
            int w_icon_half = m_pixmap.width() / 2;
            int w_btn_tenth = width() / 10;

			QPainter painter(this);
            painter.drawPixmap
            (
                isTextEmpty
                ? w_btn_half - w_icon_half
                : 40
            , h_btn_half - h_icon_half
            , m_pixmap
            ); // hardcoded horizontal margin
        }
	}
}
