File size: 1,909 Bytes
a4cf109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "ButtonItem.h"

namespace QuickKeyboard
{

ButtonItem::ButtonItem(QQuickItem *parent):
	QQuickItem(parent),
	m_active(false),
	m_mouseDown(false),
	m_modifier(false),
	m_col(0),
	m_row(0),
	m_colSpan(1),
	m_rowSpan(1),
	m_currentSymbolIndex(-1)
{
	connect(this, SIGNAL(symbolsChanged(const QStringList &)), SLOT(onSymbolsChanged()));
	connect(this, SIGNAL(triggered()), SLOT(onTriggered()));
	connect(this, SIGNAL(released()), SLOT(onReleased()));
}

ButtonItem::~ButtonItem()
{
}

void ButtonItem::setActive(bool active)
{
	if (m_active == active) {
		return;
	}

	m_active = active;
	emit activeChanged(active);
	emit pressedChanged(m_active || m_mouseDown);
	setCurrentSymbolIndex(0);
}

void ButtonItem::setMouseDown(bool mouseDown)
{
	if (m_mouseDown == mouseDown) {
		return;
	}

	m_mouseDown = mouseDown;
	emit mouseDownChanged(mouseDown);
	emit pressedChanged(m_active || m_mouseDown);
	setCurrentSymbolIndex(0);
}

void ButtonItem::setCurrentSymbolIndex(int currentSymbolIndex)
{
	if (m_symbols.length() == 0) {
		currentSymbolIndex = -1;
	}
	if (currentSymbolIndex == -1 && m_symbols.length() > 0) {
		currentSymbolIndex = 0;
	}

	if (currentSymbolIndex == m_currentSymbolIndex) {
		return;
	}

	m_currentSymbolIndex = currentSymbolIndex;
	emit currentSymbolIndexChanged(currentSymbolIndex);
}

void ButtonItem::onSymbolsChanged()
{
	if (m_symbols.length() == 0) {
		setCurrentSymbolIndex(-1);
	}
	else {
		if (m_currentSymbolIndex < 0 || m_currentSymbolIndex >= m_symbols.length()) {
			setCurrentSymbolIndex(0);
		}
	}
}

void ButtonItem::onTriggered()
{
	if (m_currentSymbolIndex < 0 || m_currentSymbolIndex >= m_symbols.length()) {
		return;
	}

	emit symbolTriggered(m_symbols[m_currentSymbolIndex]);
}

void ButtonItem::onReleased()
{
	bool active = m_active;
	if (m_modifier) {
		setActive(!active);
	}
	if (isStandard() || active) {
		emit triggered();
	}
}

} /* QuickKeyboard */