~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to src/newui/dmainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Alexander Dymo                                  *
 
3
 *   adymo@kdevelop.org                                                    *
 
4
 *                                                                         *
 
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.                       *
 
9
 *                                                                         *
 
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.                          *
 
14
 *                                                                         *
 
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"
 
21
 
 
22
#include <kdebug.h>
 
23
#include <kapplication.h>
 
24
#include <kconfig.h>
 
25
#include <kiconloader.h>
 
26
 
 
27
#include <qtoolbutton.h>
 
28
 
 
29
#include "dtabwidget.h"
 
30
#include "docksplitter.h"
 
31
 
 
32
DMainWindow::DMainWindow(QWidget *parent, const char *name)
 
33
    :KParts::MainWindow(parent, name), m_firstRemoved(false), m_currentWidget(0)
 
34
{
 
35
    loadSettings();
 
36
    createToolWindows();
 
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);
 
41
}
 
42
 
 
43
void DMainWindow::loadSettings()
 
44
{
 
45
    KConfig *config = kapp->config();
 
46
    config->setGroup("UI");
 
47
    m_openTabAfterCurrent = config->readBoolEntry("OpenNewTabAfterCurrent", true);
 
48
    m_showIconsOnTabs = config->readBoolEntry("ShowTabIcons", false);
 
49
}
 
50
 
 
51
DMainWindow::~DMainWindow()
 
52
{
 
53
/*    for (QValueList<QWidget*>::iterator it = m_widgets.begin(); it != m_widgets.end(); ++it)
 
54
        removeWidget(*it);*/
 
55
}
 
56
 
 
57
DDockWindow *DMainWindow::toolWindow(DDockWindow::Position position) const
 
58
{
 
59
    switch (position) {
 
60
        case DDockWindow::Bottom: return m_bottomDock;
 
61
        case DDockWindow::Left: return m_leftDock;
 
62
        case DDockWindow::Right: return m_rightDock;
 
63
    }
 
64
    return 0;
 
65
}
 
66
 
 
67
void DMainWindow::createToolWindows()
 
68
{
 
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);
 
75
}
 
76
 
 
77
void DMainWindow::addWidget(QWidget *widget, const QString &title)
 
78
{
 
79
//     invalidateActiveTabWidget();
 
80
    if (m_firstRemoved && m_activeTabWidget == m_tabs.first())
 
81
    {
 
82
        m_central->addDock(0, 0, m_activeTabWidget);
 
83
        m_firstRemoved = false;
 
84
    }
 
85
        
 
86
    addWidget(m_activeTabWidget, widget, title);
 
87
}
 
88
 
 
89
void DMainWindow::addWidget(DTabWidget *tab, QWidget *widget, const QString &title)
 
90
{
 
91
    int idx = -1;
 
92
    if (m_openTabAfterCurrent && (tab->count() > 0))
 
93
        idx = tab->currentPageIndex() + 1;
 
94
    if (m_showIconsOnTabs)
 
95
    {
 
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);
 
99
    }
 
100
    else
 
101
        tab->insertTab(widget, title, idx);
 
102
    m_widgets.append(widget);
 
103
    m_widgetTabs[widget] = tab;
 
104
    widget->installEventFilter(this);
 
105
    tab->showPage(widget);
 
106
}
 
107
 
 
108
void DMainWindow::removeWidget(QWidget *widget)
 
