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

ossia / score / 41391215711

02 Nov 2026 04:56AM UTC coverage: 51.66% (-0.6%) from 52.268%
41391215711

push

github

jcelerier
Port probes: what goes through a port or a cable, on hover

Resting the cursor on a port or a cable opens a probe: levels and a trace
for audio, the latest decoded messages for MIDI, the latest values with a
trail for numbers, colour, position, orientation, gain and angle views for
values with a unit, a thumbnail for textures. On a cable it shows what the
source produces, near its end what the process there receives.

The data comes from Execution::PortTap: while a probe is open, a node of
the execution graph gets the port's edges and copies what arrives into a
monitor, without allocation or lock for audio; TapTicker gives taps the
token the graph needs to run them. Probes only tap with execution updates
enabled, refresh at the UI event rate, and open only when previews are
enabled. Nothing touches the document.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPgufUMRpkKzaYvUs9iHW

96 of 644 new or added lines in 9 files covered. (14.91%)

3455 existing lines in 31 files now uncovered.

187161 of 362292 relevant lines covered (51.66%)

305640.16 hits per line

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

81.09
/src/plugins/score-lib-process/Process/Dataflow/NodeItem.cpp
1
#include <Process/Commands/Properties.hpp>
2
#include <Process/Dataflow/CableItem.hpp>
3
#include <Process/Dataflow/NodeItem.hpp>
4
#include <Process/Dataflow/PortFactory.hpp>
5
#include <Process/Dataflow/PortItem.hpp>
6
#include <Process/Dataflow/PortVisibility.hpp>
7
#include <Process/DocumentPlugin.hpp>
8
#include <Process/Focus/FocusDispatcher.hpp>
9
#include <Process/Process.hpp>
10
#include <Process/ProcessFactory.hpp>
11
#include <Process/ProcessList.hpp>
12
#include <Process/Style/Pixmaps.hpp>
13
#include <Process/Style/ScenarioStyle.hpp>
14

15
#include <Control/DefaultEffectItem.hpp>
16
#include <Effect/EffectLayer.hpp>
17
#include <Effect/EffectPainting.hpp>
18

19
#include <Process/ApplicationPlugin.hpp>
20
#include <score/application/GUIApplicationContext.hpp>
21
#include <score/command/Dispatchers/CommandDispatcher.hpp>
22
#include <score/command/Dispatchers/RuntimeDispatcher.hpp>
23
#include <score/document/DocumentContext.hpp>
24
#include <score/graphics/GraphicWidgets.hpp>
25
#include <score/graphics/GraphicsLayout.hpp>
26
#include <score/graphics/TextItem.hpp>
27
#include <score/model/Skin.hpp>
28
#include <score/selection/Selection.hpp>
29
#include <score/selection/SelectionDispatcher.hpp>
30
#include <score/selection/SelectionStack.hpp>
31
#include <score/tools/Bind.hpp>
32

33
#include <ossia/detail/algorithms.hpp>
34
#include <ossia/detail/ssize.hpp>
35

36
#include <ossia-qt/invoke.hpp>
37

38
#include <QCursor>
39
#include <QGraphicsScene>
40
#include <QGraphicsSceneMouseEvent>
41
#include <QKeyEvent>
42
#include <QMenu>
43
#include <QPainter>
44
#include <QTimer>
45

