~ken-vandine/ubuntu-filemanager-app/plugin_cleanup

« back to all changes in this revision

Viewing changes to folderlistmodel/dirmodel.cpp

  • Committer: carlos.mazieri at gmail
  • Date: 2013-04-14 19:11:30 UTC
  • Revision ID: carlos.mazieri@gmail.com-20130414191130-1ew225t17ovuxoq1
implemented Sort by name or modified date

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include <QDir>
34
34
#include <QDebug>
35
35
#include <QDateTime>
36
 
#include <QPixmap>
37
 
 
 
36
#include <QFileIconProvider>
38
37
 
39
38
#include <errno.h>
40
39
#include <string.h>
54
53
    QHash<QByteArray, int> roleMapping;
55
54
}
56
55
 
 
56
 
 
57
static bool fileCompareAscending(const QFileInfo &a, const QFileInfo &b)
 
58
{
 
59
    if (a.isDir() && !b.isDir())
 
60
        return true;
 
61
 
 
62
    if (b.isDir() && !a.isDir())
 
63
        return false;
 
64
 
 
65
    return QString::localeAwareCompare(a.fileName(), b.fileName()) < 0;
 
66
}
 
67
 
 
68
 
 
69
static bool fileCompareDescending(const QFileInfo &a, const QFileInfo &b)
 
70
{
 
71
    if (a.isDir() && !b.isDir())
 
72
        return true;
 
73
 
 
74
    if (b.isDir() && !a.isDir())
 
75
        return false;
 
76
 
 
77
    return QString::localeAwareCompare(a.fileName(), b.fileName()) > 0;
 
78
}
 
79
 
 
80
static bool dateCompareDescending(const QFileInfo &a, const QFileInfo &b)
 
81
{
 
82
    if (a.isDir() && !b.isDir())
 
83
        return true;
 
84
 
 
85
    if (b.isDir() && !a.isDir())
 
86
        return false;
 
87
 
 
88
    return a.lastModified() > b.lastModified();
 
89
}
 
90
 
 
91
static bool dateCompareAscending(const QFileInfo &a, const QFileInfo &b)
 
92
{
 
93
    if (a.isDir() && !b.isDir())
 
94
        return true;
 
95
 
 
96
    if (b.isDir() && !a.isDir())
 
97
        return false;
 
98
 
 
99
    return a.lastModified() < b.lastModified();
 
100
}
 
101
 
 
102
/*!
 
103
 *  Sort was originaly done in \ref onItemsAdded() and that code is now in \ref addItem(),
 
104
 *  the reason to keep doing sort and do not let QDir does it is that when adding new items
 
105
 *  by \ref mkdir() or \paste() it is not necessary to call refresh() to load the entire directory
 
106
 *  to organize it items again. New items order/position are organized by \ref addItem()
 
107
 *
 
108
 */
 
109
static CompareFunction availableCompareFunctions[2][2] =
 
110
{
 
111
    {fileCompareAscending, fileCompareDescending}
 
112
   ,{dateCompareAscending, dateCompareDescending}
 
113
};
 
114
 
 
115
 
 
116
 
 
117
 
57
118
class DirListWorker : public IORequest
58
119
{
59
120
    Q_OBJECT
87
148
 
88
149
        // last batch
89
150
        emit itemsAdded(directoryContents);
90
 
        emit workerFinished();
91
 
        //std::sort(directoryContents.begin(), directoryContents.end(), DirModel::fileCompare);
 
151
        emit workerFinished();      
92
152
    }
93
153
 
94
154
signals:
105
165
    , mShowDirectories(true)
106
166
    , mAwaitingResults(false)   
107
167
    , mShowHiddenFiles(false)
 
168
    , mSortBy(SortByName)
 
169
    , mSortOrder(SortAscending)
 
170
    , mCompareFunction(0)
108
171
    , m_fsAction(new FileSystemAction(this) )
109
172
{
110
173
    mNameFilters = QStringList() << "*";
137
200
    connect(this,     SIGNAL(pathChanged(QString)),
138
201
            m_fsAction, SLOT(pathChanged(QString)));
139
202
 
 
203
    setCompareAndReorder();
140
204
}
141
205
 
142
206
DirModel::~DirModel()
203
267
        return QVariant();
204
268
    }
205
269
    if (role == Qt::DecorationRole)
206
 
    {
207
 
        QModelIndex idxIcon = createIndex(index.row(), IconSourceRole - FileNameRole);
208
 
        QString iconPath = data(idxIcon, Qt::DisplayRole).toString();
209
 
        QPixmap pic(iconPath);
210
 
        return pic;
 
270
    {       
 
271
        if (index.column() == 0)
 
272
        {
 
273
            return QFileIconProvider().icon(mDirectoryContents.at(index.row()));
 
274
        }
 
275
        return QVariant();
211
276
    }
212
277
    role = FileNameRole + index.column();
213
278
#else
314
379
    emit pathChanged(pathName);
315
380
}
316
381
 
