File: screenlistmodel.cpp

package info (click to toggle)
qt6-multimedia 6.9.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,304 kB
  • sloc: cpp: 178,954; ansic: 72,398; java: 2,784; python: 262; xml: 193; sh: 136; makefile: 30
file content (54 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "screenlistmodel.h"

#include <QGuiApplication>
#include <QScreen>

#include <QTextStream>

ScreenListModel::ScreenListModel(QObject *parent) :
      QAbstractListModel(parent)
{
    auto *app = qApp;
    connect(app, &QGuiApplication::screenAdded, this, &ScreenListModel::screensChanged);
    connect(app, &QGuiApplication::screenRemoved, this, &ScreenListModel::screensChanged);
    connect(app, &QGuiApplication::primaryScreenChanged, this, &ScreenListModel::screensChanged);
}

int ScreenListModel::rowCount(const QModelIndex &) const
{
    return QGuiApplication::screens().size();
}

QVariant ScreenListModel::data(const QModelIndex &index, int role) const
{
    const auto screenList = QGuiApplication::screens();
    Q_ASSERT(index.isValid());
    Q_ASSERT(index.row() <= screenList.size());

    if (role == Qt::DisplayRole) {
        auto *screen = screenList.at(index.row());
        QString description;
        QTextStream str(&description);
        str << '"' << screen->name() << "\" " << screen->size().width() << 'x'
            << screen->size().height() << ", " << screen->logicalDotsPerInch() << "DPI";
        return description;
    }

    return {};
}

QScreen *ScreenListModel::screen(const QModelIndex &index) const
{
    return QGuiApplication::screens().value(index.row());
}

void ScreenListModel::screensChanged()
{
    beginResetModel();
    endResetModel();
}

#include "moc_screenlistmodel.cpp"