Changeset View
Changeset View
Standalone View
Standalone View
addons/tabswitcher/tabswitcher.cpp
Show All 27 Lines | |||||
28 | #include <KTextEditor/View> | 28 | #include <KTextEditor/View> | ||
29 | 29 | | |||
30 | #include <KActionCollection> | 30 | #include <KActionCollection> | ||
31 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
32 | #include <KPluginFactory> | 32 | #include <KPluginFactory> | ||
33 | #include <KXMLGUIFactory> | 33 | #include <KXMLGUIFactory> | ||
34 | 34 | | |||
35 | #include <QAction> | 35 | #include <QAction> | ||
36 | #include <QMimeDatabase> | | |||
37 | #include <QScrollBar> | 36 | #include <QScrollBar> | ||
38 | #include <QStandardItemModel> | 37 | #include <QStandardItemModel> | ||
39 | 38 | | |||
40 | K_PLUGIN_FACTORY_WITH_JSON(TabSwitcherPluginFactory, "tabswitcherplugin.json", registerPlugin<TabSwitcherPlugin>();) | 39 | K_PLUGIN_FACTORY_WITH_JSON(TabSwitcherPluginFactory, "tabswitcherplugin.json", registerPlugin<TabSwitcherPlugin>();) | ||
41 | 40 | | |||
42 | TabSwitcherPlugin::TabSwitcherPlugin(QObject *parent, const QList<QVariant> &): | 41 | TabSwitcherPlugin::TabSwitcherPlugin(QObject *parent, const QList<QVariant> &): | ||
43 | KTextEditor::Plugin(parent) | 42 | KTextEditor::Plugin(parent) | ||
44 | { | 43 | { | ||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Line(s) | 102 | { | |||
117 | aPrev->setStatusTip(i18n("Walk through the list of last used views")); | 116 | aPrev->setStatusTip(i18n("Walk through the list of last used views")); | ||
118 | connect(aPrev, &QAction::triggered, this, &TabSwitcherPluginView::walkBackward); | 117 | connect(aPrev, &QAction::triggered, this, &TabSwitcherPluginView::walkBackward); | ||
119 | 118 | | |||
120 | // make sure action work when the popup has focus | 119 | // make sure action work when the popup has focus | ||
121 | m_treeView->addAction(aNext); | 120 | m_treeView->addAction(aNext); | ||
122 | m_treeView->addAction(aPrev); | 121 | m_treeView->addAction(aPrev); | ||
123 | } | 122 | } | ||
124 | 123 | | |||
125 | static QIcon iconForDocument(KTextEditor::Document * doc) | | |||
126 | { | | |||
127 | return QIcon::fromTheme(QMimeDatabase().mimeTypeForUrl(doc->url()).iconName()); | | |||
128 | } | | |||
129 | | ||||
130 | void TabSwitcherPluginView::setupModel() | 124 | void TabSwitcherPluginView::setupModel() | ||
131 | { | 125 | { | ||
132 | // initial fill of model | 126 | // initial fill of model | ||
133 | foreach (auto doc, KTextEditor::Editor::instance()->application()->documents()) { | 127 | foreach (auto doc, KTextEditor::Editor::instance()->application()->documents()) { | ||
134 | registerDocument(doc); | 128 | registerDocument(doc); | ||
135 | } | 129 | } | ||
136 | } | 130 | } | ||
137 | 131 | | |||
138 | void TabSwitcherPluginView::registerDocument(KTextEditor::Document * document) | 132 | void TabSwitcherPluginView::registerDocument(KTextEditor::Document * document) | ||
139 | { | 133 | { | ||
140 | // insert into hash | 134 | // insert into hash | ||
141 | m_documents.insert(document); | 135 | m_documents.insert(document); | ||
142 | 136 | | |||
143 | // add to model | 137 | // add to model | ||
144 | auto item = new detail::FilenameListItem( | 138 | m_model->insertRow(0, detail::FilenameListItem(document)); | ||
145 | iconForDocument(document), | | |||
146 | document->documentName(), | | |||
147 | document->url().toLocalFile()); | | |||
148 | item->setData(QVariant::fromValue(document)); | | |||
149 | m_model->insertRow(0, item); | | |||
150 | 139 | | |||
151 | // track document name changes | 140 | // track document name changes | ||
152 | connect(document, &KTextEditor::Document::documentNameChanged, this, &TabSwitcherPluginView::updateDocumentName); | 141 | connect(document, &KTextEditor::Document::documentNameChanged, this, &TabSwitcherPluginView::updateDocumentName); | ||
153 | } | 142 | } | ||
154 | 143 | | |||
155 | void TabSwitcherPluginView::unregisterDocument(KTextEditor::Document * document) | 144 | void TabSwitcherPluginView::unregisterDocument(KTextEditor::Document * document) | ||
156 | { | 145 | { | ||
157 | // remove from hash | 146 | // remove from hash | ||
158 | if (!m_documents.contains(document)) { | 147 | if (!m_documents.contains(document)) { | ||
159 | return; | 148 | return; | ||
160 | } | 149 | } | ||
161 | m_documents.remove(document); | 150 | m_documents.remove(document); | ||
162 | 151 | | |||
163 | // remove from model | 152 | // remove from model | ||
164 | const auto rowCount = m_model->rowCount(); | 153 | const auto rowCount = m_model->rowCount(); | ||
165 | for (int i = 0; i < rowCount; ++i) { | 154 | for (int i = 0; i < rowCount; ++i) { | ||
166 | auto doc = m_model->item(i)->data().value<KTextEditor::Document*>(); | 155 | auto doc = m_model->item(i)->document; | ||
167 | if (doc == document) { | 156 | if (doc == document) { | ||
168 | m_model->removeRow(i); | 157 | m_model->removeRow(i); | ||
169 | 158 | | |||
170 | // disconnect documentNameChanged() signal | 159 | // disconnect documentNameChanged() signal | ||
171 | disconnect(document, nullptr, this, nullptr); | 160 | disconnect(document, nullptr, this, nullptr); | ||
172 | 161 | | |||
173 | break; | 162 | break; | ||
174 | } | 163 | } | ||
175 | } | 164 | } | ||
176 | } | 165 | } | ||
177 | 166 | | |||
178 | void TabSwitcherPluginView::updateDocumentName(KTextEditor::Document * document) | 167 | void TabSwitcherPluginView::updateDocumentName(KTextEditor::Document * document) | ||
179 | { | 168 | { | ||
180 | if (!m_documents.contains(document)) { | 169 | if (!m_documents.contains(document)) { | ||
181 | return; | 170 | return; | ||
182 | } | 171 | } | ||
183 | 172 | | |||
184 | const auto rowCount = m_model->rowCount(); | 173 | const auto rowCount = m_model->rowCount(); | ||
185 | for (int i = 0; i < rowCount; ++i) { | 174 | for (int i = 0; i < rowCount; ++i) { | ||
186 | auto doc = m_model->item(i)->data().value<KTextEditor::Document*>(); | 175 | auto doc = m_model->item(i)->document; | ||
187 | if (doc == document) { | 176 | if (doc == document) { | ||
188 | m_model->updateItem(m_model->item(i), document->documentName(), document->url().toLocalFile()); | 177 | m_model->updateItem(m_model->item(i), document->documentName(), document->url().toLocalFile()); | ||
189 | //m_model->item(i)->setText(document->documentName()); | 178 | //m_model->item(i)->setText(document->documentName()); | ||
190 | break; | 179 | break; | ||
191 | } | 180 | } | ||
192 | } | 181 | } | ||
193 | } | 182 | } | ||
194 | 183 | | |||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Line(s) | 262 | { | |||
275 | 264 | | |||
276 | // guard against empty selection | 265 | // guard against empty selection | ||
277 | if (m_treeView->selectionModel()->selectedRows().isEmpty()) { | 266 | if (m_treeView->selectionModel()->selectedRows().isEmpty()) { | ||
278 | return; | 267 | return; | ||
279 | } | 268 | } | ||
280 | 269 | | |||
281 | const int row = m_treeView->selectionModel()->selectedRows().first().row(); | 270 | const int row = m_treeView->selectionModel()->selectedRows().first().row(); | ||
282 | 271 | | |||
283 | auto doc = m_model->item(row)->data().value<KTextEditor::Document*>(); | 272 | auto doc = m_model->item(row)->document; | ||
284 | m_mainWindow->activateView(doc); | 273 | m_mainWindow->activateView(doc); | ||
285 | 274 | | |||
286 | m_treeView->hide(); | 275 | m_treeView->hide(); | ||
287 | } | 276 | } | ||
288 | 277 | | |||
289 | // required for TabSwitcherPluginFactory vtable | 278 | // required for TabSwitcherPluginFactory vtable | ||
290 | #include "tabswitcher.moc" | 279 | #include "tabswitcher.moc" |