Lomiri
deviceconfig.cpp
1 /*
2  * Copyright 2016 Canonical Ltd.
3  * Copyright 2019-2022 UBports Foundation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "deviceconfig.h"
19 
20 #include <deviceinfo.h>
21 #include <QDebug>
22 
23 DeviceConfig::DeviceConfig(QObject *parent):
24  QObject(parent),
25  m_info(std::make_unique<DeviceInfo>())
26 {
27 }
28 
29 DeviceConfig::~DeviceConfig() = default;
30 
31 QString DeviceConfig::name() const
32 {
33  return QString::fromStdString(m_info->name());
34 }
35 
36 Qt::ScreenOrientation DeviceConfig::primaryOrientation() const
37 {
38  return stringToOrientation(m_info->primaryOrientation(), Qt::PrimaryOrientation);
39 }
40 
41 Qt::ScreenOrientations DeviceConfig::supportedOrientations() const
42 {
43  auto values = m_info->supportedOrientations();
44  if (values.empty()) {
45  return Qt::PortraitOrientation
46  | Qt::InvertedPortraitOrientation
47  | Qt::LandscapeOrientation
48  | Qt::InvertedLandscapeOrientation;
49  }
50 
51  Qt::ScreenOrientations ret = Qt::PrimaryOrientation;
52  for (auto orientationString : values) {
53  ret |= stringToOrientation(orientationString, Qt::PrimaryOrientation);
54  }
55  return ret;
56 }
57 
58 Qt::ScreenOrientation DeviceConfig::landscapeOrientation() const
59 {
60  return stringToOrientation(m_info->landscapeOrientation(), Qt::LandscapeOrientation);
61 }
62 
63 Qt::ScreenOrientation DeviceConfig::invertedLandscapeOrientation() const
64 {
65  return stringToOrientation(m_info->invertedLandscapeOrientation(), Qt::InvertedLandscapeOrientation);
66 }
67 
68 Qt::ScreenOrientation DeviceConfig::portraitOrientation() const
69 {
70  return stringToOrientation(m_info->portraitOrientation(), Qt::PortraitOrientation);
71 }
72 
73 Qt::ScreenOrientation DeviceConfig::invertedPortraitOrientation() const
74 {
75  return stringToOrientation(m_info->invertedPortraitOrientation(), Qt::InvertedPortraitOrientation);
76 }
77 
78 QString DeviceConfig::category() const
79 {
80  QStringList supportedValues = {"phone", "tablet", "desktop"};
81  QString value = QString::fromStdString(DeviceInfo::deviceTypeToString(m_info->deviceType()));
82  if (!supportedValues.contains(value)) {
83  qWarning().nospace().noquote() << "Unknown option \"" << value
84  << ". Supported options are: " << supportedValues.join(", ") << ".";
85  return "phone";
86  }
87  return value;
88 }
89 
90 Qt::ScreenOrientation DeviceConfig::stringToOrientation(const std::string &orientationString, Qt::ScreenOrientation defaultValue) const
91 {
92  if (orientationString == "Landscape") {
93  return Qt::LandscapeOrientation;
94  }
95  if (orientationString == "InvertedLandscape") {
96  return Qt::InvertedLandscapeOrientation;
97  }
98  if (orientationString == "Portrait") {
99  return Qt::PortraitOrientation;
100  }
101  if (orientationString == "InvertedPortrait") {
102  return Qt::InvertedPortraitOrientation;
103  }
104  if (!orientationString.empty()) {
105  // Some option we don't know. Give some hint on what went wrong.
106  qWarning().nospace().noquote() << "Unknown option \"" << QString::fromStdString(orientationString)
107  << ". Supported options are: Landscape, InvertedLandscape, Portrait and InvertedPortrait.\"";
108  }
109  return defaultValue;
110 }
111 
112 bool DeviceConfig::supportsMultiColorLed() const
113 {
114  return m_info->contains("supportsMultiColorLed");
115 }