ItemRelation: new template for making up relations between ResourceItems
The new generic ItemRelation template class replaces ResourceTreeItem's, resulting in cleaner and better organized code. In the future other kind of items can be linked hierarchically using the ItemRelation template.
This commit is contained in:
@@ -64,7 +64,7 @@ private slots:
|
||||
|
||||
|
||||
private:
|
||||
void readDir( const QString & _dir, ResourceTreeItem * _parent );
|
||||
void readDir( const QString & _dir, ResourceItem::Relation * _parent );
|
||||
|
||||
ResourceItem::BaseDirectory m_baseDir;
|
||||
const QString m_dir;
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtXml/QDomDocument>
|
||||
|
||||
#include "ResourceTreeItem.h"
|
||||
#include "ResourceItem.h"
|
||||
|
||||
|
||||
class ResourceDB : public QObject
|
||||
@@ -64,7 +64,7 @@ public:
|
||||
return m_items;
|
||||
}
|
||||
|
||||
inline ResourceTreeItem * topLevelNode()
|
||||
inline ResourceItem::Relation * topLevelNode()
|
||||
{
|
||||
return &m_topLevelNode;
|
||||
}
|
||||
@@ -82,14 +82,14 @@ public:
|
||||
// add given item to DB
|
||||
void addItem( ResourceItem * _newItem );
|
||||
|
||||
void recursiveRemoveItems( ResourceTreeItem * parent,
|
||||
void recursiveRemoveItems( ResourceItem::Relation * parent,
|
||||
bool removeTopLevelParent = true );
|
||||
|
||||
|
||||
private:
|
||||
void saveTreeItem( const ResourceTreeItem * _i, QDomDocument & _doc,
|
||||
void saveRelation( const ResourceItem::Relation * _i, QDomDocument & _doc,
|
||||
QDomElement & _de );
|
||||
void loadTreeItem( ResourceTreeItem * _i, QDomElement & _de );
|
||||
void loadRelation( ResourceItem::Relation * _i, QDomElement & _de );
|
||||
|
||||
static inline QString typeName( ResourceItem::Type _t )
|
||||
{
|
||||
@@ -136,7 +136,7 @@ private:
|
||||
|
||||
ResourceProvider * m_provider;
|
||||
ItemHashMap m_items;
|
||||
ResourceTreeItem m_topLevelNode;
|
||||
ResourceItem::Relation m_topLevelNode;
|
||||
|
||||
|
||||
signals:
|
||||
|
||||
@@ -31,13 +31,14 @@
|
||||
#include <QtCore/QList>
|
||||
|
||||
#include "ResourceProvider.h"
|
||||
#include "TreeRelation.h"
|
||||
|
||||
class ResourceTreeItem;
|
||||
class ResourceModel;
|
||||
|
||||
class ResourceItem
|
||||
{
|
||||
public:
|
||||
typedef TreeRelation<ResourceItem> Relation;
|
||||
|
||||
enum BaseDirectories
|
||||
{
|
||||
BaseRoot,
|
||||
@@ -78,12 +79,12 @@ public:
|
||||
// copy constructor
|
||||
ResourceItem( const ResourceItem & _item );
|
||||
|
||||
inline void setHidden( bool _h, const ResourceModel * _model )
|
||||
inline void setHidden( bool _h, const QAbstractItemModel * _model )
|
||||
{
|
||||
m_hidden[_model] = _h;
|
||||
}
|
||||
|
||||
inline bool isHidden( const ResourceModel * _model ) const
|
||||
inline bool isHidden( const QAbstractItemModel * _model ) const
|
||||
{
|
||||
return m_hidden[_model];
|
||||
}
|
||||
@@ -174,19 +175,19 @@ public:
|
||||
return m_type != TypeUnknown && !m_name.isEmpty();
|
||||
}
|
||||
|
||||
void setTreeItem( ResourceTreeItem * _ti )
|
||||
void setRelation( Relation * _relation )
|
||||
{
|
||||
m_treeItem = _ti;
|
||||
m_relation = _relation;
|
||||
}
|
||||
|
||||
ResourceTreeItem * treeItem()
|
||||
Relation * relation()
|
||||
{
|
||||
return m_treeItem;
|
||||
return m_relation;
|
||||
}
|
||||
|
||||
const ResourceTreeItem * treeItem() const
|
||||
const Relation * relation() const
|
||||
{
|
||||
return m_treeItem;
|
||||
return m_relation;
|
||||
}
|
||||
|
||||
const QDateTime & lastMod() const
|
||||
@@ -242,9 +243,9 @@ private:
|
||||
QDateTime m_lastMod;
|
||||
QString m_tags;
|
||||
|
||||
QHash<const ResourceModel *, bool> m_hidden;
|
||||
QHash<const QAbstractItemModel *, bool> m_hidden;
|
||||
|
||||
ResourceTreeItem * m_treeItem;
|
||||
Relation * m_relation;
|
||||
|
||||
} ;
|
||||
|
||||
@@ -252,4 +253,12 @@ private:
|
||||
typedef QList<ResourceItem *> ResourceItemList;
|
||||
|
||||
|
||||
#define foreachResourceItemRelation(list) \
|
||||
for(ResourceItem::Relation::List::Iterator it=list.begin(); \
|
||||
it!=list.end();++it)
|
||||
|
||||
#define foreachConstResourceItemRelation(list) \
|
||||
for(ResourceItem::Relation::List::ConstIterator it=list.begin();\
|
||||
it!=list.end();++it)
|
||||
|
||||
#endif
|
||||
|
||||
@@ -56,16 +56,15 @@ public:
|
||||
virtual QMimeData * mimeData( const QModelIndexList & _indexes ) const;
|
||||
|
||||
// return ResourceTreeItem belonging to a certain index
|
||||
static inline ResourceTreeItem * treeItem( const QModelIndex & _idx )
|
||||
static inline ResourceItem::Relation * relation( const QModelIndex & _idx )
|
||||
{
|
||||
return static_cast<ResourceTreeItem *>(
|
||||
_idx.internalPointer() );
|
||||
return static_cast<ResourceItem::Relation *>( _idx.internalPointer() );
|
||||
}
|
||||
|
||||
// return ResourceItem belonging to a certain index
|
||||
static inline ResourceItem * item( const QModelIndex & _idx )
|
||||
{
|
||||
return treeItem( _idx )->item();
|
||||
return relation( _idx )->item();
|
||||
}
|
||||
|
||||
int totalItems() const;
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* ResourceTreeItem.h - header file for ResourceTreeItem
|
||||
*
|
||||
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* 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 (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _RESOURCE_TREE_ITEM_H
|
||||
#define _RESOURCE_TREE_ITEM_H
|
||||
|
||||
#include <QtCore/QList>
|
||||
|
||||
#include "ResourceItem.h"
|
||||
|
||||
#define foreachResourceTreeItem(list) \
|
||||
for(ResourceTreeItemList::Iterator it=list.begin(); \
|
||||
it!=list.end();++it)
|
||||
|
||||
#define foreachConstResourceTreeItem(list) \
|
||||
for(ResourceTreeItemList::ConstIterator it=list.begin();\
|
||||
it!=list.end();++it)
|
||||
|
||||
|
||||
class ResourceTreeItem;
|
||||
typedef QList<ResourceTreeItem *> ResourceTreeItemList;
|
||||
|
||||
|
||||
class ResourceTreeItem
|
||||
{
|
||||
public:
|
||||
ResourceTreeItem( ResourceTreeItem * _parent = NULL,
|
||||
ResourceItem * _item = NULL );
|
||||
|
||||
~ResourceTreeItem();
|
||||
|
||||
int rowCount( const ResourceModel * _model = NULL ) const;
|
||||
|
||||
ResourceTreeItem * getChild( int _row,
|
||||
const ResourceModel * _model = NULL );
|
||||
|
||||
int row( const ResourceModel * _model = NULL ) const;
|
||||
|
||||
inline void addChild( ResourceTreeItem * _it )
|
||||
{
|
||||
m_children.push_back( _it );
|
||||
}
|
||||
|
||||
inline void removeChild( ResourceTreeItem * _it )
|
||||
{
|
||||
m_children.removeAll( _it );
|
||||
}
|
||||
|
||||
inline ResourceTreeItemList & children()
|
||||
{
|
||||
return m_children;
|
||||
}
|
||||
|
||||
inline const ResourceTreeItemList & children() const
|
||||
{
|
||||
return m_children;
|
||||
}
|
||||
|
||||
ResourceTreeItem * findChild( const QString & _name,
|
||||
ResourceItem::BaseDirectory _base_dir );
|
||||
|
||||
inline ResourceItem * item()
|
||||
{
|
||||
return m_item;
|
||||
}
|
||||
|
||||
inline const ResourceItem * item() const
|
||||
{
|
||||
return m_item;
|
||||
}
|
||||
|
||||
inline ResourceTreeItem * parent()
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
inline void setParent( ResourceTreeItem * _parent )
|
||||
{
|
||||
m_parent = _parent;
|
||||
}
|
||||
|
||||
inline bool temporaryMarker() const
|
||||
{
|
||||
return m_temporaryMarker;
|
||||
}
|
||||
|
||||
inline void setTemporaryMarker( bool _on )
|
||||
{
|
||||
m_temporaryMarker = _on;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
// hide copy-ctor
|
||||
ResourceTreeItem( const ResourceTreeItem & ) { }
|
||||
|
||||
ResourceTreeItem * m_parent;
|
||||
ResourceTreeItemList m_children;
|
||||
|
||||
bool m_temporaryMarker;
|
||||
|
||||
ResourceItem * m_item;
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
#endif
|
||||
@@ -47,11 +47,11 @@ public:
|
||||
|
||||
|
||||
private:
|
||||
bool filterItems( ResourceTreeItem * _item,
|
||||
const QModelIndex & _parent,
|
||||
bool filterItems( ResourceItem::Relation * _item,
|
||||
const QModelIndex & _parent,
|
||||
const QStringList & _keywords );
|
||||
void setHidden( ResourceTreeItem * _item,
|
||||
const QModelIndex & _parent,
|
||||
void setHidden( ResourceItem::Relation * _item,
|
||||
const QModelIndex & _parent,
|
||||
bool _hidden,
|
||||
bool _recursive = true );
|
||||
|
||||
|
||||
227
include/TreeRelation.h
Normal file
227
include/TreeRelation.h
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* TreeRelation.h - header file for TreeRelation
|
||||
*
|
||||
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* 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 (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _TREE_RELATION_H
|
||||
#define _TREE_RELATION_H
|
||||
|
||||
#include <QtCore/QList>
|
||||
|
||||
class QAbstractItemModel;
|
||||
|
||||
#define foreachTreeRelation(list) \
|
||||
for(typename TreeRelation::List::Iterator it=list.begin(); \
|
||||
it!=list.end();++it)
|
||||
|
||||
#define foreachConstTreeRelation(list) \
|
||||
for(typename TreeRelation::List::ConstIterator it=list.begin(); \
|
||||
it!=list.end();++it)
|
||||
|
||||
|
||||
template<class T>
|
||||
class TreeRelation
|
||||
{
|
||||
public:
|
||||
typedef TreeRelation<T> ThisTreeRelation;
|
||||
typedef QList<ThisTreeRelation *> List;
|
||||
|
||||
TreeRelation( ThisTreeRelation * _parent = NULL, T * _item = NULL ) :
|
||||
m_parent( _parent ),
|
||||
m_temporaryMarker( false ),
|
||||
m_item( _item )
|
||||
{
|
||||
if( parent() )
|
||||
{
|
||||
parent()->addChild( this );
|
||||
}
|
||||
if( item() )
|
||||
{
|
||||
item()->setRelation( this );
|
||||
}
|
||||
}
|
||||
|
||||
~TreeRelation()
|
||||
{
|
||||
foreachTreeRelation( children() )
|
||||
{
|
||||
delete *it;
|
||||
}
|
||||
if( item() )
|
||||
{
|
||||
item()->setRelation( NULL );
|
||||
}
|
||||
if( parent() )
|
||||
{
|
||||
parent()->removeChild( this );
|
||||
}
|
||||
}
|
||||
|
||||
int rowCount( const QAbstractItemModel * _model = NULL ) const
|
||||
{
|
||||
int rc = 0;
|
||||
foreachConstTreeRelation( children() )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
++rc;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
ThisTreeRelation * getChild( int _row,
|
||||
const QAbstractItemModel * _model = NULL )
|
||||
{
|
||||
int rc = 0;
|
||||
foreachTreeRelation( children() )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
if( rc == _row )
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
++rc;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int row( const QAbstractItemModel * _model = NULL ) const
|
||||
{
|
||||
if( !parent() )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int row = 0;
|
||||
foreachConstTreeRelation( parent()->children() )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
if( *it == this )
|
||||
{
|
||||
return row;
|
||||
}
|
||||
++row;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void addChild( ThisTreeRelation * _it )
|
||||
{
|
||||
children().push_back( _it );
|
||||
}
|
||||
|
||||
void removeChild( ThisTreeRelation * _it )
|
||||
{
|
||||
children().removeAll( _it );
|
||||
}
|
||||
|
||||
TreeRelation::List & children()
|
||||
{
|
||||
return m_children;
|
||||
}
|
||||
|
||||
const TreeRelation::List & children() const
|
||||
{
|
||||
return m_children;
|
||||
}
|
||||
|
||||
ThisTreeRelation * findChild( const QString & _name, int _base_dir )
|
||||
{
|
||||
if( _name.isEmpty() )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int hash = qHash( _name );
|
||||
|
||||
foreachTreeRelation( children() )
|
||||
{
|
||||
ThisTreeRelation * treeRel = *it;
|
||||
const T * item = treeRel->item();
|
||||
if( item &&
|
||||
item->nameHash() == hash &&
|
||||
item->name() == _name &&
|
||||
item->baseDir() == _base_dir )
|
||||
{
|
||||
return treeRel;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const ThisTreeRelation * parent() const
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
ThisTreeRelation * parent()
|
||||
{
|
||||
return m_parent;
|
||||
}
|
||||
|
||||
void setParent( ThisTreeRelation * _parent )
|
||||
{
|
||||
m_parent = _parent;
|
||||
}
|
||||
|
||||
T * item()
|
||||
{
|
||||
return m_item;
|
||||
}
|
||||
|
||||
const T * item() const
|
||||
{
|
||||
return m_item;
|
||||
}
|
||||
|
||||
bool temporaryMarker() const
|
||||
{
|
||||
return m_temporaryMarker;
|
||||
}
|
||||
|
||||
void setTemporaryMarker( bool _on )
|
||||
{
|
||||
m_temporaryMarker = _on;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
// hide copy-ctor
|
||||
TreeRelation( const ThisTreeRelation & ) { }
|
||||
|
||||
ThisTreeRelation * m_parent;
|
||||
TreeRelation::List m_children;
|
||||
|
||||
bool m_temporaryMarker;
|
||||
|
||||
T * m_item;
|
||||
|
||||
} ;
|
||||
|
||||
|
||||
#endif
|
||||
@@ -68,10 +68,10 @@ private slots:
|
||||
|
||||
|
||||
private:
|
||||
ResourceTreeItem * addTreeItem( ResourceTreeItem * _parent,
|
||||
ResourceItem * _item );
|
||||
ResourceItem::Relation * addRelation( ResourceItem::Relation * _parent,
|
||||
ResourceItem * _item );
|
||||
void importNodeIntoDB( const QDomNode & n,
|
||||
ResourceTreeItem * _parent );
|
||||
ResourceItem::Relation * _parent );
|
||||
void download( const QString & _path, QBuffer * _target ) const;
|
||||
|
||||
static QList<int> m_downloadIDs;
|
||||
|
||||
@@ -113,7 +113,7 @@ void LocalResourceProvider::removeDirectory( const QString & _path )
|
||||
|
||||
void LocalResourceProvider::reloadDirectory( const QString & _path )
|
||||
{
|
||||
ResourceTreeItem * dirTreeItem = NULL;
|
||||
ResourceItem::Relation * dirRelation = NULL;
|
||||
QString p = _path;
|
||||
if( !p.endsWith( QDir::separator() ) )
|
||||
{
|
||||
@@ -125,18 +125,18 @@ void LocalResourceProvider::reloadDirectory( const QString & _path )
|
||||
if( it->type() == ResourceItem::TypeDirectory &&
|
||||
it->fullName() == p )
|
||||
{
|
||||
dirTreeItem = it->treeItem();
|
||||
dirRelation = it->relation();
|
||||
}
|
||||
}
|
||||
|
||||
if( dirTreeItem )
|
||||
if( dirRelation )
|
||||
{
|
||||
ResourceItem * dirItem = dirTreeItem->item();
|
||||
ResourceItem * dirItem = dirRelation->item();
|
||||
if( dirItem )
|
||||
{
|
||||
m_scannedFolders.clear();
|
||||
readDir( dirItem->fullRelativeName(),
|
||||
dirTreeItem->parent() );
|
||||
dirRelation->parent() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ void LocalResourceProvider::reloadDirectory( const QString & _path )
|
||||
|
||||
|
||||
void LocalResourceProvider::readDir( const QString & _dir,
|
||||
ResourceTreeItem * _parent )
|
||||
ResourceItem::Relation * _parent )
|
||||
{
|
||||
#ifdef LMMS_BUILD_LINUX
|
||||
if( _dir.startsWith( "/dev" ) ||
|
||||
@@ -162,14 +162,14 @@ void LocalResourceProvider::readDir( const QString & _dir,
|
||||
m_scannedFolders << d.canonicalPath();
|
||||
|
||||
ResourceItem * parentItem;
|
||||
ResourceTreeItem * curParent = _parent->findChild( d.dirName() +
|
||||
ResourceItem::Relation * curParent = _parent->findChild( d.dirName() +
|
||||
QDir::separator(),
|
||||
m_baseDir );
|
||||
printf("read dir: %s\n", d.canonicalPath().toUtf8().constData() );
|
||||
if( curParent )
|
||||
{
|
||||
parentItem = curParent->item();
|
||||
foreachResourceTreeItem( curParent->children() )
|
||||
foreachResourceItemRelation( curParent->children() )
|
||||
{
|
||||
(*it)->setTemporaryMarker( false );
|
||||
}
|
||||
@@ -188,7 +188,7 @@ printf("read dir: %s\n", d.canonicalPath().toUtf8().constData() );
|
||||
parentItem->setLastMod( QFileInfo(
|
||||
d.canonicalPath() ).lastModified() );
|
||||
database()->addItem( parentItem );
|
||||
curParent = new ResourceTreeItem( _parent, parentItem );
|
||||
curParent = new ResourceItem::Relation( _parent, parentItem );
|
||||
curParent->setTemporaryMarker( true );
|
||||
m_watcher.addPath( parentItem->fullName() );
|
||||
}
|
||||
@@ -210,7 +210,7 @@ printf("read dir: %s\n", d.canonicalPath().toUtf8().constData() );
|
||||
{
|
||||
fname += QDir::separator();
|
||||
}
|
||||
ResourceTreeItem * curChild =
|
||||
ResourceItem::Relation * curChild =
|
||||
curParent->findChild( fname, m_baseDir );
|
||||
if( curChild )
|
||||
{
|
||||
@@ -252,20 +252,21 @@ printf("read dir: %s\n", d.canonicalPath().toUtf8().constData() );
|
||||
);
|
||||
newItem->setLastMod( f.lastModified() );
|
||||
database()->addItem( newItem );
|
||||
ResourceTreeItem * rti =
|
||||
new ResourceTreeItem( curParent,
|
||||
ResourceItem::Relation * relation =
|
||||
new ResourceItem::Relation( curParent,
|
||||
newItem );
|
||||
rti->setTemporaryMarker( true );
|
||||
relation->setTemporaryMarker( true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for( ResourceTreeItemList::Iterator it = curParent->children().begin();
|
||||
it != curParent->children().end(); )
|
||||
for( ResourceItem::Relation::List::Iterator it =
|
||||
curParent->children().begin();
|
||||
it != curParent->children().end(); )
|
||||
{
|
||||
if( (*it)->temporaryMarker() == false )
|
||||
{
|
||||
ResourceTreeItem * item = *it;
|
||||
ResourceItem::Relation * item = *it;
|
||||
it = curParent->children().erase( it );
|
||||
database()->recursiveRemoveItems( item );
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ void ResourceDB::load( const QString & _file )
|
||||
|
||||
multimediaProject m( _file );
|
||||
|
||||
loadTreeItem( &m_topLevelNode, m.content() );
|
||||
loadRelation( &m_topLevelNode, m.content() );
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ void ResourceDB::save( const QString & _file )
|
||||
|
||||
if( m_topLevelNode.getChild( 0 ) )
|
||||
{
|
||||
saveTreeItem( m_topLevelNode.getChild( 0 ), m, m.content() );
|
||||
saveRelation( m_topLevelNode.getChild( 0 ), m, m.content() );
|
||||
}
|
||||
|
||||
m.writeFile( _file );
|
||||
@@ -123,14 +123,14 @@ void ResourceDB::save( const QString & _file )
|
||||
|
||||
|
||||
|
||||
void ResourceDB::saveTreeItem( const ResourceTreeItem * _i,
|
||||
QDomDocument & _doc,
|
||||
QDomElement & _de )
|
||||
void ResourceDB::saveRelation( const ResourceItem::Relation * _i,
|
||||
QDomDocument & _doc,
|
||||
QDomElement & _de )
|
||||
{
|
||||
QDomElement e = _i->item() ? _doc.createElement( "item" ) : _de;
|
||||
foreachConstResourceTreeItem( _i->children() )
|
||||
foreachConstResourceItemRelation( _i->children() )
|
||||
{
|
||||
saveTreeItem( *it, _doc, e );
|
||||
saveRelation( *it, _doc, e );
|
||||
}
|
||||
if( _i->item() )
|
||||
{
|
||||
@@ -152,7 +152,7 @@ void ResourceDB::saveTreeItem( const ResourceTreeItem * _i,
|
||||
|
||||
|
||||
|
||||
void ResourceDB::loadTreeItem( ResourceTreeItem * _i, QDomElement & _de )
|
||||
void ResourceDB::loadRelation( ResourceItem::Relation * _i, QDomElement & _de )
|
||||
{
|
||||
QDomNode node = _de.firstChild();
|
||||
while( !node.isNull() )
|
||||
@@ -177,12 +177,13 @@ void ResourceDB::loadTreeItem( ResourceTreeItem * _i, QDomElement & _de )
|
||||
Qt::ISODate )
|
||||
);
|
||||
addItem( item );
|
||||
ResourceTreeItem * treeItem = new ResourceTreeItem( _i, item );
|
||||
ResourceItem::Relation * relation =
|
||||
new ResourceItem::Relation( _i, item );
|
||||
if( item->type() == ResourceItem::TypeDirectory )
|
||||
{
|
||||
emit directoryItemAdded( item->fullName() );
|
||||
}
|
||||
loadTreeItem( treeItem, e );
|
||||
loadRelation( relation, e );
|
||||
}
|
||||
}
|
||||
node = node.nextSibling();
|
||||
@@ -279,11 +280,11 @@ void ResourceDB::addItem( ResourceItem * newItem )
|
||||
ResourceItem * oldItem = m_items[hash];
|
||||
if( oldItem )
|
||||
{
|
||||
ResourceTreeItem * oldTreeItem = oldItem->treeItem();
|
||||
if( oldTreeItem )
|
||||
ResourceItem::Relation * oldRelation = oldItem->relation();
|
||||
if( oldRelation )
|
||||
{
|
||||
recursiveRemoveItems( oldTreeItem, false );
|
||||
delete oldTreeItem;
|
||||
recursiveRemoveItems( oldRelation, false );
|
||||
delete oldRelation;
|
||||
}
|
||||
if( oldItem->type() == ResourceItem::TypeDirectory )
|
||||
{
|
||||
@@ -298,8 +299,8 @@ void ResourceDB::addItem( ResourceItem * newItem )
|
||||
|
||||
|
||||
|
||||
void ResourceDB::recursiveRemoveItems( ResourceTreeItem * parent,
|
||||
bool removeTopLevelParent )
|
||||
void ResourceDB::recursiveRemoveItems( ResourceItem::Relation * parent,
|
||||
bool removeTopLevelParent )
|
||||
{
|
||||
if( !parent )
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@ ResourceItem::ResourceItem( ResourceProvider * _provider,
|
||||
m_size( _size ),
|
||||
m_lastMod( _last_mod ),
|
||||
m_tags( _tags ),
|
||||
m_treeItem( NULL )
|
||||
m_relation( NULL )
|
||||
{
|
||||
init();
|
||||
}
|
||||
@@ -76,7 +76,7 @@ ResourceItem::ResourceItem( const ResourceItem & _item ) :
|
||||
m_size( _item.m_size ),
|
||||
m_lastMod( _item.m_lastMod ),
|
||||
m_tags( _item.m_tags ),
|
||||
m_treeItem( NULL )
|
||||
m_relation( NULL )
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ ResourceModel::ResourceModel( ResourceDB * _db, QObject * _parent ) :
|
||||
{
|
||||
setSupportedDragActions( Qt::CopyAction );
|
||||
|
||||
connect( m_db, SIGNAL( itemsChanged() ),
|
||||
connect( db(), SIGNAL( itemsChanged() ),
|
||||
this, SIGNAL( itemsChanged() ) );
|
||||
}
|
||||
|
||||
@@ -49,11 +49,11 @@ QVariant ResourceModel::data( const QModelIndex & _idx, int _role ) const
|
||||
|
||||
if( _idx.isValid() )
|
||||
{
|
||||
ResourceTreeItem * treeItem = this->treeItem( _idx );
|
||||
ResourceItem * item = treeItem->item();
|
||||
ResourceItem::Relation * relation = this->relation( _idx );
|
||||
ResourceItem * item = relation->item();
|
||||
if( _role == Qt::DisplayRole )
|
||||
{
|
||||
if( treeItem->parent() == m_db->topLevelNode() )
|
||||
if( relation->parent() == db()->topLevelNode() )
|
||||
{
|
||||
switch( item->baseDir() )
|
||||
{
|
||||
@@ -71,7 +71,7 @@ QVariant ResourceModel::data( const QModelIndex & _idx, int _role ) const
|
||||
}
|
||||
else if( _role == Qt::DecorationRole )
|
||||
{
|
||||
if( treeItem->parent() == m_db->topLevelNode() )
|
||||
if( relation->parent() == db()->topLevelNode() )
|
||||
{
|
||||
switch( item->baseDir() )
|
||||
{
|
||||
@@ -216,7 +216,7 @@ QMimeData * ResourceModel::mimeData( const QModelIndexList & _list ) const
|
||||
|
||||
int ResourceModel::totalItems() const
|
||||
{
|
||||
const ResourceDB::ItemHashMap & items = m_db->items();
|
||||
const ResourceDB::ItemHashMap & items = db()->items();
|
||||
int num = 0;
|
||||
foreach( const ResourceItem * i, items )
|
||||
{
|
||||
@@ -233,7 +233,7 @@ int ResourceModel::totalItems() const
|
||||
|
||||
int ResourceModel::shownItems() const
|
||||
{
|
||||
const ResourceDB::ItemHashMap & items = m_db->items();
|
||||
const ResourceDB::ItemHashMap & items = db()->items();
|
||||
int num = 0;
|
||||
foreach( const ResourceItem * i, items )
|
||||
{
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* ResourceTreeItem.cpp - implementation of ResourceTreeItem
|
||||
*
|
||||
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* 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 (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <QtCore/QHash>
|
||||
|
||||
#include "ResourceTreeItem.h"
|
||||
|
||||
|
||||
ResourceTreeItem::ResourceTreeItem( ResourceTreeItem * _parent,
|
||||
ResourceItem * _item ) :
|
||||
m_parent( _parent ),
|
||||
m_temporaryMarker( false ),
|
||||
m_item( _item )
|
||||
{
|
||||
if( m_parent )
|
||||
{
|
||||
m_parent->addChild( this );
|
||||
}
|
||||
if( m_item )
|
||||
{
|
||||
m_item->setTreeItem( this );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ResourceTreeItem::~ResourceTreeItem()
|
||||
{
|
||||
foreachResourceTreeItem( m_children )
|
||||
{
|
||||
delete *it;
|
||||
}
|
||||
if( m_item )
|
||||
{
|
||||
m_item->setTreeItem( NULL );
|
||||
}
|
||||
if( m_parent )
|
||||
{
|
||||
m_parent->removeChild( this );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int ResourceTreeItem::rowCount( const ResourceModel * _model ) const
|
||||
{
|
||||
int rc = 0;
|
||||
foreachConstResourceTreeItem( m_children )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
++rc;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ResourceTreeItem * ResourceTreeItem::getChild( int _row,
|
||||
const ResourceModel * _model )
|
||||
{
|
||||
int rc = 0;
|
||||
foreachResourceTreeItem( m_children )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
if( rc == _row )
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
++rc;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int ResourceTreeItem::row( const ResourceModel * _model ) const
|
||||
{
|
||||
if( !m_parent )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int row = 0;
|
||||
foreachConstResourceTreeItem( m_parent->m_children )
|
||||
{
|
||||
if( !(*it)->item()->isHidden( _model ) )
|
||||
{
|
||||
if( *it == this )
|
||||
{
|
||||
return row;
|
||||
}
|
||||
++row;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ResourceTreeItem * ResourceTreeItem::findChild(
|
||||
const QString & _name,
|
||||
ResourceItem::BaseDirectory _base_dir )
|
||||
{
|
||||
if( _name.isNull() || _name.isEmpty() )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int hash = qHash( _name );
|
||||
|
||||
foreachResourceTreeItem( m_children )
|
||||
{
|
||||
ResourceTreeItem * rti = *it;
|
||||
if( rti->item() &&
|
||||
rti->item()->nameHash() == hash &&
|
||||
rti->item()->name() == _name &&
|
||||
rti->item()->baseDir() == _base_dir )
|
||||
{
|
||||
return rti;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ int ResourceTreeModel::rowCount( const QModelIndex & _parent ) const
|
||||
{
|
||||
return db()->topLevelNode()->rowCount( this );
|
||||
}
|
||||
return treeItem( _parent )->rowCount( this );
|
||||
return relation( _parent )->rowCount( this );
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ QModelIndex ResourceTreeModel::index( int _row, int _col,
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
ResourceTreeItem * parentItem;
|
||||
ResourceItem::Relation * parentItem;
|
||||
|
||||
if( !_parent.isValid() )
|
||||
{
|
||||
@@ -71,7 +71,7 @@ QModelIndex ResourceTreeModel::index( int _row, int _col,
|
||||
}
|
||||
else
|
||||
{
|
||||
parentItem = treeItem( _parent );
|
||||
parentItem = relation( _parent );
|
||||
}
|
||||
|
||||
if( _row < parentItem->rowCount( this ) )
|
||||
@@ -91,8 +91,8 @@ QModelIndex ResourceTreeModel::parent( const QModelIndex & _idx ) const
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
ResourceTreeItem * childItem = treeItem( _idx );
|
||||
ResourceTreeItem * parentItem = childItem->parent();
|
||||
ResourceItem::Relation * childItem = relation( _idx );
|
||||
ResourceItem::Relation * parentItem = childItem->parent();
|
||||
if( parentItem == db()->topLevelNode() )
|
||||
{
|
||||
return QModelIndex();
|
||||
@@ -123,7 +123,7 @@ void ResourceTreeModel::setFilter( const QString & _s )
|
||||
|
||||
|
||||
|
||||
bool ResourceTreeModel::filterItems( ResourceTreeItem * _item,
|
||||
bool ResourceTreeModel::filterItems( ResourceItem::Relation * _item,
|
||||
const QModelIndex & _parent,
|
||||
const QStringList & _keywords )
|
||||
{
|
||||
@@ -152,8 +152,7 @@ bool ResourceTreeModel::filterItems( ResourceTreeItem * _item,
|
||||
|
||||
int row = 0;
|
||||
bool hide = true;
|
||||
for( ResourceTreeItemList::Iterator it = _item->children().begin();
|
||||
it != _item->children().end(); ++it )
|
||||
foreachResourceItemRelation( _item->children() )
|
||||
{
|
||||
QModelIndex idx = createIndex( row, 0, *it );
|
||||
if( filterItems( *it, idx , _keywords ) )
|
||||
@@ -170,16 +169,14 @@ bool ResourceTreeModel::filterItems( ResourceTreeItem * _item,
|
||||
|
||||
|
||||
|
||||
void ResourceTreeModel::setHidden( ResourceTreeItem * _item,
|
||||
void ResourceTreeModel::setHidden( ResourceItem::Relation * _item,
|
||||
const QModelIndex & _parent,
|
||||
bool _hide, bool _recursive )
|
||||
{
|
||||
if( _recursive )
|
||||
{
|
||||
int row = 0;
|
||||
for( ResourceTreeItemList::Iterator it =
|
||||
_item->children().begin();
|
||||
it != _item->children().end(); ++it )
|
||||
foreachResourceItemRelation( _item->children() )
|
||||
{
|
||||
setHidden( *it, createIndex( row, 0, *it ), _hide );
|
||||
++row;
|
||||
|
||||
@@ -51,7 +51,7 @@ UnifiedResourceProvider::~UnifiedResourceProvider()
|
||||
|
||||
void UnifiedResourceProvider::addDatabase( ResourceDB * _db )
|
||||
{
|
||||
ResourceTreeItem * childRoot = _db->topLevelNode()->getChild( 0 );
|
||||
ResourceItem::Relation * childRoot = _db->topLevelNode()->getChild( 0 );
|
||||
if( childRoot )
|
||||
{
|
||||
m_mergedDatabases << _db;
|
||||
|
||||
@@ -93,14 +93,14 @@ void WebResourceProvider::finishDownload( int _id, bool a )
|
||||
|
||||
|
||||
// create a recursive tree from flat items and their path property
|
||||
ResourceTreeItem * WebResourceProvider::addTreeItem(
|
||||
ResourceTreeItem * _parent,
|
||||
ResourceItem::Relation * WebResourceProvider::addRelation(
|
||||
ResourceItem::Relation * _parent,
|
||||
ResourceItem * _item )
|
||||
{
|
||||
if( _parent == database()->topLevelNode() ||
|
||||
_item->path().isEmpty() )
|
||||
{
|
||||
return new ResourceTreeItem( _parent, _item );
|
||||
return new ResourceItem::Relation( _parent, _item );
|
||||
}
|
||||
|
||||
const QStringList itemPath = _item->path().split( '/' );
|
||||
@@ -114,23 +114,22 @@ ResourceTreeItem * WebResourceProvider::addTreeItem(
|
||||
const int parentPathSize = parentPath.count( '/' );
|
||||
if( parentPathSize+1 >= itemPath.size() )
|
||||
{
|
||||
if( _item->path() == parentPath )
|
||||
// if( _item->path() == parentPath )
|
||||
{
|
||||
return new ResourceTreeItem( _parent, _item );
|
||||
return new ResourceItem::Relation( _parent, _item );
|
||||
}
|
||||
else
|
||||
/* else
|
||||
{
|
||||
// something went wrong
|
||||
return new ResourceTreeItem( _parent, _item );
|
||||
}
|
||||
return new ResourceItem::Relation( _parent, _item );
|
||||
}*/
|
||||
}
|
||||
|
||||
pathComponent = itemPath[parentPathSize] + "/";
|
||||
}
|
||||
|
||||
ResourceTreeItem * subParent =
|
||||
_parent->findChild( pathComponent,
|
||||
ResourceItem::BaseURL );
|
||||
ResourceItem::Relation * subParent =
|
||||
_parent->findChild( pathComponent, ResourceItem::BaseURL );
|
||||
if( subParent == NULL )
|
||||
{
|
||||
ResourceItem * dirItem =
|
||||
@@ -140,16 +139,16 @@ ResourceTreeItem * WebResourceProvider::addTreeItem(
|
||||
ResourceItem::BaseURL,
|
||||
parentPath );
|
||||
database()->addItem( dirItem );
|
||||
subParent = new ResourceTreeItem( _parent, dirItem );
|
||||
subParent = new ResourceItem::Relation( _parent, dirItem );
|
||||
}
|
||||
return addTreeItem( subParent, _item );
|
||||
return addRelation( subParent, _item );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void WebResourceProvider::importNodeIntoDB( const QDomNode & _n,
|
||||
ResourceTreeItem * _parent )
|
||||
ResourceItem::Relation * _parent )
|
||||
{
|
||||
QDomNode n = _n;
|
||||
while( !n.isNull() )
|
||||
@@ -178,10 +177,10 @@ void WebResourceProvider::importNodeIntoDB( const QDomNode & _n,
|
||||
n.firstChildElement( "size" ).text().toInt() );
|
||||
database()->addItem( item );
|
||||
|
||||
ResourceTreeItem * treeItem = addTreeItem( _parent, item );
|
||||
ResourceItem::Relation * relation = addRelation( _parent, item );
|
||||
if( n.nodeName() != "file" )
|
||||
{
|
||||
importNodeIntoDB( n.firstChild(), treeItem );
|
||||
importNodeIntoDB( n.firstChild(), relation );
|
||||
}
|
||||
n = n.nextSibling();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user