Changeset View
Changeset View
Standalone View
Standalone View
addons/tabswitcher/tabswitcher.cpp
Show All 14 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "tabswitcher.h" | 21 | #include "tabswitcher.h" | ||
22 | #include "tabswitchertreeview.h" | 22 | #include "tabswitchertreeview.h" | ||
23 | #include "tabswitcherfilesmodel.h" | ||||
23 | 24 | | |||
24 | #include <KTextEditor/Application> | 25 | #include <KTextEditor/Application> | ||
25 | #include <KTextEditor/Document> | 26 | #include <KTextEditor/Document> | ||
26 | #include <KTextEditor/Editor> | 27 | #include <KTextEditor/Editor> | ||
27 | #include <KTextEditor/View> | 28 | #include <KTextEditor/View> | ||
28 | 29 | | |||
29 | #include <KActionCollection> | 30 | #include <KActionCollection> | ||
30 | #include <KLocalizedString> | 31 | #include <KLocalizedString> | ||
Show All 20 Lines | |||||
51 | TabSwitcherPluginView::TabSwitcherPluginView(TabSwitcherPlugin *plugin, KTextEditor::MainWindow *mainWindow) | 52 | TabSwitcherPluginView::TabSwitcherPluginView(TabSwitcherPlugin *plugin, KTextEditor::MainWindow *mainWindow) | ||
52 | : QObject(mainWindow) | 53 | : QObject(mainWindow) | ||
53 | , m_plugin(plugin) | 54 | , m_plugin(plugin) | ||
54 | , m_mainWindow(mainWindow) | 55 | , m_mainWindow(mainWindow) | ||
55 | { | 56 | { | ||
56 | // register this view | 57 | // register this view | ||
57 | m_plugin->m_views.append(this); | 58 | m_plugin->m_views.append(this); | ||
58 | 59 | | |||
59 | m_model = new QStandardItemModel(this); | 60 | m_model = new detail::TabswitcherFilesModel(this); | ||
60 | m_treeView = new TabSwitcherTreeView(); | 61 | m_treeView = new TabSwitcherTreeView(); | ||
61 | m_treeView->setModel(m_model); | 62 | m_treeView->setModel(m_model); | ||
62 | 63 | | |||
63 | KXMLGUIClient::setComponentName(QStringLiteral("tabswitcher"), i18n("Document Switcher")); | 64 | KXMLGUIClient::setComponentName(QStringLiteral("tabswitcher"), i18n("Document Switcher")); | ||
64 | setXMLFile(QStringLiteral("ui.rc")); | 65 | setXMLFile(QStringLiteral("ui.rc")); | ||
65 | 66 | | |||
66 | // note: call after m_treeView is created | 67 | // note: call after m_treeView is created | ||
67 | setupActions(); | 68 | setupActions(); | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
135 | } | 136 | } | ||
136 | 137 | | |||
137 | void TabSwitcherPluginView::registerDocument(KTextEditor::Document * document) | 138 | void TabSwitcherPluginView::registerDocument(KTextEditor::Document * document) | ||
138 | { | 139 | { | ||
139 | // insert into hash | 140 | // insert into hash | ||
140 | m_documents.insert(document); | 141 | m_documents.insert(document); | ||
141 | 142 | | |||
142 | // add to model | 143 | // add to model | ||
143 | auto item = new QStandardItem(iconForDocument(document), document->documentName()); | 144 | auto item = new detail::FilenameListItem( | ||
145 | iconForDocument(document), | ||||
146 | document->documentName(), | ||||
147 | document->url().toLocalFile()); | ||||
dhaumann: Do you see the memory leak? item is never deleted, since you create a copy in ::insertRow(). | |||||
144 | item->setData(QVariant::fromValue(document)); | 148 | item->setData(QVariant::fromValue(document)); | ||
145 | m_model->insertRow(0, item); | 149 | m_model->insertRow(0, item); | ||
146 | 150 | | |||
147 | // track document name changes | 151 | // track document name changes | ||
148 | connect(document, &KTextEditor::Document::documentNameChanged, this, &TabSwitcherPluginView::updateDocumentName); | 152 | connect(document, &KTextEditor::Document::documentNameChanged, this, &TabSwitcherPluginView::updateDocumentName); | ||
149 | } | 153 | } | ||
150 | 154 | | |||
151 | void TabSwitcherPluginView::unregisterDocument(KTextEditor::Document * document) | 155 | void TabSwitcherPluginView::unregisterDocument(KTextEditor::Document * document) | ||
Show All 24 Lines | 179 | { | |||
176 | if (!m_documents.contains(document)) { | 180 | if (!m_documents.contains(document)) { | ||
177 | return; | 181 | return; | ||
178 | } | 182 | } | ||
179 | 183 | | |||
180 | const auto rowCount = m_model->rowCount(); | 184 | const auto rowCount = m_model->rowCount(); | ||
181 | for (int i = 0; i < rowCount; ++i) { | 185 | for (int i = 0; i < rowCount; ++i) { | ||
182 | auto doc = m_model->item(i)->data().value<KTextEditor::Document*>(); | 186 | auto doc = m_model->item(i)->data().value<KTextEditor::Document*>(); | ||
183 | if (doc == document) { | 187 | if (doc == document) { | ||
184 | m_model->item(i)->setText(document->documentName()); | 188 | m_model->updateItem(m_model->item(i), document->documentName(), document->url().toLocalFile()); | ||
189 | //m_model->item(i)->setText(document->documentName()); | ||||
185 | break; | 190 | break; | ||
186 | } | 191 | } | ||
187 | } | 192 | } | ||
188 | } | 193 | } | ||
189 | 194 | | |||
190 | void TabSwitcherPluginView::raiseView(KTextEditor::View * view) | 195 | void TabSwitcherPluginView::raiseView(KTextEditor::View * view) | ||
191 | { | 196 | { | ||
192 | if (!view || !m_documents.contains(view->document())) { | 197 | if (!view || !m_documents.contains(view->document())) { | ||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | 238 | { | |||
239 | const QSize viewMaxSize( centralSize.width() * 3/4, centralSize.height() * 3/4 ); | 244 | const QSize viewMaxSize( centralSize.width() * 3/4, centralSize.height() * 3/4 ); | ||
240 | 245 | | |||
241 | // The actual view size should be as big as the columns/rows need it, but | 246 | // The actual view size should be as big as the columns/rows need it, but | ||
242 | // smaller than the max-size. This means the view will get quite high with | 247 | // smaller than the max-size. This means the view will get quite high with | ||
243 | // many open files but I think thats ok. Otherwise one can easily tweak the | 248 | // many open files but I think thats ok. Otherwise one can easily tweak the | ||
244 | // max size to be only 1/2th of the central widget size | 249 | // max size to be only 1/2th of the central widget size | ||
245 | const int rowHeight = m_treeView->sizeHintForRow(0); | 250 | const int rowHeight = m_treeView->sizeHintForRow(0); | ||
246 | const int frameWidth = m_treeView->frameWidth(); | 251 | const int frameWidth = m_treeView->frameWidth(); | ||
247 | const QSize viewSize(std::min(m_treeView->sizeHintForColumn(0) + 2 * frameWidth + m_treeView->verticalScrollBar()->width(), viewMaxSize.width()), | 252 | //const QSize viewSize(std::min(m_treeView->sizeHintForColumn(0) + 2 * frameWidth + m_treeView->verticalScrollBar()->width(), viewMaxSize.width()), // ORIG line, sizeHintForColumn was QListView but is protected for QTreeView so we introduced sizeHintWidth() | ||
253 | const QSize viewSize(std::min(m_treeView->sizeHintWidth() + 2 * frameWidth + m_treeView->verticalScrollBar()->width(), viewMaxSize.width()), | ||||
248 | std::min(std::max(rowHeight * m_model->rowCount() + 2 * frameWidth, rowHeight * 6 ), viewMaxSize.height())); | 254 | std::min(std::max(rowHeight * m_model->rowCount() + 2 * frameWidth, rowHeight * 6 ), viewMaxSize.height())); | ||
249 | 255 | | |||
250 | // Position should be central over the editor area, so map to global from | 256 | // Position should be central over the editor area, so map to global from | ||
251 | // parent of central widget since the view is positioned in global coords | 257 | // parent of central widget since the view is positioned in global coords | ||
252 | const QPoint centralWidgetPos = window->parentWidget() ? window->mapToGlobal(window->pos()) : window->pos(); | 258 | const QPoint centralWidgetPos = window->parentWidget() ? window->mapToGlobal(window->pos()) : window->pos(); | ||
253 | const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width() ) / 2); | 259 | const int xPos = std::max(0, centralWidgetPos.x() + (centralSize.width() - viewSize.width() ) / 2); | ||
254 | const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height() ) / 2); | 260 | const int yPos = std::max(0, centralWidgetPos.y() + (centralSize.height() - viewSize.height() ) / 2); | ||
255 | 261 | | |||
Show All 29 Lines |
Do you see the memory leak? item is never deleted, since you create a copy in ::insertRow().