Lomiri
Loading...
Searching...
No Matches
displaycutoutsmodel.cpp
1/*
2 * Copyright (C) 2026 UBports Foundation.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "displaycutoutsmodel.h"
18
19#include <QDebug>
20#include <deviceinfo.h>
21
22QRect parseRect(QString rect)
23{
24 auto parts = rect.split(" ");
25 if (parts.size() != 4) {
26 return QRect();
27 }
28 return QRect(
29 parts[0].toInt(), parts[1].toInt(),
30 parts[2].toInt(), parts[3].toInt()
31 );
32}
33
34DisplayCutoutsModel::DisplayCutoutsModel(QObject *parent)
35 : QAbstractListModel(parent)
36{
37 loadConfig();
38}
39
40void DisplayCutoutsModel::loadConfig()
41{
42 DeviceInfo info;
43 if (!info.contains("DisplayCutoutsCollapsed")) {
44 qWarning() << "DisplayCutouts not configured, not enabling";
45 return;
46 }
47
48 m_collapsedCutouts.clear();
49 m_collapsedLightCutouts.clear();
50 m_expandedCutouts.clear();
51 auto collapsedCutouts = QString::fromStdString(info.get("DisplayCutoutsCollapsed", ""));
52 for (auto cutout : collapsedCutouts.split(",")) {
53 m_collapsedCutouts.append(parseRect(cutout));
54 }
55 if (info.contains("DisplayCutoutsCollapsedLight")) {
56 auto collapsedLightCutouts = QString::fromStdString(info.get("DisplayCutoutsCollapsedLight", ""));
57 for (auto cutout : collapsedLightCutouts.split(",")) {
58 m_collapsedLightCutouts.append(parseRect(cutout));
59 }
60 }
61 if (info.contains("DisplayCutoutsExpanded")) {
62 auto expandedCutouts = QString::fromStdString(info.get("DisplayCutoutsExpanded", ""));
63 for (auto cutout : expandedCutouts.split(",")) {
64 m_expandedCutouts.append(parseRect(cutout));
65 }
66 }
67}
68
69void DisplayCutoutsModel::setExpanded(bool value)
70{
71 if (m_expanded != value) {
72 beginResetModel();
73 m_expanded = value;
74 endResetModel();
75 }
76}
77
78void DisplayCutoutsModel::setOrientation(Qt::ScreenOrientation value)
79{
80 if (m_orientation != value) {
81 beginResetModel();
82 m_orientation = value;
83 endResetModel();
84 }
85}
86
87void DisplayCutoutsModel::setEnabled(bool value)
88{
89 if (m_enabled != value) {
90 beginResetModel();
91 m_enabled = value;
92 endResetModel();
93 }
94}
95
96void DisplayCutoutsModel::setLightMode(bool value)
97{
98 if (m_lightMode != value) {
99 beginResetModel();
100 m_lightMode = value;
101 endResetModel();
102 }
103}
104
105int DisplayCutoutsModel::rowCount(const QModelIndex &parent) const
106{
107 Q_UNUSED(parent);
108
109 if (m_orientation != Qt::PortraitOrientation || !m_enabled) {
110 return 0;
111 }
112 auto collapsed = m_lightMode && !m_collapsedLightCutouts.empty()
113 ? &m_collapsedLightCutouts : &m_collapsedCutouts;
114 return m_expanded ? m_expandedCutouts.count() : collapsed->count();
115}
116
117QVariant DisplayCutoutsModel::data(const QModelIndex &index, int role) const
118{
119 Q_UNUSED(role);
120
121 auto collapsed = m_lightMode && !m_collapsedLightCutouts.empty()
122 ? &m_collapsedLightCutouts : &m_collapsedCutouts;
123 auto data = m_expanded ? &m_expandedCutouts : collapsed;
124 if (!index.isValid() || index.row() >= data->count()) {
125 return {};
126 }
127
128 return QVariant(data->at(index.row()));
129}
130
131void DisplayCutoutsModel::reloadConfig()
132{
133 qDebug() << "config reload requested...";
134 beginResetModel();
135 loadConfig();
136 endResetModel();
137}