Commit 0ad16c9c authored by Tobias Koenig's avatar Tobias Koenig
Browse files

Add ContactSelectionWidget/Dialog, two classes to select

a group of contacts by different criterion.

Adapted PrintingWizard and XXPortManager to the new classes

svn path=/trunk/KDE/kdepim/kaddressbook/; revision=1020215
parent 2e14028b
......@@ -24,7 +24,6 @@ set( kaddressbook_printing_SRCS
printing/printprogress.cpp
printing/printstyle.cpp
printing/ringbinderstyle.cpp
printing/selectionpage.cpp
printing/stylepage.cpp
)
......@@ -37,6 +36,8 @@ set( kaddressbook_LIB_SRCS
aboutdata.cpp
contactfields.cpp
contactfiltermodel.cpp
contactselectiondialog.cpp
contactselectionwidget.cpp
contactsorter.cpp
contactstreemodel.cpp
contactswitcher.cpp
......
/*
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "contactselectiondialog.h"
#include "contactselectionwidget.h"
#include <klocale.h>
ContactSelectionDialog::ContactSelectionDialog( QAbstractItemModel *model, QItemSelectionModel *selectionModel, QWidget *parent )
: KDialog( parent )
{
setCaption( i18n( "Select Contacts" ) );
setButtons( Ok | Cancel );
mSelectionWidget = new ContactSelectionWidget( model, selectionModel, this );
setMainWidget( mSelectionWidget );
connect( mSelectionWidget, SIGNAL( selectedContacts( const KABC::Addressee::List& ) ),
this, SLOT( slotSelectedContacts( const KABC::Addressee::List& ) ) );
connect( this, SIGNAL( okClicked() ), this, SLOT( slotOkClicked() ) );
setInitialSize( QSize( 450, 220 ) );
}
void ContactSelectionDialog::setMessageText( const QString &message )
{
mSelectionWidget->setMessageText( message );
}
KABC::Addressee::List ContactSelectionDialog::selectedContacts() const
{
return mContacts;
}
void ContactSelectionDialog::slotSelectedContacts( const KABC::Addressee::List &contacts )
{
mContacts = contacts;
accept();
}
void ContactSelectionDialog::slotOkClicked()
{
// this will trigger slotSelectedContacts via signal/slot
mSelectionWidget->requestSelectedContacts();
}
#include "contactselectiondialog.moc"
/*
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CONTACTSELECTIONDIALOG_H
#define CONTACTSELECTIONDIALOG_H
#include <kabc/addressee.h>
#include <kdialog.h>
class ContactSelectionWidget;
class QAbstractItemModel;
class QItemSelectionModel;
/**
* @short A dialog to select a group of contacts.
*
* @author Tobias Koenig <tokoe@kde.org>
*/
class ContactSelectionDialog : public KDialog
{
Q_OBJECT
public:
/**
* Creates a new contact selection dialog.
*
* @param model The model that contains all available contacts.
* @param selectionModel The model that contains the currently selected contacts.
* @param parent The parent widget.
*/
ContactSelectionDialog( QAbstractItemModel *model, QItemSelectionModel *selectionModel, QWidget *parent = 0 );
/**
* Sets the @p message text.
*/
void setMessageText( const QString &message );
/**
* Returns the list of selected contacts.
*/
KABC::Addressee::List selectedContacts() const;
private Q_SLOTS:
void slotSelectedContacts( const KABC::Addressee::List& );
void slotOkClicked();
private:
ContactSelectionWidget *mSelectionWidget;
KABC::Addressee::List mContacts;
};
#endif
/*
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "contactselectionwidget.h"
#include <akonadi/contact/addressbookcombobox.h>
#include <akonadi/entitytreemodel.h>
#include <klocale.h>
#include <QtCore/QAbstractItemModel>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QItemSelectionModel>
#include <QtGui/QLabel>
#include <QtGui/QRadioButton>
#include <QtGui/QVBoxLayout>
ContactSelectionWidget::ContactSelectionWidget( QAbstractItemModel *model, QItemSelectionModel *selectionModel, QWidget *parent )
: QWidget( parent ), mModel( model ), mSelectionModel( selectionModel )
{
initGui();
mSelectedContactsButton->setEnabled( mSelectionModel->hasSelection() );
mAddressBookSelection->setEnabled( false );
// disable until we can fetch a collection recursivly
mAddressBookContactsButton->setEnabled( false );
connect( mAddressBookContactsButton, SIGNAL( toggled( bool ) ),
mAddressBookSelection, SLOT( setEnabled( bool ) ) );
// apply default configuration
if ( mSelectionModel->hasSelection() )
mSelectedContactsButton->setChecked( true );
else
mAllContactsButton->setChecked( true );
}
void ContactSelectionWidget::setMessageText( const QString &message )
{
mMessageLabel->setText( message );
}
void ContactSelectionWidget::requestSelectedContacts()
{
if ( mAllContactsButton->isChecked() )
collectAllContacts();
else if ( mSelectedContactsButton->isChecked() )
collectSelectedContacts();
else if ( mAddressBookContactsButton->isChecked() )
collectAddressBookContacts();
}
void ContactSelectionWidget::initGui()
{
QVBoxLayout *layout = new QVBoxLayout( this );
mMessageLabel = new QLabel;
layout->addWidget( mMessageLabel );
QButtonGroup *group = new QButtonGroup( this );
QGroupBox *groupBox = new QGroupBox;
QGridLayout *boxLayout = new QGridLayout;
groupBox->setLayout( boxLayout );
mAllContactsButton = new QRadioButton( i18n( "All contacts" ) );
mSelectedContactsButton = new QRadioButton( i18n( "Selected contacts" ) );
mAddressBookContactsButton = new QRadioButton( i18n( "All contacts from:" ) );
mAddressBookSelection = new Akonadi::AddressBookComboBox( Akonadi::AddressBookComboBox::ContactsOnly,
Akonadi::AddressBookComboBox::Readable );
group->addButton( mAllContactsButton );
group->addButton( mSelectedContactsButton );
group->addButton( mAddressBookContactsButton );
boxLayout->addWidget( mAllContactsButton, 0, 0, 1, 2 );
boxLayout->addWidget( mSelectedContactsButton, 1, 0, 1, 2 );
boxLayout->addWidget( mAddressBookContactsButton, 2, 0 );
boxLayout->addWidget( mAddressBookSelection, 2, 1 );
layout->addWidget( groupBox );
layout->addStretch( 1 );
}
void ContactSelectionWidget::collectAllContacts()
{
KABC::Addressee::List contacts;
for ( int i = 0; i < mModel->rowCount(); ++i ) {
const QModelIndex index = mModel->index( i, 0 );
if ( index.isValid() ) {
const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
if ( item.isValid() && item.hasPayload<KABC::Addressee>() )
contacts.append( item.payload<KABC::Addressee>() );
}
}
emit selectedContacts( contacts );
}
void ContactSelectionWidget::collectSelectedContacts()
{
KABC::Addressee::List contacts;
const QModelIndexList indexes = mSelectionModel->selectedRows( 0 );
for ( int i = 0; i < indexes.count(); ++i ) {
const QModelIndex index = indexes.at( i );
if ( index.isValid() ) {
const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
if ( item.isValid() && item.hasPayload<KABC::Addressee>() )
contacts.append( item.payload<KABC::Addressee>() );
}
}
emit selectedContacts( contacts );
}
void ContactSelectionWidget::collectAddressBookContacts()
{
}
#include "contactselectionwidget.moc"
/*
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef CONTACTSELECTIONWIDGET_H
#define CONTACTSELECTIONWIDGET_H
#include <QtGui/QWidget>
#include <kabc/addressee.h>
class QAbstractItemModel;
class QItemSelectionModel;
class QLabel;
class QRadioButton;
namespace Akonadi
{
class AddressBookComboBox;
}
/**
* @short A widget to select a group of contacts.
*
* @author Tobias Koenig <tokoe@kde.org>
*/
class ContactSelectionWidget : public QWidget
{
Q_OBJECT
public:
/**
* Creates a new contact selection widget.
*
* @param model The model that contains all available contacts.
* @param selectionModel The model that contains the currently selected contacts.
* @param parent The parent widget.
*/
ContactSelectionWidget( QAbstractItemModel *model, QItemSelectionModel *selectionModel, QWidget *parent = 0 );
/**
* Sets the @p message text.
*/
void setMessageText( const QString &message );
/**
* Requests the list of selected contacts.
* The list is made available via the selectedContacts() signal.
*/
void requestSelectedContacts();
Q_SIGNALS:
/**
* This signal is emitted when all contacts, the user has choosen,
* are available.
*/
void selectedContacts( const KABC::Addressee::List &contacts );
private:
void initGui();
void collectAllContacts();
void collectSelectedContacts();
void collectAddressBookContacts();
QAbstractItemModel *mModel;
QItemSelectionModel *mSelectionModel;
QLabel *mMessageLabel;
QRadioButton *mAllContactsButton;
QRadioButton *mSelectedContactsButton;
QRadioButton *mAddressBookContactsButton;
Akonadi::AddressBookComboBox *mAddressBookSelection;
};
#endif
......@@ -69,7 +69,7 @@
#include "printing/printingwizard.h"
MainWidget::MainWidget( KXMLGUIClient *guiClient, QWidget *parent )
: QWidget( parent )
: QWidget( parent ), mAllContactsModel( 0 )
{
mXXPortManager = new XXPortManager( this );
......@@ -88,16 +88,16 @@ MainWidget::MainWidget( KXMLGUIClient *guiClient, QWidget *parent )
* mItemTree
* ^
* |
* mDescendantTree
* ^
* |
* mCollectionView -> selectionProxyModel
* ^ ^
* | |
* mCollectionTree |
* ^ |
* | |
* \ /
* mDescendantTree mAllContactsModel
* ^ ^
* | |
* mCollectionView -> selectionProxyModel descendantsModel
* ^ ^ ^
* | | |
* mCollectionTree | |
* ^ | |
* | | _______________/
* \ / /
* GlobalContactModel::instance()
*
*
......@@ -111,8 +111,8 @@ MainWidget::MainWidget( KXMLGUIClient *guiClient, QWidget *parent )
* contactFilterModel: Filters the contacts by the content of mQuickSearchWidget
* mItemView: Shows the items (contacts and contact groups) in a view
*
* descModel: Flattens the item/collection tree to a list
* mContactCompletionModel: Provides a list of all contacts that can be used for auto-completion
* descendantsModel: Flattens the item/collection tree to a list
* mAllContactsModel: Provides a list of all available contacts from all address books
*/
mCollectionTree = new Akonadi::EntityFilterProxyModel( this );
......@@ -120,7 +120,7 @@ MainWidget::MainWidget( KXMLGUIClient *guiClient, QWidget *parent )
mCollectionTree->addMimeTypeInclusionFilter( Akonadi::Collection::mimeType() );
mCollectionTree->setHeaderSet( Akonadi::EntityTreeModel::CollectionTreeHeaders );
mXXPortManager->setCollectionModel( mCollectionTree );
mXXPortManager->setItemModel( allContactsModel() );
mCollectionView->setModel( mCollectionTree );
mCollectionView->setXmlGuiClient( guiClient );
......@@ -353,7 +353,8 @@ void MainWidget::print()
if ( !printDialog.exec() )
return;
KABPrinting::PrintingWizard wizard( &printer, mItemView, this );
KABPrinting::PrintingWizard wizard( &printer, allContactsModel(),
mItemView->selectionModel(), this );
wizard.exec();
}
......@@ -457,4 +458,19 @@ void MainWidget::editGroup( const Akonadi::Item &group )
dlg.exec();
}
QAbstractItemModel* MainWidget::allContactsModel()
{
if ( !mAllContactsModel ) {
KDescendantsProxyModel *descendantsModel = new KDescendantsProxyModel( this );
descendantsModel->setSourceModel( GlobalContactModel::instance()->model() );
mAllContactsModel = new Akonadi::EntityFilterProxyModel( this );
mAllContactsModel->setSourceModel( descendantsModel );
mAllContactsModel->addMimeTypeExclusionFilter( Akonadi::Collection::mimeType() );
mAllContactsModel->setHeaderSet( Akonadi::EntityTreeModel::ItemListHeaders );
}
return mAllContactsModel;
}
#include "mainwidget.moc"
......@@ -41,6 +41,7 @@ class KActionCollection;
class KDescendantsProxyModel;
class KXMLGUIClient;
class ModelColumnManager;
class QAbstractItemModel;
class QStackedWidget;
class QuickSearchWidget;
class XXPortManager;
......@@ -76,8 +77,11 @@ class KADDRESSBOOK_EXPORT MainWidget : public QWidget
void editContact( const Akonadi::Item& );
void editGroup( const Akonadi::Item& );
QAbstractItemModel* allContactsModel();
Akonadi::EntityFilterProxyModel *mCollectionTree;
Akonadi::EntityFilterProxyModel *mItemTree;
Akonadi::EntityFilterProxyModel *mAllContactsModel;
KDescendantsProxyModel *mDescendantTree;
QuickSearchWidget *mQuickSearchWidget;
......
......@@ -25,12 +25,10 @@
#include "printingwizard.h"
#include "contactselectionwidget.h"
#include "contactsorter.h"
#include "contactstreemodel.h"
#include "globalcontactmodel.h"
#include "printprogress.h"
#include "printstyle.h"
#include "selectionpage.h"
#include "stylepage.h"
// including the styles
......@@ -38,30 +36,28 @@
#include "mikesstyle.h"
#include "ringbinderstyle.h"
#include <akonadi/entitytreemodel.h>
#include <akonadi/entityfilterproxymodel.h>
#include <akonadi_next/entitytreeview.h>
#include <kabc/addresseelist.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kglobal.h>
#include <klocale.h>
#include <kdescendantsproxymodel.h>
#include <QtGui/QPushButton>
#include <QtGui/QPrinter>
using namespace KABPrinting;
PrintingWizard::PrintingWizard( QPrinter *printer, QAbstractItemView *itemView, QWidget *parent )
: KAssistantDialog( parent ), mPrinter( printer ), mItemView( itemView ), mStyle( 0 )
PrintingWizard::PrintingWizard( QPrinter *printer, QAbstractItemModel *itemModel,
QItemSelectionModel *selectionModel, QWidget *parent )
: KAssistantDialog( parent ), mPrinter( printer ), mStyle( 0 )
{
mSelectionPage = new SelectionPage( this );
mSelectionPage->setUseSelection( mItemView->selectionModel()->hasSelection() );
mSelectionPage = new ContactSelectionWidget( itemModel, selectionModel, this );
mSelectionPage->setMessageText( i18n( "Which contacts do you want to print?" ) );
connect( mSelectionPage, SIGNAL( selectedContacts( const KABC::Addressee::List& ) ),
this, SLOT( finalizePrinting( const KABC::Addressee::List& ) ) );
KPageWidgetItem *mSelectionPageItem = new KPageWidgetItem( mSelectionPage, i18n( "Choose Contacts to Print" ) );
addPage( mSelectionPageItem );
setAppropriate( mSelectionPageItem, true );
mStylePage = new StylePage( this );
......@@ -130,57 +126,29 @@ QPrinter* PrintingWizard::printer()
void PrintingWizard::print()
{
// create and show print progress widget:
PrintProgress *progress = new PrintProgress( this );
KPageWidgetItem *progressItem = new KPageWidgetItem( progress, i18n( "Print Progress" ) );
mProgress = new PrintProgress( this );
KPageWidgetItem *progressItem = new KPageWidgetItem( mProgress, i18n( "Print Progress" ) );
addPage( progressItem );
setCurrentPage( progressItem );
kapp->processEvents();
// prepare list of contacts to print:
QAbstractItemModel* model = mItemView->model();
Q_ASSERT(model);
KABC::Addressee::List list;
if ( mStyle != 0 ) {
if ( mSelectionPage->useSelection() ) {
foreach ( const QModelIndex &index, mItemView->selectionModel()->selectedRows() ) {
const Akonadi::Item item = model->data( index, Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
Q_ASSERT( item.isValid() );
if ( item.hasPayload<KABC::Addressee>() )
list.append( item.payload<KABC::Addressee>() );
}
} else {
Akonadi::ContactsTreeModel *contactsModel = GlobalContactModel::instance()->model();
KDescendantsProxyModel *descendantTree = new KDescendantsProxyModel( this );
descendantTree->setSourceModel( contactsModel );
Akonadi::EntityFilterProxyModel *allContacts = new Akonadi::EntityFilterProxyModel( this );
allContacts->setSourceModel( descendantTree );
allContacts->addMimeTypeInclusionFilter( KABC::Addressee::mimeType() );
// create a string list of all entries:
for ( int row = 0; row < allContacts->rowCount(); ++row ) {
const QModelIndex index = allContacts->index( row, 0 );
const Akonadi::Item item = index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
Q_ASSERT( item.isValid() );
if ( item.hasPayload<KABC::Addressee>() )
list.append( item.payload<KABC::Addressee>() );
}
}
const ContactSorter sorter( mStylePage->sortField(), mStylePage->sortOrder() );
sorter.sort( list );
}
// this will call finalizePrinting via signal/slot
mSelectionPage->requestSelectedContacts();
}
void PrintingWizard::finalizePrinting( const KABC::Addressee::List &list )
{
KABC::Addressee::List contacts( list );
const ContactSorter sorter( mStylePage->sortField(), mStylePage->sortOrder() );
sorter.sort( contacts );
kDebug(5720) <<"PrintingWizardImpl::print: printing"
<< list.count() << "contacts.";
<< contacts.count() << "contacts.";
// ... print:
enableButton( KDialog::User3, false ); // back button
enableButton( KDialog::Cancel, false );
mStyle->print( list, progress );
mStyle->print( contacts, mProgress );
}
#include "printingwizard.moc"
......@@ -29,16 +29,19 @@
#include <QtCore/QStringList>
#include <kabc/addressee.h>
#include <kassistantdialog.h>