1
/***************************************************************************
2
* Copyright (C) 2010 by Abhishek Patil <abhishekworld@gmail.com> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
20
#include <kate/documentmanager.h>
21
#include <kate/application.h>
25
#include <kstandarddirs.h>
26
#include <kglobalsettings.h>
28
#include <kpluginfactory.h>
29
#include <kpluginloader.h>
30
#include <kaboutdata.h>
32
#include <kiconloader.h>
35
#include <QtGui/QBoxLayout>
37
K_PLUGIN_FACTORY(TabBarFactory, registerPlugin<TabBarPlugin>();)
38
K_EXPORT_PLUGIN(TabBarFactory(KAboutData("tabifyplugin", "katetabifyplugin",
39
ki18n("TabifyPlugin"), "0.1", ki18n("Tabify Plugin"), KAboutData::License_LGPL_V2)))
41
///////////////////////////////////////////////////////////////////////////////
43
///////////////////////////////////////////////////////////////////////////////
44
TabBarPluginView::TabBarPluginView(Kate::MainWindow* mainwindow)
45
: Kate::PluginView(mainwindow)
47
m_tabBar = new KTabBar(mainWindow()->centralWidget());
48
m_tabIsDeleting = false;
50
m_tabBar->setTabsClosable(true);
51
m_tabBar->setDocumentMode(true);
52
m_tabBar->setMovable(true);
54
QBoxLayout* layout = qobject_cast<QBoxLayout*>(mainWindow()->centralWidget()->layout());
55
layout->insertWidget(0, m_tabBar);
57
connect(Kate::application()->documentManager(), SIGNAL(documentCreated(KTextEditor::Document*)),
58
this, SLOT(slotDocumentCreated(KTextEditor::Document*)));
59
connect(Kate::application()->documentManager(), SIGNAL(documentDeleted(KTextEditor::Document*)),
60
this, SLOT(slotDocumentDeleted(KTextEditor::Document*)));
61
connect(mainWindow(), SIGNAL(viewChanged()),
62
this, SLOT(slotViewChanged()));
64
connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(slotTabChanged(int)));
65
connect(m_tabBar, SIGNAL(closeRequest(int)), this, SLOT(slotTabCloseRequest(int)));
66
connect(m_tabBar, SIGNAL(mouseMiddleClick(int)), this, SLOT(slotMiddleMouseButtonPressed(int)));
67
connect(m_tabBar, SIGNAL(wheelDelta(int)), this, SLOT(slotWheelDelta(int)));
68
connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(slotTabMoved(int,int)));
70
foreach(KTextEditor::Document* document, Kate::application()->documentManager()->documents()) {
71
slotDocumentCreated(document);
75
TabBarPluginView::~TabBarPluginView()
80
void TabBarPluginView::slotDocumentCreated(KTextEditor::Document* document)
85
connect(document, SIGNAL(modifiedChanged(KTextEditor::Document*)),
86
this, SLOT(slotDocumentChanged(KTextEditor::Document*)));
87
connect(document, SIGNAL(modifiedOnDisk(KTextEditor::Document*, bool,
88
KTextEditor::ModificationInterface::ModifiedOnDiskReason)),
89
this, SLOT(slotModifiedOnDisc(KTextEditor::Document*, bool,
90
KTextEditor::ModificationInterface::ModifiedOnDiskReason)));
91
connect(document, SIGNAL(documentNameChanged(KTextEditor::Document*)),
92
this, SLOT(slotNameChanged(KTextEditor::Document*)));
94
int index = m_tabBar->addTab(document->documentName());
95
m_tabBar->setTabToolTip(index, document->url().pathOrUrl());
96
m_tabDocMap[index] = document;
97
m_docTabMap[document] = index;
98
m_docList.append(document);
99
m_modifiedMap[document] = false;
102
void TabBarPluginView::slotTabChanged(int index)
104
if (m_tabIsDeleting) {
108
mainWindow()->activateView(m_tabDocMap[index]);
111
void TabBarPluginView::slotDocumentDeleted(KTextEditor::Document* document)
113
const int index = m_docTabMap[document];
114
m_docTabMap.remove(document);
115
m_tabDocMap.remove(index);
116
m_modifiedMap.remove(document);
117
m_docList.removeAll(document);
119
m_tabIsDeleting = true;
120
m_tabBar->removeTab(index);
121
m_tabIsDeleting = false;
123
// Rebuild the maps using the new state of the list.
127
void TabBarPluginView::slotViewChanged()
129
if (m_tabIsDeleting) {
133
KTextEditor::View* view = mainWindow()->activeView();
138
int tabID = m_docTabMap[view->document()];
139
m_tabBar->setCurrentIndex(tabID);
142
void TabBarPluginView::slotMiddleMouseButtonPressed(int tabId)
144
// only close by middle mouse button, if the document is not externally
145
// modified. Avoids a non-trivial crash: bug #299744
146
if (!m_modifiedMap[m_tabDocMap[tabId]]) {
147
slotTabCloseRequest(tabId);
151
void TabBarPluginView::slotTabCloseRequest(int tabId)
153
Kate::application()->documentManager()->closeDocument(m_tabDocMap[tabId]);
156
void TabBarPluginView::slotDocumentChanged(KTextEditor::Document* document)
159
int tabID = m_docTabMap[document];
160
if (document->isModified()) {
161
m_tabBar->setTabIcon(tabID, KIconLoader::global()
162
->loadIcon("document-save", KIconLoader::Small, 16));
164
m_tabBar->setTabIcon(tabID, QIcon());
168
void TabBarPluginView::slotModifiedOnDisc(KTextEditor::Document* document, bool modified,
169
KTextEditor::ModificationInterface::ModifiedOnDiskReason reason)
171
int tabID = m_docTabMap[document];
172
m_modifiedMap[document] = modified;
175
m_tabBar->setTabIcon(tabID, QIcon());
178
case KTextEditor::ModificationInterface::OnDiskModified:
179
m_tabBar->setTabIcon(tabID, KIconLoader::global()
180
->loadIcon("dialog-warning", KIconLoader::Small));
182
case KTextEditor::ModificationInterface::OnDiskCreated:
183
m_tabBar->setTabIcon(tabID, KIconLoader::global()
184
->loadIcon("document-save", KIconLoader::Small));
186
case KTextEditor::ModificationInterface::OnDiskDeleted:
187
m_tabBar->setTabIcon(tabID, KIconLoader::global()
188
->loadIcon("dialog-warning", KIconLoader::Small));
190
m_tabBar->setTabIcon(tabID, KIconLoader::global()
191
->loadIcon("dialog-warning", KIconLoader::Small));
196
void TabBarPluginView::slotNameChanged(KTextEditor::Document* document)
203
int tabID = m_docTabMap[document];
204
m_tabBar->setTabText(tabID, document->documentName());
205
m_tabBar->setTabToolTip(tabID, document->url().pathOrUrl());
208
void TabBarPluginView::slotWheelDelta(int delta)
210
if (m_tabBar->count() < 2) {
214
int page = m_tabBar->currentIndex();
216
page = (page + 1) % m_tabBar->count();
222
page = m_tabBar->count() - 1;
225
m_tabBar->setCurrentIndex(page);
228
void TabBarPluginView::slotTabMoved(int from, int to)
230
KTextEditor::Document* document = m_docList.takeAt(from);
231
m_docList.insert(to, document);
235
void TabBarPluginView::rebuildMaps() {
239
for (int i = 0; i < m_docList.count(); i++) {
240
KTextEditor::Document* document = m_docList.at(i);
241
//m_tabBar->setTabToolTip(i, document->url().pathOrUrl());
242
m_tabDocMap[i] = document;
243
m_docTabMap[document] = i;
247
///////////////////////////////////////////////////////////////////////////////
249
///////////////////////////////////////////////////////////////////////////////
250
TabBarPlugin::TabBarPlugin(QObject* parent , const QList<QVariant>&)
251
: Kate::Plugin((Kate::Application*)parent)
255
TabBarPlugin::~TabBarPlugin()
259
Kate::PluginView *TabBarPlugin::createView(Kate::MainWindow *mainWindow)
261
TabBarPluginView *view = new TabBarPluginView(mainWindow);