|  | Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# addresswidget.cpp Example File
itemviews/addressbook/addresswidget.cpp

     /****************************************************************************
     **
     ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
     ** Contact: Qt Software Information (qt-info@nokia.com)
     **
     ** This file is part of the example classes of the Qt Toolkit.
     **
     ** Commercial Usage
     ** Licensees holding valid Qt Commercial licenses may use this file in
     ** accordance with the Qt Commercial License Agreement provided with the
     ** Software or, alternatively, in accordance with the terms contained in
     ** a written agreement between you and Nokia.
     **
     **
     ** GNU General Public License Usage
     ** Alternatively, this file may be used under the terms of the GNU
     ** General Public License versions 2.0 or 3.0 as published by the Free
     ** Software Foundation and appearing in the file LICENSE.GPL included in
     ** the packaging of this file.  Please review the following information
     ** to ensure GNU General Public Licensing requirements will be met:
     ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
     ** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
     ** exception, Nokia gives you certain additional rights. These rights
     ** are described in the Nokia Qt GPL Exception version 1.3, included in
     ** the file GPL_EXCEPTION.txt in this package.
     **
     ** Qt for Windows(R) Licensees
     ** As a special exception, Nokia, as the sole copyright holder for Qt
     ** Designer, grants users of the Qt/Eclipse Integration plug-in the
     ** right for the Qt/Eclipse Integration to link to functionality
     ** provided by Qt Designer and its related libraries.
     **
     ** If you are unsure which license is appropriate for your use, please
     ** contact the sales department at qt-sales@nokia.com.
     **
     ****************************************************************************/

     #include <QtGui>
     #include "addresswidget.h"
     #include "adddialog.h"

     AddressWidget::AddressWidget(QWidget *parent)
         : QTabWidget(parent)
     {
         table = new TableModel(this);
         newAddressTab = new NewAddressTab(this);
         connect(newAddressTab, SIGNAL(sendDetails(QString, QString)),
             this, SLOT(addEntry(QString, QString)));

         addTab(newAddressTab, "Address Book");

         setupTabs();
     }

     void AddressWidget::addEntry()
     {
         AddDialog aDialog;

         if (aDialog.exec()) {
             QString name = aDialog.nameText->text();
             QString address = aDialog.addressText->toPlainText();

             addEntry(name, address);
         }
     }

     void AddressWidget::addEntry(QString name, QString address)
     {
         QList< QPair<QString, QString> >list = table->getList();
         QPair<QString, QString> pair(name, address);

         if (!list.contains(pair)) {
             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));
         }
     }

     void AddressWidget::editEntry()
     {
         QTableView *temp = static_cast<QTableView*>(currentWidget());
         QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
         QItemSelectionModel *selectionModel = temp->selectionModel();

         QModelIndexList indexes = selectionModel->selectedRows();
         QModelIndex index, i;
         QString name;
         QString address;
         int row;

         foreach (index, indexes) {
             row = proxy->mapToSource(index).row();
             i = table->index(row, 0, QModelIndex());
             QVariant varName = table->data(i, Qt::DisplayRole);
             name = varName.toString();

             i = table->index(row, 1, QModelIndex());
             QVariant varAddr = table->data(i, Qt::DisplayRole);
             address = varAddr.toString();
         }

         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) {
                 i = table->index(row, 1, QModelIndex());
                 table->setData(i, newAddress, Qt::EditRole);
             }
         }
     }

     void AddressWidget::removeEntry()
     {
         QTableView *temp = static_cast<QTableView*>(currentWidget());
         QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
         QItemSelectionModel *selectionModel = temp->selectionModel();

         QModelIndexList indexes = selectionModel->selectedRows();
         QModelIndex index;

         foreach (index, indexes) {
             int row = proxy->mapToSource(index).row();
             table->removeRows(row, 1, QModelIndex());
         }

         if (table->rowCount(QModelIndex()) == 0) {
             insertTab(0, newAddressTab, "Address Book");
         }
     }

     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);

             proxyModel = new QSortFilterProxyModel(this);
             proxyModel->setSourceModel(table);
             proxyModel->setDynamicSortFilter(true);

             QTableView *tableView = new QTableView;
             tableView->setModel(proxyModel);
             tableView->setSortingEnabled(true);
             tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
             tableView->horizontalHeader()->setStretchLastSection(true);
             tableView->verticalHeader()->hide();
             tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
             tableView->setSelectionMode(QAbstractItemView::SingleSelection);

             QString newStr = QString("^[%1].*").arg(str);

             proxyModel->setFilterRegExp(QRegExp(newStr, Qt::CaseInsensitive));
             proxyModel->setFilterKeyColumn(0);
             proxyModel->sort(0, Qt::AscendingOrder);

             connect(tableView->selectionModel(),
                 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
                 this, SIGNAL(selectionChanged(const QItemSelection &)));

             addTab(tableView, str);
         }
     }

     void AddressWidget::readFromFile(QString fileName)
     {
         QFile file(fileName);

         if (!file.open(QIODevice::ReadOnly)) {
             QMessageBox::information(this, tr("Unable to open file"),
                 file.errorString());
             return;
         }

         QList< QPair<QString, QString> > pairs = table->getList();
         QDataStream in(&file);
         in >> pairs;

         if (pairs.isEmpty()) {
             QMessageBox::information(this, tr("No contacts in file"),
                 tr("The file you are attempting to open contains no contacts."));
         } else {
             for (int i=0; i<pairs.size(); ++i) {
                 QPair<QString, QString> p = pairs.at(i);
                 addEntry(p.first, p.second);
             }
         }
     }

     void AddressWidget::writeToFile(QString fileName)
     {
         QFile file(fileName);

         if (!file.open(QIODevice::WriteOnly)) {
             QMessageBox::information(this, tr("Unable to open file"), file.errorString());
             return;
         }

         QList< QPair<QString, QString> > pairs = table->getList();
         QDataStream out(&file);
         out << pairs;
     }