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

« back to all changes in this revision

Viewing changes to src/simplemainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

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 "simplemainwindow.h"
21
 
 
22
 
#include <qtextedit.h>
23
 
 
24
 
#include <kaction.h>
25
 
#include <kstdaction.h>
26
 
#include <kparts/part.h>
27
 
#include <kpopupmenu.h>
28
 
#include <kmenubar.h>
29
 
#include <kmessagebox.h>
30
 
#include <klocale.h>
31
 
#include <kiconloader.h>
32
 
#include <kedittoolbar.h>
33
 
#include <ktexteditor/view.h>
34
 
#include <ktexteditor/document.h>
35
 
#include <kapplication.h>
36
 
#include <kurldrag.h>
37
 
#include <kconfig.h>
38
 
 
39
 
#include <ddockwindow.h>
40
 
#include <dtabwidget.h>
41
 
#include <profile.h>
42
 
#include <profileengine.h>
43
 
#include <designer.h>
44
 
#include <kdevproject.h>
45
 
#include <urlutil.h>
46
 
 
47
 
#include "api.h"
48
 
#include "core.h"
49
 
#include "plugincontroller.h"
50
 
#include "mainwindowshare.h"
51
 
#include "shellextension.h"
52
 
#include "partcontroller.h"
53
 
#include "statusbar.h"
54
 
#include "documentationpart.h"
55
 
#include "toplevel.h"
56
 
#include "projectmanager.h"
57
 
#include "editorproxy.h"
58
 
#include "multibuffer.h"
59
 
 
60
 
SimpleMainWindow::SimpleMainWindow(QWidget* parent, const char *name)
61
 
    :DMainWindow(parent, name)
62
 
