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>
23
#include <kacceleratormanager.h>
26
#include <kstandarddirs.h>
27
#include <kglobalsettings.h>
29
#include <kpluginfactory.h>
30
#include <kpluginloader.h>
31
#include <kaboutdata.h>
33
#include <kiconloader.h>
36
#include <QtGui/QBoxLayout>
38
K_PLUGIN_FACTORY(TabBarFactory, registerPlugin<TabBarPlugin>();)
39
K_EXPORT_PLUGIN(TabBarFactory(KAboutData("tabifyplugin", "katetabifyplugin",
40
ki18n("TabifyPlugin"), "0.1", ki18n("Tabify Plugin"), KAboutData::License_LGPL_V2)))
42
///////////////////////////////////////////////////////////////////////////////
44
///////////////////////////////////////////////////////////////////////////////
45
TabBarPluginView::TabBarPluginView(Kate::MainWindow* mainwindow)
46
: Kate::PluginView(mainwindow)
48
m_tabBar = new KTabBar(mainWindow()->centralWidget());
49
KAcceleratorManager::setNoAccel(m_tabBar);
51
m_tabIsDeleting = false;
53
m_tabBar->setTabsClosable(true);
54
m_tabBar->setDocumentMode(true);
55
m_tabBar->setMovable(true);
57
QBoxLayout* layout = qobject_cast<QBoxLayout*>(mainWindow()->centralWidget()->layout());
58
layout->insertWidget(0, m_tabBar);
60
connect(Kate::application()->documentManager(), SIGNAL(documentCreated(KTextEditor::Document*)),
61
this, SLOT(slotDocumentCreated(KTextEditor::Document*)));
62
connect(Kate::application()->documentManager(), SIGNAL(documentDeleted(KTextEditor::Document*)),
63
this, SLOT(slotDocumentDeleted(KTextEditor::Document*)));
64
connect(mainWindow(), SIGNAL(viewChanged()),
65
this, SLOT(slotViewChanged()));
67
connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(slotTabChanged(int)));
68
connect(m_tabBar, SIGNAL(closeRequest(int)), this, SLOT(slotTabCloseRequest(int)));
69
connect(m_tabBar, SIGNAL(mouseMiddleClick(int)), this, SLOT(slotMiddleMouseButtonPressed(int)));
70
connect(m_tabBar, SIGNAL(wheelDelta(int)), this, SLOT(slotWheelDelta(int)));
71
connect(m_tabBar, SIGNAL(tabMoved(int,int)), this, SLOT(slotTabMoved(int,int)));
73
foreach(KTextEditor::Document* document, Kate::application()->documentManager()->documents()) {
74
slotDocumentCreated(document);
78
TabBarPluginView::~TabBarPluginView()
83
void TabBarPluginView::slotDocumentCreated(KTextEditor::Document* document)
88
connect(document, SIGNAL(modifiedChanged(KTextEditor::Document*)),
89
this, SLOT(slotDocumentChanged(KTextEditor::Document*)));
90
connect(document, SIGNAL(modifiedOnDisk(KTextEditor::Document*, bool,
91
KTextEditor::ModificationInterface::ModifiedOnDiskReason)),
92
this, SLOT(slotModifiedOnDisc(KTextEditor::Document*, bool,
93
KTextEditor::ModificationInterface::ModifiedOnDiskReason)));
94
connect(document, SIGNAL(documentNameChanged(KTextEditor::Document*)),
95
this, SLOT(slotNameChanged(KTextEditor::Document*)));
97
int index = m_tabBar->addTab(document->documentName());
98
m_tabBar->setTabToolTip(index, document->url().pathOrUrl());
99
m_tabDocMap[index] = document;
100
m_docTabMap[document] = index;
101
m_docList.append(document);
102
m_modifiedMap[document] = false;
105
void TabBarPluginView::slotTabChanged(int index)
107
if (m_tabIsDeleting) {
111
mainWindow()->activateView(m_tabDocMap[index]);
114
void TabBarPluginView::slotDocumentDeleted(KTextEditor::Document* document)
116
const int index = m_docTabMap[document];
117
m_docTabMap.remove(document);
118
m_tabDocMap.remove(index);
119
m_modifiedMap.remove(document);
120
m_docList.removeAll(document);
122
m_tabIsDeleting = true;
123
m_tabBar->removeTab(index);
124
m_tabIsDeleting = false;
126
// Rebuild the maps using the new state of the list.
130
void TabBarPluginView::slotViewChanged()
132
if (m_tabIsDeleting) {
136
KTextEditor::View* view = mainWindow()->activeView();
141
int tabID = m_docTabMap[view->document()];
142
m_tabBar->setCurrentIndex(tabID);
145
void TabBarPluginView::slotMiddleMouseButtonPressed(int tabId)
147
// only close by middle mouse button, if the document is not externally
148
// modified. Avoids a non-trivial crash: bug #299744
149
if (!m_modifiedMap[m_tabDocMap[tabId]]) {
150
slotTabCloseRequest(tabId);
154
void TabBarPluginView::slotTabCloseRequest(int tabId)
156
Kate::application()->documentManager()->closeDocument(m_tabDocMap[tabId]);
159
void TabBarPluginView::slotDocumentChanged(KTextEditor::Document* document)
162
int tabID = m_docTabMap[document];
163
if (document->isModified()) {
164
m_tabBar->setTabIcon(tabID, KIconLoader::global()
165
->loadIcon("document-save", KIconLoader::Small, 16));
167
m_tabBar->setTabIcon(tabID, QIcon());
171
void TabBarPluginView::slotModifiedOnDisc(KTextEditor::Document* document, bool modified,
172
KTextEditor::ModificationInterface::ModifiedOnDiskReason reason)
174
int tabID = m_docTabMap[document];
175
m_modifiedMap[document] = modified;
178
m_tabBar->setTabIcon(tabID, QIcon());
181
case KTextEditor::ModificationInterface::OnDiskModified:
182
m_tabBar->setTabIcon(tabID, KIconLoader::global()
183
->loadIcon("dialog-warning", KIconLoader::Small));
185
case KTextEditor::ModificationInterface::OnDiskCreated:
186
m_tabBar->setTabIcon(tabID, KIconLoader::global()
187
->loadIcon("document-save", KIconLoader::Small));
189
case KTextEditor::ModificationInterface::OnDiskDeleted:
190
m_tabBar->setTabIcon(tabID, KIconLoader::global()
191
->loadIcon("dialog-warning", KIconLoader::Small));
193
m_tabBar->setTabIcon(tabID, KIconLoader::global()
194
->loadIcon("dialog-warning", KIconLoader::Small));
199
void TabBarPluginView::slotNameChanged(KTextEditor::Document* document)
206
int tabID = m_docTabMap[document];
207
m_tabBar->setTabText(tabID, document->documentName());
208
m_tabBar->setTabToolTip(tabID, document->url().pathOrUrl());
211
void TabBarPluginView::slotWheelDelta(int delta)
213
if (m_tabBar->count() < 2) {
217
int page = m_tabBar->currentIndex();
219
page = (page + 1) % m_tabBar->count();
225
page = m_tabBar->count() - 1;
228
m_tabBar->setCurrentIndex(page);
231
void TabBarPluginView::slotTabMoved(int from, int to)
233
KTextEditor::Document* document = m_docList.takeAt(from);
234
m_docList.insert(to, document);
238
void TabBarPluginView::rebuildMaps() {
242
for (int i = 0; i < m_docList.count(); i++) {
243
KTextEditor::Document* document = m_docList.at(i);
244
//m_tabBar->setTabToolTip(i, document->url().pathOrUrl());
245
m_tabDocMap[i] = document;
246
m_docTabMap[document] = i;
250
///////////////////////////////////////////////////////////////////////////////
252
///////////////////////////////////////////////////////////////////////////////
253
TabBarPlugin::TabBarPlugin(QObject* parent , const QList<QVariant>&)
254
: Kate::Plugin((Kate::Application*)parent)
258
TabBarPlugin::~TabBarPlugin()
262
Kate::PluginView *TabBarPlugin::createView(Kate::MainWindow *mainWindow)
264
TabBarPluginView *view = new TabBarPluginView(mainWindow);