~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to addons/kate/tabify/tabify.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2010 by Abhishek Patil <abhishekworld@gmail.com>        *
3
 
*                                                                         *
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.                                   *
8
 
*                                                                         *
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.                          *
13
 
*                                                                         *
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
 
***************************************************************************/
19
 
#include "tabify.h"
20
 
#include <kate/documentmanager.h>
21
 
#include <kate/application.h>
22
 
 
23
 
#include <kacceleratormanager.h>
24
 
#include <kaction.h>
25
 
#include <klocale.h>
26
 
#include <kstandarddirs.h>
27
 
#include <kglobalsettings.h>
28
 
#include <kglobal.h>
29
 
#include <kpluginfactory.h>
30
 
#include <kpluginloader.h>
31
 
#include <kaboutdata.h>
32
 
 
33
 
#include <kiconloader.h>
34
 
#include <ktabbar.h>
35
 
#include <kdebug.h>
36
 
#include <QtGui/QBoxLayout>
37
 
 
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)))
41
 
 
42
 
///////////////////////////////////////////////////////////////////////////////
43
 
// TabBarPluginView
44
 
///////////////////////////////////////////////////////////////////////////////
45
 
TabBarPluginView::TabBarPluginView(Kate::MainWindow* mainwindow)
46
 
    : Kate::PluginView(mainwindow)
47
 
{
48
 
  m_tabBar = new KTabBar(mainWindow()->centralWidget());
49
 
  KAcceleratorManager::setNoAccel(m_tabBar);
50
 
 
51
 
  m_tabIsDeleting = false;
52
 
 
53
 
  m_tabBar->setTabsClosable(true);
54
 
  m_tabBar->setDocumentMode(true);
55
 
  m_tabBar->setMovable(true);
56
 
 
57
 
  QBoxLayout* layout = qobject_cast<QBoxLayout*>(mainWindow()->centralWidget()->layout());
58
 
  layout->insertWidget(0, m_tabBar);
59
 
 
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()));
66
 
 
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)));
72
 
 
73
 
  foreach(KTextEditor::Document* document, Kate::application()->documentManager()->documents()) {
74
 
    slotDocumentCreated(document);
75
 
  }
76
 
}
77
 
 
78
 
TabBarPluginView::~TabBarPluginView()
79
 
{
80
 
  delete m_tabBar;
81
 
}
82
 
 
83
 
void TabBarPluginView::slotDocumentCreated(KTextEditor::Document* document)
84
 
{
85
 
  if (!document)
86
 
    return;
87
 
 
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*)));
96
 
 
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;
103
 
}
104
 
 
105
 
void TabBarPluginView::slotTabChanged(int index)
106
 
{
107
 
  if (m_tabIsDeleting) {
108
 
    return;
109
 
  }
110
 
 
111
 
  mainWindow()->activateView(m_tabDocMap[index]);
112
 
}
113
 
 
114
 
void TabBarPluginView::slotDocumentDeleted(KTextEditor::Document* document)
115
 
{
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);
121
 
 
122
 
  m_tabIsDeleting = true;
123
 
  m_tabBar->removeTab(index);
124
 
  m_tabIsDeleting = false;
125
 
 
126
 
  // Rebuild the maps using the new state of the list.
127
 
  rebuildMaps();
128
 
}
129
 
 
130
 
void TabBarPluginView::slotViewChanged()
131
 
{
132
 
  if (m_tabIsDeleting) {
133
 
    return;
134
 
  }
135
 
 
136
 
  KTextEditor::View* view = mainWindow()->activeView();
137
 
  if (!view) {
138
 
    return;
139
 
  }
140
 
 
141
 
  int tabID = m_docTabMap[view->document()];
142
 
  m_tabBar->setCurrentIndex(tabID);
143
 
}
144
 
 
145
 
void TabBarPluginView::slotMiddleMouseButtonPressed(int tabId)
146
 
{
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);
151
 
  }
152
 
}
153
 
 
154
 
void TabBarPluginView::slotTabCloseRequest(int tabId)
155
 
{
156
 
  Kate::application()->documentManager()->closeDocument(m_tabDocMap[tabId]);
157
 
}
158
 
 
159
 
void TabBarPluginView::slotDocumentChanged(KTextEditor::Document* document)
160
 
