ViewManager.cpp 40 KB
Newer Older
1
/*
2
    Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU 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 General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20 21 22
// Own
#include "ViewManager.h"

23 24
// System

25
// Qt
26
#include <QtCore/QDateTime>
Dirk Mueller's avatar
Dirk Mueller committed
27
#include <QtCore/QSignalMapper>
Jekyll Wu's avatar
Jekyll Wu committed
28
#include <QtCore/QStringList>
29
#include <QtGui/QMenu>
30

31
// KDE
32
#include <KDebug>
Robert Knight's avatar
Robert Knight committed
33
#include <KAcceleratorManager>
34
#include <KGlobal>
35 36
#include <KLocale>
#include <KToggleAction>
37
#include <KActionCollection>
38
#include <KXMLGUIFactory>
Laurent Montel's avatar
Laurent Montel committed
39
#include <KConfigGroup>
40 41

// Konsole
42
#include "ColorScheme.h"
43
#include "ProfileList.h"
44
#include "Session.h"
45
#include "TerminalDisplay.h"
46 47 48 49
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"
50 51
#include "konsoleadaptor.h"
#include "Profile.h"
52

53 54
using namespace Konsole;

55 56
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
57
    , _viewSplitter(0)
58 59
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
60
    , _navigationMethod(TabbedNavigation)
61
    , _newViewMenu(0)
62
{
63
    // create main view area
Robert Knight's avatar
Robert Knight committed
64
    _viewSplitter = new ViewSplitter(0);  
65
    KAcceleratorManager::setNoAccel(_viewSplitter);
Robert Knight's avatar
Robert Knight committed
66

67 68 69 70 71 72 73
    // the ViewSplitter class supports both recursive and non-recursive splitting,
    // in non-recursive mode, all containers are inserted into the same top-level splitter
    // widget, and all the divider lines between the containers have the same orientation
    //
    // the ViewManager class is not currently able to handle a ViewSplitter in recursive-splitting
    // mode 
    _viewSplitter->setRecursiveSplitting(false);
74
    _viewSplitter->setFocusPolicy(Qt::NoFocus);
75

76
    // setup actions which are related to the views
77 78 79 80 81
    setupActions();

    // emit a signal when all of the views held by this view manager are destroyed
    connect( _viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()) );
    connect( _viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()) );
82 83 84 85

    // listen for addition or removal of views from associated containers
    connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this , 
            SLOT(containerViewsChanged(QObject*)) ); 
86 87

    // listen for profile changes
88 89
    connect( SessionManager::instance() , SIGNAL(profileChanged(Profile::Ptr)) , this,
            SLOT(profileChanged(Profile::Ptr)) );
90 91
    connect( SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
            SLOT(updateViewsForSession(Session*)) );
92 93 94 95 96

    //prepare DBus communication
    new KonsoleAdaptor(this);
    QDBusConnection::sessionBus().registerObject(QLatin1String("/Konsole"), this);

97 98 99 100
}

ViewManager::~ViewManager()
{
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    delete _newViewMenu;
}
QMenu* ViewManager::createNewViewMenu() 
{
    if (_newViewMenu)
        return _newViewMenu;

    _newViewMenu = new QMenu(0);
    ProfileList* newViewProfiles = new ProfileList(false,_newViewMenu);
    newViewProfiles->syncWidgetActions(_newViewMenu,true);
    connect(newViewProfiles,SIGNAL(profileSelected(Profile::Ptr)),this,
        SIGNAL(newViewRequest(Profile::Ptr)));

    return _newViewMenu;
115
}
116 117 118 119 120 121 122 123 124 125 126 127
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
    if ( container )
    {
        return container->activeView();
    }
    else
    {
        return 0;
    }
}
128

129 130 131 132 133
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

134 135
void ViewManager::setupActions()
{
136
    KActionCollection* collection = _actionCollection;
137

Kurt Hindenburg's avatar
Kurt Hindenburg committed
138 139 140 141
    KAction* nextViewAction = new KAction( i18nc("@action Shortcut entry", "Next Tab") , this );
    KAction* previousViewAction = new KAction( i18nc("@action Shortcut entry", "Previous Tab") , this );
    KAction* lastViewAction = new KAction( i18nc("@action Shortcut entry", "Switch to Last Tab") , this);
    KAction* nextContainerAction = new KAction( i18nc("@action Shortcut entry", "Next View Container") , this);
142

Kurt Hindenburg's avatar
Kurt Hindenburg committed
143 144
    KAction* moveViewLeftAction = new KAction( i18nc("@action Shortcut entry", "Move Tab Left") , this );
    KAction* moveViewRightAction = new KAction( i18nc("@action Shortcut entry", "Move Tab Right") , this );
145

146 147 148 149 150
    // list of actions that should only be enabled when there are multiple view
    // containers open
    QList<QAction*> multiViewOnlyActions;
    multiViewOnlyActions << nextContainerAction;

151 152
    if ( collection )
    {
153
        KAction* splitLeftRightAction = new KAction( KIcon("view-split-left-right"),
Thomas Reitelbach's avatar
Thomas Reitelbach committed
154
                                                      i18nc("@action:inmenu", "Split View Left/Right"),
155
                                                      this );
156
        splitLeftRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::Key_ParenLeft) );
157 158 159
        collection->addAction("split-view-left-right",splitLeftRightAction);
        connect( splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()) );

160
        KAction* splitTopBottomAction = new KAction( KIcon("view-split-top-bottom") , 
Thomas Reitelbach's avatar
Thomas Reitelbach committed
161
                                             i18nc("@action:inmenu", "Split View Top/Bottom"),this);
162
        splitTopBottomAction->setShortcut( QKeySequence(Qt::CTRL+Qt::Key_ParenRight) );
163 164 165
        collection->addAction("split-view-top-bottom",splitTopBottomAction);
        connect( splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));

Thomas Reitelbach's avatar
Thomas Reitelbach committed
166
        KAction* closeActiveAction = new KAction( i18nc("@action:inmenu Close Active View", "Close Active") , this );
Pino Toscano's avatar
Pino Toscano committed
167
        closeActiveAction->setIcon(KIcon("view-close"));
168
        closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
169
        closeActiveAction->setEnabled(false);
170 171
        collection->addAction("close-active-view",closeActiveAction);
        connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
172 173

        multiViewOnlyActions << closeActiveAction;
174

Thomas Reitelbach's avatar
Thomas Reitelbach committed
175
        KAction* closeOtherAction = new KAction( i18nc("@action:inmenu Close Other Views", "Close Others") , this );
176
        closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
177
        closeOtherAction->setEnabled(false);
178 179
        collection->addAction("close-other-views",closeOtherAction);
        connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
180 181

        multiViewOnlyActions << closeOtherAction;
182

183
        KAction* detachViewAction = collection->addAction("detach-view");
184
        detachViewAction->setIcon(KIcon("tab-detach"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
185
        detachViewAction->setText(i18nc("@action:inmenu", "D&etach Current Tab"));
186 187
        // Ctrl+Shift+D is not used as a shortcut by default because it is too close
        // to Ctrl+D - which will terminate the session in many cases
188 189 190
        detachViewAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_H));

        connect( this , SIGNAL(splitViewToggle(bool)) , this , SLOT(updateDetachViewState()) );
191
        connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
192

193
        // Expand & Shrink Active View
Thomas Reitelbach's avatar
Thomas Reitelbach committed
194
        KAction* expandActiveAction = new KAction( i18nc("@action:inmenu", "Expand View") , this );
195
        expandActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketRight) );
196
        expandActiveAction->setEnabled(false);
197 198 199
        collection->addAction("expand-active-view",expandActiveAction);
        connect( expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()) );

200 201
        multiViewOnlyActions << expandActiveAction;

Thomas Reitelbach's avatar
Thomas Reitelbach committed
202
        KAction* shrinkActiveAction = new KAction( i18nc("@action:inmenu", "Shrink View") , this );
203
        shrinkActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketLeft) );
204
        shrinkActiveAction->setEnabled(false);
205 206 207
        collection->addAction("shrink-active-view",shrinkActiveAction);
        connect( shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()) );

208 209
        multiViewOnlyActions << shrinkActiveAction;

210
        // Next / Previous View , Next Container
211 212
        collection->addAction("next-view",nextViewAction);
        collection->addAction("previous-view",previousViewAction);
213
        collection->addAction("last-tab",lastViewAction);
214
        collection->addAction("next-container",nextContainerAction);
215 216
        collection->addAction("move-view-left",moveViewLeftAction);
        collection->addAction("move-view-right",moveViewRightAction);
217 218 219 220 221 222 223

        // Switch to tab N shortcuts
        const int SWITCH_TO_TAB_COUNT = 10;
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
        connect(switchToTabMapper,SIGNAL(mapped(int)),this,SLOT(switchToView(int)));
        for (int i=0;i < SWITCH_TO_TAB_COUNT;i++)
        {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
224
            KAction* switchToTabAction = new KAction(i18nc("@action Shortcut entry", "Switch to Tab %1",i+1),this);
225 226 227 228 229
            switchToTabMapper->setMapping(switchToTabAction,i);
            connect(switchToTabAction,SIGNAL(triggered()),switchToTabMapper,
                    SLOT(map()));
            collection->addAction(QString("switch-to-tab-%1").arg(i),switchToTabAction);
        }
230
    }
Robert Knight's avatar
 
Robert Knight committed
231

232 233 234 235 236 237
    QListIterator<QAction*> iter(multiViewOnlyActions);
    while ( iter.hasNext() )
    {
        connect( this , SIGNAL(splitViewToggle(bool)) , iter.next() , SLOT(setEnabled(bool)) );
    }

238
    // keyboard shortcut only actions
239
    nextViewAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
Robert Knight's avatar
 
Robert Knight committed
240
    connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
241
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
242

243
    previousViewAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Left) );
Robert Knight's avatar
 
Robert Knight committed
244
    connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
245
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
246 247 248

    nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
    connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
249
    _viewSplitter->addAction(nextContainerAction);
250 251 252 253

    moveViewLeftAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Left) );
    connect( moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()) );
    _viewSplitter->addAction(moveViewLeftAction);
254

255 256 257
    moveViewRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Right) );
    connect( moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()) );
    _viewSplitter->addAction(moveViewRightAction);
258 259 260

    connect( lastViewAction, SIGNAL(triggered()) , this , SLOT(lastView()));
    _viewSplitter->addAction(lastViewAction);
Robert Knight's avatar
 
Robert Knight committed
261
}
262 263 264 265 266 267 268 269 270 271
void ViewManager::switchToView(int index)
{
    Q_ASSERT(index >= 0);
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    QList<QWidget*> containerViews = container->views();
    if (index >= containerViews.count())
        return;
    container->setActiveView(containerViews.at(index));
}
272 273
void ViewManager::updateDetachViewState()
{
274 275
    if (!_actionCollection)
        return;
276

277

278 279
    bool splitView = _viewSplitter->containers().count() >= 2;
    bool shouldEnable = splitView || _viewSplitter->activeContainer()->views().count() >= 2;
280

281
    QAction* detachAction = _actionCollection->action("detach-view");
282

283 284
    if ( detachAction && shouldEnable != detachAction->isEnabled() )
        detachAction->setEnabled(shouldEnable);
285
}
286 287 288 289 290 291 292 293 294 295 296 297
void ViewManager::moveActiveViewLeft()
{
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewLeft );
}
void ViewManager::moveActiveViewRight()
{
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewRight );
}
Robert Knight's avatar
 
Robert Knight committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
void ViewManager::nextContainer()
{
    _viewSplitter->activateNextContainer();
}

void ViewManager::nextView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    container->activateNextView();
}

void ViewManager::previousView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    container->activatePreviousView();
319
}
320

321 322 323 324 325 326 327 328 329
void ViewManager::lastView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    container->activateLastView();
}

330 331 332 333 334
void ViewManager::detachActiveView()
{
    // find the currently active view and remove it from its container 
    ViewContainer* container = _viewSplitter->activeContainer();

335 336 337 338 339 340 341 342 343
    detachView(container, container->activeView());
}

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
    TerminalDisplay * viewToDetach = 
        dynamic_cast<TerminalDisplay*>(widgetView);

    if (!viewToDetach)
344 345
        return;

346
    emit viewDetached(_sessionMap[viewToDetach]);
347
    
348
    _sessionMap.remove(viewToDetach);
349 350

    // remove the view from this window
351 352
    container->removeView(viewToDetach);
    viewToDetach->deleteLater();
353 354 355 356 357 358 359

    // if the container from which the view was removed is now empty then it can be deleted,
    // unless it is the only container in the window, in which case it is left empty
    // so that there is always an active container
    if ( _viewSplitter->containers().count() > 1 && 
         container->views().count() == 0 )
    {
360
        removeContainer(container);
361 362 363 364
    }

}

365
void ViewManager::sessionFinished()
366
{
367 368 369 370
    // if this slot is called after the view manager's main widget
    // has been destroyed, do nothing
    if (!_viewSplitter)
        return;
371

372 373
    Session* session = qobject_cast<Session*>(sender());

374 375
    // We're using setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab)
    // so no need to manually select next tab.
376

377 378
    Q_ASSERT(session);

379
    // close attached views
380
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
381

382
    foreach ( TerminalDisplay* view , children )
383 384 385 386
    {
        if ( _sessionMap[view] == session )
        {
            _sessionMap.remove(view);
387
            view->deleteLater();
388
        }
Robert Knight's avatar
 
Robert Knight committed
389
    }
390

391 392 393 394
    // This is needed to remove this controller from factory() in 
    // order to prevent BUG: 185466 - disappearing menu popup
    if (_pluggedController)
        emit unplugController(_pluggedController);
Robert Knight's avatar
 
Robert Knight committed
395 396 397 398
}

void ViewManager::focusActiveView()
{
Robert Knight's avatar
Robert Knight committed
399 400 401 402 403 404
    // give the active view in a container the focus.  this ensures 
    // that controller associated with that view is activated and the session-specific
    // menu items are replaced with the ones for the newly focused view

    // see the viewFocused() method

Robert Knight's avatar
 
Robert Knight committed
405 406 407 408 409 410 411 412 413
    ViewContainer* container = _viewSplitter->activeContainer(); 
    if ( container )
    {
        QWidget* activeView = container->activeView();
        if ( activeView )
        {
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
414 415
}

416

417 418
void ViewManager::viewActivated( QWidget* view )
{
419 420 421 422 423 424
    Q_ASSERT( view != 0 );

    // focus the activated view, this will cause the SessionController
    // to notify the world that the view has been focused and the appropriate UI
    // actions will be plugged in.
    view->setFocus(Qt::OtherFocusReason);
425 426
}

427
void ViewManager::splitLeftRight()
428
{
429
    splitView(Qt::Horizontal);
430
}
431
void ViewManager::splitTopBottom()
432
{
433
    splitView(Qt::Vertical);
434 435
}

436
void ViewManager::splitView(Qt::Orientation orientation)
437
{
438 439 440
    // iterate over each session which has a view in the current active
    // container and create a new view for that session in a new container 
    QListIterator<QWidget*> existingViewIter(_viewSplitter->activeContainer()->views());
441

442
    ViewContainer* container = 0; 
443 444

    while (existingViewIter.hasNext())
445
    {
446
        Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
447
        TerminalDisplay* display = createTerminalDisplay(session);
Jekyll Wu's avatar
Jekyll Wu committed
448 449
        const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
        applyProfileToView(display, profile);
450
        ViewProperties* properties = createController(session,display);
451

452
        _sessionMap[display] = session;
453

454 455 456
        // create a container using settings from the first 
        // session in the previous container
        if ( !container )
457
        {
Jekyll Wu's avatar
Jekyll Wu committed
458 459
            container = createContainer(profile);
            applyProfileToContainer(container, profile);
460
        }
461

462 463 464
        container->addView(display,properties);
        session->addView( display );
    }
465

466 467
    _viewSplitter->addContainer(container,orientation);
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
468

469 470
    // focus the new container
    container->containerWidget()->setFocus();
471 472

    // ensure that the active view is focused after the split / unsplit
473 474 475 476 477
    ViewContainer* activeContainer = _viewSplitter->activeContainer();
    QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;

    if ( activeView )
        activeView->setFocus(Qt::OtherFocusReason);
478
}
479
void ViewManager::removeContainer(ViewContainer* container)
480
{
481 482 483 484 485 486 487 488
    // remove session map entries for views in this container
    foreach( QWidget* view , container->views() )
    {
        TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
        Q_ASSERT(display);
        _sessionMap.remove(display);
    } 

489
    _viewSplitter->removeContainer(container);
490
    container->deleteLater();
491 492

    emit splitViewToggle( _viewSplitter->containers().count() > 1 );
493
}
494 495 496 497 498 499 500 501
void ViewManager::expandActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),10);
}
void ViewManager::shrinkActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),-10);
}
502 503 504 505 506 507 508 509
void ViewManager::closeActiveView()
{
    // only do something if there is more than one container active
    if ( _viewSplitter->containers().count() > 1 )
    {
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
510

511 512 513 514
        // focus next container so that user can continue typing 
        // without having to manually focus it themselves
        nextContainer();
    }
515 516 517 518 519 520 521 522 523 524
}
void ViewManager::closeOtherViews()
{
    ViewContainer* active = _viewSplitter->activeContainer();

    QListIterator<ViewContainer*> iter(_viewSplitter->containers());
    while ( iter.hasNext() )
    {
        ViewContainer* next = iter.next();
        if ( next != active )
525
            removeContainer(next);
526 527 528
    }
}

529
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
530
{
Robert Knight's avatar
Robert Knight committed
531 532
    // create a new controller for the session, and ensure that this view manager
    // is notified when the view gains the focus
533
    SessionController* controller = new SessionController(session,view,this);
534
    connect( controller , SIGNAL(focused(SessionController*)) , this , SLOT(controllerChanged(SessionController*)) );
535
    connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
536 537
    connect( session , SIGNAL(primaryScreenInUse(bool)) ,
             controller , SLOT(setupPrimaryScreenSpecificActions(bool)) );
538 539
    connect( session , SIGNAL(selectedText(QString)) ,
             controller , SLOT(updateCopyAction(QString)) );
540
    connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
541

542 543 544
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
545

546 547 548
    return controller;
}

549 550
void ViewManager::controllerChanged(SessionController* controller)
{
551 552
    if ( controller == _pluggedController )
        return;
553

554
    _viewSplitter->setFocusProxy(controller->view());
555

556 557
    _pluggedController = controller;
    emit activeViewChanged(controller);
558 559 560 561
}

SessionController* ViewManager::activeViewController() const
{
562
    return _pluggedController;
563
}
564 565 566 567 568 569

IncrementalSearchBar* ViewManager::searchBar() const
{
    return _viewSplitter->activeSplitter()->activeContainer()->searchBar();
}

570 571
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
572
    // notify this view manager when the session finishes so that its view
573
    // can be deleted
574 575 576
    //
    // TODO - Find a more efficient a way to avoid multiple connections
    disconnect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
577 578 579
    connect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );

     TerminalDisplay* display = createTerminalDisplay(session);
580 581 582 583 584 585
     const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
     applyProfileToView(display, profile);

     bool isFirst = _sessionMap.isEmpty();
     if ( isFirst)
         applyProfileToContainer(container, profile);
586

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
     // set initial size
     display->setSize(80,40);

     ViewProperties* properties = createController(session,display);

     _sessionMap[display] = session; 
     container->addView(display,properties,index);
     session->addView(display);

     // tell the session whether it has a light or dark background
     session->setDarkBackground( colorSchemeForProfile(profile)->hasDarkBackground() );

     if ( container == _viewSplitter->activeContainer() ) 
     {
         container->setActiveView(display);
         display->setFocus( Qt::OtherFocusReason );
     }
604

605
     updateDetachViewState();
606
}
607

608
void ViewManager::createView(Session* session)
609
{
610 611 612
    // create the default container
    if (_viewSplitter->containers().count() == 0)
    {
613
        _viewSplitter->addContainer( createContainer(SessionManager::instance()->sessionProfile(session)) , 
614
                                     Qt::Vertical );
615
        emit splitViewToggle(false);
616 617
    }

618
    // new tab will be put at the end by default.
619 620
    int index = -1;

621 622 623 624 625
    // TODO: currently, whether new tab should be put after current tab is a per
    // profile setting, while in concept it should be per ViewManager or global
    // setting. The current implementation is limited by the design of Profile.h
    // It should be re-implmented at some appropriate time in the future.
    // comment by jekyllwu
Jekyll Wu's avatar
Jekyll Wu committed
626 627
    Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    int newTabBehavior = profile->property<int>(Profile::NewTabBehavior);
628
    if ( newTabBehavior == Profile::PutNewTabAfterCurrentTab )
629
    {
630 631 632 633 634 635
        QWidget* view = activeView();
        if (view)
        {
            QList<QWidget*> views =  _viewSplitter->activeContainer()->views();
            index = views.indexOf(view) + 1;
        }
636 637
    }

Robert Knight's avatar
Robert Knight committed
638 639 640
    // iterate over the view containers owned by this view manager
    // and create a new terminal display for the session in each of them, along with
    // a controller for the session/display pair 
641 642 643 644
    QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());
    while ( containerIter.hasNext() )
    {
        ViewContainer* container = containerIter.next();
645
        createView(session, container, index);
646 647 648
    }
}

Jekyll Wu's avatar
Jekyll Wu committed
649
ViewContainer* ViewManager::createContainer(const Profile::Ptr profile)
650
{
Jekyll Wu's avatar
Jekyll Wu committed
651
    Q_ASSERT( profile );
652

Jekyll Wu's avatar
Jekyll Wu committed
653
    const int tabPosition = profile->property<int>(Profile::TabBarPosition);
654 655 656 657 658

    ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ?
                                                   ViewContainer::NavigationPositionTop :
                                                   ViewContainer::NavigationPositionBottom;

659 660 661 662 663
    ViewContainer* container = 0;

    switch ( _navigationMethod )
    {
        case TabbedNavigation:    
664 665 666 667 668
            {
                container =
                    new TabbedViewContainer(position,_viewSplitter);

                connect(container,
Laurent Montel's avatar
Laurent Montel committed
669
                    SIGNAL(detachTab(ViewContainer*,QWidget*)),
670
                    this,
Laurent Montel's avatar
Laurent Montel committed
671
                    SLOT(detachView(ViewContainer*,QWidget*))
672
                    );
673
                connect(container,
Laurent Montel's avatar
Laurent Montel committed
674
                    SIGNAL(closeTab(ViewContainer*,QWidget*)),
675
                    this,
Laurent Montel's avatar
Laurent Montel committed
676
                    SLOT(closeTabFromContainer(ViewContainer*,QWidget*)));
677

678
            }
679 680 681 682 683
            break;
        case NoNavigation:
        default:
            container = new StackedViewContainer(_viewSplitter);
    }
684

Jekyll Wu's avatar
Jekyll Wu committed
685
    applyProfileToContainer(container, profile);
686

687
    // connect signals and slots
688 689 690 691 692 693
    connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
           SLOT(map()) );
    connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
           SLOT(map()) ); 
    _containerSignalMapper->setMapping(container,container);

694
    connect( container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()) );
695
    connect( container, SIGNAL(moveViewRequest(int,int,bool&)), 
696
    this , SLOT(containerMoveViewRequest(int,int,bool&)) );
697
    connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
698
    connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
699
    connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
700

701
    return container;
702
}
703 704
void ViewManager::containerMoveViewRequest(int index, int id, bool& moved)
{
705 706
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
707

708 709
    if (!controller)
        return;
710

711
    createView(controller->session(),container,index);
712
    controller->session()->refresh();
713
    moved = true;
714
}
715 716 717 718 719 720 721 722
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

    if ( collection )
    {
723 724 725 726 727 728 729 730 731
        // FIXME: The following disables certain actions for the KPart that it
        // doesn't actually have a use for, to avoid polluting the action/shortcut
        // namespace of an application using the KPart (otherwise, a shortcut may
        // be in use twice, and the user gets to see an "ambiguous shortcut over-
        // load" error dialog). However, this approach sucks - it's the inverse of
        // what it should be. Rather than disabling actions not used by the KPart,
        // a method should be devised to only enable those that are used, perhaps
        // by using a separate action collection.

732
        const bool enable = (_navigationMethod != NoNavigation) ;
733 734 735
        QAction* action;

        action = collection->action( "next-view" );
736
        if ( action ) action->setEnabled( enable );
737 738

        action = collection->action( "previous-view" );
739
        if ( action ) action->setEnabled( enable );
740

741
        action = collection->action( "last-tab" );
742
        if ( action ) action->setEnabled( enable );
743

744
        action = collection->action( "split-view-left-right" );
745
        if ( action ) action->setEnabled( enable );
746 747

        action = collection->action( "split-view-top-bottom" );
748
        if ( action ) action->setEnabled( enable );
749 750

        action = collection->action( "rename-session" );
751
        if ( action ) action->setEnabled( enable );
752 753

        action = collection->action( "move-view-left" );
754
        if ( action ) action->setEnabled( enable );
755 756

        action = collection->action( "move-view-right" );
757
        if ( action ) action->setEnabled( enable );
758 759 760
    }
}

761 762
ViewManager::NavigationMethod ViewManager::navigationMethod() const { return _navigationMethod; }

763 764
void ViewManager::containerViewsChanged(QObject* container)
{
765
    if (_viewSplitter && container == _viewSplitter->activeContainer() )
766 767 768 769 770
    {
        emit viewPropertiesChanged( viewProperties() );
    } 
}

771 772
void ViewManager::viewCloseRequest(QWidget* view)
{
773 774
    //FIXME Check that this cast is actually legal
    TerminalDisplay* display = (TerminalDisplay*)view;
775

776 777
    Q_ASSERT(display);

778 779
    // 1. detach view from session
    // 2. if the session has no views left, close it
780
    Session* session = _sessionMap[ display ];
781
    _sessionMap.remove(display);
782 783
    if ( session )
    {
784
        display->deleteLater();
785

786
        if ( session->views().count() == 0 )
787
            session->close();
788
    }
789 790 791 792 793
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        focusActiveView();
        updateDetachViewState();
    }
794 795 796 797
    // The below causes the menus  to be messed up
    // Only happenss when using the tab bar close button
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
798 799
}

800
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
801
{
802 803
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
804

805
    return display;
806 807
}

Jekyll Wu's avatar
Jekyll Wu committed
808
const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr profile) const
809
{
810
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
Jekyll Wu's avatar
Jekyll Wu committed
811
                                            findColorScheme(profile->colorScheme());
812 813
    if ( !colorScheme )
       colorScheme = ColorSchemeManager::instance()->defaultColorScheme(); 
814 815
    Q_ASSERT( colorScheme );

816 817 818
    return colorScheme;
}

Jekyll Wu's avatar
Jekyll Wu committed
819
void ViewManager::applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile)
820
{
Jekyll Wu's avatar
Jekyll Wu committed
821
    Q_ASSERT( profile );
822

823
    // menu bar visibility
Jekyll Wu's avatar
Jekyll Wu committed
824
    emit setMenuBarVisibleRequest( profile->property<bool>(Profile::ShowMenuBar) );
825

Jekyll Wu's avatar
Jekyll Wu committed
826
    emit setSaveGeometryOnExitRequest( profile->property<bool>(Profile::SaveGeometryOnExit) );
827

828 829
    emit updateWindowIcon();

830
    // load color scheme
831
    ColorEntry table[TABLE_COLORS];
832
    const ColorScheme* colorScheme = colorSchemeForProfile(profile);
833 834
    colorScheme->getColorTable(table , view->randomSeed() );
    view->setColorTable(table);
835
    view->setOpacity(colorScheme->opacity());
836
    view->setWallpaper(colorScheme->wallpaper());
837

838
    // load font 
Jekyll Wu's avatar
Jekyll Wu committed
839 840 841
    view->setAntialias(profile->property<bool>(Profile::AntiAliasFonts));
    view->setBoldIntense(profile->property<bool>(Profile::BoldIntense));
    view->setVTFont(profile->font());
842 843

    // set scroll-bar position
Jekyll Wu's avatar
Jekyll Wu committed
844
    int scrollBarPosition = profile->property<int>(Profile::ScrollBarPosition);
845

846
    if ( scrollBarPosition == Profile::ScrollBarLeft )
847
       view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
848
    else if ( scrollBarPosition == Profile::ScrollBarRight )
849
       view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
850 851
    else if ( scrollBarPosition == Profile::ScrollBarHidden )
       view->setScrollBarPosition(TerminalDisplay::ScrollBarHidden);
852

853
    // show hint about termianl size after resizing
Jekyll Wu's avatar
Jekyll Wu committed
854
    view->setShowTerminalSizeHint(profile->property<bool>(Profile::ShowTerminalSizeHint));
855

856
    // terminal features
Jekyll Wu's avatar
Jekyll Wu committed
857
    bool blinkingCursor = profile->property<bool>(Profile::BlinkingCursorEnabled);
Jekyll Wu's avatar
Jekyll Wu committed
858
    view->setBlinkingCursorEnabled(blinkingCursor);
859

Jekyll Wu's avatar
Jekyll Wu committed
860
    bool blinkingText = profile->property<bool>(Profile::BlinkingTextEnabled);
861 862
    view->setBlinkingTextEnabled(blinkingText);

Jekyll Wu's avatar
Jekyll Wu committed
863
    int tripleClickMode = profile->property<int>(Profile::TripleClickMode);