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

ossia / score / 40987115292

30 Oct 2026 01:27AM UTC coverage: 51.229% (+2.1%) from 49.178%
40987115292

push

github

jcelerier
avendish: main

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

179452 of 350293 relevant lines covered (51.23%)

175809.48 hits per line

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

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

3
#include <Process/Process.hpp>
4
#include <Process/ProcessList.hpp>
5
#include <Process/ProcessMimeSerialization.hpp>
6

7
#include <Library/LibraryInterface.hpp>
8
#include <Library/LibrarySettings.hpp>
9

10
#include <score/application/GUIApplicationContext.hpp>
11
#include <score/tools/RecursiveWatch.hpp>
12

13
#include <QElapsedTimer>
14
#include <QIcon>
15
#include <QMimeData>
16
#include <QPointer>
17
#include <QTimer>
18

19
namespace Library
20
{
21
namespace
22
{
23
struct LibraryNameSort
24
{
25
  bool operator()(const QString& lhs, const Library::ProcessData& rhs) const noexcept
26
  {
27
    return QString::compare(lhs, rhs.prettyName, Qt::CaseInsensitive) < 0;
28
  }
29
  bool operator()(const Library::ProcessData& lhs, const QString& rhs) const noexcept
4,664,939 ✔
30
  {
31
    return QString::compare(lhs.prettyName, rhs, Qt::CaseInsensitive) < 0;
4,664,939 ✔
32
  }
33
  bool operator()(
120,661 ✔
34
      const Library::ProcessData& lhs, const Library::ProcessData& rhs) const noexcept
35
  {
36
    return QString::compare(lhs.prettyName, rhs.prettyName, Qt::CaseInsensitive) < 0;
120,661 ✔
37
  }
38
};
39

40
ProcessNode* findChildByName(ProcessNode& parent, const QString& name) noexcept
7,881 ✔
41
{
42
  for(auto& child : parent)
14,167,198 ✔
43
    if(QString::compare(child.prettyName, name, Qt::CaseInsensitive) == 0)
14,159,464 ✔
44
      return &child;
147 ✔
45
  return nullptr;
7,734 ✔
46
}
7,881 ✔
47

48
// Recursively turn staged data into a detached node subtree, children sorted.
49
ProcessNode toNode(StagedNode&& staged) noexcept
1,499,199 ✔
50
{
51
  ProcessNode n{std::move(staged.data), nullptr};
1,499,199 ✔
52
  for(auto& c : staged.children)
2,886,631 ✔
53
  {
54
    auto it = std::lower_bound(n.begin(), n.end(), c.data.prettyName, LibraryNameSort{});
1,387,432 ✔
55
    n.emplace(it, toNode(std::move(c)));
1,387,432 ✔
56
  }
57
  return n;
1,499,199 ✔
58
}
1,499,199 ✔
59
}
60

61
ProcessesItemModel::ProcessesItemModel(
8,775 ✔
62
    const score::GUIApplicationContext& ctx, QObject* parent)
63
    : TreeNodeBasedItemModel<ProcessNode>{parent}
2,925 ✔
64
    , context{ctx}
2,925 ✔
65
{
5,850 ✔
66
  auto& procs = ctx.interfaces<Process::ProcessFactoryList>();
2,925 ✔
67
  procs.added.connect<&ProcessesItemModel::on_newPlugin>(*this);
2,925 ✔
68

69
  auto& lib = context.settings<Library::Settings::Model>();
2,925 ✔
70
  con(lib, &Library::Settings::Model::rescanLibrary, this, &ProcessesItemModel::rescan);
2,925 ✔
71
  rescan();
2,925 ✔
72
}
×
73

74
ProcessNode& ProcessesItemModel::addCategory(const QString& c)
193,182 ✔
75
{
76
  auto split = c.split("/");
193,182 ✔
77
  auto icon = Process::getCategoryIcon(split[0]);
193,182 ✔
78

79
  auto* node = &m_root;
193,182 ✔
80
  for(QString& cat : split)
564,911 ✔
81
  {
82
    // First try to find the existing node.
83
    bool found = false;
371,729 ✔
84
    for(auto& n : *node)
2,300,622 ✔
85
    {
86
      if(n.prettyName == cat)
2,095,732 ✔
87
      {
88
        node = &n;
166,839 ✔
89
        found = true;
166,839 ✔
90
        icon = {}; // Icon only the first time
166,839 ✔
91
        break;
166,839 ✔
92
      }
93
    }
94
    if(found)
371,729 ✔
95
      continue;
166,839 ✔
96

97
    // Otherwise add it
98

99
    auto& new_node = ossia::emplace_sorted(
204,890 ✔
100
        *node, cat, LibraryNameSort{}, ProcessData{{{}, cat, {}}, icon}, node);
204,890 ✔
101
    node = &new_node;
204,890 ✔
102
    icon = {}; // Icon only the first time
204,890 ✔
103
  }
104

105
  return *node;
193,182 ✔
106
}
193,182 ✔
107

108
void ProcessesItemModel::rescan()
2,927 ✔
109
{
110
  auto& procs = context.interfaces<Process::ProcessFactoryList>();
2,927 ✔
111

112
  ossia::flat_map<QString, ossia::flat_map<QString, Process::ProcessModelFactory*>>
113
      sorted;
2,927 ✔
114
  sorted.reserve(100);
2,927 ✔
115
  for(Process::ProcessModelFactory& proc : procs)
1,249,829 ✔
116
  {
117
    static_assert((1LL << 63) == (1ULL << 63));
118
    static_assert(sizeof(Process::ProcessFlags::Deprecated) == sizeof(1ULL));
119
    static_assert(sizeof(1ULL) == sizeof(uint64_t));
120
    if(!(proc.flags() & Process::ProcessFlags::Deprecated))
1,246,902 ✔
121
      sorted[proc.category()][proc.prettyName()] = &proc;
1,202,997 ✔
122
  }
123

124
  // Entries queued by a scan that predates this rescan target nodes that are
125
  // about to be destroyed: drop them.
126
  m_generation++;
2,927 ✔
127
  m_pending.clear();
2,927 ✔
128
  m_anchors.clear();
2,927 ✔
129

130
  auto& lib_setup = context.interfaces<Library::LibraryInterfaceList>();
2,927 ✔
131

132
  beginResetModel();
2,927 ✔
133
  m_inReset = true;
2,927 ✔
134
  m_root = ProcessNode{};
2,927 ✔
135
  for(auto& e : sorted)
196,109 ✔
136
  {
137
    auto& cat = addCategory(e.first);
193,182 ✔
138

139
    for(const auto& [_, p] : e.second)
1,396,179 ✔
140
    {
141
      auto& node = cat.emplace_back(
2,405,994 ✔
142
          ProcessData{{p->concreteKey(), p->prettyName(), {}}, QIcon{}}, &cat);
3,608,991 ✔
143
      m_anchors[p->concreteKey()] = &node;
2,405,994 ✔
144
    }
145
  }
146

147
  // Handler setup runs while the model is still resetting: bulk populators
148
  // (plugin databases) insert whole subtrees here, and doing it inside the
149
  // reset envelope is what makes those insertions legal without signals.
150
  for(auto& lib : lib_setup)
87,810 ✔
151
    lib.setup(*this, context);
84,883 ✔
152

153
  m_inReset = false;
2,927 ✔
154
  endResetModel();
2,927 ✔
155

156
  auto& libsettings = context.settings<Library::Settings::Model>();
2,927 ✔
157

158
  auto libpath = libsettings.getPackagesPath();
2,927 ✔
159

160
  auto& w = m_watch;
2,927 ✔
161
  w.reset();
2,927 ✔
162
  w.setWatchedFolder(libpath.toStdString());
2,927 ✔
163

164
  for(auto& lib : lib_setup)
87,810 ✔
165
  {
166
    for(const QString& ext : lib.acceptedFiles())
374,656 ✔
167
    {
168
      score::RecursiveWatch::AsyncCallbacks cbs;
289,773 ✔
169
      cbs.filter = [&lib, model = QPointer{this},
292,225 ✔
170
                    gen = m_generation](std::string_view path) -> std::function<void()> {
289,773 ✔
171
        // Worker thread: pure scan. The commit closure only carries data and
172
        // publishes it on the GUI thread; a rescan in between just drops it.
173
        if(auto entry = lib.scanPath(path))
4,879 ✔
174
        {
175
          return [model, gen, e = std::move(*entry)]() mutable {
4,854 ✔
176
            if(model)
2,427 ✔
177
              model->publish(std::move(e), gen);
2,427 ✔
178
          };
2,427 ✔
179
        }
180
        // Handlers that commit outside this model (e.g. the QML module panel,
181
        // the preset library).
182
        return lib.asyncAddPath(path);
25 ✔
183
      };
2,452 ✔
184
      w.registerWatch(ext.toStdString(), std::move(cbs));
289,773 ✔
185
    }
289,773 ✔
186
  }
187

188
  if(!QDir{libpath}.exists())
2,927 ✔
189
    return;
7 ✔
190

191
  w.scanAsync(this);
2,920 ✔
192
}
2,927 ✔
193

194
void ProcessesItemModel::on_newPlugin(const score::InterfaceBase& base)
×
195
{
196
  auto& fact = static_cast<const Process::ProcessModelFactory&>(base);
×
197
  if(fact.flags() & Process::ProcessFlags::Deprecated)
×
198
    return;
×
199

200
  auto it = ossia::find_if(m_root, [&, cat = fact.category()](ProcessData& container) {
×
201
    return container.prettyName == cat;
×
202
  });
203
  ProcessNode* cat{};
×
204
  if(it != m_root.end())
×
205
  {
206
    cat = &*it;
×
207
  }
×
208
  else
209
  {
210
    const QString name = fact.category();
×
211
    auto pos = std::lower_bound(m_root.begin(), m_root.end(), name, LibraryNameSort{});
×
212
    const int row = std::distance(m_root.begin(), pos);
×
213
    beginInsertRows(QModelIndex{}, row, row);
×
214
    cat = &m_root.emplace(
×
215
        pos, ProcessData{{{}, name, {}}, Process::getCategoryIcon(name)}, &m_root);
×
216
    endInsertRows();
×
217
  }
×
218

219
  const QString name = fact.prettyName();
×
220
  auto pos = std::lower_bound(cat->begin(), cat->end(), name, LibraryNameSort{});
×
221
  const int row = std::distance(cat->begin(), pos);
×
222
  beginInsertRows(indexFromNode(*cat), row, row);
×
223
  auto& node
×
224
      = cat->emplace(pos, ProcessData{{fact.concreteKey(), name, {}}, QIcon{}}, cat);
×
225
  endInsertRows();
×
226
  m_anchors[fact.concreteKey()] = &node;
×
227
}
×
228

229
QModelIndex ProcessesItemModel::find(const Process::ProcessModelFactory::ConcreteKey& k)
15 ✔
230
{
231
  if(auto it = m_anchors.find(k); it != m_anchors.end())
15 ✔
232
    return indexFromNode(*it->second);
15 ✔
233
  return QModelIndex{};
×
234
}
15 ✔
235

236
QModelIndex ProcessesItemModel::indexFromNode(ProcessNode& n)
7,900 ✔
237
{
238
  if(&n == &m_root)
7,900 ✔
239
    return QModelIndex{};
×
240
  auto parent = n.parent();
7,900 ✔
241
  SCORE_ASSERT(parent);
7,900 ✔
242
  const int row = parent->indexOfChild(&n);
7,900 ✔
243
  SCORE_ASSERT(row != -1);
7,900 ✔
244
  return createIndex(row, 0, &n);
7,900 ✔
245
}
7,900 ✔
246

247
void ProcessesItemModel::publish(ProcessEntry&& entry)
36,162 ✔
248
{
249
  publish(std::move(entry), m_generation);
36,162 ✔
250
}
36,162 ✔
251

252
void ProcessesItemModel::publish(ProcessEntry&& entry, uint64_t generation)
38,591 ✔
253
{
254
  if(generation != m_generation)
38,591 ✔
255
    return;
1 ✔
256
  m_pending.push_back(PendingEntry{std::move(entry), generation});
38,590 ✔
257
  scheduleFlush();
38,590 ✔
258
}
38,591 ✔
259

260
void ProcessesItemModel::scheduleFlush()
38,590 ✔
261
{
262
  if(m_flushQueued)
38,590 ✔
263
    return;
36,014 ✔
264
  m_flushQueued = true;
2,576 ✔
265
  // 0-timer: all the publishes of one RecursiveWatch batch (they arrive in a
266
  // single event-loop turn) coalesce into one flush.
267
  QTimer::singleShot(0, this, [this] { flushPending(); });
5,145 ✔
268
}
38,590 ✔
269

270
void ProcessesItemModel::flushPending()
2,578 ✔
271
{
272
  m_flushQueued = false;
2,578 ✔
273
  if(m_pending.empty())
2,578 ✔
274
    return;
3 ✔
275

276
  auto pending = std::move(m_pending);
2,575 ✔
277
  m_pending.clear();
2,575 ✔
278

279
  // Group runs of consecutive entries sharing anchor + category chain: the
280
  // filesystem scan delivers directory contents consecutively, so this makes
281
  // one insert per directory, not one per file.
282
  std::size_t i = 0;
2,575 ✔
283
  while(i < pending.size())
10,456 ✔
284
  {
285
    if(pending[i].generation != m_generation)
7,881 ✔
286
    {
287
      i++;
×
288
      continue;
×
289
    }
290
    std::size_t j = i + 1;
7,881 ✔
291
    while(
46,470 ✔
292
        j < pending.size() && pending[j].generation == m_generation
38,589 ✔
293
        && pending[j].entry.rootKey == pending[i].entry.rootKey
36,014 ✔
294
        && pending[j].entry.categoryPath == pending[i].entry.categoryPath)
36,014 ✔
295
      j++;
30,708 ✔
296

297
    publishRun(&pending[i], j - i);
7,881 ✔
298
    i = j;
7,881 ✔
299
  }
300
}
2,578 ✔
301

302
void ProcessesItemModel::publishRun(PendingEntry* entries, std::size_t count)
7,881 ✔
303
{
304
  SCORE_ASSERT(count > 0);
7,881 ✔
305
  auto anchor_it = m_anchors.find(entries[0].entry.rootKey);
7,881 ✔
306
  if(anchor_it == m_anchors.end())
7,881 ✔
307
    return; // Not a registered process: nothing to attach to.
1 ✔
308

309
  // Walk the existing part of the category chain.
310
  ProcessNode* parent = anchor_it->second;
7,880 ✔
311
  const QStringList& path = entries[0].entry.categoryPath;
7,880 ✔
312
  int existing = 0;
7,880 ✔
313
  for(; existing < path.size(); existing++)
8,027 ✔
314
  {
315
    auto child = findChildByName(*parent, path[existing]);
7,881 ✔
316
    if(!child)
7,881 ✔
317
      break;
7,734 ✔
318
    parent = child;
147 ✔
319
  }
147 ✔
320

321
  if(existing < path.size())
7,880 ✔
322
  {
323
    // The rest of the chain does not exist: build it detached, hang every
324
    // entry under the leaf, splice the whole thing with a single insert.
325
    ProcessNode staged{ProcessData{{{}, path[existing], {}}, {}}, nullptr};
7,734 ✔
326
    ProcessNode* leaf = &staged;
7,734 ✔
327
    for(int c = existing + 1; c < path.size(); c++)
7,737 ✔
328
      leaf = &leaf->emplace_back(ProcessData{{{}, path[c], {}}, {}}, leaf);
3 ✔
329

330
    for(std::size_t e = 0; e < count; e++)
38,516 ✔
331
    {
332
      auto node = toNode(std::move(entries[e].entry.node));
30,782 ✔
333
      auto it = std::lower_bound(
30,782 ✔
334
          leaf->begin(), leaf->end(), node.prettyName, LibraryNameSort{});
30,782 ✔
335
      leaf->emplace(it, std::move(node));
30,782 ✔
336
    }
30,782 ✔
337

338
    auto pos = std::lower_bound(
7,734 ✔
339
        parent->begin(), parent->end(), staged.prettyName, LibraryNameSort{});
7,734 ✔
340
    const int row = std::distance(parent->begin(), pos);
7,734 ✔
341
    beginInsertRows(indexFromNode(*parent), row, row);
7,734 ✔
342
    parent->emplace(pos, std::move(staged));
7,734 ✔
343
    endInsertRows();
7,734 ✔
344
    return;
345
  }
7,734 ✔
346

347
  // The full chain exists: insert the entries as contiguous sorted sub-runs.
348
  std::vector<ProcessNode> nodes;
146 ✔
349
  nodes.reserve(count);
146 ✔
350
  for(std::size_t e = 0; e < count; e++)
7,952 ✔
351
    nodes.push_back(toNode(std::move(entries[e].entry.node)));
7,806 ✔
352
  std::sort(nodes.begin(), nodes.end(), LibraryNameSort{});
146 ✔
353

354
  const QModelIndex parentIndex = indexFromNode(*parent);
146 ✔
355
  auto it = parent->begin();
146 ✔
356
  int row = 0;
146 ✔
357
  std::size_t n = 0;
146 ✔
358
  while(n < nodes.size())
775 ✔
359
  {
360
    // Advance to the sorted insertion point of nodes[n].
361
    while(it != parent->end() && LibraryNameSort{}(*it, nodes[n]))
61,065 ✔
362
    {
363
      ++it;
60,436 ✔
364
      ++row;
60,436 ✔
365
    }
366
    // Everything that sorts before (or equal to) the sibling at `it` forms
367
    // one contiguous range.
368
    std::size_t last = n;
629 ✔
369
    while(last < nodes.size()
9,064 ✔
370
          && (it == parent->end() || !LibraryNameSort{}(*it, nodes[last])))
8,435 ✔
371
      last++;
7,806 ✔
372

373
    beginInsertRows(parentIndex, row, row + int(last - n) - 1);
629 ✔
374
    for(; n < last; n++)
8,435 ✔
375
    {
376
      parent->insert(it, std::move(nodes[n]));
7,806 ✔
377
      ++row;
7,806 ✔
378
    }
7,806 ✔
379
    endInsertRows();
629 ✔
380
  }
381
}
7,881 ✔
382

383
void ProcessesItemModel::replaceChildren(
14,641 ✔
384
    const Process::ProcessModelFactory::ConcreteKey& key,
385
    std::vector<StagedNode> children)
386
{
387
  auto anchor_it = m_anchors.find(key);
14,641 ✔
388
  if(anchor_it == m_anchors.end())
14,641 ✔
389
    return;
1 ✔
390
  ProcessNode& parent = *anchor_it->second;
14,640 ✔
391

392
  if(m_inReset)
14,640 ✔
393
  {
394
    // rescan() already wraps the whole model in a reset: mutate directly.
395
    parent.resize(0);
14,635 ✔
396
    for(auto& c : children)
87,810 ✔
397
      parent.push_back(toNode(std::move(c)));
73,175 ✔
398
    return;
14,635 ✔
399
  }
400

401
  const QModelIndex parentIndex = indexFromNode(parent);
5 ✔
402
  if(const int n = parent.childCount(); n > 0)
5 ✔
403
  {
404
    beginRemoveRows(parentIndex, 0, n - 1);
2 ✔
405
    parent.resize(0);
2 ✔
406
    endRemoveRows();
2 ✔
407
  }
2 ✔
408

409
  if(!children.empty())
5 ✔
410
  {
411
    beginInsertRows(parentIndex, 0, int(children.size()) - 1);
2 ✔
412
    for(auto& c : children)
6 ✔
413
      parent.push_back(toNode(std::move(c)));
4 ✔
414
    endInsertRows();
2 ✔
415
  }
2 ✔
416
}
14,641 ✔
417

418
void ProcessesItemModel::clearAnchorKey(
14,635 ✔
419
    const Process::ProcessModelFactory::ConcreteKey& key)
420
{
421
  auto anchor_it = m_anchors.find(key);
14,635 ✔
422
  if(anchor_it == m_anchors.end())
14,635 ✔
423
    return;
×
424
  anchor_it->second->key = {};
14,635 ✔
425
  if(!m_inReset)
14,635 ✔
426
  {
427
    const auto idx = indexFromNode(*anchor_it->second);
×
428
    dataChanged(idx, idx);
×
429
  }
×
430
}
14,635 ✔
431

432
ProcessNode& ProcessesItemModel::rootNode()
×
433
{
434
  return m_root;
×
435
}
436

437
const ProcessNode& ProcessesItemModel::rootNode() const
7,075,612 ✔
438
{
439
  return m_root;
7,075,612 ✔
440
}
441

442
int ProcessesItemModel::columnCount(const QModelIndex& parent) const
186,791,366 ✔
443
{
444
  return 1;
186,791,366 ✔
445
}
446

447
QVariant ProcessesItemModel::data(const QModelIndex& index, int role) const
185,495,280 ✔
448
{
449
  const auto& node = nodeFromModelIndex(index);
185,495,280 ✔
450
  switch(role)
185,495,280 ✔
451
  {
452
    case Qt::DisplayRole:
453
      return node.prettyName;
185,434,016 ✔
454
    case Qt::DecorationRole:
455
      return node.icon;
8,752 ✔
456
  }
457
  return QVariant{};
52,512 ✔
458
}
185,495,280 ✔
459

460
QVariant
461
ProcessesItemModel::headerData(int section, Qt::Orientation orientation, int role) const
×
462
{
463
  return {};
×
464
}
465

466
Qt::ItemFlags ProcessesItemModel::flags(const QModelIndex& index) const
262,560 ✔
467
{
468
  if(!index.isValid())
262,560 ✔
469
    return Qt::NoItemFlags;
×
470

471
  Qt::ItemFlags f;
262,560 ✔
472

473
  const auto& node = nodeFromModelIndex(index);
262,560 ✔
474
  if(node.key == Process::ProcessModelFactory::ConcreteKey{})
262,560 ✔
475
    f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
262,560 ✔
476
  else
477
    f = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
×
478

479
  return f;
262,560 ✔
480
}
262,560 ✔
481

482
QStringList ProcessesItemModel::mimeTypes() const
×
483
{
484
  return {score::mime::processdata()};
×
485
}
×
486

487
QMimeData* ProcessesItemModel::mimeData(const QModelIndexList& indexes) const
×
488
{
489
  // Only 1 index for now
490
  QMimeData* mimeData = new QMimeData;
×
491

492
  const auto& index = indexes.first();
×
493
  const auto& node = nodeFromModelIndex(index);
×
494
  MimeReader<Process::ProcessData>{*mimeData}.serialize(node);
×
495
  return mimeData;
×
496
}
×
497

498
Qt::DropActions ProcessesItemModel::supportedDragActions() const
×
499
{
500
  return Qt::CopyAction;
×
501
}
502

503
}
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