simplemainwindow.cpp 31 KB
Newer Older
Alexander Dymo's avatar
Alexander Dymo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/***************************************************************************
 *   Copyright (C) 2005 by Alexander Dymo                                  *
 *   adymo@kdevelop.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "simplemainwindow.h"

#include <qtextedit.h>

#include <kaction.h>
#include <kstdaction.h>
#include <kparts/part.h>
#include <kpopupmenu.h>
#include <kmenubar.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kedittoolbar.h>
#include <ktexteditor/view.h>
34
#include <ktexteditor/document.h>
Alexander Dymo's avatar
Alexander Dymo committed
35
#include <kapplication.h>
36
#include <kurldrag.h>
37
#include <kconfig.h>
Alexander Dymo's avatar
Alexander Dymo committed
38 39 40 41 42 43

#include <ddockwindow.h>
#include <dtabwidget.h>
#include <profile.h>
#include <profileengine.h>
#include <designer.h>
44
#include <kdevproject.h>
45
#include <urlutil.h>
Alexander Dymo's avatar
Alexander Dymo committed
46

47
#include "api.h"
Alexander Dymo's avatar
Alexander Dymo committed
48 49 50 51 52 53 54 55 56 57
#include "core.h"
#include "plugincontroller.h"
#include "mainwindowshare.h"
#include "shellextension.h"
#include "partcontroller.h"
#include "statusbar.h"
#include "documentationpart.h"
#include "toplevel.h"
#include "projectmanager.h"
#include "editorproxy.h"
Jens Dagerbo's avatar
Jens Dagerbo committed
58
#include "multibuffer.h"
Alexander Dymo's avatar
Alexander Dymo committed
59 60 61 62 63 64

SimpleMainWindow::SimpleMainWindow(QWidget* parent, const char *name)
    :DMainWindow(parent, name)
{
    resize(800, 600); // starts kdevelop at 800x600 the first time
    m_mainWindowShare = new MainWindowShare(this);
65 66 67 68

    connect(m_bottomDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
    connect(m_leftDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
    connect(m_rightDock, SIGNAL(hidden()), this, SLOT(raiseEditor()));
Alexander Dymo's avatar
Alexander Dymo committed
69 70 71 72 73 74 75 76 77 78 79 80
}

SimpleMainWindow::~ SimpleMainWindow( )
{
    TopLevel::invalidateInstance( this );
}

void SimpleMainWindow::init()
{
    actionCollection()->setHighlightingEnabled( true );
    setStandardToolBarMenuEnabled( true );
    setXMLFile(ShellExtension::getInstance()->xmlFile());
81

Alexander Dymo's avatar
Alexander Dymo committed
82 83 84 85
    createFramework();
    createActions();
    new KDevStatusBar(this);

86 87
    setAcceptDrops(true);

Alexander Dymo's avatar
Alexander Dymo committed
88
    createGUI(0);
89

Alexander Dymo's avatar
Alexander Dymo committed
90
    m_mainWindowShare->init();
91
    setupWindowMenu();
Alexander Dymo's avatar
Alexander Dymo committed
92 93 94 95
    menuBar()->setEnabled( false );

    //FIXME: this checks only for global offers which is not quite correct because
    //a profile can offer core plugins and no global plugins.
96
    if ( PluginController::getInstance()->engine().allOffers(ProfileEngine::Global).isEmpty() )
Alexander Dymo's avatar
Alexander Dymo committed
97 98
    {
        KMessageBox::sorry( this, i18n("Unable to find plugins, KDevelop will not work"
Andrew Coles's avatar
 
Andrew Coles committed
99 100
            " properly.\nPlease make sure "
            "that KDevelop is installed in your KDE directory; otherwise, you have "
Alexander Dymo's avatar
Alexander Dymo committed
101
            "to add KDevelop's installation "
102
            "path to the environment variable KDEDIRS and run kbuildsycoca. Restart "
Alexander Dymo's avatar
Alexander Dymo committed
103 104 105 106 107 108 109
            "KDevelop afterwards.\n"
            "Example for BASH users:\nexport KDEDIRS=/path/to/kdevelop:$KDEDIRS && kbuildsycoca"),
            i18n("Could Not Find Plugins") );
    }

    connect(Core::getInstance(), SIGNAL(coreInitialized()), this, SLOT(slotCoreInitialized()));
    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 *)));
Alexander Dymo's avatar
Alexander Dymo committed
113 114
    connect(PartController::getInstance(), SIGNAL(partURLChanged(KParts::ReadOnlyPart *)),
        this, SLOT(slotPartURLChanged(KParts::ReadOnlyPart * )));
115 116
    connect(PartController::getInstance(), SIGNAL(activePartChanged(KParts::Part*)),
        this, SLOT(activePartChanged(KParts::Part*)));
Alexander Dymo's avatar
Alexander Dymo committed
117

118
    connect(PartController::getInstance(),
Alexander Dymo's avatar
Alexander Dymo committed
119 120 121
        SIGNAL(documentChangedState(const KURL &, DocumentState)),
        this, SLOT(documentChangedState(const KURL&, DocumentState)));

122
    loadSettings();
Alexander Dymo's avatar
Alexander Dymo committed
123 124
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
void SimpleMainWindow::dragEnterEvent( QDragEnterEvent *event )
{
    event->accept(KURLDrag::canDecode(event));
}

void SimpleMainWindow::dropEvent( QDropEvent *event )
{
    slotDropEvent(event);
}

void SimpleMainWindow::slotDropEvent( QDropEvent *event )
{
    KURL::List list;
    if ( !KURLDrag::decode( event, list ) ) return;

    KURL::List::Iterator it = list.begin();
    while(  it != list.end() )
    {
        kdDebug(9000) << "drop url:" << *it << endl;
        PartController::getInstance()->editDocument( *it );
        ++it;
    }
}

149 150 151 152 153 154
void SimpleMainWindow::contextMenu(QPopupMenu *popupMenu, const Context *context)
{
    int cont = context->type();
    m_splitURLs.clear();
    if (cont == Context::EditorContext)
    {
155 156 157 158 159 160 161 162 163
        KURL url = static_cast<const EditorContext*>(context)->url();
        QWidget *w = widgetForURL(url);
        if (w && m_widgetTabs[w] && m_widgetTabs[w]->count() > 1)
        {
            m_splitURLs.append(url);
            m_splitHor1->plug(popupMenu);
            m_splitVer1->plug(popupMenu);
            popupMenu->insertSeparator();
        }
164 165 166
    }
    else if (cont == Context::FileContext)
    {
167 168
        if (PartController::getInstance()->openURLs().count() > 0)
        {
169 170 171 172 173 174 175 176 177 178 179 180 181
            KURL::List urls = static_cast<const FileContext*>(context)->urls();
            KURL::List::ConstIterator it = urls.begin();
            while ( it != urls.end() )
            {
                if ( !URLUtil::isDirectory( *it ) )
                {
                    m_splitURLs.append( *it );
                }
                ++it;
            }

            if ( m_splitURLs.isEmpty() ) return;

182 183 184
            bool isOpen = true;
            for (KURL::List::const_iterator it = m_splitURLs.begin(); it != m_splitURLs.end(); ++it)
            {
185
                if (!PartController::getInstance()->openURLs().contains(*it) && (*it).isValid())
186 187 188 189 190
                {
                    isOpen = false;
                    break;
                }
            }
191 192

            if ( (isOpen && PartController::getInstance()->openURLs().count() == 1) ||
193 194
                 (m_splitURLs.count() == 1 && !(*m_splitURLs.begin()).isValid() ) )
            {
195
                return;
196
            }
197

198
            popupMenu->insertSeparator();
199 200 201 202 203 204 205 206 207 208 209
            if (isOpen)
            {
                m_splitHor1->plug(popupMenu);
                m_splitVer1->plug(popupMenu);
            }
            else
            {
                m_splitHor2->plug(popupMenu);
                m_splitVer2->plug(popupMenu);
            }
        }
210 211 212
    }
}

Alexander Dymo's avatar
Alexander Dymo committed
213 214
void SimpleMainWindow::embedPartView(QWidget *view, const QString &title, const QString &/*toolTip*/)
{
215
    kdDebug() << "SimpleMainWindow::embedPartView: " << view << endl;
Alexander Dymo's avatar
Alexander Dymo committed
216 217 218 219 220 221 222 223 224 225 226 227
    if (!view )
        return;

    QString shortName = title;
    shortName = shortName.right( shortName.length() - (shortName.findRev('/') +1));

    addWidget(view, title);
    view->show();
}

