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

ossia / score / 41569965004

03 Nov 2026 03:04AM UTC coverage: 53.588% (+1.9%) from 51.66%
41569965004

push

github

jcelerier
ci: build the documentation on Ubuntu 24.04, which its deps script targets

ubuntu-latest is now 26.04, where ubuntu.noble.deps.sh's Qt package names
(libqt6websockets6-dev and the like) no longer exist.

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

197270 of 368126 relevant lines covered (53.59%)

304198.31 hits per line

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

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

92
  if(process.flags() & Process::ProcessFlags::FullyCustomItem)
2,637 ✔
93
  {
94
    createWithoutDecorations();
116 ✔
95
  }
116 ✔
96
  else
97
  {
98
    createWithDecorations();
2,521 ✔
99
  }
100

101
  initConnections();
2,637 ✔
102
}
×
103

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

109
  m_folded = process.folded();
2,528 ✔
110

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

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

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

127
  updateLabel();
2,528 ✔
128

129
  con(process.metadata(), &score::ModelMetadata::NameChanged, this,
5,056 ✔
130
      &NodeItem::updateLabel);
2,528 ✔
131
  con(process.metadata(), &score::ModelMetadata::LabelChanged, this,
5,056 ✔
132
      &NodeItem::updateLabel);
2,528 ✔
133

134
  con(process, &Process::ProcessModel::loopsChanged, this, [&] { updateZoomRatio(); });
2,529 ✔
135
  con(process, &Process::ProcessModel::loopDurationChanged, this,
5,056 ✔
136
      [&] { updateZoomRatio(); });
2,529 ✔
137
  con(process, &Process::ProcessModel::startOffsetChanged, this,
5,056 ✔
138
      [&] { updateZoomRatio(); });
2,528 ✔
139

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

144
  // Its presets browsed in the library, loading into it
145
  {
146
    auto& presets = ctx.app.applicationPlugin<Process::ApplicationPlugin>();
2,528 ✔
147
    m_presetTarget = presets.presetTarget() == &process;
2,528 ✔
148
    con(presets, &Process::ApplicationPlugin::presetTargetChanged, this,
5,056 ✔
149
        [this](const Process::ProcessModel* proc) {
2,541 ✔
150
      const bool target = proc == &m_model;
13 ✔
151
      if(target != m_presetTarget)
13 ✔
152
      {
153
        m_presetTarget = target;
13 ✔
154
        update();
13 ✔
155
      }
13 ✔
156
    });
13 ✔
157
  }
158

159
  if(!m_folded)
2,528 ✔
160
    createContentItem();
2,383 ✔
161
  else
162
    createFoldedItem();
145 ✔
163

164
  // This is because if it's a DefaultEffectItem the ports are created asynchronously
165
  // Thus they are added to the vertical lists instead of the actual items
166
  ossia::qt::run_async(this, [this] {
4,603 ✔
167
    resetInlets();
2,075 ✔
168
    resetOutlets();
2,075 ✔
169

170
    resizeAsync();
2,075 ✔
171
  });
2,075 ✔
172
  connect(&process, &Process::ProcessModel::controlAdded, this, &NodeItem::resetInlets);
2,528 ✔
173
  connect(
2,528 ✔
174
      &process, &Process::ProcessModel::controlRemoved, this, &NodeItem::resetInlets);
2,528 ✔
175
  connect(
2,528 ✔
176
      &process, &Process::ProcessModel::controlOutletAdded, this,
2,528 ✔
177
      &NodeItem::resetOutlets);
178
  connect(
2,528 ✔
179
      &process, &Process::ProcessModel::controlOutletRemoved, this,
2,528 ✔
180
      &NodeItem::resetOutlets);
181
  connect(&process, &Process::ProcessModel::inletsChanged, this, &NodeItem::resetInlets);
2,528 ✔
182
  connect(
2,528 ✔
183
      &process, &Process::ProcessModel::outletsChanged, this, &NodeItem::resetOutlets);
2,528 ✔
184

185
  // The button only states the intent; the model is what decides, so that undo,
186
  // reload and a preset dropped on the node all end up drawing the same thing.
187
  connect(m_fold, &score::QGraphicsPixmapToggle::toggled, this, [this](bool b) {
2,528 ✔
188
    CommandDispatcher<> disp{m_context.commandStack};
×
189
    disp.submit<Process::SetNodeFoldMode>(
×
190
        m_model, b ? Process::FoldMode::Unfolded : Process::FoldMode::Folded);
×
191
  });
×
192

193
  con(process, &Process::ProcessModel::foldModeChanged, this,
5,056 ✔
194
      [this] { applyFold(m_model.folded()); });
2,530 ✔
195

196
  resizeAsync();
2,528 ✔
197
  updateTooltip();
2,528 ✔
198
}
2,528 ✔
199

200
void NodeItem::createWithoutDecorations()
116 ✔
201
{
202
  auto& process = m_model;
116 ✔
203

204
  con(process.selection, &Selectable::changed, this, &NodeItem::setSelected);
116 ✔
205
  m_selected = process.selection.get();
116 ✔
206

207
  createContentItem();
116 ✔
208

209
  // The item shows the inlets it wants; the outlets go on the right side
210
  ossia::qt::run_async(this, [this] {
191 ✔
211
    resetOutlets();
75 ✔
212
    resizeAsync();
75 ✔
213
  });
75 ✔
214
  connect(
116 ✔
215
      &process, &Process::ProcessModel::controlOutletAdded, this,
116 ✔
216
      &NodeItem::resetOutlets);
217
  connect(
116 ✔
218
      &process, &Process::ProcessModel::controlOutletRemoved, this,
116 ✔
219
      &NodeItem::resetOutlets);
220
  connect(
116 ✔
221
      &process, &Process::ProcessModel::outletsChanged, this, &NodeItem::resetOutlets);
116 ✔
222

223
  updateTooltip();
116 ✔
224
}
116 ✔
225

226
void NodeItem::updateTooltip()
2,644 ✔
227
{
228
  if(!m_fx || m_fx->toolTip().isEmpty())
2,644 ✔
229
  {
230
    auto& p = this->m_context.app.interfaces<Process::ProcessFactoryList>();
2,585 ✔
231
    const auto& desc = p.get(m_model.concreteKey())->descriptor(m_model);
2,585 ✔
232

233
    bool has_name = !desc.prettyName.isEmpty();
2,585 ✔
234
    bool has_desc = !desc.description.isEmpty();
2,585 ✔
235
    bool has_link = !desc.documentationLink.isEmpty();
2,585 ✔
236
    QString str;
2,585 ✔
237
    if(has_name)
2,585 ✔
238
    {
239
      str += desc.prettyName;
2,585 ✔
240
      str += "\n";
2,585 ✔
241
    }
2,585 ✔
242
    if(has_desc)
2,585 ✔
243
    {
244
      str += desc.description;
2,585 ✔
245
      str += "\n";
2,585 ✔
246
    }
2,585 ✔
247
    if(has_link)
2,585 ✔
248
    {
249
      str += QStringLiteral("Press F1 to open the docs:\n%1")
4,358 ✔
250
                 .arg(desc.documentationLink.toString());
2,179 ✔
251
    }
2,179 ✔
252
    else
253
    {
254
      str += QStringLiteral("Press F1 to open the docs!");
406 ✔
255
    }
256

257
    this->setToolTip(str);
2,585 ✔
258
  }
2,585 ✔
259
  else if(m_fx)
59 ✔
260
  {
261
    this->setToolTip(m_fx->toolTip());
59 ✔
262
  }
59 ✔
263
}
2,644 ✔
264

