testmodelui.cpp Example File
incomingcalls/testmodelui.cpp
#include "testmodelui.h"
#include "qtcontacts.h"
#include "qcontactlistmodel.h"
#include "qcontactfetchrequest.h"
#include <QtGui>
TestModelView::TestModelView(QContactManager* manager)
: QListView()
{
model = new QContactListModel(manager);
setModel(model);
}
TestModelView::~TestModelView()
{
delete model;
}
QContactLocalId TestModelView::currentId() const
{
return QContactLocalId(model->data(currentIndex(), QContactListModel::IdRole).toUInt());
}
QVariant TestModelView::currentData(QContactListModel::ContactDataRole role) const
{
return model->data(currentIndex(), role);
}
TestModelUi::TestModelUi()
: QWidget()
{
manager = new QContactManager("memory");
populateContacts();
nbrMissedCalls = 0;
talkingToNumber = "";
dialog = new FilterDialog;
connect(dialog, SIGNAL(hidden()), this, SLOT(filterFound()));
fetchRequest = new QContactFetchRequest;
fetchRequest->setManager(manager);
connect(fetchRequest, SIGNAL(progress(QContactFetchRequest*,bool)), this, SLOT(dataAvailable(QContactFetchRequest*,bool)));
filterRequest = new QContactFetchRequest;
filterRequest->setManager(manager);
connect(filterRequest, SIGNAL(progress(QContactFetchRequest*,bool)), this, SLOT(filterResults(QContactFetchRequest*,bool)));
incomingCallTimer = new QTimer;
incomingCallTimer->setInterval(15000);
dialTimer = new QTimer;
dialTimer->setInterval(3000);
answerTimer = new QTimer;
answerTimer->setInterval(6000);
connect(incomingCallTimer, SIGNAL(timeout()), this, SLOT(incoming()));
connect(dialTimer, SIGNAL(timeout()), this, SLOT(talking()));
connect(answerTimer, SIGNAL(timeout()), this, SLOT(missedCall()));
incomingCallTimer->start();
list = new TestModelView(manager);
textEdit = new QTextEdit;
viewArea = new QStackedWidget;
viewArea->addWidget(list);
viewArea->addWidget(textEdit);
viewArea->setCurrentIndex(0);
missedCalls = new QLabel;
missedCalls->setAlignment(Qt::AlignLeft);
missedCalls->setText(QString(tr("# Missed Calls:")));
missedCallsNbr = new QLabel;
missedCallsNbr->setAlignment(Qt::AlignRight);
missedCallsNbr->setText(QString::number(nbrMissedCalls));
leftButton = new QPushButton(tr("Dial"));
middleButton = new QPushButton(tr("Find"));
rightButton = new QPushButton(tr("Quit"));
connect(leftButton, SIGNAL(clicked()), this, SLOT(dial()));
connect(middleButton, SIGNAL(clicked()), this, SLOT(findContact()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *missedLayout = new QHBoxLayout;
missedLayout->addWidget(missedCalls);
missedLayout->addWidget(missedCallsNbr);
QHBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addWidget(leftButton);
btnLayout->addWidget(middleButton);
btnLayout->addWidget(rightButton);
QVBoxLayout *listLayout = new QVBoxLayout;
listLayout->addLayout(missedLayout);
listLayout->addWidget(viewArea);
listLayout->addLayout(btnLayout);
setLayout(listLayout);
}
TestModelUi::~TestModelUi()
{
delete fetchRequest;
delete filterRequest;
delete leftButton;
delete rightButton;
delete list;
delete textEdit;
delete viewArea;
delete dialog;
delete manager;
}
void TestModelUi::populateContacts()
{
QStringList nameFirst;
nameFirst << "Adam" << "Adrianna" << "Alex" << "Brett" << "Bob"
<< "Carina" << "Christina" << "Carl" << "Daniel" << "Denise"
<< "Eric" << "Fred" << "Mario" << "William" << "Zandi";
QStringList nameLast;
nameLast << "Citizen" << "Civilian" << "Doe" << "Generic" << "Public" << "Unlikely";
QStringList phoneFirst;
phoneFirst << "111" << "222" << "333" << "444" << "555" << "666" << "777" << "888"
<< "123" << "234" << "345" << "456" << "567" << "678" << "789";
QStringList phoneLast;
phoneLast << "9876" << "8765" << "7654" << "6543" << "5432" << "4321";
QStringList emailFirst;
emailFirst << "testPersonOne" << "testPersonTwo" << "testPersonThree" << "testPersonFour" << "testPersonFive"
<< "testPersonSix" << "testPersonSeven" << "testPersonEight" << "testPersonNine" << "testPersonTen"
<< "testPersonEleven" << "testPersonTwelve" << "testPersonThirteen" << "testPersonFourteen" << "testPersonFifteen";
QStringList emailLast;
emailLast << "@test1.nokia.com" << "@test2.nokia.com" << "@test3.nokia.com"
<< "@test4.nokia.com" << "@test5.nokia.com" << "@test6.nokia.com";
QStringList avatarFirst;
avatarFirst << "party" << "celebration" << "happyface" << "grinning" << "smile"
<< "laughing" << "dance" << "serious" << "angry" << "sadface"
<< "tree" << "mountain" << "ocean" << "city" << "bridge";
QStringList avatarLast;
avatarLast << ".png" << ".bmp" << ".jpg" << ".gif" << ".avi" << ".mpg";
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 6; j++) {
QContact c;
QContactName n;
QContactPhoneNumber p;
QContactEmailAddress e;
QContactAvatar a;
n.setFirst(nameFirst.at(i));
n.setLast(nameLast.at(j));
p.setNumber(phoneFirst.at(i) + phoneLast.at(j));
e.setEmailAddress(emailFirst.at(i) + emailLast.at(j));
a.setAvatar(avatarFirst.at(i) + avatarLast.at(j));
c.saveDetail(&n);
c.saveDetail(&p);
c.saveDetail(&e);
c.saveDetail(&a);
manager->saveContact(&c);
}
}
}
void TestModelUi::dataAvailable(QContactFetchRequest* request, bool appendOnly)
{
Q_UNUSED(appendOnly);
if (currentState == TestModelUi::WaitingState || request->status() != QContactAbstractRequest::Finished)
return;
QString text;
QList<QContact> requestData = request->contacts();
if (requestData.isEmpty() || requestData.at(0).isEmpty()) {
text += "Unknown Contact";
talkingToDetails = text;
textEdit->setText(talkingToFirstLine + " " + talkingToNumber + "\n\n" + talkingToDetails);
return;
}
QContact curr = request->contacts().at(0);
QList<QContactDetail> allDetails = curr.details();
foreach (const QContactDetail& det, allDetails) {
QString defName = det.definitionName();
text += defName + ":" + "\n";
QList<QString> fieldKeys = det.values().keys();
foreach (const QString& key, fieldKeys) {
text += "\t" + key + " = " + det.value(key) + "\n";
}
text += "\n";
}
talkingToName = curr.displayLabel();
if (currentState == TestModelUi::DialingState) {
talkingToNumber = curr.detail(QContactPhoneNumber::DefinitionName).value(QContactPhoneNumber::FieldNumber);
}
if (!text.isEmpty())
text.chop(1);
talkingToDetails = text;
textEdit->setText(talkingToFirstLine + " " + talkingToName + "\n\n" + talkingToDetails);
}
void TestModelUi::filterResults(QContactFetchRequest* request, bool appendOnly)
{
Q_UNUSED(appendOnly);
QList<QContact> results = request->contacts();
QString text = "Matching Contacts:\n";
for (int i = 0; i < results.size(); i++) {
text += "\n" + results.at(i).displayLabel();
}
textEdit->setText(text);
if (request->status() == QContactAbstractRequest::Finished) {
if (results.isEmpty())
textEdit->setText("Matching Contacts:\n\nNo Matches Found!");
rightButton->setText(tr("Done"));
middleButton->setEnabled(true);
}
}
void TestModelUi::filterFound()
{
QContactFilter fil = dialog->filter();
if (dialog->status() > 0) {
textEdit->setText("Finding Contacts...\n\n");
filterRequest->cancel();
filterRequest->setFilter(fil);
filterRequest->start();
} else {
finishedFindContact();
}
}
void TestModelUi::showFilterDialog()
{
middleButton->setEnabled(false);
textEdit->setText("Selecting search criteria...");
rightButton->setText(tr("Cancel"));
dialog->showDialog();
}
void TestModelUi::findContact()
{
incomingCallTimer->stop();
dialTimer->stop();
answerTimer->stop();
textEdit->setText("Please select a search criteria (click search)");
middleButton->disconnect();
rightButton->disconnect();
leftButton->setEnabled(false);
middleButton->setEnabled(true);
rightButton->setEnabled(true);
middleButton->setText(tr("Search"));
rightButton->setText(tr("Cancel"));
connect(middleButton, SIGNAL(clicked()), this, SLOT(showFilterDialog()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(finishedFindContact()));
viewArea->setCurrentIndex(1);
}
void TestModelUi::finishedFindContact()
{
if (dialog->status() == 0)
return;
hangup();
}
void TestModelUi::dial()
{
incomingCallTimer->stop();
answerTimer->stop();
currentState = TestModelUi::DialingState;
QContactLocalIdFilter fil;
QList<QContactLocalId> fetchIds;
fetchIds << list->currentId();
fil.setIds(fetchIds);
fetchRequest->cancel();
fetchRequest->setFilter(fil);
fetchRequest->start();
talkingToFirstLine = "Dialing";
talkingToName = list->currentData(QContactListModel::DisplayLabelRole).toString();
textEdit->setText(talkingToFirstLine + " " + talkingToName + "\n\n" + talkingToDetails);
leftButton->disconnect();
rightButton->disconnect();
leftButton->setText(tr("Dial"));
rightButton->setText(tr("Cancel"));
connect(rightButton, SIGNAL(clicked()), this, SLOT(hangup()));
leftButton->setEnabled(false);
middleButton->setEnabled(false);
viewArea->setCurrentIndex(1);
dialTimer->start();
}
void TestModelUi::incoming()
{
incomingCallTimer->stop();
dialTimer->stop();
currentState = TestModelUi::IncomingState;
QStringList phoneFirst;
phoneFirst << "111" << "222" << "337" << "944" << "555" << "611" << "777" << "855"
<< "123" << "234" << "385" << "456" << "587" << "688" << "788";
QStringList phoneLast;
phoneLast << "9876" << "8765" << "7654" << "9543" << "5432" << "9321";
int firstIndex = qrand() % phoneFirst.size();
int lastIndex = qrand() % phoneLast.size();
talkingToNumber = phoneFirst.at(firstIndex) + phoneLast.at(lastIndex);
talkingToFirstLine = "Incoming call from";
textEdit->setText(talkingToFirstLine + " " + talkingToNumber + "\n\n" + talkingToDetails);
QContactDetailFilter fil;
fil.setDetailDefinitionName(QContactPhoneNumber::DefinitionName, QContactPhoneNumber::FieldNumber);
fil.setValue(talkingToNumber);
fetchRequest->cancel();
fetchRequest->setFilter(fil);
fetchRequest->start();
leftButton->disconnect();
rightButton->disconnect();
leftButton->setText(tr("Answer"));
rightButton->setText(tr("Hang Up"));
connect(leftButton, SIGNAL(clicked()), this, SLOT(talking()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(hangup()));
leftButton->setEnabled(true);
middleButton->setEnabled(false);
viewArea->setCurrentIndex(1);
answerTimer->start();
}
void TestModelUi::talking()
{
incomingCallTimer->stop();
dialTimer->stop();
answerTimer->stop();
currentState = TestModelUi::TalkingState;
talkingToFirstLine = "Talking to";
textEdit->setText(talkingToFirstLine + " " + (talkingToName.isEmpty() ? talkingToNumber : talkingToName) + "\n\n" + talkingToDetails);
leftButton->disconnect();
rightButton->disconnect();
leftButton->setText(tr("Answer"));
rightButton->setText(tr("Hang Up"));
connect(rightButton, SIGNAL(clicked()), this, SLOT(hangup()));
leftButton->setEnabled(false);
middleButton->setEnabled(false);
}
void TestModelUi::hangup()
{
incomingCallTimer->stop();
dialTimer->stop();
answerTimer->stop();
currentState = TestModelUi::WaitingState;
talkingToName = "";
talkingToNumber = "";
talkingToDetails = "";
talkingToFirstLine = "";
textEdit->setText("");
leftButton->disconnect();
middleButton->disconnect();
rightButton->disconnect();
leftButton->setText(tr("Dial"));
middleButton->setText(tr("Find"));
rightButton->setText(tr("Quit"));
connect(leftButton, SIGNAL(clicked()), this, SLOT(dial()));
connect(middleButton, SIGNAL(clicked()), this, SLOT(findContact()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(close()));
leftButton->setEnabled(true);
middleButton->setEnabled(true);
viewArea->setCurrentIndex(0);
incomingCallTimer->start();
}
void TestModelUi::missedCall()
{
nbrMissedCalls += 1;
missedCallsNbr->setText(QString::number(nbrMissedCalls));
hangup();
}
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) |
Trademarks |
Qt Mobility Project 1.0.0 (Technical Preview) |