1
/***************************************************************************
2
* Copyright (C) 2005 by Alexander Dymo *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU Library General Public License as *
7
* published by the Free Software Foundation; either version 2 of the *
8
* License, or (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU Library General Public *
16
* License along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
20
#include "dmainwindow.h"
23
#include <kapplication.h>
25
#include <kiconloader.h>
27
#include <qtoolbutton.h>
29
#include "dtabwidget.h"
30
#include "docksplitter.h"
32
DMainWindow::DMainWindow(QWidget *parent, const char *name)
33
:KParts::MainWindow(parent, name), m_firstRemoved(false), m_currentWidget(0)
37
m_central = new Ideal::DockSplitter(Qt::Horizontal, this);
38
m_activeTabWidget = createTab();
39
m_central->addDock(0, 0, m_activeTabWidget);
40
setCentralWidget(m_central);
43
void DMainWindow::loadSettings()
45
KConfig *config = kapp->config();
46
config->setGroup("UI");
47
m_openTabAfterCurrent = config->readBoolEntry("OpenNewTabAfterCurrent", true);
48
m_showIconsOnTabs = config->readBoolEntry("ShowTabIcons", false);
51
DMainWindow::~DMainWindow()
53
/* for (QValueList<QWidget*>::iterator it = m_widgets.begin(); it != m_widgets.end(); ++it)
57
DDockWindow *DMainWindow::toolWindow(DDockWindow::Position position) const
60
case DDockWindow::Bottom: return m_bottomDock;
61
case DDockWindow::Left: return m_leftDock;
62
case DDockWindow::Right: return m_rightDock;
67
void DMainWindow::createToolWindows()
69
m_bottomDock = new DDockWindow(this, DDockWindow::Bottom);
70
moveDockWindow(m_bottomDock, Qt::DockBottom);
71
m_leftDock = new DDockWindow(this, DDockWindow::Left);
72
moveDockWindow(m_leftDock, Qt::DockLeft);
73
m_rightDock = new DDockWindow(this, DDockWindow::Right);
74
moveDockWindow(m_rightDock, Qt::DockRight);
77
void DMainWindow::addWidget(QWidget *widget, const QString &title)
79
// invalidateActiveTabWidget();
80
if (m_firstRemoved && m_activeTabWidget == m_tabs.first())
82
m_central->addDock(0, 0, m_activeTabWidget);
83
m_firstRemoved = false;
86
addWidget(m_activeTabWidget, widget, title);
89
void DMainWindow::addWidget(DTabWidget *tab, QWidget *widget, const QString &title)
92
if (m_openTabAfterCurrent && (tab->count() > 0))
93
idx = tab->currentPageIndex() + 1;
94
if (m_showIconsOnTabs)
96
const QPixmap *pixmap = widget->icon();
97
const QIconSet &icons = (pixmap && (pixmap->size().height() <= 16)) ? *(pixmap) : SmallIcon("kdevelop");
98
tab->insertTab(widget, icons, title, idx);
101
tab->insertTab(widget, title, idx);
102
m_widgets.append(widget);
103
m_widgetTabs[widget] = tab;
104
widget->installEventFilter(this);
105
tab->showPage(widget);
108
void DMainWindow::removeWidget(QWidget *widget)
110
if (!m_widgets.contains(widget))
111
return; //not a widget in main window
113
if (m_widgetTabs.contains(widget))
115
DTabWidget *tab = m_widgetTabs[widget];
116
if (tab->indexOf(widget) >= 0)
118
tab->removePage(widget);
119
widget->reparent(0,QPoint(0,0),false);
120
if (tab->count() == 0)
122
if (tab->closeButton())
123
tab->closeButton()->hide();
124
//remove and delete tabwidget if it is not the first one
125
if (tab != m_tabs.first())
127
QPair<uint, uint> idx = m_central->indexOf(tab);
129
m_activeTabWidget = m_tabs.first();
130
m_central->removeDock(idx.first, idx.second, true);
132
//only temporarily remove the first tabwidget
135
m_central->removeDock(0, 0, false);
136
m_firstRemoved = true;
138
//focus smth in m_activeTabWidget
139
if (m_activeTabWidget)
141
if (m_activeTabWidget->currentPage())
143
kdDebug() << "trying best!" << endl;
144
m_activeTabWidget->currentPage()->setFocus();
151
m_widgets.remove(widget);
152
m_widgetTabs.remove(widget);
155
DTabWidget *DMainWindow::splitHorizontal()
157
m_activeTabWidget = createTab();
158
m_central->addDock(m_central->numRows(), 0, m_activeTabWidget);
159
return m_activeTabWidget;
162
DTabWidget *DMainWindow::splitVertical()
164
// invalidateActiveTabWidget();
165
int row = m_central->indexOf(m_activeTabWidget).first;
166
m_activeTabWidget = createTab();
167
m_central->addDock(row, m_central->numCols(row), m_activeTabWidget);
168
return m_activeTabWidget;
171
void DMainWindow::invalidateActiveTabWidget()
173
/* QWidget *focused = m_central->focusWidget();
174
kdDebug() << "invalidate: " << focused << endl;
177
if (!m_widgets.contains(focused))
179
kdDebug() << " focused is not in m_widgets" << endl;
182
if (m_widgetTabs.contains(focused))
184
kdDebug() << " focused is in m_widgets and m_widgetTabs" << endl;
185
DTabWidget *tab = m_widgetTabs[focused];
186
if (tab->indexOf(focused) >= 0)
187
m_activeTabWidget = tab;
188
kdDebug() << " tab: " << tab << endl;
192
DTabWidget *DMainWindow::createTab()
194
DTabWidget *tab = new DTabWidget(m_central);
196
if (tab->closeButton())
197
connect(tab->closeButton(), SIGNAL(clicked()), this, SLOT(closeTab()));
198
connect(tab, SIGNAL(closeRequest(QWidget*)), this, SLOT(closeTab(QWidget*)));
199
connect(tab, SIGNAL(contextMenu(QWidget*,const QPoint &)),
200
this, SLOT(tabContext(QWidget*,const QPoint &)));
204
bool DMainWindow::eventFilter(QObject *obj, QEvent *ev)
206
QWidget *w = (QWidget*)obj;
207
if (!m_widgets.contains(w))
208
return KParts::MainWindow::eventFilter(obj, ev);
210
if ((m_currentWidget != w) && (ev->type() == QEvent::FocusIn))
213
emit widgetChanged(w);
215
else if (ev->type() == QEvent::IconChange)
217
if (m_widgetTabs.contains(w))
219
DTabWidget *tab = m_widgetTabs[w];
220
tab->setTabIconSet(w, w->icon() ? (*(w->icon())) : QPixmap());
223
else if (ev->type() == QEvent::CaptionChange)
225
kdDebug() << "caption change" << endl;
228
return KParts::MainWindow::eventFilter(obj, ev);
231
void DMainWindow::closeTab()
233
//nothing to do here, should be reimplemented
236
void DMainWindow::tabContext(QWidget *, const QPoint &)
238
//nothing to do here, should be reimplemented
241
void DMainWindow::closeTab(QWidget *)
243
//nothing to do here, should be reimplemented
246
#include "dmainwindow.moc"