GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
set-path-model.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2019-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 #include <string>
32 
33 #include <QFileIconProvider>
34 #include <QMessageBox>
35 #include <QtAlgorithms>
36 
37 #include "qt-interpreter-events.h"
38 #include "set-path-model.h"
39 
40 #include "pathsearch.h"
41 
42 #include "interpreter.h"
43 #include "load-path.h"
44 
45 namespace octave
46 {
48  : QAbstractListModel (p)
49  {
50  connect (this, SIGNAL (update_data_signal (const QStringList&)),
51  this, SLOT (update_data (const QStringList&)));
52 
53  m_revertible = false;
54  }
55 
56  std::string set_path_model::to_string (void)
57  {
58  std::string path_sep = directory_path::path_sep_str ();
59 
60  std::string path_str;
61 
62  QStringList::iterator it = m_dirs.begin ();
63 
64  while (it < m_dirs.end ())
65  {
66  if (it != m_dirs.begin ())
67  path_str += path_sep;
68  path_str += it->toStdString ();
69  ++it;
70  }
71 
72  return path_str;
73  }
74 
76  {
77  std::string path_str = to_string ();
78 
80  ([path_str] (interpreter& interp)
81  {
82  // INTERPRETER THREAD
83 
84  load_path& lp = interp.get_load_path ();
85 
86  lp.set (path_str);
87  });
88  }
89 
91  {
92  beginResetModel ();
93 
94  m_dirs.clear ();
95 
96  endResetModel ();
97  }
98 
100  {
101  model_to_path ();
102 
103  emit interpreter_event
104  ([] (interpreter& interp)
105  {
106  // INTERPRETER THREAD
107 
108  interp.feval ("savepath");
109  });
110  }
111 
113  {
114  clear ();
115 
116  beginInsertRows (QModelIndex (), 0, m_orig_dirs.size () - 1);
118  endInsertRows ();
119 
120  model_to_path ();
121  }
122 
124  {
125  clear ();
126 
127  beginInsertRows (QModelIndex (), 0, m_last_dirs.size () - 1);
129  endInsertRows ();
130 
131  model_to_path ();
132  }
133 
134  void set_path_model::add_dir (const QString& p)
135  {
137 
138  beginInsertRows (QModelIndex (), m_dirs.size (), m_dirs.size ());
139 
140  QList<QString>::Iterator it = m_dirs.begin();
141 
142  m_dirs.insert (it, p);
143 
144  endInsertRows ();
145 
146  model_to_path ();
147  }
148 
149  void set_path_model::rm_dir (const QModelIndexList& indices)
150  {
152 
153  for (int i = indices.size () - 1; i >= 0; i--)
154  {
155  const QModelIndex& idx = indices.at (i);
156 
157  beginRemoveRows (idx, idx.row (), idx.row ());
158  m_dirs.removeAt (idx.row ());
159  endRemoveRows ();
160  }
161 
162  model_to_path ();
163  }
164 
165  void set_path_model::move_dir_up (const QModelIndexList& indices)
166  {
168 
169  for (int i = 0; i < indices.size (); i++)
170  {
171  const QModelIndex& idx = indices.at (i);
172 
173  if (idx.row () == 0 )
174  continue; // already at top position
175 
176  beginMoveRows (idx, idx.row (), idx.row (),
177  this->index (idx.row () - 1), idx.row () - 1);
178 
179  m_dirs.move (idx.row (), idx.row () - 1);
180 
181  endMoveRows ();
182  }
183 
184  model_to_path ();
185  }
186 
187  void set_path_model::move_dir_down (const QModelIndexList& indices)
188  {
190 
191  for (int i = indices.size () - 1; i >= 0; i--)
192  {
193  const QModelIndex& idx = indices.at (i);
194  int bottom = m_dirs.size () - 1;
195 
196  if (idx.row () >= bottom)
197  continue; // already at bottom position
198 
199  beginMoveRows (idx, idx.row (), idx.row (),
200  this->index (idx.row () + 1), idx.row () + 1);
201 
202  m_dirs.move (idx.row (), idx.row () + 1);
203 
204  endMoveRows ();
205  }
206 
207  model_to_path ();
208  }
209 
210  void set_path_model::move_dir_top (const QModelIndexList& indices)
211  {
213 
214  for (int i = 0; i < indices.size (); i++)
215  {
216  const QModelIndex& idx = indices.at (i);
217 
218  if (idx.row () == i)
219  continue; // already at target position
220 
221  beginMoveRows (idx, idx.row (), idx.row (), this->index (i), i);
222 
223  m_dirs.move (idx.row (), i);
224 
225  endMoveRows ();
226  }
227 
228  model_to_path ();
229  }
230 
231  void set_path_model::move_dir_bottom (const QModelIndexList& indices)
232  {
234 
235  for (int i = 0; i < indices.size (); i++)
236  {
237  const QModelIndex& idx = indices.at (i);
238  int target = m_dirs.size () - 1 - i;
239 
240  if (idx.row () == target)
241  continue; // already at target position
242 
243  beginMoveRows (idx, idx.row (), idx.row (),
244  this->index (target), target);
245 
246  m_dirs.move (idx.row (), target);
247 
248  endMoveRows ();
249  }
250 
251  model_to_path ();
252  }
253 
254  int set_path_model::rowCount (const QModelIndex&) const
255  {
256  return m_dirs.size ();
257  }
258 
259  QVariant set_path_model::data (const QModelIndex& idx, int role) const
260  {
261  QVariant retval;
262  if (idx.isValid ())
263  {
264  switch (role)
265  {
266  case Qt::DisplayRole:
267  retval = QVariant (m_dirs[idx.row ()]);
268  break;
269 
270  case Qt::DecorationRole:
271  retval = QVariant (QIcon ());
272  break;
273 
274  case Qt::SizeHintRole:
275  retval = QVariant (QSize (10, 20));
276  break;
277  }
278  }
279 
280  return retval;
281  }
282 
284  {
285  emit interpreter_event
286  ([this] (interpreter& interp)
287  {
288  // INTERPRETER THREAD
289 
290  load_path& lp = interp.get_load_path ();
291 
292  std::list<std::string> dir_list = lp.dir_list ();
293 
294  QStringList qs_dir_list;
295 
296  for (const auto& dir : dir_list)
297  qs_dir_list << QString::fromStdString (dir);
298 
299  emit update_data_signal (qs_dir_list);
300  });
301 
302  m_revertible = false;
303  }
304 
305  void set_path_model::update_data (const QStringList& dirs)
306  {
307  m_dirs = dirs;
308 
309  m_dirs.removeAll (".");
310 
311  if (! m_revertible)
312  {
313  // first time update
316 
317  m_revertible = true;
318  }
319 
320  int numel = m_dirs.size ();
321 
322  emit dataChanged (QAbstractListModel::index (0, 0),
323  QAbstractListModel::index (numel-1, 0));
324  }
325 }
static std::string path_sep_str(void)
Definition: pathsearch.cc:127
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
load_path & get_load_path(void)
Definition: interpreter.h:243
std::list< std::string > dir_list(void) const
Definition: load-path.cc:847
void set(const std::string &p, bool warn=false, bool is_init=false)
Definition: load-path.cc:286
std::string to_string(void)
void move_dir_down(const QModelIndexList &indices)
QVariant data(const QModelIndex &idx, int role) const
void interpreter_event(const fcn_callback &fcn)
void add_dir(const QString &p)
void move_dir_top(const QModelIndexList &indices)
void move_dir_up(const QModelIndexList &indices)
int rowCount(const QModelIndex &p=QModelIndex()) const
void rm_dir(const QModelIndexList &indices)
void move_dir_bottom(const QModelIndexList &indices)
void update_data_signal(const QStringList &dirs)
void update_data(const QStringList &dirs)
set_path_model(QObject *p=nullptr)
QString fromStdString(const std::string &s)
T::size_type numel(const T &str)
Definition: oct-string.cc:71
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811