dde-dock/frame/window/mainwindow.cpp
石博文 98f0c75943 add display mode changed signal
Change-Id: I5edc6fc2220b58d2ee0cb6c13f867bea43860084
2016-08-02 09:28:06 +08:00

158 lines
3.9 KiB
C++

#include "mainwindow.h"
#include "panel/mainpanel.h"
#include <QDebug>
#include <QResizeEvent>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent),
m_mainPanel(new MainPanel(this)),
m_settings(new DockSettings(this)),
m_xcbMisc(XcbMisc::instance()),
m_positionUpdateTimer(new QTimer(this))
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
setAttribute(Qt::WA_TranslucentBackground);
m_xcbMisc->set_window_type(winId(), XcbMisc::Dock);
initComponents();
initConnections();
}
MainWindow::~MainWindow()
{
delete m_xcbMisc;
}
void MainWindow::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
m_mainPanel->setFixedSize(e->size());
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
e->ignore();
if (e->button() == Qt::RightButton)
m_settings->showDockSettingsMenu();
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
#ifdef QT_DEBUG
case Qt::Key_Escape: qApp->quit(); break;
#endif
default:;
}
}
void MainWindow::initComponents()
{
m_positionUpdateTimer->setSingleShot(true);
m_positionUpdateTimer->setInterval(200);
m_positionUpdateTimer->start();
}
void MainWindow::initConnections()
{
connect(m_settings, &DockSettings::dataChanged, m_positionUpdateTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
connect(m_settings, &DockSettings::windowGeometryChanged, this, &MainWindow::updateGeometry);
connect(m_positionUpdateTimer, &QTimer::timeout, this, &MainWindow::updatePosition);
}
void MainWindow::updatePosition()
{
// all update operation need pass by timer
Q_ASSERT(sender() == m_positionUpdateTimer);
clearStrutPartial();
updateGeometry();
setStrutPartial();
}
void MainWindow::updateGeometry()
{
setFixedSize(m_settings->windowSize());
m_mainPanel->updateDockPosition(m_settings->position());
m_mainPanel->updateDockDisplayMode(m_settings->displayMode());
const QRect primaryRect = m_settings->primaryRect();
const int offsetX = (primaryRect.width() - width()) / 2;
const int offsetY = (primaryRect.height() - height()) / 2;
switch (m_settings->position())
{
case Top:
move(primaryRect.topLeft().x() + offsetX, 0); break;
case Left:
move(primaryRect.topLeft().x(), offsetY); break;
case Right:
move(primaryRect.right() - width(), offsetY); break;
case Bottom:
move(offsetX, primaryRect.bottom() - height() + 1); break;
default:
Q_ASSERT(false);
}
}
void MainWindow::clearStrutPartial()
{
m_xcbMisc->clear_strut_partial(winId());
}
void MainWindow::setStrutPartial()
{
// first, clear old strut partial
clearStrutPartial();
const Position side = m_settings->position();
const int maxScreenHeight = m_settings->screenHeight();
XcbMisc::Orientation orientation;
uint strut;
uint strutStart;
uint strutEnd;
const QPoint p = pos();
const QRect r = rect();
switch (side)
{
case Position::Top:
orientation = XcbMisc::OrientationTop;
strut = r.bottom() + 1;
strutStart = r.left();
strutEnd = r.right();
break;
case Position::Bottom:
orientation = XcbMisc::OrientationBottom;
strut = maxScreenHeight - p.y();
strutStart = r.left();
strutEnd = r.right();
break;
case Position::Left:
orientation = XcbMisc::OrientationLeft;
strut = r.width();
strutStart = r.top();
strutEnd = r.bottom();
break;
case Position::Right:
orientation = XcbMisc::OrientationRight;
strut = r.width();
strutStart = r.top();
strutEnd = r.bottom();
break;
default:
Q_ASSERT(false);
}
m_xcbMisc->set_strut_partial(winId(), orientation, strut, strutStart, strutEnd);
}