{
63
 
    resize(800, 600); // starts kdevelop at 800x600 the first time
64
 
    m_mainWindowShare = new MainWindowShare(this);
65
 
 
66
 
    connect(m_bottomDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
67
 
    connect(m_leftDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
68
 
    connect(m_rightDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
69
 
}
70
 
 
71
 
SimpleMainWindow::~ SimpleMainWindow( )
72
 
{
73
 
    TopLevel::invalidateInstance( this );
74
 
}
75
 
 
76
 
void SimpleMainWindow::init()
77
 
{
78
 
    actionCollection()->setHighlightingEnabled( true );
79
 
    setStandardToolBarMenuEnabled( true );
80
 
    setXMLFile(ShellExtension::getInstance()->xmlFile());
81
 
 
82
 
    createFramework();
83
 
    createActions();
84
 
    new KDevStatusBar(this);
85
 
 
86
 
    setAcceptDrops(true);
87
 
 
88
 
    createGUI(0);
89
 
 
90
 
    m_mainWindowShare->init();
91
 
    setupWindowMenu();
92
 
    menuBar()->setEnabled( false );
93
 
 
94
 
    //FIXME: this checks only for global offers which is not quite correct because
95
 
    //a profile can offer core plugins and no global plugins.
96
 
    if ( PluginController::getInstance()->engine().allOffers(ProfileEngine::Global).isEmpty() )
97
 
    {
98
 
        KMessageBox::sorry( this, i18n("Unable to find plugins, KDevelop will not work"
99
 
            " properly.\nPlease make sure "
100
 
            "that KDevelop is installed in your KDE directory; otherwise, you have "
101
 
            "to add KDevelop's installation "
102
 
            "path to the environment variable KDEDIRS and run kbuildsycoca. Restart "
103
 
            "KDevelop afterwards.\n"
104
 
            "Example for BASH users:\nexport KDEDIRS=/path/to/kdevelop:$KDEDIRS && kbuildsycoca"),
105
 
            i18n("Could Not Find Plugins") );
106
 
    }
107
 
 
108
 
    connect(Core::getInstance(), SIGNAL(coreInitialized()), this, SLOT(slotCoreInitialized()));
109
 
    connect(Core::getInstance(), SIGNAL(projectOpened()), this, SLOT(projectOpened()));
110
 
    connect(Core::getInstance(), SIGNAL(projectClosed()), this, SLOT(projectClosed()));
111
 
    connect(Core::getInstance(), SIGNAL(contextMenu(QPopupMenu *, const Context *)),
112
 
        this, SLOT(contextMenu(QPopupMenu *, const Context *)));
113
 
    connect(PartController::getInstance(), SIGNAL(partURLChanged(KParts::ReadOnlyPart *)),
114
 
        this, SLOT(slotPartURLChanged(KParts::ReadOnlyPart * )));
115
 
    connect(PartController::getInstance(), SIGNAL(activePartChanged(KParts::Part*)),
116
 
        this, SLOT(activePartChanged(KParts::Part*)));
117
 
 
118
 
    connect(PartController::getInstance(),
119
 
        SIGNAL(documentChangedState(const KURL &, DocumentState)),
120
 
        this, SLOT(documentChangedState(const KURL&, DocumentState)));
121
 
 
122
 
    loadSettings();
123
 
}
124
 
 
125
 
void SimpleMainWindow::dragEnterEvent( QDragEnterEvent *event )
126
 
{
127
 
    event->accept(KURLDrag::canDecode(event));
128
 
}
129
 
 
130
 
void SimpleMainWindow::dropEvent( QDropEvent *event )
131
 
{
132
 
    slotDropEvent(event);
133
 
}
134
 
 
135
 
void SimpleMainWindow::slotDropEvent( QDropEvent *event )
136
 
{
137
 
    KURL::List list;
138
 
    if ( !KURLDrag::decode( event, list ) ) return;
139
 
 
140
 
    KURL::List::Iterator it = list.begin();
141
 
    while(  it != list.end() )
142
 
    {
143
 
        kdDebug(9000) << "drop url:" << *it << endl;
144
 
        PartController::getInstance()->editDocument( *it );
145
 
        ++it;
146
 
    }
147
 
}
148
 
 
149
 
void SimpleMainWindow::contextMenu(QPopupMenu *popupMenu, const Context *context)
150
 
{
151
 
    int cont = context->type();
152
 
    m_splitURLs.clear();
153
 
    if (cont == Context::EditorContext)
154
 
    {
155
 
        KURL url = static_cast<const EditorContext*>(context)->url();
156
 
        QWidget *w = widgetForURL(url);
157
 
        if (w && m_widgetTabs[w] && m_widgetTabs[w]->count() > 1)
158
 
        {
159
 
            m_splitURLs.append(url);
160
 
            m_splitHor1->plug(popupMenu);
161
 
            m_splitVer1->plug(popupMenu);
162
 
            popupMenu->insertSeparator();
163
 
        }
164
 
    }
165
 
    else if (cont == Context::FileContext)
166
 
    {
167
 
        if (PartController::getInstance()->openURLs().count() > 0)
168
 
        {
169
 
            KURL::List urls = static_cast<const FileContext*>(context)->urls();
170
 
            KURL::List::ConstIterator it = urls.begin();
171
 
            while ( it != urls.end() )
172
 
            {
173
 
                if ( !URLUtil::isDirectory( *it ) )
174
 
                {
175
 
                    m_splitURLs.append( *it );
176
 
                }
177
 
                ++it;
178
 
            }
179
 
 
180
 
            if ( m_splitURLs.isEmpty() ) return;
181
 
 
182
 
            bool isOpen = true;
183
 
            for (KURL::List::const_iterator it = m_splitURLs.begin(); it != m_splitURLs.end(); ++it)
184
 
            {
185
 
                if (!PartController::getInstance()->openURLs().contains(*it) && (*it).isValid())
186
 
                {
187
 
                    isOpen = false;
188
 
                    break;
189
 
                }
190
 
            }
191
 
 
192
 
            if ( (isOpen && PartController::getInstance()->openURLs().count() == 1) ||
193
 
                 (m_splitURLs.count() == 1 && !(*m_splitURLs.begin()).isValid() ) )
194
 
            {
195
 
                return;
196
 
            }
197
 
 
198
 
            popupMenu->insertSeparator();
199
 
            if (isOpen)
200
 
            {
201
 
                m_splitHor1->plug(popupMenu);
202
 
                m_splitVer1->plug(popupMenu);
203
 
            }
204
 
            else
205
 
            {
206
 
                m_splitHor2->plug(popupMenu);
207
 
                m_splitVer2->plug(popupMenu);
208
 
            }
209
 
        }
210
 
    }
211
 
}
212
 
 
213
 
void SimpleMainWindow::embedPartView(QWidget *view, const QString &title, const QString &/*toolTip*/)
214
 
{
215
 
    kdDebug() << "SimpleMainWindow::embedPartView: " << view << endl;
216
 
    if (!view )
217
 
        return;
218
 
 
219
 
    QString shortName = title;
220
 
    shortName = shortName.right( shortName.length() - (shortName.findRev('/') +1));
221
 
 
222
 
    addWidget(view, title);
223
 
    view->show();
224
 
}
225
 
 
226
 
void SimpleMainWindow::embedSelectView(QWidget *view, const QString &title, const QString &/*toolTip*/)
227
 
{
228
 
    embedView( DDockWindow::Left, view, title );
229
 
}
230
 
 
231
 
void SimpleMainWindow::embedOutputView(QWidget *view, const QString &title, const QString &/*toolTip*/)
232
 
{
233
 
    embedView( DDockWindow::Bottom, view, title );
234
 
}
235
 
 
236
 
void SimpleMainWindow::embedSelectViewRight(QWidget *view, const QString &title, const QString &/*toolTip*/)
237
 
{
238
 
    embedView( DDockWindow::Right, view, title );
239
 
}
240
 
 
241
 
void SimpleMainWindow::embedView( DDockWindow::Position pos, QWidget * view, const QString & title )
242
 
{
243
 
    if ( !hasDockWidget( view ) )
244
 
    {
245
 
        DDockWindow::Position position = recallToolViewPosition( view->name(), pos );
246
 
        addDockWidget(position, view, title);
247
 
    }
248
 
}
249
 
 
250
 
DDockWindow::Position SimpleMainWindow::recallToolViewPosition( const QString & name, DDockWindow::Position defaultPos )
251
 
{
252
 
    KConfig * config = kapp->config();
253
 
    config->setGroup( "DToolDockPosition" );
254
 
 
255
 
    QString position = config->readEntry( name );
256
 
 
257
 
    if ( name == "unknown" )
258
 
    {
259
 
        kdDebug() << name << endl;
260
 
    }
261
 
 
262
 
    if ( position == "DockLeft" ) return DDockWindow::Left;
263
 
    if ( position == "DockRight" ) return DDockWindow::Right;
264
 
    if ( position == "DockBottom" ) return DDockWindow::Bottom;
265
 
 
266
 
    return defaultPos;
267
 
}
268
 
 
269
 
void SimpleMainWindow::rememberToolViewPosition( const QString & name, DDockWindow::Position pos )
270
 
{
271
 
    KConfig * config = kapp->config();
272
 
    config->setGroup( "DToolDockPosition" );
273
 
 
274
 
    QString position = "unknown";
275
 
 
276
 
    switch( pos )
277
 
    {
278
 
        case DDockWindow::Left:
279
 
            position = "DockLeft";
280
 
            break;
281
 
        case DDockWindow::Right:
282
 
            position = "DockRight";
283
 
            break;
284
 
        case DDockWindow::Bottom:
285
 
            position = "DockBottom";
286
 
            break;
287
 
        default: ;
288
 
    }
289
 
 
290
 
    config->writeEntry( name, position );
291
 
}
292
 
 
293
 
 
294
 
void SimpleMainWindow::removeView(QWidget *view)
295
 
{
296
 
    if (!view)
297
 
        return;
298
 
 
299
 
    //try to remove it from all parts of main window
300
 
    //@fixme This method needs to be divided in two - one for docks and one for part views
301
 
    if (hasDockWidget(view))
302
 
    {
303
 
        rememberToolViewPosition( view->name(), dockWidgetPosition(view) );
304
 
        removeDockWidget(view);
305
 
    }
306
 
    else
307
 
        removeWidget(view);
308
 
}
309
 
 
310
 
void SimpleMainWindow::setViewAvailable(QWidget *pView, bool bEnabled)
311
 
{
312
 
    DDockWindow *dock;
313
 
    if (hasDockWidget(pView))
314
 
        dock = toolWindow(dockWidgetPosition(pView));
315
 
    else
316
 
        return;
317
 
 
318
 
    bEnabled ? dock->showWidget(pView) : dock->hideWidget(pView);
319
 
}
320
 
 
321
 
void SimpleMainWindow::raiseView(QWidget *view)
322
 
{
323
 
    //adymo: a workaround to make editor wrappers work:
324
 
    //editor view is passed to this function but the ui library knows only
325
 
    //of its parent which is an editor wrapper, simply replacing the view
326
 
    //by its wrapper helps here
327
 
    if (view->parent())
328
 
        kdDebug() << view->parent()->className() << endl;
329
 
    if (view->parent() && (view->parent()->isA("EditorWrapper") || view->parent()->isA("MultiBuffer")))
330
 
    {
331
 
//         kdDebug() << "parent is editor wrapper: " <<
332
 
//             static_cast<EditorWrapper*>(view->parent()) << endl;
333
 
        view = (QWidget*)view->parent();
334
 
    }
335
 
 
336
 
    if (hasDockWidget(view))
337
 
    {
338
 
        DDockWindow *dock = toolWindow(dockWidgetPosition(view));
339
 
        dock->raiseWidget(view);
340
 
    }
341
 
    else if (m_widgets.contains(view) && m_widgetTabs.contains(view))
342
 
        m_widgetTabs[view]->showPage(view);
343
 
}
344
 
 
345
 
void SimpleMainWindow::lowerView(QWidget * view)
346
 
{
347
 
    if (!hasDockWidget(view))
348
 
        return;
349
 
 
350
 
    DDockWindow *dock = toolWindow(dockWidgetPosition(view));
351
 
    dock->lowerWidget(view);
352
 
}
353
 
 
354
 
void SimpleMainWindow::loadSettings()
355
 
{
356
 
    KConfig *config = kapp->config();
357
 
 
358
 
    ProjectManager::getInstance()->loadSettings();
359
 
    applyMainWindowSettings(config, "SimpleMainWindow");
360
 
 
361
 
    Core::setupShourtcutTips();
362
 
}
363
 
 
364
 
void SimpleMainWindow::saveSettings( )
365
 
{
366
 
    KConfig *config = kapp->config();
367
 
 
368
 
    ProjectManager::getInstance()->saveSettings();
369
 
    saveMainWindowSettings(config, "SimpleMainWindow");
370
 
 
371
 
    QMap<QWidget*, DDockWindow::Position>::iterator it = m_docks.begin();
372
 
    while ( it != m_docks.end() )
373
 
    {
374
 
        if ( !strcmp(it.key()->name(), "unnamed") )
375
 
        {
376
 
            kdError() << "Toolviews must have a name : " << it.key() << endl;
377
 
        }
378
 
 
379
 
        rememberToolViewPosition( it.key()->name(), it.data() );
380
 
        ++it;
381
 
    }
382
 
 
383
 
    DMainWindow::saveSettings();
384
 
}
385
 
 
386
 
void SimpleMainWindow::setCurrentDocumentCaption( const QString &caption )
387
 
{
388
 
    if( !PartController::getInstance()->activePart() ) return;
389
 
 
390
 
    if (QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(PartController::getInstance()->activePart()))
391
 
    {
392
 
        widget->setCaption(caption);
393
 
    }
394
 
}
395
 
 
396
 
KMainWindow *SimpleMainWindow::main()
397
 
{
398
 
    return this;
399
 
}
400
 
 
401
 
void SimpleMainWindow::createFramework()
402
 
{
403
 
    PartController::createInstance( this );
404
 
 
405
 
    connect(PartController::getInstance(), SIGNAL(activePartChanged(KParts::Part*)),
406
 
        this, SLOT(createGUI(KParts::Part*)));
407
 
}
408
 
 
409
 
void SimpleMainWindow::createActions()
410
 
{
411
 
    m_raiseEditor = new KAction(i18n("Raise &Editor"), ALT+Key_C,
412
 
        this, SLOT(raiseEditor()), actionCollection(), "raise_editor");
413
 
    m_raiseEditor->setToolTip(i18n("Raise editor"));
414
 
    m_raiseEditor->setWhatsThis(i18n("<b>Raise editor</b><p>Focuses the editor."));
415
 
 
416
 
    m_lowerAllDocks = new KAction(i18n("Lower All Docks"), CTRL+SHIFT+Key_C,
417
 
        this, SLOT(lowerAllDocks()), actionCollection(), "lower_all_docks");
418
 
 
419
 
    new KAction(i18n("Switch to next TabWidget"), 0, this,
420
 
        SLOT(switchToNextTabWidget()), actionCollection(), "switch_to_next_tabwidget" );
421
 
 
422
 
    m_splitHor = new KAction(i18n("Split &Horizontal"), CTRL+SHIFT+Key_T,
423
 
        this, SLOT(slotSplitHorizontalBase()), actionCollection(), "split_h");
424
 
    m_splitHor->setIcon("view_top_bottom");
425
 
 
426
 
    m_splitVer = new KAction(i18n("Split &Vertical"), CTRL+SHIFT+Key_L,
427
 
        this, SLOT(slotSplitVerticalBase()), actionCollection(), "split_v");
428
 
    m_splitVer->setIcon("view_left_right");
429
 
 
430
 
    m_splitHor1 = new KAction(i18n("Split &Horizontal"), 0,
431
 
        this, SLOT(slotSplitHorizontal()), actionCollection(), "split_h1");
432
 
    m_splitHor1->setIcon("view_top_bottom");
433
 
 
434
 
    m_splitVer1 = new KAction(i18n("Split &Vertical"), 0,
435
 
        this, SLOT(slotSplitVertical()), actionCollection(), "split_v1");
436
 
    m_splitVer1->setIcon("view_left_right");
437
 
 
438
 
    m_splitHor2 = new KAction(i18n("Split &Horizontal and Open"), 0,
439
 
        this, SLOT(slotSplitHorizontal()), actionCollection(), "split_h2");
440
 
    m_splitHor2->setIcon("view_top_bottom");
441
 
 
442
 
    m_splitVer2 = new KAction(i18n("Split &Vertical and Open"), 0,
443
 
        this, SLOT(slotSplitVertical()), actionCollection(), "split_v2");
444
 
    m_splitVer2->setIcon("view_left_right");
445
 
 
446
 
    m_raiseLeftDock = new KAction(i18n("Switch Left Dock"), CTRL+SHIFT+ALT+Key_L,
447
 
        this, SLOT(raiseLeftDock()), actionCollection(), "switch_left_dock");
448
 
 
449
 
    m_raiseRightDock = new KAction(i18n("Switch Right Dock"), CTRL+SHIFT+ALT+Key_R,
450
 
        this, SLOT(raiseRightDock()), actionCollection(), "switch_right_dock");
451
 
 
452
 
    m_raiseBottomDock = new KAction(i18n("Switch Bottom Dock"), CTRL+SHIFT+ALT+Key_B,
453
 
        this, SLOT(raiseBottomDock()), actionCollection(), "switch_bottom_dock");
454
 
 
455
 
    KStdAction::configureToolbars(this, SLOT(configureToolbars()),
456
 
        actionCollection(), "set_configure_toolbars");
457
 
 
458
 
    m_mainWindowShare->createActions();
459
 
 
460
 
    connect(m_mainWindowShare, SIGNAL(gotoNextWindow()), this, SLOT(gotoNextWindow()));
461
 
    connect(m_mainWindowShare, SIGNAL(gotoPreviousWindow()), this, SLOT(gotoPreviousWindow()));
462
 
    connect(m_mainWindowShare, SIGNAL(gotoFirstWindow()), this, SLOT(gotoFirstWindow()));
463
 
    connect(m_mainWindowShare, SIGNAL(gotoLastWindow()), this, SLOT(gotoLastWindow()));
464
 
}
465
 
 
466
 
void SimpleMainWindow::raiseEditor()
467
 
{
468
 
    kdDebug() << "SimpleMainWindow::raiseEditor" << endl;
469
 
    KDevPartController *partcontroller = API::getInstance()->partController();
470
 
    if (partcontroller->activePart() && partcontroller->activePart()->widget())
471
 
        partcontroller->activePart()->widget()->setFocus();
472
 
}
473
 
 
474
 
void SimpleMainWindow::gotoNextWindow()
475
 
{
476
 
    if (m_bottomDock->isActive())
477
 
        m_bottomDock->selectNextWidget();
478
 
    else if (m_rightDock->isActive())
479
 
        m_rightDock->selectNextWidget();
480
 
    else if (m_leftDock->isActive())
481
 
        m_leftDock->selectNextWidget();
482
 
    else
483
 
    {
484
 
        if ((m_activeTabWidget->currentPageIndex() + 1) < m_activeTabWidget->count())
485
 
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->currentPageIndex() + 1);
486
 
        else
487
 
            m_activeTabWidget->setCurrentPage(0);
488
 
    }
489
 
}
490
 
 
491
 
void SimpleMainWindow::gotoPreviousWindow()
492
 
{
493
 
    if (m_bottomDock->isActive())
494
 
        m_bottomDock->selectPrevWidget();
495
 
    else if (m_rightDock->isActive())
496
 
        m_rightDock->selectPrevWidget();
497
 
    else if (m_leftDock->isActive())
498
 
        m_leftDock->selectPrevWidget();
499
 
    else
500
 
    {
501
 
        if ((m_activeTabWidget->currentPageIndex() - 1) >= 0)
502
 
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->currentPageIndex() - 1);
503
 
        else
504
 
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->count() - 1);
505
 
    }
506
 
}
507
 
 
508
 