void SimpleMainWindow::embedSelectView(QWidget *view, const QString &title, const QString &/*toolTip*/)
{
228
    embedView( DDockWindow::Left, view, title );
Alexander Dymo's avatar
Alexander Dymo committed
229 230 231 232
}

void SimpleMainWindow::embedOutputView(QWidget *view, const QString &title, const QString &/*toolTip*/)
{
233
    embedView( DDockWindow::Bottom, view, title );
Alexander Dymo's avatar
Alexander Dymo committed
234 235 236 237
}

void SimpleMainWindow::embedSelectViewRight(QWidget *view, const QString &title, const QString &/*toolTip*/)
{
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    embedView( DDockWindow::Right, view, title );
}

void SimpleMainWindow::embedView( DDockWindow::Position pos, QWidget * view, const QString & title )
{
    if ( !hasDockWidget( view ) )
    {
        DDockWindow::Position position = recallToolViewPosition( view->name(), pos );
        addDockWidget(position, view, title);
    }
}

DDockWindow::Position SimpleMainWindow::recallToolViewPosition( const QString & name, DDockWindow::Position defaultPos )
{
    KConfig * config = kapp->config();
    config->setGroup( "DToolDockPosition" );

    QString position = config->readEntry( name );

    if ( name == "unknown" )
    {
        kdDebug() << name << endl;
    }

    if ( position == "DockLeft" ) return DDockWindow::Left;
    if ( position == "DockRight" ) return DDockWindow::Right;
    if ( position == "DockBottom" ) return DDockWindow::Bottom;

    return defaultPos;
}