46
#include <wobjectimpl.h>
47
W_OBJECT_IMPL(Process::NodeItem)
2,810 ✔
48
namespace Process
49
{
50

51
static const constexpr qreal TitleHeight = 20.;
52
//static const constexpr qreal TitleX0 = 15;
53
static const constexpr qreal TitleY0 = -TitleHeight + 2.;
54
static const constexpr qreal FoldX0 = 2;
55
static const constexpr qreal FoldY0 = TitleY0 + 1;
56
static const constexpr qreal UiX0 = 16;
57
static const constexpr qreal UiY0 = TitleY0 + 1.;
58
static const constexpr qreal TitleWithUiX0 = 27;
59
static const constexpr qreal SpacingIcon = 3;
60

61
static const constexpr qreal FooterHeight = 0.;
62
static const constexpr qreal Corner = 2.;
63
static const constexpr qreal PortSpacing = 10.;
64
static const constexpr qreal InletX0 = -10.;
65
static const constexpr qreal InletY0 = 0;
66
static const constexpr qreal OutletX0 = -2.;
67
static const constexpr qreal OutletY0 = InletY0; // Add to height
68
static const constexpr qreal TopButtonX0 = -12.;
69
static const constexpr qreal TopButtonY0 = TitleY0 + 2.;
70
static const constexpr qreal LeftSideWidth = 10.;
71
static const constexpr qreal RightSideWidth = LeftSideWidth;
72

73
NodeItem::NodeItem(
1,646 ✔
74
    const Process::ProcessModel& process, const Process::Context& ctx, TimeVal parentDur,
75
    QGraphicsItem* parent)
76
    : QGraphicsItem{parent}
1,646 ✔
77
    , m_model{process}
78
    , m_context{ctx}
79
    , m_dispatcher{ctx.commandStack}
80
    , m_parentDuration{parentDur}
81
{
1,646 ✔
82
  setObjectName("NodeItem");
1,646 ✔
83
  setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
1,646 ✔
84
  setAcceptHoverEvents(true);
1,646 ✔
85
  setAcceptDrops(true);
1,646 ✔
86
  setFlag(ItemIsFocusable, true);
1,646 ✔
87
  setFlag(ItemClipsChildrenToShape, true);
1,646 ✔
88
  const auto& pf
1,646 ✔
89
      = ctx.app.interfaces<Process::ProcessFactoryList>().get(process.concreteKey());
1,646 ✔
90
  setData(0xF1, pf->descriptor(process).documentationLink);
1,646 ✔
91

92
  if(process.flags() & Process::ProcessFlags::FullyCustomItem)
1,646 ✔
93
  {
94
    createWithoutDecorations();
4 ✔
95
  }
4 ✔
96
  else
97
  {
98
    createWithDecorations();
1,642 ✔
99
  }
100

101
  initConnections();
1,646 ✔
102
}
×
103

104
void NodeItem::createWithDecorations()
1,649 ✔
105
{
106
  auto& process = m_model;
1,649 ✔
107
  auto& ctx = m_context;
1,649 ✔
108

109
  m_folded = process.folded();
1,649 ✔
110

111
  // Title
112
  const auto& pixmaps = Process::Pixmaps::instance();
1,649 ✔
113
  m_fold
3,298 ✔
114
      = new score::QGraphicsPixmapToggle{pixmaps.unroll_small, pixmaps.roll_small, this};
1,649 ✔
115
  m_fold->setState(!m_folded);
1,649 ✔
116

117
  m_scriptButton = Process::makeScriptButton(
1,649 ✔
118
      const_cast<Process::ProcessModel&>(process), ctx, this, this);
1,649 ✔
119
  m_uiButton = Process::makeExternalUIButton(
1,649 ✔
120
      const_cast<Process::ProcessModel&>(process), ctx, this, this);
1,649 ✔
121
  m_presetButton = Process::makePresetButton(process, ctx, this, this);
1,649 ✔
122

123
  auto& skin = score::Skin::instance();
1,649 ✔
124
  m_label = new score::SimpleTextItem{skin.Light.main, this};
1,649 ✔
125
  m_label->setFont(skin.Bold10Pt);
1,649 ✔
126

127
  updateLabel();
1,649 ✔
128

129
  con(process.metadata(), &score::ModelMetadata::NameChanged, this,
3,298 ✔
130
      &NodeItem::updateLabel);
1,649 ✔
131
  con(process.metadata(), &score::ModelMetadata::LabelChanged, this,
3,298 ✔
132
      &NodeItem::updateLabel);
1,649 ✔
133

134
  con(process, &Process::ProcessModel::loopsChanged, this, [&] { updateZoomRatio(); });
1,650 ✔
135
  con(process, &Process::ProcessModel::loopDurationChanged, this,
3,298 ✔
136
      [&] { updateZoomRatio(); });
1,650 ✔
137
  con(process, &Process::ProcessModel::startOffsetChanged, this,
3,298 ✔
138
      [&] { updateZoomRatio(); });
1,649 ✔
139

140
  // Selection
141
  con(process.selection, &Selectable::changed, this, &NodeItem::setSelected);
1,649 ✔
142
  m_selected = process.selection.get();
1,649 ✔
143

144
  if(!m_folded)
1,649 ✔
145
    createContentItem();
1,536 ✔
146
  else
147
    createFoldedItem();
113 ✔
148

149
  // This is because if it's a DefaultEffectItem the ports are created asynchronously
150
  // Thus they are added to the vertical lists instead of the actual items
151
  ossia::qt::run_async(this, [this] {
2,879 ✔
152
    resetInlets();
1,230 ✔
153
    resetOutlets();
1,230 ✔
154

155
    resizeAsync();
1,230 ✔
156
  });
1,230 ✔
157
  connect(&process, &Process::ProcessModel::controlAdded, this, &NodeItem::resetInlets);
1,649 ✔
158
  connect(
1,649 ✔
159
      &process, &Process::ProcessModel::controlRemoved, this, &NodeItem::resetInlets);
1,649 ✔
160
  connect(
1,649 ✔
161
      &process, &Process::ProcessModel::controlOutletAdded, this,
1,649 ✔
162
      &NodeItem::resetOutlets);
163
  connect(
1,649 ✔
164
      &process, &Process::ProcessModel::controlOutletRemoved, this,
1,649 ✔
165
      &NodeItem::resetOutlets);
166
  connect(&process, &Process::ProcessModel::inletsChanged, this, &NodeItem::resetInlets);
1,649 ✔
167
  connect(
1,649 ✔
168
      &process, &Process::ProcessModel::outletsChanged, this, &NodeItem::resetOutlets);
1,649 ✔
169

170
  // The button only states the intent; the model is what decides, so that undo,
171
  // reload and a preset dropped on the node all end up drawing the same thing.
172
  connect(m_fold, &score::QGraphicsPixmapToggle::toggled, this, [this](bool b) {
1,649 ✔
173
    CommandDispatcher<> disp{m_context.commandStack};
×
174
    disp.submit<Process::SetNodeFoldMode>(
×
175
        m_model, b ? Process::FoldMode::Unfolded : Process::FoldMode::Folded);
×
176
  });
×
177

178
  con(process, &Process::ProcessModel::foldModeChanged, this,
3,298 ✔
179
      [this] { applyFold(m_model.folded()); });
1,651 ✔
180

181
  resizeAsync();
1,649 ✔
182
  updateTooltip();
1,649 ✔
183
}
1,649 ✔
184

185
void NodeItem::createWithoutDecorations()
4 ✔
186
{
187
  auto& process = m_model;
4 ✔
188

189
  con(process.selection, &Selectable::changed, this, &NodeItem::setSelected);
4 ✔
190
  m_selected = process.selection.get();
4 ✔
191

192
  createContentItem();
4 ✔
193

194
  // The item shows the inlets it wants; the outlets go on the right side
195
  ossia::qt::run_async(this, [this] {
7 ✔
196
    resetOutlets();
3 ✔
197
    resizeAsync();
3 ✔
198
  });
3 ✔
199
  connect(
4 ✔
200
      &process, &Process::ProcessModel::controlOutletAdded, this,
4 ✔
201
      &NodeItem::resetOutlets);
202
  connect(
4 ✔
203
      &process, &Process::ProcessModel::controlOutletRemoved, this,
4 ✔
204
      &NodeItem::resetOutlets);
205
  connect(
4 ✔
206
      &process, &Process::ProcessModel::outletsChanged, this, &NodeItem::resetOutlets);
4 ✔
207

208
  updateTooltip();
4 ✔
209
}
4 ✔
210

211
void NodeItem::updateTooltip()
1,653 ✔
212
{
213
  if(!m_fx || m_fx->toolTip().isEmpty())
1,653 ✔
214
  {
215
    auto& p = this->m_context.app.interfaces<Process::ProcessFactoryList>();
1,607 ✔
216
    const auto& desc = p.get(m_model.concreteKey())->descriptor(m_model);
1,607 ✔
217

218
    bool has_name = !desc.prettyName.isEmpty();
1,607 ✔
219
    bool has_desc = !desc.description.isEmpty();
1,607 ✔
220
    bool has_link = !desc.documentationLink.isEmpty();
1,607 ✔
221
    QString str;
1,607 ✔
222
    if(has_name)
1,607 ✔
223
    {
224
      str += desc.prettyName;
1,607 ✔
225
      str += "\n";
1,607 ✔
226
    }
1,607 ✔
227
    if(has_desc)
1,607 ✔
228
    {
229
      str += desc.description;
1,607 ✔
230
      str += "\n";
1,607 ✔
231
    }
1,607 ✔
232
    if(has_link)
1,607 ✔
233
    {
234
      str += QStringLiteral("Press F1 to open the docs:\n%1")
3,118 ✔
235
                 .arg(desc.documentationLink.toString());
1,559 ✔
236
    }
1,559 ✔
237
    else
238
    {
239
      str += QStringLiteral("Press F1 to open the docs!");
48 ✔
240
    }
241

242
    this->setToolTip(str);
1,607 ✔
243
  }
1,607 ✔
244
  else if(m_fx)
46 ✔
245
  {
246
    this->setToolTip(m_fx->toolTip());
46 ✔
247
  }
46 ✔
248
}
1,653 ✔
249

250
void NodeItem::resetItem()
8 ✔
251
{
252
  qDeleteAll(m_inlets);
8 ✔
253
  m_inlets.clear();
8 ✔
254

255
  qDeleteAll(m_outlets);
8 ✔
256
  m_outlets.clear();
8 ✔
257

258
  delete m_resizeCorner;
8 ✔
259
  m_resizeCorner = nullptr;
8 ✔
260
  if(m_presenter)
8 ✔
261
  {
262
    QPointer<Process::LayerView> oldView = static_cast<Process::LayerView*>(m_fx);
1 ✔
263
    delete m_presenter;
1 ✔
264
    m_presenter = nullptr;
1 ✔
265
    if(oldView)
1 ✔
266
      delete oldView;
1 ✔
267
    m_fx = nullptr;
1 ✔
268
    QObject::disconnect(&m_model, &Process::ProcessModel::setSize, this, nullptr);
1 ✔
269
  }
1 ✔
270
  else
271
  {
272
    delete m_fx;
7 ✔
273
    m_fx = nullptr;
7 ✔
274
  }
275

276
  m_contentSize = QSizeF{minimalContentWidth(), minimalContentHeight()};
8 ✔
277
}
8 ✔
278

279
void NodeItem::resetInlets()
2,516 ✔
280
{
281
  // if(!m_presenter)
282
  //   return;
283
  qDeleteAll(m_inlets);
2,516 ✔
284
  m_inlets.clear();
2,516 ✔
285

286
  // We don't display ports on the left & right side
287
  // for normal items which are supposed to handle it
288
  if(dynamic_cast<Process::DefaultEffectItem*>(m_fx) && !m_presenter)
2,516 ✔
289
    return updateTitlePos();
872 ✔
290

291
  const qreal x = InletX0;
1,644 ✔
292
  qreal y = m_label ? InletY0 : InletY0 - 10.;
1,644 ✔
293
  auto& portFactory = score::GUIAppContext().interfaces<Process::PortFactoryList>();
1,644 ✔
294
  for(Process::Inlet* port : m_model.inlets())
28,699 ✔
295
  {
296
    if(port->displayHandledExplicitly)
27,055 ✔
297
      continue;
25,341 ✔
298

299
    Process::PortFactory* fact = portFactory.get(port->concreteKey());
1,714 ✔
300
    Dataflow::PortItem* item = fact->makePortItem(*port, m_context, this, this);
1,714 ✔
301
    item->setPos(x, y);
1,714 ✔
302
    item->setZValue(10);
1,714 ✔
303
    m_inlets.push_back(item);
1,714 ✔
304

305
    y += PortSpacing;
1,714 ✔
306
  }
307

308
  updateTitlePos();
1,644 ✔
309
}
2,516 ✔
310

311
void NodeItem::resetOutlets()
2,381 ✔
312
{
313
  // if(!m_presenter)
314
  //   return;
315
  qDeleteAll(m_outlets);
2,381 ✔
316
  m_outlets.clear();
2,381 ✔
317

318
  // We don't display ports on the left & right side
319
  // for normal items which are supposed to handle it
320
  if(dynamic_cast<Process::DefaultEffectItem*>(m_fx) && !m_presenter)
2,381 ✔
321
    return updateTitlePos();
734 ✔
322

323
  const qreal x = m_contentSize.width() + OutletX0;
1,647 ✔
324
  // Next to the inlet a fully custom item shows at its top-left
325
  qreal y = m_label ? OutletY0 : 5.;
1,647 ✔
326

327
  auto& portFactory = score::AppContext().interfaces<Process::PortFactoryList>();
1,647 ✔
328
  for(Process::Outlet* port : m_model.outlets())
5,333 ✔
329
  {
330
    if(port->displayHandledExplicitly)
3,686 ✔
331
      continue;
1 ✔
332
    Process::PortFactory* fact = portFactory.get(port->concreteKey());
3,685 ✔
333
    auto item = fact->makePortItem(*port, m_context, this, this);
3,685 ✔
334
    item->setPos(x, y);
3,685 ✔
335
    item->setZValue(10);
3,685 ✔
336
    m_outlets.push_back(item);
3,685 ✔
337

338
    y += PortSpacing;
3,685 ✔
339
  }
340
}
2,381 ✔
341

342
void NodeItem::updateLabel()
1,767 ✔
343
{
344
  if(const auto& name = m_model.metadata().getName(); !name.isEmpty())
1,767 ✔
345
  {
346
    m_label->setText(name);
1,767 ✔
347
  }
1,767 ✔
UNCOV
348
  else if(const auto& label = m_model.metadata().getLabel(); !label.isEmpty())
×
349
  {
UNCOV
350
    m_label->setText(label);
×
UNCOV
351
  }
×
352
  else
353
  {
UNCOV
354
    m_label->setText(m_model.prettyShortName());
×
355
  }
356

357
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
1,767 ✔
358
  {
359
    resizeable->setMinimumWidth(minimalContentWidth());
100 ✔
360
  }
100 ✔
361
  QTimer::singleShot(2, this, &NodeItem::updateSize);
1,767 ✔
362
}
1,767 ✔
363

364
void NodeItem::updateTitlePos()
16,386 ✔
365
{
366
  if(!m_label)
16,386 ✔
367
    return;
11 ✔
368

369
  double x0 = FoldX0;
16,375 ✔
370
  if(!m_inlets.empty())
16,375 ✔
371
    x0 += InletX0;
6,924 ✔
372

373
  m_fold->setPos({x0, FoldY0});
16,375 ✔
374
  x0 += UiX0 - FoldX0 + 2.;
16,375 ✔
375

376
  if(m_scriptButton)
16,375 ✔
377
  {
378
    m_scriptButton->setPos({x0, UiY0});
3,010 ✔
379
    x0 += UiX0 + SpacingIcon;
3,010 ✔
380
  }
3,010 ✔
381

382
  if(m_uiButton)
16,375 ✔
383
  {
384
    m_uiButton->setPos({x0, UiY0});
415 ✔
385
    x0 += UiX0 + SpacingIcon;
415 ✔
386
  }
415 ✔
387

388
  if(m_presetButton)
16,375 ✔
389
  {
390
    m_presetButton->setPos({x0, UiY0});
16,375 ✔
391
    x0 += UiX0 + SpacingIcon * 2.;
16,375 ✔
392
  }
16,375 ✔
393

394
  m_label->setPos({x0, TitleY0});
16,375 ✔
395
}
16,386 ✔
396

UNCOV
397
QSizeF NodeItem::size() const noexcept
×
398
{
UNCOV
399
  return m_contentSize;
×
400
}
401

402
void NodeItem::setSelected(bool s)
28 ✔
403
{
404
  m_selected = s;
28 ✔
405
  if(s)
28 ✔
406
    setFocus();
24 ✔
407
  else
408
    clearFocus();
4 ✔
409

410
  update();
28 ✔
411
}
28 ✔
412

413
QRectF NodeItem::contentRect() const noexcept
6,777 ✔
414
{
415
  if(!m_label)
6,777 ✔
416
  {
417
    return {
14 ✔
418
        0, 0, m_contentSize.width() + (hasRightSide() ? RightSideWidth : 0.),
14 ✔
419
        m_contentSize.height() + FooterHeight};
14 ✔
420
  }
421
  else
422
  {
423
    double x = 0;
6,763 ✔
424
    double y = -TitleHeight;
6,763 ✔
425
    double w = m_contentSize.width();
6,763 ✔
426
    const double h = m_contentSize.height() + TitleHeight + FooterHeight;
6,763 ✔
427
    if(!m_inlets.empty())
6,763 ✔
428
    {
429
      x -= LeftSideWidth;
3,674 ✔
430
      w += LeftSideWidth;
3,674 ✔
431
    }
3,674 ✔
432
    if(hasRightSide())
6,763 ✔
433
      w += RightSideWidth;
4,048 ✔
434
    return {x, y, w, h};
6,763 ✔
435
  }
436
}
6,777 ✔
437

438
void NodeItem::updateContentRect()
6,775 ✔
439
{
440
  updateResizeCorner();
6,775 ✔
441
  const QRectF r = contentRect().adjusted(-2., -2., 2., 2.);
6,775 ✔
442
  if(r == m_contentRect)
6,775 ✔
443
    return;
4,684 ✔
444
  m_contentRect = r;
2,091 ✔
445
  geometryChanged();
2,091 ✔
446
}
6,775 ✔
447

448
bool NodeItem::hasRightSide() const noexcept
7,150 ✔
449
{
450
  // A fully custom item lays out the outlets it does not draw itself in that
451
  // strip, possibly before m_outlets is filled
452
  return !m_outlets.empty()
10,085 ✔
453
         || ((m_model.flags() & Process::ProcessFlags::FullyCustomItem)
7,178 ✔
454
             && ossia::any_of(m_model.outlets(), [](const Process::Outlet* o) {
2,939 ✔
455
                  return !o->displayHandledExplicitly;
4 ✔
456
                }));
457
}
458

459
class NodeResizeCorner final : public QGraphicsItem
460
{
461
public:
462
  explicit NodeResizeCorner(NodeItem& node)
99 ✔
463
      : QGraphicsItem{&node}
99 ✔
464
      , m_node{node}
99 ✔
465
  {
99 ✔
466
    setAcceptedMouseButtons(Qt::LeftButton);
99 ✔
467
    setZValue(1000.);
99 ✔
468
    setCursor(score::Skin::instance().CursorScaleFDiag);
99 ✔
469
  }
99 ✔
470

471
  QRectF boundingRect() const override { return {-10., -10., 10., 10.}; }
46 ✔
472
  void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*) override
2 ✔
473
  {
474
    p->setPen(Process::Style::instance().IntervalWarning().main.pen0_solid_round);
2 ✔
475
    p->drawLine(QPointF{-6., -2.}, QPointF{-2., -2.});
2 ✔
476
    p->drawLine(QPointF{-2., -6.}, QPointF{-2., -2.});
2 ✔
477
  }
2 ✔
478

479
protected:
480
  // The node does the resizing, as from its own corner
481
  void mousePressEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
482
  {
483
    ev->setPos(mapToParent(ev->pos()));
2 ✔
484
    m_node.m_resizeFromCorner = true;
2 ✔
485
    m_node.mousePressEvent(ev);
2 ✔
486
    m_node.m_resizeFromCorner = false;
2 ✔
487
  }
2 ✔
488
  void mouseMoveEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
489
  {
490
    ev->setPos(mapToParent(ev->pos()));
2 ✔
491
    m_node.mouseMoveEvent(ev);
2 ✔
492
  }
2 ✔
493
  void mouseReleaseEvent(QGraphicsSceneMouseEvent* ev) override
1 ✔
494
  {
495
    ev->setPos(mapToParent(ev->pos()));
1 ✔
496
    m_node.mouseReleaseEvent(ev);
1 ✔
497
  }
1 ✔
498
  // The corner holds the grab: a release the scene drops must still end the
499
  // node's gesture
500
  bool sceneEvent(QEvent* ev) override
9 ✔
501
  {
502
    if(ev->type() == QEvent::UngrabMouse)
9 ✔
503
      m_node.sceneEvent(ev);
2 ✔
504
    return QGraphicsItem::sceneEvent(ev);
9 ✔
505
  }
506

507
private:
508
  NodeItem& m_node;
509
};
510