265
void NodeItem::resetItem()
8 ✔
266
{
267
  qDeleteAll(m_inlets);
8 ✔
268
  m_inlets.clear();
8 ✔
269

270
  qDeleteAll(m_outlets);
8 ✔
271
  m_outlets.clear();
8 ✔
272

273
  delete m_resizeCorner;
8 ✔
274
  m_resizeCorner = nullptr;
8 ✔
275
  if(m_presenter)
8 ✔
276
  {
277
    QPointer<Process::LayerView> oldView = static_cast<Process::LayerView*>(m_fx);
1 ✔
278
    delete m_presenter;
1 ✔
279
    m_presenter = nullptr;
1 ✔
280
    if(oldView)
1 ✔
281
      delete oldView;
1 ✔
282
    m_fx = nullptr;
1 ✔
283
    QObject::disconnect(&m_model, &Process::ProcessModel::setSize, this, nullptr);
1 ✔
284
  }
1 ✔
285
  else
286
  {
287
    delete m_fx;
7 ✔
288
    m_fx = nullptr;
7 ✔
289
  }
290

291
  m_contentSize = QSizeF{minimalContentWidth(), minimalContentHeight()};
8 ✔
292
}
8 ✔
293

294
void NodeItem::resetInlets()
2,410 ✔
295
{
296
  // if(!m_presenter)
297
  //   return;
298
  qDeleteAll(m_inlets);
2,410 ✔
299
  m_inlets.clear();
2,410 ✔
300

301
  // We don't display ports on the left & right side
302
  // for normal items which are supposed to handle it
303
  if(dynamic_cast<Process::DefaultEffectItem*>(m_fx) && !m_presenter)
2,410 ✔
304
    return updateTitlePos();
1,489 ✔
305

306
  const qreal x = InletX0;
921 ✔
307
  qreal y = m_label ? InletY0 : InletY0 - 10.;
921 ✔
308
  auto& portFactory = score::GUIAppContext().interfaces<Process::PortFactoryList>();
921 ✔
309
  for(Process::Inlet* port : m_model.inlets())
14,452 ✔
310
  {
311
    if(port->displayHandledExplicitly)
13,531 ✔
312
      continue;
12,538 ✔
313

314
    Process::PortFactory* fact = portFactory.get(port->concreteKey());
993 ✔
315
    Dataflow::PortItem* item = fact->makePortItem(*port, m_context, this, this);
993 ✔
316
    item->setPos(x, y);
993 ✔
317
    item->setZValue(10);
993 ✔
318
    m_inlets.push_back(item);
993 ✔
319

320
    y += PortSpacing;
993 ✔
321
  }
322

323
  updateTitlePos();
921 ✔
324
}
2,410 ✔
325

326
void NodeItem::resetOutlets()
2,347 ✔
327
{
328
  // if(!m_presenter)
329
  //   return;
330
  qDeleteAll(m_outlets);
2,347 ✔
331
  m_outlets.clear();
2,347 ✔
332

333
  // We don't display ports on the left & right side
334
  // for normal items which are supposed to handle it
335
  if(dynamic_cast<Process::DefaultEffectItem*>(m_fx) && !m_presenter)
2,347 ✔
336
    return updateTitlePos();
1,351 ✔
337

338
  const qreal x = m_contentSize.width() + OutletX0;
996 ✔
339
  // Next to the inlet a fully custom item shows at its top-left
340
  qreal y = m_label ? OutletY0 : 5.;
996 ✔
341

342
  auto& portFactory = score::AppContext().interfaces<Process::PortFactoryList>();
996 ✔
343
  for(Process::Outlet* port : m_model.outlets())
3,021 ✔
344
  {
345
    if(port->displayHandledExplicitly)
2,025 ✔
346
      continue;
21 ✔
347
    Process::PortFactory* fact = portFactory.get(port->concreteKey());
2,004 ✔
348
    auto item = fact->makePortItem(*port, m_context, this, this);
2,004 ✔
349
    item->setPos(x, y);
2,004 ✔
350
    item->setZValue(10);
2,004 ✔
351
    m_outlets.push_back(item);
2,004 ✔
352

353
    y += PortSpacing;
2,004 ✔
354
  }
355
}
2,347 ✔
356

357
void NodeItem::updateLabel()
2,646 ✔
358
{
359
  if(const auto& name = m_model.metadata().getName(); !name.isEmpty())
2,646 ✔
360
  {
361
    m_label->setText(name);
2,646 ✔
362
  }
2,646 ✔
363
  else if(const auto& label = m_model.metadata().getLabel(); !label.isEmpty())
×
364
  {
365
    m_label->setText(label);
×
366
  }
×
367
  else
368
  {
369
    m_label->setText(m_model.prettyShortName());
×
370
  }
371

372
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
2,646 ✔
373
  {
374
    resizeable->setMinimumWidth(minimalContentWidth());
100 ✔
375
  }
100 ✔
376
  QTimer::singleShot(2, this, &NodeItem::updateSize);
2,646 ✔
377
}
2,646 ✔
378

379
void NodeItem::updateTitlePos()
20,682 ✔
380
{
381
  if(!m_label)
20,682 ✔
382
    return;
196 ✔
383

384
  double x0 = FoldX0;
20,486 ✔
385
  if(!m_inlets.empty())
20,486 ✔
386
    x0 += InletX0;
4,721 ✔
387

388
  m_fold->setPos({x0, FoldY0});
20,486 ✔
389
  x0 += UiX0 - FoldX0 + 2.;
20,486 ✔
390

391
  if(m_scriptButton)
20,486 ✔
392
  {
393
    m_scriptButton->setPos({x0, UiY0});
3,178 ✔
394
    x0 += UiX0 + SpacingIcon;
3,178 ✔
395
  }
3,178 ✔
396

397
  if(m_uiButton)
20,486 ✔
398
  {
399
    m_uiButton->setPos({x0, UiY0});
456 ✔
400
    x0 += UiX0 + SpacingIcon;
456 ✔
401
  }
456 ✔
402

403
  if(m_presetButton)
20,486 ✔
404
  {
405
    m_presetButton->setPos({x0, UiY0});
20,486 ✔
406
    x0 += UiX0 + SpacingIcon * 2.;
20,486 ✔
407
  }
20,486 ✔
408

409
  m_label->setPos({x0, TitleY0});
20,486 ✔
410
}
20,682 ✔
411

412
QSizeF NodeItem::size() const noexcept
×
413
{
414
  return m_contentSize;
×
415
}
416

417
void NodeItem::setSelected(bool s)
40 ✔
418
{
419
  m_selected = s;
40 ✔
420
  if(s)
40 ✔
421
    setFocus();
35 ✔
422
  else
423
    clearFocus();
5 ✔
424

425
  update();
40 ✔
426
}
40 ✔
427

428
QRectF NodeItem::contentRect() const noexcept
8,230 ✔
429
{
430
  if(!m_label)
8,230 ✔
431
  {
432
    return {
194 ✔
433
        0, 0, m_contentSize.width() + (hasRightSide() ? RightSideWidth : 0.),
194 ✔
434
        m_contentSize.height() + FooterHeight};
194 ✔
435
  }
436
  else
437
  {
438
    double x = 0;
8,036 ✔
439
    double y = -TitleHeight;
8,036 ✔
440
    double w = m_contentSize.width();
8,036 ✔
441
    const double h = m_contentSize.height() + TitleHeight + FooterHeight;
8,036 ✔
442
    if(!m_inlets.empty())
8,036 ✔
443
    {
444
      x -= LeftSideWidth;
2,997 ✔
445
      w += LeftSideWidth;
2,997 ✔
446
    }
2,997 ✔
447
    if(hasRightSide())
8,036 ✔
448
      w += RightSideWidth;
3,450 ✔
449
    return {x, y, w, h};
8,036 ✔
450
  }
451
}
8,230 ✔
452

453
void NodeItem::updateContentRect()
8,228 ✔
454
{
455
  updateResizeCorner();
8,228 ✔
456
  const QRectF r = contentRect().adjusted(-2., -2., 2., 2.);
8,228 ✔
457
  if(r == m_contentRect)
8,228 ✔
458
    return;
6,182 ✔
459
  m_contentRect = r;
2,046 ✔
460
  geometryChanged();
2,046 ✔
461
}
8,228 ✔
462

463
bool NodeItem::hasRightSide() const noexcept
8,890 ✔
464
{
465
  // A fully custom item lays out the outlets it does not draw itself in that
466
  // strip, possibly before m_outlets is filled
467
  return !m_outlets.empty()
14,031 ✔
468
         || ((m_model.flags() & Process::ProcessFlags::FullyCustomItem)
9,208 ✔
469
             && ossia::any_of(m_model.outlets(), [](const Process::Outlet* o) {
5,331 ✔
470
                  return !o->displayHandledExplicitly;
190 ✔
471
                }));
472
}
473

474
class NodeResizeCorner final : public QGraphicsItem
475
{
476
public:
477
  explicit NodeResizeCorner(NodeItem& node)
159 ✔
478
      : QGraphicsItem{&node}
159 ✔
479
      , m_node{node}
159 ✔
480
  {
159 ✔
481
    setAcceptedMouseButtons(Qt::LeftButton);
159 ✔
482
    setZValue(1000.);
159 ✔
483
    setCursor(score::Skin::instance().CursorScaleFDiag);
159 ✔
484
  }
159 ✔
485

486
  QRectF boundingRect() const override { return {-10., -10., 10., 10.}; }
170 ✔
487
  void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*) override
19 ✔
488
  {
489
    p->setPen(Process::Style::instance().IntervalWarning().main.pen0_solid_round);
19 ✔
490
    p->drawLine(QPointF{-6., -2.}, QPointF{-2., -2.});
19 ✔
491
    p->drawLine(QPointF{-2., -6.}, QPointF{-2., -2.});
19 ✔
492
  }
19 ✔
493

494
protected:
495
  // The node does the resizing, as from its own corner
496
  void mousePressEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
497
  {
498
    ev->setPos(mapToParent(ev->pos()));
2 ✔
499
    m_node.m_resizeFromCorner = true;
2 ✔
500
    m_node.mousePressEvent(ev);
2 ✔
501
    m_node.m_resizeFromCorner = false;
2 ✔
502
  }
2 ✔
503
  void mouseMoveEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
504
  {
505
    ev->setPos(mapToParent(ev->pos()));
2 ✔
506
    m_node.mouseMoveEvent(ev);
2 ✔
507
  }
2 ✔
508
  void mouseReleaseEvent(QGraphicsSceneMouseEvent* ev) override
1 ✔
509
  {
510
    ev->setPos(mapToParent(ev->pos()));
1 ✔
511
    m_node.mouseReleaseEvent(ev);
1 ✔
512
  }
1 ✔
513
  // The corner holds the grab: a release the scene drops must still end the
514
  // node's gesture
515
  bool sceneEvent(QEvent* ev) override
9 ✔
516
  {
517
    if(ev->type() == QEvent::UngrabMouse)
9 ✔
518
      m_node.sceneEvent(ev);
2 ✔
519
    return QGraphicsItem::sceneEvent(ev);
9 ✔
520
  }
521

522
private:
523
  NodeItem& m_node;
524
};
525

526
void NodeItem::updateResizeCorner()
8,228 ✔
527
{
528
  const bool wanted = m_presenter && !hasRightSide();
8,228 ✔
529
  if(!wanted)
8,228 ✔
530
  {
531
    delete m_resizeCorner;
7,912 ✔
532
    m_resizeCorner = nullptr;
7,912 ✔
533
    return;
7,912 ✔
534
  }
535
  if(!m_resizeCorner)
316 ✔
536
    m_resizeCorner = new NodeResizeCorner{*this};
159 ✔
537
  m_resizeCorner->setPos(m_contentSize.width(), m_contentSize.height());
316 ✔
538
}
8,228 ✔
539

540
QRectF NodeItem::boundingRect() const
183,566 ✔
541
{
542
  return m_contentRect.adjusted(-2., -2., 2., 2.);
183,566 ✔
543
}
544

545
//! Redraw for a fold state that has already been decided by the model.
546
void NodeItem::applyFold(bool folded)
2 ✔
547
{
548
  if(!m_fold || folded == m_folded)
2 ✔
549
    return;
1 ✔
550

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

555
  resetItem();
1 ✔
556

557
  m_folded = folded;
1 ✔
558
  if(!m_folded)
1 ✔
559
  {
560
    createContentItem();
×
561
  }
×
562
  else
563
  {
564
    createFoldedItem();
1 ✔
565
  }
566

567
  QTimer::singleShot(1, this, [this] {
2 ✔
568
    resetInlets();
1 ✔
569
    resetOutlets();
1 ✔
570

571
    resizeAsync();
1 ✔
572
  });
1 ✔
573
}
2 ✔
574

