4#ifndef QOHOSWINDOWPROPERTY_H
5#define QOHOSWINDOWPROPERTY_H
8#include <QtCore/qpointer.h>
9#include <QtCore/private/qohoscommon_p.h>
10#include <QtCore/qglobal.h>
11#include <QtCore/qvariant.h>
12#include <QtGui/qwindow.h>
20class QOhosPropertyDescriptor
23 explicit QOhosPropertyDescriptor(
const char *name);
25 const char *name()
const;
32QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor(
const char *name)
39const char *QOhosPropertyDescriptor<T>::name()
const
44template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
47template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
55 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
60 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
65 class PropertyWriteConsumersStore
69 std::shared_ptr<
void> addConsumer(QOhosConsumer<T> consumer);
72 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
75 QObject *objectOrFail()
const;
77 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
78 void notifyPropertyWriteInternal();
80 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
81 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
83 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
84 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
86 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
87 void registerPropertyNotifierIfMissing();
89 QPointer<QObject> m_object;
90 std::map<
const void *, std::shared_ptr<
void>> m_propertyWriteConsumersStoresMap;
99 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
102 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
111template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
119template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
122 qObject->setProperty(
124 QVariant::fromValue(propertyValue));
127template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
131 const auto propertyTypeId = qMetaTypeId<T>();
132 auto value = qObject->property(propertyName);
134 if (!value.isValid())
138 if (!optValue.has_value()) {
140 "Property \"%s\" type mismatch, expected: %d, got: %d",
141 propertyName, propertyTypeId, value.userType());
152template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
155 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
160 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
161 if (notifierIt != m_propertyNameToNotifierMap.end())
162 notifierIt->second();
165template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
168 registerPropertyNotifierIfMissing<T, propertyPtr>();
169 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(
std::move(propertyWriteCallback));
175 auto weakConsumersPendingNotify =
std::exchange(m_consumers, {});
176 std::vector<std::weak_ptr<QOhosConsumer<T>>> weakConsumersToAdd;
177 for (
auto &weakConsumerPendingNotify: weakConsumersPendingNotify) {
178 auto sharedConsumer = weakConsumerPendingNotify.lock();
179 if (sharedConsumer) {
180 weakConsumersToAdd.push_back(weakConsumerPendingNotify);
181 (*sharedConsumer)(value);
184 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
188std::shared_ptr<
void>
QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
190 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
191 m_consumers.emplace_back(sharedConsumer);
192 return sharedConsumer;
197 QObject *object = m_object;
198 if (object ==
nullptr)
199 qOhosReportFatalErrorAndAbort(
"%s: m_object was null", Q_FUNC_INFO);
203template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
206 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
207 if (!propertyWriteConsumersStore)
210 auto propertyValue = tryGetProperty<T, propertyPtr>();
211 if (propertyValue.has_value())
212 propertyWriteConsumersStore->notify(propertyValue.value());
215template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
218 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
219 if (!propertyWriteConsumersStore) {
220 propertyWriteConsumersStore =
std::make_shared<PropertyWriteConsumersStore<T>>();
221 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
224 return propertyWriteConsumersStore;
227template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
230 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
231 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
233 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
236template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
240 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
241 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
242 m_propertyNameToNotifierMap.emplace(
245 this->notifyPropertyWriteInternal<T, propertyPtr>();
255template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
258 return m_store->tryGetProperty<T, propertyPtr>();
261template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
264 return m_store->addPropertyWriteCallback<T, propertyPtr>(
std::move(propertyWriteCallback));
269template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
272 return propertyPtr->name();
278 return qMetaTypeId<T>() == variant.userType()
279 ?
std::optional(variant.value<T>())
QOhosPropertiesProvider(QOhosPropertiesStore &store)
std::optional< T > tryGetProperty() const
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
QOhosPropertiesStore(QObject *qObject)
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
std::optional< T > tryGetProperty() const
void notifyPropertyWrite(const QByteArray &propertyName)
bool sendEvent(WindowSystemEvent *event) override
Combined button and popup list for selecting options.
@ ForwardToGuiApplication
QOhosView * mapQWindowToViewOrNull(QWindow *window)
std::optional< T > tryMapFromQVariant(QVariant variant)
const char * qObjectOhosPropertyName()
std::shared_ptr< QWindowSystemEventHandler > makeApplicationStateTracker()
void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
std::optional< T > tryGetQOhosPropertyFromQObject(QObject *qObject)