GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
find-files-model.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013 John Donoghue
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <QFileIconProvider>
28 #include <QtAlgorithms>
29 
30 #include "find-files-model.h"
31 
33 {
34 public:
36  {
37  _sortorder = ord;
38  }
39  QVariant getValue (const QFileInfo &f) const
40  {
41  QVariant val;
42  int col = (_sortorder > 0) ? _sortorder : -_sortorder;
43 
44  switch (col-1)
45  {
46  case 0:
47  val = QVariant (f.fileName ());
48  break;
49 
50  case 1:
51  val = QVariant (f.absolutePath ());
52  break;
53 
54  default:
55  break;
56  }
57  return val;
58  }
59  bool lessThan (const QVariant &left, const QVariant &right) const
60  {
61  return
62  left.toString ().compare (right.toString (), Qt::CaseInsensitive) < 0;
63  }
64  bool operator () (const QFileInfo &left, const QFileInfo &right) const
65  {
66  QVariant leftval = getValue (left);
67  QVariant rightval = getValue (right);
68 
69  if (_sortorder > 0)
70  return lessThan (leftval, rightval);
71  else
72  return ! lessThan (leftval, rightval);
73  }
74 private:
76 };
77 
78 
80  : QAbstractListModel (p)
81 {
82  _columnNames.append (tr ("Filename"));
83  _columnNames.append (tr ("Directory"));
84  _sortorder = 0;
85 }
86 
88 {
89 }
90 
91 void
93 {
94  beginResetModel ();
95 
96  _files.clear ();
97 
98  endResetModel ();
99 }
100 
101 void
102 find_files_model::addFile (const QFileInfo &info)
103 {
104  beginInsertRows (QModelIndex (), _files.size (), _files.size () );
105 
107  find_file_less_than less_than (_sortorder);
108 
109  for (it=_files.begin (); it!=_files.end (); it++)
110  {
111  if (less_than (info, *it)) break;
112  }
113 
114  _files.insert (it, info);
115 
116  endInsertRows ();
117 }
118 
119 int
120 find_files_model::rowCount (const QModelIndex &) const
121 {
122  return _files.size ();
123 }
124 
125 int
126 find_files_model::columnCount (const QModelIndex &) const
127 {
128  return _columnNames.size ();
129 }
130 
131 QVariant
132 find_files_model::data (const QModelIndex& idx, int role) const
133 {
134  QVariant retval;
135 
136  if (idx.isValid ())
137  {
138  if (role == Qt::DisplayRole)
139  {
140  switch (idx.column ())
141  {
142  case 0:
143  retval = QVariant (_files[idx.row ()].fileName ());
144  break;
145 
146  case 1:
147  retval = QVariant (_files[idx.row ()].absolutePath ());
148  break;
149 
150  default:
151  break;
152  }
153  }
154  else if (role == Qt:: DecorationRole)
155  {
156  switch (idx.column ())
157  {
158  case 0:
159  retval = fileIcon (idx);
160  default:
161  break;
162  }
163  }
164  }
165 
166  return retval;
167 }
168 
169 QVariant
170 find_files_model::headerData (int section, Qt::Orientation orientation,
171  int role) const
172 {
173  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
174  return _columnNames[section];
175  else
176  return QVariant ();
177 }
178 
179 void
180 find_files_model::sort (int column, Qt::SortOrder order)
181 {
182  if (column >= 0)
183  {
184  if (order == Qt::DescendingOrder)
185  _sortorder = -(column+1);
186  else
187  _sortorder = column+1;
188  }
189  else
190  _sortorder = 0;
191 
192  if (_sortorder != 0)
193  {
194  beginResetModel ();
195  qSort (_files.begin (), _files.end (), find_file_less_than (_sortorder));
196  endResetModel ();
197  }
198 }
199 
200 QFileInfo
201 find_files_model::fileInfo (const QModelIndex & p) const
202 {
203  if (p.isValid ())
204  {
205  return _files[p.row ()];
206  }
207  return QFileInfo ();
208 }
209 
210 QIcon
211 find_files_model::fileIcon (const QModelIndex &p) const
212 {
213  QFileIconProvider icon_provider;
214  if (p.isValid ())
215  {
216  return icon_provider.icon (_files[p.row ()]);
217  }
218  return QIcon ();
219 }