summaryrefslogtreecommitdiffstats
path: root/src/gui/incoming_call_popup.cpp
blob: 1159ece7789d70407eea702a172c5b04987e1f8f (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
#include "incoming_call_popup.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QDeclarativeContext>
#include <QSettings>

extern QSettings* g_gui_state;

IncomingCallPopup::IncomingCallPopup(QObject *parent) : QObject(parent)
{
	m_view = new QDeclarativeView;

	// Qt5 QQuickView: setFlags()
	m_view->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::ToolTip);

	m_view->rootContext()->setContextProperty("viewerWidget", m_view);
	m_view->setSource(QUrl("qrc:/qml/incoming_call.qml"));

    // Place into the middle of the screen
	positionWindow();

	QObject* button;

	button = m_view->rootObject()->findChild<QObject*>("buttonAnswer");
	connect(button, SIGNAL(clicked()), this, SLOT(onAnswerClicked()));

	button = m_view->rootObject()->findChild<QObject*>("buttonReject");
	connect(button, SIGNAL(clicked()), this, SLOT(onRejectClicked()));

	m_callerText = m_view->rootObject()->findChild<QDeclarativeItem*>("callerText");
	connect(m_view->rootObject(), SIGNAL(moved()), this, SLOT(saveState()));
}

IncomingCallPopup::~IncomingCallPopup()
{
	delete m_view;
}

void IncomingCallPopup::positionWindow()
{
	QDesktopWidget* desktop = qApp->desktop();
	int x, y;
	int defaultX, defaultY;

	defaultX = desktop->width()/2 - m_view->width()/2;
	defaultY = desktop->height()/2 - m_view->height()/2;

	x = g_gui_state->value("incoming_popup/x", defaultX).toInt();
	y = g_gui_state->value("incoming_popup/y", defaultY).toInt();

	// Reset position if off screen
	if (x > desktop->width() || x < 0)
		x = defaultX;
	if (y > desktop->height() || y < 0)
		y = defaultY;

	m_view->move(x, y);
}

void IncomingCallPopup::saveState()
{
	QPoint pos = m_view->pos();
	g_gui_state->setValue("incoming_popup/x", pos.x());
	g_gui_state->setValue("incoming_popup/y", pos.y());
}

void IncomingCallPopup::move(int x, int y)
{
    m_view->move(QPoint(x, y));
}

void IncomingCallPopup::setCallerName(const QString& name)
{
	QString text = tr("%1 calling").arg(name);
	m_callerText->setProperty("text", text);
}

void IncomingCallPopup::onAnswerClicked()
{
	emit answerClicked();
	m_view->hide();
}

void IncomingCallPopup::onRejectClicked()
{
	emit rejectClicked();
	m_view->hide();
}

void IncomingCallPopup::show()
{
	m_view->show();
}

void IncomingCallPopup::hide()
{
	m_view->hide();
}