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

ossia / score / 44990834010

22 Nov 2026 05:46AM UTC coverage: 61.818% (+0.2%) from 61.617%
44990834010

push

github

jcelerier
UI: the main window opens centred on the screen under the mouse and fits a 4K screen at 300%

The window was sized from the primary screen, while Qt opens it on the
screen under the mouse. When it did not fit there, Qt left it at logical
0,0, which a screen of another scale maps outside the desktop
(x=-10248 on a 100% primary with 300% screens beside it). It is now sized
from the screen it opens on and placed at its centre, with room kept for
the title bar, and on Wayland for a panel the compositor does not report:
three quarters of the screen, but at least 1000x680 when the screen allows.

The inspector and the object tree ask for 200 and 100 px instead of 400
and 160, so the window's minimum height drops from 744 to 484 logical px.
The start screen's header shrinks from 220 to 140 px on a window shorter
than 640, so the start screen fits down to 560: a 4K screen at 300% leaves
about 590 under the macOS menu bar, dock and title bar.

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

266655 of 799939 branches covered (33.33%)

36 of 38 new or added lines in 4 files covered. (94.74%)

3164 existing lines in 91 files now uncovered.

271878 of 439807 relevant lines covered (61.82%)

767748.57 hits per line

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

60.31
/src/plugins/score-plugin-deviceexplorer/Explorer/Explorer/DeviceExplorerModel.cpp
1
// This is an open source non-commercial project. Dear PVS-Studio, please check
2
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
#include "DeviceExplorerModel.hpp"
4

5
#include <State/MessageListSerialization.hpp>
6
#include <State/ValueConversion.hpp>
7

8
#include <Device/Address/AddressSettings.hpp>
9
#include <Device/ItemModels/NodeBasedItemModel.hpp>
10
#include <Device/ItemModels/NodeDisplayMethods.hpp>
11
#include <Device/Node/DeviceNode.hpp>
12
#include <Device/Node/NodeListMimeSerialization.hpp>
13
#include <Device/Protocol/DeviceSettings.hpp>
14
#include <Device/Protocol/ProtocolFactoryInterface.hpp>
15
#include <Device/Protocol/ProtocolList.hpp>
16

17
#include <Explorer/Commands/Add/LoadDevice.hpp>
18
#include <Explorer/Commands/Update/UpdateAddressSettings.hpp>
19
#include <Explorer/DeviceList.hpp>
20
#include <Explorer/DocumentPlugin/DeviceDocumentPlugin.hpp>
21
#include <Explorer/DocumentPlugin/NodeUpdateProxy.hpp>
22
#include <Explorer/Explorer/DeviceExplorerMimeTypes.hpp>
23
#include <Explorer/Explorer/DeviceExplorerView.hpp>
24
#include <Explorer/Explorer/Widgets/DeviceEditDialog.hpp> // TODO why here??!!
25

26
#include <score/application/ApplicationContext.hpp>
27
#include <score/application/GUIApplicationContext.hpp>
28
#include <score/command/CommandStackFacade.hpp>
29
#include <score/document/DocumentContext.hpp>
30
#include <score/document/DocumentInterface.hpp>
31
#include <score/model/path/Path.hpp>
32
#include <score/model/tree/TreeNode.hpp>
33
#include <score/model/tree/TreeNodeSerialization.hpp>
34
#include <score/plugins/InterfaceList.hpp>
35
#include <score/plugins/StringFactoryKey.hpp>
36
#include <score/serialization/JSONVisitor.hpp>
37
#include <score/serialization/MimeVisitor.hpp>
38
#include <score/widgets/MessageBox.hpp>
39

40
#include <ossia/detail/ssize.hpp>
41
#include <ossia/network/base/node_attributes.hpp>
42
#include <ossia/network/common/destination_qualifiers.hpp>
43
#include <ossia/network/domain/domain.hpp>
44

45
#include <ossia-qt/name_utils.hpp>
46

47
#include <QAbstractProxyModel>
48
#include <QApplication>
49
#include <QMainWindow>
50
#include <QMap>
51
#include <QMimeData>
52
#include <QObject>
53
#include <QTreeView>
54

55
#include <wobjectimpl.h>
56