void SimpleMainWindow::rememberToolViewPosition( const QString & name, DDockWindow::Position pos )
{
    KConfig * config = kapp->config();
    config->setGroup( "DToolDockPosition" );

    QString position = "unknown";

    switch( pos )
    {
        case DDockWindow::Left:
            position = "DockLeft";
            break;
        case DDockWindow::Right:
            position = "DockRight";
            break;
        case DDockWindow::Bottom:
            position = "DockBottom";
            break;
        default: ;
    }

    config->writeEntry( name, position );
Alexander Dymo's avatar
Alexander Dymo committed
291 292
}

293

Alexander Dymo's avatar
Alexander Dymo committed
294 295 296 297 298 299 300
void SimpleMainWindow::removeView(QWidget *view)
{
    if (!view)
        return;

    //try to remove it from all parts of main window
    //@fixme This method needs to be divided in two - one for docks and one for part views
301 302 303 304 305
    if (hasDockWidget(view))
    {
        rememberToolViewPosition( view->name(), dockWidgetPosition(view) );
        removeDockWidget(view);
    }
Alexander Dymo's avatar
Alexander Dymo committed
306 307 308 309 310 311 312
    else
        removeWidget(view);
}

void SimpleMainWindow::setViewAvailable(QWidget *pView, bool bEnabled)
{
    DDockWindow *dock;
313 314
    if (hasDockWidget(pView))
        dock = toolWindow(dockWidgetPosition(pView));
Alexander Dymo's avatar
Alexander Dymo committed
315 316 317 318 319 320 321 322
    else
        return;

    bEnabled ? dock->showWidget(pView) : dock->hideWidget(pView);
}

void SimpleMainWindow::raiseView(QWidget *view)
{
323 324 325 326
    //adymo: a workaround to make editor wrappers work:
    //editor view is passed to this function but the ui library knows only
    //of its parent which is an editor wrapper, simply replacing the view
    //by its wrapper helps here
Alexander Dymo's avatar
Alexander Dymo committed
327 328 329
    if (view->parent())
        kdDebug() << view->parent()->className() << endl;
    if (view->parent() && (view->parent()->isA("EditorWrapper") || view->parent()->isA("MultiBuffer")))
330
    {
331
//         kdDebug() << "parent is editor wrapper: " <<
332 333 334
//             static_cast<EditorWrapper*>(view->parent()) << endl;
        view = (QWidget*)view->parent();
    }
335

336
    if (hasDockWidget(view))
337
    {
338
        DDockWindow *dock = toolWindow(dockWidgetPosition(view));
339 340
        dock->raiseWidget(view);
    }
341 342
    else if (m_widgets.contains(view) && m_widgetTabs.contains(view))
        m_widgetTabs[view]->showPage(view);
Alexander Dymo's avatar
Alexander Dymo committed
343 344
}

345
void SimpleMainWindow::lowerView(QWidget * view)
Alexander Dymo's avatar
Alexander Dymo committed
346
{
347 348 349 350 351
    if (!hasDockWidget(view))
        return;

    DDockWindow *dock = toolWindow(dockWidgetPosition(view));
    dock->lowerWidget(view);
Alexander Dymo's avatar
Alexander Dymo committed
352 353 354 355 356 357 358 359
}

void SimpleMainWindow::loadSettings()
{
    KConfig *config = kapp->config();

    ProjectManager::getInstance()->loadSettings();
    applyMainWindowSettings(config, "SimpleMainWindow");
360 361

    Core::setupShourtcutTips();
Alexander Dymo's avatar
Alexander Dymo committed
362 363 364 365 366 367 368 369
}

