author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
#include "addresswidget.h" |
|
44 |
#include "adddialog.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
AddressWidget::AddressWidget(QWidget *parent) |
|
48 |
: QTabWidget(parent) |
|
49 |
{ |
|
50 |
table = new TableModel(this); |
|
51 |
newAddressTab = new NewAddressTab(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
connect(newAddressTab, SIGNAL(sendDetails(QString,QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
this, SLOT(addEntry(QString,QString))); |
0 | 54 |
|
55 |
addTab(newAddressTab, "Address Book"); |
|
56 |
||
57 |
setupTabs(); |
|
58 |
} |
|
59 |
//! [0] |
|
60 |
||
61 |
//! [2] |
|
62 |
void AddressWidget::addEntry() |
|
63 |
{ |
|
64 |
AddDialog aDialog; |
|
65 |
||
66 |
if (aDialog.exec()) { |
|
67 |
QString name = aDialog.nameText->text(); |
|
68 |
QString address = aDialog.addressText->toPlainText(); |
|
69 |
||
70 |
addEntry(name, address); |
|
71 |
} |
|
72 |
} |
|
73 |
//! [2] |
|
74 |
||
75 |
//! [3] |
|
76 |
void AddressWidget::addEntry(QString name, QString address) |
|
77 |
{ |
|
78 |
QList< QPair<QString, QString> >list = table->getList(); |
|
79 |
QPair<QString, QString> pair(name, address); |
|
80 |
||
81 |
if (!list.contains(pair)) { |
|
82 |
table->insertRows(0, 1, QModelIndex()); |
|
83 |
||
84 |
QModelIndex index = table->index(0, 0, QModelIndex()); |
|
85 |
table->setData(index, name, Qt::EditRole); |
|
86 |
index = table->index(0, 1, QModelIndex()); |
|
87 |
table->setData(index, address, Qt::EditRole); |
|
88 |
removeTab(indexOf(newAddressTab)); |
|
89 |
} else { |
|
90 |
QMessageBox::information(this, tr("Duplicate Name"), |
|
91 |
tr("The name \"%1\" already exists.").arg(name)); |
|
92 |
} |
|
93 |
} |
|
94 |
//! [3] |
|
95 |
||
96 |
//! [4a] |
|
97 |
void AddressWidget::editEntry() |
|
98 |
{ |
|
99 |
QTableView *temp = static_cast<QTableView*>(currentWidget()); |
|
100 |
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model()); |
|
101 |
QItemSelectionModel *selectionModel = temp->selectionModel(); |
|
102 |
||
103 |
QModelIndexList indexes = selectionModel->selectedRows(); |
|
104 |
QModelIndex index, i; |
|
105 |
QString name; |
|
106 |
QString address; |
|
107 |
int row = -1; |
|
108 |
||
109 |
foreach (index, indexes) { |
|
110 |
row = proxy->mapToSource(index).row(); |
|
111 |
i = table->index(row, 0, QModelIndex()); |
|
112 |
QVariant varName = table->data(i, Qt::DisplayRole); |
|
113 |
name = varName.toString(); |
|
114 |
||
115 |
i = table->index(row, 1, QModelIndex()); |
|
116 |
QVariant varAddr = table->data(i, Qt::DisplayRole); |
|
117 |
address = varAddr.toString(); |
|
118 |
} |
|
119 |
//! [4a] |
|
120 |
||
121 |
//! [4b] |
|
122 |
AddDialog aDialog; |
|
123 |
aDialog.setWindowTitle(tr("Edit a Contact")); |
|
124 |
||
125 |
aDialog.nameText->setReadOnly(true); |
|
126 |
aDialog.nameText->setText(name); |
|
127 |
aDialog.addressText->setText(address); |
|
128 |
||
129 |
if (aDialog.exec()) { |
|
130 |
QString newAddress = aDialog.addressText->toPlainText(); |
|
131 |
if (newAddress != address) { |
|
132 |
i = table->index(row, 1, QModelIndex()); |
|
133 |
table->setData(i, newAddress, Qt::EditRole); |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
//! [4b] |
|
138 |
||
139 |
//! [5] |
|
140 |
void AddressWidget::removeEntry() |
|
141 |
{ |
|
142 |
QTableView *temp = static_cast<QTableView*>(currentWidget()); |
|
143 |
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model()); |
|
144 |
QItemSelectionModel *selectionModel = temp->selectionModel(); |
|
145 |
||
146 |
QModelIndexList indexes = selectionModel->selectedRows(); |
|
147 |
QModelIndex index; |
|
148 |
||
149 |
foreach (index, indexes) { |
|
150 |
int row = proxy->mapToSource(index).row(); |
|
151 |
table->removeRows(row, 1, QModelIndex()); |
|
152 |
} |
|
153 |
||
154 |
if (table->rowCount(QModelIndex()) == 0) { |
|
155 |
insertTab(0, newAddressTab, "Address Book"); |
|
156 |
} |
|
157 |
} |
|
158 |
//! [5] |
|
159 |
||
160 |
//! [1] |
|
161 |
void AddressWidget::setupTabs() |
|
162 |
{ |
|
163 |
QStringList groups; |
|
164 |
groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ"; |
|
165 |
||
166 |
for (int i = 0; i < groups.size(); ++i) { |
|
167 |
QString str = groups.at(i); |
|
168 |
||
169 |
proxyModel = new QSortFilterProxyModel(this); |
|
170 |
proxyModel->setSourceModel(table); |
|
171 |
proxyModel->setDynamicSortFilter(true); |
|
172 |
||
173 |
QTableView *tableView = new QTableView; |
|
174 |
tableView->setModel(proxyModel); |
|
175 |
tableView->setSortingEnabled(true); |
|
176 |
tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
177 |
tableView->horizontalHeader()->setStretchLastSection(true); |
|
178 |
tableView->verticalHeader()->hide(); |
|
179 |
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
|
180 |
tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
|
181 |
||
182 |
QString newStr = QString("^[%1].*").arg(str); |
|
183 |
||
184 |
proxyModel->setFilterRegExp(QRegExp(newStr, Qt::CaseInsensitive)); |
|
185 |
proxyModel->setFilterKeyColumn(0); |
|
186 |
proxyModel->sort(0, Qt::AscendingOrder); |
|
187 |
||
188 |
connect(tableView->selectionModel(), |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
SIGNAL(selectionChanged(QItemSelection,QItemSelection)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
this, SIGNAL(selectionChanged(QItemSelection))); |
0 | 191 |
|
192 |
addTab(tableView, str); |
|
193 |
} |
|
194 |
} |
|
195 |
//! [1] |
|
196 |
||
197 |
//! [7] |
|
198 |
void AddressWidget::readFromFile(QString fileName) |
|
199 |
{ |
|
200 |
QFile file(fileName); |
|
201 |
||
202 |
if (!file.open(QIODevice::ReadOnly)) { |
|
203 |
QMessageBox::information(this, tr("Unable to open file"), |
|
204 |
file.errorString()); |
|
205 |
return; |
|
206 |
} |
|
207 |
||
208 |
QList< QPair<QString, QString> > pairs = table->getList(); |
|
209 |
QDataStream in(&file); |
|
210 |
in >> pairs; |
|
211 |
||
212 |
if (pairs.isEmpty()) { |
|
213 |
QMessageBox::information(this, tr("No contacts in file"), |
|
214 |
tr("The file you are attempting to open contains no contacts.")); |
|
215 |
} else { |
|
216 |
for (int i=0; i<pairs.size(); ++i) { |
|
217 |
QPair<QString, QString> p = pairs.at(i); |
|
218 |
addEntry(p.first, p.second); |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
//! [7] |
|
223 |
||
224 |
//! [6] |
|
225 |
void AddressWidget::writeToFile(QString fileName) |
|
226 |
{ |
|
227 |
QFile file(fileName); |
|
228 |
||
229 |
if (!file.open(QIODevice::WriteOnly)) { |
|
230 |
QMessageBox::information(this, tr("Unable to open file"), file.errorString()); |
|
231 |
return; |
|
232 |
} |
|
233 |
||
234 |
QList< QPair<QString, QString> > pairs = table->getList(); |
|
235 |
QDataStream out(&file); |
|
236 |
out << pairs; |
|
237 |
} |
|
238 |
//! [6] |