2016-07-15 11:00:55 +08:00
|
|
|
#include "dockpopupwindow.h"
|
|
|
|
|
2016-08-30 15:11:09 +08:00
|
|
|
#include <QScreen>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
DWIDGET_USE_NAMESPACE
|
|
|
|
|
2016-09-02 09:23:48 +08:00
|
|
|
const int MOUSE_BUTTON(1 << 1);
|
2016-08-30 15:11:09 +08:00
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
DockPopupWindow::DockPopupWindow(QWidget *parent)
|
|
|
|
: DArrowRectangle(ArrowBottom, parent),
|
2016-07-18 10:09:26 +08:00
|
|
|
m_model(false),
|
|
|
|
|
|
|
|
m_acceptDelayTimer(new QTimer(this)),
|
|
|
|
|
2016-08-30 15:11:09 +08:00
|
|
|
m_mouseInter(new DBusXMouseArea(this)),
|
|
|
|
m_displayInter(new DBusDisplay(this))
|
2016-07-15 11:00:55 +08:00
|
|
|
{
|
2016-07-18 10:09:26 +08:00
|
|
|
m_acceptDelayTimer->setSingleShot(true);
|
|
|
|
m_acceptDelayTimer->setInterval(100);
|
2016-07-15 11:00:55 +08:00
|
|
|
|
2016-08-29 16:43:49 +08:00
|
|
|
setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
|
2016-09-01 16:24:25 +08:00
|
|
|
setAttribute(Qt::WA_InputMethodEnabled, false);
|
2016-09-02 09:03:25 +08:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2016-08-11 15:32:48 +08:00
|
|
|
|
2016-07-18 10:09:26 +08:00
|
|
|
connect(m_acceptDelayTimer, &QTimer::timeout, this, &DockPopupWindow::accept);
|
2016-07-15 11:00:55 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
DockPopupWindow::~DockPopupWindow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
bool DockPopupWindow::model() const
|
|
|
|
{
|
|
|
|
return m_model;
|
|
|
|
}
|
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
void DockPopupWindow::setContent(QWidget *content)
|
|
|
|
{
|
|
|
|
QWidget *lastWidget = getContent();
|
|
|
|
if (lastWidget)
|
|
|
|
lastWidget->removeEventFilter(this);
|
|
|
|
content->installEventFilter(this);
|
|
|
|
|
2016-07-28 10:59:21 +08:00
|
|
|
setAccessibleName(content->objectName() + "-popup");
|
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
DArrowRectangle::setContent(content);
|
|
|
|
}
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
void DockPopupWindow::show(const QPoint &pos, const bool model)
|
|
|
|
{
|
|
|
|
m_model = model;
|
2016-07-27 14:08:23 +08:00
|
|
|
m_lastPoint = pos;
|
2016-07-15 11:00:55 +08:00
|
|
|
|
|
|
|
DArrowRectangle::show(pos.x(), pos.y());
|
2016-07-18 10:09:26 +08:00
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
if (!model && !m_mouseAreaKey.isEmpty())
|
2016-08-26 17:38:30 +08:00
|
|
|
unRegisterMouseEvent();
|
2016-07-19 15:08:16 +08:00
|
|
|
|
|
|
|
if (model && m_mouseAreaKey.isEmpty())
|
2016-08-26 17:38:30 +08:00
|
|
|
registerMouseEvent();
|
2016-07-18 10:09:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
void DockPopupWindow::hide()
|
|
|
|
{
|
|
|
|
if (!m_mouseAreaKey.isEmpty())
|
2016-08-26 17:38:30 +08:00
|
|
|
unRegisterMouseEvent();
|
2016-07-19 15:08:16 +08:00
|
|
|
|
|
|
|
DArrowRectangle::hide();
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:33:57 +08:00
|
|
|
void DockPopupWindow::showEvent(QShowEvent *e)
|
|
|
|
{
|
|
|
|
DArrowRectangle::showEvent(e);
|
|
|
|
|
|
|
|
QTimer::singleShot(1, this, [&] {
|
|
|
|
raise();
|
2016-09-07 09:50:42 +08:00
|
|
|
if (!m_model)
|
|
|
|
return;
|
2016-09-05 14:33:57 +08:00
|
|
|
activateWindow();
|
|
|
|
setFocus(Qt::ActiveWindowFocusReason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-01 10:55:19 +08:00
|
|
|
void DockPopupWindow::enterEvent(QEvent *e)
|
|
|
|
{
|
|
|
|
DArrowRectangle::enterEvent(e);
|
|
|
|
|
|
|
|
raise();
|
2016-09-07 09:50:42 +08:00
|
|
|
if (!m_model)
|
|
|
|
return;
|
2016-09-05 14:33:57 +08:00
|
|
|
activateWindow();
|
2016-09-01 10:55:19 +08:00
|
|
|
setFocus(Qt::ActiveWindowFocusReason);
|
|
|
|
}
|
|
|
|
|
2016-07-18 10:09:26 +08:00
|
|
|
void DockPopupWindow::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
DArrowRectangle::mousePressEvent(e);
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
// if (e->button() == Qt::LeftButton)
|
2016-08-01 14:26:15 +08:00
|
|
|
// m_acceptDelayTimer->start();
|
2016-07-27 14:08:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DockPopupWindow::eventFilter(QObject *o, QEvent *e)
|
|
|
|
{
|
|
|
|
if (o != getContent() || e->type() != QEvent::Resize)
|
|
|
|
return false;
|
|
|
|
|
2016-07-27 14:49:29 +08:00
|
|
|
// FIXME: ensure position move after global mouse release event
|
2016-07-28 09:49:43 +08:00
|
|
|
QTimer::singleShot(100, this, [this] {if (isVisible()) show(m_lastPoint, m_model);});
|
2016-07-27 14:49:29 +08:00
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
return false;
|
2016-07-18 10:09:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-20 16:30:56 +08:00
|
|
|
void DockPopupWindow::globalMouseRelease(int button, int x, int y, const QString &id)
|
2016-07-18 10:09:26 +08:00
|
|
|
{
|
2016-07-20 16:30:56 +08:00
|
|
|
Q_UNUSED(button);
|
|
|
|
|
|
|
|
if (id != m_mouseAreaKey)
|
|
|
|
return;
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
Q_ASSERT(m_model);
|
2016-07-18 10:09:26 +08:00
|
|
|
|
|
|
|
const QRect rect = QRect(pos(), size());
|
2016-07-20 16:30:56 +08:00
|
|
|
const QPoint pos = QPoint(x, y);
|
2016-07-18 10:09:26 +08:00
|
|
|
|
|
|
|
if (rect.contains(pos))
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit accept();
|
|
|
|
|
2016-08-26 17:38:30 +08:00
|
|
|
unRegisterMouseEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DockPopupWindow::registerMouseEvent()
|
|
|
|
{
|
2016-08-30 15:11:09 +08:00
|
|
|
// only regist mouse button event
|
|
|
|
m_mouseAreaKey = m_mouseInter->RegisterArea(0, 0, m_displayInter->screenWidth(), m_displayInter->screenHeight(), MOUSE_BUTTON);
|
|
|
|
// m_mouseAreaKey = m_mouseInter->RegisterFullScreen();
|
|
|
|
|
2016-08-26 17:38:30 +08:00
|
|
|
connect(m_mouseInter, &DBusXMouseArea::ButtonRelease, this, &DockPopupWindow::globalMouseRelease, Qt::UniqueConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DockPopupWindow::unRegisterMouseEvent()
|
|
|
|
{
|
|
|
|
if (m_mouseAreaKey.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
disconnect(m_mouseInter, &DBusXMouseArea::ButtonRelease, this, &DockPopupWindow::globalMouseRelease);
|
|
|
|
|
2016-09-02 09:23:48 +08:00
|
|
|
m_mouseInter->UnregisterArea(m_mouseAreaKey).waitForFinished();
|
2016-07-19 15:08:16 +08:00
|
|
|
m_mouseAreaKey.clear();
|
2016-07-15 11:00:55 +08:00
|
|
|
}
|