{
161
 
 
162
 
  int tabID = m_docTabMap[document];
163
 
  if (document->isModified()) {
164
 
    m_tabBar->setTabIcon(tabID, KIconLoader::global()
165
 
                         ->loadIcon("document-save", KIconLoader::Small, 16));
166
 
  } else {
167
 
    m_tabBar->setTabIcon(tabID, QIcon());
168
 
  }
169
 
}
170
 
 
171
 
void TabBarPluginView::slotModifiedOnDisc(KTextEditor::Document* document, bool modified,
172
 
    KTextEditor::ModificationInterface::ModifiedOnDiskReason reason)
173
 
{
174
 
  int tabID = m_docTabMap[document];
175
 
  m_modifiedMap[document] = modified;
176
 
 
177
 
  if (!modified) {
178
 
    m_tabBar->setTabIcon(tabID, QIcon());
179
 
  } else {
180
 
    switch (reason) {
181
 
    case KTextEditor::ModificationInterface::OnDiskModified:
182
 
      m_tabBar->setTabIcon(tabID, KIconLoader::global()
183
 
                           ->loadIcon("dialog-warning", KIconLoader::Small));
184
 
      break;
185
 
    case KTextEditor::ModificationInterface::OnDiskCreated:
186
 
      m_tabBar->setTabIcon(tabID, KIconLoader::global()
187
 
                           ->loadIcon("document-save", KIconLoader::Small));
188
 
      break;
189
 
    case KTextEditor::ModificationInterface::OnDiskDeleted:
190
 
      m_tabBar->setTabIcon(tabID, KIconLoader::global()
191
 
                           ->loadIcon("dialog-warning", KIconLoader::Small));
192
 
    default:
193
 
      m_tabBar->setTabIcon(tabID, KIconLoader::global()
194
 
                           ->loadIcon("dialog-warning", KIconLoader::Small));
195
 
    }
196
 
  }
197
 
}
198
 
 
199
 
void TabBarPluginView::slotNameChanged(KTextEditor::Document* document)
200
 
{
201
 
 
202
 
  if (!document) {
203
 
    return;
204
 
  }
205
 
 
206
 
  int tabID = m_docTabMap[document];
207
 
  m_tabBar->setTabText(tabID, document->documentName());
208
 
  m_tabBar->setTabToolTip(tabID, document->url().pathOrUrl());
209
 
}
210
 
 
211
 
void TabBarPluginView::slotWheelDelta(int delta)
212
 
{
213
 
  if (m_tabBar->count() < 2) {
214
 
    return;
215
 
  }
216
 
 
217
 
  int page = m_tabBar->currentIndex();
218
 
  if (delta < 0) {
219
 
    page = (page + 1) % m_tabBar->count();
220
 
  } else {
221
 
    page --;
222
 
  }
223
 
 
224
 
  if (page < 0) {
225
 
    page = m_tabBar->count() - 1;
226
 
  }
227
 
 
228
 
  m_tabBar->setCurrentIndex(page);
229
 
}
230
 
 
231
 
void TabBarPluginView::slotTabMoved(int from, int to)
232
 
{
233
 
  KTextEditor::Document* document = m_docList.takeAt(from);
234
 
  m_docList.insert(to, document);
235
 
  rebuildMaps();
236
 
}
237
 
 
238
 
void TabBarPluginView::rebuildMaps() {
239
 
  m_tabDocMap.clear();
240
 
  m_docTabMap.clear();
241
 
 
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;
247
 
  }
248
 
}
249
 
 
250
 
///////////////////////////////////////////////////////////////////////////////
251
 
// TabBarPlugin
252
 
///////////////////////////////////////////////////////////////////////////////
253
 
TabBarPlugin::TabBarPlugin(QObject* parent , const QList<QVariant>&)
254
 
    : Kate::Plugin((Kate::Application*)parent)
255
 
{
256
 
}
257
 
 
258
 
TabBarPlugin::~TabBarPlugin()
259
 
{
260
 
}
261
 
 
262
 
Kate::PluginView *TabBarPlugin::createView(Kate::MainWindow *mainWindow)
263
 
{
264
 
  TabBarPluginView *view = new TabBarPluginView(mainWindow);
265
 
  return view;
266
 
}
267