[Marble-commits] KDE/kdeedu/marble/src/lib

Thibaut Gridel tgridel at free.fr
Fri Sep 11 22:38:51 CEST 2009


SVN commit 1022471 by tgridel:

move file storage from FileViewModel to PlacemarkManager

 M  +1 -1      AbstractFileViewItem.h  
 M  +35 -79    FileViewModel.cpp  
 M  +9 -10     FileViewModel.h  
 M  +5 -10     GpxFileViewItem.cpp  
 M  +1 -1      GpxFileViewItem.h  
 M  +11 -14    KmlFileViewItem.cpp  
 M  +1 -1      KmlFileViewItem.h  
 M  +3 -1      MarbleControlBox.cpp  
 M  +1 -1      MarbleModel.cpp  
 M  +67 -10    PlacemarkManager.cpp  
 M  +12 -0     PlacemarkManager.h  


--- trunk/KDE/kdeedu/marble/src/lib/AbstractFileViewItem.h #1022470:1022471
@@ -29,9 +29,9 @@
     virtual void saveFile() = 0;
     virtual void closeFile( int start, bool finalize = true ) = 0;
     virtual int size() const { return 0; };
-    virtual QVariant data( int role = Qt::DisplayRole ) const = 0;
     virtual bool isShown() const = 0;
     virtual void setShown( bool value ) = 0;
+    virtual QString name() const = 0;
 };
 
 }
--- trunk/KDE/kdeedu/marble/src/lib/FileViewModel.cpp #1022470:1022471
@@ -15,27 +15,23 @@
 // Other
 #include "AbstractFileViewItem.h"
 
-#include <QStringList>
-
 using namespace Marble;
 
 FileViewModel::FileViewModel( QObject* parent ) :
-    QAbstractListModel( parent )
+    QAbstractListModel( parent ),
+    m_selectionModel(new QItemSelectionModel(this))
 {
 }
 
 FileViewModel::~FileViewModel()
 {
-    foreach ( AbstractFileViewItem* item, m_itemList ) {
-        delete item;
-    }
 }
 
 int FileViewModel::rowCount( const QModelIndex & parent ) const
 {
     Q_UNUSED( parent );
 
-    return m_itemList.count();
+    return m_manager->size();
 }
 
 QVariant FileViewModel::data( const QModelIndex & index, int role ) const
@@ -46,15 +42,15 @@
 
     int row = index.row();
 
-    if ( row < m_itemList.count() ) {
+    if ( row < m_manager->size() ) {
         if ( index.column() == 0 ) {
-            const AbstractFileViewItem& item = *m_itemList.at( row );
+            const AbstractFileViewItem& item = *m_manager->at( row );
 
             if ( role == Qt::CheckStateRole ) {
                 return item.isShown () ? Qt::Checked : Qt::Unchecked;
             }
-            else {
-                return item.data( role );
+            if ( role == Qt::DisplayRole ) {
+                return item.name();
             }
         }
     }
@@ -71,17 +67,6 @@
                           Qt::ItemIsSelectable );
 }
 
