~aacid/qtubuntu/remove_unused_signal

« back to all changes in this revision

Viewing changes to src/ubuntuappmenu/gmenumodelplatformmenu.cpp

  • Committer: Bileto Bot
  • Date: 2017-01-16 06:39:32 UTC
  • mfrom: (360.2.4 qtubuntu)
  • Revision ID: ci-train-bot@canonical.com-20170116063932-70jenqyqw3jplt3q
Resync trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Lesser General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
// Local
 
18
#include "gmenumodelplatformmenu.h"
 
19
#include "gmenumodelexporter.h"
 
20
#include "registry.h"
 
21
#include "menuregistrar.h"
 
22
#include "logging.h"
 
23
 
 
24
// Qt
 
25
#include <QDebug>
 
26
#include <QWindow>
 
27
#include <QCoreApplication>
 
28
 
 
29
#define BAR_DEBUG_MSG qCDebug(ubuntuappmenu).nospace() << "UbuntuPlatformMenuBar[" << (void*)this <<"]::" << __func__
 
30
#define MENU_DEBUG_MSG qCDebug(ubuntuappmenu).nospace() << "UbuntuPlatformMenu[" << (void*)this <<"]::" << __func__
 
31
#define ITEM_DEBUG_MSG qCDebug(ubuntuappmenu).nospace() << "UbuntuPlatformMenuItem[" << (void*)this <<"]::" << __func__
 
32
 
 
33
namespace {
 
34
 
 
35
int logRecusion = 0;
 
36
 
 
37
}
 
38
 
 
39
QDebug operator<<(QDebug stream, UbuntuPlatformMenuBar* bar) {
 
40
    if (bar) return bar->operator<<(stream);
 
41
    return stream;
 
42
}
 
43
QDebug operator<<(QDebug stream, UbuntuPlatformMenu* menu) {
 
44
    if (menu) return menu->operator<<(stream);
 
45
    return stream;
 
46
}
 
47
QDebug operator<<(QDebug stream, UbuntuPlatformMenuItem* menuItem) {
 
48
    if (menuItem) return menuItem->operator<<(stream);
 
49
    return stream;
 
50
}
 
51
 
 
52
UbuntuPlatformMenuBar::UbuntuPlatformMenuBar()
 
53
    : m_exporter(new UbuntuMenuBarExporter(this))
 
54
    , m_registrar(new UbuntuMenuRegistrar())
 
55
    , m_ready(false)
 
56
{
 
57
    BAR_DEBUG_MSG << "()";
 
58
 
 
59
    connect(this, &UbuntuPlatformMenuBar::menuInserted, this, &UbuntuPlatformMenuBar::structureChanged);
 
60
    connect(this,&UbuntuPlatformMenuBar::menuRemoved, this, &UbuntuPlatformMenuBar::structureChanged);
 
61
}
 
62
 
 
63
UbuntuPlatformMenuBar::~UbuntuPlatformMenuBar()
 
64
{
 
65
    BAR_DEBUG_MSG << "()";
 
66
}
 
67
 
 
68
void UbuntuPlatformMenuBar::insertMenu(QPlatformMenu *menu, QPlatformMenu *before)
 
69
{
 
70
    BAR_DEBUG_MSG << "(menu=" << menu << ", before=" <<  before << ")";
 
71
 
 
72
    if (m_menus.contains(menu)) return;
 
73
 
 
74
    if (!before) {
 
75
        m_menus.push_back(menu);
 
76
    } else {
 
77
        for (auto iter = m_menus.begin(); iter != m_menus.end(); ++iter) {
 
78
            if (*iter == before) {
 
79
                m_menus.insert(iter, menu);
 
80
                break;
 
81
            }
 
82
        }
 
83
    }
 
84
    connect(static_cast<UbuntuPlatformMenu*>(menu), &UbuntuPlatformMenu::structureChanged,
 
85
            this, &UbuntuPlatformMenuBar::structureChanged);
 
86
    Q_EMIT menuInserted(menu);
 
87
}
 
88
 
 
89
void UbuntuPlatformMenuBar::removeMenu(QPlatformMenu *menu)
 
