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