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

ossia / score / 45070253512

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

Pull #2409

github

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

269268 of 804435 branches covered (33.47%)

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

108 existing lines in 24 files now uncovered.

274840 of 442315 relevant lines covered (62.14%)

763452.11 hits per line

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

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

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

101
  initConnections();
4,388 !
102
}
4,388 ✔
103

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

109
  m_folded = process.folded();
4,243 ✔
110

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

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

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

127
  updateLabel();
4,243 ✔
128

129
  con(process, &Process::ProcessModel::prettyNameChanged, this,
8,486 ✔
130
      &NodeItem::updateLabel);
4,243 ✔
131
  con(process.metadata(), &score::ModelMetadata::LabelChanged, this,
8,486 ✔
132
      &NodeItem::updateLabel);
4,243 ✔
133

134
  con(process, &Process::ProcessModel::loopsChanged, this, [&] { updateZoomRatio(); });
4,244 ✔
135
  con(process, &Process::ProcessModel::loopDurationChanged, this,
8,486 ✔
136
      [&] { updateZoomRatio(); });
4,244 ✔
137
  con(process, &Process::ProcessModel::startOffsetChanged, this,
8,486 ✔
138
      [&] { updateZoomRatio(); });
4,243 ✔
139

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

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

159
  if(!m_folded)
4,243 ✔
160
    createContentItem();
4,015 ✔
161
  else
162
    createFoldedItem();
228 ✔
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] {
7,795 ✔
167
    resetInlets();
3,552 ✔
168
    resetOutlets();
3,552 ✔
169

170
    resizeAsync();
3,552 ✔
171
  });
3,552 ✔
172
  connect(&process, &Process::ProcessModel::controlAdded, this, &NodeItem::resetInlets);
4,243 ✔
173
  connect(
4,243 ✔
174
      &process, &Process::ProcessModel::controlRemoved, this, &NodeItem::resetInlets);
4,243 ✔
175
  connect(
4,243 ✔
176
      &process, &Process::ProcessModel::controlOutletAdded, this,
4,243 ✔
177
      &NodeItem::resetOutlets);
4,243 ✔
178
  connect(
4,243 ✔
179
      &process, &Process::ProcessModel::controlOutletRemoved, this,
4,243 ✔
180
      &NodeItem::resetOutlets);
4,243 ✔
181
  connect(&process, &Process::ProcessModel::inletsChanged, this, &NodeItem::resetInlets);
4,243 ✔
182
  connect(
4,243 ✔
183
      &process, &Process::ProcessModel::outletsChanged, this, &NodeItem::resetOutlets);
4,243 ✔
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) {
4,243 ✔
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,
8,486 ✔
194
      [this] { applyFold(m_model.folded()); });
4,248 ✔
195

196
  resizeAsync();
4,243 ✔
197
  updateTooltip();
4,243 ✔
198
}
4,243 ✔
199

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

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

207
  createContentItem();
159 ✔
208

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

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

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

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

257
    this->setToolTip(str);
4,335 !
258
  }
4,335 ✔
259
  else if(m_fx)
67 !
260
  {
261
    this->setToolTip(m_fx->toolTip());
67 !
262
  }
67 ✔
263
}
4,402 ✔
264

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

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

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

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

294
void NodeItem::resetInlets()
3,903 ✔
295
{
296
  // if(!m_presenter)
297
  //   return;
298
  qDeleteAll(m_inlets);
3,903 ✔
299
  m_inlets.clear();
3,903 ✔
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)
3,903 !
304
    return updateTitlePos();
2,756 ✔
305

306
  const qreal x = InletX0;
1,147 ✔
307
  qreal y = m_label ? InletY0 : InletY0 - 10.;
1,147 ✔
308
  auto& portFactory = score::GUIAppContext().interfaces<Process::PortFactoryList>();
1,147 ✔
309
  for(Process::Inlet* port : m_model.inlets())
17,117 ✔
310
  {
311
    if(port->displayHandledExplicitly)
15,970 ✔
312
      continue;
14,817 ✔
313

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

320
    y += PortSpacing;
1,153 ✔
321
  }
322

323
  updateTitlePos();
1,147 ✔
324
}
3,903 ✔
325

326
bool NodeItem::contentShowsPort(const Process::Port& port) const noexcept
2,373 ✔
327
{
328
  if(!m_fx)
2,373 !
329
    return false;
×
330
  auto& ports = m_context.dataflow.ports();
2,373 ✔
331
  auto it = ports.find(&port);
2,373 !
332
  return it != ports.end() && it->second && m_fx->isAncestorOf(it->second);
2,385 !
333
}
2,373 ✔
334

335
void NodeItem::resetOutlets()
3,874 ✔
336
{
337
  // if(!m_presenter)
338
  //   return;
339
  qDeleteAll(m_outlets);
3,874 ✔
340
  m_outlets.clear();
3,874 ✔
341

342
  // We don't display ports on the left & right side
343
  // for normal items which are supposed to handle it
344
  if(dynamic_cast<Process::DefaultEffectItem*>(m_fx) && !m_presenter)
3,874 !
345
    return updateTitlePos();
2,615 ✔
346

347
  const qreal x = m_contentSize.width() + OutletX0;
1,259 ✔
348
  // Next to the inlet a fully custom item shows at its top-left
349
  qreal y = m_label ? OutletY0 : 5.;
1,259 ✔
350

351
  auto& portFactory = score::AppContext().interfaces<Process::PortFactoryList>();
1,259 ✔
352
  for(Process::Outlet* port : m_model.outlets())
3,660 ✔
353
  {
354
    if(port->displayHandledExplicitly || contentShowsPort(*port))
2,401 ✔
355
      continue;
34 ✔
356
    Process::PortFactory* fact = portFactory.get(port->concreteKey());
2,367 ✔
357
    auto item = fact->makePortItem(*port, m_context, this, this);
2,367 ✔
358
    item->setPos(x, y);
2,367 ✔
359
    item->setZValue(10);
2,367 ✔
360
    m_outlets.push_back(item);
2,367 ✔
361

362
    y += PortSpacing;
2,367 ✔
363
  }
364
}
3,874 ✔
365

366
void NodeItem::updateLabel()
4,455 ✔
367
{
368
  if(const auto& name = m_model.metadata().getName(); !name.isEmpty())
4,455 ✔
369
  {
370
    m_label->setText(m_model.metadata().touchedName() ? name : m_model.prettyName());
4,407 !
371
  }
4,407 ✔
372
  else if(const auto& label = m_model.metadata().getLabel(); !label.isEmpty())
48 !
373
  {
374
    m_label->setText(label);
×
375
  }
×
376
  else
377
  {
378
    m_label->setText(m_model.prettyShortName());
48 !
379
  }
380

381
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
4,455 ✔
382
  {
383
    resizeable->setMinimumWidth(minimalContentWidth());
189 ✔
384
  }
189 ✔
385
  QTimer::singleShot(2, this, &NodeItem::updateSize);
4,455 ✔
386
}
4,455 ✔
387

