#include "edockwidget.h"
#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::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();
	lbl_icon->setPixmap(windowIcon().pixmap(QSize(24, 24)));
	lbl_icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	lbl_title = new QLabel(windowTitle());
	lbl_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
	btn_dock = new QToolButton();
	//btn_dock->setIconSize(QSize(16, 16));
	int btn_wh = style()->pixelMetric(QStyle::PM_SmallIconSize);
	QSize icon_size(btn_wh, btn_wh);
	btn_wh += 2 * style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin, 0, this);
	QSize btn_size(btn_wh, btn_wh);
	btn_dock->setIcon(style()->standardIcon(QStyle::SP_TitleBarNormalButton));
	btn_dock->setIconSize(icon_size);
	btn_dock->setFixedSize(btn_size);
	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->setIcon(style()->standardIcon(QStyle::SP_DockWidgetCloseButton));
	btn_hide->setIconSize(icon_size);
	btn_hide->setFixedSize(btn_size);
	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);
	setTitleBarWidget(header);
}
