| 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 |
// FIXME(loicm) |
| 25 |
// - Not sure how to add support for the loggers API? |
| 26 |
// - Add support for the generic logging API. |
| 27 |
|
| 28 |
class ApplicationMonitorWrapper : public QObject |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
|
| 32 |
Q_FLAGS(LoggingFilters); |
| 33 |
Q_ENUMS(Event); |
| 34 |
Q_PROPERTY(bool overlay READ overlay WRITE setOverlay NOTIFY overlayChanged) |
| 35 |
Q_PROPERTY(bool logging READ logging WRITE setLogging NOTIFY loggingChanged) |
| 36 |
Q_PROPERTY(LoggingFilters loggingFilter READ loggingFilter WRITE setLoggingFilter |
| 37 |
NOTIFY loggingFilterChanged) |
| 38 |
Q_PROPERTY(int processUpdateInterval READ processUpdateInterval |
| 39 |
WRITE setProcessUpdateInterval NOTIFY processUpdateIntervalChanged) |
| 40 |
|
| 41 |
public: |
| 42 |
ApplicationMonitorWrapper(QObject* parent = 0) |
| 43 |
: QObject(parent) |
| 44 |
, m_applicationMonitor(UMApplicationMonitor::instance()) |
| 45 |
{ |
| 46 |
QObject::connect(m_applicationMonitor, SIGNAL(overlayChanged()), |
| 47 |
this, SIGNAL(overlayChanged())); |
| 48 |
QObject::connect(m_applicationMonitor, SIGNAL(loggingChanged()), |
| 49 |
this, SIGNAL(loggingChanged())); |
| 50 |
QObject::connect(m_applicationMonitor, SIGNAL(loggingFilterChanged()), |
| 51 |
this, SIGNAL(loggingFilterChanged())); |
| 52 |
QObject::connect(m_applicationMonitor, SIGNAL(updateIntervalChanged(UMEvent::Type)), |
| 53 |
this, SLOT(updateIntervalChanged(UMEvent::Type))); |
| 54 |
} |
| 55 |
~ApplicationMonitorWrapper() {} |
| 56 |
|
| 57 |
enum LoggingFilter { |
| 58 |
ProcessEvent = UMApplicationMonitor::ProcessEvent, |
| 59 |
WindowEvent = UMApplicationMonitor::WindowEvent, |
| 60 |
FrameEvent = UMApplicationMonitor::FrameEvent, |
| 61 |
GenericEvent = UMApplicationMonitor::GenericEvent, |
| 62 |
AllEvents = UMApplicationMonitor::AllEvents |
| 63 |
}; |
| 64 |
Q_DECLARE_FLAGS(LoggingFilters, LoggingFilter) |
| 65 |
|
| 66 |
enum Event { |
| 67 |
UserInterfaceReady = UMApplicationMonitor::UserInterfaceReady |
| 68 |
}; |
| 69 |
|
| 70 |
bool overlay() const { return m_applicationMonitor->overlay(); } |
| 71 |
void setOverlay(bool overlay) { m_applicationMonitor->setOverlay(overlay); } |
| 72 |
bool logging() const { return m_applicationMonitor->logging(); } |
| 73 |
void setLogging(bool logging) { m_applicationMonitor->setLogging(logging); } |
| 74 |
LoggingFilters loggingFilter() const { |
| 75 |
return LoggingFilters::enum_type( |
| 76 |
UMApplicationMonitor::LoggingFilters::Int( |
| 77 |
m_applicationMonitor->loggingFilter())); } |
| 78 |
void setLoggingFilter(LoggingFilters filter) { |
| 79 |
m_applicationMonitor->setLoggingFilter( |
| 80 |
UMApplicationMonitor::LoggingFilters::enum_type( |
| 81 |
LoggingFilters::Int(filter))); } |
| 82 |
int processUpdateInterval() const { |
| 83 |
return m_applicationMonitor->updateInterval(UMEvent::Process); } |
| 84 |
void setProcessUpdateInterval(int interval) { |
| 85 |
m_applicationMonitor->setUpdateInterval(UMEvent::Process, interval); } |
| 86 |
|
| 87 |
Q_INVOKABLE bool logEvent(Event event) { |
| 88 |
return m_applicationMonitor->logEvent(static_cast<UMApplicationMonitor::Event>(event)); } |
| 89 |
|
| 90 |
Q_SIGNALS: |
| 91 |
void overlayChanged(); |
| 92 |
void loggingChanged(); |
| 93 |
void loggingFilterChanged(); |
| 94 |
void processUpdateIntervalChanged(); |
| 95 |
|
| 96 |
private Q_SLOTS: |
| 97 |
void updateIntervalChanged(UMEvent::Type type) |
| 98 |
{ |
| 99 |
if (type == UMEvent::Process) { |
| 100 |
Q_EMIT processUpdateIntervalChanged(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
private: |
| 105 |
UMApplicationMonitor* m_applicationMonitor; |
| 106 |
}; |
| 107 |
|
| 108 |
static QObject* applicationMonitorSingletonProvider(QQmlEngine* engine, QJSEngine* scriptEngine) |
| 109 |
{ |
| 110 |
Q_UNUSED(engine) |
| 111 |
Q_UNUSED(scriptEngine) |
| 112 |
return new ApplicationMonitorWrapper(); |
| 113 |
} |
| 114 |
|
| 115 |
class LomiriMetricsPlugin : public QQmlExtensionPlugin |
| 116 |
{ |
| 117 |
Q_OBJECT |
| 118 |
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface/1.0") |
| 119 |
|
| 120 |
public: |
| 121 |
~LomiriMetricsPlugin() {} |
| 122 |
|
| 123 |
virtual void registerTypes(const char* uri) override |
| 124 |
{ |
| 125 |
Q_ASSERT(QLatin1String(uri) == QLatin1String("Lomiri.Metrics")); |
| 126 |
qmlRegisterModule(uri, 0, 1); |
| 127 |
qmlRegisterSingletonType<ApplicationMonitorWrapper>( |
| 128 |
uri, 1, 0, "ApplicationMonitor", applicationMonitorSingletonProvider); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
#include "plugin.moc" |