void SimpleMainWindow::saveSettings( )
{
    KConfig *config = kapp->config();

    ProjectManager::getInstance()->saveSettings();
    saveMainWindowSettings(config, "SimpleMainWindow");
370

371 372 373
    QMap<QWidget*, DDockWindow::Position>::iterator it = m_docks.begin();
    while ( it != m_docks.end() )
    {
Stephan Binner's avatar
Stephan Binner committed
374
        if ( !strcmp(it.key()->name(), "unnamed") )
375 376 377 378
        {
            kdError() << "Toolviews must have a name : " << it.key() << endl;
        }

379 380 381 382
        rememberToolViewPosition( it.key()->name(), it.data() );
        ++it;
    }

383
    DMainWindow::saveSettings();
Alexander Dymo's avatar
Alexander Dymo committed
384 385
}

386
void SimpleMainWindow::setCurrentDocumentCaption( const QString &caption )
387
{
388 389 390 391 392 393
    if( !PartController::getInstance()->activePart() ) return;

    if (QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(PartController::getInstance()->activePart()))
    {
        widget->setCaption(caption);
    }
394
}
395

Alexander Dymo's avatar
Alexander Dymo committed
396 397 398 399 400 401 402 403 404
KMainWindow *SimpleMainWindow::main()
{
    return this;
}

void SimpleMainWindow::createFramework()
{
    PartController::createInstance( this );

405
    connect(PartController::getInstance(), SIGNAL(activePartChanged(KParts::Part*)),
Alexander Dymo's avatar
Alexander Dymo committed
406 407 408 409 410 411 412 413 414
        this, SLOT(createGUI(KParts::Part*)));
}

void SimpleMainWindow::createActions()
{
    m_raiseEditor = new KAction(i18n("Raise &Editor"), ALT+Key_C,
        this, SLOT(raiseEditor()), actionCollection(), "raise_editor");
    m_raiseEditor->setToolTip(i18n("Raise editor"));
    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 418
        this, SLOT(lowerAllDocks()), actionCollection(), "lower_all_docks");

419
    new KAction(i18n("Switch to next TabWidget"), 0, this,
420 421
        SLOT(switchToNextTabWidget()), actionCollection(), "switch_to_next_tabwidget" );

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");
Alexander Dymo's avatar
Alexander Dymo committed
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 431
    m_splitHor1 = new KAction(i18n("Split &Horizontal"), 0,
        this, SLOT(slotSplitHorizontal()), actionCollection(), "split_h1");
432
    m_splitHor1->setIcon("view_top_bottom");
433 434 435

    m_splitVer1 = new KAction(i18n("Split &Vertical"), 0,
        this, SLOT(slotSplitVertical()), actionCollection(), "split_v1");
436
    m_splitVer1->setIcon("view_left_right");
437 438 439

    m_splitHor2 = new KAction(i18n("Split &Horizontal and Open"), 0,
        this, SLOT(slotSplitHorizontal()), actionCollection(), "split_h2");
440
    m_splitHor2->setIcon("view_top_bottom");
441 442 443

    m_splitVer2 = new KAction(i18n("Split &Vertical and Open"), 0,
        this, SLOT(slotSplitVertical()), actionCollection(), "split_v2");
444
    m_splitVer2->setIcon("view_left_right");
445

446 447 448 449 450 451 452 453 454
    m_raiseLeftDock = new KAction(i18n("Switch Left Dock"), CTRL+SHIFT+ALT+Key_L,
        this, SLOT(raiseLeftDock()), actionCollection(), "switch_left_dock");

    m_raiseRightDock = new KAction(i18n("Switch Right Dock"), CTRL+SHIFT+ALT+Key_R,
        this, SLOT(raiseRightDock()), actionCollection(), "switch_right_dock");

    m_raiseBottomDock = new KAction(i18n("Switch Bottom Dock"), CTRL+SHIFT+ALT+Key_B,
        this, SLOT(raiseBottomDock()), actionCollection(), "switch_bottom_dock");

455
    KStdAction::configureToolbars(this, SLOT(configureToolbars()),
Alexander Dymo's avatar
Alexander Dymo committed
456
        actionCollection(), "set_configure_toolbars");
457

Alexander Dymo's avatar
Alexander Dymo committed
458 459 460 461 462 463 464 465
    m_mainWindowShare->createActions();

    connect(m_mainWindowShare, SIGNAL(gotoNextWindow()), this, SLOT(gotoNextWindow()));
    connect(m_mainWindowShare, SIGNAL(gotoPreviousWindow()), this, SLOT(gotoPreviousWindow()));
    connect(m_mainWindowShare, SIGNAL(gotoFirstWindow()), this, SLOT(gotoFirstWindow()));
    connect(m_mainWindowShare, SIGNAL(gotoLastWindow()), this, SLOT(gotoLastWindow()));
}

466 467
void SimpleMainWindow::raiseEditor()
{
468
    kdDebug() << "SimpleMainWindow::raiseEditor" << endl;
469 470 471 472 473
    KDevPartController *partcontroller = API::getInstance()->partController();
    if (partcontroller->activePart() && partcontroller->activePart()->widget())
        partcontroller->activePart()->widget()->setFocus();
}