90
{
 
91
    BAR_DEBUG_MSG << "(menu=" << menu << ")";
 
92
 
 
93
    QMutableListIterator<QPlatformMenu*> iterator(m_menus);
 
94
    while(iterator.hasNext()) {
 
95
        if (iterator.next() == menu) {
 
96
            iterator.remove();
 
97
            break;
 
98
        }
 
99
    }
 
100
    disconnect(static_cast<UbuntuPlatformMenu*>(menu), &UbuntuPlatformMenu::structureChanged,
 
101
            this, &UbuntuPlatformMenuBar::structureChanged);
 
102
    Q_EMIT menuRemoved(menu);
 
103
}
 
104
 
 
105
void UbuntuPlatformMenuBar::syncMenu(QPlatformMenu *menu)
 
106
{
 
107
    BAR_DEBUG_MSG << "(menu=" << menu << ")";
 
108
 
 
109
    Q_UNUSED(menu)
 
110
}
 
111
 
 
112
void UbuntuPlatformMenuBar::handleReparent(QWindow *parentWindow)
 
113
{
 
114
    BAR_DEBUG_MSG << "(parentWindow=" << parentWindow << ")";
 
115
 
 
116
    setReady(true);
 
117
    m_registrar->registerMenuForWindow(parentWindow, QDBusObjectPath(m_exporter->menuPath()));
 
118
}
 
119
 
 
120
QPlatformMenu *UbuntuPlatformMenuBar::menuForTag(quintptr tag) const
 
121
{
 
122
    Q_FOREACH(QPlatformMenu* menu, m_menus) {
 
123
        if (menu->tag() == tag) {
 
124
            return menu;
 
125
        }
 
126
    }
 
127
    return nullptr;
 
128
}
 
129
 
 
130
const QList<QPlatformMenu *> UbuntuPlatformMenuBar::menus() const
 
131
{
 
132
    return m_menus;
 
133
}
 
134
 
 
135
QDebug UbuntuPlatformMenuBar::operator<<(QDebug stream)
 
136
{
 
137
    stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
 
138
            << "UbuntuPlatformMenuBar(this=" << (void*)this << ")" << endl;
 
139
    Q_FOREACH(QPlatformMenu* menu, m_menus) {
 
140
        auto myMenu = static_cast<UbuntuPlatformMenu*>(menu);
 
141
        if (myMenu) {
 
142
            logRecusion++;
 
143
            stream << myMenu;
 
144
            logRecusion--;
 
145
        }
 
146
    }
 
147
 
 
148
    return stream;
 
149
}
 
150
 
 
151
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
 
152
QPlatformMenu *UbuntuPlatformMenuBar::createMenu() const
 
153
{
 
154
    return new UbuntuPlatformMenu();
 
155
}
 
156
#endif
 
157
 
 
158
void UbuntuPlatformMenuBar::setReady(bool isReady)
 
159
{
 
160
    if (m_ready != isReady) {
 
161
        m_ready = isReady;
 
162
        Q_EMIT ready();
 
163
    }
 
164
}
 
165
 
 
166
//////////////////////////////////////////////////////////////
 
167
 
 
168
UbuntuPlatformMenu::UbuntuPlatformMenu()
 
169
    : m_parentWindow(nullptr)
 
170
    , m_exporter(nullptr)
 
171
    , m_registrar(nullptr)
 
172
{
 
173
    MENU_DEBUG_MSG << "()";
 
174
 
 
175
    connect(this, &UbuntuPlatformMenu::menuItemInserted, this, &UbuntuPlatformMenu::structureChanged);
 
176
    connect(this, &UbuntuPlatformMenu::menuItemRemoved, this, &UbuntuPlatformMenu::structureChanged);
 
177
}
 
178
 
 
179
UbuntuPlatformMenu::~UbuntuPlatformMenu()
 
180
{
 
181
    MENU_DEBUG_MSG << "()";
 
182
}
 
183
 
 
184
void UbuntuPlatformMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem *before)
 
185
{
 
186
    MENU_DEBUG_MSG << "(menuItem=" << menuItem << ", before=" << before << ")";
 
187
 
 
188
    if (m_menuItems.contains(menuItem)) return;
 
189
 
 
190
    if (!before) {
 
191
        m_menuItems.push_back(menuItem);
 
192
    } else {
 
193
        for (auto iter = m_menuItems.begin(); iter != m_menuItems.end(); ++iter) {
 
194
            if (*iter == before) {
 
195
                m_menuItems.insert(iter, menuItem);
 
196
                break;
 
197
            }
 
198
        }
 
199
    }
 
200
 
 
201
    Q_EMIT menuItemInserted(menuItem);
 
202
}
 
203
 
 
204
void UbuntuPlatformMenu::removeMenuItem(QPlatformMenuItem *menuItem)
 
205
{
 
206
    MENU_DEBUG_MSG << "(menuItem=" << menuItem << ")";
 
207
 
 
208
    QMutableListIterator<QPlatformMenuItem*> iterator(m_menuItems);
 
209
    while(iterator.hasNext()) {
 
210
        if (iterator.next() == menuItem) {
 
211
            iterator.remove();
 
212
            break;
 
213
        }
 
214
    }
 
215
    Q_EMIT menuItemRemoved(menuItem);
 
216
}
 
217
 
 
218
void UbuntuPlatformMenu::syncMenuItem(QPlatformMenuItem *menuItem)
 
219
{
 
220
    MENU_DEBUG_MSG << "(menuItem=" << menuItem << ")";
 
221
 
 
222
    Q_UNUSED(menuItem)
 
223
}
 
224
 
 
225
void UbuntuPlatformMenu::syncSeparatorsCollapsible(bool enable)
 
226
{
 
227
    MENU_DEBUG_MSG << "(enable=" << enable << ")";
 
228
    Q_UNUSED(enable)
 
229
}
 
230
 
 
231
void UbuntuPlatformMenu::setTag(quintptr tag)
 
232
{
 
233
    MENU_DEBUG_MSG << "(tag=" << tag << ")";
 
234
    m_tag = tag;
 
235
}
 
236
 
 
237
quintptr UbuntuPlatformMenu::tag() const
 
238
{
 
239
    return m_tag;
 
240
}
 
241
 
 
242
void UbuntuPlatformMenu::setText(const QString &text)
 
243
{
 
244
    MENU_DEBUG_MSG << "(text=" << text << ")";
 
245
    if (m_text != text) {
 
246
        m_text = text;
 
247
        Q_EMIT propertyChanged();
 
248
    }
 
249
}
 
250
 
 
251
void UbuntuPlatformMenu::setIcon(const QIcon &icon)
 
252
{
 
253
    MENU_DEBUG_MSG << "(icon=" << icon.name() << ")";
 
254
 
 
255
    if (!icon.isNull() || (!m_icon.isNull() && icon.isNull())) {
 
256
        m_icon = icon;
 
257
        Q_EMIT propertyChanged();
 
258
    }
 
259
}
 
260
 
 
261
void UbuntuPlatformMenu::setEnabled(bool enabled)
 
262
{
 
263
    MENU_DEBUG_MSG << "(enabled=" << enabled << ")";
 
264
 
 
265
    if (m_enabled != enabled) {
 
266
        m_enabled = enabled;
 
267
        Q_EMIT propertyChanged();
 
268
    }
 
269
}
 
270
 
 
271
void UbuntuPlatformMenu::setVisible(bool isVisible)
 
272
{
 
273
    MENU_DEBUG_MSG << "(visible=" << isVisible << ")";
 
274
 
 
275
    if (m_visible != isVisible) {
 
276
        m_visible = isVisible;
 
277
        Q_EMIT propertyChanged();
 
278
    }
 
279
}
 
280
 
 
281
void UbuntuPlatformMenu::setMinimumWidth(int width)
 
282
{
 
283
    MENU_DEBUG_MSG << "(width=" << width << ")";
 
284
 
 
285
    Q_UNUSED(width)
 
286
}
 
287
 
 
288
void UbuntuPlatformMenu::setFont(const QFont &font)
 
289
{
 
290
    MENU_DEBUG_MSG << "(font=" << font << ")";
 
291
 
 
292
    Q_UNUSED(font)
 
293
}
 
294
 
 
295
void UbuntuPlatformMenu::showPopup(const QWindow *parentWindow, const QRect &targetRect, const QPlatformMenuItem *item)
 
296
{
 
297
    MENU_DEBUG_MSG << "(parentWindow=" << parentWindow << ", targetRect=" << targetRect << ", item=" << item << ")";
 
298
 
 
299
    if (!m_exporter) {
 
300
        m_exporter.reset(new UbuntuMenuExporter(this));
 
301
        m_exporter->exportModels();
 
302
    }
 
303
 
 
304
    if (parentWindow != m_parentWindow) {
 
305
        if (m_parentWindow) {
 
306
            m_registrar->unregisterMenu();
 
307
        }
 
308
 
 
309
        m_parentWindow = parentWindow;
 
310
 
 
311
        if (m_parentWindow) {
 
312
            if (!m_registrar) m_registrar.reset(new UbuntuMenuRegistrar);
 
313
            m_registrar->registerMenuForWindow(const_cast<QWindow*>(m_parentWindow),
 
314
                                                      QDBusObjectPath(m_exporter->menuPath()));
 
315
        }
 
316
    }
 
317
 
 
318
    Q_UNUSED(targetRect);
 
319
    Q_UNUSED(item);
 
320
    setVisible(true);
 
321
}
 
322
 
 
323
void UbuntuPlatformMenu::dismiss()
 
324
{
 
325
    MENU_DEBUG_MSG << "()";
 
326
 
 
327
    if (m_registrar) { m_registrar->unregisterMenu(); }
 
328
    if (m_exporter) { m_exporter->unexportModels(); }
 
329
}
 
330
 
 
331
QPlatformMenuItem *UbuntuPlatformMenu::menuItemAt(int position) const
 
332
{
 
333
    if (position < 0 || position >= m_menuItems.count()) return nullptr;
 
334
    return m_menuItems.at(position);
 
335
}
 
336
 
 
337
QPlatformMenuItem *UbuntuPlatformMenu::menuItemForTag(quintptr tag) const
 
338
{
 
339
    Q_FOREACH(QPlatformMenuItem* menuItem, m_menuItems) {
 
340
        if (menuItem->tag() == tag) {
 
341
            return menuItem;
 
342
        }
 
343
    }
 
344
    return nullptr;
 
345
}
 
346
 
 
347
QPlatformMenuItem *UbuntuPlatformMenu::createMenuItem() const
 
348
{
 
349
    return new UbuntuPlatformMenuItem();
 
350
}
 
351
 
 
352
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
 
353
QPlatformMenu *UbuntuPlatformMenu::createSubMenu() const
 
354
{
 
355
    return new UbuntuPlatformMenu();
 
356
}
 
357
#endif
 
358
 
 
359
const QList<QPlatformMenuItem *> UbuntuPlatformMenu::menuItems() const
 
360
{
 
361
    return m_menuItems;
 
362
}
 
363
 
 
364
QDebug UbuntuPlatformMenu::operator<<(QDebug stream)
 
365
{
 
366
    stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
 
367
            << "UbuntuPlatformMenu(this=" << (void*)this << ", text=\"" << m_text << "\")" << endl;
 
368
    Q_FOREACH(QPlatformMenuItem* item, m_menuItems) {
 
369
        logRecusion++;
 
370
        auto myItem = static_cast<UbuntuPlatformMenuItem*>(item);
 
371
        if (myItem) {
 
372
            stream << myItem;
 
373
        }
 
374
        logRecusion--;
 
375
    }
 
376
    return stream;
 
377
}
 
378
 
 
379
//////////////////////////////////////////////////////////////
 
380
 
 
381
UbuntuPlatformMenuItem::UbuntuPlatformMenuItem()
 
382
    : m_menu(nullptr)
 
383
{
 
384
    ITEM_DEBUG_MSG << "()";
 
385
}
 
386
 
 
387
UbuntuPlatformMenuItem::~UbuntuPlatformMenuItem()
 
388
{
 
389
    ITEM_DEBUG_MSG << "()";
 
390
}
 
391
 
 
392
void UbuntuPlatformMenuItem::setTag(quintptr tag)
 
393
{
 
394
    ITEM_DEBUG_MSG << "(tag=" << tag << ")";
 
395
    m_tag = tag;
 
396
}
 
397
 
 
398
quintptr UbuntuPlatformMenuItem::tag() const
 
399
{
 
400
    return m_tag;
 
401
}
 
402
 
 
403
void UbuntuPlatformMenuItem::setText(const QString &text)
 
404
{
 
405
    ITEM_DEBUG_MSG << "(text=" << text << ")";
 
406
    if (m_text != text) {
 
407
        m_text = text;
 
408
        Q_EMIT propertyChanged();
 
409
    }
 
410
}
 
411
 
 
412
void UbuntuPlatformMenuItem::setIcon(const QIcon &icon)
 
413
{
 
414
    ITEM_DEBUG_MSG << "(icon=" << icon.name() << ")";
 
415
 
 
416
    if (!icon.isNull() || (!m_icon.isNull() && icon.isNull())) {
 
417
        m_icon = icon;
 
418
        Q_EMIT propertyChanged();
 
419
    }
 
420
}
 
421
 
 
422
void UbuntuPlatformMenuItem::setVisible(bool isVisible)
 