511
void NodeItem::updateResizeCorner()
6,775 ✔
512
{
513
  const bool wanted = m_presenter && !hasRightSide();
6,775 ✔
514
  if(!wanted)
6,775 ✔
515
  {
516
    delete m_resizeCorner;
6,571 ✔
517
    m_resizeCorner = nullptr;
6,571 ✔
518
    return;
6,571 ✔
519
  }
520
  if(!m_resizeCorner)
204 ✔
521
    m_resizeCorner = new NodeResizeCorner{*this};
99 ✔
522
  m_resizeCorner->setPos(m_contentSize.width(), m_contentSize.height());
204 ✔
523
}
6,775 ✔
524

525
QRectF NodeItem::boundingRect() const
179,712 ✔
526
{
527
  return m_contentRect.adjusted(-2., -2., 2., 2.);
179,712 ✔
528
}
529

530
//! Redraw for a fold state that has already been decided by the model.
531
void NodeItem::applyFold(bool folded)
2 ✔
532
{
533
  if(!m_fold || folded == m_folded)
2 ✔
534
    return;
1 ✔
535

536
  // setState() deliberately does not re-emit toggled(), so this cannot loop
537
  // back into the command that brought us here.
538
  m_fold->setState(!folded);
1 ✔
539

540
  resetItem();
1 ✔
541

542
  m_folded = folded;
1 ✔
543
  if(!m_folded)
1 ✔
544
  {
UNCOV
545
    createContentItem();
×
UNCOV
546
  }
×
547
  else
548
  {
549
    createFoldedItem();
1 ✔
550
  }
551

552
  QTimer::singleShot(1, this, [this] {
2 ✔
553
    resetInlets();
1 ✔
554
    resetOutlets();
1 ✔
555

556
    resizeAsync();
1 ✔
557
  });
1 ✔
558
}
2 ✔
559

