diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-11-18 14:06:19 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-11-18 17:04:40 +0000 |
commit | 1eeeca9d202ceaf11b2935ebe590a85145d33ef5 (patch) | |
tree | 2feffe49d30a95abd5e30bd480ed684aba969b2b /examples | |
parent | 0cfadeb6de542548c45ef485a4452e820cefb401 (diff) |
Overhaul the Contact List example
Some renames, API tweaks, and structural changes. Glue the pieces
(view/model/dialog) together in contactlist.qml.
Change-Id: Id3a31248b391838c6d39f2f0f355e1c35456ccf5
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/quickcontrols2/contactlist/ContactDialog.qml | 49 | ||||
-rw-r--r-- | examples/quickcontrols2/contactlist/ContactForm.ui.qml (renamed from examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml) | 0 | ||||
-rw-r--r-- | examples/quickcontrols2/contactlist/ContactView.ui.qml (renamed from examples/quickcontrols2/contactlist/MainForm.ui.qml) | 38 | ||||
-rw-r--r-- | examples/quickcontrols2/contactlist/contactlist.qml | 22 | ||||
-rw-r--r-- | examples/quickcontrols2/contactlist/contactmodel.cpp | 59 | ||||
-rw-r--r-- | examples/quickcontrols2/contactlist/contactmodel.h | 11 |
6 files changed, 90 insertions, 89 deletions
diff --git a/examples/quickcontrols2/contactlist/ContactDialog.qml b/examples/quickcontrols2/contactlist/ContactDialog.qml index fd0ddbfd..1836ea28 100644 --- a/examples/quickcontrols2/contactlist/ContactDialog.qml +++ b/examples/quickcontrols2/contactlist/ContactDialog.qml @@ -49,52 +49,47 @@ ****************************************************************************/ import QtQuick 2.7 -import QtQuick.Window 2.2 import QtQuick.Controls 2.1 Dialog { id: dialog - x: parent.width / 2 - width / 2 - y: parent.height / 2 - height / 2 - standardButtons: Dialog.Ok | Dialog.Cancel - modal: true - focus: true - - property int lastIndex - property QtObject lastModel - - property alias form: form + property int index: -1 + signal finished(string fullName, string address, string city, string number) - function addContact(model) { + function createContact() { form.fullName.clear(); form.address.clear(); form.city.clear(); form.number.clear(); - lastIndex = -1; - lastModel = model; - dialog.title = qsTr("Add Contact") + dialog.title = qsTr("Add Contact"); + dialog.index = -1; dialog.open(); } - function editContact(model, index) { - form.fullName.text = model.getFullName(index); - form.address.text = model.getAddress(index); - form.city.text = model.getCity(index); - form.number.text = model.getNumber(index); - lastIndex = index; - lastModel = model; + function editContact(index, contact) { + form.fullName.text = contact.fullName; + form.address.text = contact.address; + form.city.text = contact.city; + form.number.text = contact.number; - dialog.title = qsTr("Edit Contact") + dialog.title = qsTr("Edit Contact"); + dialog.index = index; dialog.open(); } - contentItem: ContactDialogForm { + x: parent.width / 2 - width / 2 + y: parent.height / 2 - height / 2 + + focus: true + modal: true + title: qsTr("Add Contact") + standardButtons: Dialog.Ok | Dialog.Cancel + + contentItem: ContactForm { id: form } - onAccepted: lastModel.updateContact(lastIndex, form.fullName.text, - form.address.text, form.city.text, - form.number.text) + onAccepted: finished(form.fullName.text, form.address.text, form.city.text, form.number.text) } diff --git a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml b/examples/quickcontrols2/contactlist/ContactForm.ui.qml index f3397bb0..f3397bb0 100644 --- a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml +++ b/examples/quickcontrols2/contactlist/ContactForm.ui.qml diff --git a/examples/quickcontrols2/contactlist/MainForm.ui.qml b/examples/quickcontrols2/contactlist/ContactView.ui.qml index 1639ecc8..30a72b65 100644 --- a/examples/quickcontrols2/contactlist/MainForm.ui.qml +++ b/examples/quickcontrols2/contactlist/ContactView.ui.qml @@ -49,27 +49,26 @@ ****************************************************************************/ import QtQuick 2.7 -import Backend 1.0 import QtQuick.Controls 2.1 +import Backend 1.0 Page { - id: form + id: page - width: 320 - height: 480 + signal addContact() + signal editContact(int index) + signal removeContact(int index) - property alias button: button - property alias listView: listView + property alias model: contactModel - property ContactDialog dialog: ContactDialog { - } + width: 320 + height: 480 ListView { id: listView + anchors.fill: parent focus: true - anchors.fill: parent - snapMode: ListView.SnapToItem boundsBehavior: Flickable.StopAtBounds section.property: "fullName" @@ -84,27 +83,34 @@ Page { Connections { target: delegate.edit - onClicked: dialog.editContact(listView.model, index) + onClicked: page.editContact(index) } Connections { target: delegate.remove - onClicked: listView.model.removeContact(index) + onClicked: page.removeContact(index) } } model: ContactModel { + id: contactModel } - ScrollBar.vertical: ScrollBar { - } + ScrollBar.vertical: ScrollBar { } } footer: ToolBar { + id: footer + ToolButton { - id: button - text: "Add Contact" + id: addButton + text: qsTr("Add Contact") anchors.right: parent.right + + Connections { + target: addButton + onClicked: page.addContact() + } } } } diff --git a/examples/quickcontrols2/contactlist/contactlist.qml b/examples/quickcontrols2/contactlist/contactlist.qml index 7ace67fa..04382407 100644 --- a/examples/quickcontrols2/contactlist/contactlist.qml +++ b/examples/quickcontrols2/contactlist/contactlist.qml @@ -52,13 +52,29 @@ import QtQuick 2.7 import QtQuick.Controls 2.1 ApplicationWindow { - visible: true + id: window + width: 320 height: 480 + visible: true title: qsTr("Contact List") - MainForm { - button.onClicked: dialog.addContact(listView.model) + ContactDialog { + id: contactDialog + onFinished: { + if (index === -1) + contactView.model.append(fullName, address, city, number) + else + contactView.model.set(index, fullName, address, city, number) + } + } + + ContactView { + id: contactView anchors.fill: parent + + onAddContact: contactDialog.createContact() + onEditContact: contactDialog.editContact(index, model.get(index)) + onRemoveContact: model.remove(index) } } diff --git a/examples/quickcontrols2/contactlist/contactmodel.cpp b/examples/quickcontrols2/contactlist/contactmodel.cpp index 1f435a56..308da2da 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.cpp +++ b/examples/quickcontrols2/contactlist/contactmodel.cpp @@ -85,59 +85,46 @@ QVariant ContactModel::data(const QModelIndex &index, int role) const QHash<int, QByteArray> ContactModel::roleNames() const { - static QHash<int, QByteArray> roleNames { + static const QHash<int, QByteArray> roles { { FullNameRole, "fullName" }, { AddressRole, "address" }, { CityRole, "city" }, { NumberRole, "number" } }; - return roleNames; + return roles; } -void ContactModel::updateContact(int row, - const QString &fullName, - const QString &address, - const QString &city, - const QString &number) +QVariantMap ContactModel::get(int row) const { - if (row >= 0 && row < rowCount()) { - m_contacts.replace(row, { fullName, address, city, number }); - dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); - } else if (row < 0) { - row = 0; - while (row < m_contacts.count() && fullName > m_contacts.at(row).fullName) - ++row; - beginInsertRows(QModelIndex(), row, row); - m_contacts.insert(row, {fullName, address, city, number}); - endInsertRows(); - } + const Contact contact = m_contacts.value(row); + return { {"fullName", contact.fullName}, {"address", contact.address}, {"city", contact.city}, {"number", contact.number} }; } -void ContactModel::removeContact(int row) +void ContactModel::append(const QString &fullName, const QString &address, const QString &city, const QString &number) { - if (row >= 0 && row < rowCount()) { - beginRemoveRows(QModelIndex(), row, row); - m_contacts.removeAt(row); - endRemoveRows(); - } + int row = 0; + while (row < m_contacts.count() && fullName > m_contacts.at(row).fullName) + ++row; + beginInsertRows(QModelIndex(), row, row); + m_contacts.insert(row, {fullName, address, city, number}); + endInsertRows(); } -QString ContactModel::getFullName(int row) const +void ContactModel::set(int row, const QString &fullName, const QString &address, const QString &city, const QString &number) { - return m_contacts.value(row).fullName; -} + if (row < 0 || row >= m_contacts.count()) + return; -QString ContactModel::getAddress(int row) const -{ - return m_contacts.value(row).address; + m_contacts.replace(row, { fullName, address, city, number }); + dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); } -QString ContactModel::getCity(int row) const +void ContactModel::remove(int row) { - return m_contacts.value(row).city; -} + if (row < 0 || row >= m_contacts.count()) + return; -QString ContactModel::getNumber(int row) const -{ - return m_contacts.value(row).number; + beginRemoveRows(QModelIndex(), row, row); + m_contacts.removeAt(row); + endRemoveRows(); } diff --git a/examples/quickcontrols2/contactlist/contactmodel.h b/examples/quickcontrols2/contactlist/contactmodel.h index 2776905f..d09602c4 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.h +++ b/examples/quickcontrols2/contactlist/contactmodel.h @@ -72,13 +72,10 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash<int, QByteArray> roleNames() const; - Q_INVOKABLE QString getFullName(int row) const; - Q_INVOKABLE QString getAddress(int row) const; - Q_INVOKABLE QString getCity(int row) const; - Q_INVOKABLE QString getNumber(int row) const; - - Q_INVOKABLE void updateContact(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); - Q_INVOKABLE void removeContact(int row); + Q_INVOKABLE QVariantMap get(int row) const; + Q_INVOKABLE void append(const QString &fullName, const QString &address, const QString &city, const QString &number); + Q_INVOKABLE void set(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); + Q_INVOKABLE void remove(int row); private: struct Contact { |