source: trunk/examples/itemviews/addressbook/addresswidget.cpp@ 641

Last change on this file since 641 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 7.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include "addresswidget.h"
44#include "adddialog.h"
45
46//! [0]
47AddressWidget::AddressWidget(QWidget *parent)
48 : QTabWidget(parent)
49{
50 table = new TableModel(this);
51 newAddressTab = new NewAddressTab(this);
52 connect(newAddressTab, SIGNAL(sendDetails(QString,QString)),
53 this, SLOT(addEntry(QString,QString)));
54
55 addTab(newAddressTab, "Address Book");
56
57 setupTabs();
58}
59//! [0]
60
61//! [2]
62void 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]
76void 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]
97void 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]
140void 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]
161void 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(),
189 SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
190 this, SIGNAL(selectionChanged(QItemSelection)));
191
192 addTab(tableView, str);
193 }
194}
195//! [1]
196
197//! [7]
198void 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]
225void 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]
Note: See TracBrowser for help on using the repository browser.