Commit 6ff85ba3 authored by Harald Hvaal's avatar Harald Hvaal
Browse files

BUG: 181746

Adding sorting to archivemodel

svn path=/trunk/KDE/kdeutils/ark/; revision=948150
parent caf80ca7
......@@ -41,6 +41,7 @@ class ArchiveDirNode;
//used to speed up the loading of large archives
static ArchiveNode* previousMatch = NULL;
static QStringList previousPieces;
static int currentSortColumn = 0;
class ArchiveNode
......@@ -54,9 +55,24 @@ class ArchiveNode
virtual ~ArchiveNode() {}
static bool compare(const ArchiveNode* a, const ArchiveNode* b)
static bool compareAscending(const ArchiveNode* a, const ArchiveNode* b)
{
return (a->m_name < b->m_name);
if (currentSortColumn == FileName)
return (a->m_name < b->m_name);
else if (currentSortColumn == Size)
return (a->entry()[currentSortColumn].toInt() < b->entry()[currentSortColumn].toInt());
else
return (a->entry()[currentSortColumn].toString() < b->entry()[currentSortColumn].toString());
}
static bool compareDescending(const ArchiveNode* a, const ArchiveNode* b)
{
if (currentSortColumn == FileName)
return (a->m_name > b->m_name);
else if (currentSortColumn == Size)
return (a->entry()[currentSortColumn].toInt() > b->entry()[currentSortColumn].toInt());
else
return (a->entry()[currentSortColumn].toString() > b->entry()[currentSortColumn].toString());
}
ArchiveEntry entry() const { return m_entry; }
......@@ -157,15 +173,6 @@ class ArchiveDirNode: public ArchiveNode
}
}
void sort()
{
QList<ArchiveDirNode*> dirNodes;
returnDirNodes(&dirNodes);
foreach(ArchiveDirNode* dir, dirNodes) {
qSort(dir->entries().begin(), dir->entries().end(),ArchiveNode::compare );
}
}
void clear()
{
qDeleteAll( m_entries );
......@@ -433,8 +440,24 @@ int ArchiveModel::columnCount( const QModelIndex &parent ) const
void ArchiveModel::sort ( int column, Qt::SortOrder order )
{
kDebug(1601);
m_rootNode->sort();
reset();
QList<ArchiveDirNode*> dirNodes;
m_rootNode->returnDirNodes(&dirNodes);
foreach(ArchiveDirNode* dir, dirNodes) {
Q_ASSERT(m_showColumns.size() > column);
currentSortColumn = m_showColumns.at(column);
if (order == Qt::AscendingOrder)
qSort(dir->entries().begin(), dir->entries().end(),ArchiveNode::compareAscending );
else
qSort(dir->entries().begin(), dir->entries().end(),ArchiveNode::compareDescending );
emit dataChanged(
index(0, 0, indexForNode(dir)),
index(dir->entries().size() - 1, 0, indexForNode(dir)));
}
}
Qt::DropActions ArchiveModel::supportedDropActions () const
......
......@@ -86,6 +86,8 @@ class ArchiveModel: public QAbstractItemModel
private:
ArchiveDirNode* parentFor( const ArchiveEntry& entry );
QModelIndex indexForNode( ArchiveNode *node );
static bool compareAscending(const QModelIndex& a, const QModelIndex& b);
static bool compareDescending(const QModelIndex& a, const QModelIndex& b);
/**
* Insert the node @p node into the model, ensuring all views are notified
* of the change.
......
......@@ -401,6 +401,7 @@ void Part::slotLoadingFinished(KJob *job)
if (job->error())
if (arguments().metaData()["createNewArchive"] != "true")
KMessageBox::sorry(NULL, i18n("Reading the archive '%1' failed with the error '%2'", localFilePath(), job->errorText()), i18n("Error opening archive"));
m_view->sortByColumn(0, Qt::AscendingOrder);
m_view->expandToDepth(0);
}
......
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