Alexander Dymo's avatar
Alexander Dymo committed
474 475
void SimpleMainWindow::gotoNextWindow()
{
476 477 478 479 480 481
    if (m_bottomDock->isActive())
        m_bottomDock->selectNextWidget();
    else if (m_rightDock->isActive())
        m_rightDock->selectNextWidget();
    else if (m_leftDock->isActive())
        m_leftDock->selectNextWidget();
Alexander Dymo's avatar
Alexander Dymo committed
482
    else
483 484 485 486 487 488
    {
        if ((m_activeTabWidget->currentPageIndex() + 1) < m_activeTabWidget->count())
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->currentPageIndex() + 1);
        else
            m_activeTabWidget->setCurrentPage(0);
    }
Alexander Dymo's avatar
Alexander Dymo committed
489 490 491 492
}

void SimpleMainWindow::gotoPreviousWindow()
{
493 494 495 496 497 498
    if (m_bottomDock->isActive())
        m_bottomDock->selectPrevWidget();
    else if (m_rightDock->isActive())
        m_rightDock->selectPrevWidget();
    else if (m_leftDock->isActive())
        m_leftDock->selectPrevWidget();
Alexander Dymo's avatar
Alexander Dymo committed
499
    else
500 501 502 503 504 505
    {
        if ((m_activeTabWidget->currentPageIndex() - 1) >= 0)
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->currentPageIndex() - 1);
        else
            m_activeTabWidget->setCurrentPage(m_activeTabWidget->count() - 1);
    }
Alexander Dymo's avatar
Alexander Dymo committed
506 507 508 509
}

void SimpleMainWindow::gotoFirstWindow()
{
510
    //@TODO: implement me
Alexander Dymo's avatar
Alexander Dymo committed
511 512 513 514
}

void SimpleMainWindow::gotoLastWindow()
{
515 516

    //@TODO: implement me
Alexander Dymo's avatar
Alexander Dymo committed
517 518 519 520
}

void SimpleMainWindow::slotCoreInitialized()
{
521
    menuBar()->setEnabled(true);
Alexander Dymo's avatar
Alexander Dymo committed
522 523 524 525 526 527 528
}

void SimpleMainWindow::projectOpened()
{
    setCaption(QString::null);
}

529
void SimpleMainWindow::slotPartURLChanged(KParts::ReadOnlyPart *part)
Alexander Dymo's avatar
Alexander Dymo committed
530
{
531
    if (QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(part))
532 533
    {
        kdDebug() << "new caption for widget: " << part->url().fileName() << endl;
534
        widget->setCaption(part->url().fileName());
535
    }
Alexander Dymo's avatar
Alexander Dymo committed
536 537 538 539 540 541 542
        //do smth with caption: ro_part->url().fileName()
}

void SimpleMainWindow::documentChangedState(const KURL &url, DocumentState state)
{
    QWidget * widget = EditorProxy::getInstance()->topWidgetForPart(
        PartController::getInstance()->partForURL(url));
543
    kdDebug() << "SimpleMainWindow::documentChangedState: " << widget << endl;
Alexander Dymo's avatar
Alexander Dymo committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    if (widget)
    {
        //calculate the icon size if showTabIcons is false
        //this is necessary to avoid tab resizing by setIcon() call
        int isize = 16;
        if (m_activeTabWidget && !m_showIconsOnTabs)
        {
            isize = m_activeTabWidget->fontMetrics().height() - 1;
            isize = isize > 16 ? 16 : isize;
        }
        switch (state)
        {
            // we should probably restore the original icon instead of just using "kdevelop",
            // but I have never seen any other icon in use so this should do for now
            case Clean:
                if (m_showIconsOnTabs)
                    widget->setIcon(SmallIcon("kdevelop", isize));
                else
                    widget->setIcon(QPixmap());
                break;
            case Modified:
                widget->setIcon(SmallIcon("filesave", isize));
                break;
            case Dirty:
                widget->setIcon(SmallIcon("revert", isize));
                break;
            case DirtyAndModified:
                widget->setIcon(SmallIcon("stop", isize));
                break;
        }
    }
575
    setCaption(url.url());
Alexander Dymo's avatar
Alexander Dymo committed
576 577 578 579
}

void SimpleMainWindow::closeTab()
{
580 581 582
//    actionCollection()->action("file_close")->activate();
    if (sender()->isA("QToolButton") && sender()->parent()->isA("DTabWidget"))
    {
583 584 585
        DTabWidget *tab = (DTabWidget*)sender()->parent();
        if (tab && tab->currentPage())
            closeTab(tab->currentPage());
586
    }
Alexander Dymo's avatar
Alexander Dymo committed
587 588 589 590 591 592 593
}

