• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ossia / score / 45070253512

22 Nov 2026 09:06PM UTC coverage: 62.137% (+0.3%) from 61.818%
45070253512

Pull #2409

github

web-flow
Merge 74d2d6e4b into ef0dd3ce5
Pull Request #2409: Snippets, a keyboard-driven process picker, node copy and paste, musical snapping in the piano roll

269268 of 804435 branches covered (33.47%)

2227 of 2815 new or added lines in 90 files covered. (79.11%)

108 existing lines in 24 files now uncovered.

274840 of 442315 relevant lines covered (62.14%)

763452.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.33
/src/plugins/score-plugin-library/Library/ProcessWidget.cpp
1
#include "ProcessWidget.hpp"
2

3
#include <Process/ApplicationPlugin.hpp>
4
#include <Process/PresetTree.hpp>
5
#include <Process/Process.hpp>
6
#include <Process/PreviewSettings.hpp>
7
#include <Process/ProcessList.hpp>
8

9
#include <Library/Snippet.hpp>
10
#include <Library/PresetFile.hpp>
11
#include <Library/ItemModelFilterLineEdit.hpp>
12
#include <Library/LibrarySettings.hpp>
13
#include <Library/LibraryWidget.hpp>
14
#include <Library/PresetItemModel.hpp>
15
#include <Library/ProcessesItemModel.hpp>
16
#include <Library/RecursiveFilterProxy.hpp>
17

18
#include <score/application/GUIApplicationContext.hpp>
19
#include <score/document/DocumentInterface.hpp>
20
#include <score/selection/SelectionDispatcher.hpp>
21
#include <score/selection/SelectionStack.hpp>
22
#include <score/widgets/Pixmap.hpp>
23
#include <score/tools/File.hpp>
24
#include <score/tools/ProjectFiles.hpp>
25
#include <score/widgets/HelpInteraction.hpp>
26
#include <score/widgets/MarginLess.hpp>
27
#include <score/widgets/MessageBox.hpp>
28

29
#include <core/document/Document.hpp>
30
#include <core/presenter/DocumentManager.hpp>
31
#include <core/view/FixedTabWidget.hpp>
32
#include <core/view/Window.hpp>
33

34
#include <ossia/detail/algorithms.hpp>
35
#include <ossia/detail/math.hpp>
36

37
#include <QApplication>
38
#include <QLabel>
39
#include <QFile>
40
#include <QLineEdit>
41
#include <QPlainTextEdit>
42
#include <QMainWindow>
43
#include <QScrollArea>
44
#include <QTimer>
45
#include <QPushButton>
46
#include <QSettings>
47
#include <QShortcut>
48
#include <QSortFilterProxyModel>
49
#include <QToolButton>
50
#include <QVBoxLayout>
51

