11#include " server_widgetsettingsscript.h"
22#include " ui_server_widgetsettingsscript.h"
33
4+ #include < QStringListModel>
5+
46#include < server.h>
57#include < gui/server_ui.h>
68#include < gui/dialogs/server_dialogs.h>
@@ -50,6 +52,17 @@ mbServerWidgetSettingsScript::mbServerWidgetSettingsScript(QWidget *parent) :
5052 m_modelInterpreters->setAutoDetected (mbServer::global ()->scriptAutoDetectedExecutables ());
5153 ui->viewInterpreters ->setModel (m_modelInterpreters);
5254
55+ m_modelImportPath = new QStringListModel (this );
56+ ui->viewImportPath ->setModel (m_modelImportPath);
57+ ui->viewImportPath ->setSelectionMode (QAbstractItemView::ExtendedSelection);
58+ connect (m_modelImportPath, &QAbstractItemModel::dataChanged, this , [this ](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int > &roles = QVector<int >())
59+ {
60+ QModelIndex newIndex = topLeft;
61+ if (m_modelImportPath->data (newIndex).toString ().trimmed ().isEmpty ())
62+ m_modelImportPath->removeRow (newIndex.row ());
63+
64+ });
65+
5366 connect (ui->btnFont , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotFont );
5467 connect (ui->btnOutputFont , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotOutputFont );
5568 connect (ui->btnPyAdd , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotPyAdd );
@@ -58,6 +71,13 @@ mbServerWidgetSettingsScript::mbServerWidgetSettingsScript(QWidget *parent) :
5871 connect (ui->btnPyMakeDefault , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotPyMakeDefault);
5972 connect (ui->btnPyClear , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotPyClear );
6073 connect (ui->btnPyBrowse , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotPyBrowse );
74+ connect (ui->btnImportAdd , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportAdd );
75+ connect (ui->btnImportBrowse , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportBrowse );
76+ connect (ui->btnImportRemove , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportRemove );
77+ connect (ui->btnImportClear , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportClear );
78+ connect (ui->btnImportUp , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportUp );
79+ connect (ui->btnImportDown , &QPushButton::clicked, this , &mbServerWidgetSettingsScript::slotImportDown );
80+
6181
6282 QItemSelectionModel *sm = ui->viewInterpreters ->selectionModel ();
6383 connect (sm, &QItemSelectionModel::selectionChanged, this , &mbServerWidgetSettingsScript::selectionChanged);
@@ -195,6 +215,16 @@ void mbServerWidgetSettingsScript::scriptSetDefaultExecutable(const QString &exe
195215 m_modelInterpreters->scriptSetDefaultExecutable (exec);
196216}
197217
218+ QStringList mbServerWidgetSettingsScript::scriptImportPath () const
219+ {
220+ return m_modelImportPath->stringList ();
221+ }
222+
223+ void mbServerWidgetSettingsScript::scriptSetImportPath (const QStringList &path)
224+ {
225+ m_modelImportPath->setStringList (path);
226+ }
227+
198228QFont mbServerWidgetSettingsScript::getScriptEditorFont () const
199229{
200230 QFont f = ui->cmbFontFamily ->currentFont ();
@@ -293,6 +323,123 @@ void mbServerWidgetSettingsScript::slotPyBrowse()
293323 ui->lnExecPath ->setText (s);
294324}
295325
326+ void mbServerWidgetSettingsScript::slotImportAdd ()
327+ {
328+ m_modelImportPath->insertRows (m_modelImportPath->rowCount (), 1 );
329+ QModelIndex newIndex = m_modelImportPath->index (m_modelImportPath->rowCount () - 1 );
330+ ui->viewImportPath ->edit (newIndex);
331+ }
332+
333+ void mbServerWidgetSettingsScript::slotImportBrowse ()
334+ {
335+ QString s = mbServer::global ()->ui ()->dialogs ()->getExistingDirectory (this , " Browse Import Path" );
336+ if (s.count ())
337+ {
338+ m_modelImportPath->insertRows (m_modelImportPath->rowCount (), 1 );
339+ QModelIndex newIndex = m_modelImportPath->index (m_modelImportPath->rowCount () - 1 );
340+ m_modelImportPath->setData (newIndex, s);
341+ }
342+ }
343+
344+ void mbServerWidgetSettingsScript::slotImportRemove ()
345+ {
346+ QItemSelectionModel *sel = ui->viewImportPath ->selectionModel ();
347+ QModelIndexList selected = sel->selectedIndexes ();
348+ std::sort (selected.begin (), selected.end (), [](const QModelIndex &a, const QModelIndex &b) { return a.row () > b.row (); });
349+ Q_FOREACH (const QModelIndex &index, selected)
350+ {
351+ m_modelImportPath->removeRow (index.row ());
352+ }
353+ }
354+
355+ void mbServerWidgetSettingsScript::slotImportClear ()
356+ {
357+ m_modelImportPath->removeRows (0 , m_modelImportPath->rowCount ());
358+ }
359+
360+ void mbServerWidgetSettingsScript::slotImportUp ()
361+ {
362+ QItemSelectionModel *sel = ui->viewImportPath ->selectionModel ();
363+ QModelIndexList selected = sel->selectedIndexes ();
364+ if (selected.isEmpty ())
365+ return ;
366+
367+ // Sort by row ascending
368+ std::sort (selected.begin (), selected.end (), [](const QModelIndex &a, const QModelIndex &b) { return a.row () < b.row (); });
369+
370+ QStringList list = m_modelImportPath->stringList ();
371+ bool changed = false ;
372+
373+ Q_FOREACH (const QModelIndex &index, selected)
374+ {
375+ int row = index.row ();
376+ if (row > 0 )
377+ {
378+ list.swapItemsAt (row, row - 1 );
379+ changed = true ;
380+ }
381+ }
382+
383+ if (changed)
384+ {
385+ m_modelImportPath->setStringList (list);
386+
387+ // Reselect moved items
388+ QItemSelection newSelection;
389+ Q_FOREACH (const QModelIndex &index, selected)
390+ {
391+ int row = index.row ();
392+ if (row > 0 )
393+ newSelection.select (m_modelImportPath->index (row - 1 ), m_modelImportPath->index (row - 1 ));
394+ else
395+ newSelection.select (m_modelImportPath->index (row), m_modelImportPath->index (row));
396+ }
397+ sel->select (newSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
398+ }
399+ }
400+
401+ void mbServerWidgetSettingsScript::slotImportDown ()
402+ {
403+ QItemSelectionModel *sel = ui->viewImportPath ->selectionModel ();
404+ QModelIndexList selected = sel->selectedIndexes ();
405+ if (selected.isEmpty ())
406+ return ;
407+
408+ // Sort by row descending
409+ std::sort (selected.begin (), selected.end (), [](const QModelIndex &a, const QModelIndex &b) { return a.row () > b.row (); });
410+
411+ QStringList list = m_modelImportPath->stringList ();
412+ bool changed = false ;
413+ int rowCount = list.size ();
414+
415+ Q_FOREACH (const QModelIndex &index, selected)
416+ {
417+ int row = index.row ();
418+ if (row < rowCount - 1 )
419+ {
420+ list.swapItemsAt (row, row + 1 );
421+ changed = true ;
422+ }
423+ }
424+
425+ if (changed)
426+ {
427+ m_modelImportPath->setStringList (list);
428+
429+ // Reselect moved items
430+ QItemSelection newSelection;
431+ Q_FOREACH (const QModelIndex &index, selected)
432+ {
433+ int row = index.row ();
434+ if (row < rowCount - 1 )
435+ newSelection.select (m_modelImportPath->index (row + 1 ), m_modelImportPath->index (row + 1 ));
436+ else
437+ newSelection.select (m_modelImportPath->index (row), m_modelImportPath->index (row));
438+ }
439+ sel->select (newSelection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
440+ }
441+ }
442+
296443void mbServerWidgetSettingsScript::selectionChanged (const QItemSelection &selected, const QItemSelection &)
297444{
298445 QModelIndexList ls = selected.indexes ();
0 commit comments