diff options
author | Lauro Neto <lauro.neto@openbossa.org> | 2011-09-09 17:25:22 -0300 |
---|---|---|
committer | Lauro Neto <lauro.neto@openbossa.org> | 2011-09-14 17:17:28 -0300 |
commit | 4cd3e5759c4d84e449d4aad219a63bcce464cf86 (patch) | |
tree | 050aebb4c8c22e94adfe30038c7b4da8a1495bc2 /mobility/samplephonebook | |
parent | c12d626c45a333125b541e181bb905e3e67b9d83 (diff) |
Add samplephonebook in QML
Diffstat (limited to 'mobility/samplephonebook')
-rw-r--r-- | mobility/samplephonebook/ContactEdit.qml | 116 | ||||
-rw-r--r-- | mobility/samplephonebook/MainPage.qml | 47 | ||||
-rw-r--r-- | mobility/samplephonebook/SelectBackend.qml | 41 | ||||
-rw-r--r-- | mobility/samplephonebook/main.qml | 10 | ||||
-rw-r--r-- | mobility/samplephonebook/qmlsamplephonebook.py | 274 |
5 files changed, 488 insertions, 0 deletions
diff --git a/mobility/samplephonebook/ContactEdit.qml b/mobility/samplephonebook/ContactEdit.qml new file mode 100644 index 0000000..295f32b --- /dev/null +++ b/mobility/samplephonebook/ContactEdit.qml @@ -0,0 +1,116 @@ + +import QtQuick 1.1 +import com.nokia.meego 1.0 + +Page { + id: contactEdit + anchors.margins: UiConstants.DefaultMargin + orientationLock: PageOrientation.LockPortrait + + + Flickable { + anchors.fill: parent + contentHeight: textFields.height + Column { + id: textFields + anchors.fill: parent + Label { + text: "Name" + } + TextField { + id: fieldName + anchors.left: parent.left + anchors.right: parent.right + text: manager.contactData[0] + } + Label { + text: "Phone" + } + TextField { + id: fieldPhone + text: manager.contactData[1] + anchors.left: parent.left + anchors.right: parent.right + } + Label { + text: "Email" + } + TextField { + id: fieldEmail + text: (manager.emailEnabled ? manager.contactData[2] : "<not supported>") + readOnly: !manager.emailEnabled + anchors.left: parent.left + anchors.right: parent.right + } + Label { + text: "Address" + } + TextField { + id: fieldAddress + text: (manager.addressEnabled ? manager.contactData[3] : "<not supported>") + readOnly: !manager.addressEnabled + anchors.left: parent.left + anchors.right: parent.right + } + + } + } + Dialog { + id: errorDialog + visualParent: contactEdit + title: Item { + id: titleField + height: errorDialog.platformStyle.titleBarHeight + width: parent.width + Label { + id: titleLabel + anchors.verticalCenter: titleField.verticalCenter + font.capitalization: Font.MixedCase + color: "white" + text: "Error" + } + } + content:Item { + id: name + height: childrenRect.height + Text { + id: text + font.pixelSize: 22 + color: "white" + text: manager.errorMessage + } + } + buttons: ButtonRow { + platformStyle: ButtonStyle { } + anchors.horizontalCenter: parent.horizontalCenter + Button {id: b1; text: "Edit fields"; onClicked: errorDialog.accept()} + Button {id: b2; text: "Cancel entry"; onClicked: errorDialog.reject()} + } + } + + tools: ToolBarLayout { + id: mainTools + ToolButton { + text: "Save" + onClicked: { + console.log("Save contact"); + if (!manager.saveContact(fieldName.text, fieldPhone.text, fieldEmail.text, fieldAddress.text)) { + errorDialog.open(); + } else { + manager.selectContact(-1); + pageStack.pop(); + } + } + } + ToolButton { + text: "Cancel" + onClicked: { + console.log("Cancel edit/add") + manager.selectContact(-1) + pageStack.pop() + } + } + } + + +} diff --git a/mobility/samplephonebook/MainPage.qml b/mobility/samplephonebook/MainPage.qml new file mode 100644 index 0000000..bbbef5b --- /dev/null +++ b/mobility/samplephonebook/MainPage.qml @@ -0,0 +1,47 @@ + + +import QtQuick 1.1 +import com.nokia.meego 1.0 +import com.nokia.extras 1.1 + +Page { + id: mainPage + anchors.margins: UiConstants.DefaultMargin + orientationLock: PageOrientation.LockPortrait + + Flickable { + anchors.fill: parent + + ButtonColumn { + Repeater { + model: manager.contactsNames + Button { + text: modelData + onClicked: { + console.log("Editing existing contact") + manager.selectContact(index) + pageStack.push(Qt.createComponent("ContactEdit.qml")) + } + } + } + } + } + + tools: ToolBarLayout { + id: mainTools + ToolButton { + text: "Add..." + onClicked: { + console.log("Add new contact") + pageStack.push(Qt.createComponent("ContactEdit.qml")) + } + } + ToolButton { + text: "Select backend..." + onClicked: { + pageStack.push(Qt.createComponent("SelectBackend.qml")) + } + } + } + +} diff --git a/mobility/samplephonebook/SelectBackend.qml b/mobility/samplephonebook/SelectBackend.qml new file mode 100644 index 0000000..ec927e8 --- /dev/null +++ b/mobility/samplephonebook/SelectBackend.qml @@ -0,0 +1,41 @@ + +import QtQuick 1.1 +import com.nokia.meego 1.0 + +Page { + id: selectBackend + anchors.margins: UiConstants.DefaultMargin + orientationLock: PageOrientation.LockPortrait + + + ListView { + anchors.fill: parent + anchors.centerIn: parent + model: manager.availableManagers + delegate: Button { + text: modelData + onClicked: { + manager.selectManager(modelData) + pageStack.pop() + } + } + } + + tools: ToolBarLayout { + id: mainTools + ToolButton { + text: "Select" + onClicked: { + console.log("Selected new backend") + pageStack.pop() + } + } + ToolButton { + text: "Cancel" + onClicked: { + console.log("Cancel edit/add") + pageStack.pop() + } + } + } +} diff --git a/mobility/samplephonebook/main.qml b/mobility/samplephonebook/main.qml new file mode 100644 index 0000000..49eb008 --- /dev/null +++ b/mobility/samplephonebook/main.qml @@ -0,0 +1,10 @@ + +import QtQuick 1.1 +import com.nokia.meego 1.0 + +PageStackWindow { + + id: rootWindow + showStatusBar: false + initialPage: MainPage { } +} diff --git a/mobility/samplephonebook/qmlsamplephonebook.py b/mobility/samplephonebook/qmlsamplephonebook.py new file mode 100644 index 0000000..796c15b --- /dev/null +++ b/mobility/samplephonebook/qmlsamplephonebook.py @@ -0,0 +1,274 @@ +''' + Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + All rights reserved. + Contact: Nokia Corporation (qt-info@nokia.com) + + This file is part of the Qt Mobility Components. + + $QT_BEGIN_LICENSE:LGPL$ + No Commercial Usage + This file contains pre-release code and may not be distributed. + You may use this file in accordance with the terms and conditions + contained in the Technology Preview License Agreement accompanying + this package. + + GNU Lesser General Public License Usage + Alternatively, this file may be used under the terms of the GNU Lesser + General Public License version 2.1 as published by the Free Software + Foundation and appearing in the file LICENSE.LGPL included in the + packaging of this file. Please review the following information to + ensure the GNU Lesser General Public License version 2.1 requirements + will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. + + In addition, as a special exception, Nokia gives you certain additional + rights. These rights are described in the Nokia Qt LGPL Exception + version 1.1, included in the file LGPL_EXCEPTION.txt in this package. + + If you have questions regarding the use of this file, please contact + Nokia at qt-info@nokia.com. +''' + +import os +import sys + +from PySide.QtCore import QObject, Signal, Slot, Property, QUrl, qWarning +from PySide.QtGui import QApplication +from PySide.QtDeclarative import QDeclarativeView +from QtMobility.Contacts import QContactManager, QContactPhoneNumber, QContact +from QtMobility.Contacts import QContactAddress, QContactEmailAddress, QContactType +from QtMobility.Contacts import QContactName + + +class MyContactManager(QObject): + + def __init__(self): + QObject.__init__(self) + + self._availableManagers = {} + self._contacts = [] + + self.initialisedManagers = {} + self.manager = None + self.contactId = 0 + + self._errorMessage = "" + self._emailEnabled = False + self._addressEnabled = True + + availableMgrs = QContactManager.availableManagers() + availableMgrs.remove("invalid") + for managerName in availableMgrs: + params = {} + managerUri = QContactManager.buildUri(managerName, params) + self._availableManagers[managerName] = managerUri + + self.selectManager(self.availableManagers[0]) + + # Email Enabled property + onErrorMessageChanged = Signal() + + @Property(str, notify=onErrorMessageChanged) + def errorMessage(self): + return self._errorMessage + + @errorMessage.setter + def setErrorMessage(self, value): + self._errorMessage = value + self.onErrorMessageChanged.emit() + + # Email Enabled property + onEmailEnabledChanged = Signal() + + @Property("bool", notify=onEmailEnabledChanged) + def emailEnabled(self): + return self._emailEnabled + + @emailEnabled.setter + def setEmailEnabled(self, value): + self._emailEnabled = value + self.onEmailEnabledChanged.emit() + + # Address Enabled property + onAddressEnabledChanged = Signal() + + @Property("bool", notify=onAddressEnabledChanged) + def addressEnabled(self): + return self._addressEnabled + + @addressEnabled.setter + def setAddressEnabled(self, value): + self._addressEnabled = value + self.onAddressEnabledChanged.emit() + + @Property("QStringList", constant=True) + def availableManagers(self): + return self._availableManagers.keys() + + # List of contacts changed property + onContactsChanged = Signal() + + @Property("QStringList", notify=onContactsChanged) + def contactsNames(self): + return [x[0] for x in self._contacts] + + onSelectedContactChanged = Signal() + + def emitContactsChanged(self): + self.onContactsChanged.emit() + + @Property("QStringList", notify=onSelectedContactChanged) + def contactData(self): + if not self.contactId: + print "Trying to get data while no contact selected..." + return ["", "", "", ""] + + print "Getting existing contact data" + + contact = self.manager.contact(self.contactId) + + name = self.manager.synthesizedContactDisplayLabel(contact) + phone = contact.detail(QContactPhoneNumber.DefinitionName).value(QContactPhoneNumber.FieldNumber) + + if self.emailEnabled: + emailObj = contact.detail(QContactEmailAddress.DefinitionName) + email = emailObj.value(QContactEmailAddress.FieldEmailAddress) + else: + email = "" + + if self.addressEnabled: + addressObj = contact.detail(QContactAddress.DefinitionName) + address = addressObj.value(QContactAddress.FieldStreet) + else: + address = "" + + return name, phone, email, address + + @Slot(int) + def selectContact(self, idx): + if idx == -1: + self.contactId = 0 + else: + self.contactId = self._contacts[idx][1] + + @Slot(str) + def selectManager(self, name): + managerUri = self._availableManagers[name] + + # first, check to see if they reselected the same backend. + if self.manager and self.manager.managerUri() == managerUri: + return + + # the change is real. update. + if self.initialisedManagers.has_key(managerUri): + self.manager = self.initialisedManagers[managerUri] + else: + self.manager = QContactManager.fromUri(managerUri) + if self.manager.error(): + print "Failed to open store...." + del self.manager + self.manager = None + return + self.initialisedManagers[managerUri] = self.manager + + defs = self.manager.detailDefinitions(QContactType.TypeContact) + + self.emailEnabled = bool(defs["EmailAddress"]) + self.addressEnabled = bool(defs["Address"]) + + self.updateContactList() + + def updateContactList(self): + self._contacts = [] + + for contact in self.manager.contacts(): + name = self.manager.synthesizedContactDisplayLabel(contact) + self._contacts.append((name, contact.localId())) + self.emitContactsChanged() + + saveEmptyName = Signal() + + @Slot(str, str, str, str, result=bool) + def saveContact(self, name, phone, email, address): + if not self.manager: + qWarning("No manager selected, cannot save") + return + + if self.contactId: + print "Updating existing contact" + contact = self.manager.contact(self.contactId) + else: + print "Creating new contact" + contact = QContact() + + if not name: + self.errorMessage = "Name must not be empty!" + return False + + # Name + if name != self.manager.synthesizedContactDisplayLabel(contact): + saveNameField = self.nameField() + if saveNameField: + nm = QContactName(contact.detail(QContactName().DefinitionName)) + nm.setValue(saveNameField, name) + contact.saveDetail(nm) + + # Phone + phoneObj = QContactPhoneNumber(contact.detail(QContactPhoneNumber.DefinitionName)) + phoneObj.setNumber(phone) + contact.saveDetail(phoneObj) + + # Email + if self.emailEnabled: + emailObj = QContactEmailAddress(contact.detail(QContactEmailAddress.DefinitionName)) + emailObj.setEmailAddress(email) + contact.saveDetail(emailObj) + + # Address + if self.addressEnabled: + addressObj = QContactAddress(contact.detail(QContactAddress.DefinitionName)) + addressObj.setStreet(address) + contact.saveDetail(addressObj) + + contact = self.manager.compatibleContact(contact) + success = self.manager.saveContact(contact) + if not success: + qWarning("Failed to save contact") + + self.updateContactList() + + return True + + + def nameField(self): + # return the field which the name data should be saved in. + if not self.manager: + return "" + + defs = self.manager.detailDefinitions(QContactType.TypeContact) + nameDef = defs[QContactName.DefinitionName] + if QContactName.FieldCustomLabel in nameDef.fields(): + return QContactName.FieldCustomLabel + elif QContactName.FieldFirstName in nameDef.fields(): + return QContactName.FieldFirstName + else: + return "" + +def main(): + app = QApplication([]) + view = QDeclarativeView() + manager = MyContactManager() + context = view.rootContext() + context.setContextProperty("manager", manager) + + url = QUrl('main.qml') + view.setSource(url) + view.showFullScreen() + + app.exec_() + + +if __name__ == '__main__': + main() + + + |