OSDN Git Service

12199f6582ae2ed78fadc4f6fd6bf1b7ce915bc4
[kita/kita.git] / kita / src / kitaui / tabwidgetbase.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Hideki Ikemoto , (c) 2004 by 421                *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 /* Basic class of Tab widget, Tab bar, and Dock widget. */
12
13 #include "tabwidgetbase.h"
14
15 #include <QtGui/QApplication>
16
17 #include <kaction.h>
18 #include <kactioncollection.h>
19 #include <klocale.h>
20 #include <kshortcutsdialog.h>
21 #include <kstandardshortcut.h>
22 #include <ktabbar.h>
23
24 using namespace Kita;
25
26 TabWidgetBase::TabWidgetBase(QWidget* parent) : KTabWidget(parent)
27 {
28     connect(this, SIGNAL(currentChanged (QWidget *)),
29              SLOT(slotCurrentChanged (QWidget *)));
30     setupActions();
31
32     if (parent) {
33         /* setup part manager */
34         m_manager = new KParts::PartManager(parent, this);
35         m_manager->addManagedTopLevelWidget(parent);
36     } else {
37         m_manager = 0;
38     }
39 }
40
41
42 TabWidgetBase::~TabWidgetBase()
43 {
44     /* remove parts */
45     if (m_manager && !(m_manager->parts().isEmpty())) {
46         KParts::Part * part;
47         while ((part = m_manager->parts().first()) != 0) {
48             m_manager->removePart(part);
49             removePage(part->widget());
50             delete part;
51         }
52     }
53     delete m_manager;
54     m_manager = 0;
55
56     /* remove widgets which don't belong to parts */
57     QWidget* view = currentWidget();
58     while (count() > 0 && view) {
59         removePage(view);
60         delete view;
61         view = currentWidget();
62     }
63 }
64
65 /* public slot  */
66 void TabWidgetBase::slotCurrentChanged(QWidget * w)
67 {
68     if (m_manager == 0) return ;
69     if (w == 0) return ;
70     w->activateWindow();
71     w->setFocus();
72
73     KParts::Part* part = findPartFromWidget(w);
74     if (part) {
75         m_manager->setActivePart(part);
76     }
77 }
78
79
80 /* close num-th tab       */
81 /* see also customEvent   */ /* public slot */
82 void TabWidgetBase::slotCloseTab(int num)
83 {
84     CloseTabEvent * e = new CloseTabEvent(num);
85     QApplication::postEvent(this, e);  // Qt will delete it when done
86 }
87
88
89 /* Calling deleteWidget in the child part will be the
90    cause of crash.  So you need to call deleteWidget
91    via custom event.                                   */ /* protected */ /* virtual */
92 void TabWidgetBase::customEvent(QEvent * e)
93 {
94     if (e->type() == EVENT_CloseTab) {
95         deleteWidget (widget(static_cast< CloseTabEvent* >(e) ->getIndex()));
96     }
97 }
98
99
100 /* protected */ /* virtual */
101 void TabWidgetBase::deleteWidget(QWidget* w)
102 {
103     if (w == 0) return ;
104
105     removePage(w);
106     KParts::Part* part = findPartFromWidget(w);
107     if (part) m_manager->removePart(part);
108     delete w;
109 }
110
111
112 /* protected */
113 KParts::Part* TabWidgetBase::findPartFromWidget(QWidget* w)
114 {
115     if (w == 0) return 0;
116     if (m_manager == 0) return 0;
117     if (m_manager->parts().isEmpty()) return 0;
118
119     KParts::Part *part;
120     QList<KParts::Part*>::const_iterator it = m_manager->parts().begin();
121     while ((part = (*it)) != 0) {
122         if (part->widget() == w) return part;
123         ++it;
124     }
125
126     return 0;
127 }
128
129 /*------------------------------------*/
130 /* common tab actions */
131
132
133 /* private */
134 void TabWidgetBase::setupActions()
135 {
136     actionCollection()->associateWidget(this);
137
138     QString str = i18nc("@action:inmenu", "Configure Shortcuts...");
139     KAction* tabConfigkeysAction
140         = actionCollection()->addAction("tab_configkeys");
141     tabConfigkeysAction->setText(str);
142     connect(tabConfigkeysAction, SIGNAL(triggered()),
143             this, SLOT(slotConfigureKeys()));
144
145     KAction* tabNexttabAction
146         = actionCollection()->addAction("tab_nexttab");
147     tabNexttabAction->setText(i18nc("@action:inmenu", "Activate Next Tab"));
148     tabNexttabAction->setShortcut(KStandardShortcut::tabNext());
149     connect(tabNexttabAction, SIGNAL(triggered()), this, SLOT(slotNextTab()));
150
151     KAction* tabPrevtabAction = actionCollection()->addAction("tab_prevtab");
152     tabPrevtabAction->setText(i18nc("@action:inmenu",
153                 "Activate Previous Tab"));
154     tabPrevtabAction->setShortcut(KStandardShortcut::tabPrev());
155     connect(tabPrevtabAction, SIGNAL(triggered()), this, SLOT(slotPrevTab()));
156
157     KAction* tabClosetabAction = actionCollection()->addAction("tab_closetab");
158     tabClosetabAction->setText(i18nc("@action:inmenu", "Close This Tab"));
159     tabClosetabAction->setShortcut(KStandardShortcut::close());
160     connect(tabClosetabAction, SIGNAL(triggered()),
161             this, SLOT(slotCloseCurrentTab()));
162
163     KAction* tabCloseothertabAction
164         = actionCollection()->addAction("tab_closetotherab");
165     tabCloseothertabAction->setText(i18nc("@action:inmenu",
166                 "Close Other Tabs"));
167     tabCloseothertabAction->setShortcut(KStandardShortcut::close());
168     connect(tabCloseothertabAction, SIGNAL(triggered()),
169             this, SLOT(slotCloseOtherTab()));
170
171     KAction* tabCloserighttabAction
172         = actionCollection()->addAction("tab_closetrightab");
173     tabCloserighttabAction->setText(i18nc("@action:inmenu",
174                 "Close Right Tabs"));
175     connect(tabCloserighttabAction, SIGNAL(triggered()),
176             this, SLOT(slotCloseRightTab()));
177
178     KAction* tabCloselefttabAction
179         = actionCollection()->addAction("tab_closetleftab");
180     tabCloselefttabAction->setText(i18nc("@action:inmenu", "Close Left Tabs"));
181     connect(tabCloselefttabAction, SIGNAL(triggered()),
182             this, SLOT(slotCloseLeftTab()));
183
184     KAction* tabClosealltabAction
185         = actionCollection()->addAction("tab_closealltab");
186     tabClosealltabAction->setText(i18nc("@action:inmenu", "Close All Tabs"));
187     connect(tabClosealltabAction, SIGNAL(triggered()),
188             this, SLOT(slotCloseAllTab()));
189 }
190
191
192
193 /* public slot */
194 void TabWidgetBase::slotConfigureKeys()
195 {
196     QString str = "Tab Actions";
197     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
198     dlg.addCollection(actionCollection(), str);
199     dlg.configure();
200 }
201
202
203 /* public slot */
204 void TabWidgetBase::slotPrevTab()
205 {
206     int max = count();
207     int curpage = currentIndex();
208     if (max <= 1) return ;
209
210     if (curpage == 0) setCurrentIndex(max - 1);
211     else setCurrentIndex(curpage - 1);
212 }
213
214 /* public slot */
215 void TabWidgetBase::slotNextTab()
216 {
217     int max = count();
218     int curpage = currentIndex();
219     if (max <= 1) return ;
220
221     if (curpage == max - 1) setCurrentIndex(0);
222     else setCurrentIndex(curpage + 1);
223 }
224
225
226 /* see also customEvent */ /* public slot */
227 void TabWidgetBase::slotCloseCurrentTab()
228 {
229     slotCloseTab(currentIndex());
230 }
231
232
233 /* see also customEvent */ /* public slot */
234 void TabWidgetBase::slotCloseOtherTab(int idx)
235 {
236     int max = count();
237     if (max == 0) return ;
238     if (idx == -1) idx = currentIndex();
239
240     int i = 0;
241
242     while (i < max && i != idx) {
243         slotCloseTab(0);
244         i++;
245     }
246
247     i++;
248     while (i < max) {
249         slotCloseTab(1);
250         i++;
251     }
252 }
253
254
255 /* see also customEvent */ /* public slot */
256 void TabWidgetBase::slotCloseRightTab(int idx)
257 {
258     int max = count();
259     if (max == 0) return ;
260     if (idx == -1) idx = currentIndex();
261
262     int i, i2;
263
264     i = i2 = idx + 1;
265     while (i < max) {
266         slotCloseTab(i2);
267         i++;
268     }
269 }
270
271
272 /* see also customEvent */ /* public slot */
273 void TabWidgetBase::slotCloseLeftTab(int idx)
274 {
275     int max = count();
276     if (max == 0) return ;
277     if (idx == -1) idx = currentIndex();
278
279     int i = 0;
280
281     while (i < max && i != idx) {
282         slotCloseTab(0);
283         i++;
284     }
285 }
286
287
288 /* see also customEvent */ /* public slot */
289 void TabWidgetBase::slotCloseAllTab()
290 {
291     int max = count();
292     if (max == 0) return ;
293
294     int i = 0;
295
296     while (i < max) {
297         slotCloseTab(0);
298         i++;
299     }
300 }