560
void NodeItem::createFoldedItem()
114 ✔
561
{
562
  if(m_fx)
114 ✔
UNCOV
563
    return;
×
564

565
  auto& ctx = m_context;
114 ✔
566
  auto& model = m_model;
114 ✔
567
  // Body
568
  score::ResizeableItem* resizeable{};
114 ✔
569
  {
570
    auto fx = new Process::DefaultEffectItem{true, model, ctx, this};
114 ✔
571
    m_fx = fx;
114 ✔
572
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
114 ✔
573
    resizeable = fx;
114 ✔
574
  }
575
  setupItem(resizeable);
114 ✔
576
}
114 ✔
577

578
void NodeItem::createContentItem()
1,540 ✔
579
{
580
  if(m_fx)
1,540 ✔
UNCOV
581
    return;
×
582

583
  auto& ctx = m_context;
1,540 ✔
584
  auto& model = m_model;
1,540 ✔
585
  // Body
586
  auto& fact = ctx.app.interfaces<Process::LayerFactoryList>();
1,540 ✔
587
  score::ResizeableItem* resizeable{};
1,540 ✔
588
  if(auto factory = fact.findDefaultFactory(model))
1,540 ✔
589
  {
590
    if(auto fx = factory->makeItem(model, ctx, this))
1,231 ✔
591
    {
592
      m_fx = fx;
1,132 ✔
593
      m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
1,132 ✔
594
      resizeable = fx;
1,132 ✔
595
    }
1,132 ✔
596
    else if(auto fx = factory->makeLayerView(model, ctx, this))
99 ✔
597
    {
598
      m_fx = fx;
99 ✔
599
      m_presenter = factory->makeLayerPresenter(model, fx, ctx, this);
99 ✔
600
      m_contentSize = m_model.size();
99 ✔
601
      m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
99 ✔
602
      m_presenter->setHeight(m_contentSize.height());
99 ✔
603

604
      // TODO review the resizing heuristic...
605
      if(m_presenter)
99 ✔
606
      {
607
        ::bind(model, Process::ProcessModel::p_size{}, this, [this](QSizeF s) {
203 ✔
608
          if(s != m_contentSize)
104 ✔
609
            setSize(s);
4 ✔
610
        });
104 ✔
611
          connect(m_presenter, &Process::LayerPresenter::contextMenuRequested, this, [this] (QPoint pos, QPointF sp) {
99 ✔
UNCOV
612
              auto menu = new QMenu;
×
UNCOV
613
              auto& reg = score::GUIAppContext()
×
UNCOV
614
                              .applicationPlugin<Process::ApplicationPlugin>()
×
UNCOV
615
                              .layerContextMenuRegistrar();
×
616

UNCOV
617
              m_presenter->fillContextMenu(*menu, pos, sp, reg);
×
UNCOV
618
              if(menu->actions().size() > 0) {
×
UNCOV
619
                menu->exec(pos);
×
UNCOV
620
                menu->close();
×
UNCOV
621
              }
×
622

UNCOV
623
              menu->deleteLater();
×
UNCOV
624
          }, Qt::QueuedConnection);
×
625
      }
99 ✔
626
    }
99 ✔
627
  }
1,231 ✔
628

629
  if(!m_fx)
1,540 ✔
630
  {
631
    auto fx = new Process::DefaultEffectItem{false, model, ctx, this};
309 ✔
632
    m_fx = fx;
309 ✔
633
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
309 ✔
634
    resizeable = fx;
309 ✔
635
  }
309 ✔
636

637
  setupItem(resizeable);
1,540 ✔
638
}
1,540 ✔
639

640
void NodeItem::resizeAsync()
6,416 ✔
641
{
642
  updateTitlePos();
6,416 ✔
643
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
6,416 ✔
644
  {
645
    resizeable->setMinimumWidth(minimalContentWidth());
6,148 ✔
646
    // Needed to leave some time for defaulteffectitem
647
    QTimer::singleShot(2, this, [this] {
11,307 ✔
648
      updateSize();
5,159 ✔
649
      updateZoomRatio();
5,159 ✔
650
    });
5,159 ✔
651
  }
6,148 ✔
652
  else
653
  {
654
    QMetaObject::invokeMethod(this, [this] {
536 ✔
655
      updateSize();
268 ✔
656
      updateZoomRatio();
268 ✔
657
    });
268 ✔
658
  }
659
}
6,416 ✔
660

661
void NodeItem::recreate()
7 ✔
662
{
663
  resetItem();
7 ✔
664
  qDeleteAll(this->childItems());
7 ✔
665
  m_resizeCorner = nullptr;
7 ✔
666
  m_scriptButton = nullptr;
7 ✔
667
  m_uiButton = nullptr;
7 ✔
668
  m_presetButton = nullptr;
7 ✔
669
  m_label = nullptr;
7 ✔
670
  m_fold = nullptr;
7 ✔
671
  disconnect(&this->m_model, nullptr, this, nullptr);
7 ✔
672
  disconnect(&this->m_model.selection, nullptr, this, nullptr);
7 ✔
673
  disconnect(&this->m_model.metadata(), nullptr, this, nullptr);
7 ✔
674

675
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
7 ✔
676
  {
UNCOV
677
    createWithoutDecorations();
×
UNCOV
678
  }
×
679
  else
680
  {
681
    createWithDecorations();
7 ✔
682
  }
683

684
  initConnections();
7 ✔
685
}
7 ✔
686

