forked from benlau/quickios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqiactionsheet.cpp
113 lines (82 loc) · 2.31 KB
/
qiactionsheet.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
#include "qisystemdispatcher.h"
#include "qiactionsheet.h"
QIActionSheet::QIActionSheet(QQuickItem* parent) : QQuickItem(parent)
{
m_clickedButtonIndex = -1;
running = false;
m_cancelButtonTitle = tr("Cancel");
}
QIActionSheet::~QIActionSheet()
{
}
QString QIActionSheet::title() const
{
return m_title;
}
void QIActionSheet::setTitle(const QString &title)
{
m_title = title;
emit titleChanged();
}
QStringList QIActionSheet::otherButtonTitles() const
{
return m_otherButtonTitles;
}
void QIActionSheet::setOtherButtonTitles(const QStringList &otherButtonTitles)
{
m_otherButtonTitles = otherButtonTitles;
emit otherButtonTitlesChanged();
}
void QIActionSheet::show()
{
if (running)
return;
running = true;
QISystemDispatcher* system = QISystemDispatcher::instance();
QVariantMap data;
data["title"] = m_title;
data["otherButtonTitles"] = m_otherButtonTitles;
data["cancelButtonTitle"] = m_cancelButtonTitle;
QRect rect(x(),y(),width(),height());
data["sourceRect"] = mapRectToScene(rect);
running = true;
connect(system,SIGNAL(dispatched(QString,QVariantMap)),
this,SLOT(onReceived(QString,QVariantMap)));
system->dispatch("actionSheetCreate",data);
}
void QIActionSheet::onReceived(QString name, QVariantMap data)
{
if (name != "actionSheetClickedButtonAtIndex" &&
name != "actionSheetDidDismissWithButtonIndex"){
return;
}
int buttonIndex = data["buttonIndex"].toInt();
if (buttonIndex >= m_otherButtonTitles.size())
buttonIndex = -1;
if (name == "actionSheetClickedButtonAtIndex") {
setClickedButtonIndex(buttonIndex);
emit clicked(buttonIndex);
} else {
running = false;
QISystemDispatcher* system = QISystemDispatcher::instance();
system->disconnect(this);
emit dismissed(buttonIndex);
}
}
QString QIActionSheet::cancelButtonTitle() const
{
return m_cancelButtonTitle;
}
void QIActionSheet::setCancelButtonTitle(const QString &cancelButtonTitle)
{
m_cancelButtonTitle = cancelButtonTitle;
}
int QIActionSheet::clickedButtonIndex() const
{
return m_clickedButtonIndex;
}
void QIActionSheet::setClickedButtonIndex(int clickedButton)
{
m_clickedButtonIndex = clickedButton;
emit clickedButtonIndexChanged();
}