109
{
 
110
    if (!m_widgets.contains(widget))
 
111
        return; //not a widget in main window
 
112
    
 
113
    if (m_widgetTabs.contains(widget))
 
114
    {
 
115
        DTabWidget *tab = m_widgetTabs[widget];
 
116
        if (tab->indexOf(widget) >= 0)
 
117
        {
 
118
            tab->removePage(widget);
 
119
            widget->reparent(0,QPoint(0,0),false);
 
120
            if (tab->count() == 0)
 
121
            {
 
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())
 
126
                {
 
127
                    QPair<uint, uint> idx = m_central->indexOf(tab);
 
128
                    m_tabs.remove(tab);
 
129
                    m_activeTabWidget = m_tabs.first();
 
130
                    m_central->removeDock(idx.first, idx.second, true);
 
131
                }
 
132
                //only temporarily remove the first tabwidget
 
133
                else
 
134
                {
 
135
                    m_central->removeDock(0, 0, false);
 
136
                    m_firstRemoved = true;
 
137
                }
 
138
                //focus smth in m_activeTabWidget
 
139
                if (m_activeTabWidget)
 
140
                {
 
141
                    if (m_activeTabWidget->currentPage())
 
142
                    {
 
143
                        kdDebug() << "trying best!" << endl;
 
144
                        m_activeTabWidget->currentPage()->setFocus();
 
145
                    }
 
146
                }
 
147
            }
 
148
        }
 
149
    }
 
150
    
 
151
    m_widgets.remove(widget);
 
152
    m_widgetTabs.remove(widget);
 
153
}
 
154
 
 
155
DTabWidget *DMainWindow::splitHorizontal() 
 
156
{
 
157
    m_activeTabWidget = createTab();
 
158
    m_central->addDock(m_central->numRows(), 0, m_activeTabWidget);
 
159
    return m_activeTabWidget;
 
160
}
 
161
 
 
162
DTabWidget *DMainWindow::splitVertical() 
 
163
{
 
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;
 
169
}
 
170
 
 
171
void DMainWindow::invalidateActiveTabWidget()
 
172
{
 
173
/*    QWidget *focused = m_central->focusWidget();
 
174
    kdDebug() << "invalidate: " << focused << endl;
 
175
    if (focused == 0)
 
176
        return;
 
177
    if (!m_widgets.contains(focused))
 
178
    {
 
179
        kdDebug() << "    focused is not in m_widgets" << endl;
 
180
        return;
 
181
    }
 
182
    if (m_widgetTabs.contains(focused))
 
183
    {
 
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;
 
189
    }*/
 
190
}
 
191
 
 
192
DTabWidget *DMainWindow::createTab()
 
193
{
 
194
    DTabWidget *tab = new DTabWidget(m_central);
 
195
    m_tabs.append(tab);
 
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 &)));
 
201
    return tab;
 
202
}
 
203
 
 
204
bool DMainWindow::eventFilter(QObject *obj, QEvent *ev)
 
205
{
 
206
    QWidget *w = (QWidget*)obj;
 
207
    if (!m_widgets.contains(w))
 
208
        return KParts::MainWindow::eventFilter(obj, ev);
 
209
    
 
210
    if ((m_currentWidget != w) && (ev->type() == QEvent::FocusIn))
 
211
    {
 
212
        m_currentWidget = w;
 
213
        emit widgetChanged(w);
 
214
    }
 
215
    else if (ev->type() == QEvent::IconChange)
 
216
    {
 
217
        if (m_widgetTabs.contains(w))
 
218
        {
 
219
            DTabWidget *tab = m_widgetTabs[w];
 
220
            tab->setTabIconSet(w, w->icon() ? (*(w->icon())) : QPixmap());
 
221
        }
 
222
    }
 
223
    else if (ev->type() == QEvent::CaptionChange)
 
224
    {
 
225
        kdDebug() << "caption change" << endl;
 
226
    }
 
227
 
 
228
    return KParts::MainWindow::eventFilter(obj, ev);
 
229
}
 
230
 
 
231
void DMainWindow::closeTab()
 
232
{
 
233
    //nothing to do here, should be reimplemented
 
234
}
 
235
 
 
236
void DMainWindow::tabContext(QWidget *, const QPoint &)
 
237
{
 
238
    //nothing to do here, should be reimplemented
 
239
}
 
240
 
 
241
void DMainWindow::closeTab(QWidget *)
 
242
{
 
243
    //nothing to do here, should be reimplemented
 
244
}
 
245
 
 
246
#include "dmainwindow.moc"