Changeset View
Changeset View
Standalone View
Standalone View
addons/close-except-like/close_except_plugin.cpp
Show First 20 Lines • Show All 155 Lines • ▼ Show 20 Line(s) | |||||
156 | { | 156 | { | ||
157 | updateMenu(); | 157 | updateMenu(); | ||
158 | } | 158 | } | ||
159 | 159 | | |||
160 | void CloseExceptPluginView::appendActionsFrom( | 160 | void CloseExceptPluginView::appendActionsFrom( | ||
161 | const std::set<QUrl>& paths | 161 | const std::set<QUrl>& paths | ||
162 | , actions_map_type& actions | 162 | , actions_map_type& actions | ||
163 | , KActionMenu* menu | 163 | , KActionMenu* menu | ||
164 | , QSignalMapper* mapper | 164 | , CloseFunction closeFunction | ||
165 | ) | 165 | ) | ||
166 | { | 166 | { | ||
167 | Q_FOREACH(const QUrl& path, paths) | 167 | Q_FOREACH(const QUrl& path, paths) | ||
168 | { | 168 | { | ||
169 | QString action = path.path() + QLatin1Char('*'); | 169 | QString action = path.path() + QLatin1Char('*'); | ||
170 | actions[action] = QPointer<QAction>(new QAction(action, menu)); | 170 | actions[action] = QPointer<QAction>(new QAction(action, menu)); | ||
171 | menu->addAction(actions[action]); | 171 | menu->addAction(actions[action]); | ||
172 | //connect(actions[action], &QAction::triggered, mapper, &QSignalMapper::map); | | |||
173 | connect(actions[action].data(), &QAction::triggered, | 172 | connect(actions[action].data(), &QAction::triggered, | ||
174 | mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map)); | 173 | this, [this, closeFunction, action]() { (this->*closeFunction)(action); }); | ||
175 | mapper->setMapping(actions[action], action); | | |||
176 | } | 174 | } | ||
177 | } | 175 | } | ||
178 | 176 | | |||
179 | void CloseExceptPluginView::appendActionsFrom( | 177 | void CloseExceptPluginView::appendActionsFrom( | ||
180 | const std::set<QString>& masks | 178 | const std::set<QString>& masks | ||
181 | , actions_map_type& actions | 179 | , actions_map_type& actions | ||
182 | , KActionMenu* menu | 180 | , KActionMenu* menu | ||
183 | , QSignalMapper* mapper | 181 | , CloseFunction closeFunction | ||
184 | ) | 182 | ) | ||
185 | { | 183 | { | ||
186 | Q_FOREACH(const QString& mask, masks) | 184 | Q_FOREACH(const QString& mask, masks) | ||
187 | { | 185 | { | ||
188 | QString action = mask.startsWith(QLatin1Char('*')) ? mask : mask + QLatin1Char('*'); | 186 | QString action = mask.startsWith(QLatin1Char('*')) ? mask : mask + QLatin1Char('*'); | ||
189 | actions[action] = QPointer<QAction>(new QAction(action, menu)); | 187 | actions[action] = QPointer<QAction>(new QAction(action, menu)); | ||
190 | menu->addAction(actions[action]); | 188 | menu->addAction(actions[action]); | ||
191 | connect(actions[action].data(), &QAction::triggered, | 189 | connect(actions[action].data(), &QAction::triggered, | ||
192 | mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map)); | 190 | this, [this, closeFunction, action]() { (this->*closeFunction)(action); }); | ||
193 | mapper->setMapping(actions[action], action); | | |||
194 | } | 191 | } | ||
195 | } | 192 | } | ||
196 | 193 | | |||
197 | QPointer<QSignalMapper> CloseExceptPluginView::updateMenu( | 194 | void CloseExceptPluginView::updateMenu( | ||
198 | const std::set<QUrl>& paths | 195 | const std::set<QUrl>& paths | ||
199 | , const std::set<QString>& masks | 196 | , const std::set<QString>& masks | ||
200 | , actions_map_type& actions | 197 | , actions_map_type& actions | ||
201 | , KActionMenu* menu | 198 | , KActionMenu* menu | ||
199 | , CloseFunction closeFunction | ||||
202 | ) | 200 | ) | ||
203 | { | 201 | { | ||
204 | // turn menu ON or OFF depending on collected results | 202 | // turn menu ON or OFF depending on collected results | ||
205 | menu->setEnabled(!paths.empty()); | 203 | menu->setEnabled(!paths.empty()); | ||
206 | 204 | | |||
207 | // Clear previous menus | 205 | // Clear previous menus | ||
208 | for (actions_map_type::iterator it = actions.begin(), last = actions.end(); it !=last;) | 206 | for (actions_map_type::iterator it = actions.begin(), last = actions.end(); it !=last;) | ||
209 | { | 207 | { | ||
210 | menu->removeAction(*it); | 208 | menu->removeAction(*it); | ||
211 | actions.erase(it++); | 209 | actions.erase(it++); | ||
212 | } | 210 | } | ||
213 | // Form a new one | 211 | // Form a new one | ||
214 | QPointer<QSignalMapper> mapper = QPointer<QSignalMapper>(new QSignalMapper(this)); | 212 | appendActionsFrom(paths, actions, menu, closeFunction); | ||
215 | appendActionsFrom(paths, actions, menu, mapper); | | |||
216 | if (!masks.empty()) | 213 | if (!masks.empty()) | ||
217 | { | 214 | { | ||
218 | if (!paths.empty()) | 215 | if (!paths.empty()) | ||
219 | menu->addSeparator(); // Add separator between paths and file's ext filters | 216 | menu->addSeparator(); // Add separator between paths and file's ext filters | ||
220 | appendActionsFrom(masks, actions, menu, mapper); | 217 | appendActionsFrom(masks, actions, menu, closeFunction); | ||
221 | } | 218 | } | ||
222 | // Append 'Show Confirmation' toggle menu item | 219 | // Append 'Show Confirmation' toggle menu item | ||
223 | menu->addSeparator(); // Add separator between paths and show confirmation | 220 | menu->addSeparator(); // Add separator between paths and show confirmation | ||
224 | menu->addAction(m_show_confirmation_action); | 221 | menu->addAction(m_show_confirmation_action); | ||
225 | return mapper; | | |||
226 | } | 222 | } | ||
227 | 223 | | |||
228 | void CloseExceptPluginView::updateMenu() | 224 | void CloseExceptPluginView::updateMenu() | ||
229 | { | 225 | { | ||
230 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | 226 | const QList<KTextEditor::Document*>& docs = KTextEditor::Editor::instance()->application()->documents(); | ||
231 | if (docs.size() < 2) | 227 | if (docs.size() < 2) | ||
232 | { | 228 | { | ||
233 | qDebug() << "No docs r (or the only) opened right now --> disable menu"; | 229 | qDebug() << "No docs r (or the only) opened right now --> disable menu"; | ||
Show All 36 Lines | 260 | { | |||
270 | { | 266 | { | ||
271 | paths.insert(url); | 267 | paths.insert(url); | ||
272 | break; | 268 | break; | ||
273 | } | 269 | } | ||
274 | } | 270 | } | ||
275 | } | 271 | } | ||
276 | qDebug() << "stage #2: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | 272 | qDebug() << "stage #2: Collected" << paths.size() << "paths and" << masks.size() << "masks"; | ||
277 | // | 273 | // | ||
278 | m_except_mapper = updateMenu(paths, masks, m_except_actions, m_except_menu); | 274 | updateMenu(paths, masks, m_except_actions, m_except_menu, &CloseExceptPluginView::closeExcept); | ||
279 | m_like_mapper = updateMenu(paths, masks, m_like_actions, m_like_menu); | 275 | updateMenu(paths, masks, m_like_actions, m_like_menu, &CloseExceptPluginView::closeLike); | ||
280 | connect(m_except_mapper.data(), static_cast<void (QSignalMapper::*)(const QString&)>(&QSignalMapper::mapped), | | |||
281 | this, &CloseExceptPluginView::closeExcept); | | |||
282 | connect(m_like_mapper.data(), static_cast<void (QSignalMapper::*)(const QString&)>(&QSignalMapper::mapped), | | |||
283 | this, &CloseExceptPluginView::closeLike); | | |||
284 | } | 276 | } | ||
285 | } | 277 | } | ||
286 | 278 | | |||
287 | void CloseExceptPluginView::close(const QString& item, const bool close_if_match) | 279 | void CloseExceptPluginView::close(const QString& item, const bool close_if_match) | ||
288 | { | 280 | { | ||
289 | QChar asterisk=QLatin1Char('*'); | 281 | QChar asterisk=QLatin1Char('*'); | ||
290 | assert( | 282 | assert( | ||
291 | "Parameter seems invalid! Is smth has changed in the code?" | 283 | "Parameter seems invalid! Is smth has changed in the code?" | ||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |