#include "TitleBar.h"

#include <QMenu>
#include <QHBoxLayout>
#include <QEvent>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

TitleBar::TitleBar(QWidget *parent, bool showUserInfo)
	: QFrame(parent)
	, m_pIconLabel(new QLabel(this))
	, m_pTitleLabel(new QLabel(this))	
	, m_pUserButton(nullptr)
	, m_pWindowButtonGroup(new WindowButtonGroup(parent))
{	
	if (parent) {
		parent->installEventFilter(this);
	}

	setFixedHeight(28);

	//初始化图标Label
	m_pIconLabel->setFixedSize(18, 18);
	m_pIconLabel->setScaledContents(true);
	m_pIconLabel->hide();

	m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

	if (showUserInfo)
	{
		m_pUserButton = new QToolButton(this);

		QMenu *menu = new QMenu(this);
        menu->addAction(QIcon(":/MainWindow/Resources/changePassword.png"), "修改密码", parent, SLOT(onChangePassword()));
		menu->addAction(QIcon(":/MainWindow/Resources/logout.png"), "退出登录", parent, SLOT(onLogout()));
		menu->setStyleSheet("\
			QMenu {background-color:#272727;border: 1px solid #333333;} \
			QMenu::item { color: rgb(225,225,225); background-color:#1E1E1E; padding:7px 20px; min-width:110px;} \
			QMenu::item:selected {background-color:#494949;} \
			QMenu::item:pressed {border: 1px solid rgb(60,60,61);}");

		QIcon iconUser(":/MainWindow/Resources/user.png");
		m_pUserButton->setIcon(iconUser);
		m_pUserButton->setText("admin");
		m_pUserButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
		m_pUserButton->setPopupMode(QToolButton::InstantPopup);
		m_pUserButton->setMenu(menu);
		m_pUserButton->setStyleSheet("\
			QToolButton{color:#cccccc; background-color:transparent; border-radius:2px;  border:1px solid transparent} \
			QToolButton:hover{color:#ffffff; border-color:#ffffff; border:1px solid #999999} \
			QToolButton::menu-indicator { image:none }");
	}


	//标题栏布局 
	QHBoxLayout *pLayout = new QHBoxLayout(this);
	pLayout->setAlignment(Qt::AlignVCenter);
	pLayout->setSpacing(0);
	pLayout->setMargin(0);
	pLayout->setContentsMargins(5, 0, 0, 0);
	setLayout(pLayout);

	pLayout->addWidget(m_pIconLabel);
	pLayout->addSpacing(5);
	pLayout->addWidget(m_pTitleLabel);
	if (m_pUserButton)
		pLayout->addWidget(m_pUserButton);
	pLayout->addSpacing(5);
	pLayout->addWidget(m_pWindowButtonGroup);
}

TitleBar::~TitleBar()
{
}

bool TitleBar::eventFilter(QObject *obj, QEvent *event)
{
	switch (event->type()) //判断发生事件的类型
	{
	case QEvent::WindowTitleChange: //窗口标题改变事件
	{
		QWidget *pWidget = qobject_cast<QWidget *>(obj); //获得发生事件的窗口对象
		if (pWidget)
		{
			//窗体标题改变，则标题栏标题也随之改变
			m_pTitleLabel->setText(pWidget->windowTitle());
			return true;
		}
	}
	break;

	case QEvent::WindowIconChange: //窗口图标改变事件
	{
		QWidget *pWidget = qobject_cast<QWidget *>(obj);
		if (pWidget)
		{
			//窗体图标改变，则标题栏图标也随之改变
			QIcon icon = pWidget->windowIcon();
			m_pIconLabel->setPixmap(icon.pixmap(m_pIconLabel->size()));
			m_pIconLabel->show();
			return true;
		}
	}
	break;

	default:
		return QWidget::eventFilter(obj, event);
	}

	return QWidget::eventFilter(obj, event);
}
