Changeset View
Changeset View
Standalone View
Standalone View
orientation_sensor.cpp
Show All 12 Lines | |||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | GNU General Public License for more details. | 15 | GNU General Public License for more details. | ||
16 | 16 | | |||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | #include "orientation_sensor.h" | 20 | #include "orientation_sensor.h" | ||
21 | #include <orientationsensoradaptor.h> | ||||
21 | 22 | | |||
22 | #include <QOrientationSensor> | 23 | #include <QOrientationSensor> | ||
23 | #include <QOrientationReading> | 24 | #include <QOrientationReading> | ||
24 | 25 | | |||
26 | #include <KConfigGroup> | ||||
25 | #include <KStatusNotifierItem> | 27 | #include <KStatusNotifierItem> | ||
26 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
27 | 29 | | |||
28 | namespace KWin | 30 | namespace KWin | ||
29 | { | 31 | { | ||
30 | 32 | | |||
31 | OrientationSensor::OrientationSensor(QObject *parent) | 33 | OrientationSensor::OrientationSensor(QObject *parent) | ||
32 | : QObject(parent) | 34 | : QObject(parent) | ||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | |||||
79 | 81 | | |||
80 | void OrientationSensor::setEnabled(bool enabled) | 82 | void OrientationSensor::setEnabled(bool enabled) | ||
81 | { | 83 | { | ||
82 | if (m_enabled == enabled) { | 84 | if (m_enabled == enabled) { | ||
83 | return; | 85 | return; | ||
84 | } | 86 | } | ||
85 | m_enabled = enabled; | 87 | m_enabled = enabled; | ||
86 | if (m_enabled) { | 88 | if (m_enabled) { | ||
89 | loadConfig(); | ||||
87 | setupStatusNotifier(); | 90 | setupStatusNotifier(); | ||
91 | m_adaptor = new OrientationSensorAdaptor(this); | ||||
88 | } else { | 92 | } else { | ||
89 | delete m_sni; | 93 | delete m_sni; | ||
90 | m_sni = nullptr; | 94 | m_sni = nullptr; | ||
95 | delete m_adaptor; | ||||
96 | m_adaptor = nullptr; | ||||
91 | } | 97 | } | ||
92 | startStopSensor(); | 98 | startStopSensor(); | ||
93 | } | 99 | } | ||
94 | 100 | | |||
101 | void OrientationSensor::loadConfig() | ||||
102 | { | ||||
103 | if (!m_config) { | ||||
104 | return; | ||||
105 | } | ||||
106 | m_userEnabled = m_config->group("OrientationSensor").readEntry("Enabled", true); | ||||
anthonyfieroni: ```
m_userEnabled = m_config && m_config->group("OrientationSensor").readEntry("Enabled", true)… | |||||
I might want to extend the loadConfig with further options (who knows) and then it wouldn't help any more. Also I find the code easier to read if it's spread around more lines. graesslin: I might want to extend the loadConfig with further options (who knows) and then it wouldn't… | |||||
107 | } | ||||
108 | | ||||
95 | void OrientationSensor::setupStatusNotifier() | 109 | void OrientationSensor::setupStatusNotifier() | ||
96 | { | 110 | { | ||
97 | if (m_sni) { | 111 | if (m_sni) { | ||
98 | return; | 112 | return; | ||
99 | } | 113 | } | ||
100 | m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); | 114 | m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); | ||
101 | m_sni->setStandardActionsEnabled(false); | 115 | m_sni->setStandardActionsEnabled(false); | ||
102 | m_sni->setCategory(KStatusNotifierItem::Hardware); | 116 | m_sni->setCategory(KStatusNotifierItem::Hardware); | ||
103 | m_sni->setStatus(KStatusNotifierItem::Passive); | 117 | m_sni->setStatus(KStatusNotifierItem::Passive); | ||
104 | m_sni->setTitle(i18n("Automatic Screen Rotation")); | 118 | m_sni->setTitle(i18n("Automatic Screen Rotation")); | ||
105 | // TODO: proper icon with state | 119 | // TODO: proper icon with state | ||
106 | m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); | 120 | m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); | ||
107 | // we start disabled, it gets updated when the sensor becomes active | 121 | // we start disabled, it gets updated when the sensor becomes active | ||
108 | m_sni->setToolTipTitle(i18n("Automatic screen rotation is disabled")); | 122 | m_sni->setToolTipTitle(i18n("Automatic screen rotation is disabled")); | ||
109 | connect(m_sni, &KStatusNotifierItem::activateRequested, this, | 123 | connect(m_sni, &KStatusNotifierItem::activateRequested, this, | ||
110 | [this] { | 124 | [this] { | ||
111 | m_userEnabled = !m_userEnabled; | 125 | m_userEnabled = !m_userEnabled; | ||
112 | startStopSensor(); | 126 | startStopSensor(); | ||
127 | emit userEnabledChanged(m_userEnabled); | ||||
113 | } | 128 | } | ||
114 | ); | 129 | ); | ||
115 | } | 130 | } | ||
116 | 131 | | |||
117 | void OrientationSensor::startStopSensor() | 132 | void OrientationSensor::startStopSensor() | ||
118 | { | 133 | { | ||
119 | if (m_enabled && m_userEnabled) { | 134 | if (m_enabled && m_userEnabled) { | ||
120 | m_sensor->start(); | 135 | m_sensor->start(); | ||
121 | } else { | 136 | } else { | ||
122 | m_sensor->stop(); | 137 | m_sensor->stop(); | ||
123 | } | 138 | } | ||
124 | } | 139 | } | ||
125 | 140 | | |||
141 | void OrientationSensor::setUserEnabled(bool enabled) | ||||
142 | { | ||||
143 | if (m_userEnabled == enabled) { | ||||
144 | return; | ||||
145 | } | ||||
146 | m_userEnabled = enabled; | ||||
147 | if (m_config) { | ||||
148 | m_config->group("OrientationSensor").writeEntry("Enabled", m_userEnabled); | ||||
149 | } | ||||
150 | emit userEnabledChanged(m_userEnabled); | ||||
151 | } | ||||
152 | | ||||
126 | } | 153 | } |
?