575
void NodeItem::createFoldedItem()
146 ✔
576
{
577
  if(m_fx)
146 ✔
578
    return;
×
579

580
  auto& ctx = m_context;
146 ✔
581
  auto& model = m_model;
146 ✔
582
  // Body
583
  score::ResizeableItem* resizeable{};
146 ✔
584
  {
585
    auto fx = new Process::DefaultEffectItem{true, model, ctx, this};
146 ✔
586
    m_fx = fx;
146 ✔
587
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
146 ✔
588
    resizeable = fx;
146 ✔
589
  }
590
  setupItem(resizeable);
146 ✔
591
}
146 ✔
592

593
void NodeItem::createContentItem()
2,499 ✔
594
{
595
  if(m_fx)
2,499 ✔
596
    return;
×
597

598
  auto& ctx = m_context;
2,499 ✔
599
  auto& model = m_model;
2,499 ✔
600
  // Body
601
  auto& fact = ctx.app.interfaces<Process::LayerFactoryList>();
2,499 ✔
602
  score::ResizeableItem* resizeable{};
2,499 ✔
603
  if(auto factory = fact.findDefaultFactory(model))
2,499 ✔
604
  {
605
    if(auto fx = factory->makeItem(model, ctx, this))
1,519 ✔
606
    {
607
      m_fx = fx;
1,294 ✔
608
      m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
1,294 ✔
609
      resizeable = fx;
1,294 ✔
610
    }
1,294 ✔
611
    else if(auto fx = factory->makeLayerView(model, ctx, this))
225 ✔
612
    {
613
      m_fx = fx;
225 ✔
614
      m_presenter = factory->makeLayerPresenter(model, fx, ctx, this);
225 ✔
615
      m_contentSize = m_model.size();
225 ✔
616
      m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
225 ✔
617
      m_presenter->setHeight(m_contentSize.height());
225 ✔
618

619
      // TODO review the resizing heuristic...
620
      if(m_presenter)
225 ✔
621
      {
622
        ::bind(model, Process::ProcessModel::p_size{}, this, [this](QSizeF s) {
456 ✔
623
          if(s != m_contentSize)
231 ✔
624
            setSize(s);
5 ✔
625
        });
231 ✔
626
          connect(m_presenter, &Process::LayerPresenter::contextMenuRequested, this, [this] (QPoint pos, QPointF sp) {
225 ✔
627
              auto menu = new QMenu;
×
628
              auto& reg = score::GUIAppContext()
×
629
                              .applicationPlugin<Process::ApplicationPlugin>()
×
630
                              .layerContextMenuRegistrar();
×
631

632
              m_presenter->fillContextMenu(*menu, pos, sp, reg);
×
633
              if(menu->actions().size() > 0) {
×
634
                menu->exec(pos);
×
635
                menu->close();
×
636
              }
×
637

638
              menu->deleteLater();
×
639
          }, Qt::QueuedConnection);
×
640
      }
225 ✔
641
    }
225 ✔
642
  }
1,519 ✔
643

644
  if(!m_fx)
2,499 ✔
645
  {
646
    auto fx = new Process::DefaultEffectItem{false, model, ctx, this};
980 ✔
647
    m_fx = fx;
980 ✔
648
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
980 ✔
649
    resizeable = fx;
980 ✔
650
  }
980 ✔
651

652
  setupItem(resizeable);
2,499 ✔
653
}
2,499 ✔
654

655
void NodeItem::resizeAsync()
8,971 ✔
656
{
657
  updateTitlePos();
8,971 ✔
658
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
8,971 ✔
659
  {
660
    resizeable->setMinimumWidth(minimalContentWidth());
8,470 ✔
661
    // Needed to leave some time for defaulteffectitem
662
    QTimer::singleShot(2, this, [this] {
14,353 ✔
663
      updateSize();
5,883 ✔
664
      updateZoomRatio();
5,883 ✔
665
    });
5,883 ✔
666
  }
8,470 ✔
667
  else
668
  {
669
    QMetaObject::invokeMethod(this, [this] {
1,002 ✔
670
      updateSize();
501 ✔
671
      updateZoomRatio();
501 ✔
672
    });
501 ✔
673
  }
674
}
8,971 ✔
675

676
void NodeItem::recreate()
7 ✔
677
{
678
  resetItem();
7 ✔
679
  qDeleteAll(this->childItems());
7 ✔
680
  m_resizeCorner = nullptr;
7 ✔
681
  m_scriptButton = nullptr;
7 ✔
682
  m_uiButton = nullptr;
7 ✔
683
  m_presetButton = nullptr;
7 ✔
684
  m_label = nullptr;
7 ✔
685
  m_fold = nullptr;
7 ✔
686
  disconnect(&this->m_model, nullptr, this, nullptr);
7 ✔
687
  disconnect(&this->m_model.selection, nullptr, this, nullptr);
7 ✔
688
  disconnect(&this->m_model.metadata(), nullptr, this, nullptr);
7 ✔
689

690
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
7 ✔
691
  {
692
    createWithoutDecorations();
×
693
  }
×
694
  else
695
  {
696
    createWithDecorations();
7 ✔
697
  }
698

699
  initConnections();
7 ✔
700
}
7 ✔
701

702
void NodeItem::setupItem(score::ResizeableItem* resizeable)
2,645 ✔
703
{
704
  // Positions / size
705
  double w = std::max(minimalContentWidth(), m_contentSize.width());
2,645 ✔
706
  double h = std::max(minimalContentHeight(), m_contentSize.height());
2,645 ✔
707
  m_contentSize = QSizeF{w, h};
2,645 ✔
708

709
  resizeAsync();
2,645 ✔
710
  publishSize();
2,645 ✔
711

712
  if(resizeable)
2,645 ✔
713
  {
714
    if(auto lay = dynamic_cast<score::GraphicsLayout*>(resizeable))
2,420 ✔
715
      connect(lay, &score::ResizeableItem::childrenSizeChanged, this, [this, lay] {
1,083 ✔
716
        lay->layout();
206 ✔
717
        lay->fitChildrenRect();
206 ✔
718
        m_contentSize = lay->rect().size();
206 ✔
719

720
        resizeAsync();
206 ✔
721
      });
206 ✔
722
    connect(resizeable, &score::ResizeableItem::sizeChanged, this, [this](QSizeF sz) {
3,650 ✔
723
      double w = std::max(minimalContentWidth(), sz.width());
1,230 ✔
724
      double h = std::max(minimalContentHeight(), sz.height());
1,230 ✔
725
      m_contentSize = QSizeF{w, h};
1,230 ✔
726

727
      resizeAsync();
1,230 ✔
728
    });
1,230 ✔
729
  }
2,420 ✔
730
}
2,645 ✔
731