void SimpleMainWindow::tabContext(QWidget *w, const QPoint &p)
{
    DTabWidget *tabWidget = static_cast<DTabWidget*>(const_cast<QObject*>(sender()));
    if (!tabWidget)
        return;
594

Alexander Dymo's avatar
Alexander Dymo committed
595 596 597 598 599 600 601 602 603
    KPopupMenu tabMenu;
    tabMenu.insertTitle(tabWidget->tabLabel(w));

    //Find the document on whose tab the user clicked
    m_currentTabURL = QString::null;
    QPtrListIterator<KParts::Part> it(*PartController::getInstance()->parts());
    while (KParts::Part* part = it.current())
    {
        QWidget *top_widget = EditorProxy::getInstance()->topWidgetForPart(part);
604
        if (top_widget == w)
Alexander Dymo's avatar
Alexander Dymo committed
605 606 607 608 609 610 611 612 613
        {
            if (KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(part))
            {
                m_currentTabURL = ro_part->url();
                tabMenu.insertItem(i18n("Close"), 0);

                if (PartController::getInstance()->parts()->count() > 1)
                    tabMenu.insertItem(i18n("Close All Others"), 4);

614 615 616 617 618 619
                if( KParts::ReadWritePart * rw_part = dynamic_cast<KParts::ReadWritePart*>( ro_part ) )
                {
                    if( rw_part->isModified() ) tabMenu.insertItem( i18n("Save"),1);
                    tabMenu.insertItem( i18n("Reload"),2);
                }

620
                if (dynamic_cast<HTMLDocumentationPart*>(ro_part))
Alexander Dymo's avatar
Alexander Dymo committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
                {
                    tabMenu.insertItem(i18n("Duplicate"), 3);
                    break;
                }

                //Create the file context
                KURL::List list;
                list << m_currentTabURL;
                FileContext context( list );
                Core::getInstance()->fillContextMenu(&tabMenu, &context);
            }
            break;
        }
        ++it;
    }

    connect(&tabMenu, SIGNAL(activated(int)), this, SLOT(tabContextActivated(int)));
    tabMenu.exec(p);
}

void SimpleMainWindow::tabContextActivated(int id)
{
    if(m_currentTabURL.isEmpty())
            return;

    switch(id)
    {
        case 0:
            PartController::getInstance()->closeFile(m_currentTabURL);
            break;
        case 1:
            PartController::getInstance()->saveFile(m_currentTabURL);
            break;
        case 2:
            PartController::getInstance()->reloadFile(m_currentTabURL);
            break;
        case 3:
            PartController::getInstance()->showDocument(m_currentTabURL, true);
            break;
        case 4:
            PartController::getInstance()->closeAllOthers(m_currentTabURL);
            break;
        default:
            break;
    }
}

void SimpleMainWindow::configureToolbars()
{
    saveMainWindowSettings(KGlobal::config(), "SimpleMainWindow");
    KEditToolbar dlg(factory());
    connect(&dlg, SIGNAL(newToolbarConfig()), this, SLOT(slotNewToolbarConfig()));
    dlg.exec();
}

void SimpleMainWindow::slotNewToolbarConfig()
{
//    setupWindowMenu();
    m_mainWindowShare->slotGUICreated(PartController::getInstance()->activePart());
    applyMainWindowSettings(KGlobal::config(), "SimpleMainWindow");
}

683 684 685 686 687 688 689 690 691 692 693
bool SimpleMainWindow::queryClose()
{
    saveSettings();
    return Core::getInstance()->queryClose();
}