void SimpleMainWindow::gotoFirstWindow()
509
 
{
510
 
    //@TODO: Implement me
511
 
}
512
 
 
513
 
void SimpleMainWindow::gotoLastWindow()
514
 
{
515
 
    //@TODO: implement me
516
 
}
517
 
 
518
 
void SimpleMainWindow::slotCoreInitialized()
519
 
{
520
 
    menuBar()->setEnabled(true);
521
 
}
522
 
 
523
 
void SimpleMainWindow::projectOpened()
524
 
{
525
 
    setCaption(QString::null);
526
 
}
527
 
 
528
 
void SimpleMainWindow::slotPartURLChanged(KParts::ReadOnlyPart *part)
529
 
{
530
 
    if (QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(part))
531
 
    {
532
 
        kdDebug() << "new caption for widget: " << part->url().fileName() << endl;
533
 
        widget->setCaption(part->url().fileName());
534
 
    }
535
 
        //do smth with caption: ro_part->url().fileName()
536
 
}
537
 
 
538
 
void SimpleMainWindow::documentChangedState(const KURL &url, DocumentState state)
539
 
{
540
 
    QWidget * widget = EditorProxy::getInstance()->topWidgetForPart(
541
 
        PartController::getInstance()->partForURL(url));
542
 
    kdDebug() << "SimpleMainWindow::documentChangedState: " << widget << endl;
543
 
    if (widget)
544
 
    {
545
 
        //calculate the icon size if showTabIcons is false
546
 
        //this is necessary to avoid tab resizing by setIcon() call
547
 
        int isize = 16;
548
 
        if (m_activeTabWidget && !m_showIconsOnTabs)
549
 
        {
550
 
            isize = m_activeTabWidget->fontMetrics().height() - 1;
551
 
            isize = isize > 16 ? 16 : isize;
552
 
        }
553
 
        switch (state)
554
 
        {
555
 
            // we should probably restore the original icon instead of just using "kdevelop",
556
 
            // but I have never seen any other icon in use so this should do for now
557
 
            case Clean:
558
 
                if (m_showIconsOnTabs)
559
 
                    widget->setIcon(SmallIcon("kdevelop", isize));
560
 
                else
561
 
                    widget->setIcon(QPixmap());
562
 
                break;
563
 
            case Modified:
564
 
                widget->setIcon(SmallIcon("filesave", isize));
565
 
                break;
566
 
            case Dirty:
567
 
                widget->setIcon(SmallIcon("revert", isize));
568
 
                break;
569
 
            case DirtyAndModified:
570
 
                widget->setIcon(SmallIcon("stop", isize));
571
 
                break;
572
 
        }
573
 
    }
574
 
    setCaption(url.url());
575
 
}
576
 
 
577
 