732
double NodeItem::minimalContentWidth() const noexcept
20,595 ✔
733
{
734
  if(Q_UNLIKELY(!m_label))
20,595 ✔
735
  {
736
    if((m_inlets.size() + m_outlets.size()) <= 1)
318 ✔
737
      return 10.;
302 ✔
738
    else
739
      return 20.;
16 ✔
740
  }
741
  else
742
  {
743
    return std::max(75.0, m_label->pos().x() + m_label->boundingRect().width() + 6.);
20,277 ✔
744
  }
745
}
20,595 ✔
746
double NodeItem::minimalContentHeight() const noexcept
12,025 ✔
747
{
748
  if(Q_UNLIKELY(!m_label))
12,025 ✔
749
    if(std::max(m_inlets.size(), m_outlets.size()) <= 1)
308 ✔
750
      return 10.;
292 ✔
751

752
  double h = 22.;
11,733 ✔
753
  if(!m_inlets.empty())
11,733 ✔
754
    h = std::max(h, m_inlets.back()->y() + 10.);
2,997 ✔
755
  if(!m_outlets.empty())
11,733 ✔
756
    h = std::max(h, m_outlets.back()->y() + 10.);
3,464 ✔
757
  return h;
11,733 ✔
758
}
12,025 ✔
759

760
void NodeItem::updateSize()
8,139 ✔
761
{
762
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
8,139 ✔
763
  {
764
    if(m_presenter)
189 ✔
765
      resizePresenter();
184 ✔
766
    // A fully-custom item lays out its own inlets, so the node has to be at
767
    // least as wide as that item: keeping the width the title asked for draws
768
    // the border inside the item, and the outlets end up inside it. Short
769
    // titles are where this shows, since that is when the title is not what
770
    // decides the width.
771
    QSizeF sz{minimalContentWidth(), minimalContentHeight()};
189 ✔
772
    if(m_fx)
189 ✔
773
    {
774
      auto fx_sz = m_fx->boundingRect().size();
189 ✔
775
      sz.rwidth() = std::max(sz.width(), fx_sz.width());
189 ✔
776
      sz.rheight() = std::max(sz.height(), fx_sz.height());
189 ✔
777
    }
189 ✔
778
    if(sz != m_contentSize)
189 ✔
779
    {
780
      prepareGeometryChange();
×
781
      m_contentSize = sz;
×
782
      publishSize();
×
783
    }
×
784
    for(auto& outlet : m_outlets)
261 ✔
785
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
72 ✔
786
    updateContentRect();
189 ✔
787
    update();
189 ✔
788
    if(auto sc = this->scene())
189 ✔
789
      sc->update();
189 ✔
790
    return;
189 ✔
791
  }
792

793
  if(!m_label)
7,950 ✔
794
  {
795
    if(auto sc = this->scene())
×
796
      sc->update();
×
797
    return;
×
798
  }
799
  QSizeF sz{minimalContentWidth(), minimalContentHeight()};
7,950 ✔
800
  if(m_fx)
7,950 ✔
801
  {
802
    auto fx_sz = m_fx->boundingRect().size();
7,950 ✔
803
    sz.rwidth() = std::max(sz.width(), fx_sz.width());
7,950 ✔
804
    sz.rheight() = std::max(sz.height(), fx_sz.height());
7,950 ✔
805
  }
7,950 ✔
806
  //if (sz != m_contentSize || !m_fx)
807
  {
808
    prepareGeometryChange();
7,950 ✔
809
    if(m_fx)
7,950 ✔
810
    {
811
      m_contentSize = sz;
7,950 ✔
812
      if(!m_presenter)
7,950 ✔
813
      {
814
        if(m_model.outlets().size() > 0 && m_model.inlets().size() == 0)
7,536 ✔
815
        {
816
          // Align right for the case where we only have a series of outlets
817
          updateContentRect();
84 ✔
818
          m_fx->setPos(m_contentRect.width() - m_fx->boundingRect().width(), 0);
84 ✔
819
        }
84 ✔
820
        else
821
        {
822
          m_fx->setPos(0, 0);
7,452 ✔
823
        }
824
      }
7,536 ✔
825
    }
7,950 ✔
826

827
    if(m_presenter)
7,950 ✔
828
      resizePresenter();
414 ✔
829

830
    if(m_scriptButton)
7,950 ✔
831
    {
832
      m_scriptButton->setParentItem(nullptr);
1,057 ✔
833
    }
1,057 ✔
834
    if(m_uiButton)
7,950 ✔
835
    {
836
      m_uiButton->setParentItem(nullptr);
200 ✔
837
    }
200 ✔
838

839
    for(auto& outlet : m_outlets)
15,614 ✔
840
    {
841
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
7,664 ✔
842
    }
843

844
    if(m_scriptButton)
7,950 ✔
845
    {
846
      m_scriptButton->setParentItem(this);
1,057 ✔
847
    }
1,057 ✔
848
    if(m_uiButton)
7,950 ✔
849
    {
850
      m_uiButton->setParentItem(this);
200 ✔
851
    }
200 ✔
852
    updateTitlePos();
7,950 ✔
853
    updateContentRect();
7,950 ✔
854
    publishSize();
7,950 ✔
855
    update();
7,950 ✔
856
    if(auto sc = this->scene())
7,950 ✔
857
      sc->update();
7,950 ✔
858
  }
859
}
8,139 ✔
860

861
void NodeItem::publishSize()
10,595 ✔
862
{
863
  // Node placement (e.g. creating a process after another one) reads the size
864
  // off the model: keep it in sync with what is actually drawn. A folded node
865
  // must not overwrite the unfolded size it will go back to.
866
  if(m_folded)
10,595 ✔
867
    return;
775 ✔
868
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
9,820 ✔
869
    return;
116 ✔
870
  if(m_model.size() == m_contentSize)
9,704 ✔
871
    return;
7,657 ✔
872

873
  const_cast<Process::ProcessModel&>(m_model).setSize(m_contentSize);
2,047 ✔
874
}
10,595 ✔
875

