Debian Codegraph @

src/imports-compat/Metrics/plugin.cpp

raw
1 // Copyright © 2016 Canonical Ltd.
2 //
3 // This file is part of Lomiri UI Toolkit.
4 //
5 // Lomiri UI Toolkit is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by the
7 // Free Software Foundation; version 3.
8 //
9 // Lomiri UI Toolkit is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 // for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>.
16
17 #include <QtQml/QtQml>
18 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
19 #include <LomiriMetrics-Qt6/applicationmonitor.h>
20 #else
21 #include <LomiriMetrics/applicationmonitor.h>
22 #endif
23
24 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
25 #include <LomiriToolkit-Qt6/private/deprecationprinter_p.h>
26 #else
27 #include <LomiriToolkit/private/deprecationprinter_p.h>
28 #endif
29
30 // FIXME(loicm)
31 // - Not sure how to add support for the loggers API?
32 // - Add support for the generic logging API.
33
34 class ApplicationMonitorWrapper : public QObject
35 {
36 Q_OBJECT
37
38 Q_FLAGS(LoggingFilters);
39 Q_ENUMS(Event);
40 Q_PROPERTY(bool overlay READ overlay WRITE setOverlay NOTIFY overlayChanged)
41 Q_PROPERTY(bool logging READ logging WRITE setLogging NOTIFY loggingChanged)
42 Q_PROPERTY(LoggingFilters loggingFilter READ loggingFilter WRITE setLoggingFilter
43 NOTIFY loggingFilterChanged)
44 Q_PROPERTY(int processUpdateInterval READ processUpdateInterval
45 WRITE setProcessUpdateInterval NOTIFY processUpdateIntervalChanged)
46
47 public:
48 ApplicationMonitorWrapper(QObject* parent = 0)
49 : QObject(parent)
50 , m_applicationMonitor(UMApplicationMonitor::instance())
51 {
52 QObject::connect(m_applicationMonitor, SIGNAL(overlayChanged()),
53 this, SIGNAL(overlayChanged()));
54 QObject::connect(m_applicationMonitor, SIGNAL(loggingChanged()),
55 this, SIGNAL(loggingChanged()));
56 QObject::connect(m_applicationMonitor, SIGNAL(loggingFilterChanged()),
57 this, SIGNAL(loggingFilterChanged()));
58 QObject::connect(m_applicationMonitor, SIGNAL(updateIntervalChanged(UMEvent::Type)),
59 this, SLOT(updateIntervalChanged(UMEvent::Type)));
60 }
61 ~ApplicationMonitorWrapper() {}
62
63 enum LoggingFilter {
64 ProcessEvent = UMApplicationMonitor::ProcessEvent,
65 WindowEvent = UMApplicationMonitor::WindowEvent,
66 FrameEvent = UMApplicationMonitor::FrameEvent,
67 GenericEvent = UMApplicationMonitor::GenericEvent,
68 AllEvents = UMApplicationMonitor::AllEvents
69 };
70 Q_DECLARE_FLAGS(LoggingFilters, LoggingFilter)
71
72 enum Event {
73 UserInterfaceReady = UMApplicationMonitor::UserInterfaceReady
74 };
75
76 bool overlay() const { return m_applicationMonitor->overlay(); }
77 void setOverlay(bool overlay) { m_applicationMonitor->setOverlay(overlay); }
78 bool logging() const { return m_applicationMonitor->logging(); }
79 void setLogging(bool logging) { m_applicationMonitor->setLogging(logging); }
80 LoggingFilters loggingFilter() const {
81 return LoggingFilters::enum_type(
82 UMApplicationMonitor::LoggingFilters::Int(
83 m_applicationMonitor->loggingFilter())); }
84 void setLoggingFilter(LoggingFilters filter) {
85 m_applicationMonitor->setLoggingFilter(
86 UMApplicationMonitor::LoggingFilters::enum_type(
87 LoggingFilters::Int(filter))); }
88 int processUpdateInterval() const {
89 return m_applicationMonitor->updateInterval(UMEvent::Process); }
90 void setProcessUpdateInterval(int interval) {
91 m_applicationMonitor->setUpdateInterval(UMEvent::Process, interval); }
92
93 Q_INVOKABLE bool logEvent(Event event) {
94 return m_applicationMonitor->logEvent(static_cast<UMApplicationMonitor::Event>(event)); }
95
96 Q_SIGNALS:
97 void overlayChanged();
98 void loggingChanged();
99 void loggingFilterChanged();
100 void processUpdateIntervalChanged();
101
102 private Q_SLOTS:
103 void updateIntervalChanged(UMEvent::Type type)
104 {
105 if (type == UMEvent::Process) {
106 Q_EMIT processUpdateIntervalChanged();
107 }
108 }
109
110 private:
111 UMApplicationMonitor* m_applicationMonitor;
112 };
113
114 static QObject* applicationMonitorSingletonProvider(QQmlEngine* engine, QJSEngine* scriptEngine)
115 {
116 Q_UNUSED(engine)
117 Q_UNUSED(scriptEngine)
118 return new ApplicationMonitorWrapper();
119 }
120
121 class UbuntuMetricsPlugin : public QQmlExtensionPlugin
122 {
123 Q_OBJECT
124 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface/1.0")
125
126 public:
127 ~UbuntuMetricsPlugin() {}
128
129 virtual void registerTypes(const char* uri) override
130 {
131 Q_ASSERT(QLatin1String(uri) == QLatin1String("Ubuntu.Metrics"));
132
133 UT_PREPEND_NAMESPACE(DeprecationPrinter)::instance()->printDeprecation(
134 UT_PREPEND_NAMESPACE(DeprecationPrinter)::METRICS);
135
136 qmlRegisterSingletonType<ApplicationMonitorWrapper>(
137 uri, 1, 0, "ApplicationMonitor", applicationMonitorSingletonProvider);
138 }
139 };
140
141 #include "plugin.moc"