void SimpleMainWindow::closeTab()
578
 
{
579
 
//    actionCollection()->action("file_close")->activate();
580
 
    if (sender()->isA("QToolButton") && sender()->parent()->isA("DTabWidget"))
581
 
    {
582
 
        DTabWidget *tab = (DTabWidget*)sender()->parent();
583
 
        if (tab && tab->currentPage())
584
 
            closeTab(tab->currentPage());
585
 
    }
586
 
}
587
 
 
588
 
void SimpleMainWindow::tabContext(QWidget *w, const QPoint &p)
589
 
{
590
 
    DTabWidget *tabWidget = static_cast<DTabWidget*>(const_cast<QObject*>(sender()));
591
 
    if (!tabWidget)
592
 
        return;
593
 
 
594
 
    KPopupMenu tabMenu;
595
 
    tabMenu.insertTitle(tabWidget->tabLabel(w));
596
 
 
597
 
    //Find the document on whose tab the user clicked
598
 
    m_currentTabURL = QString::null;
599
 
    QPtrListIterator<KParts::Part> it(*PartController::getInstance()->parts());
600
 
    while (KParts::Part* part = it.current())
601
 
    {
602
 
        QWidget *top_widget = EditorProxy::getInstance()->topWidgetForPart(part);
603
 
        if (top_widget == w)
604
 
        {
605
 
            if (KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(part))
606
 
            {
607
 
                m_currentTabURL = ro_part->url();
608
 
                tabMenu.insertItem(i18n("Close"), 0);
609
 
 
610
 
                if (PartController::getInstance()->parts()->count() > 1)
611
 
                    tabMenu.insertItem(i18n("Close All Others"), 4);
612
 
 
613
 
                if( KParts::ReadWritePart * rw_part = dynamic_cast<KParts::ReadWritePart*>( ro_part ) )
614
 
                {
615
 
                    if( rw_part->isModified() ) tabMenu.insertItem( i18n("Save"),1);
616
 
                    tabMenu.insertItem( i18n("Reload"),2);
617
 
                }
618
 
 
619
 
                if (dynamic_cast<HTMLDocumentationPart*>(ro_part))
620
 
                {
621
 
                    tabMenu.insertItem(i18n("Duplicate"), 3);
622
 
                    break;
623
 
                }
624
 
 
625
 
                //Create the file context
626
 
                KURL::List list;
627
 
                list << m_currentTabURL;
628
 
                FileContext context( list );
629
 
                Core::getInstance()->fillContextMenu(&tabMenu, &context);
630
 
            }
631
 
            break;
632
 
        }
633
 
        ++it;
634
 
    }
635
 
 
636
 
    connect(&tabMenu, SIGNAL(activated(int)), this, SLOT(tabContextActivated(int)));
637
 
    tabMenu.exec(p);
638
 
}
639
 
 
640
 
void SimpleMainWindow::tabContextActivated(int id)
641
 
{
642
 
    if(m_currentTabURL.isEmpty())
643
 
            return;
644
 
 
645
 
    switch(id)
646
 
    {
647
 
        case 0:
648
 
            PartController::getInstance()->closeFile(m_currentTabURL);
649
 
            break;
650
 
        case 1:
651
 
            PartController::getInstance()->saveFile(m_currentTabURL);
652
 
            break;
653
 
        case 2:
654
 
            PartController::getInstance()->reloadFile(m_currentTabURL);
655
 
            break;
656
 
        case 3:
657
 
            PartController::getInstance()->showDocument(m_currentTabURL, true);
658
 
            break;
659
 
        case 4:
660
 
            PartController::getInstance()->closeAllOthers(m_currentTabURL);
661
 
            break;
662
 
        default:
663
 
            break;
664
 
    }
665
 
}
666
 
 
667
 
void SimpleMainWindow::configureToolbars()
668
 
{
669
 
    saveMainWindowSettings(KGlobal::config(), "SimpleMainWindow");
670
 
    KEditToolbar dlg(factory());
671
 
    connect(&dlg, SIGNAL(newToolbarConfig()), this, SLOT(slotNewToolbarConfig()));
672
 
    dlg.exec();
673
 
}
674
 
 
675
 
