Changeset View
Changeset View
Standalone View
Standalone View
containments/desktop/plugins/folder/screenmapper.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 40 | { | |||
---|---|---|---|---|---|
53 | 53 | | |||
54 | // used to compress screenMappingChanged signals when addMapping is called multiple times, | 54 | // used to compress screenMappingChanged signals when addMapping is called multiple times, | ||
55 | // eg. from FolderModel::filterAcceptRows. The timer interval is an arbitrary number, | 55 | // eg. from FolderModel::filterAcceptRows. The timer interval is an arbitrary number, | ||
56 | // that doesn't delay too much the signal, but still compresses as much as possible | 56 | // that doesn't delay too much the signal, but still compresses as much as possible | ||
57 | m_screenMappingChangedTimer->setInterval(100); | 57 | m_screenMappingChangedTimer->setInterval(100); | ||
58 | m_screenMappingChangedTimer->setSingleShot(true); | 58 | m_screenMappingChangedTimer->setSingleShot(true); | ||
59 | } | 59 | } | ||
60 | 60 | | |||
61 | void ScreenMapper::removeScreen(int screenId, const QString &path) | 61 | void ScreenMapper::removeScreen(int screenId, const QUrl &screenUrl) | ||
62 | { | 62 | { | ||
63 | if (screenId < 0 || !m_availableScreens.contains(screenId)) | 63 | if (screenId < 0 || !m_availableScreens.contains(screenId)) | ||
64 | return; | 64 | return; | ||
65 | 65 | | |||
66 | QUrl screenUrl = QUrl::fromUserInput(path, {}, QUrl::AssumeLocalFile); | | |||
67 | const auto screenPathWithScheme = screenUrl.url(); | 66 | const auto screenPathWithScheme = screenUrl.url(); | ||
68 | // store the original location for the items | 67 | // store the original location for the items | ||
69 | auto it = m_screenItemMap.constBegin(); | 68 | auto it = m_screenItemMap.constBegin(); | ||
70 | while (it != m_screenItemMap.constEnd()) { | 69 | while (it != m_screenItemMap.constEnd()) { | ||
71 | const auto name = it.key(); | 70 | const auto name = it.key(); | ||
72 | if (it.value() == screenId && name.startsWith(screenPathWithScheme)) { | 71 | if (it.value() == screenId && name.url().startsWith(screenPathWithScheme)) { | ||
73 | m_itemsOnDisabledScreensMap[screenId].append(name); | 72 | m_itemsOnDisabledScreensMap[screenId].append(name); | ||
74 | } | 73 | } | ||
75 | ++it; | 74 | ++it; | ||
76 | } | 75 | } | ||
77 | 76 | | |||
78 | m_availableScreens.removeAll(screenId); | 77 | m_availableScreens.removeAll(screenId); | ||
79 | 78 | | |||
80 | const auto newFirstScreen = std::min_element(m_availableScreens.constBegin(), m_availableScreens.constEnd()); | 79 | const auto newFirstScreen = std::min_element(m_availableScreens.constBegin(), m_availableScreens.constEnd()); | ||
81 | auto pathIt = m_screensPerPath.find(path); | 80 | auto pathIt = m_screensPerPath.find(screenUrl); | ||
82 | if (pathIt != m_screensPerPath.end() && pathIt.value() > 0) { | 81 | if (pathIt != m_screensPerPath.end() && pathIt.value() > 0) { | ||
83 | int firstScreen = m_firstScreenForPath.value(path, -1); | 82 | int firstScreen = m_firstScreenForPath.value(screenUrl, -1); | ||
84 | if (firstScreen == screenId) { | 83 | if (firstScreen == screenId) { | ||
85 | m_firstScreenForPath[path] = (newFirstScreen == m_availableScreens.constEnd()) ? -1 : *newFirstScreen; | 84 | m_firstScreenForPath[screenUrl] = (newFirstScreen == m_availableScreens.constEnd()) ? -1 : *newFirstScreen; | ||
86 | } | 85 | } | ||
87 | *pathIt = pathIt.value() - 1; | 86 | *pathIt = pathIt.value() - 1; | ||
88 | } else if (path.isEmpty()) { | 87 | } else if (screenUrl.isEmpty()) { | ||
89 | // The screen got completely removed, not only its path changed. | 88 | // The screen got completely removed, not only its path changed. | ||
90 | // If the removed screen was the first screen for a desktop path, the first screen for that path | 89 | // If the removed screen was the first screen for a desktop path, the first screen for that path | ||
91 | // needs to be updated. | 90 | // needs to be updated. | ||
92 | for (auto it = m_firstScreenForPath.begin(); it != m_firstScreenForPath.end(); ++it) { | 91 | for (auto it = m_firstScreenForPath.begin(); it != m_firstScreenForPath.end(); ++it) { | ||
93 | if (*it == screenId) { | 92 | if (*it == screenId) { | ||
94 | *it = *newFirstScreen; | 93 | *it = *newFirstScreen; | ||
95 | 94 | | |||
96 | // we have now the path for the screen that was removed, so adjust it | 95 | // we have now the path for the screen that was removed, so adjust it | ||
97 | pathIt = m_screensPerPath.find(it.key()); | 96 | pathIt = m_screensPerPath.find(it.key()); | ||
98 | if (pathIt != m_screensPerPath.end()) { | 97 | if (pathIt != m_screensPerPath.end()) { | ||
99 | *pathIt = pathIt.value() - 1; | 98 | *pathIt = pathIt.value() - 1; | ||
100 | } | 99 | } | ||
101 | } | 100 | } | ||
102 | } | 101 | } | ||
103 | } | 102 | } | ||
104 | 103 | | |||
105 | emit screensChanged(); | 104 | emit screensChanged(); | ||
106 | } | 105 | } | ||
107 | 106 | | |||
108 | void ScreenMapper::addScreen(int screenId, const QString &path) | 107 | void ScreenMapper::addScreen(int screenId, const QUrl &screenUrl) | ||
109 | { | 108 | { | ||
110 | if (screenId < 0 || m_availableScreens.contains(screenId)) | 109 | if (screenId < 0 || m_availableScreens.contains(screenId)) | ||
111 | return; | 110 | return; | ||
112 | 111 | | |||
113 | QUrl screenUrl = QUrl::fromUserInput(path, {}, QUrl::AssumeLocalFile); | | |||
114 | const auto screenPathWithScheme = screenUrl.url(); | 112 | const auto screenPathWithScheme = screenUrl.url(); | ||
115 | const bool isEmpty = (path.isEmpty() || screenUrl.path() == "/"); | 113 | const bool isEmpty = (screenUrl.isEmpty() || screenUrl.path() == QLatin1String("/")); | ||
mlaurent: "== QLatin1Char('/') ?" | |||||
116 | // restore the stored locations | 114 | // restore the stored locations | ||
117 | auto it = m_itemsOnDisabledScreensMap.find(screenId); | 115 | auto it = m_itemsOnDisabledScreensMap.find(screenId); | ||
118 | if (it != m_itemsOnDisabledScreensMap.end()) { | 116 | if (it != m_itemsOnDisabledScreensMap.end()) { | ||
119 | auto items = it.value(); | 117 | auto items = it.value(); | ||
120 | for (const auto &name: it.value()) { | 118 | for (const auto &name: it.value()) { | ||
121 | // add the items to the new screen, if they are on a disabled screen and their | 119 | // add the items to the new screen, if they are on a disabled screen and their | ||
122 | // location is below the new screen's path | 120 | // location is below the new screen's path | ||
123 | if (isEmpty || name.startsWith(screenPathWithScheme)) { | 121 | if (isEmpty || name.url().startsWith(screenPathWithScheme)) { | ||
124 | addMapping(name, screenId, DelayedSignal); | 122 | addMapping(name, screenId, DelayedSignal); | ||
125 | items.removeAll(name); | 123 | items.removeAll(name); | ||
126 | } | 124 | } | ||
127 | } | 125 | } | ||
128 | if (items.isEmpty()) { | 126 | if (items.isEmpty()) { | ||
129 | m_itemsOnDisabledScreensMap.erase(it); | 127 | m_itemsOnDisabledScreensMap.erase(it); | ||
130 | } else { | 128 | } else { | ||
131 | *it = items; | 129 | *it = items; | ||
132 | } | 130 | } | ||
133 | } | 131 | } | ||
134 | 132 | | |||
135 | m_availableScreens.append(screenId); | 133 | m_availableScreens.append(screenId); | ||
136 | 134 | | |||
137 | // path is empty when a new screen appears that has no folderview base path associated with | 135 | // path is empty when a new screen appears that has no folderview base path associated with | ||
138 | if (!path.isEmpty()) { | 136 | if (!screenUrl.isEmpty()) { | ||
139 | auto it = m_screensPerPath.find(path); | 137 | auto it = m_screensPerPath.find(screenUrl); | ||
140 | int firstScreen = m_firstScreenForPath.value(path, -1); | 138 | int firstScreen = m_firstScreenForPath.value(screenUrl, -1); | ||
141 | if (firstScreen == -1 || screenId < firstScreen) { | 139 | if (firstScreen == -1 || screenId < firstScreen) { | ||
142 | m_firstScreenForPath[path] = screenId; | 140 | m_firstScreenForPath[screenUrl] = screenId; | ||
143 | } | 141 | } | ||
144 | if (it == m_screensPerPath.end()) { | 142 | if (it == m_screensPerPath.end()) { | ||
145 | m_screensPerPath[path] = 1; | 143 | m_screensPerPath[screenUrl] = 1; | ||
146 | } else { | 144 | } else { | ||
147 | *it = it.value() + 1; | 145 | *it = it.value() + 1; | ||
148 | } | 146 | } | ||
149 | } | 147 | } | ||
150 | 148 | | |||
151 | emit screensChanged(); | 149 | emit screensChanged(); | ||
152 | } | 150 | } | ||
153 | 151 | | |||
154 | void ScreenMapper::addMapping(const QString &name, int screen, MappingSignalBehavior behavior) | 152 | void ScreenMapper::addMapping(const QUrl &url, int screen, MappingSignalBehavior behavior) | ||
155 | { | 153 | { | ||
156 | m_screenItemMap[name] = screen; | 154 | m_screenItemMap[url] = screen; | ||
157 | if (behavior == DelayedSignal) { | 155 | if (behavior == DelayedSignal) { | ||
158 | m_screenMappingChangedTimer->start(); | 156 | m_screenMappingChangedTimer->start(); | ||
159 | } else { | 157 | } else { | ||
160 | emit screenMappingChanged(); | 158 | emit screenMappingChanged(); | ||
161 | } | 159 | } | ||
162 | } | 160 | } | ||
163 | 161 | | |||
164 | void ScreenMapper::removeFromMap(const QString &name) | 162 | void ScreenMapper::removeFromMap(const QUrl &url) | ||
165 | { | 163 | { | ||
166 | m_screenItemMap.remove(name); | 164 | m_screenItemMap.remove(url); | ||
167 | m_screenMappingChangedTimer->start(); | 165 | m_screenMappingChangedTimer->start(); | ||
168 | } | 166 | } | ||
169 | 167 | | |||
170 | int ScreenMapper::firstAvailableScreen(const QString &path) const | 168 | int ScreenMapper::firstAvailableScreen(const QUrl &screenUrl) const | ||
171 | { | 169 | { | ||
172 | return m_firstScreenForPath.value(path, -1); | 170 | return m_firstScreenForPath.value(screenUrl, -1); | ||
173 | } | 171 | } | ||
174 | 172 | | |||
175 | void ScreenMapper::removeItemFromDisabledScreen(const QString &name) | 173 | void ScreenMapper::removeItemFromDisabledScreen(const QUrl &url) | ||
176 | { | 174 | { | ||
177 | for (auto it = m_itemsOnDisabledScreensMap.begin(); | 175 | for (auto it = m_itemsOnDisabledScreensMap.begin(); | ||
178 | it != m_itemsOnDisabledScreensMap.end(); ++it) { | 176 | it != m_itemsOnDisabledScreensMap.end(); ++it) { | ||
179 | auto names = &(*it); | 177 | auto urls = &(*it); | ||
180 | names->removeAll(name); | 178 | urls->removeAll(url); | ||
181 | } | 179 | } | ||
182 | } | 180 | } | ||
183 | 181 | | |||
184 | #ifdef BUILD_TESTING | 182 | #ifdef BUILD_TESTING | ||
185 | void ScreenMapper::cleanup() | 183 | void ScreenMapper::cleanup() | ||
186 | { | 184 | { | ||
187 | m_screenItemMap.clear(); | 185 | m_screenItemMap.clear(); | ||
188 | m_itemsOnDisabledScreensMap.clear(); | 186 | m_itemsOnDisabledScreensMap.clear(); | ||
Show All 26 Lines | |||||
215 | } | 213 | } | ||
216 | 214 | | |||
217 | QStringList ScreenMapper::screenMapping() const | 215 | QStringList ScreenMapper::screenMapping() const | ||
218 | { | 216 | { | ||
219 | QStringList result; | 217 | QStringList result; | ||
220 | result.reserve(m_screenItemMap.count() * 2); | 218 | result.reserve(m_screenItemMap.count() * 2); | ||
221 | auto it = m_screenItemMap.constBegin(); | 219 | auto it = m_screenItemMap.constBegin(); | ||
222 | while (it != m_screenItemMap.constEnd()) { | 220 | while (it != m_screenItemMap.constEnd()) { | ||
223 | result.append(it.key()); | 221 | result.append(it.key().toString()); | ||
224 | result.append(QString::number(it.value())); | 222 | result.append(QString::number(it.value())); | ||
225 | ++it; | 223 | ++it; | ||
226 | } | 224 | } | ||
227 | 225 | | |||
228 | return result; | 226 | return result; | ||
229 | } | 227 | } | ||
230 | 228 | | |||
231 | void ScreenMapper::setScreenMapping(const QStringList &mapping) | 229 | void ScreenMapper::setScreenMapping(const QStringList &mapping) | ||
232 | { | 230 | { | ||
233 | QHash<QString, int> newMap; | 231 | QHash<QUrl, int> newMap; | ||
234 | const int count = mapping.count(); | 232 | const int count = mapping.count(); | ||
235 | newMap.reserve(count / 2); | 233 | newMap.reserve(count / 2); | ||
236 | for (int i = 0; i < count - 1; i += 2) { | 234 | for (int i = 0; i < count - 1; i += 2) { | ||
237 | if (i + 1 < count) { | 235 | if (i + 1 < count) { | ||
238 | newMap[mapping[i]] = mapping[i + 1].toInt(); | 236 | const QUrl url = QUrl::fromUserInput(mapping[i], {}, QUrl::AssumeLocalFile); | ||
237 | newMap[url] = mapping[i + 1].toInt(); | ||||
239 | } | 238 | } | ||
240 | } | 239 | } | ||
241 | 240 | | |||
242 | if (m_screenItemMap != newMap) { | 241 | if (m_screenItemMap != newMap) { | ||
243 | m_screenItemMap = newMap; | 242 | m_screenItemMap = newMap; | ||
244 | emit screenMappingChanged(); | 243 | emit screenMappingChanged(); | ||
245 | } | 244 | } | ||
246 | } | 245 | } | ||
247 | 246 | | |||
248 | int ScreenMapper::screenForItem(const QString &name) const | 247 | int ScreenMapper::screenForItem(const QUrl &url) const | ||
249 | { | 248 | { | ||
250 | int screen = m_screenItemMap.value(name, -1); | 249 | int screen = m_screenItemMap.value(url, -1); | ||
251 | if (!m_availableScreens.contains(screen)) | 250 | if (!m_availableScreens.contains(screen)) | ||
252 | screen = -1; | 251 | screen = -1; | ||
253 | 252 | | |||
254 | return screen; | 253 | return screen; | ||
255 | } | 254 | } | ||
255 | | ||||
256 | QUrl ScreenMapper::stringToUrl(const QString &path) | ||||
257 | { | ||||
258 | return QUrl::fromUserInput(path, {}, QUrl::AssumeLocalFile); | ||||
259 | } |
"== QLatin1Char('/') ?"