388
void NodeItem::updateTitlePos()
34,465 ✔
389
{
390
  if(!m_label)
34,465 ✔
391
    return;
277 ✔
392

393
  double x0 = FoldX0;
34,188 ✔
394
  if(!m_inlets.empty())
34,188 ✔
395
    x0 += InletX0;
5,292 ✔
396

397
  m_fold->setPos({x0, FoldY0});
34,188 ✔
398
  x0 += UiX0 - FoldX0 + 2.;
34,188 ✔
399

400
  if(m_scriptButton)
34,188 ✔
401
  {
402
    m_scriptButton->setPos({x0, UiY0});
4,546 ✔
403
    x0 += UiX0 + SpacingIcon;
4,546 ✔
404
  }
4,546 ✔
405

406
  if(m_uiButton)
34,188 ✔
407
  {
408
    m_uiButton->setPos({x0, UiY0});
757 ✔
409
    x0 += UiX0 + SpacingIcon;
757 ✔
410
  }
757 ✔
411

412
  if(m_presetButton)
34,188 !
413
  {
414
    m_presetButton->setPos({x0, UiY0});
34,188 ✔
415
    x0 += UiX0 + SpacingIcon * 2.;
34,188 ✔
416
  }
34,188 ✔
417

418
  m_label->setPos({x0, TitleY0});
34,188 ✔
419
}
34,465 ✔
420

421
QSizeF NodeItem::size() const noexcept
×
422
{
423
  return m_contentSize;
×
424
}
425

426
void NodeItem::setSelected(bool s)
145 ✔
427
{
428
  m_selected = s;
145 ✔
429
  if(s)
145 ✔
430
    setFocus();
113 ✔
431
  else
432
    clearFocus();
32 ✔
433

434
  // Some processes draw their own ports: the dataflow plug-in knows them all
435
  auto& items = m_context.dataflow.ports();
145 ✔
436
  auto highlight = [&](const Process::Port* port) {
1,030 ✔
437
    if(auto it = items.find(port); it != items.end() && it->second)
885 ✔
438
      for(auto& cable : it->second->connectedCables())
564 ✔
439
        if(cable)
43 !
440
          cable->updateHighlight();
564 ✔
441
  };
885 ✔
442
  for(auto* port : m_model.inlets())
874 ✔
443
    highlight(port);
729 ✔
444
  for(auto* port : m_model.outlets())
301 ✔
445
    highlight(port);
156 ✔
446
  update();
145 ✔
447
}
145 ✔
448

449
QRectF NodeItem::contentRect() const noexcept
13,455 ✔
450
{
451
  if(!m_label)
13,455 ✔
452
  {
453
    return {
273 ✔
454
        0, 0, m_contentSize.width() + (hasRightSide() ? RightSideWidth : 0.),
273 ✔
455
        m_contentSize.height() + FooterHeight};
273 ✔
456
  }
457
  else
458
  {
459
    double x = 0;
13,182 ✔
460
    double y = -TitleHeight;
13,182 ✔
461
    double w = m_contentSize.width();
13,182 ✔
462
    const double h = m_contentSize.height() + TitleHeight + FooterHeight;
13,182 ✔
463
    if(!m_inlets.empty())
13,182 ✔
464
    {
465
      x -= LeftSideWidth;
3,312 ✔
466
      w += LeftSideWidth;
3,312 ✔
467
    }
3,312 ✔
468
    if(hasRightSide())
13,182 ✔
469
      w += RightSideWidth;
4,079 ✔
470
    return {x, y, w, h};
13,182 ✔
471
  }
472
}
13,455 ✔
473

474
void NodeItem::updateContentRect()
13,453 ✔
475
{
476
  updateResizeCorner();
13,453 ✔
477
  const QRectF r = contentRect().adjusted(-2., -2., 2., 2.);
13,453 ✔
478
  if(r == m_contentRect)
13,453 ✔
479
    return;
10,341 ✔
480
  m_contentRect = r;
3,112 ✔
481
  geometryChanged();
3,112 ✔
482
}
13,453 ✔
483

484
bool NodeItem::hasRightSide() const noexcept
14,279 ✔
485
{
486
  // A fully custom item lays out the outlets it does not draw itself in that
487
  // strip, possibly before m_outlets is filled
488
  return !m_outlets.empty()
24,119 ✔
489
         || ((m_model.flags() & Process::ProcessFlags::FullyCustomItem)
14,733 ✔
490
             && ossia::any_of(m_model.outlets(), [](const Process::Outlet* o) {
10,081 ✔
491
                  return !o->displayHandledExplicitly;
241 ✔
492
                }));
493
}
494

495
class NodeResizeCorner final : public QGraphicsItem
496
{
497
public:
498
  explicit NodeResizeCorner(NodeItem& node)
206 ✔
499
      : QGraphicsItem{&node}
206 !
500
      , m_node{node}
206 ✔
501
  {
206 ✔
502
    setAcceptedMouseButtons(Qt::LeftButton);
206 !
503
    setZValue(1000.);
206 !
504
    setCursor(score::Skin::instance().CursorScaleFDiag);
206 !
505
  }
206 ✔
506

507
  QRectF boundingRect() const override { return {-10., -10., 10., 10.}; }
248 ✔
508
  void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*) override
29 ✔
509
  {
510
    p->setPen(Process::Style::instance().IntervalWarning().main.pen0_solid_round);
29 ✔
511
    p->drawLine(QPointF{-6., -2.}, QPointF{-2., -2.});
29 ✔
512
    p->drawLine(QPointF{-2., -6.}, QPointF{-2., -2.});
29 ✔
513
  }
29 ✔
514

515
protected:
516
  // The node does the resizing, as from its own corner
517
  void mousePressEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
518
  {
519
    ev->setPos(mapToParent(ev->pos()));
2 ✔
520
    m_node.m_resizeFromCorner = true;
2 ✔
521
    m_node.mousePressEvent(ev);
2 ✔
522
    m_node.m_resizeFromCorner = false;
2 ✔
523
  }
2 ✔
524
  void mouseMoveEvent(QGraphicsSceneMouseEvent* ev) override
2 ✔
525
  {
526
    ev->setPos(mapToParent(ev->pos()));
2 ✔
527
    m_node.mouseMoveEvent(ev);
2 ✔
528
  }
2 ✔
529
  void mouseReleaseEvent(QGraphicsSceneMouseEvent* ev) override