687
void NodeItem::setupItem(score::ResizeableItem* resizeable)
1,654 ✔
688
{
689
  // Positions / size
690
  double w = std::max(minimalContentWidth(), m_contentSize.width());
1,654 ✔
691
  double h = std::max(minimalContentHeight(), m_contentSize.height());
1,654 ✔
692
  m_contentSize = QSizeF{w, h};
1,654 ✔
693

694
  resizeAsync();
1,654 ✔
695
  publishSize();
1,654 ✔
696

697
  if(resizeable)
1,654 ✔
698
  {
699
    if(auto lay = dynamic_cast<score::GraphicsLayout*>(resizeable))
1,555 ✔
700
      connect(lay, &score::ResizeableItem::childrenSizeChanged, this, [this, lay] {
954 ✔
701
        lay->layout();
206 ✔
702
        lay->fitChildrenRect();
206 ✔
703
        m_contentSize = lay->rect().size();
206 ✔
704

705
        resizeAsync();
206 ✔
706
      });
206 ✔
707
    connect(resizeable, &score::ResizeableItem::sizeChanged, this, [this](QSizeF sz) {
2,068 ✔
708
      double w = std::max(minimalContentWidth(), sz.width());
513 ✔
709
      double h = std::max(minimalContentHeight(), sz.height());
513 ✔
710
      m_contentSize = QSizeF{w, h};
513 ✔
711

712
      resizeAsync();
513 ✔
713
    });
513 ✔
714
  }
1,555 ✔
715
}
1,654 ✔
716

717
double NodeItem::minimalContentWidth() const noexcept
15,156 ✔
718
{
719
  if(Q_UNLIKELY(!m_label))
15,156 ✔
720
  {
721
    if((m_inlets.size() + m_outlets.size()) <= 1)
17 ✔
722
      return 10.;
17 ✔
723
    else
UNCOV
724
      return 20.;
×
725
  }
726
  else
727
  {
728
    return std::max(75.0, m_label->pos().x() + m_label->boundingRect().width() + 6.);
15,139 ✔
729
  }
730
}
15,156 ✔
731
double NodeItem::minimalContentHeight() const noexcept
8,908 ✔
732
{
733
  if(Q_UNLIKELY(!m_label))
8,908 ✔
734
    if(std::max(m_inlets.size(), m_outlets.size()) <= 1)
17 ✔
735
      return 10.;
17 ✔
736

737
  double h = 22.;
8,891 ✔
738
  if(!m_inlets.empty())
8,891 ✔
739
    h = std::max(h, m_inlets.back()->y() + 10.);
3,674 ✔
740
  if(!m_outlets.empty())
8,891 ✔
741
    h = std::max(h, m_outlets.back()->y() + 10.);
4,046 ✔
742
  return h;
8,891 ✔
743
}
8,908 ✔
744

745
void NodeItem::updateSize()
6,730 ✔
746
{
747
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
6,730 ✔
748
  {
749
    if(m_presenter)
10 ✔
750
      resizePresenter();
10 ✔
751
    // A fully-custom item lays out its own inlets, so the node has to be at
752
    // least as wide as that item: keeping the width the title asked for draws
753
    // the border inside the item, and the outlets end up inside it. Short
754
    // titles are where this shows, since that is when the title is not what
755
    // decides the width.
756
    QSizeF sz{minimalContentWidth(), minimalContentHeight()};
10 ✔
757
    if(m_fx)
10 ✔
758
    {
759
      auto fx_sz = m_fx->boundingRect().size();
10 ✔
760
      sz.rwidth() = std::max(sz.width(), fx_sz.width());
10 ✔
761
      sz.rheight() = std::max(sz.height(), fx_sz.height());
10 ✔
762
    }
10 ✔
763
    if(sz != m_contentSize)
10 ✔
764
    {
765
      prepareGeometryChange();
×
UNCOV
766
      m_contentSize = sz;
×
767
      publishSize();
×
UNCOV
768
    }
×
769
    for(auto& outlet : m_outlets)
10 ✔
UNCOV
770
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
×
771
    updateContentRect();
10 ✔
772
    update();
10 ✔
773
    if(auto sc = this->scene())
10 ✔
774
      sc->update();
10 ✔
775
    return;
10 ✔
776
  }
777

778
  if(!m_label)
6,720 ✔
779
  {
UNCOV
780
    if(auto sc = this->scene())
×
781
      sc->update();
×
782
    return;
×
783
  }
784
  QSizeF sz{minimalContentWidth(), minimalContentHeight()};
6,720 ✔
785
  if(m_fx)
6,720 ✔
786
  {
787
    auto fx_sz = m_fx->boundingRect().size();
6,720 ✔
788
    sz.rwidth() = std::max(sz.width(), fx_sz.width());
6,720 ✔
789
    sz.rheight() = std::max(sz.height(), fx_sz.height());
6,720 ✔
790
  }
6,720 ✔
791
  //if (sz != m_contentSize || !m_fx)
792
  {
793
    prepareGeometryChange();
6,720 ✔
794
    if(m_fx)
6,720 ✔
795
    {
796
      m_contentSize = sz;
6,720 ✔
797
      if(!m_presenter)
6,720 ✔
798
      {
799
        if(m_model.outlets().size() > 0 && m_model.inlets().size() == 0)
6,370 ✔
800
        {
801
          // Align right for the case where we only have a series of outlets
802
          updateContentRect();
41 ✔
803
          m_fx->setPos(m_contentRect.width() - m_fx->boundingRect().width(), 0);
41 ✔
804
        }
41 ✔
805
        else
806
        {
807
          m_fx->setPos(0, 0);
6,329 ✔
808
        }
809
      }
6,370 ✔
810
    }
6,720 ✔
811

812
    if(m_presenter)
6,720 ✔
813
      resizePresenter();
350 ✔
814

815
    if(m_scriptButton)
6,720 ✔
816
    {
817
      m_scriptButton->setParentItem(nullptr);
1,006 ✔
818
    }
1,006 ✔
819
    if(m_uiButton)
6,720 ✔
820
    {
821
      m_uiButton->setParentItem(nullptr);
183 ✔
822
    }
183 ✔
823

824
    for(auto& outlet : m_outlets)
15,900 ✔
825
    {
826
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
9,180 ✔
827
    }
828

829
    if(m_scriptButton)
6,720 ✔
830
    {
831
      m_scriptButton->setParentItem(this);
1,006 ✔
832
    }
1,006 ✔
833
    if(m_uiButton)
6,720 ✔
834
    {
835
      m_uiButton->setParentItem(this);
183 ✔
836
    }
183 ✔
837
    updateTitlePos();
6,720 ✔
838
    updateContentRect();
6,720 ✔
839
    publishSize();
6,720 ✔
840
    update();
6,720 ✔
841
    if(auto sc = this->scene())
6,720 ✔
842
      sc->update();
6,720 ✔
843
  }
844
}
6,730 ✔
845

846
void NodeItem::publishSize()
8,374 ✔
847
{
848
  // Node placement (e.g. creating a process after another one) reads the size
849
  // off the model: keep it in sync with what is actually drawn. A folded node
850
  // must not overwrite the unfolded size it will go back to.
851
  if(m_folded)
8,374 ✔
852
    return;
731 ✔
853
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
7,643 ✔
854
    return;
4 ✔
855
  if(m_model.size() == m_contentSize)
7,639 ✔
856
    return;
5,897 ✔
857

858
  const_cast<Process::ProcessModel&>(m_model).setSize(m_contentSize);
1,742 ✔
859
}
8,374 ✔
860

