GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
find-files-model.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QFileIconProvider>
28 #include <QtAlgorithms>
29 
30 #include "find-files-model.h"
31 
32 namespace octave
33 {
35  {
36  public:
37 
38  find_file_less_than (int ord) { m_sortorder = ord; }
39 
40  QVariant getValue (const QFileInfo& f) const
41  {
42  QVariant val;
43 
44  int col = (m_sortorder > 0) ? m_sortorder : -m_sortorder;
45 
46  switch (col-1)
47  {
48  case 0:
49  val = QVariant (f.fileName ());
50  break;
51 
52  case 1:
53  val = QVariant (f.absolutePath ());
54  break;
55 
56  default:
57  break;
58  }
59 
60  return val;
61  }
62 
63  bool lessThan (const QVariant& left, const QVariant& right) const
64  {
65  return
66  left.toString ().compare (right.toString (), Qt::CaseInsensitive) < 0;
67  }
68 
69  bool operator () (const QFileInfo& left, const QFileInfo& right) const
70  {
71  QVariant leftval = getValue (left);
72  QVariant rightval = getValue (right);
73 
74  if (m_sortorder > 0)
75  return lessThan (leftval, rightval);
76  else
77  return ! lessThan (leftval, rightval);
78  }
79 
80  private:
81 
83  };
84 
87  {
88  m_columnNames.append (tr ("Filename"));
89  m_columnNames.append (tr ("Directory"));
90  m_sortorder = 0;
91  }
92 
94  {
95  beginResetModel ();
96 
97  m_files.clear ();
98 
99  endResetModel ();
100  }
101 
102  void find_files_model::addFile (const QFileInfo& info)
103  {
104  beginInsertRows (QModelIndex (), m_files.size (), m_files.size ());
105 
107  find_file_less_than less_than (m_sortorder);
108 
109  for (it = m_files.begin (); it != m_files.end (); it++)
110  {
111  if (less_than (info, *it))
112  break;
113  }
114 
115  m_files.insert (it, info);
116 
117  endInsertRows ();
118  }
119 
120  int find_files_model::rowCount (const QModelIndex &) const
121  {
122  return m_files.size ();
123  }
124 
125  int find_files_model::columnCount (const QModelIndex &) const
126  {
127  return m_columnNames.size ();
128  }
129 
130  QVariant find_files_model::data (const QModelIndex& idx, int role) const
131  {
132  QVariant retval;
133 
134  if (idx.isValid ())
135  {
136  if (role == Qt::DisplayRole)
137  {
138  switch (idx.column ())
139  {
140  case 0:
141  retval = QVariant (m_files[idx.row ()].fileName ());
142  break;
143 
144  case 1:
145  retval = QVariant (m_files[idx.row ()].absolutePath ());
146  break;
147 
148  default:
149  break;
150  }
151  }
152  else if (role == Qt::DecorationRole)
153  {
154  switch (idx.column ())
155  {
156  case 0:
157  retval = fileIcon (idx);
158 
159  default:
160  break;
161  }
162  }
163  }
164 
165  return retval;
166  }
167 
168  QVariant find_files_model::headerData (int section,
169  Qt::Orientation orientation,
170  int role) const
171  {
172  return ((orientation == Qt::Horizontal && role == Qt::DisplayRole)
173  ? m_columnNames[section] : QVariant ());
174  }
175 
176  void find_files_model::sort (int column, Qt::SortOrder order)
177  {
178  if (column >= 0)
179  {
180  if (order == Qt::DescendingOrder)
181  m_sortorder = -(column+1);
182  else
183  m_sortorder = column+1;
184  }
185  else
186  m_sortorder = 0;
187 
188  if (m_sortorder != 0)
189  {
190  beginResetModel ();
191 
192  qSort (m_files.begin (), m_files.end (),
194 
195  endResetModel ();
196  }
197  }
198 
199  QFileInfo find_files_model::fileInfo (const QModelIndex & p) const
200  {
201  return p.isValid () ? m_files[p.row ()] : QFileInfo ();
202  }
203 
204  QIcon find_files_model::fileIcon (const QModelIndex& p) const
205  {
206  QFileIconProvider icon_provider;
207 
208  return p.isValid () ? icon_provider.icon (m_files[p.row ()]) : QIcon ();
209  }
210 }
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
bool lessThan(const QVariant &left, const QVariant &right) const
static int left
Definition: randmtzig.cc:184
OCTAVE_EXPORT octave_value_list column
Definition: sparse.cc:123
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void addFile(const QFileInfo &info)
QIcon fileIcon(const QModelIndex &p) const
int columnCount(const QModelIndex &p=QModelIndex()) const
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
in this the arguments are accumulated from left to right
Definition: data.cc:390
QFileInfo fileInfo(const QModelIndex &p) const
QList< QFileInfo > m_files
QVariant data(const QModelIndex &idx, int role) const
octave_value retval
Definition: data.cc:6246
find_files_model(QObject *p=nullptr)
p
Definition: lu.cc:138
QVariant getValue(const QFileInfo &f) const
bool operator()(const QFileInfo &left, const QFileInfo &right) const
int rowCount(const QModelIndex &p=QModelIndex()) const