1 ✔
530
  {
531
    ev->setPos(mapToParent(ev->pos()));
1 ✔
532
    m_node.mouseReleaseEvent(ev);
1 ✔
533
  }
1 ✔
534
  // The corner holds the grab: a release the scene drops must still end the
535
  // node's gesture
536
  bool sceneEvent(QEvent* ev) override
9 ✔
537
  {
538
    if(ev->type() == QEvent::UngrabMouse)
9 ✔
539
      m_node.sceneEvent(ev);
2 ✔
540
    return QGraphicsItem::sceneEvent(ev);
9 ✔
541
  }
542

543
private:
544
  NodeItem& m_node;
545
};
546

547
void NodeItem::updateResizeCorner()
13,453 ✔
548
{
549
  const bool wanted = m_presenter && !hasRightSide();
13,453 ✔
550
  if(!wanted)
13,453 ✔
551
  {
552
    delete m_resizeCorner;
13,044 ✔
553
    m_resizeCorner = nullptr;
13,044 ✔
554
    return;
13,044 ✔
555
  }
556
  if(!m_resizeCorner)
409 ✔
557
    m_resizeCorner = new NodeResizeCorner{*this};
206 !
558
  m_resizeCorner->setPos(m_contentSize.width(), m_contentSize.height());
409 ✔
559
}
13,453 ✔
560

561
QRectF NodeItem::boundingRect() const
197,877 ✔
562
{
563
  return m_contentRect.adjusted(-2., -2., 2., 2.);
197,877 ✔
564
}
565

566
//! Redraw for a fold state that has already been decided by the model.
567
void NodeItem::applyFold(bool folded)
5 ✔
568
{
569
  if(!m_fold || folded == m_folded)
5 !
570
    return;
3 ✔
571

572
  // setState() deliberately does not re-emit toggled(), so this cannot loop
573
  // back into the command that brought us here.
574
  m_fold->setState(!folded);
2 ✔
575

576
  resetItem();
2 ✔
577

578
  m_folded = folded;
2 ✔
579
  if(!m_folded)
2 !
580
  {
581
    createContentItem();
×
582
  }
×
583
  else
584
  {
585
    createFoldedItem();
2 ✔
586
  }
587

588
  QTimer::singleShot(1, this, [this] {
4 ✔
589
    resetInlets();
2 ✔
590
    resetOutlets();
2 ✔
591

592
    resizeAsync();
2 ✔
593
  });
2 ✔
594
}
5 ✔
595

596
void NodeItem::createFoldedItem()
230 ✔
597
{
598
  if(m_fx)
230 !
599
    return;
×
600

601
  auto& ctx = m_context;
230 ✔
602
  auto& model = m_model;
230 ✔
603
  // Body
604
  score::ResizeableItem* resizeable{};
230 ✔
605
  {
606
    auto fx = new Process::DefaultEffectItem{true, model, ctx, this};
230 !
607
    m_fx = fx;
230 !
608
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
230 ✔
609
    resizeable = fx;
230 ✔
610
  }
611
  setupItem(resizeable);
230 ✔
612
}
230 ✔
613

614
void NodeItem::createContentItem()
4,174 ✔
615
{
616
  if(m_fx)
4,174 !
617
    return;
×
618

619
  auto& ctx = m_context;
4,174 ✔
620
  auto& model = m_model;
4,174 ✔
621
  // Body
622
  auto& fact = ctx.app.interfaces<Process::LayerFactoryList>();
4,174 ✔
623
  score::ResizeableItem* resizeable{};
4,174 ✔
624
  if(auto factory = fact.findDefaultFactory(model))
4,174 ✔
625
  {
626
    if(auto fx = factory->makeItem(model, ctx, this))
2,100 ✔
627
    {
628
      m_fx = fx;
1,814 !
629
      m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
1,814 ✔
630
      resizeable = fx;
1,814 ✔
631
    }
1,814 ✔
632
    else if(auto fx = factory->makeLayerView(model, ctx, this))
286 !
633
    {
634
      m_fx = fx;
286 !
635
      m_presenter = factory->makeLayerPresenter(model, fx, ctx, this);
286 ✔
636
      m_contentSize = m_model.size();
286 ✔
637
      m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
286 ✔
638
      m_presenter->setHeight(m_contentSize.height());
286 ✔
639

640
      // TODO review the resizing heuristic...
641
      if(m_presenter)
286 !
642
      {
643
        ::bind(model, Process::ProcessModel::p_size{}, this, [this](QSizeF s) {
581 ✔
644
          if(s != m_contentSize)
295 ✔
645
            setSize(s);
7 ✔
646
        });
295 ✔
647
          connect(m_presenter, &Process::LayerPresenter::contextMenuRequested, this, [this] (QPoint pos, QPointF sp) {
286 ✔
648
              auto menu = new QMenu;
×
649
              auto& reg = score::GUIAppContext()
×
650
                              .applicationPlugin<Process::ApplicationPlugin>()
×
651
                              .layerContextMenuRegistrar();
×
652

653
              m_presenter->fillContextMenu(*menu, pos, sp, reg);
×
654
              if(menu->actions().size() > 0) {
×
655
                menu->exec(pos);
×
656
                menu->close();
×
657
              }
×
658

659
              menu->deleteLater();
×
660
          }, Qt::QueuedConnection);
×
661
      }
286 ✔
662
    }
286 ✔
663
  }
2,100 ✔
664

665
  if(!m_fx)
4,174 ✔
666
  {
667
    auto fx = new Process::DefaultEffectItem{false, model, ctx, this};
2,074 !
668
    m_fx = fx;
2,074 !
669
    m_contentSize = m_fx->boundingRect().size(); // FIXME async, size is not set yet
2,074 ✔
670
    resizeable = fx;
2,074 ✔
671
  }
2,074 ✔
672

673
  setupItem(resizeable);
4,174 ✔
674
}
4,174 ✔
675

676
void NodeItem::resizeAsync()
15,217 ✔
677
{
678
  updateTitlePos();
15,217 ✔
679
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
15,217 !
680
  {
681
    resizeable->setMinimumWidth(minimalContentWidth());
14,584 ✔
682
    // Needed to leave some time for defaulteffectitem
683
    QTimer::singleShot(2, this, [this] {
24,123 ✔
684
      updateSize();
9,539 ✔
685
      updateZoomRatio();
9,539 ✔
686
    });
9,539 ✔
687
  }
14,584 ✔
688
  else
689
  {
690
    QMetaObject::invokeMethod(this, [this] {
1,266 ✔
691
      updateSize();
633 ✔
692
      updateZoomRatio();
633 ✔
693
    });
633 ✔
694
  }
695
}
15,217 ✔
696

697
void NodeItem::recreate()
14 ✔
698
{
699
  resetItem();
14 ✔
700
  qDeleteAll(this->childItems());
14 !
701
  m_resizeCorner = nullptr;
14 ✔
702
  m_scriptButton = nullptr;
14 ✔
703
  m_uiButton = nullptr;
14 ✔
704
  m_presetButton = nullptr;
14 ✔
705
  m_label = nullptr;
14 ✔
706
  m_fold = nullptr;
14 ✔
707
  disconnect(&this->m_model, nullptr, this, nullptr);
14 ✔
708
  disconnect(&this->m_model.selection, nullptr, this, nullptr);
14 ✔
709
  disconnect(&this->m_model.metadata(), nullptr, this, nullptr);
14 ✔
710

711
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
14 !
712
  {
713
    createWithoutDecorations();
×
714
  }
×
715
  else
716
  {
717
    createWithDecorations();
14 ✔
718
  }
719

720
  initConnections();
14 ✔
721
}
14 ✔
722

723
void NodeItem::setupItem(score::ResizeableItem* resizeable)
4,404 ✔
724
{
725
  // Positions / size
726
  double w = std::max(minimalContentWidth(), m_contentSize.width());
4,404 ✔
727
  double h = std::max(minimalContentHeight(), m_contentSize.height());
4,404 ✔
728
  m_contentSize = QSizeF{w, h};
4,404 ✔
729

730
  resizeAsync();
4,404 ✔
731
  publishSize();
4,404 ✔
732

733
  if(resizeable)
4,404 ✔
734
  {
735
    if(auto lay = dynamic_cast<score::GraphicsLayout*>(resizeable))
4,118 !
736
      connect(lay, &score::ResizeableItem::childrenSizeChanged, this, [this, lay] {
1,312 ✔
737
        // As GraphicsLayout::relayoutAround does: the items are placed in
738
        // the width they make, then aligned and stretched to it
739
        lay->layout();
207 ✔
740
        lay->fitChildrenRect();
207 ✔
741
        lay->centerContent();
207 ✔
742
        m_contentSize = lay->rect().size();
207 ✔
743

744
        resizeAsync();
207 ✔
745
      });
207 ✔
746
    connect(resizeable, &score::ResizeableItem::sizeChanged, this, [this](QSizeF sz) {
6,596 ✔
747
      double w = std::max(minimalContentWidth(), sz.width());
2,478 ✔
748
      double h = std::max(minimalContentHeight(), sz.height());
2,478 ✔
749
      m_contentSize = QSizeF{w, h};
2,478 ✔
750

751
      resizeAsync();
2,478 ✔
752
    });
2,478 ✔
753
  }
4,118 ✔
754
}
4,404 ✔
755

756
double NodeItem::minimalContentWidth() const noexcept
34,671 ✔
757
{
758
  if(Q_UNLIKELY(!m_label))
34,671 ✔
759
  {
760
    if((m_inlets.size() + m_outlets.size()) <= 1)
443 ✔
761
      return 10.;
419 ✔
762
    else
763
      return 20.;
24 ✔
764
  }
765
  else
766
  {
767
    return std::max(75.0, m_label->pos().x() + m_label->boundingRect().width() + 6.);
34,228 !
768
  }
769
}
34,671 ✔
770
double NodeItem::minimalContentHeight() const noexcept
19,898 ✔
771
{
772
  if(Q_UNLIKELY(!m_label))
19,898 ✔
773
    if(std::max(m_inlets.size(), m_outlets.size()) <= 1)
429 !
774
      return 10.;
405 ✔
775

776
  double h = 22.;
19,493 ✔
777
  if(!m_inlets.empty())
19,493 ✔
778
    h = std::max(h, m_inlets.back()->y() + 10.);
3,316 !
779
  if(!m_outlets.empty())
19,493 ✔
780
    h = std::max(h, m_outlets.back()->y() + 10.);
4,105 !
781
  return h;
19,493 ✔
782
}
19,898 ✔
783

784
void NodeItem::updateSize()
12,997 ✔
785
{
786
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
12,997 ✔
787
  {
788
    if(m_presenter)
267 ✔
789
      resizePresenter();
260 ✔
790
    // A fully-custom item lays out its own inlets, so the node has to be at
791
    // least as wide as that item: keeping the width the title asked for draws
792
    // the border inside the item, and the outlets end up inside it. Short
793
    // titles are where this shows, since that is when the title is not what
794
    // decides the width.
795
    QSizeF sz{minimalContentWidth(), minimalContentHeight()};
267 ✔
796
    if(m_fx)
267 !
797
    {
798
      auto fx_sz = m_fx->boundingRect().size();
267 ✔
799
      sz.rwidth() = std::max(sz.width(), fx_sz.width());
267 ✔
800
      sz.rheight() = std::max(sz.height(), fx_sz.height());
267 ✔
801
    }
267 ✔
802
    if(sz != m_contentSize)
267 !
803
    {
804
      prepareGeometryChange();
×
805
      m_contentSize = sz;
×
806
      publishSize();
×
807
    }
×
808
    for(auto& outlet : m_outlets)
369 ✔
809
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
102 ✔
810
    updateContentRect();
267 ✔
811
    update();
267 ✔
812
    if(auto sc = this->scene())
267 !
813
      sc->update();
267 ✔
814
    return;
267 ✔
815
  }
816

817
  if(!m_label)
12,730 !
818
  {
819
    if(auto sc = this->scene())
×
820
      sc->update();
×
821
    return;
×
822
  }
823
  QSizeF sz{minimalContentWidth(), minimalContentHeight()};
12,730 ✔
824
  if(m_fx)
12,730 !
825
  {
826
    auto fx_sz = m_fx->boundingRect().size();
12,730 ✔
827
    sz.rwidth() = std::max(sz.width(), fx_sz.width());
12,730 ✔
828
    sz.rheight() = std::max(sz.height(), fx_sz.height());
12,730 ✔
829
  }
12,730 ✔
830
  //if (sz != m_contentSize || !m_fx)
831
  {
832
    prepareGeometryChange();
12,730 ✔
833
    if(m_fx)
12,730 !
834
    {
835
      m_contentSize = sz;
12,730 ✔
836
      if(!m_presenter)
12,730 ✔
837
      {
838
        if(m_model.outlets().size() > 0 && m_model.inlets().size() == 0)
12,254 ✔
839
        {
840
          // Align right for the case where we only have a series of outlets
841
          updateContentRect();
449 ✔
842
          m_fx->setPos(m_contentRect.width() - m_fx->boundingRect().width(), 0);
449 ✔
843
        }
449 ✔
844
        else
845
        {
846
          m_fx->setPos(0, 0);
11,805 ✔
847
        }
848
      }
12,254 ✔
849
    }
12,730 ✔
850

851
    if(m_presenter)
12,730 ✔
852
      resizePresenter();
476 ✔
853

854
    if(m_scriptButton)
12,730 ✔
855
    {
856
      m_scriptButton->setParentItem(nullptr);
1,567 ✔
857
    }
1,567 ✔
858
    if(m_uiButton)
12,730 ✔
859
    {
860
      m_uiButton->setParentItem(nullptr);
325 ✔
861
    }
325 ✔
862

863
    for(auto& outlet : m_outlets)
21,319 ✔
864
    {
865
      outlet->setPos(m_contentSize.width() + OutletX0, outlet->pos().y());
8,589 ✔
866
    }
867

868
    if(m_scriptButton)
12,730 ✔
869
    {
870
      m_scriptButton->setParentItem(this);
1,567 ✔
871
    }
1,567 ✔
872
    if(m_uiButton)
12,730 ✔
873
    {
874
      m_uiButton->setParentItem(this);
325 ✔
875
    }
325 ✔
876
    updateTitlePos();
12,730 ✔
877
    updateContentRect();
12,730 ✔
878
    publishSize();
12,730 ✔
879
    update();
12,730 ✔
880
    if(auto sc = this->scene())
12,730 !
881
      sc->update();
12,730 ✔
882
  }
883
}
12,997 ✔
884

