2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "adddialog.h"
|
2012-11-21 14:45:13 +00:00
|
|
|
#include "addresswidget.h"
|
|
|
|
|
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [0]
|
|
|
|
AddressWidget::AddressWidget(QWidget *parent)
|
|
|
|
: QTabWidget(parent)
|
|
|
|
{
|
|
|
|
table = new TableModel(this);
|
|
|
|
newAddressTab = new NewAddressTab(this);
|
2015-07-31 11:53:29 +00:00
|
|
|
connect(newAddressTab, &NewAddressTab::sendDetails,
|
|
|
|
this, &AddressWidget::addEntry);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-14 23:42:15 +00:00
|
|
|
addTab(newAddressTab, "Address Book");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
setupTabs();
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [2]
|
2015-07-31 11:53:29 +00:00
|
|
|
void AddressWidget::showAddEntryDialog()
|
2013-03-14 23:42:15 +00:00
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
AddDialog aDialog;
|
|
|
|
|
|
|
|
if (aDialog.exec()) {
|
|
|
|
QString name = aDialog.nameText->text();
|
|
|
|
QString address = aDialog.addressText->toPlainText();
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
addEntry(name, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void AddressWidget::addEntry(QString name, QString address)
|
2013-03-14 23:42:15 +00:00
|
|
|
{
|
2017-09-04 15:09:52 +00:00
|
|
|
if (!table->getContacts().contains({ name, address })) {
|
2011-04-27 10:05:43 +00:00
|
|
|
table->insertRows(0, 1, QModelIndex());
|
|
|
|
|
|
|
|
QModelIndex index = table->index(0, 0, QModelIndex());
|
|
|
|
table->setData(index, name, Qt::EditRole);
|
|
|
|
index = table->index(0, 1, QModelIndex());
|
|
|
|
table->setData(index, address, Qt::EditRole);
|
|
|
|
removeTab(indexOf(newAddressTab));
|
|
|
|
} else {
|
|
|
|
QMessageBox::information(this, tr("Duplicate Name"),
|
|
|
|
tr("The name \"%1\" already exists.").arg(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4a]
|
|
|
|
void AddressWidget::editEntry()
|
|
|
|
{
|
|
|
|
QTableView *temp = static_cast<QTableView*>(currentWidget());
|
|
|
|
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
|
|
|
|
QItemSelectionModel *selectionModel = temp->selectionModel();
|
|
|
|
|
|
|
|
QModelIndexList indexes = selectionModel->selectedRows();
|
|
|
|
QString name;
|
|
|
|
QString address;
|
|
|
|
int row = -1;
|
|
|
|
|
2012-11-21 14:45:13 +00:00
|
|
|
foreach (QModelIndex index, indexes) {
|
2011-04-27 10:05:43 +00:00
|
|
|
row = proxy->mapToSource(index).row();
|
2012-11-21 14:45:13 +00:00
|
|
|
QModelIndex nameIndex = table->index(row, 0, QModelIndex());
|
|
|
|
QVariant varName = table->data(nameIndex, Qt::DisplayRole);
|
2011-04-27 10:05:43 +00:00
|
|
|
name = varName.toString();
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2012-11-21 14:45:13 +00:00
|
|
|
QModelIndex addressIndex = table->index(row, 1, QModelIndex());
|
|
|
|
QVariant varAddr = table->data(addressIndex, Qt::DisplayRole);
|
2011-04-27 10:05:43 +00:00
|
|
|
address = varAddr.toString();
|
|
|
|
}
|
|
|
|
//! [4a]
|
2013-03-14 23:42:15 +00:00
|
|
|
|
|
|
|
//! [4b]
|
2011-04-27 10:05:43 +00:00
|
|
|
AddDialog aDialog;
|
|
|
|
aDialog.setWindowTitle(tr("Edit a Contact"));
|
|
|
|
|
|
|
|
aDialog.nameText->setReadOnly(true);
|
|
|
|
aDialog.nameText->setText(name);
|
|
|
|
aDialog.addressText->setText(address);
|
|
|
|
|
|
|
|
if (aDialog.exec()) {
|
|
|
|
QString newAddress = aDialog.addressText->toPlainText();
|
|
|
|
if (newAddress != address) {
|
2012-11-21 14:45:13 +00:00
|
|
|
QModelIndex index = table->index(row, 1, QModelIndex());
|
|
|
|
table->setData(index, newAddress, Qt::EditRole);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [4b]
|
|
|
|
|
|
|
|
//! [5]
|
|
|
|
void AddressWidget::removeEntry()
|
|
|
|
{
|
|
|
|
QTableView *temp = static_cast<QTableView*>(currentWidget());
|
|
|
|
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
|
|
|
|
QItemSelectionModel *selectionModel = temp->selectionModel();
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QModelIndexList indexes = selectionModel->selectedRows();
|
|
|
|
|
2012-11-21 14:45:13 +00:00
|
|
|
foreach (QModelIndex index, indexes) {
|
2011-04-27 10:05:43 +00:00
|
|
|
int row = proxy->mapToSource(index).row();
|
|
|
|
table->removeRows(row, 1, QModelIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table->rowCount(QModelIndex()) == 0) {
|
|
|
|
insertTab(0, newAddressTab, "Address Book");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
void AddressWidget::setupTabs()
|
|
|
|
{
|
|
|
|
QStringList groups;
|
|
|
|
groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ";
|
|
|
|
|
|
|
|
for (int i = 0; i < groups.size(); ++i) {
|
|
|
|
QString str = groups.at(i);
|
2014-06-18 07:58:01 +00:00
|
|
|
QString regExp = QString("^[%1].*").arg(str);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
proxyModel = new QSortFilterProxyModel(this);
|
|
|
|
proxyModel->setSourceModel(table);
|
2014-06-18 07:58:01 +00:00
|
|
|
proxyModel->setFilterRegExp(QRegExp(regExp, Qt::CaseInsensitive));
|
|
|
|
proxyModel->setFilterKeyColumn(0);
|
2011-12-20 11:56:01 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTableView *tableView = new QTableView;
|
|
|
|
tableView->setModel(proxyModel);
|
2014-06-18 07:58:01 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
tableView->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
tableView->verticalHeader()->hide();
|
|
|
|
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
2014-06-18 07:58:01 +00:00
|
|
|
tableView->setSortingEnabled(true);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
connect(tableView->selectionModel(),
|
2015-07-31 11:53:29 +00:00
|
|
|
&QItemSelectionModel::selectionChanged,
|
|
|
|
this, &AddressWidget::selectionChanged);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
addTab(tableView, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
//! [7]
|
2012-11-21 14:45:13 +00:00
|
|
|
void AddressWidget::readFromFile(const QString &fileName)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
QMessageBox::information(this, tr("Unable to open file"),
|
|
|
|
file.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-04 15:09:52 +00:00
|
|
|
QList<Contact> contacts;
|
2011-04-27 10:05:43 +00:00
|
|
|
QDataStream in(&file);
|
2017-09-04 15:09:52 +00:00
|
|
|
in >> contacts;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-09-04 15:09:52 +00:00
|
|
|
if (contacts.isEmpty()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QMessageBox::information(this, tr("No contacts in file"),
|
2012-11-21 14:45:13 +00:00
|
|
|
tr("The file you are attempting to open contains no contacts."));
|
2011-04-27 10:05:43 +00:00
|
|
|
} else {
|
2017-09-04 15:09:52 +00:00
|
|
|
for (const auto &contact: qAsConst(contacts))
|
|
|
|
addEntry(contact.name, contact.address);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [7]
|
|
|
|
|
|
|
|
//! [6]
|
2012-11-21 14:45:13 +00:00
|
|
|
void AddressWidget::writeToFile(const QString &fileName)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QFile file(fileName);
|
|
|
|
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream out(&file);
|
2017-09-04 15:09:52 +00:00
|
|
|
out << table->getContacts();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [6]
|