Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qioscolordialog.mm
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#import <UIKit/UIKit.h>
5
6#include <QtGui/qwindow.h>
7#include <QDebug>
8
9#include <QtCore/private/qcore_mac_p.h>
10
11#include "qioscolordialog.h"
12#include "qiosintegration.h"
13
14@interface QIOSColorDialogController : UIColorPickerViewController <UIColorPickerViewControllerDelegate,
15 UIAdaptivePresentationControllerDelegate>
16@end
17
18@implementation QIOSColorDialogController {
19 QIOSColorDialog *m_colorDialog;
20}
21
22- (instancetype)initWithQIOSColorDialog:(QIOSColorDialog *)dialog
23{
24 if (self = [super init]) {
25 m_colorDialog = dialog;
26 self.delegate = self;
27 self.presentationController.delegate = self;
28 self.supportsAlpha = dialog->options()->testOption(QColorDialogOptions::ShowAlphaChannel);
29 }
30 return self;
31}
32
33- (void)setQColor:(const QColor &)qColor
34{
35 UIColor *uiColor;
36 const QColor::Spec spec = qColor.spec();
37 if (spec == QColor::Hsv) {
38 uiColor = [UIColor colorWithHue:qColor.hsvHueF()
39 saturation:qColor.hsvSaturationF()
40 brightness:qColor.valueF()
41 alpha:qColor.alphaF()];
42 } else {
43 uiColor = [UIColor colorWithRed:qColor.redF()
44 green:qColor.greenF()
45 blue:qColor.blueF()
46 alpha:qColor.alphaF()];
47 }
48 self.selectedColor = uiColor;
49}
50
51- (void)updateQColor
52{
53 UIColor *color = self.selectedColor;
54 CGFloat red = 0, green = 0, blue = 0, alpha = 0;
55
56 QColor newColor;
57 if ([color getRed:&red green:&green blue:&blue alpha:&alpha])
58 newColor.setRgbF(red, green, blue, alpha);
59 else
60 qWarning() << "Incompatible color space";
61
62
63 if (m_colorDialog) {
64 m_colorDialog->updateColor(newColor);
65 emit m_colorDialog->currentColorChanged(newColor);
66 }
67}
68
69// ----------------------UIColorPickerViewControllerDelegate--------------------------
70- (void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController
71{
72 Q_UNUSED(viewController);
73 [self updateQColor];
74}
75
76- (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController
77{
78 Q_UNUSED(viewController);
79 [self updateQColor];
80 emit m_colorDialog->accept();
81}
82
83// ----------------------UIAdaptivePresentationControllerDelegate--------------------------
84- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
85{
86 Q_UNUSED(presentationController);
87 emit m_colorDialog->reject();
88}
89
90@end
91
93 : m_viewController(nullptr)
94{
95}
96
98{
99 hide();
100}
101
103{
104 m_eventLoop.exec(QEventLoop::DialogExec);
105}
106
107bool QIOSColorDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
108{
109 Q_UNUSED(windowFlags);
110
111 if (!m_viewController) {
112 m_viewController = [[QIOSColorDialogController alloc] initWithQIOSColorDialog:this];
113 if (m_currentColor.isValid())
114 [m_viewController setQColor:m_currentColor];
115 }
116
117 if (windowModality == Qt::ApplicationModal || windowModality == Qt::WindowModal)
118 m_viewController.modalInPresentation = YES;
119
120 UIWindow *window = parent ? reinterpret_cast<UIView *>(parent->winId()).window
121 : qt_apple_sharedApplication().keyWindow;
122 if (!window)
123 return false;
124
125 // We can't present from view controller if already presenting
126 if (window.rootViewController.presentedViewController)
127 return false;
128
129 [window.rootViewController presentViewController:m_viewController animated:YES completion:nil];
130
131 return true;
132}
133
135{
136 [m_viewController dismissViewControllerAnimated:YES completion:nil];
137 [m_viewController release];
138 m_viewController = nullptr;
139 m_eventLoop.exit();
140}
141
143{
145 if (m_viewController)
146 [m_viewController setQColor:color];
147}
148
150{
151 return m_currentColor;
152}
153
155{
156 m_currentColor = color;
157}
158
159
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
Spec
The type of color specified, either RGB, extended RGB, HSV, CMYK or HSL.
Definition qcolor.h:35
@ Hsv
Definition qcolor.h:35
void setRgbF(float r, float g, float b, float a=1.0)
Sets the color channels of this color to r (red), g (green), b (blue) and a (alpha,...
Definition qcolor.cpp:1317
bool isValid() const noexcept
Returns true if the color is valid; otherwise returns false.
Definition qcolor.h:285
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
void exit(int returnCode=0)
Tells the event loop to exit with a return code.
Options options
the various options that affect the look and feel of the dialog
Definition qfiledialog.h:34
void setCurrentColor(const QColor &) override
void exec() override
QColor currentColor() const override
void hide() override
void updateColor(const QColor &)
bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
\inmodule QtGui
Definition qwindow.h:63
WindowModality
@ WindowModal
@ ApplicationModal
float CGFloat
AppleApplication * qt_apple_sharedApplication()
Definition qcore_mac.mm:430
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define qWarning
Definition qlogging.h:162
GLbyte GLbyte blue
Definition qopenglext.h:385
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
GLbyte green
Definition qopenglext.h:385
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define emit
#define Q_UNUSED(x)
QObject::connect nullptr
QFileDialog dialog(this)
[1]
aWidget window() -> setWindowTitle("New Window Title")
[2]
QGraphicsSvgItem * red
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent