Commit 3b26f14a authored by Tobias Koenig's avatar Tobias Koenig
Browse files

Introduced new class ContactSorter, that sorts a list

of contacts according to a given ContactField.
This fixes the 'descending sorting does not work in printing'
as well.

BUG: 206307

svn path=/trunk/KDE/kdepim/kaddressbook/; revision=1020045
parent b5a9f4a6
......@@ -22,7 +22,6 @@ set( kaddressbook_printing_SRCS
printing/mikesstyle.cpp
printing/printingwizard.cpp
printing/printprogress.cpp
printing/printsortmode.cpp
printing/printstyle.cpp
printing/ringbinderstyle.cpp
printing/selectionpage.cpp
......@@ -38,6 +37,7 @@ set( kaddressbook_LIB_SRCS
aboutdata.cpp
contactfields.cpp
contactfiltermodel.cpp
contactsorter.cpp
contactstreemodel.cpp
contactswitcher.cpp
globalcontactmodel.cpp
......
/*
This file is part of KAddressBook.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "contactsorter.h"
#include <kabc/addressee.h>
class ContactSortHelper
{
public:
ContactSortHelper( ContactFields::Field field, Qt::SortOrder sortOrder )
: mSortField( field ), mSortOrder( sortOrder )
{
}
inline bool operator()( const KABC::Addressee &contact, const KABC::Addressee &otherContact ) const
{
int result = QString::localeAwareCompare( ContactFields::value( mSortField, contact ),
ContactFields::value( mSortField, otherContact ) );
if ( result == 0 ) {
int givenNameResult = QString::localeAwareCompare( ContactFields::value( ContactFields::GivenName, contact ),
ContactFields::value( ContactFields::GivenName, otherContact ) );
if ( givenNameResult == 0 ) {
int familyNameResult = QString::localeAwareCompare( ContactFields::value( ContactFields::FamilyName, contact ),
ContactFields::value( ContactFields::FamilyName, otherContact ) );
if ( familyNameResult == 0 ) {
result = QString::localeAwareCompare( ContactFields::value( ContactFields::FormattedName, contact ),
ContactFields::value( ContactFields::FormattedName, otherContact ) );
} else
result = familyNameResult;
} else
result = givenNameResult;
}
bool lesser = result < 0;
if ( mSortOrder == Qt::DescendingOrder )
lesser = !lesser;
return lesser;
}
private:
const ContactFields::Field mSortField;
const Qt::SortOrder mSortOrder;
};
ContactSorter::ContactSorter( ContactFields::Field field, Qt::SortOrder sortOrder )
: mSortField( field ), mSortOrder( sortOrder )
{
}
void ContactSorter::sort( QList<KABC::Addressee> &contacts ) const
{
qStableSort( contacts.begin(), contacts.end(), ContactSortHelper( mSortField, mSortOrder ) );
}
/*
This file is part of KAddressBook.
Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -15,32 +16,23 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#ifndef PRINTSORTMODE_H
#define PRINTSORTMODE_H
#ifndef CONTACTSORTER_H
#define CONTACTSORTER_H
#include <kabc/sortmode.h>
#include "contactfields.h"
class PrintSortMode : public KABC::SortMode
class ContactSorter
{
public:
explicit PrintSortMode(ContactFields::Field field, bool ascending = true );
explicit ContactSorter( ContactFields::Field field, Qt::SortOrder sortOrder = Qt::AscendingOrder );
virtual bool lesser( const KABC::Addressee&, const KABC::Addressee& ) const;
void sort( QList<KABC::Addressee> &contacts ) const;
private:
ContactFields::Field mSortField;
ContactFields::Field mGivenNameField;
ContactFields::Field mFamilyNameField;
ContactFields::Field mFormattedNameField;
bool mAscending;
const ContactFields::Field mSortField;
const Qt::SortOrder mSortOrder;
};
#endif // PRINTSORTMODE_H
#endif
......@@ -25,9 +25,22 @@
#include "printingwizard.h"
#include <QtGui/QPushButton>
#include <QtGui/QPrinter>
#include "contactsorter.h"
#include "contactstreemodel.h"
#include "globalcontactmodel.h"
#include "printprogress.h"
#include "printstyle.h"
#include "selectionpage.h"
#include "stylepage.h"
// including the styles
#include "detailledstyle.h"
#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>
......@@ -36,21 +49,8 @@
#include <klocale.h>
#include <kdescendantsproxymodel.h>
#include <akonadi/entitytreemodel.h>
#include <akonadi/entityfilterproxymodel.h>
#include "globalcontactmodel.h"
#include "contactstreemodel.h"
#include <akonadi_next/entitytreeview.h>
// including the styles
#include "detailledstyle.h"
#include "mikesstyle.h"
#include "ringbinderstyle.h"
#include "printprogress.h"
#include "printstyle.h"
#include "printsortmode.h"
#include <QtGui/QPushButton>
#include <QtGui/QPrinter>
using namespace KABPrinting;
......@@ -123,7 +123,10 @@ void PrintingWizard::slotStyleSelected( int index )
if ( mStyle->preferredSortField() != 0 ) {
mStylePage->setSortField( mStyle->preferredSortField() );
mStylePage->setSortAscending( mStyle->preferredSortType() );
if ( mStyle->preferredSortType() )
mStylePage->setSortOrder( Qt::AscendingOrder );
else
mStylePage->setSortOrder( Qt::DescendingOrder );
}
}
......@@ -145,7 +148,7 @@ void PrintingWizard::print()
QAbstractItemModel* model = mItemView->model();
Q_ASSERT(model);
KABC::AddresseeList list;
KABC::Addressee::List list;
if ( mStyle != 0 ) {
if ( mSelectionPage->useSelection() ) {
foreach ( const QModelIndex &index, mItemView->selectionModel()->selectedRows() ) {
......@@ -155,24 +158,6 @@ void PrintingWizard::print()
if ( item.hasPayload<KABC::Addressee>() )
list.append( item.payload<KABC::Addressee>() );
}
} else if ( mSelectionPage->useFilters() ) {
//TODO ? or remove it it's not necessary
} else if ( mSelectionPage->useCategories() ) {
#if 0
QStringList categories = mSelectionPage->categories();
KABC::AddressBook::ConstIterator it;
for ( it = addressBook()->constBegin(); it != addressBook()->constEnd(); ++it ) {
const QStringList tmp( (*it).categories() );
QStringList::ConstIterator tmpIt;
for ( tmpIt = tmp.constBegin(); tmpIt != tmp.constEnd(); ++tmpIt )
if ( categories.contains( *tmpIt ) ) {
list.append( *it );
break;
}
}
#else
Q_ASSERT(false);
#endif
} else {
Akonadi::ContactsTreeModel *contactsModel = GlobalContactModel::instance()->model();
......@@ -194,9 +179,8 @@ void PrintingWizard::print()
}
}
list.setReverseSorting( !mStylePage->sortAscending() );
PrintSortMode sortMode( mStylePage->sortField() );
list.sortByMode( &sortMode );
const ContactSorter sorter( mStylePage->sortField(), mStylePage->sortOrder() );
sorter.sort( list );
}
kDebug(5720) <<"PrintingWizardImpl::print: printing"
......
......@@ -31,15 +31,17 @@
#include <kassistantdialog.h>
#include "printstyle.h"
#include "selectionpage.h"
#include "stylepage.h"
class QAbstractItemView;
class QPrinter;
class SelectionPage;
class StylePage;
namespace KABPrinting {
class PrintStyle;
class PrintStyleFactory;
/**
* The PrintingWizard combines pages common for all print styles
* and those provided by the respective style.
......
/*
This file is part of KAddressBook.
Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "printsortmode.h"
#include <kabc/field.h>
PrintSortMode::PrintSortMode( ContactFields::Field field, bool ascending )
: mSortField( field ), mAscending( ascending )
{
const ContactFields::Fields fields = ContactFields::allFields();
ContactFields::Fields::ConstIterator it;
for ( it = fields.begin(); it != fields.end(); ++it ) {
if ( ContactFields::label(*it) == ContactFields::label( ContactFields::NickName) )
mGivenNameField = *it;
else if ( ContactFields::label(*it) == ContactFields::label(ContactFields::FamilyName) )
mFamilyNameField = *it;
else if ( ContactFields::label(*it) == ContactFields::label( ContactFields::FormattedName) )
mFormattedNameField = *it;
}
}
bool PrintSortMode::lesser( const KABC::Addressee &first,
const KABC::Addressee &second ) const
{
if ( !mSortField )
return false;
int result = QString::localeAwareCompare( ContactFields::value( mSortField, first ),
ContactFields::value( mSortField, second ) );
if ( result == 0 ) {
int givenNameResult = QString::localeAwareCompare( ContactFields::value( mGivenNameField, first ),
ContactFields::value( mGivenNameField, second ) );
if ( givenNameResult == 0 ) {
int familyNameResult = QString::localeAwareCompare( ContactFields::value( mFamilyNameField, first ),
ContactFields::value( mFamilyNameField, second ) );
if ( familyNameResult == 0 ) {
result = QString::localeAwareCompare( ContactFields::value( mFormattedNameField, first ),
ContactFields::value( mFormattedNameField, second ) );
} else
result = familyNameResult;
} else
result = givenNameResult;
}
bool lesser = result < 0;
if ( !mAscending )
lesser = !lesser;
return lesser;
}
......@@ -75,15 +75,15 @@ void StylePage::setSortField( ContactFields::Field field )
mFieldCombo->setItemText( mFieldCombo->currentIndex(), ContactFields::label( field) );
}
void StylePage::setSortAscending( bool value )
void StylePage::setSortOrder( Qt::SortOrder sortOrder )
{
if ( value )
if ( sortOrder == Qt::AscendingOrder )
mSortTypeCombo->setCurrentIndex( 0 );
else
mSortTypeCombo->setCurrentIndex( 1 );
}
ContactFields::Field StylePage::sortField()
ContactFields::Field StylePage::sortField() const
{
if ( mFieldCombo->currentIndex() == -1 )
return mFields[ 0 ];
......@@ -91,9 +91,9 @@ ContactFields::Field StylePage::sortField()
return mFields[ mFieldCombo->currentIndex() ];
}
bool StylePage::sortAscending()
Qt::SortOrder StylePage::sortOrder() const
{
return ( mSortTypeCombo->currentIndex() == 0 );
return ( mSortTypeCombo->currentIndex() == 0 ? Qt::AscendingOrder : Qt::DescendingOrder );
}
void StylePage::initFieldCombo()
......
......@@ -66,17 +66,17 @@ class StylePage : public QWidget
/**
* Returns the sort criterion field.
*/
ContactFields::Field sortField();
ContactFields::Field sortField() const;
/**
* Set the sort type.
* Sets the sort order.
*/
void setSortAscending( bool value = true );
void setSortOrder( Qt::SortOrder sortOrder );
/**
* Returns whether the sort type is ascending.
* Returns the sort order.
*/
bool sortAscending();
Qt::SortOrder sortOrder() const;
Q_SIGNALS:
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment