22 #include "entityorderproxymodel.h"
26 #include <KDE/KConfigGroup>
29 #include "collection.h"
31 #include "entitytreemodel.h"
36 class EntityOrderProxyModelPrivate
45 void saveOrder(
const QModelIndex &index );
47 KConfigGroup m_orderConfig;
56 using namespace Akonadi;
59 : QSortFilterProxyModel(parent), d_ptr( new EntityOrderProxyModelPrivate( this ) )
61 setDynamicSortFilter(
true );
73 layoutAboutToBeChanged();
74 d->m_orderConfig = configGroup;
82 if ( !d->m_orderConfig.isValid() ) {
83 return QSortFilterProxyModel::lessThan( left, right );
87 if ( !d->m_orderConfig.hasKey( QString::number( col.
id() ) ) ) {
88 return QSortFilterProxyModel::lessThan( left, right );
91 const QStringList list = d->m_orderConfig.readEntry( QString::number( col.
id() ), QStringList() );
93 if ( list.isEmpty() ) {
94 return QSortFilterProxyModel::lessThan( left, right );
97 const QString leftValue = configString( left );
98 const QString rightValue = configString( right );
100 const int leftPosition = list.indexOf( leftValue );
101 const int rightPosition = list.indexOf( rightValue );
103 if ( leftPosition < 0 || rightPosition < 0 ) {
104 return QSortFilterProxyModel::lessThan( left, right );
107 return leftPosition < rightPosition;
114 if ( !d->m_orderConfig.isValid() ) {
115 return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
118 if ( !data->hasFormat( QLatin1String(
"text/uri-list" ) ) ) {
119 return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
123 return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
126 bool containsMove =
false;
128 const KUrl::List urls = KUrl::List::fromMimeData( data );
132 if ( parent.isValid() ) {
135 if ( !hasChildren( parent ) ) {
136 return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
139 const QModelIndex targetIndex = index( 0, column, parent );
144 QStringList droppedList;
145 foreach (
const KUrl &url, urls ) {
149 Item item = Item::fromUrl( url );
150 if ( !item.isValid() ) {
155 if ( list.isEmpty() ) {
163 droppedList << configString( list.first() );
166 if ( !idx.isValid() ) {
174 droppedList << configString( idx );
178 QStringList existingList;
179 if ( d->m_orderConfig.hasKey( QString::number( parentCol.
id() ) ) ) {
180 existingList = d->m_orderConfig.readEntry( QString::number( parentCol.
id() ), QStringList() );
182 const int rowCount = this->rowCount( parent );
183 for (
int row = 0; row < rowCount; ++row ) {
184 static const int column = 0;
185 const QModelIndex idx = this->index( row, column, parent );
186 existingList.append( configString( idx ) );
189 const int numberOfDroppedElement( droppedList.size() );
190 for (
int i = 0; i < numberOfDroppedElement; ++i ) {
191 const QString droppedItem = droppedList.at( i );
192 const int existingIndex = existingList.indexOf( droppedItem );
193 existingList.removeAt( existingIndex );
194 existingList.insert( row + i - ( existingIndex > row ? 0 : 1 ), droppedList.at( i ) );
197 d->m_orderConfig.writeEntry( QString::number( parentCol.
id() ), existingList );
199 if ( containsMove ) {
200 bool result = QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
210 if ( role < Qt::UserRole ) {
211 return QSortFilterProxyModel::match( start, role, value, hits, flags );
214 QModelIndexList list;
215 QModelIndex proxyIndex;
216 foreach (
const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
217 proxyIndex = mapFromSource( idx );
218 if ( proxyIndex.isValid() ) {
226 void EntityOrderProxyModelPrivate::saveOrder(
const QModelIndex &parent )
229 int rowCount = q->rowCount( parent );
231 if ( rowCount == 0 ) {
235 static const int column = 0;
236 QModelIndex childIndex = q->index( 0, column, parent );
238 QString parentKey = q->parentConfigString( childIndex );
240 if ( parentKey.isEmpty() ) {
246 list << q->configString( childIndex );
247 saveOrder( childIndex );
249 for (
int row = 1; row < rowCount; ++row ) {
250 childIndex = q->index( row, column, parent );
251 list << q->configString( childIndex );
252 saveOrder( childIndex );
255 m_orderConfig.writeEntry( parentKey, list );
258 QString EntityOrderProxyModel::parentConfigString(
const QModelIndex& index )
const
267 return QString::number( col.
id() );
270 QString EntityOrderProxyModel::configString(
const QModelIndex& index )
const
274 return QString::fromLatin1(
"i" ) + QString::number( eId );
278 return QString::fromLatin1(
"c" ) + QString::number( eId );
280 Q_ASSERT( !
"Invalid entity" );
287 d->saveOrder( QModelIndex() );
288 d->m_orderConfig.sync();
291 void EntityOrderProxyModel::clearOrder(
const QModelIndex& parent )
295 const QString parentKey = parentConfigString( index( 0, 0, parent ) );
297 if ( parentKey.isEmpty() ) {
301 d->m_orderConfig.deleteEntry( parentKey );
305 void EntityOrderProxyModel::clearTreeOrder()
308 d->m_orderConfig.deleteGroup();
Represents a collection of PIM items.
qint64 Id
Describes the unique id type.
void setOrderConfig(KConfigGroup &group)
Sets the config group that will be used for storing the order.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
A model that keeps the order of entities persistent.
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
The parent collection of the entity.
Id id() const
Returns the unique identifier of the entity.
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
void saveOrder()
Saves the order.
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const
virtual ~EntityOrderProxyModel()
Destroys the entity order proxy model.
static QModelIndexList modelIndexesForItem(const QAbstractItemModel *model, const Item &item)
Returns a QModelIndex in model which points to item.
bool isValid() const
Returns whether the entity is valid.
static Collection fromUrl(const KUrl &url)
Creates a collection from the given url.