void SimpleMainWindow::slotNewToolbarConfig()
676
 
{
677
 
//    setupWindowMenu();
678
 
    m_mainWindowShare->slotGUICreated(PartController::getInstance()->activePart());
679
 
    applyMainWindowSettings(KGlobal::config(), "SimpleMainWindow");
680
 
}
681
 
 
682
 
bool SimpleMainWindow::queryClose()
683
 
{
684
 
    saveSettings();
685
 
    return Core::getInstance()->queryClose();
686
 
}
687
 
 
688
 
bool SimpleMainWindow::queryExit()
689
 
{
690
 
    return true;
691
 
}
692
 
 
693
 
void SimpleMainWindow::setupWindowMenu()
694
 
{
695
 
    // get the xmlgui created one instead
696
 
    m_windowMenu = static_cast<QPopupMenu*>(main()->child("window", "KPopupMenu"));
697
 
 
698
 
    if (!m_windowMenu)
699
 
    {
700
 
        kdDebug(9000) << "Couldn't find the XMLGUI window menu. Creating new." << endl;
701
 
 
702
 
        m_windowMenu = new QPopupMenu(main(), "window");
703
 
        menuBar()->insertItem(i18n("&Window"), m_windowMenu);
704
 
    }
705
 
 
706
 
    actionCollection()->action("file_close")->plug(m_windowMenu);
707
 
    actionCollection()->action("file_close_all")->plug(m_windowMenu);
708
 
    actionCollection()->action("file_closeother")->plug(m_windowMenu);
709
 
 
710
 
    QObject::connect(m_windowMenu, SIGNAL(activated(int)), this, SLOT(openURL(int)));
711
 
    QObject::connect(m_windowMenu, SIGNAL(aboutToShow()), this, SLOT(fillWindowMenu()));
712
 
}
713
 
 
714
 
void SimpleMainWindow::openURL(int w)
715
 
{
716
 
    QValueList<QPair<int, KURL> >::ConstIterator it = m_windowList.begin();
717
 
    while (it != m_windowList.end())
718
 
    {
719
 
        if ((*it).first == w)
720
 
        {
721
 
            KURL url((*it).second);
722
 
            if (!url.isEmpty())
723
 
            {
724
 
                PartController::getInstance()->editDocument(url);
725
 
                return;
726
 
            }
727
 
        }
728
 
        ++it;
729
 
    }
730
 
}
731
 
 
732
 
void SimpleMainWindow::fillWindowMenu()
733
 