52
namespace Library
53
{
54
class InfoWidget final : public QScrollArea
55
{
56
public:
57
  InfoWidget(QWidget* parent)
6,704 !
58
      : QScrollArea{parent}
3,352 ✔
59
  {
3,352 ✔
60
    setObjectName(QStringLiteral("ProcessInfo"));
3,352 !
61
    // A long description scrolls rather than taking the presets' room
62
    setWidgetResizable(true);
3,352 !
63
    setFrameShape(QFrame::NoFrame);
3,352 !
64
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
3,352 !
65
    setMaximumHeight(160);
3,352 !
66
    // As tall as its text: the room left goes to the description being edited
67
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
3,352 !
68
    auto content = new QWidget;
3,352 !
69
    auto lay = new score::MarginLess<QVBoxLayout>{content};
3,352 !
70
    lay->setAlignment(Qt::AlignTop);
3,352 !
71
    lay->setContentsMargins(6, 6, 6, 6);
3,352 !
72
    QFont f;
3,352 !
73
    f.setBold(true);
3,352 !
74
    m_name.setFont(f);
3,352 !
75
    m_name.setAlignment(Qt::AlignTop);
3,352 !
76
    lay->addWidget(&m_name);
3,352 !
77
    lay->addWidget(&m_author);
3,352 !
78
    m_author.setWordWrap(true);
3,352 !
79
    m_author.setAlignment(Qt::AlignTop);
3,352 !
80
    lay->addWidget(&m_io);
3,352 !
81
    lay->addWidget(&m_description);
3,352 !
82
    m_description.setWordWrap(true);
3,352 !
83
    m_description.setAlignment(Qt::AlignTop);
3,352 !
84
    lay->addWidget(&m_tags);
3,352 !
85
    m_tags.setWordWrap(true);
3,352 !
86
    m_tags.setAlignment(Qt::AlignTop);
3,352 !
87
    m_documentationLink.setAlignment(Qt::AlignBottom);
3,352 !
88
    lay->addStretch(2);
3,352 !
89
    lay->addWidget(&m_documentationLink);
3,352 !
90
    setWidget(content);
3,352 !
91
    setVisible(false);
3,352 !
92
  }
3,352 ✔
93

94
  //! As tall as its text, up to its maximum, past which it scrolls
95
  QSize sizeHint() const override
2 ✔
96
  {
97
    int h = 0;
2 ✔
98
    if(auto w = widget())
2 !
99
    {
100
      // Wrapped text is as tall as the width it wraps in
101
      const int width = viewport()->width();
2 ✔
102
      h = w->hasHeightForWidth() && width > 0 ? w->heightForWidth(width)
2 !
103
                                               : w->sizeHint().height();
×
104
      h += 2 * frameWidth();
2 ✔
105
    }
2 ✔
106
    return {QScrollArea::sizeHint().width(), std::min(h, maximumHeight())};
2 ✔
107
  }
108

109
  //! A scroll area is otherwise never shorter than about four lines, however
110
  //! little text it holds
111
  QSize minimumSizeHint() const override
1 ✔
112
  {
113
    return {QScrollArea::minimumSizeHint().width(), std::min(sizeHint().height(), 40)};
1 ✔
114
  }
115

116
  //! `snippet`: what the snippet `d` says of itself, if it is one
117
  void setData(const std::optional<ProcessData>& d, const SnippetInfo* snippet = nullptr)
17 ✔
118
  {
119
    setContent(d, snippet);
17 ✔
120
    updateVisibility();
17 ✔
121
  }
17 ✔
122

123
  //! The description of what is shown, hidden while an editable one is
124
  void showDescription(bool on)
30 ✔
125
  {
126
    m_showDescription = on;
30 ✔
127
    updateVisibility();
30 ✔
128
  }
30 ✔
129

130
private:
131
  //! Empty lines take no room
132
  void updateVisibility()
47 ✔
133
  {
134
    for(QLabel* l : {&m_author, &m_io, &m_tags, &m_documentationLink})
235 ✔
135
      l->setVisible(!l->text().isEmpty());
188 !
136
    m_description.setVisible(m_showDescription && !m_description.text().isEmpty());
47 !
137
    updateGeometry();
47 ✔
138
  }
47 ✔
139

140
  void setContent(const std::optional<ProcessData>& d, const SnippetInfo* snippet)
17 ✔
141
  {
142
    m_author.setText(QString{});
17 !
143
    m_description.setText(QString{});
17 !
144
    m_io.setText(QString{});
17 !
145
    m_tags.setText(QString{});
17 !
146
    m_documentationLink.clear();
17 ✔
147

148
    if(d && d->key == snippetKey())
17 !
149
    {
NEW
150
      setVisible(true);
×
NEW
151
      m_name.setText(d->prettyName);
×
NEW
152
      if(snippet)
×
153
      {
154
        // Hidden while the field below edits it
NEW
155
        m_description.setText(snippet->description);
×
NEW
156
        if(!snippet->tags.isEmpty())
×
NEW
157
          m_tags.setText(tr("Tags: ") + snippet->tags.join(", "));
×
NEW
158
      }
×
NEW
159
      return;
×
160
    }
161

162
    if(d)
17 ✔
163
    {
164
      if(auto f
14 !
165
         = score::GUIAppContext().interfaces<Process::ProcessFactoryList>().get(d->key))
14 ✔
166
      {
167
        setVisible(true);
14 ✔
168
        auto desc = f->descriptor(d->customData);
14 ✔
169

170
        if(!d->prettyName.isEmpty())
14 !
171
          m_name.setText(d->prettyName);
14 !
172
        else
173
          m_name.setText(desc.prettyName);
×
174

175
        if(!desc.author.isEmpty())
14 !
176
          m_author.setText(tr("Provided by ") + desc.author);
14 !
177
        else
178
          m_author.setText(tr("Author unknown"));
×
179

180
        if(!desc.description.isEmpty())
14 !
181
          m_description.setText(desc.description);
14 !
182

183
        QString io;
14 ✔
184
        if(desc.inlets)
14 !
185
        {
186
          io += QString::number(desc.inlets->size()) + tr(" input");
14 !
187
          if(desc.inlets->size() > 1)
14 ✔
188
            io += "s";
11 !
189
        }
14 ✔
190
        if(desc.inlets && desc.outlets)
14 !
191
          io += ", ";
14 !
192
        if(desc.outlets)
14 !
193
        {
194
          io += QString::number(desc.outlets->size()) + tr(" output");
14 !
195
          if(desc.outlets->size() > 1)
14 ✔
196
            io += "s";
3 !
197
        }
14 ✔
198
        m_io.setText(io);
14 !
199
        if(!desc.tags.empty())
14 !
200
        {
201
          m_tags.setText(tr("Tags: ") + desc.tags.join(", "));
×
202
        }
×
203

204
        if(!desc.documentationLink.isEmpty())
14 !
205
        {
206
          QString linkText = "Explore the documentation";
14 !
207
          QString iconPath = ":/icons/undock_on.png";
14 !
208
          QString iconHtml = "<img src=\"" + iconPath + "\" width=\"16\" height=\"16\" style=\"vertical-align:middle;\" />";
14 !
209
          QString fullLink = "<a href=\"" + desc.documentationLink.toString() + "\">" + linkText + " " + iconHtml + "</a>";
14 !
210
          m_documentationLink.setTextFormat(Qt::RichText);
14 !
211
          m_documentationLink.setText(fullLink);
14 !
212
          m_documentationLink.setOpenExternalLinks(true);
14 !
213
        }
14 ✔
214

215
        return;
216
      }
14 ✔
217
    }
×
218

219
    setVisible(false);
3 ✔
220
  }
17 ✔
221

222
  QLabel m_name;
223
  QLabel m_io;
224
  QLabel m_description;
225
  QLabel m_author;
226
  QLabel m_tags;
227
  QLabel m_documentationLink;
228
  bool m_showDescription{true};
3,352 ✔
229
};
230

231
//! Calls `done` when it loses the focus: where an edit is finished.
232
class DescriptionEdit final : public QPlainTextEdit
233
{
234
public:
235
  DescriptionEdit(std::function<void()> done, QWidget* parent)
3,352 ✔
236
      : QPlainTextEdit{parent}
3,352 ✔
237
      , m_done{std::move(done)}
3,352 ✔
238
  {
3,352 ✔
239
    setTabChangesFocus(true);
3,352 !
240
  }
3,352 ✔
241

242
private:
NEW
243
  void focusOutEvent(QFocusEvent* e) override
×
244
  {
NEW
245
    QPlainTextEdit::focusOutEvent(e);
×
NEW
246
    m_done();
×
NEW
247
  }
×
248
  std::function<void()> m_done;
249
};
250

251
ProcessWidget::ProcessWidget(const score::GUIApplicationContext& ctx, QWidget* parent)
13,408 !
252
    : QWidget{parent}
3,352 ✔
253
    , m_processModel{new ProcessesItemModel{ctx, this}}
3,352 !
254
    , m_presetModel{new PresetItemModel{ctx, this}}
3,352 !
255
    , m_preview{this}
3,352 !
256
{
3,352 ✔
257
  auto slay = new score::MarginLess<QVBoxLayout>{this};
3,352 !
258
  setLayout(slay);
3,352 !
259

260
  setStatusTip(
3,352 !
261
      QObject::tr("This panel shows the available processes.\n"
3,352 !
262
                  "They can be drag'n'dropped in the score, in intervals, "
263
                  "and sometimes in effect chains."));
264

265
  {
266
    m_notice.setWordWrap(true);
3,352 !
267
    m_notice.setTextFormat(Qt::RichText);
3,352 !
268
    m_notice.setTextInteractionFlags(Qt::TextBrowserInteraction);
3,352 !
269
    m_notice.setOpenExternalLinks(false);
3,352 !
270
    m_notice.setContentsMargins(4, 4, 4, 4);
3,352 !
271
    m_notice.hide();
3,352 !
272
    slay->addWidget(&m_notice);
3,352 !
273
    connect(&m_notice, &QLabel::linkActivated, this, [this](const QString&) {
3,353 !
274
      if(m_noticeAction)
1 !
275
        m_noticeAction();
1 ✔
276
    });
1 ✔
277
  }
278

279
  QLineEdit* filter{};
3,352 ✔
280

281
  {
282
    auto processFilterProxy = new ProcessFilterProxy{this};
3,352 !
283
    processFilterProxy->setSourceModel(m_processModel);
3,352 !
284
    processFilterProxy->setFilterKeyColumn(0);
3,352 !
285
    filter = new ItemModelFilterLineEdit{*processFilterProxy, m_tv, this};
3,352 !
286
    m_processFilter = filter;
3,352 ✔
287
    slay->addWidget(filter);
3,352 !
288
    slay->addWidget(&m_tv, 3);
3,352 !
289
    m_tv.setModel(processFilterProxy);
3,352 !
290
    // Only snippets can be edited, by their name: F2 or their menu
291
    m_tv.setEditTriggers(QAbstractItemView::EditKeyPressed);
3,352 !
292
    // A renamed snippet is another file: what shows it is built again
293
    connect(
3,352 !
294
        m_processModel, &QAbstractItemModel::dataChanged, this,
3,352 ✔
295
        [this, processFilterProxy](const QModelIndex& first, const QModelIndex&) {
3,352 ✔
NEW
296
      if(m_tv.selectionModel()
×
NEW
297
         && m_tv.selectionModel()->isSelected(processFilterProxy->mapFromSource(first)))
×
NEW
298
        m_tv.reselect();
×
NEW
299
    });
×
300
    m_tv.setStatusTip(statusTip());
3,352 !
301
    m_tv.setUniformRowHeights(true);
3,352 !
302
    setup_treeview(m_tv);
3,352 !
303
  }
304

305
  {
306
    auto lay = new score::MarginLess<QHBoxLayout>{&m_targetBar};
3,352 !
307
    lay->setContentsMargins(4, 2, 2, 2);
3,352 !
308
    m_targetLabel.setTextFormat(Qt::PlainText);
3,352 !
309
    // A browser's preview toggle: lit while presets load as they are browsed
310
    auto audition = new QPushButton{&m_targetBar};
3,352 !
311
    audition->setCheckable(true);
3,352 !
312
    audition->setFlat(true);
3,352 !
313
    audition->setMinimumWidth(24);
3,352 !
314
    audition->setMaximumWidth(24);
3,352 !
315
    const auto onIcon = QIcon{score::get_pixmap(":/icons/headphones_on.png")};
3,352 !
316
    const auto offIcon = QIcon{score::get_pixmap(":/icons/headphones_off.png")};
3,352 !
317
    const auto showAudition = [=, this](bool on) {
13,412 !
318
      audition->setIcon(on ? onIcon : offIcon);
3,356 ✔
319
      audition->setToolTip(
6,712 !
320
          on ? tr("Presets load as you move through them. Click to only load "
3,356 ✔
321
                  "one on Enter or a double-click.")
322
             : tr("Presets load on Enter or a double-click. Click to load them "
2 ✔
323
                  "as you move through them."));
324
    };
3,356 ✔
325
    m_auditionEnabled = QSettings{}.value("Library/AuditionPresets", true).toBool();
3,352 !
326
    audition->setChecked(m_auditionEnabled);
3,352 !
327
    showAudition(m_auditionEnabled);
3,352 !
328
    connect(audition, &QPushButton::toggled, this, [this, showAudition](bool on) {
3,356 !
329
      m_auditionEnabled = on;
4 ✔
330
      QSettings{}.setValue("Library/AuditionPresets", on);
4 !
331
      showAudition(on);
4 ✔
332
      // What is heard is kept
333
      m_audition.commit();
4 ✔
334
      m_presetModel->refreshCurrent();
4 ✔
335
    });
4 ✔
336
    lay->addWidget(audition);
3,352 !
337
    lay->addWidget(&m_targetLabel, 1);
3,352 !
338
    auto save = new QToolButton{&m_targetBar};
3,352 !
339
    save->setText(tr("Save"));
3,352 !
340
    save->setToolTip(
3,352 !
341
        tr("Save the process's current state as a preset, in the selected folder"));
3,352 !
342
    connect(save, &QToolButton::clicked, this, [this] {
3,352 !
343
      if(auto proc = m_presetModel->target())
×
344
        m_presetModel->savePreset(
×
345
            *proc, m_presetModel->folderPath(m_presets.currentIndex()).join('/'));
×
346
    });
×
347
    lay->addWidget(save);
3,352 !
348
    auto close = new QToolButton{&m_targetBar};
3,352 !
349
    close->setIcon(QIcon{score::get_pixmap(":/icons/close_off.png")});
3,352 !
350
    close->setAutoRaise(true);
3,352 !
351
    close->setToolTip(tr("Stop loading the presets into this process"));
3,352 !
352
    connect(close, &QToolButton::clicked, this, &ProcessWidget::clearTarget);
3,352 !
353
    lay->addWidget(close);
3,352 !
354
    m_targetBar.hide();
3,352 !
355
    slay->addWidget(&m_targetBar);
3,352 !
356
  }
3,352 ✔
357

358
  {
359
    auto search = new QLineEdit{this};
3,352 !
360
    m_presetSearch = search;
3,352 ✔
361
    search->setPlaceholderText(tr("Search presets"));
3,352 !
362
    search->setClearButtonEnabled(true);
3,352 !
363
    connect(search, &QLineEdit::textChanged, m_presetModel, &PresetItemModel::setFilterText);
3,352 !
364
    slay->addWidget(search);
3,352 !
365
    m_presets.setPresetModel(m_presetModel);
3,352 !
366
    slay->addWidget(&m_presets, 2);
3,352 !
367
  }
368

369
  slay->addWidget(&m_preview);
3,352 !
370
  {
371
    auto previewLay = new score::MarginLess<QHBoxLayout>{&m_preview};
3,352 !
372
    m_preview.setLayout(previewLay);
3,352 !
373
    m_preview.hide();
3,352 !
374
  }
375

376
  // Turning previews off has to reach the thumbnail already on screen, not wait
377
  // for the next selection. Both handlers share m_previewChild, so rebuild the
378
  // one that is actually showing: re-announcing the tree selection would
379
  // destroy a preset preview and clear state nothing asked to clear.
380
  con(Process::PreviewSettings::instance(), &Process::PreviewSettings::enabledChanged,
6,704 !
381
      this, [this](bool) {
3,352 ✔
382
    if(m_presetModel->preset(m_presets.currentIndex()) && m_refreshPresetPreview)
×
383
      m_refreshPresetPreview();
×
384
    else if(auto* sm = m_tv.selectionModel(); sm && !sm->selectedIndexes().isEmpty())
×
385
      m_tv.reselect();
×
386
  });
×
387

388
  auto infoWidg = m_info = new InfoWidget{this};
3,352 !
389
  score::setHelp(infoWidg, statusTip());
3,352 !
390
  slay->addWidget(infoWidg);
3,352 !
391

392
  {
393
    m_descriptionEdit = new DescriptionEdit{[this] { commitDescription(); }, this};
3,352 !
394
    m_descriptionEdit->setPlaceholderText(tr("Description"));
3,352 !
395
    m_descriptionEdit->setToolTip(tr("Saved in the file when the field loses the focus"));
3,352 !
396
    m_descriptionEdit->setMinimumHeight(60);
3,352 !
397
    m_descriptionEdit->hide();
3,352 !
398
    slay->addWidget(m_descriptionEdit, 1);
3,352 !
399
  }
400

401
  connect(
3,352 !
402
      &m_tv, &ProcessTreeView::selected, this,
3,352 ✔
403
      [this, infoWidg, filter](const std::optional<Library::ProcessData>& selected) {
3,369 ✔
404
    // Folders (categories, preset and file groups) carry the nil key: they are
405
    // no process. A factory registered under that key, such as an addon whose
406
    // uuid is still the template's placeholder, must not stand in for them.
407
    const auto pdata = selected && selected->key != Process::ProcessModelFactory::ConcreteKey{}
17 !
408
                           ? selected
14 ✔
409
                           : std::nullopt;
3 ✔
410
#if defined(_WIN32)
411
    const bool filter_had_focus = filter->hasFocus();
412
    const bool tree_had_focus = m_tv.hasFocus();
413
#endif
414
    m_preview.hide();
17 !
415

416
    // A folder or a snippet has no presets: no list nor search for them.
417
    const bool snippet = pdata && pdata->key == snippetKey();
31 !
418
    const bool hasPresets = pdata && !snippet;
17 ✔
419
    m_presets.setVisible(hasPresets);
17 !
420
    m_presetSearch->setVisible(hasPresets);
17 !
421

422
    // What a snippet says of itself, read once for all that shows it
423
    std::optional<SnippetInfo> snippetInfo;
17 ✔
424
    if(snippet)
17 !
NEW
425
      snippetInfo = readSnippetInfoFile(pdata->customData, false);
×
426
    infoWidg->setData(pdata, snippetInfo ? &*snippetInfo : nullptr);
17 !
427

428
    // Only the user's own snippets are written to
429
    if(snippet && isUserLibraryFile(pdata->customData))
17 !
430
    {
NEW
431
      editDescription(
×
NEW
432
          snippetInfo->description,
×
NEW
433
          [this, path = pdata->customData, tags = snippetInfo->tags](const QString& text) {
×
NEW
434
        if(!writeSnippetDescription(path, text))
×
435
        {
NEW
436
          score::warning(
×
NEW
437
              this, tr("Snippet"), tr("The description could not be saved in %1").arg(path));
×
NEW
438
          return;
×
439
        }
NEW
440
        m_processModel->setSearchText(
×
NEW
441
            snippetKey(), path, snippetSearchText(SnippetInfo{text, tags, {}}));
×
NEW
442
      });
×
NEW
443
    }
×
444
    else
445
    {
446
      editDescription({}, {});
17 !
447
    }
448

449
    // Another process chosen by the user: its presets no longer load into
450
    // the one whose button opened them
451
    if(!m_choosingTarget)
17 ✔
452
      if(auto t = m_presetModel->target(); t && (!pdata || pdata->key != t->concreteKey()))
7 !
453
        clearTarget();
1 !
454

455
    // Its presets
456
    if(pdata)
17 ✔
457
      m_presetModel->setProcess(Process::ProcessIdentifier{pdata->key, pdata->customData});
14 !
458
    else
459
      m_presetModel->setProcess(std::nullopt);
3 !
460

461
    // Update the preview
462
    delete m_previewChild;
17 !
463
    m_previewChild = nullptr;
17 ✔
464
    if(pdata)
17 ✔
465
    {
466
      if(QFile::exists(pdata->customData))
14 !
467
      {
468
        for(auto lib : libraryInterface(pdata->customData))
×
469
        {
470
          if((m_previewChild = lib->previewWidget(pdata->customData, &m_preview)))
×
471
          {
472
            m_preview.layout()->addWidget(m_previewChild);
×
473
            m_preview.show();
×
474
            break;
×
475
          }
476
        }
477
      }
×
478
    }
14 ✔
479

480
#if defined(_WIN32)
481
    for(int i = 0; i < 100; i++)
482
    {
483
      QTimer::singleShot(i, this, [=, this] {
484
        if(filter_had_focus)
485
          filter->setFocus();
486
        else if(tree_had_focus)
487
          m_tv.setFocus();
488
      });
489
    }
490
#endif
491
  });
17 ✔
492

493
  connect(
3,352 !
494
      &m_presets, &PresetTreeView::selected, this,
3,352 ✔
495
      [this, filter](const std::optional<Process::Preset>& selected) {
3,365 ✔
496
    if(!selected)
13 !
497
      return;
×
498

499
    // Heard at once in the process the presets load into
500
    if(auto proc = m_presetModel->target(); proc && m_auditionEnabled)
13 !
501
      if(m_audition.preview(*proc, *selected))
24 !
502
        m_presetModel->refreshCurrent();
12 ✔
503

504
#if defined(_WIN32)
505
    const bool filter_had_focus = filter->hasFocus();
506
    const bool tree_had_focus = m_tv.hasFocus();
507
#endif
508

509
    m_preview.hide();
13 ✔
510
    delete m_previewChild;
13 !
511
    m_previewChild = nullptr;
13 ✔
512

513
    const auto& preset = *selected;
13 ✔
514

515
    // Only the user's own presets are written to
516
    if(const int row = m_presetModel->presetRow(m_presets.currentIndex());
26 !
517
       m_presetModel->canEdit(row))
13 ✔
NEW
518
      editDescription(
×
NEW
519
          preset.description, [this, path = preset.path](const QString& text) {
×
520
        // Rows move as presets come and go: found again by its file
NEW
521
        m_presetModel->setDescription(m_presetModel->presetRowOf(path), text);
×
NEW
522
      });
×
523
    else
524
      editDescription({}, {});
13 !
525

526
    const auto buildPreview = [this](const Process::Preset& p) {
26 ✔
527
      delete m_previewChild;
13 !
528
      m_previewChild = nullptr;
13 ✔
529
      for(auto& lib : score::GUIAppContext().interfaces<LibraryInterfaceList>())
403 ✔
530
      {
531
        if((m_previewChild = lib.previewWidget(p, &m_preview)))
390 !
532
        {
533
          m_preview.layout()->addWidget(m_previewChild);
×
534
          m_preview.show();
×
535
          break;
×
536
        }
537
      }
538
    };
13 ✔
539
    buildPreview(preset);
13 ✔
540
    m_refreshPresetPreview = [this, buildPreview, p = preset] { buildPreview(p); };
13 !
541
#if defined(_WIN32)
542
    for(int i = 0; i < 100; i++)
543
    {
544
      QTimer::singleShot(i, this, [=, this] {
545
        if(filter_had_focus)
546
          filter->setFocus();
547
        else if(tree_had_focus)
548
          m_tv.setFocus();
549
      });
550
    }
551
#endif
552
  });
13 ✔
553
  m_presets.setMinimumHeight(100);
3,352 !
554

555
  auto& presetLib = ctx.applicationPlugin<Process::ApplicationPlugin>();
3,352 !
556
  con(presetLib, &Process::ApplicationPlugin::savePreset, this,
6,704 !
557
      [&](const Process::ProcessModel* proc) {
3,352 ✔
558
    SCORE_ASSERT(proc);
×
559
    this->m_presetModel->savePreset(*proc);
×
560
  });
×
561
  con(presetLib, &Process::ApplicationPlugin::showPresets, this,
6,704 !
562
      [this](const Process::ProcessModel* proc) {
3,364 ✔
563
    if(proc)
12 !
564
      showPresets(*proc);
12 ✔
565
  });
12 ✔
566
  con(presetLib, &Process::ApplicationPlugin::currentPresetChanged, this,
6,704 !
567
      [this](const Process::ProcessModel* proc) {
3,354 ✔
568
    if(proc && proc == m_presetModel->target())
2 !
569
    {
570
      m_presetModel->refreshCurrent();
2 ✔
571
      m_presets.showCurrentPreset();
2 ✔
572
    }
2 ✔
573
  });
2 ✔
574
  connect(
3,352 !
575
      &m_presets, &PresetTreeView::loadRequested, this,
3,352 ✔
576
      [this](const Process::Preset& preset) {
3,355 ✔
577
    auto proc = m_presetModel->target();
3 ✔
578
    if(!proc)
3 !
579
      return;
×
580
    // The one being heard is kept; another is loaded as it is
581
    if(m_audition.active() && Process::isCurrentPreset(*proc, preset))
3 !
582
      m_audition.commit();
2 ✔
583
    else
584
    {
585
      m_audition.commit();
1 ✔
586
      Process::loadPreset(*proc, preset, score::IDocument::documentContext(*proc));
1 ✔
587
    }
588
    m_presetModel->refreshCurrent();
3 ✔
589
  });
3 ✔
590
  connect(&m_presets, &PresetTreeView::cancelRequested, this, [this] {
3,353 !
591
    if(!m_audition.active())
1 !
592
      return;
×
593
    m_audition.rollback();
1 ✔
594
    m_presetModel->refreshCurrent();
1 ✔
595
    m_presets.showCurrentPreset();
1 ✔
596
  });
1 ✔
597
  // Doing anything else keeps what is heard: a click elsewhere takes the
598
  // focus before it acts
599
  con(*qApp, &QApplication::focusChanged, this, [this](QWidget*, QWidget* now) {
3,695 !
600
    if(m_audition.active() && now != &m_presets && !m_presets.isAncestorOf(now))
343 !
601
      m_audition.commit();
1 ✔
602
  });
343 ✔
603
}
3,352 ✔
604

605
void ProcessWidget::raisePanel()
12 ✔
606
{
607
  // Wherever the panel is docked
608
  if(auto view = qobject_cast<score::View*>(score::GUIAppContext().mainWindow))
12 !
609
    for(auto tabs : {view->leftTabs, view->rightTabs, view->bottomTabs})
48 ✔
610
      if(tabs)
36 !
611
        tabs->showTab(this);
48 ✔
612
}
12 ✔
613

614
void ProcessWidget::showPresets(const Process::ProcessModel& proc)
12 ✔
615
{
616
  m_audition.commit();
12 ✔
617
  raisePanel();
12 ✔
618

619
  // Its entry in the process tree, which a search there could hide
620
  auto idx = m_processModel->find(proc.concreteKey(), proc.effect());
12 !
621
  if(auto proxy = qobject_cast<QSortFilterProxyModel*>(m_tv.model()))
12 !
622
  {
623
    if(!proxy->mapFromSource(idx).isValid() && m_processFilter)
12 ✔
624
      m_processFilter->clear();
1 ✔
625
    idx = proxy->mapFromSource(idx);
12 ✔
626
  }
12 ✔
627
  m_choosingTarget = true;
12 ✔
628
  if(idx.isValid())
12 !
629
  {
630
    for(auto p = idx.parent(); p.isValid(); p = p.parent())
36 ✔
631
      m_tv.expand(p);
24 ✔
632
    m_tv.setCurrentIndex(idx);
12 ✔
633
    // Once the rows just opened are laid out
634
    QTimer::singleShot(0, &m_tv, [this] {
24 ✔
635
      m_tv.scrollTo(m_tv.currentIndex(), QAbstractItemView::PositionAtCenter);
12 ✔
636
    });
12 ✔
637
  }
12 ✔
638
  m_choosingTarget = false;
12 ✔
639

640
  // The presets are those of this very process: its script, its plug-in
641
  m_presetModel->setTarget(&proc);
12 ✔
642
  score::GUIAppContext().applicationPlugin<Process::ApplicationPlugin>().setPresetTarget(
24 ✔
643
      &proc);
12 ✔
644
  disconnect(m_targetGone);
12 ✔
645
  m_targetGone = connect(&proc, &QObject::destroyed, this, [this] { clearTarget(); });
21 ✔
646

647
  // It is the one selected while its presets load into it; deselected, the
648
  // library is browsed for any process again
649
  auto& sel = score::IDocument::documentContext(proc).selectionStack;
12 ✔
650
  score::SelectionDispatcher{sel}.select(proc);
12 ✔
651
  disconnect(m_targetDeselected);
12 ✔
652
  m_targetDeselected = connect(
12 ✔
653
      &sel, &score::SelectionStack::currentSelectionChanged, this,
12 ✔
654
      [this](const Selection&, const Selection&) {
16 ✔
655
    // Once what deselected it is done: undoing its creation deselects it on
656
    // the way, which must not put anything in the history meanwhile
657
    QTimer::singleShot(0, this, [this] {
8 ✔
658
      auto t = m_presetModel->target();
4 ✔
659
      if(!t)
4 ✔
660
        return;
3 ✔
661
      const auto now
662
          = score::IDocument::documentContext(*t).selectionStack.currentSelection();
1 ✔
663
      if(!ossia::any_of(now, [t](const auto& obj) { return obj.data() == t; }))
1 !
664
        clearTarget();
1 !
665
    });
4 ✔
666
  });
4 ✔
667
  const auto name = proc.metadata().getLabel().isEmpty() ? proc.metadata().getName()
12 !
668
                                                         : proc.metadata().getLabel();
×
669
  m_targetLabel.setText(tr("Presets: %1").arg(name));
12 !
670
  m_targetBar.show();
12 !
671
  m_presets.showCurrentPreset();
12 !
672
  m_presets.setFocus();
12 !
673
}
12 ✔
674

675
void ProcessWidget::clearTarget()
12 ✔
676
{
677
  m_audition.commit();
12 ✔
678
  disconnect(m_targetGone);
12 ✔
679
  disconnect(m_targetDeselected);
12 ✔
680
  score::GUIAppContext().applicationPlugin<Process::ApplicationPlugin>().setPresetTarget(
12 ✔
681
      nullptr);
682
  m_presetModel->setTarget(nullptr);
12 ✔
683
  m_targetBar.hide();
12 ✔
684
}
12 ✔
685

686
void ProcessWidget::setNotice(const QString& html, std::function<void()> onActivated)
2 ✔
687
{
688
  m_noticeAction = std::move(onActivated);
2 ✔
689
  m_notice.setText(html);
2 ✔
690
  m_notice.setVisible(!html.isEmpty());
2 ✔
691
}
2 ✔
692

693
ProcessWidget::~ProcessWidget() { }
6,702 ✔
694

695
QSet<QString> PresetLibraryHandler::acceptedFiles() const noexcept
16,797 ✔
696
{
697
  return {"scp"};
16,797 !
698
}
699

700
QSet<QString> PresetLibraryHandler::acceptedMimeTypes() const noexcept
6,704 ✔
701
{
702
  return {score::mime::processpreset()};
6,704 !
703
}
704

705
void PresetLibraryHandler::setup(
3,394 ✔
706
    ProcessesItemModel& model, const score::GUIApplicationContext& ctx)
707
{
708
  presetLib = &ctx.applicationPlugin<Process::ApplicationPlugin>();
3,394 ✔
709
  processes = &ctx.interfaces<Process::ProcessFactoryList>();
3,394 ✔
710
  // Called at each scan of the library, which reads every preset again
711
  presetLib->clearPresets();
3,394 ✔
712
}
3,394 ✔
713

714
void PresetLibraryHandler::addPath(std::string_view path)
4 ✔
715
{
716
  if(auto p = Process::Preset::fromFile(
8 !
717
         *processes, QString::fromUtf8(path.data(), qsizetype(path.length()))))
4 ✔
718
    presetLib->addPreset(std::move(*p));
4 !
719
}
4 ✔
720

721
bool PresetLibraryHandler::onDrop(
2 ✔
722
    const QMimeData& mime, int row, int column, const QDir& parent)
723
{
724
  if(!mime.hasFormat(score::mime::processpreset()))
2 !
725
    return false;
×
726

727
  const auto data = mime.data(score::mime::processpreset());
2 !
728
  auto json = readJson(data);
2 !
729
  if(!json.IsObject() || !json.HasMember("Name") || !json["Name"].IsString())
2 !
730
    return true;
×
731

732
  // A name, not a path out of the folder it is dropped in
733
  const auto basename = QFileInfo{JsonValue{json["Name"]}.toString()}.fileName();
2 !
734
  if(basename.isEmpty())
2 !
735
    return false;
×
736
  const QString filename
737
      = score::addUniqueSuffix(parent.absolutePath() + "/" + basename + ".scp");
2 !
738

739
  // Files next to the new preset file are stored relative to it, as for a
740
  // preset saved from the presets panel. A process is dragged from the
741
  // document being edited: its paths are relative to that one.
742
  if(processes)
2 !
743
  {
744
    if(auto p = Process::Preset::fromJson(*processes, data))
4 !
745
    {
746
      auto* doc = score::GUIAppContext().docManager.currentDocument();
2 !
747
      const auto roots = doc ? score::pathRoots(doc->context()) : score::pathRoots();
2 !
748
      if(auto stored = score::relativizePresetPaths(p->data, filename, roots);
4 ✔
749
         stored != p->data)
3 ✔
750
      {
751
        p->data = std::move(stored);
1 ✔
752
        return writePresetData(filename, p->toJson());
1 !
753
      }
754
    }
2 ✔
755
  }
1 ✔
756
  return writePresetData(filename, data);
1 !
757
}
2 ✔
758

759

760
void ProcessWidget::editDescription(
30 ✔
761
    const QString& text, std::function<void(const QString&)> save)
762
{
763
  // What was typed for the previous item is saved before showing this one.
764
  commitDescription();
30 ✔
765
  m_saveDescription = std::move(save);
30 ✔
766
  m_descriptionShown = text;
30 ✔
767
  m_descriptionEdit->setPlainText(text);
30 ✔
768
  m_descriptionEdit->setVisible(bool(m_saveDescription));
30 ✔
769
  // Only the description that can be edited is shown, not the process's
770
  m_info->showDescription(!m_saveDescription);
30 ✔
771
}
30 ✔
772

773
void ProcessWidget::commitDescription()
30 ✔
774
{
775
  if(!m_saveDescription)
30 !
776
    return;
30 ✔
NEW
777
  const QString text = m_descriptionEdit->toPlainText().trimmed();
×
NEW
778
  if(text == m_descriptionShown)
×
NEW
779
    return;
×
NEW
780
  m_descriptionShown = text;
×
NEW
781
  m_saveDescription(text);
×
782
}
30 ✔
783
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc