• 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

64.55
/src/plugins/score-plugin-deviceexplorer/Explorer/Explorer/DeviceExplorerView.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 "DeviceExplorerView.hpp"
4

5
#include "DeviceExplorerFilterProxyModel.hpp"
6
#include "DeviceExplorerModel.hpp"
7

8
#include <score/model/Skin.hpp>
9
#include <score/widgets/CursorTheme.hpp>
10
#include <score/widgets/Pixmap.hpp>
11
#if defined(__EMSCRIPTEN__)
12
#include <score/widgets/ItemViewDrag.hpp>
13

14
#include <QStyleOptionViewItem>
15
#endif
16

17
#include <ossia/detail/ssize.hpp>
18

19
#include <QAbstractItemView>
20
#include <QAbstractProxyModel>
21
#include <QAction>
22
#include <QDebug>
23
#include <QDrag>
24
#include <QFile>
25
#include <QHeaderView>
26
#include <QKeyEvent>
27
#include <QMenu>
28
#include <QPainter>
29
#include <QSettings>
30
#include <QString>
31

32
W_REGISTER_ARGTYPE(QItemSelection)
33

34
#include <wobjectimpl.h>
35
W_OBJECT_IMPL(Explorer::DeviceExplorerView)
86,560 !
36
namespace
37
{
38
const QString GeometrySetting("DeviceExplorerView/Geometry");
1,026 ✔
39
const QString HeaderViewSetting("DeviceExplorerView/HeaderView");
1,026 ✔
40
}
41

42
namespace Explorer
43
{
44
DeviceExplorerView::DeviceExplorerView(QWidget* parent)
6,678 ✔
45
    : QTreeView(parent)
3,339 ✔
46
    , m_hasProxy(false)
3,339 ✔
47
{
3,339 ✔
48
  setAllColumnsShowFocus(true);
3,339 !
49

50
  setSelectionBehavior(QAbstractItemView::SelectRows);
3,339 !
51
  setSelectionMode(QAbstractItemView::ExtendedSelection);
3,339 !
52
  setAlternatingRowColors(true);
3,339 !
53

54
  header()->setContextMenuPolicy(Qt::CustomContextMenu);
3,339 !
55
  connect(
3,339 !
56
      header(), &QWidget::customContextMenuRequested, this,
3,339 !
57
      &DeviceExplorerView::headerMenuRequested);
3,339 ✔
58

59
  header()->setMaximumHeight(20);
3,339 !
60
  //- Drag'n Drop.
61

62
  setDragEnabled(true);
3,339 !
63
  setAcceptDrops(true);
3,339 !
64
  setUniformRowHeights(true);
3,339 !
65

66
  setDragDropMode(QAbstractItemView::DragDrop);
3,339 !
67
  setDropIndicatorShown(true);
3,339 !
68

69
  setDefaultDropAction(Qt::MoveAction);
3,339 !
70

71
  setDragDropOverwriteMode(false);
3,339 !
72
}
3,339 ✔
73

74
DeviceExplorerView::~DeviceExplorerView()
6,676 ✔
75
{
3,338 ✔
76
  saveSettings();
3,338 !
77
}
6,676 ✔
UNCOV
78
QModelIndexList DeviceExplorerView::selectedDraggableIndexes() const
×
79
{
80
  QModelIndexList indexes = selectedIndexes();
×
81
  auto m = QTreeView::model();
×
82
  auto isNotDragEnabled = [m](const QModelIndex& index) {
×
UNCOV
83
    return !(m->flags(index) & Qt::ItemIsDragEnabled);
×
84
  };
85
  indexes.erase(
×
86
      std::remove_if(indexes.begin(), indexes.end(), isNotDragEnabled), indexes.end());
×
87
  return indexes;
×
UNCOV
88
}
×
89

UNCOV
90
void DeviceExplorerView::startDrag(Qt::DropActions)
×
91
{
92
  QModelIndexList indexes = selectedDraggableIndexes();
×
UNCOV
93
  if(indexes.count() > 0)
×
94
  {
95
    QMimeData* data = QTreeView::model()->mimeData(indexes);
×
96
    if(!data)
×
UNCOV
97
      return;
×
98

99
    QDrag* drag = new QDrag(this);
×
UNCOV
100
    drag->setMimeData(data);
×
UNCOV
101
    score::CursorTheme::instance().setDragCursors(*drag);
×
102
#if defined(__EMSCRIPTEN__)
103
    QStyleOptionViewItem opt;
104
    initViewItemOption(&opt);
105
    score::setItemViewDragPixmap(*drag, *this, opt, indexes);
106
#endif
UNCOV
107
    drag->exec();
×
UNCOV
108
  }
×
UNCOV
109
}
×
110

111
void DeviceExplorerView::setInitialColumnsSizes()
1,086 ✔
112
{
113
  SCORE_ASSERT(model());
1,086 !
114

115
  header()->resizeSection((int)Explorer::Column::Name, 180);
1,086 ✔
116
  header()->resizeSection((int)Explorer::Column::Value, 50);
1,086 ✔
117
}
1,086 ✔
118

119
void DeviceExplorerView::saveSettings()
3,338 ✔
120
{
121
  QSettings settings;
3,338 ✔
122
  settings.setValue(GeometrySetting, saveGeometry());
3,338 !
123
  settings.setValue(HeaderViewSetting, header()->saveState());
3,338 !
124
}
3,338 ✔
125

UNCOV
126
void DeviceExplorerView::restoreSettings()
×
127
{
UNCOV
128
  QSettings settings;
×
UNCOV
129
  if(settings.contains(GeometrySetting))
×
130
  {
UNCOV
131
    restoreGeometry(settings.value(GeometrySetting).toByteArray());
×
UNCOV
132
    header()->restoreState(settings.value(HeaderViewSetting).toByteArray());
×
UNCOV
133
    this->updateGeometry();
×
UNCOV
134
  }
×
135
}
×
136

137
void DeviceExplorerView::setModel(QAbstractItemModel* model)
4,344 ✔
138
{
139
  m_hasProxy = false;
4,344 ✔
140
  QTreeView::setModel(model);
4,344 ✔
141
  // TODO review the save/restore system
142
  if(model)
4,344 !
143
  {
144
    setInitialColumnsSizes();
×
145
    //        restoreSettings();
UNCOV
146
    initActions(); // after restoreSettings() to have actions correctly
×
147
    // initialized
UNCOV
148
  }
×
149
}
4,344 ✔
150

151
void DeviceExplorerView::setModel(DeviceExplorerFilterProxyModel* model)
1,086 ✔
152
{
153
  QTreeView::setModel(static_cast<QAbstractItemModel*>(model));
1,086 ✔
154
  m_hasProxy = true;
1,086 ✔
155

156
  if(model)
1,086 !
157
  {
158
    setInitialColumnsSizes();
1,086 ✔
159
    //        restoreSettings();
160
    initActions(); // after restoreSettings() to have actions correctly
1,086 ✔
161
    // initialized
162
  }
1,086 ✔
163
}
1,086 ✔
164

165
DeviceExplorerModel* DeviceExplorerView::model()
14,673 ✔
166
{
167
  if(!m_hasProxy)
14,673 ✔
168
  {
169
    return static_cast<DeviceExplorerModel*>(QTreeView::model());
4,334 ✔
170
  }
171
  else
172
  {
173
    return static_cast<DeviceExplorerModel*>(
10,339 ✔
174
        static_cast<QAbstractProxyModel*>(QTreeView::model())->sourceModel());
10,339 ✔
175
  }
176
}
14,673 ✔
177

UNCOV
178
const DeviceExplorerModel* DeviceExplorerView::model() const
×
179
{
UNCOV
180
  auto m = QTreeView::model();
×
UNCOV
181
  if(!m)
×
UNCOV
182
    return nullptr;
×
183

UNCOV
184
  if(!m_hasProxy)
×
185
  {
UNCOV
186
    return static_cast<const DeviceExplorerModel*>(m);
×
187
  }
188
  else
189
  {
190
    auto model = static_cast<const QAbstractProxyModel*>(m);
×
191
    return qobject_cast<const DeviceExplorerModel*>(model->sourceModel());
×
192
  }
193
}
×
194

195
void DeviceExplorerView::initActions()
1,086 ✔
196
{
197
  SCORE_ASSERT(model());
1,086 !
198
  const int n = model()->columnCount();
1,086 ✔
199
  m_actions.reserve(n);
1,086 ✔
200

201
  for(int i = 0; i < n; ++i)
3,258 ✔
202
  {
203
    QAction* a = new QAction(
4,344 !
204
        model()->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(), this);
2,172 !
205
    a->setCheckable(true);
2,172 ✔
206

207
    a->setChecked(!isColumnHidden(i));
2,172 ✔
208

209
    connect(a, &QAction::toggled, this, &DeviceExplorerView::columnVisibilityChanged);
2,172 ✔
210
    m_actions.append(a);
2,172 ✔
211
  }
2,172 ✔
212
}
1,086 ✔
213
bool DeviceExplorerView::hasProxy() const
897 ✔
214
{
215
  return m_hasProxy;
897 ✔
216
}
217

UNCOV
218
void DeviceExplorerView::columnVisibilityChanged(bool shown)
×
219
{
UNCOV
220
  QAction* a = qobject_cast<QAction*>(sender());
×
UNCOV
221
  SCORE_ASSERT(a);
×
UNCOV
222
  const int ind = m_actions.indexOf(a);
×
UNCOV
223
  SCORE_ASSERT(ind != -1);
×
UNCOV
224
  setColumnHidden(ind, !shown);
×
UNCOV
225
}
×
226

227
void DeviceExplorerView::keyPressEvent(QKeyEvent* k)
×
228
{
229
  if(k->key() == Qt::Key_Escape)
×
230
  {
231
    clearSelection();
×
232
    k->accept();
×
233
  }
×
234
  else
235
  {
236
    QTreeView::keyPressEvent(k);
×
237
  }
238
}
×
239

240
void DeviceExplorerView::rowsInserted(const QModelIndex& parent, int start, int end)
175 ✔
241
{
242
  QTreeView::rowsInserted(parent, start, end);
175 ✔
243
  created(parent, start, end);
175 ✔
244
}
175 ✔
245

UNCOV
246
void DeviceExplorerView::headerMenuRequested(const QPoint& pos)
×
247
{
UNCOV
248
  QMenu contextMenu(this);
×
UNCOV
249
  const int n = std::ssize(m_actions);
×
250

UNCOV
251
  for(int i = 0; i < n; ++i)
×
252
  {
UNCOV
253
    contextMenu.addAction(m_actions.at(i));
×
UNCOV
254
  }
×
255

UNCOV
256
  contextMenu.exec(mapToGlobal(pos));
×
257
}
×
258

259
void DeviceExplorerView::selectionChanged(
5,444 ✔
260
    const QItemSelection& selected, const QItemSelection& deselected)
261
{
262
  QTreeView::selectionChanged(selected, deselected);
5,444 ✔
263
  selectionChanged();
5,444 ✔
264
}
5,444 ✔
265

266
QModelIndexList DeviceExplorerView::selectedIndexes() const
5,929 ✔
267
{
268
  // As we have done setSelectionBehavior(QAbstractItemView::SelectRows)
269
  // we get the indexes for each column corresponding to a selected row.
270
  // We want only the indexes of the NAME column.
271

272
  const int col = (int)Explorer::Column::Name;
5,929 ✔
273
  QModelIndexList l = QTreeView::selectedIndexes();
5,929 ✔
274

275
  QModelIndexList l0;
5,929 ✔
276
  Q_FOREACH(const QModelIndex& index, l)
5,983 !
277
  {
278
    if(index.column() == col)
54 ✔
279
    {
280

281
      if(!index.isValid())
27 !
282
      {
UNCOV
283
        qDebug() << " !!! invalid index in selection !!!";
×
UNCOV
284
      }
×
285

286
      l0.append(index);
27 !
287
      // REM: we append index in ProxyModel if m_hasProxy
288
      // it is necessary because drag'n drop will automatically call
289
      // selectedIndexes() & mapToSource().
290
    }
27 ✔
291
  }
292

293
  return l0;
5,929 ✔
294
}
5,929 ✔
295

296
// REM: use selectedIndex() & setSelectedIndex()
297
// instead of currentIndex() & setCurrentIndex()
298
// to take into account proxy presence.
299

300
QModelIndex DeviceExplorerView::selectedIndex() const
21 ✔
301
{
302
  // Selecting does not always move the current index
303
  QModelIndex index = currentIndex();
21 ✔
304
  if(auto sel = selectionModel())
21 !
305
    if(const auto rows = sel->selectedRows(); rows.size() == 1)
42 !
306
      index = rows.front();
21 ✔
307

308
  if(!m_hasProxy)
21 !
UNCOV
309
    return index;
×
310
  return static_cast<const QAbstractProxyModel*>(QTreeView::model())->mapToSource(index);
21 ✔
311
}
21 ✔
312

313
void DeviceExplorerView::setSelectedIndex(const QModelIndex& index)
4 ✔
314
{
315
  if(!m_hasProxy)
4 !
316
  {
UNCOV
317
    return setCurrentIndex(index);
×
318
  }
319
  else
320
  {
321
    return setCurrentIndex(static_cast<const QAbstractProxyModel*>(QTreeView::model())
8 ✔
322
                               ->mapFromSource(index));
4 ✔
323
  }
324
}
4 ✔
325
void DeviceExplorerView::paintEvent(QPaintEvent* event)
107 ✔
326
{
327
  QTreeView::paintEvent(event);
107 ✔
328
  if(!this->isEnabled())
107 ✔
329
    return;
28 ✔
330
  if(model() && model()->rowCount(rootIndex()) > 0)
79 !
331
    return;
41 ✔
332

333
  QPainter p{this->viewport()};
38 !
334
  const auto& skin = score::Skin::instance();
38 ✔
335
  auto font = skin.Bold12Pt;
38 !
336
  // px, not pt: pt shrinks on macOS' 72 DPI. Scaled so the one oversized
337
  // piece of text in the UI stays in proportion under a small-font skin.
338
  score::setSnappedPixelSize(font, score::scaledPixels(32));
38 ✔
339
  p.setFont(font);
38 !
340
  auto pen = p.pen();
38 !
341
  auto col = pen.color();
38 !
342
  col.setAlphaF(0.5);
38 !
343
  pen.setColor(col);
38 !
344
  p.setPen(pen);
38 !
345
  p.drawText(rect(), Qt::AlignCenter, "Right-click\nto add a device");
38 !
346
}
107 ✔
347
}
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