317
 
static bool fileCompare(const QFileInfo &a, const QFileInfo &b)
318
 
{
319
 
    if (a.isDir() && !b.isDir())
320
 
        return true;
321
 
 
322
 
    if (b.isDir() && !a.isDir())
323
 
        return false;
324
 
 
325
 
    return QString::localeAwareCompare(a.fileName(), b.fileName()) < 0;
326
 
}
327
382
 
328
383
void DirModel::onResultsFetched() {
329
384
    if (mAwaitingResults) {
633
688
 * \sa insertedRow()
634
689
 */
635
690
int DirModel::addItem(const QFileInfo &fi)
636
 
{
 
691
{      
637
692
    QVector<QFileInfo>::Iterator it = qLowerBound(mDirectoryContents.begin(),
638
693
                                                  mDirectoryContents.end(),
639
694
                                                  fi,
640
 
                                                  fileCompare);
 
695
                                                  mCompareFunction);
641
696
    int idx =  mDirectoryContents.count();
642
697
    if (it == mDirectoryContents.end()) {
643
698
        beginInsertRows(QModelIndex(), mDirectoryContents.count(), mDirectoryContents.count());
659
714
    m_fsAction->cancel();
660
715
}
661
716
 
 
717
 
662
718
QString DirModel::fileSize(qint64 size) const
663
719
{
664
720
    struct UnitSizes
694
750
 
695
751
 
696
752
 
697
 
bool DirModel::showHiddenFiles() const
 
753
bool DirModel::getShowHiddenFiles() const
698
754
{
699
755
    return mShowHiddenFiles;
700
756
}
702
758
 
703
759
void DirModel::setShowHiddenFiles(bool show)
704
760
{
705
 
    mShowHiddenFiles = show;
706
 
    refresh();
707
 
    emit showHiddenFilesChanged();
 
761
    if (show != mShowHiddenFiles)
 
762
    {
 
763
        mShowHiddenFiles = show;
 
764
        refresh();
 
765
        emit showHiddenFilesChanged();
 
766
    }
 
767
}
 
768
 
 
769
 
 
770
void DirModel::toggleShowDirectories()
 
771
{
 
772
    setShowDirectories(!mShowDirectories);
 
773
}
 
774
 
 
775
 
 
776
void DirModel::toggleShowHiddenFiles()
 
777
{
 
778
    setShowHiddenFiles(!mShowHiddenFiles);
 
779
}
 
780
 
 
781
 
 
782
DirModel::SortBy
 
783
DirModel::getSortBy()  const
 
784
{
 
785
    return mSortBy;
 
786
}
 
787
 
 
788
 
 
789
void DirModel::setSortBy(SortBy field)
 
790
{
 
791
    if (field != mSortBy)
 
792
    {
 
793
        mSortBy = field;
 
794
        setCompareAndReorder();
 
795
        emit sortByChanged();
 
796
    }
 
797
}
 
798
 
 
799
 
 
800
DirModel::SortOrder
 
801
DirModel::getSortOrder() const
 
802
{
 
803
    return mSortOrder;
 
804
}
 
805
 
 
806
void DirModel::setSortOrder(SortOrder order)
 
807
{
 
808
    if ( order != mSortOrder )
 
809
    {
 
810
        mSortOrder = order;
 
811
        setCompareAndReorder();
 
812
        emit sortOrderChanged();
 
813
    }
 
814
}
 
815
 
 
816
 
 
817
void DirModel::toggleSortOrder()
 
818
{
 
819
    SortOrder  order = static_cast<SortOrder> (mSortOrder ^ 1);
 
820
    setSortOrder(order);
 
821
}
 
822
 
 
823
 
 
824
void DirModel::toggleSortBy()
 
825
{
 
826
    SortBy by = static_cast<SortBy> (mSortBy ^ 1);
 
827
    setSortBy(by);
 
828
}
 
829
 
 
830
/*!
 
831
 * \brief DirModel::setCompareAndReorder() called when  SortOrder or SortBy change
 
832
 *
 
833
 *  It does not reload items from disk, just reorganize items from \a mDirectoryContents array
 
834
 */
 
835
void DirModel::setCompareAndReorder()
 
836
{
 
837
    mCompareFunction = availableCompareFunctions[mSortBy][mSortOrder];
 
838
    if (mDirectoryContents.count() > 0)
 
839
    {
 
840
        QVector<QFileInfo> tmpDirectoryContents = mDirectoryContents;
 
841
        beginResetModel();
 
842
        mDirectoryContents.clear();
 
843
        endResetModel();
 
844
        for(int counter=0; counter < tmpDirectoryContents.count(); counter++)
 
845
        {
 
846
            addItem(tmpDirectoryContents.at(counter));
 
847
        }
 
848
    }
708
849
}
709
850
 
710
851
// for dirlistworker