{
734
 
    // clear menu
735
 
    QValueList< QPair< int, KURL > >::ConstIterator it = m_windowList.begin();
736
 
    while (it != m_windowList.end())
737
 
    {
738
 
        m_windowMenu->removeItem( (*it).first );
739
 
        ++it;
740
 
    }
741
 
 
742
 
    int temp = 0;
743
 
 
744
 
    QMap<QString, KURL> map;
745
 
    QStringList string_list;
746
 
    KURL::List list = PartController::getInstance()->openURLs();
747
 
    KURL::List::Iterator itt = list.begin();
748
 
    while (itt != list.end())
749
 
    {
750
 
        map[(*itt).fileName()] = *itt;
751
 
        string_list.append((*itt).fileName());
752
 
        ++itt;
753
 
    }
754
 
    string_list.sort();
755
 
 
756
 
    list.clear();
757
 
    for(uint i = 0; i != string_list.size(); ++i)
758
 
        list.append(map[string_list[i]]);
759
 
 
760
 
    itt = list.begin();
761
 
    int i = 0;
762
 
 
763
 
    if (list.count() > 0)
764
 
        m_windowList << qMakePair(m_windowMenu->insertSeparator(), KURL());
765
 
 
766
 
    while (itt != list.end())
767
 
    {
768
 
        temp = m_windowMenu->insertItem( i < 10 ? QString("&%1 %2").arg(i).arg((*itt).fileName()) : (*itt).fileName() );
769
 
        m_windowList << qMakePair(temp, *itt);
770
 
        ++i;
771
 
        ++itt;
772
 
    }
773
 
}
774
 
 
775
 
void SimpleMainWindow::slotSplitVertical()
776
 
{
777
 
    DTabWidget *tab = splitVertical();
778
 
    openDocumentsAfterSplit(tab);
779
 
}
780
 
 
781
 
void SimpleMainWindow::slotSplitHorizontal()
782
 
{
783
 
    DTabWidget *tab = splitHorizontal();
784
 
    openDocumentsAfterSplit(tab);
785
 
}
786
 
 
787
 
void SimpleMainWindow::slotSplitVerticalBase()
788
 
{
789
 
    if (KParts::ReadOnlyPart *ro_part = activePartForSplitting())
790
 
    {
791
 
        m_splitURLs << ro_part->url();
792
 
        slotSplitVertical();
793
 
    }
794
 
}
795
 
 
796
 
KParts::ReadOnlyPart *SimpleMainWindow::activePartForSplitting()
797
 
{
798
 
    if (PartController::getInstance()->openURLs().count() < 2)
799
 
        return 0;
800
 
    m_splitURLs.clear();
801
 
    KParts::ReadOnlyPart *ro_part =
802
 
        dynamic_cast<KParts::ReadOnlyPart*>(PartController::getInstance()->activePart());
803
 
    return ro_part;
804
 
}
805
 
 
806
 
void SimpleMainWindow::slotSplitHorizontalBase()
807
 
{
808
 
    if (KParts::ReadOnlyPart *ro_part = activePartForSplitting())
809
 
    {
810
 
        m_splitURLs << ro_part->url();
811
 
        slotSplitHorizontal();
812
 
    }
813
 
}
814
 
 
815
 
void SimpleMainWindow::openDocumentsAfterSplit(DTabWidget *tab)
816
 
{
817
 
    if (m_splitURLs.count() > 0)
818
 
    {
819
 
        for (KURL::List::const_iterator it = m_splitURLs.begin(); it != m_splitURLs.end(); ++it)
820
 
        {
821
 
            KParts::ReadOnlyPart *part = PartController::getInstance()->partForURL(*it);
822
 
            if (!part)
823
 
                PartController::getInstance()->editDocument(*it);
824
 
            else
825
 
            {
826
 
                PartController::getInstance()->activatePart( part );
827
 
                QWidget *inTab = widgetForURL(*it);
828
 
                if (inTab)
829
 
                {
830
 
                    DTabWidget *oldTab = m_widgetTabs[inTab];
831
 
                    QString title = oldTab->tabLabel(inTab);
832
 
                    removeWidget(inTab);
833
 
                    addWidget(tab, inTab, title);
834
 
                }
835
 
            }
836
 
        }
837
 
        m_splitURLs.clear();
838
 
    }
839
 
}
840
 
 
841
 
QWidget *SimpleMainWindow::widgetForURL(KURL url)
842
 
{
843
 
    KParts::ReadOnlyPart *part = PartController::getInstance()->partForURL(url);
844
 
    return widgetInTab( part ? part->widget() : 0 );
845
 
}
846
 
 
847
 
QWidget *SimpleMainWindow::widgetInTab(QWidget *w)
848
 
{
849
 
    QWidget *inTab = 0;
850
 
    if (w && w->parent() && w->parent()->isA("EditorProxy"))
851
 
        inTab = (QWidget*)w->parent();
852
 
    else if (w && w->parent() && w->parent()->isA("MultiBuffer")
853
 
        && w->parent()->parent() && w->parent()->parent()->isA("EditorProxy"))
854
 
        inTab = (QWidget*)w->parent()->parent();
855
 
    else if (w && w->parent() && w->parent()->isA("MultiBuffer"))
856
 
        inTab = (QWidget*)w->parent();
857
 
    else
858
 
        inTab = w;
859
 
    return inTab;
860
 
}
861
 
 
862
 
void SimpleMainWindow::closeTab(QWidget *w)
863
 
