File: menu_object.cpp

package info (click to toggle)
deepin-menu 5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 332 kB
  • sloc: cpp: 1,720; python: 271; xml: 50; makefile: 7
file content (145 lines) | stat: -rw-r--r-- 4,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2015 ~ 2018 Deepin Technology Co., Ltd.
 *
 * Author:     Hualet Wang <mr.asianwang@gmail.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 <QColor>
#include <QGuiApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QtGlobal>
#include <QStyle>
#include <QDebug>
#include <QScreen>

#include "menu_object.h"
#include "ddesktopmenu.h"
#include "ddockmenu.h"

static DArrowRectangle::ArrowDirection DirectionFromString(QString direction) {
    if (direction == "top") {
        return DArrowRectangle::ArrowTop;
    } else if (direction == "left") {
        return DArrowRectangle::ArrowLeft;
    } else if (direction == "right") {
        return DArrowRectangle::ArrowRight;
    }

    return DArrowRectangle::ArrowBottom;
}

MenuObject::MenuObject():
    QObject(),
    m_dockMenu(nullptr),
    m_desktopMenu(nullptr)
{

}

MenuObject::~MenuObject()
{
    if (!m_dockMenu.isNull()) {
        delete m_dockMenu;
    }

    if (!m_desktopMenu.isNull()) {
        delete m_desktopMenu;
    }
}

void MenuObject::SetItemActivity(const QString &itemId, bool isActive)
{
    if (!m_dockMenu.isNull()) m_dockMenu->setItemActivity(itemId, isActive);
    if (!m_desktopMenu.isNull()) m_desktopMenu->setItemActivity(itemId, isActive);
}

void MenuObject::SetItemChecked(const QString &itemId, bool checked)
{
    if (!m_dockMenu.isNull()) m_dockMenu->setItemChecked(itemId, checked);
    if (!m_desktopMenu.isNull()) m_desktopMenu->setItemChecked(itemId, checked);
}

void MenuObject::SetItemText(const QString &itemId, const QString &text)
{
    if (!m_dockMenu.isNull()) m_dockMenu->setItemText(itemId, text);
    if (!m_desktopMenu.isNull()) m_desktopMenu->setItemText(itemId, text);
}

void MenuObject::ShowMenu(const QString &menuJsonContent)
{
    QByteArray bytes;
    bytes.append(menuJsonContent);
    QJsonDocument menuDocument = QJsonDocument::fromJson(bytes);
    QJsonObject jsonObj = menuDocument.object();

    int x = jsonObj["x"].toDouble();
    int y = jsonObj["y"].toDouble();
    QString direction = jsonObj["direction"].toString();

    if(jsonObj["isDockMenu"].toBool()) {
        m_dockMenu = new DDockMenu;
        connect(m_dockMenu, &DDockMenu::destroyed, this, &MenuObject::menuDismissedSlot, Qt::QueuedConnection);
        connect(m_dockMenu, &DDockMenu::itemClicked, this, &MenuObject::ItemInvoked);
    } else {
        m_desktopMenu = new DDesktopMenu;
        connect(m_desktopMenu, &DDesktopMenu::aboutToHide, this, &MenuObject::menuDismissedSlot, Qt::QueuedConnection);
        connect(m_desktopMenu, &DDesktopMenu::itemClicked, this, &MenuObject::ItemInvoked);
    }

    bytes.clear();
    bytes.append(jsonObj["menuJsonContent"].toString());
    QJsonDocument menuContent = QJsonDocument::fromJson(bytes);
    QJsonObject menuContentObj = menuContent.object();

    if (!m_dockMenu.isNull()) {
        m_dockMenu->setArrowDirection(DirectionFromString(direction));
        m_dockMenu->setItems(menuContentObj["items"].toArray());
        m_dockMenu->show(x, y);
    } else if (!m_desktopMenu.isNull()) {
        m_desktopMenu->setItems(menuContentObj["items"].toArray());

        // 在Qt 5.10.x上, 菜单 show 之前没有被polish, 导致 dstyle 中无法将菜单设置为"圆角+模糊"样式
        if (m_desktopMenu->style() && !m_desktopMenu->testAttribute(Qt::WA_WState_Polished)) {
            m_desktopMenu->style()->polish(m_desktopMenu);
        }

        bool isScaled = true;
        if (!jsonObj["isScaled"].isNull()) {
            isScaled = jsonObj["isScaled"].toBool();
        }

        m_desktopMenu->showMenu(QPoint(x, y), isScaled);
    }
}

void MenuObject::menuDismissedSlot()
{
    emit MenuUnregistered();

    if (!m_dockMenu.isNull()) {
        m_dockMenu->deleteLater();
        m_dockMenu = nullptr;
    }

    if (!m_desktopMenu.isNull()) {
        m_desktopMenu->deleteLater();
        m_desktopMenu = nullptr;
    }

    deleteLater();
}