#include "edockwidget.h"
#include "qad_types.h"
#include <QEvent>
#include <QStyle>


void EDockWidget::setFeatures(QDockWidget::DockWidgetFeatures features) {
	btn_dock->setVisible(features.testFlag(DockWidgetFloatable));
	btn_hide->setVisible(features.testFlag(DockWidgetClosable));
	QDockWidget::setFeatures(features);
}


void EDockWidget::setWindowTitle(const QString & title) {
	lbl_title->setText(title);
	QDockWidget::setWindowTitle(title);
}


void EDockWidget::setWindowIcon(const QIcon & icon) {
//#ifndef Q_OS_MACOS
	lbl_icon->setPixmap(icon.pixmap(QSize(256, 256)));
	QDockWidget::setWindowIcon(icon);
	if (!icon.isNull()) {
		lbl_icon->setScaledContents(true);
		lbl_icon->setFixedSize(preferredIconSize(1.5, this));
	}
	//#endif
}


bool EDockWidget::event(QEvent * e) {
	if (e->type() == QEvent::FontChange || e->type() == QEvent::Polish) {
		updateStyle();
	}
	return QDockWidget::event(e);
}


void EDockWidget::init() {
	header = new QFrame();
	header->setFrameShape(QFrame::StyledPanel);
	QBoxLayout * lay = new QBoxLayout(features().testFlag(QDockWidget::DockWidgetVerticalTitleBar) ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight);
	lay->setContentsMargins(2, 2, 2, 2);
	lay->setSpacing(2);
	lbl_icon = new QLabel();
//#ifndef Q_OS_MACOS
	QIcon wi = windowIcon();
	if (!wi.isNull()) {
		lbl_icon->setPixmap(wi.pixmap(QSize(256,256)));
/*#if QT_VERSION >= 0x500000
		if (lbl_icon->pixmap())
			const_cast<QPixmap*>(lbl_icon->pixmap())->setDevicePixelRatio(1.);
#endif*/
		//qDebug() << windowTitle() << wi.pixmap(QSize(256,256)).size();
		lbl_icon->setScaledContents(true);
	}
	lbl_icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
//#endif
	lbl_title = new QLabel(windowTitle());
	lbl_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
	btn_dock = new QToolButton();
	//btn_dock->setIconSize(QSize(16, 16));
	btn_dock->setAutoRaise(true);
	btn_dock->setFocusPolicy(Qt::NoFocus);
	btn_dock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	btn_hide = new QToolButton();
	//btn_hide->setIconSize(QSize(16, 16));
	btn_hide->setAutoRaise(true);
	btn_hide->setFocusPolicy(Qt::NoFocus);
	btn_hide->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	connect(btn_dock, SIGNAL(clicked(bool)), this, SLOT(dockClicked()));
	connect(btn_hide, SIGNAL(clicked(bool)), this, SLOT(hide()));
	lay->addWidget(lbl_icon);
	lay->addWidget(lbl_title);
	lay->addWidget(btn_dock);
	lay->addWidget(btn_hide);
	header->setLayout(lay);
	updateStyle();
	setTitleBarWidget(header);
}


void EDockWidget::updateStyle() {
	QSize icon_size = preferredIconSize(0.75, this);
	int bm = 2 * style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin, 0, this);
	QSize btn_size = icon_size;
	btn_size += QSize(bm, bm);
	btn_dock->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));
	btn_dock->setIconSize(icon_size);
	btn_dock->setFixedSize(btn_size);
	btn_hide->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
	btn_hide->setIconSize(icon_size);
	btn_hide->setFixedSize(btn_size);
	lbl_icon->setFixedSize(preferredIconSize(1.5, this));
}