-QStringList FileViewModel::containers() const
-{
-    QStringList retList;
-
-    for( int line = 0; line < m_itemList.count(); ++line ) {
-        retList << m_itemList.at( line )->data().toString();
-    }
-    
-    return retList;
-}
-
 bool FileViewModel::setData( const QModelIndex& index, const QVariant& value, int role )
 {
     if ( !index.isValid() ) {
@@ -90,11 +75,11 @@
 
     int row = index.row();
 
-    if ( row < m_itemList.count() ) {
+    if ( row < m_manager->size() ) {
         if ( index.column() == 0 ) {
             if ( role == Qt::CheckStateRole ) {
 
-                AbstractFileViewItem& item = *m_itemList.at( row );
+                AbstractFileViewItem& item = *m_manager->at( row );
                 bool newValue = value.toBool ();
 
                 if ( item.isShown() != newValue ) {
@@ -111,76 +96,47 @@
     return false;
 }
 
-void FileViewModel::setSelectedIndex( const QModelIndex& index )
+void FileViewModel::saveFile()
 {
-    m_selectedIndex = index;
+    if ( m_selectionModel->hasSelection() )
+    {
+        m_manager->saveFile( m_selectionModel->selectedRows().first().row() );
+    }
 }
 
-void FileViewModel::append( AbstractFileViewItem* item )
+void FileViewModel::closeFile()
 {
-    m_itemList.append( item );
-    emit layoutChanged();
-    emit modelChanged();
+    if ( m_selectionModel->hasSelection() )
+    {
+        m_manager->closeFile( m_selectionModel->selectedRows().first().row() );
+    }
 }
-
-void FileViewModel::remove( const QModelIndex& index )
+void FileViewModel::setPlacemarkManager( PlacemarkManager *placemarkManager)
 {
-    if ( index.isValid() ) {
-
-        int row = index.row();
-        int start = indexStart( index );
-        if ( row < m_itemList.count() ) {
-            if ( index.column() == 0 ) {
-
-                AbstractFileViewItem *item = m_itemList.at( row );
-                item->closeFile( start );
-
-                delete item;
-                m_itemList.removeAt( row );
-
-                emit layoutChanged();
-                emit modelChanged();
-            }
-        }
-    }
+    m_manager = placemarkManager;
+    connect (m_manager, SIGNAL(fileAdded(int)),
+             this, SLOT(append(int)));
+    connect (m_manager, SIGNAL(fileRemoved(int)),
+             this, SLOT(remove(int)));
 }
 
-void FileViewModel::saveFile()
+QItemSelectionModel * FileViewModel::selectionModel()
 {
-    if ( m_selectedIndex.isValid() ) {
-
-        int row = m_selectedIndex.row();
-
-        if ( row < m_itemList.count() ) {
-            if ( m_selectedIndex.column() == 0 ) {
-
-                AbstractFileViewItem& item = *m_itemList.at( row );
-                item.saveFile();
-
-/*                delete &item;
-                m_itemList.removeAt( row );
-
-                emit layoutChanged();
-                emit modelChanged();*/
-            }
-        }
-    }
+    return m_selectionModel;
 }
 
-void FileViewModel::closeFile()
+void FileViewModel::append( int order )
 {
-    remove( m_selectedIndex );
+    beginInsertRows(QModelIndex(), order, order);
+    endInsertRows();
+    emit modelChanged();
 }
 
-int FileViewModel::indexStart( const QModelIndex& index )
+void FileViewModel::remove( int index )
 {
-    int start = 0;
-    if ( index.isValid() ) {
-        for( int i = 0; i < index.row(); i++ ) {
-            start += m_itemList.at( i )->size();
-        };
-    }
-    return start;
+    beginRemoveRows(QModelIndex(), index, index);
+    endRemoveRows();
+    emit modelChanged();
 }
 
 #include "FileViewModel.moc"
--- trunk/KDE/kdeedu/marble/src/lib/FileViewModel.h #1022470:1022471
@@ -14,12 +14,13 @@
 #define FILEVIEWMODEL_H
 
 #include <QtCore/QAbstractListModel>
-#include <QtCore/QList>
-#include <QtCore/QModelIndex>
 #include <QtCore/QVariant>
+#include <QtGui/QItemSelectionModel>
 
 #include "marble_export.h"
 
+#include "PlacemarkManager.h"
+
 namespace Marble
 {
 
@@ -38,11 +39,8 @@
     virtual Qt::ItemFlags flags( const QModelIndex& index ) const;
     virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole );
 
-    void setSelectedIndex( const QModelIndex& index );
-    void append( AbstractFileViewItem* item );
-    void remove( const QModelIndex& index );
-    
-    QStringList containers() const;
+    void setPlacemarkManager( PlacemarkManager * placemarkManager );
+    QItemSelectionModel * selectionModel();
 
   Q_SIGNALS:
     void modelChanged();
@@ -50,13 +48,14 @@
   public slots:
     void saveFile();
     void closeFile();
+    void append( int item );
+    void remove( int item );
 
   private:
     Q_DISABLE_COPY( FileViewModel )
-    QModelIndex m_selectedIndex;
-    QList < AbstractFileViewItem* > m_itemList;
+    QItemSelectionModel *m_selectionModel;
+    PlacemarkManager *m_manager;
     
-    int indexStart( const QModelIndex& index );
 };
 
 }
--- trunk/KDE/kdeedu/marble/src/lib/GpxFileViewItem.cpp #1022470:1022471
@@ -62,16 +62,6 @@
     //TODO
 }
 
-QVariant GpxFileViewItem::data( int role ) const
-{
-    if( role == Qt::DisplayRole )
-        return m_gpxFile->display();
-    else if( role == AbstractFileViewItem::FilePointerRole )
-        return qVariantFromValue(m_gpxFile);
-    else
-        return QVariant();
-}
-
 bool GpxFileViewItem::isShown() const
 {
     return (m_gpxFile->checkState() == Qt::Checked );
@@ -81,3 +71,8 @@
 {
     m_gpxFile->setCheckState( value );
 }
+
+QString GpxFileViewItem::name() const
+{
+    return m_gpxFile->display();
+}
--- trunk/KDE/kdeedu/marble/src/lib/GpxFileViewItem.h #1022470:1022471
@@ -35,9 +35,9 @@
      */
     virtual void saveFile();
     virtual void closeFile( int start, bool finalize = true );
-    virtual QVariant data( int role = Qt::DisplayRole ) const;
     virtual bool isShown() const;
     virtual void setShown( bool value );
+    virtual QString name() const;
 
   private:
     Q_DISABLE_COPY( GpxFileViewItem )
--- trunk/KDE/kdeedu/marble/src/lib/KmlFileViewItem.cpp #1022470:1022471
@@ -46,20 +46,6 @@
     m_placemarkManager.model()->removePlacemarks( m_document.fileName(), start, size(), finalize );
 }
 
-QVariant KmlFileViewItem::data( int role ) const
-{
-    if( role == Qt::DisplayRole ) {
-        if(!m_document.name().isEmpty())
-            return m_document.name();
-        else if(!m_document.fileName().isEmpty())
-            return m_document.fileName();
-        else
-            return QString("KML Document");
-    }
-    else
-        return QVariant();
-}
-
 int KmlFileViewItem::size() const
 {
     return m_document.placemarks().size();
@@ -74,3 +60,14 @@
 {
     m_document.setVisible( value );
 }
+
+QString KmlFileViewItem::name() const
+{
+    if(!m_document.name().isEmpty())
+        return m_document.name();
+    else if(!m_document.fileName().isEmpty())
+        return m_document.fileName();
+    else
+        return QString("KML Document");
+
+}
--- trunk/KDE/kdeedu/marble/src/lib/KmlFileViewItem.h #1022470:1022471
@@ -32,10 +32,10 @@
      */
     virtual void saveFile();
     virtual void closeFile( int start, bool finalize = true );
-    virtual QVariant data( int role = Qt::DisplayRole ) const;
     virtual bool isShown() const;
     virtual void setShown( bool value );
     virtual int size() const;
+    virtual QString name() const;
 
     GeoDataDocument* document();
   private:
--- trunk/KDE/kdeedu/marble/src/lib/MarbleControlBox.cpp #1022470:1022471
@@ -270,7 +270,9 @@
 
     //set up everything for the FileModel
     d->uiWidget.m_fileView->setModel( widget->fileViewModel() );
-
+    delete d->uiWidget.m_fileView->selectionModel();
+    d->uiWidget.m_fileView->setSelectionModel(
+            widget->fileViewModel()->selectionModel());
     connect( d->uiWidget.m_fileView->selectionModel(),
 	     SIGNAL( selectionChanged( QItemSelection, QItemSelection )),
 	     this,
--- trunk/KDE/kdeedu/marble/src/lib/MarbleModel.cpp #1022470:1022471
@@ -700,7 +700,7 @@
     GpxFile* gpxFile = new GpxFile( filename );
     GpxFileViewItem* item = new GpxFileViewItem( gpxFile );
 
-    d->m_dataFacade->fileViewModel()->append( item );
+    d->m_placemarkmanager->addFile( item );
     d->m_gpxFileModel->addFile( gpxFile );
 }
 
--- trunk/KDE/kdeedu/marble/src/lib/PlacemarkManager.cpp #1022470:1022471
@@ -48,6 +48,7 @@
         MarbleDataFacade* m_datafacade;
         QList<PlacemarkLoader*> m_loaderList;
         QStringList m_pathList;
+        QList < AbstractFileViewItem* > m_fileItemList;
 };
 }
 
@@ -67,6 +68,11 @@
         }
     }
 