{
864
 
    const QPtrList<KParts::Part> *partlist = PartController::getInstance()->parts();
865
 
    QPtrListIterator<KParts::Part> it(*partlist);
866
 
    while (KParts::Part* part = it.current())
867
 
    {
868
 
        QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(part);
869
 
        if (widget && widget == w)
870
 
        {
871
 
            // weirdness beyond weirdness.. sometimes the active view is an embedded splitter with two files
872
 
            // so we check if the widget is a multibuffer, in which case we let it decide what part to close
873
 
            if (MultiBuffer * mb = dynamic_cast<MultiBuffer*>( widget ) )
874
 
            {
875
 
                PartController::getInstance()->closePart( mb->activeBuffer() );
876
 
            }
877
 
            else
878
 
            {
879
 
                PartController::getInstance()->closePart(part);
880
 
            }
881
 
            return;
882
 
        }
883
 
        ++it;
884
 
    }
885
 
}
886
 
 
887
 
void SimpleMainWindow::activePartChanged(KParts::Part *part)
888
 
{
889
 
    if (!part)
890
 
        return;
891
 
    QWidget *w = part->widget();
892
 
    kdDebug() << "active part widget is : " << w << endl;
893
 
    QWidget *inTab = widgetInTab(w);
894
 
    if (m_widgetTabs[inTab] != 0)
895
 
    {
896
 
        kdDebug() << " setting m_activeTabWidget " << endl;
897
 
        m_activeTabWidget = m_widgetTabs[inTab];
898
 
    }
899
 
}
900
 
 
901
 
void SimpleMainWindow::createGUI(KParts::Part *part) {
902
 
    if ( !part )
903
 
        setCaption( QString::null );
904
 
    DMainWindow::createGUI(part);
905
 
 
906
 
    m_mainWindowShare->slotGUICreated( part );
907
 
}
908
 
 
909
 
void SimpleMainWindow::raiseBottomDock()
910
 
{
911
 
    raiseDock(m_bottomDock);
912
 
}
913
 
 
914
 
void SimpleMainWindow::raiseLeftDock()
915
 
{
916
 
    raiseDock(m_leftDock);
917
 
}
918
 
 
919
 
void SimpleMainWindow::raiseRightDock()
920
 
{
921
 
    raiseDock(m_rightDock);
922
 
}
923
 
 
924
 
void SimpleMainWindow::raiseDock(DDockWindow *dock)
925
 
{
926
 
    dock->selectLastWidget();
927
 
}
928
 
 
929
 
void SimpleMainWindow::lowerAllDocks()
930
 
{
931
 
    m_bottomDock->lowerWidget( m_bottomDock->currentWidget() );
932
 
    m_leftDock->lowerWidget( m_leftDock->currentWidget() );
933
 
    m_rightDock->lowerWidget( m_rightDock->currentWidget() );
934
 
}
935
 
 
936
 
void SimpleMainWindow::switchToNextTabWidget()
937
 
{
938
 
    QValueList<DTabWidget*> tabWidgets = m_widgetTabs.values();
939
 
 
940
 
    if ( tabWidgets.count() < 2 ) return;
941
 
 
942
 
    QValueList<DTabWidget*> reduced;
943
 
    QValueList<DTabWidget*>::iterator it = tabWidgets.begin();
944
 
    while ( it != tabWidgets.end() )
945
 
    {
946
 
        if ( !reduced.contains( *it ) )
947
 
        {
948
 
            reduced << *it;
949
 
        }
950
 
        ++it;
951
 
    }
952
 
 
953
 
    it = reduced.begin();
954
 
    while ( it != reduced.end() )
955
 
    {
956
 
        if ( *it == m_activeTabWidget )
957
 
        {
958
 
            if ( ++it != reduced.end() )
959
 
            {
960
 
                if ( (*it)->currentPage() )
961
 
                    (*it)->currentPage()->setFocus();
962
 
            }
963
 
            else
964
 
            {
965
 
                if ( (*reduced.begin())->currentPage() )
966
 
                    (*reduced.begin())->currentPage()->setFocus();
967
 
            }
968
 
            return;
969
 
        }
970
 
        ++it;
971
 
    }
972
 
}
973
 
 
974
 
void SimpleMainWindow::setCaption(const QString &caption)
975
 
{
976
 
    kdDebug(9000) << k_funcinfo << endl;
977
 
 
978
 
    bool modified = false;
979
 
    if ( !caption.isEmpty() )
980
 
    {
981
 
        KURL url( caption );
982
 
        DocumentState const state = PartController::getInstance()->documentState( url );
983
 
        modified = ( state == Modified || state == DirtyAndModified );
984
 
    }
985
 
 
986
 
    KDevProject *project = API::getInstance()->project();
987
 
    if (project)
988
 
    {
989
 
        QString projectname = project->projectName();
990
 
 
991
 
        QString suffix(".kdevelop");
992
 
        if (projectname.endsWith(suffix))
993
 
            projectname.truncate(projectname.length() - suffix.length());
994
 
 
995
 
        if (!caption.isEmpty())
996
 
            DMainWindow::setCaption(projectname + " - " + caption, modified);
997
 
        else
998
 
            DMainWindow::setCaption(projectname, modified);
999
 
    }
1000
 
    else
1001
 
        DMainWindow::setCaption(caption, modified);
1002
 
}
1003
 
 
1004
 
void SimpleMainWindow::projectClosed()
1005
 
{
1006
 
    DMainWindow::setCaption(QString::null);
1007
 
}
1008
 
 
1009
 
#include "simplemainwindow.moc"
1010
 
 
1011
 
// kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on