diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-04-22 04:47:24 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-04-22 04:47:24 (GMT) |
commit | 2366667fc97eb6a56203b2dd7dac776ff4164abd (patch) | |
tree | b2acb6cc6bfe475d7e619e4788973b61fff775e0 /examples/declarative/support | |
parent | 2c762f3b8b284a7c6dc0c499b7052013bad5b707 (diff) | |
download | Qt-2366667fc97eb6a56203b2dd7dac776ff4164abd.zip Qt-2366667fc97eb6a56203b2dd7dac776ff4164abd.tar.gz Qt-2366667fc97eb6a56203b2dd7dac776ff4164abd.tar.bz2 |
Initial import of kinetic-dui branch from the old kinetic
Diffstat (limited to 'examples/declarative/support')
-rw-r--r-- | examples/declarative/support/contact.cpp | 83 | ||||
-rw-r--r-- | examples/declarative/support/contact.h | 141 | ||||
-rw-r--r-- | examples/declarative/support/contactmodel.cpp | 159 | ||||
-rw-r--r-- | examples/declarative/support/contactmodel.h | 55 | ||||
-rw-r--r-- | examples/declarative/support/support.pro | 12 |
5 files changed, 450 insertions, 0 deletions
diff --git a/examples/declarative/support/contact.cpp b/examples/declarative/support/contact.cpp new file mode 100644 index 0000000..7d90fc4 --- /dev/null +++ b/examples/declarative/support/contact.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved. +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include "contact.h" +#include "qmltypes.h" + +QML_DEFINE_TYPE(Contact,Contact); +Contact::Contact() : QObject(0) +{ + m_firstName = "John"; + m_lastName = "Smith"; + m_portraitFile = "contact.png"; + m_company = "Trollkia"; + m_emails << "smith@trollkia.com" << "john45@gmail.com"; + + m_numbers << new PhoneNumber; + m_numbers << new PhoneNumber; + m_numbers << new PhoneNumber; + + m_numbers.at(0)->setType(PhoneNumber::HomePhone); + m_numbers.at(0)->setNumber("35412451"); + + m_numbers.at(1)->setType(PhoneNumber::BusinessPhone); + m_numbers.at(1)->setNumber("33424994"); + + m_numbers.at(2)->setType(PhoneNumber::MobilePhone); + m_numbers.at(2)->setNumber("0424655137"); + + m_addresses << new Address; + m_addresses << new Address; + m_addresses << new Address; + m_addresses.at(0)->setNumber(13); + m_addresses.at(0)->setStreet("Blackhill Cr"); + m_addresses.at(0)->setCountry("Australia"); + m_addresses.at(1)->setNumber(116); + m_addresses.at(1)->setStreet("Sandankerveien"); + m_addresses.at(1)->setCountry("Norway"); + m_addresses.at(2)->setNumber(92); + m_addresses.at(2)->setStreet("Elizibeth St"); + m_addresses.at(2)->setCountry("Australia"); +} + +void Contact::addNumber(PhoneNumber *newNumber) +{ + + m_numbers << newNumber; + emit numbersChanged(); +} + +void Contact::addAddress(Address *newAddress) +{ + m_addresses << newAddress; + emit addressesChanged(); +} + +void Contact::addEmail(QString &newEmail) +{ + + m_emails << newEmail; + emit emailsChanged(); +} + +QML_DEFINE_TYPE(Address,Address); +Address::Address() +: _number(0) +{ +} + +QML_DEFINE_TYPE(PhoneNumber, PhoneNumber); +PhoneNumber::PhoneNumber() +: _type(HomePhone) +{ +} diff --git a/examples/declarative/support/contact.h b/examples/declarative/support/contact.h new file mode 100644 index 0000000..c403f8b --- /dev/null +++ b/examples/declarative/support/contact.h @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved. +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef CONTACT_H +#define CONTACT_H + +#include <qml.h> +#include <QtGui> + + +class Address : public QObject +{ + Q_OBJECT +public: + Address(); + + Q_PROPERTY(int number READ number WRITE setNumber NOTIFY changed); + Q_PROPERTY(QString street READ street WRITE setStreet NOTIFY changed); + Q_PROPERTY(QString country READ country WRITE setCountry NOTIFY changed); + + int number() const { return _number; } + void setNumber(int n) { _number = n; emit changed(); } + + QString street() const { return _street; } + void setStreet(const QString &s) { _street = s; emit changed(); } + + QString country() const { return _country; } + void setCountry(const QString &c) { _country = c; emit changed(); } + +signals: + void changed(); + +private: + int _number; + QString _street; + QString _country; +}; +QML_DECLARE_TYPE(Address); + +class PhoneNumber : public QObject +{ + Q_OBJECT + Q_ENUMS(PhoneType) +public: + PhoneNumber(); + + enum PhoneType { + HomePhone, + BusinessPhone, + MobilePhone + }; + + Q_PROPERTY(QString number READ number WRITE setNumber NOTIFY changed); + Q_PROPERTY(PhoneType type READ type WRITE setType NOTIFY changed); + + QString number() const { return _number; } + void setNumber(QString n) { _number = n; emit changed(); } + + PhoneType type() const { return _type; } + void setType(PhoneType type) { _type = type; emit changed(); } + +signals: + void changed(); + +private: + QString _number; + PhoneType _type; +}; +QML_DECLARE_TYPE(PhoneNumber); + +class Contact : public QObject +{ + Q_OBJECT +public: + Contact(); + + Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY nameChanged); + QString firstName() const { return m_firstName; } + + Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY nameChanged); + QString lastName() const { return m_lastName; } + + Q_PROPERTY(QString portraitFile READ portraitFile WRITE setPortraitFile NOTIFY portraitChanged); + QString portraitFile() const { return m_portraitFile; } + + Q_PROPERTY(QString company READ company WRITE setCompany NOTIFY companyChanged); + QString company() const { return m_company; } + + Q_PROPERTY(QStringList emails READ emails WRITE setEmails NOTIFY emailsChanged); + QStringList emails() const { return m_emails; } + + Q_PROPERTY(QList<Address *>* addresses READ addresses); + QList<Address *>* addresses() { return &m_addresses; } + + Q_PROPERTY(QList<PhoneNumber *>* numbers READ numbers); + QList<PhoneNumber *>* numbers() { return &m_numbers; } + + + void addEmail(QString&); + void addAddress(Address*); + void addNumber(PhoneNumber*); + +public slots: + void setFirstName(const QString &name) { m_firstName = name; emit nameChanged(); } + void setLastName(const QString &name) { m_lastName = name; emit nameChanged(); } + void setPortraitFile(const QString &portraitFile) { m_portraitFile = portraitFile; emit portraitChanged(); } + void setCompany(const QString &company) { m_company = company; emit companyChanged(); } + void setEmails(const QStringList &emails) { m_emails = emails; emit emailsChanged(); } + +signals: + void nameChanged(); + void portraitChanged(); + void companyChanged(); + void emailsChanged(); + void numbersChanged(); + void addressesChanged(); + +private: + QString m_firstName; + QString m_lastName; + QString m_portraitFile; + + QString m_company; + + QList<Address *> m_addresses; + QList<PhoneNumber *>m_numbers; + QStringList m_emails; +}; +QML_DECLARE_TYPE(Contact); + +#endif diff --git a/examples/declarative/support/contactmodel.cpp b/examples/declarative/support/contactmodel.cpp new file mode 100644 index 0000000..efe108fb --- /dev/null +++ b/examples/declarative/support/contactmodel.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved. +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#include "contactmodel.h" +#include "qmltypes.h" + +ContactModel::ContactModel(QObject *parent) : QListModelInterface(parent) +{ + QFile f("../contacts/contacts.txt"); + f.open(QIODevice::ReadOnly); + QTextStream ts(&f); + QString text = ts.readLine(); + while(!text.isEmpty()) { + Contact *c = new Contact; + QStringList list = text.split(" "); + c->setFirstName(list[0]); + c->setLastName(list[1]); + for (int i = 2; i < list.count(); ++i) + c->addEmail(list[i]); + //contactList.append(c); + insertContact(c); + + text = ts.readLine(); + } + f.close(); +} + +ContactModel::~ContactModel() +{ + while (!contactList.isEmpty()) { + Contact *c = contactList.takeFirst(); + delete c; + } +} + +int ContactModel::count() const +{ + return contactList.count(); +} + +QHash<int,QVariant> ContactModel::data(int index, const QList<int> &roles) const +{ + QHash<int,QVariant> returnHash; + + for (int i = 0; i < roles.size(); ++i) { + int role = roles.at(i); + QVariant info; + switch(role) { + case PortraitRole: + info = "contact.png"; + break; + case FirstNameRole: + info = contactList.at(index)->firstName(); + break; + case LastNameRole: + info = contactList.at(index)->lastName(); + break; + case CompanyRole: + info = contactList.at(index)->company(); + break; + case EmailsRole: + info = contactList.at(index)->emails(); + break; + case AddressesRole: + //returns QVariant BOOL + info = QVariant::fromValue(contactList.at(index)->addresses()); + break; + case NumbersRole: + info = QVariant::fromValue(contactList.at(index)->numbers()); + break; + default: + break; + } + returnHash.insert(role, info); + } + + return returnHash; +} + +QString ContactModel::toString(int role) const +{ + switch(role) { + case PortraitRole: + return "portrait"; + case FirstNameRole: + return "firstName"; + case LastNameRole: + return "lastName"; + case CompanyRole: + return "company"; + case EmailsRole: + return "emails"; + case AddressesRole: + return "addresses"; + case NumbersRole: + return "numbers"; + default: + return ""; + } +} + +QList<int> ContactModel::roles() const +{ + return QList<int>() << PortraitRole << FirstNameRole << LastNameRole << CompanyRole << EmailsRole << AddressesRole << NumbersRole; +} + +void ContactModel::deleteContact(int index) +{ + delete contactList.takeAt(index); + emit itemsRemoved(index, 1); +} + +int ContactModel::insertContact(Contact *contact) +{ + int index = 0; + QString fullName = contact->lastName(); + index = findIndex(fullName); + contactList.insert(index, contact); + emit itemsInserted(index, 1); + return index; +} + + +//search - binary search algorithm lastname only + +int ContactModel::findIndex(QString &searchName) const +{ + int start = 0; + int end = contactList.size()-1; + int middle = 0; + QString middleString; + + while (start <= end) + { + middle = (start+end)/2; + middleString = contactList.at(middle)->lastName(); + if (isAfter(searchName, middleString) < 0) start = middle+1; + else if( isAfter(middleString, searchName) < 0) end = middle-1; + else return middle; + } + return start; +} + +int ContactModel::isAfter(QString &name1, QString &name2) const +{ + //if c1 is after c2 alphabetically, return positive + int compString = QString::compare(name1, name2, Qt::CaseInsensitive); + return -compString; +} diff --git a/examples/declarative/support/contactmodel.h b/examples/declarative/support/contactmodel.h new file mode 100644 index 0000000..e262358 --- /dev/null +++ b/examples/declarative/support/contactmodel.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved. +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $TROLLTECH_DUAL_LICENSE$ +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ + +#ifndef _CONTACTMODEL_H_ +#define _CONTACTMODEL_H_ + +#include <qlistmodelinterface.h> +#include "contact.h" + +class ContactModel : public QListModelInterface +{ + Q_OBJECT +public: + ContactModel(QObject *parent = 0); + ~ContactModel(); + + enum Roles { + PortraitRole, + FirstNameRole, + LastNameRole, + CompanyRole, + EmailsRole, + AddressesRole, + NumbersRole + }; + + int count() const; + + QHash<int,QVariant> data(int index, const QList<int> &roles) const; + QList<int> roles() const; + + + QString toString(int role) const; + + void deleteContact(int index); + int insertContact(Contact *contact); + + int isAfter(QString &name1, QString &name2) const; + int findIndex(QString &searchName) const; + +private: + QList<Contact*> contactList; +}; + +#endif diff --git a/examples/declarative/support/support.pro b/examples/declarative/support/support.pro new file mode 100644 index 0000000..1da1a28 --- /dev/null +++ b/examples/declarative/support/support.pro @@ -0,0 +1,12 @@ +TEMPLATE = lib +TARGET = QtFxSupport +DEPENDPATH += . +INCLUDEPATH += . +MOC_DIR = .moc +OBJECTS_DIR = .obj +DESTDIR = ../../lib +QT += script declarative + +HEADERS += contact.h contactmodel.h +SOURCES += contact.cpp contactmodel.cpp + |