Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#import <UIKit/UIKit.h>
6
7#include <QtGui/qwindow.h>
8#include <QDebug>
9
10#include <QtCore/private/qcore_mac_p.h>
11
12#include "qiosglobal.h"
15
16@interface QIOSColorDialogController : UIColorPickerViewController <UIColorPickerViewControllerDelegate,
17 UIAdaptivePresentationControllerDelegate>
18@end
19
20@implementation QIOSColorDialogController {
21 QIOSColorDialog *m_colorDialog;
22}
23
24- (instancetype)initWithQIOSColorDialog:(QIOSColorDialog *)dialog
25{
26 if (self = [super init]) {
27 m_colorDialog = dialog;
28 self.delegate = self;
29 self.presentationController.delegate = self;
30 self.supportsAlpha = dialog->options()->testOption(QColorDialogOptions::ShowAlphaChannel);
31 }
32 return self;
33}
34
35- (void)setQColor:(const QColor &)qColor
36{
37 UIColor *uiColor;
38 const QColor::Spec spec = qColor.spec();
39 if (spec == QColor::Hsv) {
40 uiColor = [UIColor colorWithHue:qColor.hsvHueF()
41 saturation:qColor.hsvSaturationF()
42 brightness:qColor.valueF()
43 alpha:qColor.alphaF()];
44 } else {
45 uiColor = [UIColor colorWithRed:qColor.redF()
46 green:qColor.greenF()
47 blue:qColor.blueF()
48 alpha:qColor.alphaF()];
49 }
50 self.selectedColor = uiColor;
51}
52
53- (void)updateQColor
54{
55 UIColor *color = self.selectedColor;
56 CGFloat red = 0, green = 0, blue = 0, alpha = 0;
57
58 QColor newColor;
59 if ([color getRed:&red green:&green blue:&blue alpha:&alpha])
60 newColor.setRgbF(red, green, blue, alpha);
61 else
62 qWarning() << "Incompatible color space";
63
64
65 if (m_colorDialog) {
66 m_colorDialog->updateColor(newColor);
67 emit m_colorDialog->currentColorChanged(newColor);
68 }
69}
70
71// ----------------------UIColorPickerViewControllerDelegate--------------------------
72- (void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController
73{
74 Q_UNUSED(viewController);
75 [self updateQColor];
76}
77
78- (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController
79{
80 Q_UNUSED(viewController);
81 [self updateQColor];
82 emit m_colorDialog->accept();
83}
84
85// ----------------------UIAdaptivePresentationControllerDelegate--------------------------
86- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
87{
88 Q_UNUSED(presentationController);
89 emit m_colorDialog->reject();
90}
91
92@end
93
94QIOSColorDialog::QIOSColorDialog()
95 : m_viewController(nullptr)
96{
97}
98
99QIOSColorDialog::~QIOSColorDialog()
100{
101 hide();
102}
103
104void QIOSColorDialog::exec()
105{
106 m_eventLoop.exec(QEventLoop::DialogExec);
107}
108
109bool QIOSColorDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
110{
111 Q_UNUSED(windowFlags);
112
113 if (!m_viewController) {
114 m_viewController = [[QIOSColorDialogController alloc] initWithQIOSColorDialog:this];
115 if (m_currentColor.isValid())
116 [m_viewController setQColor:m_currentColor];
117 }
118
119 if (windowModality == Qt::ApplicationModal || windowModality == Qt::WindowModal)
120 m_viewController.modalInPresentation = YES;
121
122 UIWindow *window = presentationWindow(parent);
123 if (!window)
124 return false;
125
126 // We can't present from view controller if already presenting
127 if (window.rootViewController.presentedViewController)
128 return false;
129
130 [window.rootViewController presentViewController:m_viewController animated:YES completion:nil];
131
132 return true;
133}
134
135void QIOSColorDialog::hide()
136{
137 [m_viewController dismissViewControllerAnimated:YES completion:nil];
138 [m_viewController release];
139 m_viewController = nullptr;
140 m_eventLoop.exit();
141}
142
143void QIOSColorDialog::setCurrentColor(const QColor &color)
144{
145 updateColor(color);
146 if (m_viewController)
147 [m_viewController setQColor:color];
148}
149
150QColor QIOSColorDialog::currentColor() const
151{
152 return m_currentColor;
153}
154
155void QIOSColorDialog::updateColor(const QColor &color)
156{
157 m_currentColor = color;
158}