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>
19class QOhosPropertyDescriptor
22 explicit QOhosPropertyDescriptor(
const char *name);
24 const char *name()
const;
31QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor(
const char *name)
38const char *QOhosPropertyDescriptor<T>::name()
const
43template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
46template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
54 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
59 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
64 class PropertyWriteConsumersStore
68 std::shared_ptr<
void> addConsumer(QOhosConsumer<T> consumer);
71 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
74 QObject *objectOrFail()
const;
76 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
77 void notifyPropertyWriteInternal();
79 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
80 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
82 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
83 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
85 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
86 void registerPropertyNotifierIfMissing();
88 QPointer<QObject> m_object;
89 std::map<
const void *, std::shared_ptr<
void>> m_propertyWriteConsumersStoresMap;
98 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
101 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
110template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
118template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
121 qObject->setProperty(
123 QVariant::fromValue(propertyValue));
126template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
130 const auto propertyTypeId = qMetaTypeId<T>();
131 auto value = qObject->property(propertyName);
133 if (!value.isValid())
137 if (!optValue.has_value()) {
139 "Property \"%s\" type mismatch, expected: %d, got: %d",
140 propertyName, propertyTypeId, value.userType());
151template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
154 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
159 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
160 if (notifierIt != m_propertyNameToNotifierMap.end())
161 notifierIt->second();
164template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
167 registerPropertyNotifierIfMissing<T, propertyPtr>();
168 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(
std::move(propertyWriteCallback));
174 auto weakConsumersPendingNotify = std::exchange(m_consumers, {});
175 std::vector<std::weak_ptr<QOhosConsumer<T>>> weakConsumersToAdd;
176 for (
auto &weakConsumerPendingNotify: weakConsumersPendingNotify) {
177 auto sharedConsumer = weakConsumerPendingNotify.lock();
178 if (sharedConsumer) {
179 weakConsumersToAdd.push_back(weakConsumerPendingNotify);
180 (*sharedConsumer)(value);
183 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
187std::shared_ptr<
void>
QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
189 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
190 m_consumers.emplace_back(sharedConsumer);
191 return sharedConsumer;
196 QObject *object = m_object;
197 if (object ==
nullptr)
198 qOhosReportFatalErrorAndAbort(
"%s: m_object was null", Q_FUNC_INFO);
202template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
205 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
206 if (!propertyWriteConsumersStore)
209 auto propertyValue = tryGetProperty<T, propertyPtr>();
210 if (propertyValue.has_value())
211 propertyWriteConsumersStore->notify(propertyValue.value());
214template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
217 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
218 if (!propertyWriteConsumersStore) {
219 propertyWriteConsumersStore =
std::make_shared<PropertyWriteConsumersStore<T>>();
220 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
223 return propertyWriteConsumersStore;
226template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
229 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
230 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
232 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
235template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
239 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
240 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
241 m_propertyNameToNotifierMap.emplace(
244 this->notifyPropertyWriteInternal<T, propertyPtr>();
254template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
257 return m_store->tryGetProperty<T, propertyPtr>();
260template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
263 return m_store->addPropertyWriteCallback<T, propertyPtr>(
std::move(propertyWriteCallback));
268template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
271 return propertyPtr->name();
277 return qMetaTypeId<T>() == variant.userType()
278 ?
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)