87 lines
2.1 KiB
C++
87 lines
2.1 KiB
C++
/**
|
|
* @file PaiToolButton.cpp
|
|
* @date 2012-1-31
|
|
*/
|
|
#include <QStyleOptionToolButton>
|
|
#include <QStylePainter>
|
|
#include <QGridLayout>
|
|
#include <QApplication>
|
|
#include <QDesktopWidget>
|
|
|
|
#include "PaiToolButton.h"
|
|
|
|
using namespace pai::gui;
|
|
|
|
PaiToolButton::~PaiToolButton()
|
|
{
|
|
}
|
|
|
|
PaiToolButton::PaiToolButton(QWidget *pParent) :
|
|
QToolButton(pParent)
|
|
{
|
|
m_pPopupWidget = NULL;
|
|
m_pPopupFrame = NULL;
|
|
}
|
|
|
|
PaiToolButton::PaiToolButton(const QIcon & icon, QWidget *pParent) :
|
|
QToolButton(pParent)
|
|
{
|
|
setIcon(icon);
|
|
m_pPopupWidget = NULL;
|
|
m_pPopupFrame = NULL;
|
|
}
|
|
|
|
void PaiToolButton::SetPopupWidget(QWidget *pPopupCenterWidget)
|
|
{
|
|
if((NULL != m_pPopupFrame) || (NULL == pPopupCenterWidget))
|
|
{
|
|
return;
|
|
}
|
|
m_pPopupWidget = pPopupCenterWidget;
|
|
m_pPopupFrame = new QWidget(this, Qt::Popup);
|
|
QGridLayout *pLay = new QGridLayout(m_pPopupFrame);
|
|
pLay->addWidget(pPopupCenterWidget, 0, 0, 1, 1);
|
|
pLay->setContentsMargins(1, 1, 1, 1);
|
|
m_pPopupFrame->resize(pPopupCenterWidget->width(), pPopupCenterWidget->height());
|
|
}
|
|
|
|
void PaiToolButton::mousePressEvent(QMouseEvent *pEvent)
|
|
{
|
|
if(NULL != m_pPopupFrame)
|
|
{
|
|
int popupFrameX = mapToGlobal(QPoint(0, 0)).x() + m_pPopupFrame->width();
|
|
QDesktopWidget *pWidget = QApplication::desktop();
|
|
QRect desktopRect = pWidget->screenGeometry(pWidget->primaryScreen());
|
|
|
|
if(popupFrameX > desktopRect.width())
|
|
{
|
|
m_pPopupFrame->move(mapToGlobal(QPoint((width() - m_pPopupFrame->width()), height())));
|
|
}
|
|
else
|
|
{
|
|
m_pPopupFrame->move(mapToGlobal(QPoint(0, height())));
|
|
}
|
|
|
|
m_pPopupFrame->raise();
|
|
m_pPopupFrame->show();
|
|
}
|
|
QToolButton::mousePressEvent(pEvent);
|
|
}
|
|
void PaiToolButton::paintEvent(QPaintEvent*)
|
|
{
|
|
QStylePainter painter(this);
|
|
QStyleOptionToolButton option;
|
|
initStyleOption(&option);
|
|
if(m_pPopupFrame != NULL)
|
|
{
|
|
option.features |= QStyleOptionToolButton::HasMenu;
|
|
}
|
|
painter.drawComplexControl(QStyle::CC_ToolButton, option);
|
|
}
|
|
|
|
void PaiToolButton::enterEvent(QEvent *pEvent)
|
|
{
|
|
QToolButton::enterEvent(pEvent);
|
|
setCursor(Qt::ArrowCursor);
|
|
}
|