+    foreach( AbstractFileViewItem *file, d->m_fileItemList)
+    {
+        delete file;
+    }
+
     delete d;
 }
 
@@ -78,11 +84,16 @@
 void PlacemarkManager::setDataFacade( MarbleDataFacade *facade )
 {
     d->m_datafacade = facade;
+    d->m_datafacade->fileViewModel()->setPlacemarkManager(this);
 }
 
 QStringList PlacemarkManager::containers() const
 {
-    return d->m_datafacade->fileViewModel()->containers() + d->m_pathList;
+    QStringList retList;
+    for( int line = 0; line < d->m_fileItemList.count(); ++line ) {
+        retList << d->m_fileItemList.at( line )->name();
+    }
+    return retList + d->m_pathList;
 }
 
 QString PlacemarkManager::toRegularName( QString name )
@@ -102,12 +113,11 @@
 
 void PlacemarkManager::addGeoDataDocument( GeoDataDocument* document )
 {
-    AbstractFileViewItem* item = new KmlFileViewItem( *this, *document );
+    KmlFileViewItem* item = new KmlFileViewItem( *this, *document );
+    addFile( item );
 
-    d->m_datafacade->fileViewModel()->append( item );
-
     // now get the document that will be preserved throughout the life time
-    GeoDataDocument* doc = dynamic_cast<KmlFileViewItem*>(item)->document();
+    GeoDataDocument* doc = item->document();
     // remove the hashes in front of the styles.
     QVector<GeoDataFeature>::Iterator end = doc->end();
     QVector<GeoDataFeature>::Iterator itr = doc->begin();
@@ -135,17 +145,55 @@
 void PlacemarkManager::removePlacemarkKey( const QString& key )
 {
     QString nkey = key;
-    FileViewModel *fileViewModel = d->m_datafacade->fileViewModel();
     qDebug() << "trying to remove file:" << key;
-    for( int i = 0; i < fileViewModel->rowCount(); ++i )
+    for( int i = 0; i < d->m_fileItemList.size(); ++i )
     {
-        if( toRegularName( nkey ) == toRegularName( fileViewModel->data(fileViewModel->index(i, 0)).toString() ) ) {
-            fileViewModel->remove(fileViewModel->index(i, 0));
+        if( toRegularName( nkey ) == toRegularName( d->m_fileItemList.at(i)->name() ) ) {
+            closeFile(i);
             break;
         }
-    };
+    }
 }
 
+void PlacemarkManager::addFile ( AbstractFileViewItem * item )
+{
+    d->m_fileItemList.append( item );
+    emit fileAdded(d->m_fileItemList.indexOf( item ) );
+}
+
+void PlacemarkManager::saveFile( int index )
+{
+    if (index < d->m_fileItemList.size() )
+    {
+        d->m_fileItemList.at( index )->saveFile();
+    }
+}
+
+void PlacemarkManager::closeFile( int index )
+{
+    if (index < d->m_fileItemList.size() )
+    {
+        d->m_fileItemList.at( index )->closeFile( indexStart( index ));
+        delete d->m_fileItemList.at( index );
+        d->m_fileItemList.removeAt( index );
+        emit fileRemoved( index );
+    }
+}
+
+int PlacemarkManager::size() const
+{
+    return d->m_fileItemList.size();
+}
+
+AbstractFileViewItem * PlacemarkManager::at( int index )
+{
+    if (index < d->m_fileItemList.size() )
+    {
+        return d->m_fileItemList.at( index );
+    }
+    return 0;
+}
+
 void PlacemarkManager::appendLoader( PlacemarkLoader *loader )
 {
     connect (   loader, SIGNAL( placemarksLoaded( PlacemarkLoader*, PlacemarkContainer * ) ),
@@ -189,4 +237,13 @@
     }
 }
 
+int PlacemarkManager::indexStart( int index )
+{
+    int start = 0;
+    for( int i = 0; i < index; i++ ) {
+        start += d->m_fileItemList.at( i )->size();
+    }
+    return start;
+}
+
 #include "PlacemarkManager.moc"
--- trunk/KDE/kdeedu/marble/src/lib/PlacemarkManager.h #1022470:1022471
@@ -88,9 +88,19 @@
     */
     void addPlacemarkData( const QString& data, const QString& key );
 
+    void addFile( AbstractFileViewItem * item );
+    void saveFile( int index );
+    void closeFile( int index );
+
+    int size() const;
+    AbstractFileViewItem * at( int index );
+
+
  Q_SIGNALS:
     void geoDataDocumentAdded( const GeoDataDocument& );
     void finalize();
+    void fileAdded( int index );
+    void fileRemoved( int index );
 
  private Q_SLOTS:
     void loadPlacemarkContainer( PlacemarkLoader* loader, PlacemarkContainer * );
@@ -99,6 +109,8 @@
 
  private:
 
+    int indexStart( int index );
+
     void appendLoader( PlacemarkLoader *loader );
 
     /**


More information about the Marble-commits mailing list