861
void NodeItem::setSize(QSizeF sz)
4 ✔
862
{
863
  if(m_presenter)
4 ✔
864
  {
865
    // TODO: find a way to indicate to a model what will be the size of the
866
    // item
867
    //      - maybe set it without a command in that case ?
868
    prepareGeometryChange();
4 ✔
869
    m_contentSize = sz;
4 ✔
870

871
    if(m_scriptButton)
4 ✔
872
    {
UNCOV
873
      m_scriptButton->setParentItem(nullptr);
×
UNCOV
874
    }
×
875
    if(m_uiButton)
4 ✔
876
    {
UNCOV
877
      m_uiButton->setParentItem(nullptr);
×
UNCOV
878
    }
×
879

880
    resizePresenter();
4 ✔
881

882
    resetInlets();
4 ✔
883
    resetOutlets();
4 ✔
884
    auto cur_x = m_contentSize.width() + TopButtonX0;
4 ✔
885
    if(m_scriptButton)
4 ✔
886
    {
UNCOV
887
      m_scriptButton->setParentItem(this);
×
UNCOV
888
      m_scriptButton->setPos({cur_x, TopButtonY0});
×
UNCOV
889
      cur_x += UiX0 + SpacingIcon;
×
UNCOV
890
    }
×
891
    if(m_uiButton)
4 ✔
892
    {
UNCOV
893
      m_uiButton->setParentItem(this);
×
UNCOV
894
      m_uiButton->setPos({cur_x, TopButtonY0});
×
UNCOV
895
    }
×
896
  }
4 ✔
897
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
4 ✔
898
  {
UNCOV
899
    resizeable->setMinimumWidth(sz.width());
×
UNCOV
900
  }
×
901
  updateContentRect();
4 ✔
902
  update();
4 ✔
903
  if(auto sc = this->scene())
4 ✔
904
    sc->update();
4 ✔
905
}
4 ✔
906

907
const Id<Process::ProcessModel>& NodeItem::id() const noexcept
6 ✔
908
{
909
  return m_model.id();
6 ✔
910
}
911

912
NodeItem::~NodeItem()
3,292 ✔
913
{
3,292 ✔
914
  clearDropCableHighlight();
1,646 ✔
915
  delete m_presenter;
1,646 ✔
916
}
3,292 ✔
917

918
void NodeItem::setParentDuration(TimeVal r)
5 ✔
919
{
920
  if(r != m_parentDuration)
5 ✔
921
  {
UNCOV
922
    m_parentDuration = r;
×
923
    updateZoomRatio();
×
UNCOV
924
  }
×
925
}
5 ✔
926

927
void NodeItem::setPlayPercentage(float f, TimeVal parent_dur)
1,109 ✔
928
{
929
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
1,109 ✔
UNCOV
930
    return;
×
931

932
  if(m_playPercentage != f)
1,109 ✔
933
  {
934
    // Comes from the interval -> if we are looping we make a small modulo of it
UNCOV
935
    if(m_model.loops() && m_model.loopDuration().impl > 0)
×
936
    {
UNCOV
937
      auto loopDur = m_model.loopDuration().impl;
×
UNCOV
938
      double playdur = double(f) * parent_dur.impl;
×
939
      f = std::fmod(playdur, loopDur) / loopDur;
×
UNCOV
940
      if(m_playPercentage != f)
×
941
      {
UNCOV
942
        m_playPercentage = f;
×
UNCOV
943
        update({12., -1., (m_contentSize.width() - 24.), 3.});
×
UNCOV
944
      }
×
UNCOV
945
    }
×
946
    else
947
    {
UNCOV
948
      f = ossia::clamp(f, 0.f, 1.f);
×
UNCOV
949
      if(m_playPercentage != f)
×
950
      {
UNCOV
951
        m_playPercentage = f;
×
UNCOV
952
        update({12., -1., (m_contentSize.width() - 24.) * f + 1., 3.});
×
UNCOV
953
      }
×
954
    }
UNCOV
955
  }
×
956
}
1,109 ✔
957

UNCOV
958
qreal NodeItem::width() const noexcept
×
959
{
UNCOV
960
  return m_contentSize.width();
×
961
}
962

UNCOV
963
qreal NodeItem::height() const
×
964
{
UNCOV
965
  return TitleHeight + m_contentSize.height() + FooterHeight;
×
966
}
967

968
void NodeItem::initConnections()
1,653 ✔
969
{
970
  auto& process = this->model();
1,653 ✔
971
  ::bind(process, Process::ProcessModel::p_position{}, this, [this](QPointF p) {
3,313 ✔
972
    if(p != pos())
1,660 ✔
973
      setPos(p);
1,650 ✔
974
  });
1,660 ✔
975

976
  auto on_sizeChanged = [this] {
4,077 ✔
977
    m_needResize = true;
2,424 ✔
978
    QTimer::singleShot(1, this, [this] {
4,740 ✔
979
      if(!m_needResize)
2,316 ✔
980
        return;
1,156 ✔
981
      m_needResize = false;
1,160 ✔
982
      resizeAsync();
1,160 ✔
983
    });
2,316 ✔
984
  };
2,424 ✔
985
  connect(&process, &Process::ProcessModel::controlAdded, this, on_sizeChanged);
1,653 ✔
986
  connect(&process, &Process::ProcessModel::controlRemoved, this, on_sizeChanged);
1,653 ✔
987
  connect(&process, &Process::ProcessModel::controlOutletAdded, this, on_sizeChanged);
1,653 ✔
988
  connect(&process, &Process::ProcessModel::controlOutletRemoved, this, on_sizeChanged);
1,653 ✔
989
  connect(&process, &Process::ProcessModel::inletsChanged, this, on_sizeChanged);
1,653 ✔
990
  connect(&process, &Process::ProcessModel::outletsChanged, this, on_sizeChanged);
1,653 ✔
991
  connect(&process, &Process::ProcessModel::flagsChanged, this, &NodeItem::recreate);
1,653 ✔
992
}
1,653 ✔
993

994
const ProcessModel& NodeItem::model() const noexcept
8,697 ✔
995
{
996
  return m_model;
8,697 ✔
997
}
998

999
void NodeItem::resizePresenter()
364 ✔
1000
{
1001
  m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
364 ✔
1002
  m_presenter->setHeight(m_contentSize.height());
364 ✔
1003
  updateZoomRatio();
364 ✔
1004
}
364 ✔
1005

1006
void NodeItem::updateZoomRatio() const noexcept
5,793 ✔
1007
{
1008
  const auto dur = m_model.loops() ? m_model.loopDuration() : m_parentDuration;
5,793 ✔
1009
  if(m_presenter)
5,793 ✔
1010
  {
1011
    m_presenter->on_zoomRatioChanged(dur.impl / m_contentSize.width());
634 ✔
1012
    // TODO investigate why this is necessary for scenario:
1013
    m_presenter->parentGeometryChanged();
634 ✔
1014
  }
634 ✔
1015
}
5,793 ✔
1016

1017
bool NodeItem::isInSelectionCorner(QPointF p, QRectF r) const
3 ✔
1018
{
1019
  return (p.x() - r.x()) > (r.width() - 10.) && (p.y() - r.y()) > (r.height() - 10.);
3 ✔
1020
}
1021

