1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/***************************************************************************
* Copyright (C) 2006 by Pino Toscano <pino@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. *
***************************************************************************/
#include "annotationmodel.h"
#include <qlinkedlist.h>
#include <qlist.h>
#include <qpointer.h>
#include <kicon.h>
#include <klocale.h>
#include "core/annotations.h"
#include "core/document.h"
#include "core/observer.h"
#include "core/page.h"
#include "ui/guiutils.h"
struct AnnItem
{
AnnItem();
AnnItem( AnnItem *parent, Okular::Annotation *ann );
AnnItem( AnnItem *parent, int page );
~AnnItem();
AnnItem *parent;
QList< AnnItem* > children;
Okular::Annotation *annotation;
int page;
};
class AnnotationModelPrivate : public Okular::DocumentObserver
{
public:
AnnotationModelPrivate( AnnotationModel *qq );
virtual ~AnnotationModelPrivate();
virtual uint observerId() const;
virtual void notifySetup( const QVector< Okular::Page * > &pages, int setupFlags );
virtual void notifyPageChanged( int page, int flags );
QModelIndex indexForItem( AnnItem *item ) const;
void rebuildTree( const QVector< Okular::Page * > &pages );
AnnItem* findItem( int page, int *index ) const;
AnnotationModel *q;
AnnItem *root;
QPointer< Okular::Document > document;
};
AnnItem::AnnItem()
: parent( 0 ), annotation( 0 ), page( -1 )
{
}
AnnItem::AnnItem( AnnItem *_parent, Okular::Annotation *ann )
: parent( _parent ), annotation( ann ), page( _parent->page )
{
Q_ASSERT( !parent->annotation );
parent->children.append( this );
}
AnnItem::AnnItem( AnnItem *_parent, int _page )
: parent( _parent ), annotation( 0 ), page( _page )
{
Q_ASSERT( !parent->parent );
parent->children.append( this );
}
AnnItem::~AnnItem()
{
qDeleteAll( children );
}
AnnotationModelPrivate::AnnotationModelPrivate( AnnotationModel *qq )
: q( qq ), root( new AnnItem )
{
}
AnnotationModelPrivate::~AnnotationModelPrivate()
{
delete root;
}
uint AnnotationModelPrivate::observerId() const
{
return ANNOTATIONMODEL_ID;
}
void AnnotationModelPrivate::notifySetup( const QVector< Okular::Page * > &pages, int setupFlags )
{
if ( !( setupFlags & Okular::DocumentObserver::DocumentChanged ) )
return;
qDeleteAll( root->children );
root->children.clear();
q->reset();
rebuildTree( pages );
}
void AnnotationModelPrivate::notifyPageChanged( int page, int flags )
{
// we are strictly interested in annotations
if ( !(flags & Okular::DocumentObserver::Annotations ) )
return;
QLinkedList< Okular::Annotation* > annots = document->page( page )->annotations();
int annItemIndex = -1;
AnnItem *annItem = findItem( page, &annItemIndex );
// case 1: the page has no more annotations
// => remove the branch, if any
if ( annots.isEmpty() )
{
if ( annItem )
{
q->beginRemoveRows( indexForItem( root ), annItemIndex, annItemIndex );
delete root->children.at( annItemIndex );
root->children.removeAt( annItemIndex );
q->endRemoveRows();
}
return;
}
// case 2: no existing branch
// => add a new branch, and add the annotations for the page
if ( !annItem )
{
int i = 0;
while ( i < root->children.count() && root->children.at( i )->page < page ) ++i;
AnnItem *annItem = new AnnItem();
annItem->page = page;
annItem->parent = root;
q->beginInsertRows( indexForItem( root ), i, i );
annItem->parent->children.insert( i, annItem );
q->endInsertRows();
QLinkedList< Okular::Annotation* >::ConstIterator it = annots.begin(), itEnd = annots.end();
int newid = 0;
for ( ; it != itEnd; ++it, ++newid )
{
q->beginInsertRows( indexForItem( annItem ), newid, newid );
new AnnItem( annItem, *it );
q->endInsertRows();
}
return;
}
// case 3: existing branch, less annotations than items
// => lookup and remove the annotations
if ( annItem->children.count() > annots.count() )
{
for ( int i = annItem->children.count(); i > 0; --i )
{
Okular::Annotation *ref = annItem->children.at( i - 1 )->annotation;
bool found = false;
QLinkedList< Okular::Annotation* >::ConstIterator it = annots.begin(), itEnd = annots.end();
for ( ; !found && it != itEnd; ++it )
{
if ( ( *it ) == ref )
found = true;
}
if ( !found )
{
q->beginRemoveRows( indexForItem( annItem ), i - 1, i - 1 );
delete annItem->children.at( i - 1 );
annItem->children.removeAt( i - 1 );
q->endRemoveRows();
}
}
return;
}
// case 4: existing branch, less items than annotations
// => lookup and add annotations if not in the branch
if ( annots.count() > annItem->children.count() )
{
QLinkedList< Okular::Annotation* >::ConstIterator it = annots.begin(), itEnd = annots.end();
for ( ; it != itEnd; ++it )
{
Okular::Annotation *ref = *it;
bool found = false;
int count = annItem->children.count();
for ( int i = 0; !found && i < count; ++i )
{
if ( ref == annItem->children.at( i )->annotation )
found = true;
}
if ( !found )
{
q->beginInsertRows( indexForItem( annItem ), count, count );
new AnnItem( annItem, ref );
q->endInsertRows();
}
}
return;
}
// case 5: the data of some annotation changed
// TODO: what do we do in this case?
// FIXME: for now, update ALL the annotations for that page
for ( int i = 0; i < annItem->children.count(); ++i )
{
QModelIndex index = indexForItem( annItem->children.at( i ) );
emit q->dataChanged( index, index );
}
}
QModelIndex AnnotationModelPrivate::indexForItem( AnnItem *item ) const
{
if ( item->parent )
{
int id = item->parent->children.indexOf( item );
if ( id >= 0 && id < item->parent->children.count() )
return q->createIndex( id, 0, item );
}
return QModelIndex();
}
void AnnotationModelPrivate::rebuildTree( const QVector< Okular::Page * > &pages )
{
if ( pages.isEmpty() )
return;
emit q->layoutAboutToBeChanged();
for ( int i = 0; i < pages.count(); ++i )
{
QLinkedList< Okular::Annotation* > annots = pages.at( i )->annotations();
if ( annots.isEmpty() )
continue;
AnnItem *annItem = new AnnItem( root, i );
QLinkedList< Okular::Annotation* >::ConstIterator it = annots.begin(), itEnd = annots.end();
for ( ; it != itEnd; ++it )
{
new AnnItem( annItem, *it );
}
}
emit q->layoutChanged();
}
AnnItem* AnnotationModelPrivate::findItem( int page, int *index ) const
{
for ( int i = 0; i < root->children.count(); ++i )
{
AnnItem *tmp = root->children.at( i );
if ( tmp->page == page )
{
if ( index )
*index = i;
return tmp;
}
}
if ( index )
*index = -1;
return 0;
}
AnnotationModel::AnnotationModel( Okular::Document *document, QObject *parent )
: QAbstractItemModel( parent ), d( new AnnotationModelPrivate( this ) )
{
d->document = document;
d->document->addObserver( d );
}
AnnotationModel::~AnnotationModel()
{
if ( d->document )
d->document->removeObserver( d );
delete d;
}
int AnnotationModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return 1;
}
QVariant AnnotationModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() )
return QVariant();
AnnItem *item = static_cast< AnnItem* >( index.internalPointer() );
if ( !item->annotation )
{
if ( role == Qt::DisplayRole )
return i18n( "Page %1", item->page + 1 );
else if ( role == Qt::DecorationRole )
return KIcon( "text-plain" );
else if ( role == PageRole )
return item->page;
return QVariant();
}
switch ( role )
{
case Qt::DisplayRole:
return GuiUtils::captionForAnnotation( item->annotation );
break;
case Qt::DecorationRole:
return KIcon( "okular" );
break;
case Qt::ToolTipRole:
return GuiUtils::prettyToolTip( item->annotation );
break;
case AuthorRole:
return item->annotation->author();
break;
case PageRole:
return item->page;
break;
}
return QVariant();
}
bool AnnotationModel::hasChildren( const QModelIndex &parent ) const
{
if ( !parent.isValid() )
return true;
AnnItem *item = static_cast< AnnItem* >( parent.internalPointer() );
return !item->children.isEmpty();
}
QVariant AnnotationModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation != Qt::Horizontal )
return QVariant();
if ( section == 0 && role == Qt::DisplayRole )
return "Annotations";
return QVariant();
}
QModelIndex AnnotationModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( row < 0 || column != 0 )
return QModelIndex();
AnnItem *item = parent.isValid() ? static_cast< AnnItem* >( parent.internalPointer() ) : d->root;
if ( row < item->children.count() )
return createIndex( row, column, item->children.at( row ) );
return QModelIndex();
}
QModelIndex AnnotationModel::parent( const QModelIndex &index ) const
{
if ( !index.isValid() )
return QModelIndex();
AnnItem *item = static_cast< AnnItem* >( index.internalPointer() );
return d->indexForItem( item->parent );
}
int AnnotationModel::rowCount( const QModelIndex &parent ) const
{
AnnItem *item = parent.isValid() ? static_cast< AnnItem* >( parent.internalPointer() ) : d->root;
return item->children.count();
}
bool AnnotationModel::isAnnotation( const QModelIndex &index ) const
{
return annotationForIndex( index );
}
Okular::Annotation* AnnotationModel::annotationForIndex( const QModelIndex &index ) const
{
if ( !index.isValid() )
return 0;
AnnItem *item = static_cast< AnnItem* >( index.internalPointer() );
return item->annotation;
}
#include "annotationmodel.moc"
|