57
#include <iostream>
58
#include <string>
59
#include <vector>
60
W_OBJECT_IMPL(Explorer::DeviceExplorerModel)
497,247 !
61
namespace Explorer
62
{
63
static const QMap<Explorer::Column, QString> HEADERS{
1,026 !
64
    {Explorer::Column::Name, QObject::tr("Address")},
1,026 !
65
    {Explorer::Column::Value, QObject::tr("Value")}};
1,026 !
66

67
DeviceExplorerModel::DeviceExplorerModel(DeviceDocumentPlugin& plug, QObject* parent)
2,202 ✔
68
    : NodeBasedItemModel{parent}
2,202 ✔
69
    , m_devicePlugin{plug}
2,202 ✔
70
    , m_rootNode{plug.rootNode()}
2,202 !
71
    , m_cmdQ{plug.context().commandStack}
2,202 !
72
{
2,202 ✔
73
  this->setObjectName("DeviceExplorerModel");
2,202 !
74

75
  beginResetModel();
2,202 !
76
  endResetModel();
2,202 !
77
}
2,202 ✔
78

79
DeviceExplorerModel::~DeviceExplorerModel() { }
4,404 ✔
80

81
DeviceDocumentPlugin& DeviceExplorerModel::deviceModel() const
9,096 ✔
82
{
83
  return m_devicePlugin;
9,096 ✔
84
}
85

86
QModelIndexList DeviceExplorerModel::selectedIndexes() const
3 ✔
87
{
88
  if(!m_view)
3 !
89
  {
90
    return {};
×
91
  }
92
  else
93
  {
94
    // We have to do this check if we have a proxy
95
    if(m_view->hasProxy())
3 !
96
    {
97
      auto indexes = m_view->selectedIndexes();
3 ✔
98
      for(auto& index : indexes)
6 !
99
        index = static_cast<const QAbstractProxyModel*>(m_view->QTreeView::model())
6 !
100
                    ->mapToSource(index);
3 !
101
      return indexes;
3 ✔
102
    }
3 ✔
103
    else
104
    {
105
      return m_view->selectedIndexes();
×
106
    }
107
  }
108
}
3 ✔
109

110
QStringList DeviceExplorerModel::getColumns() const
1,086 ✔
111
{
112
  return HEADERS.values();
1,086 ✔
113
}
114

115
int DeviceExplorerModel::addDevice(const Device::Node& deviceNode)
7 ✔
116
{
117
  int row = m_rootNode.childCount();
7 ✔
118
  QModelIndex parent; // invalid
7 ✔
119

120
  beginInsertRows(parent, row, row);
7 ✔
121
  rootNode().push_back(deviceNode);
7 ✔
122
  endInsertRows();
7 ✔
123

124
  return row;
7 ✔
125
}
126

127
int DeviceExplorerModel::addDevice(Device::Node&& deviceNode)
310 ✔
128
{
129
  deviceNode.setParent(&rootNode());
310 ✔
130

131
  int row = m_rootNode.childCount();
310 ✔
132
  QModelIndex parent; // invalid
310 ✔
133

134
  beginInsertRows(parent, row, row);
310 ✔
135
  rootNode().push_back(std::move(deviceNode));
310 ✔
136
  endInsertRows();
310 ✔
137

138
  return row;
310 ✔
139
}
140

141
bool DeviceExplorerModel::replaceDevice(Device::Node&& deviceNode)
129 ✔
142
{
143
  const auto& name = deviceNode.get<Device::DeviceSettings>().name;
129 ✔
144
  int row = 0;
129 ✔
145
  for(auto& n : m_rootNode)
133 !
146
  {
147
    if(n.get<Device::DeviceSettings>().name == name)
133 ✔
148
    {
149
      const QModelIndex index = createIndex(row, 0, &n);
129 ✔
150

151
      deviceTreeAboutToChange(&n);
129 ✔
152

153
      // Out with the old children...
154
      if(const int count = n.childCount(); count > 0)
129 !
155
      {
156
        beginRemoveRows(index, 0, count - 1);
129 ✔
157
        n.takeChildren();
129 ✔
158
        endRemoveRows();
129 ✔
159
      }
129 ✔
160

161
      // ... update the settings ...
162
      n.set(deviceNode.get<Device::DeviceSettings>());
129 ✔
163
      dataChanged(index, createIndex(row, columnCount() - 1, &n));
129 !
164

165
      // ... and in with the new.
166
      if(const int count = deviceNode.childCount(); count > 0)
129 !
167
      {
168
        beginInsertRows(index, 0, count - 1);
129 ✔
169
        deviceNode.moveChildren(n);
129 ✔
170
        endInsertRows();
129 ✔
171
      }
129 ✔
172

173
      deviceTreeChanged(&n);
129 ✔
174
      return true;
129 ✔
175
    }
176
    row++;
4 ✔
177
  }
178
  return false;
×
179
}
129 ✔
180

181
void DeviceExplorerModel::updateDevice(
31 ✔
182
    const QString& name, const Device::DeviceSettings& dev)
183
{
184
  int i = 0;
31 ✔
185
  for(auto& n : m_rootNode)
31 !
186
  {
187
    if(n.get<Device::DeviceSettings>().name == name)
31 !
188
    {
189
      n.set(dev);
31 ✔
190

191
      QModelIndex index = createIndex(i, 0, &n);
31 ✔
192
      dataChanged(index, index);
31 !
193
      return;
31 ✔
194
    }
195
    i++;
×
196
  }
197
}
31 ✔
198

199
Device::Node* DeviceExplorerModel::addAddress(
5 ✔
200
    Device::Node* parentNode, const Device::AddressSettings& addressSettings, int row)
201
{
202
  SCORE_ASSERT(parentNode);
5 !
203
  SCORE_ASSERT(parentNode != &m_rootNode);
5 !
204

205
  Device::Node* grandparent = parentNode->parent();
5 ✔
206
  SCORE_ASSERT(grandparent);
5 !
207
  int rowParent = grandparent->indexOfChild(parentNode);
5 ✔
208
  QModelIndex parentIndex = createIndex(rowParent, 0, parentNode);
5 ✔
209

210
  beginInsertRows(parentIndex, row, row);
5 ✔
211

212
  auto it = parentNode->begin();
5 ✔
213
  std::advance(it, row);
5 ✔
214

215
  auto& ret = parentNode->emplace(it, addressSettings, parentNode);
5 ✔
216

217
  endInsertRows();
5 ✔
218

219
  return &ret;
5 ✔
220
}
221

222
void DeviceExplorerModel::addNode(
573 ✔
223
    Device::Node* parentNode, Device::Node&& child, int row)
224
{
225
  SCORE_ASSERT(parentNode);
573 !
226
  SCORE_ASSERT(parentNode != &m_rootNode);
573 !
227

228
  Device::Node* grandparent = parentNode->parent();
573 ✔
229
  SCORE_ASSERT(grandparent);
573 !
230
  int rowParent = grandparent->indexOfChild(parentNode);
573 ✔
231
  QModelIndex parentIndex = createIndex(rowParent, 0, parentNode);
573 ✔
232

233
  beginInsertRows(parentIndex, row, row);
573 ✔
234

235
  auto it = parentNode->begin();
573 ✔
236
  std::advance(it, row);
573 ✔
237
  parentNode->emplace(it, std::move(child));
573 ✔
238

239
  endInsertRows();
573 ✔
240
}
573 ✔
241

242
void DeviceExplorerModel::updateAddress(
408 ✔
243
    Device::Node* node, const Device::AddressSettings& addressSettings)
244
{
245
  SCORE_ASSERT(node);
408 !
246
  SCORE_ASSERT(node != &m_rootNode);
408 !
247

248
  node->set(addressSettings);
408 ✔
249

250
  nodeChanged(node);
408 ✔
251

252
  dataChanged(
408 !
253
      modelIndexFromNode(*node, 0), modelIndexFromNode(*node, (int)Column::Count - 1));
408 ✔
254
}
408 ✔
255

256
void DeviceExplorerModel::updateValue(
93 ✔
257
    Device::Node* n, const State::AddressAccessor& addr, const ossia::value& v)
258
{
259
  if(!addr.qualifiers.get().accessors.empty())
93 !
260
  {
261
    SCORE_TODO;
×
262
  }
×
263
  n->get<Device::AddressSettings>().value = v;
93 ✔
264

265
  QModelIndex nodeIndex = modelIndexFromNode(*n, 1);
93 ✔
266

267
  dataChanged(nodeIndex, nodeIndex);
93 !
268
  valueUpdated(n);
93 ✔
269
}
93 ✔
270

271
bool DeviceExplorerModel::checkDeviceInstantiatable(
30 ✔
272
    const Device::DeviceSettings& n) const
273
{
274
  // No name -> no love
275
  if(n.name.isEmpty())
30 !
276
    return false;
×
277

278
  // "score" is the document's local device, and only it
279
  if((n.name == DeviceDocumentPlugin::localName())
30 ✔
280
     != DeviceDocumentPlugin::isLocalProtocol(n.protocol))
30 ✔
281
    return false;
2 ✔
282

283
  // Request from the protocol factory the protocol to see
284
  // if it is compatible.
285
  auto& context = m_devicePlugin.context().app;
28 ✔
286
  auto prot = context.interfaces<Device::ProtocolFactoryList>().get(n.protocol);
28 ✔
287
  if(!prot)
28 ✔
288
    return false;
27 ✔
289

290
  // Look for other childs in the same protocol.
291
  bool none_incompatible = std::none_of(
1 ✔
292
      rootNode().begin(), rootNode().end(), [&](const Device::Node& child) {
1 ✔
UNCOV
293
    SCORE_ASSERT(child.is<Device::DeviceSettings>());
×
UNCOV
294
    const auto& set = child.get<Device::DeviceSettings>();
×
UNCOV
295
    return (set.name == n.name)
×
UNCOV
296
           || (set.protocol
×
UNCOV
297
                   == n.protocol // FIXME distinct protocols could use the same port...
×
UNCOV
298
               && !prot->checkCompatibility(n, child.get<Device::DeviceSettings>()));
×
299
  });
300
  if(!none_incompatible)
1 !
UNCOV
301
    return false;
×
302

303
  // Check compatibility with an empty device
304
  return prot->checkCompatibility(n, Device::DeviceSettings{});
1 ✔
305
}
30 ✔
306

307
bool DeviceExplorerModel::checkDeviceEditable(
5 ✔
308
    const QString& originalName, const Device::DeviceSettings& n) const
309
{
310
  // No name -> no love
311
  if(n.name.isEmpty())
5 !
312
    return false;
×
313

314
  // "score" is the document's local device, and only it
315
  if((n.name == DeviceDocumentPlugin::localName())
5 ✔
316
     != DeviceDocumentPlugin::isLocalProtocol(n.protocol))
5 !
317
    return false;
×
318

319
  // Request from the protocol factory the protocol to see
320
  // if it is compatible.
321
  auto& context = m_devicePlugin.context().app;
5 ✔
322
  auto prot = context.interfaces<Device::ProtocolFactoryList>().get(n.protocol);
5 ✔
323
  if(!prot)
5 ✔
324
    return false;
3 ✔
325

326
  // Look for other childs in the same protocol.
327
  bool none_incompatible = std::none_of(
2 ✔
328
      rootNode().begin(), rootNode().end(), [&](const Device::Node& child) {
4 ✔
329
    SCORE_ASSERT(child.is<Device::DeviceSettings>());
2 !
330
    const auto& set = child.get<Device::DeviceSettings>();
2 ✔
331
    // Ignore the device that is being edited
332
    if(set.name == originalName)
2 !
333
      return false;
2 ✔
334

335
    return (set.name == n.name)
×
336
           || (set.protocol == n.protocol
×
337
               && !prot->checkCompatibility(n, child.get<Device::DeviceSettings>()));
×
338
  });
2 ✔
339
  if(!none_incompatible)
2 !
340
    return false;
×
341

342
  // Check compatibility with an empty device
343
  return prot->checkCompatibility(n, Device::DeviceSettings{});
2 ✔
344
}
5 ✔
345

346
bool DeviceExplorerModel::checkAddressInstantiatable(
×
347
    Device::Node& parent, const Device::AddressSettings& addr)
348
{
349
  SCORE_ASSERT(!parent.is<InvisibleRootNode>());
×
350

351
  if(addr.name.isEmpty())
×
352
    return false;
×
353

354
  return std::none_of(parent.begin(), parent.end(), [&](const Device::Node& n) {
×
355
    return n.get<Device::AddressSettings>().name == addr.name;
×
356
  });
357
}
×
358

359
bool DeviceExplorerModel::checkAddressEditable(
6 ✔
360
    Device::Node& parent, const Device::AddressSettings& before,
361
    const Device::AddressSettings& after)
362
{
363
  SCORE_ASSERT(!parent.is<InvisibleRootNode>());
6 !
364

365
  if(after.name.isEmpty())
6 !
366
    return false;
×
367

368
  auto it = std::find_if(parent.begin(), parent.end(), [&](const Device::Node& n) {
17 ✔
369
    return n.get<Device::AddressSettings>().name == after.name;
11 ✔
370
  });
371
  if(it != parent.end())
6 ✔
372
  {
373
    //  We didn't change name, it's ok
374
    if(after.name == before.name)
4 ✔
375
      return true;
3 ✔
376
    else
377
      return false;
1 ✔
378
  }
379
  else
380
  {
381
    // Ok, no conflicts
382
    return true;
2 ✔
383
  }
384
}
6 ✔
385

386
bool DeviceExplorerModel::canRenameNode(const Device::Node& n) const
8,954 ✔
387
{
388
  if(!n.is<Device::AddressSettings>())
8,954 !
389
    return false;
×
390

391
  // Called for every visible cell: walk up to the device rather than building
392
  // the whole address just to read its first component.
393
  const Device::Node* root = &n;
8,954 ✔
394
  while(root->parent() && !root->is<Device::DeviceSettings>())
18,606 !
395
    root = root->parent();
9,652 ✔
396

397
  if(!root->is<Device::DeviceSettings>())
8,954 !
398
    return false;
×
399

400
  auto* dev = m_devicePlugin.list().findDevice(root->get<Device::DeviceSettings>().name);
8,954 ✔
401
  return dev && dev->capabilities().canRenameNode;
8,954 !
402
}
8,954 ✔
403

404
int DeviceExplorerModel::columnCount() const
1,215 ✔
405
{
406
  return (int)Column::Count;
1,215 ✔
407
}
408

409
int DeviceExplorerModel::columnCount(const QModelIndex& /*parent*/) const
35,477 ✔
410
{
411
  return (int)Column::Count;
35,477 ✔
412
}
413

414
// must return an invalid QVariant for cases not handled
415
QVariant DeviceExplorerModel::data(const QModelIndex& index, int role) const
65,500 ✔
416
{
417
  const int col = index.column();
65,500 ✔
418

419
  if(col < 0 || col >= (int)Column::Count)
65,500 !
420
  {
421
    return QVariant();
×
422
  }
423

424
  const Device::Node& n = nodeFromModelIndex(index);
65,500 ✔
425
  if(role != Qt::ToolTipRole)
65,500 !
426
  {
427
    switch((Column)col)
65,500 !
428
    {
429
      case Column::Name: {
430
        if(n.is<Device::AddressSettings>())
34,816 ✔
431
          return Device::nameColumnData(n, role);
31,288 ✔
432
        else if(n.is<Device::DeviceSettings>())
3,528 !
433
        {
434
          auto& dev_set = n.get<Device::DeviceSettings>();
3,528 ✔
435
          return Device::deviceNameColumnData(
3,528 ✔
436
              n, deviceModel().list().device(dev_set.name).connected(), role);
3,528 ✔
437
        }
438
        return {};
×
439
      }
440

441
      case Column::Value:
442
        return Device::valueColumnData(n, role);
30,684 ✔
443

444
      case Column::Count:
×
445
      default:
446
        SCORE_ABORT;
×
447
        return {};
×
448
    }
449
  }
450
  else
451
  {
452
    // Tooltip
453
    if(n.is<Device::AddressSettings>())
×
454
    {
455
      auto& addr_set = n.get<Device::AddressSettings>();
×
456

457
      // What the one-line cell had to fold away.
458
      if((Column)col == Column::Value)
×
459
      {
460
        if(const auto* s = addr_set.value.target<std::string>())
×
461
        {
462
          const auto tip
463
              = State::convert::stringCellToolTip(QByteArray::fromStdString(*s));
×
464
          if(!tip.isEmpty())
×
465
            return tip;
×
466
        }
×
467
      }
×
468

469
      if(const auto& desc = ossia::net::get_description(addr_set))
×
470
        return QString::fromStdString(*desc);
×
471
    }
×
472
    else
473
    {
474
      return {};
×
475
    }
476
  }
477
  return {};
×
478
}
65,500 ✔
479

480
QVariant
481
DeviceExplorerModel::headerData(int section, Qt::Orientation orientation, int role) const
15,423 ✔
482
{
483
  if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
15,423 !
484
  {
485
    if(section >= 0 && section < (int)Column::Count)
5,239 !
486
    {
487
      return HEADERS[(Column)section];
5,239 ✔
488
    }
489
  }
×
490

491
  return {};
10,184 ✔
492
}
15,423 ✔
493

494
Qt::ItemFlags DeviceExplorerModel::flags(const QModelIndex& index) const
14,819 ✔
495
{
496
  Qt::ItemFlags f = Qt::ItemIsEnabled;
14,819 ✔
497
  // by default QAbstractItemModel::flags(index); returns Qt::ItemIsEnabled |
498
  // Qt::ItemIsSelectable
499

500
  if(index.isValid())
14,819 !
501
  {
502
    const Device::Node& n = nodeFromModelIndex(index);
14,819 ✔
503

504
    if(n.isSelectable())
14,819 !
505
    {
506
      f |= Qt::ItemIsSelectable;
14,819 ✔
507
    }
14,819 ✔
508

509
    // we allow drag'n drop only from the name column
510
    if(index.column() == (int)Column::Name)
14,819 ✔
511
    {
512
      f |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
10,041 ✔
513
    }
10,041 ✔
514

515
    if(n.isEditable() && index.column() == (int)Column::Value)
14,819 ✔
516
    {
517
      // A boolean toggles on one click and an impulse is a bang painted in the
518
      // cell: what the row draws *is* the editor in both cases, so the row is
519
      // not editable on top of it.
520
      const auto* addr = n.target<Device::AddressSettings>();
4,370 ✔
521
      const bool boolean = addr && addr->value.target<bool>();
4,370 !
522
      const bool impulse
4,370 ✔
523
          = addr && addr->value.get_type() == ossia::val_type::IMPULSE;
4,370 !
524

525
      if(boolean)
4,370 ✔
526
        f |= Qt::ItemIsUserCheckable;
329 ✔
527
      else if(!impulse)
4,041 ✔
528
        f |= Qt::ItemIsEditable;
3,710 ✔
529
    }
4,370 ✔
530

531
    if(index.column() == (int)Column::Name && n.is<Device::AddressSettings>()
14,819 ✔
532
       && canRenameNode(n))
10,041 ✔
533
    {
534
      f |= Qt::ItemIsEditable;
8,554 ✔
535
    }
8,554 ✔
536
  }
14,819 ✔
537
  else
538
  {
539
    // to be able to drop even where there is nothing
540
    f |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
×
541
  }
542

543
  return f;
14,819 ✔
544
}
545

546
/*
547
  return false if no change was made.
548
  dataChanged() & return true if a change is made.
549

550
  Note: this is the function that gets called when the user changes the value
551
  in the tree.
552
  It then sends a command that calls editData.
553
*/
554
bool DeviceExplorerModel::setData(
27 ✔
555
    const QModelIndex& index, const QVariant& value, int role)
556
{
557
  if(!index.isValid())
27 !
558
    return false;
×
559

560
  auto& n = nodeFromModelIndex(index);
27 ✔
561

562
  if(!n.is<Device::AddressSettings>())
27 !
563
    return false;
×
564

565
  auto col = Explorer::Column(index.column());
27 ✔
566

567
  if(role == Qt::EditRole || role == Qt::CheckStateRole)
27 !
568
  {
569
    if(col == Column::Value)
27 ✔
570
    {
571
      // In this case we don't make a command, but we directly push the
572
      // new value.
573
      // A check state is a tri-state enum, not a number: read it as the
574
      // boolean it stands for rather than leaving Qt::Checked == 2 to convert.
575
      auto copy = role == Qt::CheckStateRole
47 ✔
576
                      ? ossia::value{value.value<Qt::CheckState>() == Qt::Checked}
1 ✔
577
                  : value.canConvert<ossia::value>()
23 !
578
                      ? value.value<ossia::value>()
23 ✔
579
                      : State::convert::fromQVariant(value);
×
580

581
      // We may have to convert types.
582
      const ossia::value& orig = n.get<Device::AddressSettings>().value;
24 !
583
      if(copy.v.which() != orig.v.which() && !State::convert::convert(orig, copy))
24 !
584
        return false;
×
585

586
      n.get<Device::AddressSettings>().value = copy;
24 !
587

588
      // Note : if we want to disable remote updating, we have to do it
589
      // here (e.g. if this becomes a settings)
590
      m_devicePlugin.updateProxy.updateRemoteValue(Device::address(n).address, copy);
24 !
591

592
      dataChanged(index, index);
24 !
593
      valueUpdated(&n);
24 !
594
      return true;
24 ✔
595
    }
24 ✔
596
    else
597
    {
598
      // Here we make a command because we change the structure of the tree.
599
      auto settings = n.get<Device::AddressSettings>();
3 ✔
600
      if(col == Column::Name)
3 !
601
      {
602
        const QString s = value.toString();
3 !
603
        auto* parent = n.parent();
3 ✔
604
        if(s.isEmpty() || !parent || !canRenameNode(n))
3 !
605
          return false;
1 ✔
606

607
        auto renamed = n.get<Device::AddressSettings>();
2 !
608
        renamed.name = s;
2 ✔
609

610
        if(!checkAddressEditable(*parent, n.get<Device::AddressSettings>(), renamed))
2 !
611
          return false;
1 ✔
612

613
        settings.name = s;
1 ✔
614
      }
3 ✔
615

616
      if(settings != n.get<Device::AddressSettings>())
1 !
617
      {
618
        // We changed
619
        m_cmdQ.redoAndPush(new Explorer::Command::UpdateAddressSettings{
2 !
620
            this->deviceModel(), Device::NodePath{n}, settings});
1 !
621
        return true;
1 ✔
622
      }
623
    }
3 ✔
624
  }
×
625

626
  return false;
×
627
}
27 ✔
628

629
bool DeviceExplorerModel::setHeaderData(int, Qt::Orientation, const QVariant&, int)
×
630
{
631
  return false; // we prevent editing the (column) headers
×
632
}
633

634
/**
635
 * @brief DeviceExplorerModel::editData
636
 *
637
 * This functions gets called by the command
638
 * that edit the columns.
639
 */
640
void DeviceExplorerModel::editData(
×
641
    const Device::NodePath& path, Explorer::Column column, const ossia::value& value,
642
    int role)
643
{
644
  Device::Node* node = path.toNode(&rootNode());
×
645
  editData(*node, column, value, role);
×
646
}
×
647

648
void DeviceExplorerModel::editData(
1 ✔
649
    Device::Node& node, Column column, const ossia::value& value, int role)
650
{
651
  SCORE_ASSERT(node.parent());
1 !
652

653
  if(node.is<Device::DeviceSettings>())
1 !
654
    return;
×
655

656
  if(role == Qt::EditRole && column == Column::Value)
1 !
657
  {
658
    node.get<Device::AddressSettings>().value = value;
1 ✔
659
  }
1 ✔
660

661
  const QModelIndex index = modelIndexFromNode(node, (int)column);
1 ✔
662
  dataChanged(index, index);
1 !
663
}
1 ✔
664

665
QModelIndex DeviceExplorerModel::bottomIndex(const QModelIndex& index) const
×
666
{
667
  auto& node = nodeFromModelIndex(index);
×
668

669
  if(!node.hasChildren())
×
670
  {
671
    return index;
×
672
  }
673

674
  return bottomIndex(createIndex(
×
675
      node.childCount() - 1, index.column(), &node.childAt(node.childCount() - 1)));
×
676
}
×
677

678
bool DeviceExplorerModel::isDevice(QModelIndex index) const
×
679
{
680
  if(!index.isValid())
×
681
  {
682
    return false;
×
683
  }
684

685
  const Device::Node& n = nodeFromModelIndex(index);
×
686
  return n.is<Device::DeviceSettings>();
×
687
}
×
688

689
bool DeviceExplorerModel::isEmpty() const
2,197 ✔
690
{
691
  return m_rootNode.childCount() == 0;
2,197 ✔
692
}
693

694
/*
695
Drag and drop works by deleting the dragged items and creating a new set of
696
dropped items that match those dragged.
697
I will/may call insertRows(), removeRows(), dropMimeData(), ...
698
We define two MimeTypes : address and device.
699
It allows to distinguish whether we are drag'n dropping devices or addresses.
700
 */
701

702
Qt::DropActions DeviceExplorerModel::supportedDropActions() const
×
703
{
704
  return (Qt::CopyAction);
×
705
}
706

707
// Default supportedDragActions() implementation returns
708
// supportedDropActions().
709

710
Qt::DropActions DeviceExplorerModel::supportedDragActions() const
×
711
{
712
  return (Qt::CopyAction);
×
713
}
714

715
QStringList DeviceExplorerModel::mimeTypes() const
×
716
{
717
  return {score::mime::device(), score::mime::address()};
×
718
}
×
719

720
SelectedNodes
721
DeviceExplorerModel::uniqueSelectedNodes(const QModelIndexList& indexes) const
2 ✔
722
{
723
  SelectedNodes nodes;
2 ✔
724
  ossia::transform(
2 !
725
      indexes, std::back_inserter(nodes.parents),
2 !
726
      [&](const QModelIndex& idx) { return &nodeFromModelIndex(idx); });
4 ✔
727

728
  ossia::remove_erase(nodes.parents, &m_rootNode);
2 !
729

730
  nodes.messages.reserve(nodes.messages.size() + nodes.parents.size());
2 !
731
  if(nodes.parents.size() > 1)
2 !
732
  {
733
    // Filter messages
734
    for(auto node : nodes.parents)
×
735
    {
736
      if(node->is<Device::AddressSettings>())
×
737
      {
738
        auto& val = node->get<Device::AddressSettings>();
×
739
        if(val.ioType == ossia::access_mode::SET)
×
740
        {
741
          nodes.messages.push_back(node);
×
742
        }
×
743
      }
×
744
    }
745

746
    // Filter parents
747
    nodes.parents = filterUniqueParents(nodes.parents);
×
748
  }
×
749
  else if(nodes.parents.size() == 1)
2 !
750
  {
751
    Device::Node* node = nodes.parents[0];
2 ✔
752
    if(node->is<Device::AddressSettings>() && node->childCount() == 0)
2 !
753
    {
754
      // If a single node is selected we copy it even if it is "get"
755
      nodes.messages = std::move(nodes.parents);
1 ✔
756
    }
1 ✔
757
  }
2 ✔
758

759
  return nodes;
2 ✔
760
}
2 ✔
761
// method called when a drag is initiated
762
QMimeData* DeviceExplorerModel::mimeData(const QModelIndexList& indexes) const
1 ✔
763
{
764
  auto uniqueNodes = uniqueSelectedNodes(indexes);
1 ✔
765

766
  // Handle case of a single device which can have custom mimeData
767
  if(uniqueNodes.parents.size() == 1
1 ✔
768
     && uniqueNodes.parents[0]->is<Device::DeviceSettings>())
1 !
769
  {
770
    auto node = uniqueNodes.parents[0];
1 ✔
771
    if(node->is<Device::DeviceSettings>())
1 !
772
    {
773
      auto& dev = deviceModel().list().device(node->get<Device::DeviceSettings>().name);
1 !
774
      if(auto d = dev.mimeData())
1 !
775
        return d;
×
776
    }
1 ✔
777
  }
1 ✔
778

779
  auto mimeData = new QMimeData;
1 !
780

781
  // Now we request an update to the device explorer.
782
  m_devicePlugin.updateProxy.refreshRemoteValues(uniqueNodes.parents);
1 !
783

784
  // The "MessagesList" part.
785
  State::MessageList messages;
1 ✔
786
  messages.reserve(uniqueNodes.parents.size() + uniqueNodes.messages.size());
1 !
787
  for(const auto& node : uniqueNodes.parents)
2 ✔
788
  {
789
    Device::parametersList(*node, messages);
1 !
790
  }
791

792
  for(const auto& node : uniqueNodes.messages)
1 !
793
  {
794
    messages.push_back(Device::message(*node));
×
795
  }
796

797
  if(!messages.empty())
1 !
798
  {
799
    Mime<State::MessageList>::Serializer s{*mimeData};
1 !
800
    s.serialize(messages);
1 !
801
  }
1 ✔
802

803
  // The "Nodes" part. Deserialize with FreeNodeList to get the addresses.
804
  {
805
    Mime<Device::NodeList>::Serializer s{*mimeData};
1 !
806
    Device::NodeList vec;
1 ✔
807
    vec.reserve(uniqueNodes.parents.size() + uniqueNodes.messages.size());
1 !
808
    vec.insert(vec.end(), uniqueNodes.parents.begin(), uniqueNodes.parents.end());
1 !
809
    vec.insert(vec.end(), uniqueNodes.messages.begin(), uniqueNodes.messages.end());
1 !
810
    s.serialize(vec);
1 !
811
  }
1 ✔
812

813
  if(messages.empty() && uniqueNodes.parents.empty() && uniqueNodes.messages.empty())
1 !
814
  {
815
    delete mimeData;
×
816
    return nullptr;
×
817
  }
818

819
  return mimeData;
1 ✔
820
}
1 ✔
821

822
bool DeviceExplorerModel::canDropMimeData(
×
823
    const QMimeData* mimeData, Qt::DropAction action, int /*row*/, int /*column*/,
824
    const QModelIndex& parent) const
825
{
826
  if(action == Qt::IgnoreAction)
×
827
  {
828
    return true;
×
829
  }
830

831
  if(action != Qt::MoveAction && action != Qt::CopyAction)
×
832
  {
833
    return false;
×
834
  }
835

836
  if(!mimeData
×
837
     || (!mimeData->hasFormat(score::mime::device())
×
838
         && !mimeData->hasFormat(score::mime::address())))
×
839
  {
840
    return false;
×
841
  }
842

843
  const Device::Node& parentNode = nodeFromModelIndex(parent);
×
844

845
  if(mimeData->hasFormat(score::mime::address()))
×
846
  {
847
    if(&parentNode == &m_rootNode)
×
848
    {
849
      return false;
×
850
    }
851
  }
×
852
  else
853
  {
854
    SCORE_ASSERT(mimeData->hasFormat(score::mime::device()));
×
855

856
    if(&parentNode != &m_rootNode)
×
857
    {
858
      return false;
×
859
    }
860
  }
861

862
  return true;
×
863
}
×
864

865
// method called when a drop occurs
866
// return true if drop really handled, false otherwise.
867
//
868
// if dropMimeData returns true && action==Qt::MoveAction, removeRows is called
869
// immediately after
870
bool DeviceExplorerModel::dropMimeData(
×
871
    const QMimeData* mimeData, Qt::DropAction action, int row, int column,
872
    const QModelIndex& parent)
873
{
874
  if(action == Qt::IgnoreAction)
×
875
  {
876
    return true;
×
877
  }
878

879
  if(action != Qt::MoveAction && action != Qt::CopyAction)
×
880
  {
881
    return false;
×
882
  }
883

884
  if(!mimeData
×
885
     || (!mimeData->hasFormat(score::mime::device())
×
886
         && !mimeData->hasFormat(score::mime::address())))
×
887
  {
888
    return false;
×
889
  }
890

891
  QModelIndex parentIndex; // invalid
×
892
  Device::Node* parentNode = &m_rootNode;
×
893
  QString mimeType = score::mime::device();
×
894

895
  if(mimeData->hasFormat(score::mime::address()))
×
896
  {
897
    parentIndex = parent;
×
898
    parentNode = &nodeFromModelIndex(parent);
×
899
    mimeType = score::mime::address();
×
900

901
    if(parentNode == &m_rootNode)
×
902
    {
903
      return false;
×
904
    }
905
  }
×
906
  else
907
  {
908
    SCORE_ASSERT(mimeData->hasFormat(score::mime::device()));
×
909
    SCORE_ASSERT(mimeType == score::mime::device());
×
910
  }
911

912
  if(parentNode)
×
913
  {
914
    // Note : when dropping a device,
915
    // if there is an existing device that would use the same ports, etc.
916
    // we have to open a dialog to change the device settings.
917

918
    const auto& json = readJson(mimeData->data(mimeType));
×
919
    JSONObject::Deserializer deser{json};
×
920
    Device::Node n;
×
921
    deser.writeTo(n);
×
922

923
    if(mimeType == score::mime::device())
×
924
    {
925
      checkAndLoadDevice(n);
×
926
    }
×
927
    return true;
×
928
  }
×
929
  return false;
×
930
}
×
931

932
void DeviceExplorerModel::checkAndLoadDevice(Device::Node n)
×
933
{
934
  SCORE_ASSERT(n.is<Device::DeviceSettings>());
×
935
  auto& deviceSettings = *n.target<Device::DeviceSettings>();
×
936

937
  // We ask the user to fix the incompatibilities by himself, maybe change
938
  // the name also.
939
  auto dialog = new DeviceEditDialog{
×
940
      *this, m_devicePlugin.context().app.interfaces<Device::ProtocolFactoryList>(),
×
941
      DeviceEditDialog::Creating, QApplication::activeWindow()};
×
942

943
  dialog->setSettings(deviceSettings);
×
944
  dialog->setBrowserEnabled(false);
×
945

946
  connect(
×
947
      dialog, &QDialog::accepted, this, [this, dialog, node = std::move(n)]() mutable {
×
948
        auto new_settings = dialog->getSettings();
×
949
        auto& deviceSettings = *node.target<Device::DeviceSettings>();
×
950
        deviceSettings = new_settings;
×
951
        if(!checkDeviceInstantiatable(deviceSettings))
×
952
        {
953
          score::warning(
×
954
              m_devicePlugin.context().app.mainWindow, "Error",
×
955
              "Could not create the device");
×
956
          dialog->deleteLater();
×
957
          return;
×
958
        }
959
        ossia::net::sanitize_device_name(deviceSettings.name);
×
960

961
        auto cmd = new Command::LoadDevice{deviceModel(), std::move(node)};
×
962
        m_cmdQ.redoAndPush(cmd);
×
963
        dialog->deleteLater();
×
964
      });
×
965
  connect(dialog, &QDialog::rejected, this, [dialog] { dialog->deleteLater(); });
×
966
  dialog->show();
×
967
}
×
968

969
void DeviceExplorerModel::checkAndLoadDevice(Device::DeviceSettings deviceSettings)
×
970
{
971
  // We ask the user to fix the incompatibilities by himself, maybe change
972
  // the name also.
973
  auto dialog = new DeviceEditDialog{
×
974
      *this, m_devicePlugin.context().app.interfaces<Device::ProtocolFactoryList>(),
×
975
      DeviceEditDialog::Creating, QApplication::activeWindow()};
×
976

977
  dialog->setSettings(deviceSettings);
×
978
  dialog->setBrowserEnabled(false);
×
979

980
  connect(dialog, &QDialog::accepted, this, [this, dialog] {
×
981
    auto node = dialog->getDevice();
×
982
    auto& deviceSettings = *node.target<Device::DeviceSettings>();
×
983
    if(!checkDeviceInstantiatable(deviceSettings))
×
984
    {
985
      score::warning(
×
986
          m_devicePlugin.context().app.mainWindow, "Error",
×
987
          "Could not create the device");
×
988
      dialog->deleteLater();
×
989
      return;
×
990
    }
991
    ossia::net::sanitize_device_name(deviceSettings.name);
×
992

993
    auto cmd = new Command::LoadDevice{deviceModel(), std::move(node)};
×
994
    m_cmdQ.redoAndPush(cmd);
×
995
    dialog->deleteLater();
×
996
  });
×
997
  connect(dialog, &QDialog::rejected, this, [dialog] { dialog->deleteLater(); });
×
998
  dialog->show();
×
999
}
×
1000

1001
void DeviceExplorerModel::debug_printPath(const Device::NodePath& path)
×
1002
{
1003
  const int pathSize = std::ssize(path);
×
1004

1005
  for(int i = 0; i < pathSize; ++i)
×
1006
  {
1007
    std::cerr << path.at(i) << " ";
×
1008
  }
×
1009

1010
  std::cerr << "\n";
×
1011
}
×
1012

1013
void DeviceExplorerModel::debug_printIndexes(const QModelIndexList& indexes)
×
1014
{
1015
  std::cerr << "indexes: " << indexes.size() << " nodes: \n";
×
1016
  Q_FOREACH(const QModelIndex& index, indexes)
×
1017
  {
1018
    if(index.isValid())
×
1019
    {
1020
      std::cerr << " index.row=" << index.row() << " col=" << index.column() << " ";
×
1021
      Device::Node* n = &nodeFromModelIndex(index);
×
1022
      std::cerr << " n=" << n << " ";
×
1023
      Device::Node* parent = n->parent();
×
1024

1025
      if(n == &m_rootNode)
×
1026
      {
1027
        std::cerr << " rootNode parent=" << parent << "\n";
×
1028
      }
×
1029
      else
1030
      {
1031
        std::cerr << " n->name=" << n->displayName().toStdString();
×
1032
        std::cerr << " parent=" << parent;
×
1033
        std::cerr << " parent->name=" << parent->displayName().toStdString() << "\n";
×
1034
      }
1035
    }
×
1036
    else
1037
    {
1038
      std::cerr << " invalid index \n";
×
1039
    }
1040
  }
1041
}
×
1042

1043
State::MessageList getSelectionSnapshot(DeviceExplorerModel& model)
1 ✔
1044
{
1045
  // Filter
1046
  auto uniqueNodes = model.uniqueSelectedNodes(model.selectedIndexes());
1 !
1047

1048
  // Recursive refresh
1049
  model.deviceModel().updateProxy.refreshRemoteValues(uniqueNodes.parents);
1 !
1050

1051
  // Conversion
1052
  State::MessageList messages;
1 ✔
1053
  messages.reserve(uniqueNodes.parents.size() + uniqueNodes.messages.size());
1 !
1054
  for(const auto& node : uniqueNodes.parents)
1 !
1055
  {
1056
    Device::parametersList(*node, messages);
×
1057
  }
1058
  for(const auto& node : uniqueNodes.messages)
2 ✔
1059
  {
1060
    messages.push_back(Device::message(*node));
1 !
1061
  }
1062

1063
  return messages;
1 ✔
1064
}
1 ✔
1065

1066
DeviceExplorerModel* try_deviceExplorerFromObject(const QObject& obj)
4,738 ✔
1067
{
1068
  auto plug = score::IDocument::documentContext(obj).findPlugin<DeviceDocumentPlugin>();
4,738 ✔
1069
  if(plug)
4,738 !
1070
    return &plug->explorer();
4,738 ✔
1071
  return nullptr;
×
1072
}
4,738 ✔
1073

1074
DeviceExplorerModel& deviceExplorerFromObject(const QObject& obj)
6 ✔
1075
{
1076
  auto expl = try_deviceExplorerFromObject(obj);
6 ✔
1077
  SCORE_ASSERT(expl);
6 !
1078
  return *expl;
6 ✔
1079
}
1080

1081
DeviceExplorerModel* try_deviceExplorerFromContext(const score::DocumentContext& ctx)
16 ✔
1082
{
1083
  auto plug = ctx.findPlugin<DeviceDocumentPlugin>();
16 ✔
1084
  if(plug)
16 !
1085
    return &plug->explorer();
16 ✔
1086
  return nullptr;
×
1087
}
16 ✔
1088

1089
DeviceExplorerModel& deviceExplorerFromContext(const score::DocumentContext& ctx)
16 ✔
1090
{
1091
  auto expl = try_deviceExplorerFromContext(ctx);
16 ✔
1092
  SCORE_ASSERT(expl);
16 !
1093
  return *expl;
16 ✔
1094
}
1095

1096
Device::FullAddressAccessorSettings makeFullAddressAccessorSettings(
5 ✔
1097
    const State::AddressAccessor& addr, const score::DocumentContext& ctx,
1098
    ossia::value min, ossia::value max, ossia::value val)
1099
{
1100
  return Device::makeFullAddressAccessorSettings(
5 !
1101
      addr, ctx, std::move(min), std::move(max), std::move(val));
5 ✔
1102
}
×
1103
}
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