~ubuntu-branches/ubuntu/saucy/kate/saucy

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Rohan Garg, Jonathan Riddell
  • Date: 2013-06-21 00:48:29 UTC
  • mfrom: (1.1.28)
  • Revision ID: package-import@ubuntu.com-20130621004829-y2ui02eg0j47h94y
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release
  - Update and sort install files
  - Drop kubuntu_pate_find_python.diff, kubuntu_kate_initial_preference.patch,
    kubuntu_find_python.diff from debian/patches , not required

[ Jonathan Riddell ]
* New upstream release

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