Changeset View
Changeset View
Standalone View
Standalone View
orientation_sensor.cpp
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | 60 | if (m_orientation != orientation) { | |||
---|---|---|---|---|---|
61 | m_orientation = orientation; | 61 | m_orientation = orientation; | ||
62 | emit orientationChanged(); | 62 | emit orientationChanged(); | ||
63 | } | 63 | } | ||
64 | } | 64 | } | ||
65 | ); | 65 | ); | ||
66 | connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh); | 66 | connect(m_sensor, &QOrientationSensor::activeChanged, this, &OrientationSensor::refresh); | ||
67 | } | 67 | } | ||
68 | 68 | | |||
69 | void OrientationSensor::activate() | ||||
70 | { | ||||
71 | m_userEnabled = !m_userEnabled; | ||||
72 | startStopSensor(); | ||||
73 | emit userEnabledChanged(m_userEnabled); | ||||
74 | } | ||||
75 | | ||||
69 | void OrientationSensor::refresh() | 76 | void OrientationSensor::refresh() | ||
70 | { | 77 | { | ||
71 | if (!m_sni) { | 78 | if (m_orientation == Orientation::Undefined) { | ||
79 | delete m_sni; | ||||
80 | m_sni = nullptr; | ||||
72 | return; | 81 | return; | ||
73 | } | 82 | } | ||
83 | | ||||
84 | if (!m_sni) { | ||||
85 | m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); | ||||
86 | m_sni->setStandardActionsEnabled(false); | ||||
87 | m_sni->setCategory(KStatusNotifierItem::Hardware); | ||||
88 | m_sni->setStatus(KStatusNotifierItem::Passive); | ||||
89 | // TODO: proper icon with state | ||||
90 | m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); | ||||
91 | // we start disabled, it gets updated when the sensor becomes active | ||||
92 | | ||||
93 | connect(m_sni, &KStatusNotifierItem::activateRequested, this, &OrientationSensor::activate); | ||||
94 | } | ||||
95 | | ||||
74 | if (m_sensor->isActive()) { | 96 | if (m_sensor->isActive()) { | ||
75 | m_sni->setTitle(i18n("Allow Rotation")); | 97 | m_sni->setTitle(i18n("Allow Rotation")); | ||
76 | m_sni->setToolTipTitle(i18n("Automatic screen rotation is enabled")); | 98 | m_sni->setToolTipTitle(i18n("Automatic screen rotation is enabled")); | ||
77 | } else { | 99 | } else { | ||
78 | QString text; | 100 | QString text; | ||
79 | switch(m_orientation) { | 101 | switch(m_orientation) { | ||
102 | case Orientation::Undefined: | ||||
103 | Q_UNREACHABLE(); //we don't want an sni if it's undefined | ||||
104 | break; | ||||
80 | case Orientation::FaceUp: | 105 | case Orientation::FaceUp: | ||
81 | case Orientation::FaceDown: | 106 | case Orientation::FaceDown: | ||
82 | case Orientation::Undefined: | | |||
83 | text = i18n("Undefined"); | 107 | text = i18n("Undefined"); | ||
84 | break; | 108 | break; | ||
85 | case Orientation::TopUp: | 109 | case Orientation::TopUp: | ||
86 | case Orientation::TopDown: | 110 | case Orientation::TopDown: | ||
87 | text = i18nc("Portrait oriented display", "Vertical"); | 111 | text = i18nc("Portrait oriented display", "Vertical"); | ||
88 | break; | 112 | break; | ||
89 | case Orientation::LeftUp: | 113 | case Orientation::LeftUp: | ||
90 | case Orientation::RightUp: | 114 | case Orientation::RightUp: | ||
Show All 10 Lines | |||||
101 | void OrientationSensor::setEnabled(bool enabled) | 125 | void OrientationSensor::setEnabled(bool enabled) | ||
102 | { | 126 | { | ||
103 | if (m_enabled == enabled) { | 127 | if (m_enabled == enabled) { | ||
104 | return; | 128 | return; | ||
105 | } | 129 | } | ||
106 | m_enabled = enabled; | 130 | m_enabled = enabled; | ||
107 | if (m_enabled) { | 131 | if (m_enabled) { | ||
108 | loadConfig(); | 132 | loadConfig(); | ||
109 | setupStatusNotifier(); | 133 | refresh(); | ||
110 | m_adaptor = new OrientationSensorAdaptor(this); | 134 | m_adaptor = new OrientationSensorAdaptor(this); | ||
111 | } else { | 135 | } else { | ||
112 | delete m_sni; | 136 | delete m_sni; | ||
113 | m_sni = nullptr; | 137 | m_sni = nullptr; | ||
114 | delete m_adaptor; | 138 | delete m_adaptor; | ||
115 | m_adaptor = nullptr; | 139 | m_adaptor = nullptr; | ||
116 | } | 140 | } | ||
117 | startStopSensor(); | 141 | startStopSensor(); | ||
118 | } | 142 | } | ||
119 | 143 | | |||
120 | void OrientationSensor::loadConfig() | 144 | void OrientationSensor::loadConfig() | ||
121 | { | 145 | { | ||
122 | if (!m_config) { | 146 | if (!m_config) { | ||
123 | return; | 147 | return; | ||
124 | } | 148 | } | ||
125 | m_userEnabled = m_config->group("OrientationSensor").readEntry("Enabled", true); | 149 | m_userEnabled = m_config->group("OrientationSensor").readEntry("Enabled", true); | ||
126 | } | 150 | } | ||
127 | 151 | | |||
128 | void OrientationSensor::setupStatusNotifier() | | |||
129 | { | | |||
130 | if (m_sni) { | | |||
131 | return; | | |||
132 | } | | |||
133 | m_sni = new KStatusNotifierItem(QStringLiteral("kwin-automatic-rotation"), this); | | |||
134 | m_sni->setStandardActionsEnabled(false); | | |||
135 | m_sni->setCategory(KStatusNotifierItem::Hardware); | | |||
136 | m_sni->setStatus(KStatusNotifierItem::Passive); | | |||
137 | // TODO: proper icon with state | | |||
138 | m_sni->setIconByName(QStringLiteral("preferences-desktop-display")); | | |||
139 | // we start disabled, it gets updated when the sensor becomes active | | |||
140 | | ||||
141 | refresh(); | | |||
142 | connect(m_sni, &KStatusNotifierItem::activateRequested, this, | | |||
143 | [this] { | | |||
144 | m_userEnabled = !m_userEnabled; | | |||
145 | startStopSensor(); | | |||
146 | emit userEnabledChanged(m_userEnabled); | | |||
147 | } | | |||
148 | ); | | |||
149 | } | | |||
150 | | ||||
151 | void OrientationSensor::startStopSensor() | 152 | void OrientationSensor::startStopSensor() | ||
152 | { | 153 | { | ||
153 | if (m_enabled && m_userEnabled) { | 154 | if (m_enabled && m_userEnabled) { | ||
154 | m_sensor->start(); | 155 | m_sensor->start(); | ||
155 | } else { | 156 | } else { | ||
156 | m_sensor->stop(); | 157 | m_sensor->stop(); | ||
157 | } | 158 | } | ||
158 | } | 159 | } | ||
Show All 14 Lines |