Skip to content

Commit

Permalink
feat: add DWindowGroupLeader
Browse files Browse the repository at this point in the history
Change-Id: I9bf91ada334038107123655b36576132e18eb33f
  • Loading branch information
zccrs committed Apr 9, 2018
1 parent 5fecfc0 commit d6d9f4f
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/widgets/DWindowGroupLeader
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dwindowgroupleader.h"
157 changes: 157 additions & 0 deletions src/widgets/dwindowgroupleader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: zccrs <zccrs@live.com>
*
* Maintainer: zccrs <zhangjide@deepin.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dwindowgroupleader.h"

#include <QWidget>
#include <QWindow>
#include <QGuiApplication>
#include <QPointer>

DWIDGET_BEGIN_NAMESPACE

#define DEFINE_CONST_CHAR(Name) const char _##Name[] = "_d_" #Name

DEFINE_CONST_CHAR(groupLeader);
DEFINE_CONST_CHAR(createGroupWindow);
DEFINE_CONST_CHAR(destoryGroupWindow);
DEFINE_CONST_CHAR(setWindowGroup);
DEFINE_CONST_CHAR(clientLeader);

class DWindowGroupLeaderPrivate
{
public:
DWindowGroupLeaderPrivate(quint32 groupLeader)
: groupLeader(groupLeader) {
QFunctionPointer clientLeaderFun = qApp->platformFunction(_clientLeader);

if (clientLeaderFun) {
clientLeader = reinterpret_cast<quint32(*)()>(clientLeaderFun)();
}
}

quint32 groupLeader;
quint32 clientLeader = 0;
bool groupLeaderFromUser = false;

QList<QPointer<QWindow>> windowList;

void ensureGroupLeader();
bool setWindowGroupLeader(quint32 window, quint32 groupLeader);
};

void DWindowGroupLeaderPrivate::ensureGroupLeader()
{
if (groupLeader != 0)
return;

QFunctionPointer createGroupWindow = qApp->platformFunction(_createGroupWindow);

if (!createGroupWindow)
return;

groupLeader = reinterpret_cast<quint32(*)()>(createGroupWindow)();
}

bool DWindowGroupLeaderPrivate::setWindowGroupLeader(quint32 window, quint32 groupLeader)
{
QFunctionPointer setWindowGroup = qApp->platformFunction(_setWindowGroup);

if (!setWindowGroup) {
return false;
}

reinterpret_cast<void(*)(quint32, quint32)>(setWindowGroup)(window, groupLeader);

return true;
}

DWindowGroupLeader::DWindowGroupLeader(quint32 groupId)
: d_ptr(new DWindowGroupLeaderPrivate(groupId))
{
if (groupId != 0)
d_ptr->groupLeaderFromUser = true;
}

DWindowGroupLeader::~DWindowGroupLeader()
{
Q_D(DWindowGroupLeader);

for (auto window : d->windowList)
removeWindow(window);

if (!d->groupLeaderFromUser) {
QFunctionPointer destoryGroupWindow = qApp->platformFunction(_destoryGroupWindow);

if (!destoryGroupWindow)
return;

reinterpret_cast<void(*)(quint32)>(destoryGroupWindow)(d->groupLeader);
}
}

void DWindowGroupLeader::addWindow(QWindow *window)
{
Q_ASSERT(window);
Q_D(DWindowGroupLeader);

d->ensureGroupLeader();

window->setProperty(_groupLeader, d->groupLeader);

if (window->handle()) {
d->setWindowGroupLeader(window->winId(), d->groupLeader);
}

d->windowList << window;
}

void DWindowGroupLeader::removeWindow(QWindow *window)
{
if (!window)
return;

window->setProperty(_groupLeader, QVariant());

Q_D(DWindowGroupLeader);

if (window->handle()) {
d->setWindowGroupLeader(window->winId(), d->clientLeader);
}
}

void DWindowGroupLeader::addWindow(QWidget *window)
{
Q_ASSERT(window);
Q_ASSERT(window->isWindow());

if (!window->windowHandle()) {
window->setAttribute(Qt::WA_NativeWindow);
}

return addWindow(window->windowHandle());
}

void DWindowGroupLeader::removeWindow(QWidget *window)
{
return removeWindow(window->windowHandle());
}

DWIDGET_END_NAMESPACE
52 changes: 52 additions & 0 deletions src/widgets/dwindowgroupleader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: zccrs <zccrs@live.com>
*
* Maintainer: zccrs <zhangjide@deepin.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DWINDOWGROUPLEADER_H
#define DWINDOWGROUPLEADER_H

#include <dtkwidget_global.h>

QT_BEGIN_NAMESPACE
class QWindow;
QT_END_NAMESPACE

DWIDGET_BEGIN_NAMESPACE

class DWindowGroupLeaderPrivate;
class DWindowGroupLeader
{
public:
explicit DWindowGroupLeader(quint32 groupId = 0);
~DWindowGroupLeader();

void addWindow(QWindow *window);
void removeWindow(QWindow *window);
void addWindow(QWidget *window);
void removeWindow(QWidget *window);

private:
QScopedPointer<DWindowGroupLeaderPrivate> d_ptr;

Q_DECLARE_PRIVATE(DWindowGroupLeader)
};

DWIDGET_END_NAMESPACE

#endif // DWINDOWGROUPLEADER_H
9 changes: 6 additions & 3 deletions src/widgets/widgets.pri
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ HEADERS += $$PWD/dslider.h\
$$PWD/dsuggestbutton.h \
$$PWD/dstyleoption.h \
$$PWD/dtoast.h \
$$PWD/danchors.h
$$PWD/danchors.h \
$$PWD/dwindowgroupleader.h

SOURCES += $$PWD/dslider.cpp \
$$PWD/dthememanager.cpp \
Expand Down Expand Up @@ -203,7 +204,8 @@ SOURCES += $$PWD/dslider.cpp \
$$PWD/dsuggestbutton.cpp \
$$PWD/dstyleoption.cpp \
$$PWD/dtoast.cpp \
$$PWD/danchors.cpp
$$PWD/danchors.cpp \
$$PWD/dwindowgroupleader.cpp

RESOURCES += \
$$PWD/themes/dui_theme_dark.qrc \
Expand Down Expand Up @@ -244,6 +246,7 @@ includes.files += \
$$PWD/DToast \
$$PWD/DFileDialog \
$$PWD/DLineEdit \
$$PWD/DStyleOptionLineEdit
$$PWD/DStyleOptionLineEdit \
$$PWD/DWindowGroupLeader


0 comments on commit d6d9f4f

Please sign in to comment.