GNU Octave  8.1.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-2023 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 <QPointer>
36 #include <QtAlgorithms>
37 
38 #include "qt-interpreter-events.h"
39 #include "set-path-model.h"
40 
41 #include "pathsearch.h"
42 
43 #include "interpreter.h"
44 #include "load-path.h"
45 
47 
50 {
51  connect (this, &set_path_model::update_data_signal,
53 
54  m_revertible = false;
55 }
56 
57 std::string set_path_model::to_string (void)
58 {
59  std::string path_sep = directory_path::path_sep_str ();
60 
61  std::string path_str;
62 
63  QStringList::iterator it = m_dirs.begin ();
64 
65  while (it < m_dirs.end ())
66  {
67  if (it != m_dirs.begin ())
68  path_str += path_sep;
69  path_str += it->toStdString ();
70  ++it;
71  }
72 
73  return path_str;
74 }
75 
77 {
78  std::string path_str = to_string ();
79 
81  ([=] (interpreter& interp)
82  {
83  // INTERPRETER THREAD
84 
85  load_path& lp = interp.get_load_path ();
86 
87  lp.set (path_str);
88  });
89 }
90 
92 {
93  beginResetModel ();
94 
95  m_dirs.clear ();
96 
97  endResetModel ();
98 }
99 
101 {
102  model_to_path ();
103 
104  emit interpreter_event
105  ([] (interpreter& interp)
106  {
107  // INTERPRETER THREAD
108 
109  interp.feval ("savepath");
110  });
111 }
112 
114 {
115  clear ();
116 
117  beginInsertRows (QModelIndex (), 0, m_orig_dirs.size () - 1);
119  endInsertRows ();
120 
121  model_to_path ();
122 }
123 
125 {
126  clear ();
127 
128  beginInsertRows (QModelIndex (), 0, m_last_dirs.size () - 1);
130  endInsertRows ();
131 
132  model_to_path ();
133 }
134 
135 void set_path_model::add_dir (const QString& p)
136 {
138 
139  beginInsertRows (QModelIndex (), m_dirs.size (), m_dirs.size ());
140 
141  QList<QString>::Iterator it = m_dirs.begin();
142 
143  m_dirs.insert (it, p);
144 
145  endInsertRows ();
146 
147  model_to_path ();
148 }
149 
150 void set_path_model::rm_dir (const QModelIndexList& indices)
151 {
153 
154  for (int i = indices.size () - 1; i >= 0; i--)
155  {
156  const QModelIndex& idx = indices.at (i);
157 
158  beginRemoveRows (idx, idx.row (), idx.row ());
159  m_dirs.removeAt (idx.row ());
160  endRemoveRows ();
161  }
162 
163  model_to_path ();
164 }
165 
166 void set_path_model::move_dir_up (const QModelIndexList& indices)
167 {
169 
170  for (int i = 0; i < indices.size (); i++)
171  {
172  const QModelIndex& idx = indices.at (i);
173 
174  if (idx.row () == 0 )
175  continue; // already at top position
176 
177  beginMoveRows (idx, idx.row (), idx.row (),
178  this->index (idx.row () - 1), idx.row () - 1);
179 
180  m_dirs.move (idx.row (), idx.row () - 1);
181 
182  endMoveRows ();
183  }
184 
185  model_to_path ();
186 }
187 
188 void set_path_model::move_dir_down (const QModelIndexList& indices)
189 {
191 
192  for (int i = indices.size () - 1; i >= 0; i--)
193  {
194  const QModelIndex& idx = indices.at (i);
195  int bottom = m_dirs.size () - 1;
196 
197  if (idx.row () >= bottom)
198  continue; // already at bottom position
199 
200  beginMoveRows (idx, idx.row (), idx.row (),
201  this->index (idx.row () + 1), idx.row () + 1);
202 
203  m_dirs.move (idx.row (), idx.row () + 1);
204 
205  endMoveRows ();
206  }
207 
208  model_to_path ();
209 }
210 
211 void set_path_model::move_dir_top (const QModelIndexList& indices)
212 {
214 
215  for (int i = 0; i < indices.size (); i++)
216  {
217  const QModelIndex& idx = indices.at (i);
218 
219  if (idx.row () == i)
220  continue; // already at target position
221 
222  beginMoveRows (idx, idx.row (), idx.row (), this->index (i), i);
223 
224  m_dirs.move (idx.row (), i);
225 
226  endMoveRows ();
227  }
228 
229  model_to_path ();
230 }
231 
232 void set_path_model::move_dir_bottom (const QModelIndexList& indices)
233 {
235 
236  for (int i = 0; i < indices.size (); i++)
237  {
238  const QModelIndex& idx = indices.at (i);
239  int target = m_dirs.size () - 1 - i;
240 
241  if (idx.row () == target)
242  continue; // already at target position
243 
244  beginMoveRows (idx, idx.row (), idx.row (),
245  this->index (target), target);
246 
247  m_dirs.move (idx.row (), target);
248 
249  endMoveRows ();
250  }
251 
252  model_to_path ();
253 }
254 
255 int set_path_model::rowCount (const QModelIndex&) const
256 {
257  return m_dirs.size ();
258 }
259 
260 QVariant set_path_model::data (const QModelIndex& idx, int role) const
261 {
262  QVariant retval;
263  if (idx.isValid ())
264  {
265  switch (role)
266  {
267  case Qt::DisplayRole:
268  retval = QVariant (m_dirs[idx.row ()]);
269  break;
270 
271  case Qt::DecorationRole:
272  retval = QVariant (QIcon ());
273  break;
274 
275  case Qt::SizeHintRole:
276  retval = QVariant (QSize (10, 20));
277  break;
278  }
279  }
280 
281  return retval;
282 }
283 
285 {
286  // The interpreter_event callback function below emits a signal.
287  // Because we don't control when that happens, use a guarded pointer
288  // so that the callback can abort if this object is no longer valid.
289 
290  QPointer<set_path_model> this_spm (this);
291 
292  emit interpreter_event
293  ([=] (interpreter& interp)
294  {
295  // INTERPRETER THREAD
296 
297  // We can skip the entire callback function because it does not
298  // make any changes to the interpreter state.
299 
300  if (this_spm.isNull ())
301  return;
302 
303  load_path& lp = interp.get_load_path ();
304 
305  std::list<std::string> dir_list = lp.dir_list ();
306 
307  QStringList qs_dir_list;
308 
309  for (const auto& dir : dir_list)
310  qs_dir_list << QString::fromStdString (dir);
311 
312  emit update_data_signal (qs_dir_list);
313  });
314 
315  m_revertible = false;
316 }
317 
318 void set_path_model::update_data (const QStringList& dirs)
319 {
320  m_dirs = dirs;
321 
322  m_dirs.removeAll (".");
323 
324  if (! m_revertible)
325  {
326  // first time update
329 
330  m_revertible = true;
331  }
332 
333  int numel = m_dirs.size ();
334 
335  emit dataChanged (QAbstractListModel::index (0, 0),
336  QAbstractListModel::index (numel-1, 0));
337 }
338 
OCTAVE_END_NAMESPACE(octave)
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:283
void set(const std::string &p, bool warn=false, bool is_init=false)
Definition: load-path.cc:348
std::list< std::string > dir_list(void) const
Definition: load-path.cc:911
void move_dir_down(const QModelIndexList &indices)
void add_dir(const QString &p)
void update_data_signal(const QStringList &dirs)
void move_dir_top(const QModelIndexList &indices)
void path_to_model(void)
void interpreter_event(const fcn_callback &fcn)
void model_to_path(void)
int rowCount(const QModelIndex &p=QModelIndex()) const
QStringList m_orig_dirs
void clear(void)
void move_dir_bottom(const QModelIndexList &indices)
QStringList m_last_dirs
QVariant data(const QModelIndex &idx, int role) const
QStringList m_dirs
std::string to_string(void)
void rm_dir(const QModelIndexList &indices)
void move_dir_up(const QModelIndexList &indices)
void revert(void)
void revert_last(void)
void update_data(const QStringList &dirs)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
for(octave_idx_type i=0;i< n;i++) ac+
Definition: mx-inlines.cc:756
QString fromStdString(const std::string &s)
T::size_type numel(const T &str)
Definition: oct-string.cc:71