423
{
 
424
    ITEM_DEBUG_MSG << "(visible=" << isVisible << ")";
 
425
    if (m_visible != isVisible) {
 
426
        m_visible = isVisible;
 
427
        Q_EMIT propertyChanged();
 
428
    }
 
429
}
 
430
 
 
431
void UbuntuPlatformMenuItem::setIsSeparator(bool isSeparator)
 
432
{
 
433
    ITEM_DEBUG_MSG << "(separator=" << isSeparator << ")";
 
434
    if (m_separator != isSeparator) {
 
435
        m_separator = isSeparator;
 
436
        Q_EMIT propertyChanged();
 
437
    }
 
438
}
 
439
 
 
440
void UbuntuPlatformMenuItem::setFont(const QFont &font)
 
441
{
 
442
    ITEM_DEBUG_MSG << "(font=" << font << ")";
 
443
    Q_UNUSED(font);
 
444
}
 
445
 
 
446
void UbuntuPlatformMenuItem::setRole(QPlatformMenuItem::MenuRole role)
 
447
{
 
448
    ITEM_DEBUG_MSG << "(role=" << role << ")";
 
449
    Q_UNUSED(role);
 
450
}
 
451
 
 
452
void UbuntuPlatformMenuItem::setCheckable(bool checkable)
 
453
{
 
454
    ITEM_DEBUG_MSG << "(checkable=" << checkable << ")";
 
455
    if (m_checkable != checkable) {
 
456
        m_checkable = checkable;
 
457
        Q_EMIT propertyChanged();
 
458
    }
 
459
}
 
460
 
 
461
void UbuntuPlatformMenuItem::setChecked(bool isChecked)
 
462
{
 
463
    ITEM_DEBUG_MSG << "(checked=" << isChecked << ")";
 
464
    if (m_checked != isChecked) {
 
465
        m_checked = isChecked;
 
466
        Q_EMIT checkedChanged(isChecked);
 
467
        Q_EMIT propertyChanged();
 
468
    }
 
469
}
 
470
 
 
471
void UbuntuPlatformMenuItem::setShortcut(const QKeySequence &shortcut)
 
472
{
 
473
    ITEM_DEBUG_MSG << "(shortcut=" << shortcut << ")";
 
474
    if (m_shortcut != shortcut) {
 
475
        m_shortcut = shortcut;
 
476
        Q_EMIT propertyChanged();
 
477
    }
 
478
}
 
479
 
 
480
void UbuntuPlatformMenuItem::setEnabled(bool enabled)
 
481
{
 
482
    ITEM_DEBUG_MSG << "(enabled=" << enabled << ")";
 
483
    if (m_enabled != enabled) {
 
484
        m_enabled = enabled;
 
485
        Q_EMIT enabledChanged(enabled);
 
486
        Q_EMIT propertyChanged();
 
487
    }
 
488
}
 
489
 
 
490
void UbuntuPlatformMenuItem::setIconSize(int size)
 
491
{
 
492
    ITEM_DEBUG_MSG << "(size=" << size << ")";
 
493
    Q_UNUSED(size);
 
494
}
 
495
 
 
496
void UbuntuPlatformMenuItem::setMenu(QPlatformMenu *menu)
 
497
{
 
498
    ITEM_DEBUG_MSG << "(menu=" << menu << ")";
 
499
    if (m_menu != menu) {
 
500
        m_menu = menu;
 
501
        Q_EMIT propertyChanged();
 
502
    }
 
503
}
 
504
 
 
505
QPlatformMenu *UbuntuPlatformMenuItem::menu() const
 
506
{
 
507
    return m_menu;
 
508
}
 
509
 
 
510
QDebug UbuntuPlatformMenuItem::operator<<(QDebug stream)
 
511
{
 
512
    QString properties = "text=\"" + m_text + "\"";
 
513
 
 
514
    stream.nospace().noquote() << QString("%1").arg("", logRecusion, QLatin1Char('\t'))
 
515
            << "UbuntuPlatformMenuItem(this=" << (void*)this << ", "
 
516
            << (m_separator ? "Separator" : properties) << ")" << endl;
 
517
    if (m_menu) {
 
518
        auto myMenu = static_cast<UbuntuPlatformMenu*>(m_menu);
 
519
        if (myMenu) {
 
520
            logRecusion++;
 
521
            stream << myMenu;
 
522
            logRecusion--;
 
523
        }
 
524
    }
 
525
    return stream;
 
526
}