1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include "addresswidget.h"
|
---|
43 | #include "adddialog.h"
|
---|
44 |
|
---|
45 | //! [0]
|
---|
46 | AddressWidget::AddressWidget(QWidget *parent)
|
---|
47 | : QTabWidget(parent)
|
---|
48 | {
|
---|
49 | table = new TableModel(this);
|
---|
50 | newAddressTab = new NewAddressTab(this);
|
---|
51 | connect(newAddressTab, SIGNAL(sendDetails(QString,QString)),
|
---|
52 | this, SLOT(addEntry(QString,QString)));
|
---|
53 |
|
---|
54 | addTab(newAddressTab, "Address Book");
|
---|
55 |
|
---|
56 | setupTabs();
|
---|
57 | }
|
---|
58 | //! [0]
|
---|
59 |
|
---|
60 | //! [2]
|
---|
61 | void AddressWidget::addEntry()
|
---|
62 | {
|
---|
63 | AddDialog aDialog;
|
---|
64 |
|
---|
65 | if (aDialog.exec()) {
|
---|
66 | QString name = aDialog.nameText->text();
|
---|
67 | QString address = aDialog.addressText->toPlainText();
|
---|
68 |
|
---|
69 | addEntry(name, address);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | //! [2]
|
---|
73 |
|
---|
74 | //! [3]
|
---|
75 | void AddressWidget::addEntry(QString name, QString address)
|
---|
76 | {
|
---|
77 | QList< QPair<QString, QString> >list = table->getList();
|
---|
78 | QPair<QString, QString> pair(name, address);
|
---|
79 |
|
---|
80 | if (!list.contains(pair)) {
|
---|
81 | table->insertRows(0, 1, QModelIndex());
|
---|
82 |
|
---|
83 | QModelIndex index = table->index(0, 0, QModelIndex());
|
---|
84 | table->setData(index, name, Qt::EditRole);
|
---|
85 | index = table->index(0, 1, QModelIndex());
|
---|
86 | table->setData(index, address, Qt::EditRole);
|
---|
87 | removeTab(indexOf(newAddressTab));
|
---|
88 | } else {
|
---|
89 | QMessageBox::information(this, tr("Duplicate Name"),
|
---|
90 | tr("The name \"%1\" already exists.").arg(name));
|
---|
91 | }
|
---|
92 | }
|
---|
93 | //! [3]
|
---|
94 |
|
---|
95 | //! [4a]
|
---|
96 | void AddressWidget::editEntry()
|
---|
97 | {
|
---|
98 | QTableView *temp = static_cast<QTableView*>(currentWidget());
|
---|
99 | QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
|
---|
100 | QItemSelectionModel *selectionModel = temp->selectionModel();
|
---|
101 |
|
---|
102 | QModelIndexList indexes = selectionModel->selectedRows();
|
---|
103 | QModelIndex index, i;
|
---|
104 | QString name;
|
---|
105 | QString address;
|
---|
106 | int row = -1;
|
---|
107 |
|
---|
108 | foreach (index, indexes) {
|
---|
109 | row = proxy->mapToSource(index).row();
|
---|
110 | i = table->index(row, 0, QModelIndex());
|
---|
111 | QVariant varName = table->data(i, Qt::DisplayRole);
|
---|
112 | name = varName.toString();
|
---|
113 |
|
---|
114 | i = table->index(row, 1, QModelIndex());
|
---|
115 | QVariant varAddr = table->data(i, Qt::DisplayRole);
|
---|
116 | address = varAddr.toString();
|
---|
117 | }
|
---|
118 | //! [4a]
|
---|
119 |
|
---|
120 | //! [4b]
|
---|
121 | AddDialog aDialog;
|
---|
122 | aDialog.setWindowTitle(tr("Edit a Contact"));
|
---|
123 |
|
---|
124 | aDialog.nameText->setReadOnly(true);
|
---|
125 | aDialog.nameText->setText(name);
|
---|
126 | aDialog.addressText->setText(address);
|
---|
127 |
|
---|
128 | if (aDialog.exec()) {
|
---|
129 | QString newAddress = aDialog.addressText->toPlainText();
|
---|
130 | if (newAddress != address) {
|
---|
131 | i = table->index(row, 1, QModelIndex());
|
---|
132 | table->setData(i, newAddress, Qt::EditRole);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | //! [4b]
|
---|
137 |
|
---|
138 | //! [5]
|
---|
139 | void AddressWidget::removeEntry()
|
---|
140 | {
|
---|
141 | QTableView *temp = static_cast<QTableView*>(currentWidget());
|
---|
142 | QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
|
---|
143 | QItemSelectionModel *selectionModel = temp->selectionModel();
|
---|
144 |
|
---|
145 | QModelIndexList indexes = selectionModel->selectedRows();
|
---|
146 | QModelIndex index;
|
---|
147 |
|
---|
148 | foreach (index, indexes) {
|
---|
149 | int row = proxy->mapToSource(index).row();
|
---|
150 | table->removeRows(row, 1, QModelIndex());
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (table->rowCount(QModelIndex()) == 0) {
|
---|
154 | insertTab(0, newAddressTab, "Address Book");
|
---|
155 | }
|
---|
156 | }
|
---|
157 | //! [5]
|
---|
158 |
|
---|
159 | //! [1]
|
---|
160 | void AddressWidget::setupTabs()
|
---|
161 | {
|
---|
162 | QStringList groups;
|
---|
163 | groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ";
|
---|
164 |
|
---|
165 | for (int i = 0; i < groups.size(); ++i) {
|
---|
166 | QString str = groups.at(i);
|
---|
167 |
|
---|
168 | proxyModel = new QSortFilterProxyModel(this);
|
---|
169 | proxyModel->setSourceModel(table);
|
---|
170 | proxyModel->setDynamicSortFilter(true);
|
---|
171 |
|
---|
172 | QTableView *tableView = new QTableView;
|
---|
173 | tableView->setModel(proxyModel);
|
---|
174 | tableView->setSortingEnabled(true);
|
---|
175 | tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
---|
176 | tableView->horizontalHeader()->setStretchLastSection(true);
|
---|
177 | tableView->verticalHeader()->hide();
|
---|
178 | tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
---|
179 | tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
180 |
|
---|
181 | QString newStr = QString("^[%1].*").arg(str);
|
---|
182 |
|
---|
183 | proxyModel->setFilterRegExp(QRegExp(newStr, Qt::CaseInsensitive));
|
---|
184 | proxyModel->setFilterKeyColumn(0);
|
---|
185 | proxyModel->sort(0, Qt::AscendingOrder);
|
---|
186 |
|
---|
187 | connect(tableView->selectionModel(),
|
---|
188 | SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
|
---|
189 | this, SIGNAL(selectionChanged(QItemSelection)));
|
---|
190 |
|
---|
191 | addTab(tableView, str);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | //! [1]
|
---|
195 |
|
---|
196 | //! [7]
|
---|
197 | void AddressWidget::readFromFile(QString fileName)
|
---|
198 | {
|
---|
199 | QFile file(fileName);
|
---|
200 |
|
---|
201 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
202 | QMessageBox::information(this, tr("Unable to open file"),
|
---|
203 | file.errorString());
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | QList< QPair<QString, QString> > pairs = table->getList();
|
---|
208 | QDataStream in(&file);
|
---|
209 | in >> pairs;
|
---|
210 |
|
---|
211 | if (pairs.isEmpty()) {
|
---|
212 | QMessageBox::information(this, tr("No contacts in file"),
|
---|
213 | tr("The file you are attempting to open contains no contacts."));
|
---|
214 | } else {
|
---|
215 | for (int i=0; i<pairs.size(); ++i) {
|
---|
216 | QPair<QString, QString> p = pairs.at(i);
|
---|
217 | addEntry(p.first, p.second);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | //! [7]
|
---|
222 |
|
---|
223 | //! [6]
|
---|
224 | void AddressWidget::writeToFile(QString fileName)
|
---|
225 | {
|
---|
226 | QFile file(fileName);
|
---|
227 |
|
---|
228 | if (!file.open(QIODevice::WriteOnly)) {
|
---|
229 | QMessageBox::information(this, tr("Unable to open file"), file.errorString());
|
---|
230 | return;
|
---|
231 | }
|
---|
232 |
|
---|
233 | QList< QPair<QString, QString> > pairs = table->getList();
|
---|
234 | QDataStream out(&file);
|
---|
235 | out << pairs;
|
---|
236 | }
|
---|
237 | //! [6]
|
---|