1 /***************************************************************************
2 * Copyright (C) 2004 by Hideki Ikemoto , (c) 2004 by 421 *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
11 /* Basic class of Tab widget, Tab bar, and Dock widget. */
13 #include "tabwidgetbase.h"
15 #include <QtGui/QApplication>
18 #include <kactioncollection.h>
20 #include <kshortcutsdialog.h>
21 #include <kstandardshortcut.h>
26 TabWidgetBase::TabWidgetBase(QWidget* parent) : KTabWidget(parent)
28 connect(this, SIGNAL(currentChanged (QWidget *)),
29 SLOT(slotCurrentChanged (QWidget *)));
33 /* setup part manager */
34 m_manager = new KParts::PartManager(parent, this);
35 m_manager->addManagedTopLevelWidget(parent);
42 TabWidgetBase::~TabWidgetBase()
45 if (m_manager && !(m_manager->parts().isEmpty())) {
47 while ((part = m_manager->parts().first()) != 0) {
48 m_manager->removePart(part);
49 removePage(part->widget());
56 /* remove widgets which don't belong to parts */
57 QWidget* view = currentWidget();
58 while (count() > 0 && view) {
61 view = currentWidget();
66 void TabWidgetBase::slotCurrentChanged(QWidget * w)
68 if (m_manager == 0) return ;
73 KParts::Part* part = findPartFromWidget(w);
75 m_manager->setActivePart(part);
80 /* close num-th tab */
81 /* see also customEvent */ /* public slot */
82 void TabWidgetBase::slotCloseTab(int num)
84 CloseTabEvent * e = new CloseTabEvent(num);
85 QApplication::postEvent(this, e); // Qt will delete it when done
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)
94 if (e->type() == EVENT_CloseTab) {
95 deleteWidget (widget(static_cast< CloseTabEvent* >(e) ->getIndex()));
100 /* protected */ /* virtual */
101 void TabWidgetBase::deleteWidget(QWidget* w)
106 KParts::Part* part = findPartFromWidget(w);
107 if (part) m_manager->removePart(part);
113 KParts::Part* TabWidgetBase::findPartFromWidget(QWidget* w)
115 if (w == 0) return 0;
116 if (m_manager == 0) return 0;
117 if (m_manager->parts().isEmpty()) return 0;
120 QList<KParts::Part*>::const_iterator it = m_manager->parts().begin();
121 while ((part = (*it)) != 0) {
122 if (part->widget() == w) return part;
129 /*------------------------------------*/
130 /* common tab actions */
134 void TabWidgetBase::setupActions()
136 actionCollection()->associateWidget(this);
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
194 void TabWidgetBase::slotConfigureKeys()
196 QString str = "Tab Actions";
197 KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
198 dlg.addCollection(actionCollection(), str);
204 void TabWidgetBase::slotPrevTab()
207 int curpage = currentIndex();
208 if (max <= 1) return ;
210 if (curpage == 0) setCurrentIndex(max - 1);
211 else setCurrentIndex(curpage - 1);
215 void TabWidgetBase::slotNextTab()
218 int curpage = currentIndex();
219 if (max <= 1) return ;
221 if (curpage == max - 1) setCurrentIndex(0);
222 else setCurrentIndex(curpage + 1);
226 /* see also customEvent */ /* public slot */
227 void TabWidgetBase::slotCloseCurrentTab()
229 slotCloseTab(currentIndex());
233 /* see also customEvent */ /* public slot */
234 void TabWidgetBase::slotCloseOtherTab(int idx)
237 if (max == 0) return ;
238 if (idx == -1) idx = currentIndex();
242 while (i < max && i != idx) {
255 /* see also customEvent */ /* public slot */
256 void TabWidgetBase::slotCloseRightTab(int idx)
259 if (max == 0) return ;
260 if (idx == -1) idx = currentIndex();
272 /* see also customEvent */ /* public slot */
273 void TabWidgetBase::slotCloseLeftTab(int idx)
276 if (max == 0) return ;
277 if (idx == -1) idx = currentIndex();
281 while (i < max && i != idx) {
288 /* see also customEvent */ /* public slot */
289 void TabWidgetBase::slotCloseAllTab()
292 if (max == 0) return ;