876
void NodeItem::setSize(QSizeF sz)
5 ✔
877
{
878
  if(m_presenter)
5 ✔
879
  {
880
    // TODO: find a way to indicate to a model what will be the size of the
881
    // item
882
    //      - maybe set it without a command in that case ?
883
    prepareGeometryChange();
5 ✔
884
    m_contentSize = sz;
5 ✔
885

886
    if(m_scriptButton)
5 ✔
887
    {
888
      m_scriptButton->setParentItem(nullptr);
×
889
    }
×
890
    if(m_uiButton)
5 ✔
891
    {
892
      m_uiButton->setParentItem(nullptr);
×
893
    }
×
894

895
    resizePresenter();
5 ✔
896

897
    resetInlets();
5 ✔
898
    resetOutlets();
5 ✔
899
    auto cur_x = m_contentSize.width() + TopButtonX0;
5 ✔
900
    if(m_scriptButton)
5 ✔
901
    {
902
      m_scriptButton->setParentItem(this);
×
903
      m_scriptButton->setPos({cur_x, TopButtonY0});
×
904
      cur_x += UiX0 + SpacingIcon;
×
905
    }
×
906
    if(m_uiButton)
5 ✔
907
    {
908
      m_uiButton->setParentItem(this);
×
909
      m_uiButton->setPos({cur_x, TopButtonY0});
×
910
    }
×
911
  }
5 ✔
912
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
5 ✔
913
  {
914
    resizeable->setMinimumWidth(sz.width());
×
915
  }
×
916
  updateContentRect();
5 ✔
917
  update();
5 ✔
918
  if(auto sc = this->scene())
5 ✔
919
    sc->update();
5 ✔
920
}
5 ✔
921

922
const Id<Process::ProcessModel>& NodeItem::id() const noexcept
6 ✔
923
{
924
  return m_model.id();
6 ✔
925
}
926

927
NodeItem::~NodeItem()
5,274 ✔
928
{
5,274 ✔
929
  clearDropCableHighlight();
2,637 ✔
930
  delete m_presenter;
2,637 ✔
931
}
5,274 ✔
932

933
void NodeItem::setParentDuration(TimeVal r)
5 ✔
934
{
935
  if(r != m_parentDuration)
5 ✔
936
  {
937
    m_parentDuration = r;
×
938
    updateZoomRatio();
×
939
  }
×
940
}
5 ✔
941

942
void NodeItem::setPlayPercentage(float f, TimeVal parent_dur)
1,216 ✔
943
{
944
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
1,216 ✔
945
    return;
60 ✔
946

947
  if(m_playPercentage != f)
1,156 ✔
948
  {
949
    // Comes from the interval -> if we are looping we make a small modulo of it
950
    if(m_model.loops() && m_model.loopDuration().impl > 0)
×
951
    {
952
      auto loopDur = m_model.loopDuration().impl;
×
953
      double playdur = double(f) * parent_dur.impl;
×
954
      f = std::fmod(playdur, loopDur) / loopDur;
×
955
      if(m_playPercentage != f)
×
956
      {
957
        m_playPercentage = f;
×
958
        update({12., -1., (m_contentSize.width() - 24.), 3.});
×
959
      }
×
960
    }
×
961
    else
962
    {
963
      f = ossia::clamp(f, 0.f, 1.f);
×
964
      if(m_playPercentage != f)
×
965
      {
966
        m_playPercentage = f;
×
967
        update({12., -1., (m_contentSize.width() - 24.) * f + 1., 3.});
×
968
      }
×
969
    }
970
  }
×
971
}
1,216 ✔
972

973
qreal NodeItem::width() const noexcept
×
974
{
975
  return m_contentSize.width();
×
976
}
977

978
qreal NodeItem::height() const
×
979
{
980
  return TitleHeight + m_contentSize.height() + FooterHeight;
×
981
}
982

983
void NodeItem::initConnections()
2,644 ✔
984
{
985
  auto& process = this->model();
2,644 ✔
986
  ::bind(process, Process::ProcessModel::p_position{}, this, [this](QPointF p) {
5,295 ✔
987
    if(p != pos())
2,651 ✔
988
      setPos(p);
2,641 ✔
989
  });
2,651 ✔
990

991
  auto on_sizeChanged = [this] {
3,164 ✔
992
    m_needResize = true;
520 ✔
993
    QTimer::singleShot(1, this, [this] {
939 ✔
994
      if(!m_needResize)
419 ✔
995
        return;
208 ✔
996
      m_needResize = false;
211 ✔
997
      resizeAsync();
211 ✔
998
    });
419 ✔
999
  };
520 ✔
1000
  connect(&process, &Process::ProcessModel::controlAdded, this, on_sizeChanged);
2,644 ✔
1001
  connect(&process, &Process::ProcessModel::controlRemoved, this, on_sizeChanged);
2,644 ✔
1002
  connect(&process, &Process::ProcessModel::controlOutletAdded, this, on_sizeChanged);
2,644 ✔
1003
  connect(&process, &Process::ProcessModel::controlOutletRemoved, this, on_sizeChanged);
2,644 ✔
1004
  connect(&process, &Process::ProcessModel::inletsChanged, this, on_sizeChanged);
2,644 ✔
1005
  connect(&process, &Process::ProcessModel::outletsChanged, this, on_sizeChanged);
2,644 ✔
1006
  connect(&process, &Process::ProcessModel::flagsChanged, this, &NodeItem::recreate);
2,644 ✔
1007
}
2,644 ✔
1008

1009
const ProcessModel& NodeItem::model() const noexcept
11,641 ✔
1010
{
1011
  return m_model;
11,641 ✔
1012
}
1013

1014
void NodeItem::resizePresenter()
603 ✔
1015
{
1016
  m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
603 ✔
1017
  m_presenter->setHeight(m_contentSize.height());
603 ✔
1018
  updateZoomRatio();
603 ✔
1019
}
603 ✔
1020

1021
void NodeItem::updateZoomRatio() const noexcept
6,989 ✔
1022
{
1023
  const auto dur = m_model.loops() ? m_model.loopDuration() : m_parentDuration;
6,989 ✔
1024
  if(m_presenter)
6,989 ✔
1025
  {
1026
    m_presenter->on_zoomRatioChanged(dur.impl / m_contentSize.width());
1,106 ✔
1027
    // TODO investigate why this is necessary for scenario:
1028
    m_presenter->parentGeometryChanged();
1,106 ✔
1029
  }
1,106 ✔
1030
}
6,989 ✔
1031

1032
bool NodeItem::isInSelectionCorner(QPointF p, QRectF r) const
3 ✔
1033
{
1034
  return (p.x() - r.x()) > (r.width() - 10.) && (p.y() - r.y()) > (r.height() - 10.);
3 ✔
1035
}
1036