1022
void NodeItem::paint(
9 ✔
1023
    QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
1024
{
1025
  auto& style = Process::Style::instance();
9 ✔
1026
  const auto& skin = style.skin;
9 ✔
1027
  const auto rect = m_contentRect;
9 ✔
1028
  //painter->fillRect(boundingRect(), Qt::red);
1029
  //return;
1030
  const auto flags = this->m_model.flags();
9 ✔
1031

1032
  const auto& bset = skin.Emphasis5;
9 ✔
1033
  const auto& fillbrush = !(flags & Process::ProcessFlags::NodeHasNoBackground)
9 ✔
1034
                              ? skin.Emphasis5
9 ✔
UNCOV
1035
                              : skin.TransparentBrush;
×
1036
  const auto& brush = m_selected ? skin.Base2.darker
18 ✔
1037
                      : m_hover  ? bset.lighter
9 ✔
1038
                                 : bset.main;
9 ✔
1039
  const auto& pen = m_dropping ? skin.Warn2.main.pen3_solid_round_round
9 ✔
1040
                               : brush.pen2_solid_round_round;
9 ✔
1041

1042
  painter->setRenderHint(QPainter::Antialiasing, true);
9 ✔
1043

1044
  // Body
1045
  painter->setPen(pen);
9 ✔
1046

1047
#if defined(SCORE_DEBUG_REDRAWS)
1048
  {
1049
    QColor c;
1050
    c.setRedF(0.5 * double(rand()) / RAND_MAX);
1051
    c.setGreenF(0.5 * double(rand()) / RAND_MAX);
1052
    c.setBlueF(0.5 * double(rand()) / RAND_MAX);
1053
    c.setAlphaF(1.);
1054
    painter->setBrush(c);
1055
  }
1056
#else
1057
  painter->setBrush(fillbrush);
9 ✔
1058
#endif
1059

1060
  painter->drawRoundedRect(rect, Corner, Corner);
9 ✔
1061

1062
  painter->setRenderHint(QPainter::Antialiasing, false);
9 ✔
1063

1064
  // Exec
1065
  if(!(flags & Process::ProcessFlags::FullyCustomItem))
9 ✔
1066
    if(m_playPercentage > 0.)
9 ✔
1067
    {
UNCOV
1068
      painter->setPen(style.IntervalPlayFill().main.pen1_solid_flat_miter);
×
UNCOV
1069
      painter->drawLine(
×
UNCOV
1070
          QPointF{12., 0.}, QPointF{12. + (width() - 24.) * m_playPercentage, 0.});
×
UNCOV
1071
    }
×
1072

1073
  // Resizing handle, in the right strip; NodeResizeCorner draws it otherwise
1074
  if(m_presenter && hasRightSide())
9 ✔
1075
  {
1076
    const auto h = m_contentSize.height();
7 ✔
1077
    const auto w = m_contentSize.width() + RightSideWidth;
7 ✔
1078
    painter->setPen(style.IntervalWarning().main.pen0_solid_round);
7 ✔
1079
    double start_x = w - 6.;
7 ✔
1080
    double start_y = h - 6.;
7 ✔
1081
    double center_x = w - 2.;
7 ✔
1082
    double center_y = h - 2.;
7 ✔
1083
    painter->drawLine(start_x, center_y, center_x, center_y);
7 ✔
1084
    painter->drawLine(center_x, start_y, center_x, center_y);
7 ✔
1085
  }
7 ✔
1086
}
9 ✔
1087

1088
namespace
1089
{
1090
enum Interaction
1091
{
1092
  None,
1093
  Move,
1094
  Resize
1095
} nodeItemInteraction{};
1096
QSizeF origNodeSize{};
1097
bool nodeDidMove{};
1098
bool nodeSelectOnRelease{};
1099
bool nodeSelectCumulation{};
1100
}
1101

1102
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
6 ✔
1103
{
1104
  nodeItemInteraction = Interaction::None;
6 ✔
1105
  if(event->button() == Qt::LeftButton)
6 ✔
1106
  {
1107
    nodeDidMove = false;
6 ✔
1108
    if(m_presenter
6 ✔
1109
       && (isInSelectionCorner(event->pos(), m_contentRect)
6 ✔
1110
           || m_resizeFromCorner))
2 ✔
1111
    {
1112
      nodeItemInteraction = Interaction::Resize;
2 ✔
1113
      origNodeSize = m_model.size();
2 ✔
1114
    }
2 ✔
1115
    else
1116
    {
1117
      nodeItemInteraction = Interaction::Move;
4 ✔
1118
    }
1119

1120
    if(m_presenter && m_model.flags() & Process::ProcessFlags::ItemRequiresUniqueFocus)
6 ✔
1121
    {
UNCOV
1122
      m_context.focusDispatcher.focus(m_presenter);
×
1123
    }
×
1124
    {
1125
      const bool cumulation = event->modifiers() & Qt::ControlModifier;
6 ✔
1126

1127
      // Membership of the selection stack, not ProcessModel::selection: the
1128
      // latter is also set on a process that merely *contains* the selection --
1129
      // clicking a port marks its parent node selected so that the node draws
1130
      // its frame, while the stack holds only the port. Deferring on the flag
1131
      // left such a node drawn as selected and yet impossible to drag, since
1132
      // selectedProcesses() below reads the stack and would come back empty.
1133
      const Selection current = m_context.selectionStack.currentSelection();
6 ✔
1134
      if(current.contains(&m_model))
6 ✔
1135
      {
1136
        nodeSelectOnRelease = true;
2 ✔
1137
        nodeSelectCumulation = cumulation;
2 ✔
1138
      }
2 ✔
1139
      else
1140
      {
1141
        nodeSelectOnRelease = false;
4 ✔
1142
        Selection sel;
4 ✔
1143
        if(cumulation)
4 ✔
1144
          sel = current;
×
1145
        sel.append(&m_model);
4 ✔
1146
        score::SelectionDispatcher{m_context.selectionStack}.select(sel);
4 ✔
1147
      }
4 ✔
1148
    }
6 ✔
1149
    event->accept();
6 ✔
1150
  }
6 ✔
1151
  else
1152
  {
UNCOV
1153
    return QGraphicsItem::mousePressEvent(event);
×
1154
  }
1155
}
6 ✔
1156
namespace
1157
{
1158
static std::vector<const Process::ProcessModel*>
1159
selectedProcesses(const score::DocumentContext& ctx)
5 ✔
1160
{
1161
  std::vector<const Process::ProcessModel*> ps;
5 ✔
1162
  auto sel = ctx.selectionStack.currentSelection();
5 ✔
1163
  ps.reserve(sel.size());
5 ✔
1164
  for(auto& obj : sel)
10 ✔
1165
    if(auto p = qobject_cast<Process::ProcessModel*>(obj))
5 ✔
1166
    {
1167
      ps.push_back(p);
5 ✔
1168
    }
5 ✔
1169
  return ps;
5 ✔
1170
}
5 ✔
1171
}
1172
//! The single cable the node was let go on top of, if any: ambiguous drops
1173
//! (more than one cable under the node) do nothing.
UNCOV
1174
static const Process::Cable* cableUnder(const NodeItem& node, QRectF contentRect)
×
1175
{
UNCOV
1176
  auto sc = node.scene();
×
UNCOV
1177
  if(!sc)
×
UNCOV
1178
    return nullptr;
×
1179

UNCOV
1180
  const Process::Cable* res{};
×
UNCOV
1181
  for(auto item : sc->items(node.mapToScene(contentRect.center())))
×
1182
  {
UNCOV
1183
    if(item->type() != Dataflow::CableItem::Type)
×
1184
      continue;
×
1185
    if(res)
×
UNCOV
1186
      return nullptr;
×
1187
    res = &static_cast<Dataflow::CableItem*>(item)->model();
×
1188
  }
1189
  return res;
×
1190
}
×
1191

1192
void NodeItem::updateDropCableHighlight()
5 ✔
1193
{
1194
  Dataflow::CableItem* next{};
5 ✔
1195
  if(canDropOnCableHandler)
5 ✔
1196
  {
1197
    if(auto* cbl = cableUnder(*this, m_contentRect))
×
1198
    {
UNCOV
1199
      if(canDropOnCableHandler(m_model, *cbl))
×
1200
      {
UNCOV
1201
        auto& cables = m_context.dataflow.cables();
×
UNCOV
1202
        if(auto it = cables.find(cbl); it != cables.end())
×
UNCOV
1203
          next = it->second;
×
UNCOV
1204
      }
×
UNCOV
1205
    }
×
UNCOV
1206
  }
×
1207

1208
  if(next == m_dropCable)
5 ✔
1209
    return;
5 ✔
UNCOV
1210
  if(m_dropCable)
×
UNCOV
1211
    m_dropCable->setDropTarget(false);
×
UNCOV
1212
  m_dropCable = next;
×
UNCOV
1213
  if(m_dropCable)
×
UNCOV
1214
    m_dropCable->setDropTarget(true);
×
1215
}
5 ✔
1216

1217
void NodeItem::clearDropCableHighlight()
1,654 ✔
1218
{
1219
  if(m_dropCable)
1,654 ✔
UNCOV
1220
    m_dropCable->setDropTarget(false);
×
1221
  m_dropCable = nullptr;
1,654 ✔
1222
}
1,654 ✔
1223

1224
void NodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
9 ✔
1225
{
1226
  if(nodeItemInteraction == Interaction::None)
9 ✔
1227
    return QGraphicsItem::mouseMoveEvent(event);
×
1228

1229
  auto parent = this->parentItem();
9 ✔
1230
  auto origp = parent->mapFromScene(event->buttonDownScenePos(Qt::LeftButton));
9 ✔
1231
  auto p = parent->mapFromScene(event->scenePos());
9 ✔
1232
  if(p != origp)
9 ✔
1233
    nodeDidMove = true;
8 ✔
1234

1235
  if(nodeDidMove)
9 ✔
1236
  {
1237
    switch(nodeItemInteraction)
8 ✔
1238
    {
1239
      case Interaction::Resize: {
1240
        const auto sz = origNodeSize + QSizeF{p.x() - origp.x(), p.y() - origp.y()};
3 ✔
1241
        m_dispatcher.submit<Process::ResizeNode>(
6 ✔
1242
            m_model, sz.expandedTo({minimalContentWidth(), minimalContentHeight()}));
3 ✔
1243
        updateSize();
3 ✔
1244
        break;
3 ✔
1245
      }
1246
      case Interaction::Move: {
1247
        auto procs = selectedProcesses(m_context);
5 ✔
1248
        // The press put this node in the selection, so it is normally already
1249
        // in here; a selection that somehow does not hold it would turn the
1250
        // whole drag into a silent no-op rather than moving the node under the
1251
        // mouse.
1252
        if(!ossia::contains(procs, &m_model))
5 ✔
1253
          procs.push_back(&m_model);
×
1254
        m_dispatcher.submit<Process::MoveNodes>(std::move(procs), p - origp);
5 ✔
1255
        updateDropCableHighlight();
5 ✔
1256
        break;
1257
      }
5 ✔
1258
      default:
UNCOV
1259
        break;
×
1260
    }
1261
  }
8 ✔
1262
  event->accept();
9 ✔
1263
}
9 ✔
1264

1265

1266
void NodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
4 ✔
1267
{
1268
  if(nodeItemInteraction == Interaction::None)
4 ✔
UNCOV
1269
    return QGraphicsItem::mouseReleaseEvent(event);
×
1270

1271
  mouseMoveEvent(event);
4 ✔
1272

1273
  clearDropCableHighlight();
4 ✔
1274

1275
  if(nodeDidMove)
4 ✔
1276
  {
1277
    // Part of the same macro as the move: dropping a node on a cable is a
1278
    // single gesture and must undo as one.
1279
    if(dropOnCableHandler && nodeItemInteraction == Interaction::Move)
3 ✔
1280
    {
1281
      if(auto cbl = cableUnder(*this, m_contentRect);
×
1282
         cbl && (!canDropOnCableHandler || canDropOnCableHandler(m_model, *cbl)))
×
1283
      {
1284
        score::Dispatcher_T<MultiOngoingCommandDispatcher> disp{m_dispatcher};
×
UNCOV
1285
        dropOnCableHandler(m_model, *cbl, disp);
×
1286
      }
×
UNCOV
1287
    }
×
1288
    m_dispatcher.commit<Process::MoveNodesMacro>();
3 ✔
1289
  }
3 ✔
1290
  else if(nodeSelectOnRelease)
1 ✔
1291
  {
1292
    Selection sel = filterSelections(
×
1293
        &m_model, m_context.selectionStack.currentSelection(), nodeSelectCumulation);
×
1294
    score::SelectionDispatcher{m_context.selectionStack}.select(sel);
×
1295
  }
×
1296
  nodeSelectOnRelease = false;
4 ✔
1297
  nodeDidMove = false;
4 ✔
1298
  event->accept();
4 ✔
1299
}
4 ✔
1300

1301
//! QEvent::UngrabMouse: the scene took the implicit grab away and there will be
1302
//! no release to end the drag on -- most visibly when the button was let go
1303
//! outside the window and the pointer then comes back in. Without this the
1304
//! MoveNodes command stays open, and the next drag of this node updates *it*
1305
//! instead of starting fresh, moving the node relative to the previous press.
1306
bool NodeItem::sceneEvent(QEvent* event)
9,000 ✔
1307
{
1308
  if(event->type() == QEvent::UngrabMouse)
9,000 ✔
1309
  {
1310
    if(nodeItemInteraction != Interaction::None)
4 ✔
1311
    {
1312
      clearDropCableHighlight();
4 ✔
1313
      if(nodeDidMove)
4 ✔
1314
        m_dispatcher.commit<Process::MoveNodesMacro>();
2 ✔
1315

1316
      nodeItemInteraction = Interaction::None;
4 ✔
1317
      nodeSelectOnRelease = false;
4 ✔
1318
      nodeDidMove = false;
4 ✔
1319
    }
4 ✔
1320
  }
4 ✔
1321
  return QGraphicsItem::sceneEvent(event);
9,000 ✔
1322
}
1323

UNCOV
1324
void NodeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
×
1325
{
UNCOV
1326
  event->accept();
×
UNCOV
1327
}
×
1328

1329
void NodeItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
122 ✔
1330
{
1331
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
122 ✔
1332
  {
1333
    auto& skin = score::Skin::instance();
1 ✔
1334
    setCursor(skin.CursorScaleFDiag);
1 ✔
1335
  }
1 ✔
1336
  else
1337
  {
1338
    unsetCursor();
121 ✔
1339
  }
1340

1341
  m_hover = true;
122 ✔
1342
  update();
122 ✔
1343
  event->accept();
122 ✔
1344
}
122 ✔
1345

1346
void NodeItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
8,744 ✔
1347
{
1348
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
8,744 ✔
1349
  {
UNCOV
1350
    auto& skin = score::Skin::instance();
×
UNCOV
1351
    setCursor(skin.CursorScaleFDiag);
×
UNCOV
1352
  }
×
1353
  else
1354
  {
1355
    unsetCursor();
8,744 ✔
1356
  }
1357
  event->accept();
8,744 ✔
1358
}
8,744 ✔
1359

1360
void NodeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
119 ✔
1361
{
1362
  unsetCursor();
119 ✔
1363

1364
  m_hover = false;
119 ✔
1365
  update();
119 ✔
1366
  event->accept();
119 ✔
1367
}
119 ✔
1368

UNCOV
1369
void NodeItem::keyPressEvent(QKeyEvent* event)
×
1370
{
UNCOV
1371
  if(event->key() == Qt::Key_Down)
×
1372
  {
UNCOV
1373
    auto ports = Dataflow::getProcessPorts(this->m_model);
×
UNCOV
1374
    if(!ports.empty())
×
1375
    {
UNCOV
1376
      score::SelectionDispatcher disp{m_context.selectionStack};
×
UNCOV
1377
      disp.select(*ports.front());
×
UNCOV
1378
    }
×
UNCOV
1379
  }
×
UNCOV
1380
  event->accept();
×
UNCOV
1381
}
×
1382

UNCOV
1383
void NodeItem::keyReleaseEvent(QKeyEvent* event)
×
1384
{
UNCOV
1385
  event->accept();
×
UNCOV
1386
}
×
UNCOV
1387
void NodeItem::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
×
1388
{
UNCOV
1389
  m_dropping = true;
×
UNCOV
1390
  event->accept();
×
UNCOV
1391
  update();
×
UNCOV
1392
}
×
1393

UNCOV
1394
void NodeItem::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
×
1395
{
UNCOV
1396
  m_dropping = true;
×
UNCOV
1397
  event->accept();
×
UNCOV
1398
  update();
×
UNCOV
1399
}
×
1400

UNCOV
1401
void NodeItem::dragLeaveEvent(QGraphicsSceneDragDropEvent* event)
×
1402
{
UNCOV
1403
  m_dropping = false;
×
UNCOV
1404
  event->accept();
×
UNCOV
1405
  update();
×
UNCOV
1406
}
×
1407

UNCOV
1408
void NodeItem::dropEvent(QGraphicsSceneDragDropEvent* event)
×
1409
{
UNCOV
1410
  m_dropping = false;
×
UNCOV
1411
  event->accept();
×
UNCOV
1412
  dropReceived(event->pos(), *event->mimeData());
×
UNCOV
1413
  update();
×
UNCOV
1414
}
×
1415
}
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