885
void NodeItem::publishSize()
17,134 ✔
886
{
887
  // Node placement (e.g. creating a process after another one) reads the size
888
  // off the model: keep it in sync with what is actually drawn. A folded node
889
  // must not overwrite the unfolded size it will go back to.
890
  if(m_folded)
17,134 ✔
891
  {
892
    const_cast<Process::ProcessModel&>(m_model).setFoldedSize(m_contentSize);
1,065 ✔
893
    return;
1,065 ✔
894
  }
895
  if(m_model.flags() & Process::ProcessFlags::FullyCustomItem)
16,069 ✔
896
    return;
159 ✔
897
  if(m_model.size() == m_contentSize)
15,910 ✔
898
    return;
11,635 ✔
899

900
  const_cast<Process::ProcessModel&>(m_model).setSize(m_contentSize);
4,275 ✔
901
}
17,134 ✔
902

903
void NodeItem::setSize(QSizeF sz)
7 ✔
904
{
905
  if(m_presenter)
7 ✔
906
  {
907
    // TODO: find a way to indicate to a model what will be the size of the
908
    // item
909
    //      - maybe set it without a command in that case ?
910
    prepareGeometryChange();
6 ✔
911
    m_contentSize = sz;
6 ✔
912

913
    if(m_scriptButton)
6 !
914
    {
915
      m_scriptButton->setParentItem(nullptr);
×
916
    }
×
917
    if(m_uiButton)
6 !
918
    {
919
      m_uiButton->setParentItem(nullptr);
×
920
    }
×
921

922
    resizePresenter();
6 ✔
923

924
    resetInlets();
6 ✔
925
    resetOutlets();
6 ✔
926
    auto cur_x = m_contentSize.width() + TopButtonX0;
6 ✔
927
    if(m_scriptButton)
6 !
928
    {
929
      m_scriptButton->setParentItem(this);
×
930
      m_scriptButton->setPos({cur_x, TopButtonY0});
×
931
      cur_x += UiX0 + SpacingIcon;
×
932
    }
×
933
    if(m_uiButton)
6 !
934
    {
935
      m_uiButton->setParentItem(this);
×
936
      m_uiButton->setPos({cur_x, TopButtonY0});
×
937
    }
×
938
  }
6 ✔
939
  if(auto resizeable = dynamic_cast<score::ResizeableItem*>(m_fx))
7 !
940
  {
941
    resizeable->setMinimumWidth(sz.width());
1 ✔
942
  }
1 ✔
943
  updateContentRect();
7 ✔
944
  update();
7 ✔
945
  if(auto sc = this->scene())
7 !
946
    sc->update();
7 ✔
947
}
7 ✔
948

949
const Id<Process::ProcessModel>& NodeItem::id() const noexcept
20 ✔
950
{
951
  return m_model.id();
20 ✔
952
}
953

954
NodeItem::~NodeItem()
8,776 ✔
955
{
8,776 ✔
956
  clearDropCableHighlight();
4,388 !
957
  delete m_presenter;
4,388 ✔
958
}
8,776 ✔
959

960
void NodeItem::setParentDuration(TimeVal r)
95 ✔
961
{
962
  if(r != m_parentDuration)
95 !
963
  {
964
    m_parentDuration = r;
×
965
    updateZoomRatio();
×
966
  }
×
967
}
95 ✔
968

969
void NodeItem::setPlayPercentage(float f, TimeVal parent_dur)
1,592 ✔
970
{
971
  if(this->m_model.flags() & Process::ProcessFlags::FullyCustomItem)
1,592 ✔
972
    return;
80 ✔
973

974
  if(m_playPercentage != f)
1,512 !
975
  {
976
    // Comes from the interval -> if we are looping we make a small modulo of it
977
    if(m_model.loops() && m_model.loopDuration().impl > 0)
×
978
    {
979
      auto loopDur = m_model.loopDuration().impl;
×
980
      double playdur = double(f) * parent_dur.impl;
×
981
      f = std::fmod(playdur, loopDur) / loopDur;
×
982
      if(m_playPercentage != f)
×
983
      {
984
        m_playPercentage = f;
×
985
        update({12., -1., (m_contentSize.width() - 24.), 3.});
×
986
      }
×
987
    }
×
988
    else
989
    {
990
      f = ossia::clamp(f, 0.f, 1.f);
×
991
      if(m_playPercentage != f)
×
992
      {
993
        m_playPercentage = f;
×
994
        update({12., -1., (m_contentSize.width() - 24.) * f + 1., 3.});
×
995
      }
×
996
    }
997
  }
×
998
}
1,592 ✔
999

1000
qreal NodeItem::width() const noexcept
×
1001
{
1002
  return m_contentSize.width();
×
1003
}
1004

1005
qreal NodeItem::height() const
×
1006
{
1007
  return TitleHeight + m_contentSize.height() + FooterHeight;
×
1008
}
1009

1010
void NodeItem::initConnections()
4,402 ✔
1011
{
1012
  auto& process = this->model();
4,402 ✔
1013
  ::bind(process, Process::ProcessModel::p_position{}, this, [this](QPointF p) {
8,811 ✔
1014
    if(p != pos())
4,409 ✔
1015
      setPos(p);
4,390 ✔
1016
  });
4,409 ✔
1017

1018
  auto on_sizeChanged = [this] {
4,947 ✔
1019
    m_needResize = true;
545 ✔
1020
    QTimer::singleShot(1, this, [this] {
981 ✔
1021
      if(!m_needResize)
436 ✔
1022
        return;
217 ✔
1023
      m_needResize = false;
219 ✔
1024
      resizeAsync();
219 ✔
1025
    });
436 ✔
1026
  };
545 ✔
1027
  connect(&process, &Process::ProcessModel::controlAdded, this, on_sizeChanged);
4,402 ✔
1028
  connect(&process, &Process::ProcessModel::controlRemoved, this, on_sizeChanged);
4,402 ✔
1029
  connect(&process, &Process::ProcessModel::controlOutletAdded, this, on_sizeChanged);
4,402 ✔
1030
  connect(&process, &Process::ProcessModel::controlOutletRemoved, this, on_sizeChanged);
4,402 ✔
1031
  connect(&process, &Process::ProcessModel::inletsChanged, this, on_sizeChanged);
4,402 ✔
1032
  connect(&process, &Process::ProcessModel::outletsChanged, this, on_sizeChanged);
4,402 ✔
1033
  connect(&process, &Process::ProcessModel::flagsChanged, this, &NodeItem::recreate);
4,402 ✔
1034
}
4,402 ✔
1035

1036
const ProcessModel& NodeItem::model() const noexcept
23,009 ✔
1037
{
1038
  return m_model;
23,009 ✔
1039
}
1040

1041
void NodeItem::resizePresenter()
742 ✔
1042
{
1043
  m_presenter->setWidth(m_contentSize.width(), m_contentSize.width());
742 ✔
1044
  m_presenter->setHeight(m_contentSize.height());
742 ✔
1045
  updateZoomRatio();
742 ✔
1046
}
742 ✔
1047

1048
void NodeItem::updateZoomRatio() const noexcept
10,916 ✔
1049
{
1050
  const auto dur = m_model.loops() ? m_model.loopDuration() : m_parentDuration;
10,916 ✔
1051
  if(m_presenter)
10,916 ✔
1052
  {
1053
    m_presenter->on_zoomRatioChanged(dur.impl / m_contentSize.width());
1,377 !
1054
    // TODO investigate why this is necessary for scenario:
1055
    m_presenter->parentGeometryChanged();
1,377 !
1056
  }
1,377 ✔
1057
}
10,916 ✔
1058

1059
bool NodeItem::isInSelectionCorner(QPointF p, QRectF r) const
3 ✔
1060
{
1061
  return (p.x() - r.x()) > (r.width() - 10.) && (p.y() - r.y()) > (r.height() - 10.);
3 !
1062
}
1063

