Mercurial > hg > octave-lojdl
changeset 16999:90d50e56a06a
Add sortable table headers to find_files_dialog results
* libgui/src/find-files-dialog.cc
(find_files_dialog::find_files_dialog): enable sorting of rows in _file_list.
* libgui/src/find-files-model.cc, libgui/src/find-files-model.h
(global): New local class find_file_less_than.
(find_files_model::find_files_model): Add/initialize _sortorder var.
(find_files_model::addFile): Find pos to add data to sorted entries.
(find_files_model::sort): New function.
author | John Donoghue <john.donoghue@ieee.org> |
---|---|
date | Wed, 17 Jul 2013 21:42:11 -0400 |
parents | a1d091243d11 |
children | 5069b6c6c062 |
files | libgui/src/find-files-dialog.cc libgui/src/find-files-model.cc libgui/src/find-files-model.h |
diffstat | 3 files changed, 88 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/find-files-dialog.cc +++ b/libgui/src/find-files-dialog.cc @@ -96,7 +96,8 @@ _content_case_check->setChecked (settings->value ("findfiles/content_case", false).toBool()); _content_case_check->setToolTip (tr ("Set text content is case insensitive")); - find_files_model * model = new find_files_model (); + find_files_model * model = new find_files_model (this); + _file_list = new QTableView; _file_list->setWordWrap (false); _file_list->setModel (model); @@ -105,7 +106,10 @@ _file_list->setSelectionMode(QAbstractItemView::SingleSelection); _file_list->setAlternatingRowColors(true); _file_list->setToolTip (tr ("Search results")); + _file_list->setSortingEnabled (true); _file_list->horizontalHeader ()->restoreState (settings->value ("findfiles/column_state").toByteArray ()); + _file_list->horizontalHeader ()->setSortIndicatorShown (true); + _file_list->horizontalHeader ()->setClickable (true); _file_list->horizontalHeader ()->setStretchLastSection (true); _file_list->sortByColumn ( settings->value ("findfiles/sort_files_by_column",0).toInt (),
--- a/libgui/src/find-files-model.cc +++ b/libgui/src/find-files-model.cc @@ -22,12 +22,60 @@ #include "find-files-model.h" #include <QFileIconProvider> +#include <QtAlgorithms> + +class find_file_less_than +{ +public: + find_file_less_than (int ord) + { + _sortorder = ord; + } + QVariant getValue (const QFileInfo &f) const + { + QVariant val; + int col = (_sortorder > 0) ? _sortorder : -_sortorder; + + switch (col-1) + { + case 0: + val = QVariant (f.fileName()); + break; + + case 1: + val = QVariant (f.absolutePath()); + break; + + default: + break; + } + return val; + } + bool lessThan (const QVariant &left, const QVariant &right) const + { + return left.toString ().compare (right.toString (), Qt::CaseInsensitive) < 0; + } + bool operator () (const QFileInfo &left, const QFileInfo &right) const + { + QVariant leftval = getValue(left); + QVariant rightval = getValue(right); + + if (_sortorder > 0) + return lessThan(leftval, rightval); + else + return ! lessThan(leftval, rightval); + } +private: + int _sortorder; +}; + find_files_model::find_files_model (QObject *p) : QAbstractListModel(p) { _columnNames.append (tr ("Filename")); _columnNames.append (tr ("Directory")); + _sortorder = 0; } find_files_model::~find_files_model () @@ -47,11 +95,19 @@ void find_files_model::addFile (const QFileInfo &info) { - beginInsertRows(QModelIndex(), _files.size(), _files.size() ); + beginInsertRows (QModelIndex (), _files.size (), _files.size () ); + + QList<QFileInfo>::Iterator it; + find_file_less_than less_than(_sortorder); - _files.append(info); + for (it=_files.begin ();it!=_files.end ();it++) + { + if (less_than (info, *it)) break; + } - endInsertRows(); + _files.insert (it, info); + + endInsertRows (); } int @@ -113,6 +169,27 @@ return QVariant (); } +void +find_files_model::sort (int column, Qt::SortOrder order) +{ + if(column >= 0) + { + if (order == Qt::DescendingOrder) + _sortorder = -(column+1); + else + _sortorder = column+1; + } + else + _sortorder = 0; + + if (_sortorder != 0) + { + beginResetModel (); + qSort (_files.begin (), _files.end (), find_file_less_than (_sortorder)); + endResetModel (); + } +} + QFileInfo find_files_model::fileInfo (const QModelIndex & p) const {
--- a/libgui/src/find-files-model.h +++ b/libgui/src/find-files-model.h @@ -50,11 +50,14 @@ QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + void sort (int column, Qt::SortOrder order=Qt::AscendingOrder); + QFileInfo fileInfo (const QModelIndex & p) const; QIcon fileIcon (const QModelIndex &p) const; private: QList<QFileInfo> _files; QStringList _columnNames; + int _sortorder; }; #endif // find_files_model_h