summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/ios/qioscolordialog.mm
blob: 92c0f3e46c8d95c6d538a7a6c9d9e063e406e2c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#import <UIKit/UIKit.h>

#include <QtGui/qwindow.h>
#include <QDebug>

#include <QtCore/private/qcore_mac_p.h>

#include "qioscolordialog.h"
#include "qiosintegration.h"

@interface QIOSColorDialogController : UIColorPickerViewController <UIColorPickerViewControllerDelegate,
                                                                    UIAdaptivePresentationControllerDelegate>
@end

@implementation QIOSColorDialogController {
    QIOSColorDialog *m_colorDialog;
}

- (instancetype)initWithQIOSColorDialog:(QIOSColorDialog *)dialog
{
    if (self = [super init]) {
        m_colorDialog = dialog;
        self.delegate = self;
        self.presentationController.delegate = self;
        self.supportsAlpha = dialog->options()->testOption(QColorDialogOptions::ShowAlphaChannel);
    }
    return self;
}

- (void)setQColor:(const QColor &)qColor
{
    UIColor *uiColor;
    const QColor::Spec spec = qColor.spec();
    if (spec == QColor::Hsv) {
        uiColor = [UIColor colorWithHue:qColor.hsvHueF()
                             saturation:qColor.hsvSaturationF()
                             brightness:qColor.valueF()
                                  alpha:qColor.alphaF()];
    } else {
        uiColor = [UIColor colorWithRed:qColor.redF()
                                  green:qColor.greenF()
                                   blue:qColor.blueF()
                                  alpha:qColor.alphaF()];
    }
    self.selectedColor = uiColor;
}

- (void)updateQColor
{
    UIColor *color = self.selectedColor;
    CGFloat red = 0, green = 0, blue = 0, alpha = 0;

    QColor newColor;
    if ([color getRed:&red green:&green blue:&blue alpha:&alpha])
        newColor.setRgbF(red, green, blue, alpha);
    else
        qWarning() << "Incompatible color space";


    if (m_colorDialog) {
        m_colorDialog->updateColor(newColor);
        emit m_colorDialog->currentColorChanged(newColor);
    }
}

// ----------------------UIColorPickerViewControllerDelegate--------------------------
- (void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController
{
    Q_UNUSED(viewController);
    [self updateQColor];
}

- (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController
{
    Q_UNUSED(viewController);
    [self updateQColor];
    emit m_colorDialog->accept();
}

// ----------------------UIAdaptivePresentationControllerDelegate--------------------------
- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
{
    Q_UNUSED(presentationController);
    emit m_colorDialog->reject();
}

@end

QIOSColorDialog::QIOSColorDialog()
    : m_viewController(nullptr)
{
}

QIOSColorDialog::~QIOSColorDialog()
{
    hide();
}

void QIOSColorDialog::exec()
{
    m_eventLoop.exec(QEventLoop::DialogExec);
}

bool QIOSColorDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
{
    Q_UNUSED(windowFlags);

    if (!m_viewController) {
        m_viewController = [[QIOSColorDialogController alloc] initWithQIOSColorDialog:this];
        if (m_currentColor.isValid())
            [m_viewController setQColor:m_currentColor];
    }

    if (windowModality == Qt::ApplicationModal || windowModality == Qt::WindowModal)
        m_viewController.modalInPresentation = YES;

    UIWindow *window = parent ? reinterpret_cast<UIView *>(parent->winId()).window
        : qt_apple_sharedApplication().keyWindow;
    if (!window)
        return false;

    // We can't present from view controller if already presenting
    if (window.rootViewController.presentedViewController)
        return false;

    [window.rootViewController presentViewController:m_viewController animated:YES completion:nil];

    return true;
}

void QIOSColorDialog::hide()
{
    [m_viewController dismissViewControllerAnimated:YES completion:nil];
    [m_viewController release];
    m_viewController = nullptr;
    m_eventLoop.exit();
}

void QIOSColorDialog::setCurrentColor(const QColor &color)
{
    updateColor(color);
    if (m_viewController)
        [m_viewController setQColor:color];
}

QColor QIOSColorDialog::currentColor() const
{
    return m_currentColor;
}

void QIOSColorDialog::updateColor(const QColor &color)
{
    m_currentColor = color;
}