This repository was archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathqialertview.cpp
157 lines (115 loc) · 3.09 KB
/
qialertview.cpp
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
#include "qialertview.h"
#include "qisystemdispatcher.h"
#ifndef Q_OS_IOS
#endif
/*! \qmltype AlertView
* \inqmlmodule QuickIOS
*/
QIAlertView::QIAlertView(QQuickItem *parent) :
QQuickItem(parent)
{
m_buttons << tr("Cancel") << tr("OK");
m_opened = false;
m_clickedButtonIndex = -1;
}
QIAlertView::~QIAlertView()
{
#ifndef Q_OS_IOS
if (!dialog.isNull()) {
dialog->reject();
}
#endif
}
QString QIAlertView::title() const {
return m_title;
}
QString QIAlertView::message() const
{
return m_message;
}
void QIAlertView::setTitle(const QString &arg)
{
m_title = arg;
emit titleChanged();
}
void QIAlertView::setMessage(const QString &arg)
{
m_message = arg;
emit messageChanged();
}
void QIAlertView::show()
{
if (m_opened)
return;
#ifdef Q_OS_IOS
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap data;
data["title"] = m_title;
data["message"] = m_message;
data["buttons"] = m_buttons;
m_opened = true;
connect(system,SIGNAL(dispatched(QString,QVariantMap)),
this,SLOT(onReceived(QString,QVariantMap)));
system->dispatch("alertViewCreate",data);
#else
dialog = new QMessageBox();
dialog->setWindowTitle(m_title);
dialog->setText(m_message);
for (int i = 0 ; i < m_buttons.size() ; i++) {
dialog->addButton(m_buttons.at(i),QMessageBox::ActionRole);
}
connect(dialog.data(),SIGNAL(finished(int)),
this,SLOT(onFinished(int)));
connect(dialog.data(),SIGNAL(finished(int)),
dialog.data(),SLOT(close()));
connect(dialog.data(),SIGNAL(finished(int)),
dialog.data(),SLOT(deleteLater()));
dialog->show();
#endif
}
/*! \qmlmethod AlertView::dismiss(int clickedButtonIndex, bool animated)
Dismisses the AlertView, optionally with animation.
clickedButtonIndex: The index of the button that was clicked just before invoking this method.
animated: Enable/disable animation
*/
void QIAlertView::dismiss(int clickedButtonIndex, bool animated)
{
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap message;
message["index"] = clickedButtonIndex;
message["animated"] = animated;
system->dispatch("alertViewDismissWithClickedButtonIndex",message);
}
void QIAlertView::onReceived(QString name, QVariantMap data)
{
if (name != "alertViewClickedButtonAtIndex"){
return;
}
QISystemDispatcher* system = QISystemDispatcher::instance();
system->disconnect(this);
int buttonIndex = data["buttonIndex"].toInt();
onFinished(buttonIndex);
}
void QIAlertView::onFinished(int index)
{
m_opened = false;
setClickedButtonIndex(index);
emit clicked(index);
}
int QIAlertView::clickedButtonIndex() const
{
return m_clickedButtonIndex;
}
void QIAlertView::setClickedButtonIndex(int clickedButtonIndex)
{
m_clickedButtonIndex = clickedButtonIndex;
emit clickedButtonIndexChanged();
}
QStringList QIAlertView::buttons() const
{
return m_buttons;
}
void QIAlertView::setButtons(const QStringList &buttons)
{
m_buttons = buttons;
}