bool SimpleMainWindow::queryExit()
{
    return true;
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
void SimpleMainWindow::setupWindowMenu()
{
    // get the xmlgui created one instead
    m_windowMenu = static_cast<QPopupMenu*>(main()->child("window", "KPopupMenu"));

    if (!m_windowMenu)
    {
        kdDebug(9000) << "Couldn't find the XMLGUI window menu. Creating new." << endl;

        m_windowMenu = new QPopupMenu(main(), "window");
        menuBar()->insertItem(i18n("&Window"), m_windowMenu);
    }

    actionCollection()->action("file_close")->plug(m_windowMenu);
    actionCollection()->action("file_close_all")->plug(m_windowMenu);
    actionCollection()->action("file_closeother")->plug(m_windowMenu);

    QObject::connect(m_windowMenu, SIGNAL(activated(int)), this, SLOT(openURL(int)));
    QObject::connect(m_windowMenu, SIGNAL(aboutToShow()), this, SLOT(fillWindowMenu()));
}

void SimpleMainWindow::openURL(int w)
{
    QValueList<QPair<int, KURL> >::ConstIterator it = m_windowList.begin();
    while (it != m_windowList.end())
    {
        if ((*it).first == w)
        {
            KURL url((*it).second);
            if (!url.isEmpty())
            {
                PartController::getInstance()->editDocument(url);
                return;
            }
        }
        ++it;
    }
}

void SimpleMainWindow::fillWindowMenu()
{
    // clear menu
    QValueList< QPair< int, KURL > >::ConstIterator it = m_windowList.begin();
    while (it != m_windowList.end())
    {
        m_windowMenu->removeItem( (*it).first );
        ++it;
    }

    int temp = 0;

    QMap<QString, KURL> map;
    QStringList string_list;
    KURL::List list = PartController::getInstance()->openURLs();
    KURL::List::Iterator itt = list.begin();
    while (itt != list.end())
    {
        map[(*itt).fileName()] = *itt;
        string_list.append((*itt).fileName());
        ++itt;
    }
    string_list.sort();

    list.clear();
    for(uint i = 0; i != string_list.size(); ++i)
        list.append(map[string_list[i]]);

    itt = list.begin();
    int i = 0;

    if (list.count() > 0)
        m_windowList << qMakePair(m_windowMenu->insertSeparator(), KURL());
766

767 768 769 770 771 772 773 774 775
    while (itt != list.end())
    {
        temp = m_windowMenu->insertItem( i < 10 ? QString("&%1 %2").arg(i).arg((*itt).fileName()) : (*itt).fileName() );
        m_windowList << qMakePair(temp, *itt);
        ++i;
        ++itt;
    }
}

776 777 778
void SimpleMainWindow::slotSplitVertical()
{
    DTabWidget *tab = splitVertical();
779
    openDocumentsAfterSplit(tab);
780 781 782 783 784
}

void SimpleMainWindow::slotSplitHorizontal()
{
    DTabWidget *tab = splitHorizontal();
785 786 787
    openDocumentsAfterSplit(tab);
}

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
void SimpleMainWindow::slotSplitVerticalBase()
{
    if (KParts::ReadOnlyPart *ro_part = activePartForSplitting())
    {
        m_splitURLs << ro_part->url();
        slotSplitVertical();
    }
}

KParts::ReadOnlyPart *SimpleMainWindow::activePartForSplitting()
{
    if (PartController::getInstance()->openURLs().count() < 2)
        return 0;
    m_splitURLs.clear();
    KParts::ReadOnlyPart *ro_part =
        dynamic_cast<KParts::ReadOnlyPart*>(PartController::getInstance()->activePart());
    return ro_part;
}

void SimpleMainWindow::slotSplitHorizontalBase()
{
    if (KParts::ReadOnlyPart *ro_part = activePartForSplitting())
    {
        m_splitURLs << ro_part->url();
        slotSplitHorizontal();
    }
}

816 817 818 819
void SimpleMainWindow::openDocumentsAfterSplit(DTabWidget *tab)
{
    if (m_splitURLs.count() > 0)
    {
820 821 822 823 824 825 826
        for (KURL::List::const_iterator it = m_splitURLs.begin(); it != m_splitURLs.end(); ++it)
        {
            KParts::ReadOnlyPart *part = PartController::getInstance()->partForURL(*it);
            if (!part)
                PartController::getInstance()->editDocument(*it);
            else
            {
827
                PartController::getInstance()->activatePart( part );
828 829 830 831 832 833 834 835 836 837 838
                QWidget *inTab = widgetForURL(*it);
                if (inTab)
                {
                    DTabWidget *oldTab = m_widgetTabs[inTab];
                    QString title = oldTab->tabLabel(inTab);
                    removeWidget(inTab);
                    addWidget(tab, inTab, title);
                }
            }
        }
        m_splitURLs.clear();
839
    }
840 841
}

842 843 844
QWidget *SimpleMainWindow::widgetForURL(KURL url)
{
    KParts::ReadOnlyPart *part = PartController::getInstance()->partForURL(url);
Jens Dagerbo's avatar
Jens Dagerbo committed
845
    return widgetInTab( part ? part->widget() : 0 );
846 847 848 849
}

QWidget *SimpleMainWindow::widgetInTab(QWidget *w)
{
850
    QWidget *inTab = 0;
851 852 853 854 855 856 857
    if (w && w->parent() && w->parent()->isA("EditorProxy"))
        inTab = (QWidget*)w->parent();
    else if (w && w->parent() && w->parent()->isA("MultiBuffer")
        && w->parent()->parent() && w->parent()->parent()->isA("EditorProxy"))
        inTab = (QWidget*)w->parent()->parent();
    else if (w && w->parent() && w->parent()->isA("MultiBuffer"))
        inTab = (QWidget*)w->parent();
858
    else
859
        inTab = w;
860 861 862
    return inTab;
}

863 864 865 866 867 868 869 870 871
void SimpleMainWindow::closeTab(QWidget *w)
{
    const QPtrList<KParts::Part> *partlist = PartController::getInstance()->parts();
    QPtrListIterator<KParts::Part> it(*partlist);
    while (KParts::Part* part = it.current())
    {
        QWidget *widget = EditorProxy::getInstance()->topWidgetForPart(part);
        if (widget && widget == w)
        {
Jens Dagerbo's avatar
Jens Dagerbo committed
872 873 874 875 876 877 878 879 880 881
            // weirdness beyond weirdness.. sometimes the active view is an embedded splitter with two files
            // so we check if the widget is a multibuffer, in which case we let it decide what part to close
            if (MultiBuffer * mb = dynamic_cast<MultiBuffer*>( widget ) )
            {
                PartController::getInstance()->closePart( mb->activeBuffer() );
            }
            else
            {
                PartController::getInstance()->closePart(part);
            }
882 883 884 885 886 887 888 889 890 891 892 893
            return;
        }
        ++it;
    }
}

void SimpleMainWindow::activePartChanged(KParts::Part *part)
{
    if (!part)
        return;
    QWidget *w = part->widget();
    kdDebug() << "active part widget is : " << w << endl;
894 895
    QWidget *inTab = widgetInTab(w);
    if (m_widgetTabs[inTab] != 0)
896 897
    {
        kdDebug() << " setting m_activeTabWidget " << endl;
898
        m_activeTabWidget = m_widgetTabs[inTab];
899
    }
900 901
}

902 903 904 905 906 907 908 909
void SimpleMainWindow::createGUI(KParts::Part *part) {
    if ( !part )
        setCaption( QString::null );
    DMainWindow::createGUI(part);

    m_mainWindowShare->slotGUICreated( part );
}

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
void SimpleMainWindow::raiseBottomDock()
{
    raiseDock(m_bottomDock);
}

void SimpleMainWindow::raiseLeftDock()
{
    raiseDock(m_leftDock);
}

void SimpleMainWindow::raiseRightDock()
{
    raiseDock(m_rightDock);
}

void SimpleMainWindow::raiseDock(DDockWindow *dock)
{
    dock->selectLastWidget();
}

930 931 932 933 934 935 936
void SimpleMainWindow::lowerAllDocks()
{
    m_bottomDock->lowerWidget( m_bottomDock->currentWidget() );
    m_leftDock->lowerWidget( m_leftDock->currentWidget() );
    m_rightDock->lowerWidget( m_rightDock->currentWidget() );
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
void SimpleMainWindow::switchToNextTabWidget()
{
    QValueList<DTabWidget*> tabWidgets = m_widgetTabs.values();

    if ( tabWidgets.count() < 2 ) return;

    QValueList<DTabWidget*> reduced;
    QValueList<DTabWidget*>::iterator it = tabWidgets.begin();
    while ( it != tabWidgets.end() )
    {
        if ( !reduced.contains( *it ) )
        {
            reduced << *it;
        }
        ++it;
    }

    it = reduced.begin();
    while ( it != reduced.end() )
    {
        if ( *it == m_activeTabWidget )
        {
            if ( ++it != reduced.end() )
            {
                if ( (*it)->currentPage() )
                    (*it)->currentPage()->setFocus();
            }
            else
            {
                if ( (*reduced.begin())->currentPage() )
                    (*reduced.begin())->currentPage()->setFocus();
            }
            return;
        }
        ++it;
    }
}

975 976
void SimpleMainWindow::setCaption(const QString &caption)
{
977 978 979 980 981 982 983 984 985 986
    kdDebug(9000) << k_funcinfo << endl;

    bool modified = false;
    if ( !caption.isEmpty() )
    {
        KURL url( caption );
        DocumentState const state = PartController::getInstance()->documentState( url );
        modified = ( state == Modified || state == DirtyAndModified );
    }

987 988 989 990 991 992 993 994 995 996
    KDevProject *project = API::getInstance()->project();
    if (project)
    {
        QString projectname = project->projectName();

        QString suffix(".kdevelop");
        if (projectname.endsWith(suffix))
            projectname.truncate(projectname.length() - suffix.length());

        if (!caption.isEmpty())
997
            DMainWindow::setCaption(projectname + " - " + caption, modified);
998
        else
999
            DMainWindow::setCaption(projectname, modified);
1000 1001
    }
    else
1002
        DMainWindow::setCaption(caption, modified);
1003 1004 1005 1006 1007 1008 1009
}

void SimpleMainWindow::projectClosed()
{
    DMainWindow::setCaption(QString::null);
}

Alexander Dymo's avatar
Alexander Dymo committed
1010
#include "simplemainwindow.moc"
1011 1012

// kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on