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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-21 14:45:13 +00:00
|
|
|
#include "addresswidget.h"
|
2018-11-12 20:20:34 +00:00
|
|
|
#include "adddialog.h"
|
2012-11-21 14:45:13 +00:00
|
|
|
|
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [0]
|
|
|
|
AddressWidget::AddressWidget(QWidget *parent)
|
2018-11-12 20:20:34 +00:00
|
|
|
: QTabWidget(parent),
|
|
|
|
table(new TableModel(this)),
|
|
|
|
newAddressTab(new NewAddressTab(this))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2015-07-31 11:53:29 +00:00
|
|
|
connect(newAddressTab, &NewAddressTab::sendDetails,
|
|
|
|
this, &AddressWidget::addEntry);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
addTab(newAddressTab, tr("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;
|
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
if (aDialog.exec())
|
|
|
|
addEntry(aDialog.name(), aDialog.address());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
//! [3]
|
2018-11-12 20:20:34 +00:00
|
|
|
void AddressWidget::addEntry(const QString &name, const 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();
|
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
const QModelIndexList indexes = selectionModel->selectedRows();
|
2011-04-27 10:05:43 +00:00
|
|
|
QString name;
|
|
|
|
QString address;
|
|
|
|
int row = -1;
|
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
for (const 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"));
|
2018-11-12 20:20:34 +00:00
|
|
|
aDialog.editAddress(name, address);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (aDialog.exec()) {
|
2018-11-12 20:20:34 +00:00
|
|
|
const QString newAddress = aDialog.address();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (newAddress != address) {
|
2018-11-12 20:20:34 +00:00
|
|
|
const QModelIndex index = table->index(row, 1, QModelIndex());
|
2012-11-21 14:45:13 +00:00
|
|
|
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
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
const QModelIndexList indexes = selectionModel->selectedRows();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
for (QModelIndex index : indexes) {
|
2011-04-27 10:05:43 +00:00
|
|
|
int row = proxy->mapToSource(index).row();
|
|
|
|
table->removeRows(row, 1, QModelIndex());
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
if (table->rowCount(QModelIndex()) == 0)
|
|
|
|
insertTab(0, newAddressTab, tr("Address Book"));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
void AddressWidget::setupTabs()
|
|
|
|
{
|
2018-11-12 20:20:34 +00:00
|
|
|
const auto groups = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ" };
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
for (const QString &str : groups) {
|
|
|
|
const auto regExp = QRegularExpression(QString("^[%1].*").arg(str),
|
|
|
|
QRegularExpression::CaseInsensitiveOption);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
auto proxyModel = new QSortFilterProxyModel(this);
|
2011-04-27 10:05:43 +00:00
|
|
|
proxyModel->setSourceModel(table);
|
2018-11-12 20:20:34 +00:00
|
|
|
proxyModel->setFilterRegularExpression(regExp);
|
2014-06-18 07:58:01 +00:00
|
|
|
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);
|
|
|
|
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
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
|
|
|
|
this, &AddressWidget::selectionChanged);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
connect(this, &QTabWidget::currentChanged, this, [this, tableView](int tabIndex) {
|
|
|
|
if (widget(tabIndex) == tableView)
|
2017-10-20 11:10:42 +00:00
|
|
|
emit selectionChanged(tableView->selectionModel()->selection());
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:20:34 +00:00
|
|
|
QVector<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]
|