1064
void NodeItem::paint(
2,336 ✔
1065
    QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
1066
{
1067
  auto& style = Process::Style::instance();
2,336 ✔
1068
  const auto& skin = style.skin;
2,336 ✔
1069
  const auto rect = m_contentRect;
2,336 ✔
1070
  //painter->fillRect(boundingRect(), Qt::red);
1071
  //return;
1072
  const auto flags = this->m_model.flags();
2,336 ✔
1073

1074
  const auto& bset = skin.Emphasis5;
2,336 ✔
1075
  const auto& fillbrush = !(flags & Process::ProcessFlags::NodeHasNoBackground)
2,336 ✔
1076
                              ? skin.Emphasis5
2,334 ✔
1077
                              : skin.TransparentBrush;
2 ✔
1078
  const auto& brush = m_selected ? skin.Base2.darker
4,560 ✔
1079
                      : m_hover  ? bset.lighter
2,224 !
1080
                                 : bset.main;
2,224 ✔
1081
  // Dashed while its presets load into it from the library
1082
  const auto& pen = m_dropping       ? skin.Warn2.main.pen3_solid_round_round
4,672 !
1083
                    : m_presetTarget ? skin.Base2.darker.pen3_dashed_flat_miter
2,336 !
1084
                                     : brush.pen2_solid_round_round;
2,336 ✔
1085

1086
  painter->setRenderHint(QPainter::Antialiasing, true);
2,336 ✔
1087

1088
  // Body
1089
  painter->setPen(pen);
2,336 ✔
1090

1091
#if defined(SCORE_DEBUG_REDRAWS)
1092
  {
1093
    QColor c;
1094
    c.setRedF(0.5 * double(rand()) / RAND_MAX);
1095
    c.setGreenF(0.5 * double(rand()) / RAND_MAX);
1096
    c.setBlueF(0.5 * double(rand()) / RAND_MAX);
1097
    c.setAlphaF(1.);
1098
    painter->setBrush(c);
1099
  }
1100
#else
1101
  painter->setBrush(fillbrush);
2,336 ✔
1102
#endif
1103

1104
  painter->drawRoundedRect(rect, Corner, Corner);
2,336 ✔
1105

1106
  painter->setRenderHint(QPainter::Antialiasing, false);
2,336 ✔
1107

1108
  // Exec
1109
  if(!(flags & Process::ProcessFlags::FullyCustomItem))
2,336 ✔
1110
    if(m_playPercentage > 0.)
2,283 !
1111
    {
1112
      painter->setPen(style.IntervalPlayFill().main.pen1_solid_flat_miter);
×
1113
      painter->drawLine(
×
1114
          QPointF{12., 0.}, QPointF{12. + (width() - 24.) * m_playPercentage, 0.});
×
1115
    }
×
1116

1117
  // Resizing handle, in the right strip; NodeResizeCorner draws it otherwise
1118
  if(m_presenter && hasRightSide())
2,336 ✔
1119
  {
1120
    const auto h = m_contentSize.height();
53 ✔
1121
    const auto w = m_contentSize.width() + RightSideWidth;
53 ✔
1122
    painter->setPen(style.IntervalWarning().main.pen0_solid_round);
53 ✔
1123
    double start_x = w - 6.;
53 ✔
1124
    double start_y = h - 6.;
53 ✔
1125
    double center_x = w - 2.;
53 ✔
1126
    double center_y = h - 2.;
53 ✔
1127
    painter->drawLine(start_x, center_y, center_x, center_y);
53 ✔
1128
    painter->drawLine(center_x, start_y, center_x, center_y);
53 ✔
1129
  }
53 ✔
1130
}
2,336 ✔
1131

1132
namespace
1133
{
1134
enum Interaction
1135
{
1136
  None,
1137
  Move,
1138
  Resize
1139
} nodeItemInteraction{};
1140
QSizeF origNodeSize{};
1141
bool nodeDidMove{};
1142
bool nodeSelectOnRelease{};
1143
bool nodeSelectCumulation{};
1144
}
1145

1146
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
6 ✔
1147
{
1148
  nodeItemInteraction = Interaction::None;
6 ✔
1149
  if(event->button() == Qt::LeftButton)
6 !
1150
  {
1151
    nodeDidMove = false;
6 ✔
1152
    if(m_presenter
6 ✔
1153
       && (isInSelectionCorner(event->pos(), m_contentRect)
6 !
1154
           || m_resizeFromCorner))
2 !
1155
    {
1156
      nodeItemInteraction = Interaction::Resize;
2 ✔
1157
      origNodeSize = m_model.size();
2 ✔
1158
    }
2 ✔
1159
    else
1160
    {
1161
      nodeItemInteraction = Interaction::Move;
4 ✔
1162
    }
1163

1164
    if(m_presenter && m_model.flags() & Process::ProcessFlags::ItemRequiresUniqueFocus)
6 !
1165
    {
1166
      m_context.focusDispatcher.focus(m_presenter);
×
1167
    }
×
1168
    {
1169
      const bool cumulation = event->modifiers() & Qt::ControlModifier;
6 ✔
1170

1171
      // Membership of the selection stack, not ProcessModel::selection: the
1172
      // latter is also set on a process that merely *contains* the selection --
1173
      // clicking a port marks its parent node selected so that the node draws
1174
      // its frame, while the stack holds only the port. Deferring on the flag
1175
      // left such a node drawn as selected and yet impossible to drag, since
1176
      // selectedProcesses() below reads the stack and would come back empty.
1177
      const Selection current = m_context.selectionStack.currentSelection();
6 ✔
1178
      if(current.contains(&m_model))
6 ✔
1179
      {
1180
        nodeSelectOnRelease = true;
2 ✔
1181
        nodeSelectCumulation = cumulation;
2 ✔
1182
      }
2 ✔
1183
      else
1184
      {
1185
        nodeSelectOnRelease = false;
4 ✔
1186
        Selection sel;
4 ✔
1187
        if(cumulation)
4 !
1188
          sel = current;
×
1189
        sel.append(&m_model);
4 !
1190
        score::SelectionDispatcher{m_context.selectionStack}.select(sel);
4 !
1191
      }
4 ✔
1192
    }
6 ✔
1193
    event->accept();
6 ✔
1194
  }
6 ✔
1195
  else
1196
  {
1197
    return QGraphicsItem::mousePressEvent(event);
×
1198
  }
1199
}
6 ✔
1200
namespace
1201
{
1202
static std::vector<const Process::ProcessModel*>
1203
selectedProcesses(const score::DocumentContext& ctx)
5 ✔
1204
{
1205
  std::vector<const Process::ProcessModel*> ps;
5 ✔
1206
  auto sel = ctx.selectionStack.currentSelection();
5 !
1207
  ps.reserve(sel.size());
5 !
1208
  for(auto& obj : sel)
10 !
1209
    if(auto p = qobject_cast<Process::ProcessModel*>(obj))
5 !
1210
    {
1211
      ps.push_back(p);
5 !
1212
    }
5 ✔
1213
  return ps;
5 ✔
1214
}
5 ✔
1215
}
1216
//! The single cable the node was let go on top of, if any: ambiguous drops
1217
//! (more than one cable under the node) do nothing.
1218
static const Process::Cable* cableUnder(const NodeItem& node, QRectF contentRect)
×
1219
{
1220
  auto sc = node.scene();
×
1221
  if(!sc)
×
1222
    return nullptr;
×
1223

1224
  const Process::Cable* res{};
×
1225
  for(auto item : sc->items(node.mapToScene(contentRect.center())))
×
1226
  {
1227
    if(item->type() != Dataflow::CableItem::Type)
×
1228
      continue;
×
1229
    if(res)
×
1230
      return nullptr;
×
1231
    res = &static_cast<Dataflow::CableItem*>(item)->model();
×
1232
  }
1233
  return res;
×
1234
}
×
1235

1236
void NodeItem::updateDropCableHighlight()
5 ✔
1237
{
1238
  Dataflow::CableItem* next{};
5 ✔
1239
  if(canDropOnCableHandler)
5 !
1240
  {
1241
    if(auto* cbl = cableUnder(*this, m_contentRect))
×
1242
    {
1243
      if(canDropOnCableHandler(m_model, *cbl))
×
1244
      {
1245
        auto& cables = m_context.dataflow.cables();
×
1246
        if(auto it = cables.find(cbl); it != cables.end())
×
1247
          next = it->second;
×
1248
      }
×
1249
    }
×
1250
  }
×
1251

1252
  if(next == m_dropCable)
5 !
1253
    return;
5 ✔
1254
  if(m_dropCable)
×
1255
    m_dropCable->setDropTarget(false);
×
1256
  m_dropCable = next;
×
1257
  if(m_dropCable)
×
1258
    m_dropCable->setDropTarget(true);
×
1259
}
5 ✔
1260

1261
void NodeItem::clearDropCableHighlight()
4,396 ✔
1262
{
1263
  if(m_dropCable)
4,396 !
1264
    m_dropCable->setDropTarget(false);
×
1265
  m_dropCable = nullptr;
4,396 ✔
1266
}
4,396 ✔
1267

1268
void NodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
9 ✔
1269
{
1270
  if(nodeItemInteraction == Interaction::None)
9 !
1271
    return QGraphicsItem::mouseMoveEvent(event);
×
1272

1273
  auto parent = this->parentItem();
9 ✔
1274
  auto origp = parent->mapFromScene(event->buttonDownScenePos(Qt::LeftButton));
9 ✔
1275
  auto p = parent->mapFromScene(event->scenePos());
9 ✔
1276
  if(p != origp)
9 ✔
1277
    nodeDidMove = true;
8 ✔
1278

1279
  if(nodeDidMove)
9 ✔
1280
  {
1281
    switch(nodeItemInteraction)
8 !
1282
    {
1283
      case Interaction::Resize: {
1284
        const auto sz = origNodeSize + QSizeF{p.x() - origp.x(), p.y() - origp.y()};
3 ✔
1285
        m_dispatcher.submit<Process::ResizeNode>(
6 ✔
1286
            m_model, sz.expandedTo({minimalContentWidth(), minimalContentHeight()}));
3 ✔
1287
        updateSize();
3 ✔
1288
        break;
3 ✔
1289
      }
1290
      case Interaction::Move: {
1291
        auto procs = selectedProcesses(m_context);
5 ✔
1292
        // The press put this node in the selection, so it is normally already
1293
        // in here; a selection that somehow does not hold it would turn the
1294
        // whole drag into a silent no-op rather than moving the node under the
1295
        // mouse.
1296
        if(!ossia::contains(procs, &m_model))
5 !
1297
          procs.push_back(&m_model);
×
1298
        m_dispatcher.submit<Process::MoveNodes>(std::move(procs), p - origp);
5 !
1299
        updateDropCableHighlight();
5 !
1300
        break;
1301
      }
5 ✔
1302
      default:
1303
        break;
×
1304
    }
1305
  }
8 ✔
1306
  event->accept();
9 ✔
1307
}
9 ✔
1308

1309

1310
void NodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
4 ✔
1311
{
1312
  if(nodeItemInteraction == Interaction::None)
4 !
1313
    return QGraphicsItem::mouseReleaseEvent(event);
×
1314

1315
  mouseMoveEvent(event);
4 ✔
1316

1317
  clearDropCableHighlight();
4 ✔
1318

1319
  if(nodeDidMove)
4 ✔
1320
  {
1321
    // Part of the same macro as the move: dropping a node on a cable is a
1322
    // single gesture and must undo as one.
1323
    if(dropOnCableHandler && nodeItemInteraction == Interaction::Move)
3 !
1324
    {
1325
      if(auto cbl = cableUnder(*this, m_contentRect);
×
1326
         cbl && (!canDropOnCableHandler || canDropOnCableHandler(m_model, *cbl)))
×
1327
      {
1328
        score::Dispatcher_T<MultiOngoingCommandDispatcher> disp{m_dispatcher};
×
1329
        dropOnCableHandler(m_model, *cbl, disp);
×
1330
      }
×
1331
    }
×
1332
    m_dispatcher.commit<Process::MoveNodesMacro>();
3 ✔
1333
  }
3 ✔
1334
  else if(nodeSelectOnRelease)
1 !
1335
  {
1336
    Selection sel = filterSelections(
×
1337
        &m_model, m_context.selectionStack.currentSelection(), nodeSelectCumulation);
×
1338
    score::SelectionDispatcher{m_context.selectionStack}.select(sel);
×
1339
  }
×
1340
  nodeSelectOnRelease = false;
4 ✔
1341
  nodeDidMove = false;
4 ✔
1342
  event->accept();
4 ✔
1343
}
4 ✔
1344

1345
//! QEvent::UngrabMouse: the scene took the implicit grab away and there will be
1346
//! no release to end the drag on -- most visibly when the button was let go
1347
//! outside the window and the pointer then comes back in. Without this the
1348
//! MoveNodes command stays open, and the next drag of this node updates *it*
1349
//! instead of starting fresh, moving the node relative to the previous press.
1350
bool NodeItem::sceneEvent(QEvent* event)
9,009 ✔
1351
{
1352
  if(event->type() == QEvent::UngrabMouse)
9,009 ✔
1353
  {
1354
    if(nodeItemInteraction != Interaction::None)
4 !
1355
    {
1356
      clearDropCableHighlight();
4 ✔
1357
      if(nodeDidMove)
4 ✔
1358
        m_dispatcher.commit<Process::MoveNodesMacro>();
2 ✔
1359

1360
      nodeItemInteraction = Interaction::None;
4 ✔
1361
      nodeSelectOnRelease = false;
4 ✔
1362
      nodeDidMove = false;
4 ✔
1363
    }
4 ✔
1364
  }
4 ✔
1365
  return QGraphicsItem::sceneEvent(event);
9,009 ✔
1366
}
1367

1368
void NodeItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
2 ✔
1369
{
1370
  // The frame and title have no menu of their own: the view the node is in
1371
  // gives its menu, which acts on the selected nodes, this one among them.
1372
  // What the node shows inside may have its own, from its layer.
1373
  if(!m_model.selection.get())
2 !
1374
    score::SelectionDispatcher{m_context.selectionStack}.select(m_model);
2 ✔
1375

1376
  // Given to what holds the node, not to a node under it: that one would
1377
  // select itself in turn.
1378
  if(auto s = scene())
2 !
1379
  {
1380
    for(auto parent = parentItem(); parent; parent = parent->parentItem())
4 !
1381
    {
1382
      event->setPos(parent->mapFromScene(event->scenePos()));
4 ✔
1383
      event->accept();
4 ✔
1384
      s->sendEvent(parent, event);
4 ✔
1385
      if(event->isAccepted())
4 ✔
1386
        return;
2 ✔
1387
    }
2 ✔
NEW
1388
  }
×
1389
  event->accept();
×
1390
}
2 ✔
1391

1392
void NodeItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
122 ✔
1393
{
1394
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
122 !
1395
  {
1396
    auto& skin = score::Skin::instance();
1 ✔
1397
    setCursor(skin.CursorScaleFDiag);
1 ✔
1398
  }
1 ✔
1399
  else
1400
  {
1401
    unsetCursor();
121 ✔
1402
  }
1403

1404
  m_hover = true;
122 ✔
1405
  update();
122 ✔
1406
  event->accept();
122 ✔
1407
}
122 ✔
1408

1409
void NodeItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
8,747 ✔
1410
{
1411
  if(m_presenter && isInSelectionCorner(event->pos(), m_contentRect))
8,747 !
1412
  {
1413
    auto& skin = score::Skin::instance();
×
1414
    setCursor(skin.CursorScaleFDiag);
×
1415
  }
×
1416
  else
1417
  {
1418
    unsetCursor();
8,747 ✔
1419
  }
1420
  event->accept();
8,747 ✔
1421
}
8,747 ✔
1422

1423
void NodeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
119 ✔
1424
{
1425
  unsetCursor();
119 ✔
1426

1427
  m_hover = false;
119 ✔
1428
  update();
119 ✔
1429
  event->accept();
119 ✔
1430
}
119 ✔
1431

1432
void NodeItem::keyPressEvent(QKeyEvent* event)
×
1433
{
1434
  if(event->key() == Qt::Key_Down)
×
1435
  {
1436
    auto ports = Dataflow::getProcessPorts(this->m_model);
×
1437
    if(!ports.empty())
×
1438
    {
1439
      score::SelectionDispatcher disp{m_context.selectionStack};
×
1440
      disp.select(*ports.front());
×
1441
    }
×
1442
  }
×
1443
  event->accept();
×
1444
}
×
1445

1446
void NodeItem::keyReleaseEvent(QKeyEvent* event)
×
1447
{
1448
  event->accept();
×
1449
}
×
1450
void NodeItem::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
×
1451
{
1452
  m_dropping = true;
×
1453
  event->accept();
×
1454
  update();
×
1455
}
×
1456

1457
void NodeItem::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
×
1458
{
1459
  m_dropping = true;
×
1460
  event->accept();
×
1461
  update();
×
1462
}
×
1463

1464
void NodeItem::dragLeaveEvent(QGraphicsSceneDragDropEvent* event)
×
1465
{
1466
  m_dropping = false;
×
1467
  event->accept();
×
1468
  update();
×
1469
}
×
1470

1471
void NodeItem::dropEvent(QGraphicsSceneDragDropEvent* event)
×
1472
{
1473
  m_dropping = false;
×
1474
  event->accept();
×
1475
  dropReceived(event->pos(), *event->mimeData());
×
1476
  update();
×
1477
}
×
1478
}
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