1037
void NodeItem::paint(
464 ✔
1038
    QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
1039
{
1040
  auto& style = Process::Style::instance();
464 ✔
1041
  const auto& skin = style.skin;
464 ✔
1042
  const auto rect = m_contentRect;
464 ✔
1043
  //painter->fillRect(boundingRect(), Qt::red);
1044
  //return;
1045
  const auto flags = this->m_model.flags();
464 ✔
1046

1047
  const auto& bset = skin.Emphasis5;
464 ✔
1048
  const auto& fillbrush = !(flags & Process::ProcessFlags::NodeHasNoBackground)
464 ✔
1049
                              ? skin.Emphasis5
463 ✔
1050
                              : skin.TransparentBrush;
1 ✔
1051
  const auto& brush = m_selected ? skin.Base2.darker
928 ✔
1052
                      : m_hover  ? bset.lighter
464 ✔
1053
                                 : bset.main;
464 ✔
1054
  // Dashed while its presets load into it from the library
1055
  const auto& pen = m_dropping       ? skin.Warn2.main.pen3_solid_round_round
928 ✔
1056
                    : m_presetTarget ? skin.Base2.darker.pen3_dashed_flat_miter
464 ✔
1057
                                     : brush.pen2_solid_round_round;
464 ✔
1058

1059
  painter->setRenderHint(QPainter::Antialiasing, true);
464 ✔
1060

1061
  // Body
1062
  painter->setPen(pen);
464 ✔
1063

1064
#if defined(SCORE_DEBUG_REDRAWS)
1065
  {
1066
    QColor c;
1067
    c.setRedF(0.5 * double(rand()) / RAND_MAX);
1068
    c.setGreenF(0.5 * double(rand()) / RAND_MAX);
1069
    c.setBlueF(0.5 * double(rand()) / RAND_MAX);
1070
    c.setAlphaF(1.);
1071
    painter->setBrush(c);
1072
  }
1073
#else
1074
  painter->setBrush(fillbrush);
464 ✔
1075
#endif
1076

1077
  painter->drawRoundedRect(rect, Corner, Corner);
464 ✔
1078

1079
  painter->setRenderHint(QPainter::Antialiasing, false);
464 ✔
1080

1081
  // Exec
1082
  if(!(flags & Process::ProcessFlags::FullyCustomItem))
464 ✔
1083
    if(m_playPercentage > 0.)
427 ✔
1084
    {
1085
      painter->setPen(style.IntervalPlayFill().main.pen1_solid_flat_miter);
×
1086
      painter->drawLine(
×
1087
          QPointF{12., 0.}, QPointF{12. + (width() - 24.) * m_playPercentage, 0.});
×
1088
    }
×
1089

1090
  // Resizing handle, in the right strip; NodeResizeCorner draws it otherwise
1091
  if(m_presenter && hasRightSide())
464 ✔
1092
  {
1093
    const auto h = m_contentSize.height();
38 ✔
1094
    const auto w = m_contentSize.width() + RightSideWidth;
38 ✔
1095
    painter->setPen(style.IntervalWarning().main.pen0_solid_round);
38 ✔
1096
    double start_x = w - 6.;
38 ✔
1097
    double start_y = h - 6.;
38 ✔
1098
    double center_x = w - 2.;
38 ✔
1099
    double center_y = h - 2.;
38 ✔
1100
    painter->drawLine(start_x, center_y, center_x, center_y);
38 ✔
1101
    painter->drawLine(center_x, start_y, center_x, center_y);
38 ✔
1102
  }
38 ✔
1103
}
464 ✔
1104

1105
namespace
1106
{
1107
enum Interaction
1108
{
1109
  None,
1110
  Move,
1111
  Resize
1112
} nodeItemInteraction{};
1113
QSizeF origNodeSize{};
1114
bool nodeDidMove{};
1115
bool nodeSelectOnRelease{};
1116
bool nodeSelectCumulation{};
1117
}
1118

1119
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
6 ✔
1120
{
1121
  nodeItemInteraction = Interaction::None;
6 ✔
1122
  if(event->button() == Qt::LeftButton)
6 ✔
1123
  {
1124
    nodeDidMove = false;
6 ✔
1125
    if(m_presenter
6 ✔
1126
       && (isInSelectionCorner(event->pos(), m_contentRect)
6 ✔
1127
           || m_resizeFromCorner))
2 ✔
1128
    {
1129
      nodeItemInteraction = Interaction::Resize;
2 ✔
1130
      origNodeSize = m_model.size();
2 ✔
1131
    }
2 ✔
1132
    else
1133
    {
1134
      nodeItemInteraction = Interaction::Move;
4 ✔
1135
    }
1136

1137
    if(m_presenter && m_model.flags() & Process::ProcessFlags::ItemRequiresUniqueFocus)
6 ✔
1138
    {
1139
      m_context.focusDispatcher.focus(m_presenter);
×
1140
    }
×
1141
    {
1142
      const bool cumulation = event->modifiers() & Qt::ControlModifier;
6 ✔
1143

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

1197
  const Process::Cable* res{};
×
1198
  for(auto item : sc->items(node.mapToScene(contentRect.center())))
×
1199
  {
1200
    if(item->type() != Dataflow::CableItem::Type)
×
1201
      continue;
×
1202
    if(res)
×
1203
      return nullptr;
×
1204
    res = &static_cast<Dataflow::CableItem*>(item)->model();
×
1205
  }
1206
  return res;
×
1207
}
×
1208

1209
void NodeItem::updateDropCableHighlight()
5 ✔
1210
{
1211
  Dataflow::CableItem* next{};
5 ✔
1212
  if(canDropOnCableHandler)
5 ✔
1213
  {
1214
    if(auto* cbl = cableUnder(*this, m_contentRect))
×
1215
    {
1216
      if(canDropOnCableHandler(m_model, *cbl))
×
1217
      {
1218
        auto& cables = m_context.dataflow.cables();
×
1219
        if(auto it = cables.find(cbl); it != cables.end())
×
1220
          next = it->second;
×
1221
      }
×
1222
    }
×
1223
  }
×
1224

1225
  if(next == m_dropCable)
5 ✔
1226
    return;
5 ✔
1227
  if(m_dropCable)
×
1228
    m_dropCable->setDropTarget(false);
×
1229
  m_dropCable = next;
×
1230
  if(m_dropCable)
×
1231
    m_dropCable->setDropTarget(true);
×
1232
}
5 ✔
1233

1234
void NodeItem::clearDropCableHighlight()
2,645 ✔
1235
{
1236
  if(m_dropCable)
2,645 ✔
1237
    m_dropCable->setDropTarget(false);
×
1238
  m_dropCable = nullptr;
2,645 ✔
1239
}
2,645 ✔
1240

1241
void NodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
9 ✔
1242
{
1243
  if(nodeItemInteraction == Interaction::None)
9 ✔
1244
    return QGraphicsItem::mouseMoveEvent(event);
×
1245

1246
  auto parent = this->parentItem();
9 ✔
1247
  auto origp = parent->mapFromScene(event->buttonDownScenePos(Qt::LeftButton));
9 ✔
1248
  auto p = parent->mapFromScene(event->scenePos());
9 ✔
1249
  if(p != origp)
9 ✔
1250
    nodeDidMove = true;
8 ✔
1251

1252
  if(nodeDidMove)
9 ✔
1253
  {
1254
    switch(nodeItemInteraction)
8 ✔
1255
    {
1256
      case Interaction::Resize: {
1257
        const auto sz = origNodeSize + QSizeF{p.x() - origp.x(), p.y() - origp.y()};
3 ✔
1258
        m_dispatcher.submit<Process::ResizeNode>(
6 ✔
1259
            m_model, sz.expandedTo({minimalContentWidth(), minimalContentHeight()}));
3 ✔
1260
        updateSize();
3 ✔
1261
        break;
3 ✔
1262
      }
1263
      case Interaction::Move: {
1264
        auto procs = selectedProcesses(m_context);
5 ✔
1265
        // The press put this node in the selection, so it is normally already
1266
        // in here; a selection that somehow does not hold it would turn the
1267
        // whole drag into a silent no-op rather than moving the node under the
1268
        // mouse.
1269
        if(!ossia::contains(procs, &m_model))
5 ✔
1270
          procs.push_back(&m_model);
×
1271
        m_dispatcher.submit<Process::MoveNodes>(std::move(procs), p - origp);
5 ✔
1272
        updateDropCableHighlight();
5 ✔
1273
        break;
1274
      }
5 ✔
1275
      default:
1276
        break;
×
1277
    }
1278
  }
8 ✔
1279
  event->accept();
9 ✔
1280
}
9 ✔
1281

1282

1283
void NodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
4 ✔
1284
{
1285
  if(nodeItemInteraction == Interaction::None)
4 ✔
1286
    return QGraphicsItem::mouseReleaseEvent(event);
×
1287

1288
  mouseMoveEvent(event);
4 ✔
1289

1290
  clearDropCableHighlight();
4 ✔
1291

1292
  if(nodeDidMove)
4 ✔
1293
  {
1294
    // Part of the same macro as the move: dropping a node on a cable is a
1295
    // single gesture and must undo as one.
1296
    if(dropOnCableHandler && nodeItemInteraction == Interaction::Move)
3 ✔
1297
    {
1298
      if(auto cbl = cableUnder(*this, m_contentRect);
×
1299
         cbl && (!canDropOnCableHandler || canDropOnCableHandler(m_model, *cbl)))
×
1300
      {
1301
        score::Dispatcher_T<MultiOngoingCommandDispatcher> disp{m_dispatcher};
×
1302
        dropOnCableHandler(m_model, *cbl, disp);
×
1303
      }
×
1304
    }
×
1305
    m_dispatcher.commit<Process::MoveNodesMacro>();
3 ✔
1306
  }
3 ✔
1307
  else if(nodeSelectOnRelease)
1 ✔
1308
  {
1309
    Selection sel = filterSelections(
×
1310
        &m_model, m_context.selectionStack.currentSelection(), nodeSelectCumulation);
×
1311
    score::SelectionDispatcher{m_context.selectionStack}.select(sel);
×
1312
  }
×
1313
  nodeSelectOnRelease = false;
4 ✔
1314
  nodeDidMove = false;
4 ✔
1315
  event->accept();
4 ✔
1316
}
4 ✔
1317

1318
//! QEvent::UngrabMouse: the scene took the implicit grab away and there will be
1319
//! no release to end the drag on -- most visibly when the button was let go
1320
//! outside the window and the pointer then comes back in. Without this the
1321
//! MoveNodes command stays open, and the next drag of this node updates *it*
1322
//! instead of starting fresh, moving the node relative to the previous press.
1323
bool NodeItem::sceneEvent(QEvent* event)
9,000 ✔
1324
{
1325
  if(event->type() == QEvent::UngrabMouse)
9,000 ✔
1326
  {
1327
    if(nodeItemInteraction != Interaction::None)
4 ✔
1328
    {
1329
      clearDropCableHighlight();
4 ✔
1330
      if(nodeDidMove)
4 ✔
1331
        m_dispatcher.commit<Process::MoveNodesMacro>();
2 ✔
1332

1333
      nodeItemInteraction = Interaction::None;
4 ✔
1334
      nodeSelectOnRelease = false;
4 ✔
1335
      nodeDidMove = false;
4 ✔
1336
    }
4 ✔
1337
  }
4 ✔
1338
  return QGraphicsItem::sceneEvent(event);
9,000 ✔
1339
}
1340

1341
void NodeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
×
1342
{
1343
  event->accept();
×
1344
}
×
1345

1346
void NodeItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
122 ✔
1347
{
1348
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
122 ✔
1349
  {
1350
    auto& skin = score::Skin::instance();
1 ✔
1351
    setCursor(skin.CursorScaleFDiag);
1 ✔
1352
  }
1 ✔
1353
  else
1354
  {
1355
    unsetCursor();
121 ✔
1356
  }
1357

1358
  m_hover = true;
122 ✔
1359
  update();
122 ✔
1360
  event->accept();
122 ✔
1361
}
122 ✔
1362

1363
void NodeItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
8,744 ✔
1364
{
1365
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
8,744 ✔
1366
  {
1367
    auto& skin = score::Skin::instance();
×
1368
    setCursor(skin.CursorScaleFDiag);
×
1369
  }
×
1370
  else
1371
  {
1372
    unsetCursor();
8,744 ✔
1373
  }
1374
  event->accept();
8,744 ✔
1375
}
8,744 ✔
1376

1377
void NodeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
119 ✔
1378
{
1379
  unsetCursor();
119 ✔
1380

1381
  m_hover = false;
119 ✔
1382
  update();
119 ✔
1383
  event->accept();
119 ✔
1384
}
119 ✔
1385

1386
void NodeItem::keyPressEvent(QKeyEvent* event)
×
1387
{
1388
  if(event->key() == Qt::Key_Down)
×
1389
  {
1390
    auto ports = Dataflow::getProcessPorts(this->m_model);
×
1391
    if(!ports.empty())
×
1392
    {
1393
      score::SelectionDispatcher disp{m_context.selectionStack};
×
1394
      disp.select(*ports.front());
×
1395
    }
×
1396
  }
×
1397
  event->accept();
×
1398
}
×
1399

1400
void NodeItem::keyReleaseEvent(QKeyEvent* event)
×
1401
{
1402
  event->accept();
×
1403
}
×
1404
void NodeItem::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
×
1405
{
1406
  m_dropping = true;
×
1407
  event->accept();
×
1408
  update();
×
1409
}
×
1410

1411
void NodeItem::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
×
1412
{
1413
  m_dropping = true;
×
1414
  event->accept();
×
1415
  update();
×
1416
}
×
1417

1418
void NodeItem::dragLeaveEvent(QGraphicsSceneDragDropEvent* event)
×
1419
{
1420
  m_dropping = false;
×
1421
  event->accept();
×
1422
  update();
×
1423
}
×
1424

1425
void NodeItem::dropEvent(QGraphicsSceneDragDropEvent* event)
×
1426
{
1427
  m_dropping = false;
×
1428
  event->accept();
×
1429
  dropReceived(event->pos(), *event->mimeData());
×
1430
  update();
×
1431
}
×
1432
}
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