ViewManager.cpp 37.4 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
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24
#include <QtCore/QSignalMapper>
Jekyll Wu's avatar
Jekyll Wu committed
25
#include <QtCore/QStringList>
26
#include <QtGui/QMenu>
Jekyll Wu's avatar
Jekyll Wu committed
27
#include <QtDBus/QtDBus>
28

29
// KDE
Robert Knight's avatar
Robert Knight committed
30
#include <KAcceleratorManager>
31
#include <KLocalizedString>
Jekyll Wu's avatar
Jekyll Wu committed
32
#include <KAction>
33
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
34
#include <KConfigGroup>
35 36

// Konsole
37 38
#include <konsoleadaptor.h>

39
#include "ColorScheme.h"
40
#include "ColorSchemeManager.h"
41
#include "ProfileList.h"
42
#include "Session.h"
43
#include "TerminalDisplay.h"
44 45
#include "SessionController.h"
#include "SessionManager.h"
46
#include "ProfileManager.h"
47 48
#include "ViewContainer.h"
#include "ViewSplitter.h"
49
#include "Profile.h"
50
#include "Enumeration.h"
51

52 53
using namespace Konsole;

54 55
int ViewManager::lastManagerId = 0;

56 57
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
58
    , _viewSplitter(0)
59 60
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
61
    , _newViewMenu(0)
62 63 64 65 66
    , _navigationMethod(TabbedNavigation)
    , _navigationVisibility(ViewContainer::AlwaysShowNavigation)
    , _navigationPosition(ViewContainer::NavigationPositionTop)
    , _showQuickButtons(false)
    , _newTabBehavior(PutNewTabAtTheEnd)
67
    , _navigationStyleSheet(QString())
68
    , _managerId(0)
69
{
70
    // create main view area
Kurt Hindenburg's avatar
Kurt Hindenburg committed
71
    _viewSplitter = new ViewSplitter(0);
72
    KAcceleratorManager::setNoAccel(_viewSplitter);
Robert Knight's avatar
Robert Knight committed
73

74 75 76 77 78
    // 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
Kurt Hindenburg's avatar
Kurt Hindenburg committed
79
    // mode
80
    _viewSplitter->setRecursiveSplitting(false);
81
    _viewSplitter->setFocusPolicy(Qt::NoFocus);
82

83
    // setup actions which are related to the views
84 85 86
    setupActions();

    // emit a signal when all of the views held by this view manager are destroyed
Kurt Hindenburg's avatar
Kurt Hindenburg committed
87 88
    connect(_viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()));
    connect(_viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()));
89 90

    // listen for addition or removal of views from associated containers
Kurt Hindenburg's avatar
Kurt Hindenburg committed
91 92
    connect(_containerSignalMapper , SIGNAL(mapped(QObject*)) , this ,
            SLOT(containerViewsChanged(QObject*)));
93 94

    // listen for profile changes
95
    connect(ProfileManager::instance() , SIGNAL(profileChanged(Profile::Ptr)) , this,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
96 97 98
            SLOT(profileChanged(Profile::Ptr)));
    connect(SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
            SLOT(updateViewsForSession(Session*)));
99 100 101 102 103

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

104 105
    _managerId = ++lastManagerId;
    QDBusConnection::sessionBus().registerObject(QLatin1String("/Windows/") + QString::number(_managerId), this);
106 107 108 109
}

ViewManager::~ViewManager()
{
110 111
    delete _newViewMenu;
}
112 113 114 115 116 117

int ViewManager::managerId() const
{
    return _managerId;
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
118
QMenu* ViewManager::createNewViewMenu()
119 120 121 122 123
{
    if (_newViewMenu)
        return _newViewMenu;

    _newViewMenu = new QMenu(0);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
124 125 126 127
    ProfileList* newViewProfiles = new ProfileList(false, _newViewMenu);
    newViewProfiles->syncWidgetActions(_newViewMenu, true);
    connect(newViewProfiles, SIGNAL(profileSelected(Profile::Ptr)), this,
            SIGNAL(newViewRequest(Profile::Ptr)));
128 129

    return _newViewMenu;
130
}
131 132 133
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
134
    if (container) {
135
        return container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
136
    } else {
137 138 139
        return 0;
    }
}
140

141 142 143 144 145
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

146 147
void ViewManager::setupActions()
{
148
    KActionCollection* collection = _actionCollection;
149

Kurt Hindenburg's avatar
Kurt Hindenburg committed
150 151 152 153
    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);
154

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

158 159 160 161 162
    // list of actions that should only be enabled when there are multiple view
    // containers open
    QList<QAction*> multiViewOnlyActions;
    multiViewOnlyActions << nextContainerAction;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    if (collection) {
        KAction* splitLeftRightAction = new KAction(KIcon("view-split-left-right"),
                i18nc("@action:inmenu", "Split View Left/Right"),
                this);
        splitLeftRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_ParenLeft));
        collection->addAction("split-view-left-right", splitLeftRightAction);
        connect(splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()));

        KAction* splitTopBottomAction = new KAction(KIcon("view-split-top-bottom") ,
                i18nc("@action:inmenu", "Split View Top/Bottom"), this);
        splitTopBottomAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_ParenRight));
        collection->addAction("split-view-top-bottom", splitTopBottomAction);
        connect(splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));

        KAction* closeActiveAction = new KAction(i18nc("@action:inmenu Close Active View", "Close Active") , this);
Pino Toscano's avatar
Pino Toscano committed
178
        closeActiveAction->setIcon(KIcon("view-close"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
179
        closeActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
180
        closeActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
181
        collection->addAction("close-active-view", closeActiveAction);
182
        connect(closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveContainer()));
183 184

        multiViewOnlyActions << closeActiveAction;
185

Kurt Hindenburg's avatar
Kurt Hindenburg committed
186 187
        KAction* closeOtherAction = new KAction(i18nc("@action:inmenu Close Other Views", "Close Others") , this);
        closeOtherAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O));
188
        closeOtherAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
189
        collection->addAction("close-other-views", closeOtherAction);
190
        connect(closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherContainers()));
191 192

        multiViewOnlyActions << closeOtherAction;
193

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

201 202
        multiViewOnlyActions << expandActiveAction;

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

209 210
        multiViewOnlyActions << shrinkActiveAction;

211 212 213 214 215 216 217 218 219 220
        KAction* detachViewAction = collection->addAction("detach-view");
        detachViewAction->setIcon(KIcon("tab-detach"));
        detachViewAction->setText(i18nc("@action:inmenu", "D&etach Current Tab"));
        // 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
        detachViewAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_H));

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

221
        // Next / Previous View , Next Container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
222 223 224 225 226 227
        collection->addAction("next-view", nextViewAction);
        collection->addAction("previous-view", previousViewAction);
        collection->addAction("last-tab", lastViewAction);
        collection->addAction("next-container", nextContainerAction);
        collection->addAction("move-view-left", moveViewLeftAction);
        collection->addAction("move-view-right", moveViewRightAction);
228 229 230 231

        // Switch to tab N shortcuts
        const int SWITCH_TO_TAB_COUNT = 10;
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
232 233 234 235 236
        connect(switchToTabMapper, SIGNAL(mapped(int)), this, SLOT(switchToView(int)));
        for (int i = 0; i < SWITCH_TO_TAB_COUNT; i++) {
            KAction* switchToTabAction = new KAction(i18nc("@action Shortcut entry", "Switch to Tab %1", i + 1), this);
            switchToTabMapper->setMapping(switchToTabAction, i);
            connect(switchToTabAction, SIGNAL(triggered()), switchToTabMapper,
237
                    SLOT(map()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
238
            collection->addAction(QString("switch-to-tab-%1").arg(i), switchToTabAction);
239
        }
240
    }
Robert Knight's avatar
 
Robert Knight committed
241

Kurt Hindenburg's avatar
Kurt Hindenburg committed
242
    foreach(QAction* action, multiViewOnlyActions) {
243
        connect(this , SIGNAL(splitViewToggle(bool)) , action , SLOT(setEnabled(bool)));
244 245
    }

246
    // keyboard shortcut only actions
Kurt Hindenburg's avatar
Kurt Hindenburg committed
247 248
    nextViewAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Right));
    connect(nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()));
249
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
250

Kurt Hindenburg's avatar
Kurt Hindenburg committed
251 252
    previousViewAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Left));
    connect(previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()));
253
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
254

Kurt Hindenburg's avatar
Kurt Hindenburg committed
255 256
    nextContainerAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Tab));
    connect(nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()));
257
    _viewSplitter->addAction(nextContainerAction);
258

Kurt Hindenburg's avatar
Kurt Hindenburg committed
259 260
    moveViewLeftAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
    connect(moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()));
261
    _viewSplitter->addAction(moveViewLeftAction);
262

Kurt Hindenburg's avatar
Kurt Hindenburg committed
263 264
    moveViewRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
    connect(moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()));
265
    _viewSplitter->addAction(moveViewRightAction);
266

Kurt Hindenburg's avatar
Kurt Hindenburg committed
267
    connect(lastViewAction, SIGNAL(triggered()) , this , SLOT(lastView()));
268
    _viewSplitter->addAction(lastViewAction);
Robert Knight's avatar
 
Robert Knight committed
269
}
270 271 272 273
void ViewManager::switchToView(int index)
{
    Q_ASSERT(index >= 0);
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
274
    Q_ASSERT(container);
275 276 277 278 279
    QList<QWidget*> containerViews = container->views();
    if (index >= containerViews.count())
        return;
    container->setActiveView(containerViews.at(index));
}
280 281
void ViewManager::updateDetachViewState()
{
282 283
    if (!_actionCollection)
        return;
284

Jekyll Wu's avatar
Jekyll Wu committed
285 286 287
    const bool splitView = _viewSplitter->containers().count() >= 2;
    const bool shouldEnable = splitView ||
                              _viewSplitter->activeContainer()->views().count() >= 2;
288

289
    QAction* detachAction = _actionCollection->action("detach-view");
290

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
315
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
316 317 318 319 320 321 322 323

    container->activateNextView();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
324
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
325 326

    container->activatePreviousView();
327
}
328

329 330 331 332
void ViewManager::lastView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
333
    Q_ASSERT(container);
334 335 336 337

    container->activateLastView();
}

338 339
void ViewManager::detachActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
340
    // find the currently active view and remove it from its container
341 342
    ViewContainer* container = _viewSplitter->activeContainer();

343 344 345 346 347
    detachView(container, container->activeView());
}

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
348
    TerminalDisplay * viewToDetach = qobject_cast<TerminalDisplay*>(widgetView);
349 350

    if (!viewToDetach)
351 352
        return;

353
    emit viewDetached(_sessionMap[viewToDetach]);
354

355
    _sessionMap.remove(viewToDetach);
356 357

    // remove the view from this window
358 359
    container->removeView(viewToDetach);
    viewToDetach->deleteLater();
360 361 362 363

    // 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
Kurt Hindenburg's avatar
Kurt Hindenburg committed
364 365
    if (_viewSplitter->containers().count() > 1 &&
            container->views().count() == 0) {
366
        removeContainer(container);
367 368 369 370
    }

}

371
void ViewManager::sessionFinished()
372
{
373 374 375 376
    // if this slot is called after the view manager's main widget
    // has been destroyed, do nothing
    if (!_viewSplitter)
        return;
377

378 379 380
    Session* session = qobject_cast<Session*>(sender());
    Q_ASSERT(session);

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
391
    // This is needed to remove this controller from factory() in
392 393 394
    // 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()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
399
    // give the active view in a container the focus.  this ensures
Robert Knight's avatar
Robert Knight committed
400 401 402 403 404
    // 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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
414
void ViewManager::viewActivated(QWidget* view)
415
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
416
    Q_ASSERT(view != 0);
417 418 419 420 421

    // 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);
422 423
}

424
void ViewManager::splitLeftRight()
425
{
426
    splitView(Qt::Horizontal);
427
}
428
void ViewManager::splitTopBottom()
429
{
430
    splitView(Qt::Vertical);
431 432
}

433
void ViewManager::splitView(Qt::Orientation orientation)
434
{
435 436
    ViewContainer* container = createContainer();

437
    // iterate over each session which has a view in the current active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
438
    // container and create a new view for that session in a new container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
439
    foreach(QWidget* view,  _viewSplitter->activeContainer()->views()) {
440
        Session* session = _sessionMap[qobject_cast<TerminalDisplay*>(view)];
441
        TerminalDisplay* display = createTerminalDisplay(session);
Jekyll Wu's avatar
Jekyll Wu committed
442 443
        const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
        applyProfileToView(display, profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
444
        ViewProperties* properties = createController(session, display);
445

446
        _sessionMap[display] = session;
447

Kurt Hindenburg's avatar
Kurt Hindenburg committed
448 449
        container->addView(display, properties);
        session->addView(display);
450
    }
451

Kurt Hindenburg's avatar
Kurt Hindenburg committed
452
    _viewSplitter->addContainer(container, orientation);
453
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
454

455 456
    // focus the new container
    container->containerWidget()->setFocus();
457 458

    // ensure that the active view is focused after the split / unsplit
459 460 461
    ViewContainer* activeContainer = _viewSplitter->activeContainer();
    QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
462
    if (activeView)
463
        activeView->setFocus(Qt::OtherFocusReason);
464
}
465
void ViewManager::removeContainer(ViewContainer* container)
466
{
467
    // remove session map entries for views in this container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
468
    foreach(QWidget* view , container->views()) {
469 470 471
        TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
        Q_ASSERT(display);
        _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
472
    }
473

474
    _viewSplitter->removeContainer(container);
475
    container->deleteLater();
476

Kurt Hindenburg's avatar
Kurt Hindenburg committed
477
    emit splitViewToggle(_viewSplitter->containers().count() > 1);
478
}
479
void ViewManager::expandActiveContainer()
480
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
481
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), 10);
482
}
483
void ViewManager::shrinkActiveContainer()
484
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
485
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), -10);
486
}
487
void ViewManager::closeActiveContainer()
488 489
{
    // only do something if there is more than one container active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
490
    if (_viewSplitter->containers().count() > 1) {
491 492 493
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
494

Kurt Hindenburg's avatar
Kurt Hindenburg committed
495
        // focus next container so that user can continue typing
496 497 498
        // without having to manually focus it themselves
        nextContainer();
    }
499
}
500
void ViewManager::closeOtherContainers()
501 502 503
{
    ViewContainer* active = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
504
    foreach(ViewContainer* container, _viewSplitter->containers()) {
505 506
        if (container != active)
            removeContainer(container);
507 508 509
    }
}

510
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
511
{
Robert Knight's avatar
Robert Knight committed
512 513
    // create a new controller for the session, and ensure that this view manager
    // is notified when the view gains the focus
Kurt Hindenburg's avatar
Kurt Hindenburg committed
514 515 516 517 518
    SessionController* controller = new SessionController(session, view, this);
    connect(controller , SIGNAL(focused(SessionController*)) , this , SLOT(controllerChanged(SessionController*)));
    connect(session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
    connect(session , SIGNAL(primaryScreenInUse(bool)) ,
            controller , SLOT(setupPrimaryScreenSpecificActions(bool)));
519
    connect(session , SIGNAL(selectionChanged(QString)) ,
Jekyll Wu's avatar
Jekyll Wu committed
520
            controller , SLOT(selectionChanged(QString)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
521
    connect(view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
522

523 524 525
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
526

527 528 529
    return controller;
}

530 531
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
532
    if (controller == _pluggedController)
533
        return;
534

535
    _viewSplitter->setFocusProxy(controller->view());
536

537 538
    _pluggedController = controller;
    emit activeViewChanged(controller);
539 540 541 542
}

SessionController* ViewManager::activeViewController() const
{
543
    return _pluggedController;
544
}
545 546 547 548 549 550

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

551 552
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
553
    // notify this view manager when the session finishes so that its view
554
    // can be deleted
555 556
    //
    // TODO - Find a more efficient a way to avoid multiple connections
Kurt Hindenburg's avatar
Kurt Hindenburg committed
557 558
    disconnect(session , SIGNAL(finished()) , this , SLOT(sessionFinished()));
    connect(session , SIGNAL(finished()) , this , SLOT(sessionFinished()));
559

Kurt Hindenburg's avatar
Kurt Hindenburg committed
560 561 562
    TerminalDisplay* display = createTerminalDisplay(session);
    const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    applyProfileToView(display, profile);
563

Kurt Hindenburg's avatar
Kurt Hindenburg committed
564
    // set initial size
565 566 567
    const QSize& preferredSize = session->preferredSize();
    // FIXME: +1 is needed here for getting the expected rows
    display->setSize(preferredSize.width(), preferredSize.height() + 1);
568

Kurt Hindenburg's avatar
Kurt Hindenburg committed
569
    ViewProperties* properties = createController(session, display);
570

Kurt Hindenburg's avatar
Kurt Hindenburg committed
571 572 573
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
574

Kurt Hindenburg's avatar
Kurt Hindenburg committed
575 576
    // tell the session whether it has a light or dark background
    session->setDarkBackground(colorSchemeForProfile(profile)->hasDarkBackground());
577

Kurt Hindenburg's avatar
Kurt Hindenburg committed
578 579 580 581
    if (container == _viewSplitter->activeContainer()) {
        container->setActiveView(display);
        display->setFocus(Qt::OtherFocusReason);
    }
582

Kurt Hindenburg's avatar
Kurt Hindenburg committed
583
    updateDetachViewState();
584
}
585

586
void ViewManager::createView(Session* session)
587
{
588
    // create the default container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
589
    if (_viewSplitter->containers().count() == 0) {
590 591
        ViewContainer* container = createContainer();
        _viewSplitter->addContainer(container, Qt::Vertical);
592
        emit splitViewToggle(false);
593 594
    }

595
    // new tab will be put at the end by default.
596 597
    int index = -1;

598
    if (_newTabBehavior == PutNewTabAfterCurrentTab) {
599
        QWidget* view = activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
600
        if (view) {
601 602 603
            QList<QWidget*> views =  _viewSplitter->activeContainer()->views();
            index = views.indexOf(view) + 1;
        }
604 605
    }

Robert Knight's avatar
Robert Knight committed
606 607
    // 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
Kurt Hindenburg's avatar
Kurt Hindenburg committed
608
    // a controller for the session/display pair
Kurt Hindenburg's avatar
Kurt Hindenburg committed
609
    foreach(ViewContainer* container,  _viewSplitter->containers()) {
610
        createView(session, container, index);
611 612 613
    }
}

614
ViewContainer* ViewManager::createContainer()
615
{
616 617
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
618 619
    switch (_navigationMethod) {
    case TabbedNavigation: {
Jekyll Wu's avatar
Jekyll Wu committed
620
        container = new TabbedViewContainer(_navigationPosition, _viewSplitter);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
621

Kurt Hindenburg's avatar
Kurt Hindenburg committed
622 623
        connect(container, SIGNAL(detachTab(ViewContainer*,QWidget*)),
                this, SLOT(detachView(ViewContainer*,QWidget*))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
624
               );
Kurt Hindenburg's avatar
Kurt Hindenburg committed
625 626
        connect(container, SIGNAL(closeTab(ViewContainer*,QWidget*)),
                this, SLOT(closeTabFromContainer(ViewContainer*,QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
627 628 629 630 631
    }
    break;
    case NoNavigation:
    default:
        container = new StackedViewContainer(_viewSplitter);
632
    }
633

634
    applyNavigationOptions(container);
635

636
    // connect signals and slots
Kurt Hindenburg's avatar
Kurt Hindenburg committed
637
    connect(container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
638 639 640 641 642 643
            SLOT(map()));
    connect(container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
            SLOT(map()));
    _containerSignalMapper->setMapping(container, container);

    connect(container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
644 645
    connect(container, SIGNAL(moveViewRequest(int,int,bool&)),
            this , SLOT(containerMoveViewRequest(int,int,bool&)));
646
    connect(container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewDestroyed(QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
647
    connect(container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
648

649
    return container;
650
}
651 652
void ViewManager::containerMoveViewRequest(int index, int id, bool& moved)
{
653 654
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
655

656 657
    if (!controller)
        return;
658

Kurt Hindenburg's avatar
Kurt Hindenburg committed
659
    createView(controller->session(), container, index);
660
    controller->session()->refresh();
661
    moved = true;
662
}
663 664 665 666 667 668
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
669
    if (collection) {
670 671 672 673 674 675 676 677 678
        // 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.

679
        const bool enable = (_navigationMethod != NoNavigation);
680 681
        QAction* action;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
682 683
        action = collection->action("next-view");
        if (action) action->setEnabled(enable);
684

Kurt Hindenburg's avatar
Kurt Hindenburg committed
685 686
        action = collection->action("previous-view");
        if (action) action->setEnabled(enable);
687

Kurt Hindenburg's avatar
Kurt Hindenburg committed
688 689
        action = collection->action("last-tab");
        if (action) action->setEnabled(enable);
690

Kurt Hindenburg's avatar
Kurt Hindenburg committed
691 692
        action = collection->action("split-view-left-right");
        if (action) action->setEnabled(enable);
693

Kurt Hindenburg's avatar
Kurt Hindenburg committed
694 695
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
696

Kurt Hindenburg's avatar
Kurt Hindenburg committed
697 698
        action = collection->action("rename-session");
        if (action) action->setEnabled(enable);
699

Kurt Hindenburg's avatar
Kurt Hindenburg committed
700 701
        action = collection->action("move-view-left");
        if (action) action->setEnabled(enable);
702

Kurt Hindenburg's avatar
Kurt Hindenburg committed
703 704
        action = collection->action("move-view-right");
        if (action) action->setEnabled(enable);
705 706 707
    }
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
708 709 710 711
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
712

713 714
void ViewManager::containerViewsChanged(QObject* container)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
715 716 717
    if (_viewSplitter && container == _viewSplitter->activeContainer()) {
        emit viewPropertiesChanged(viewProperties());
    }
718 719
}

720
void ViewManager::viewDestroyed(QWidget* view)
721
{
722 723 724
    // Note: the received QWidget has already been destroyed, so
    // using dynamic_cast<> or qobject_cast<> does not work here
    TerminalDisplay* display = static_cast<TerminalDisplay*>(view);
725 726
    Q_ASSERT(display);

727 728
    // 1. detach view from session
    // 2. if the session has no views left, close it
729
    Session* session = _sessionMap[ display ];
730
    _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
731
    if (session) {
732
        display->deleteLater();
733

Kurt Hindenburg's avatar
Kurt Hindenburg committed
734
        if (session->views().count() == 0)
735
            session->close();
736
    }
737 738 739 740 741
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        focusActiveView();
        updateDetachViewState();
    }
742 743 744 745
    // The below causes the menus  to be messed up
    // Only happenss when using the tab bar close button
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
746 747
}

748
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
749
{
750 751
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
752

753
    return display;
754 755
}

756
const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr profile)
757
{
758
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
Kurt Hindenburg's avatar
Kurt Hindenburg committed
759 760 761 762
                                     findColorScheme(profile->colorScheme());
    if (!colorScheme)
        colorScheme = ColorSchemeManager::instance()->defaultColorScheme();
    Q_ASSERT(colorScheme);
763

764 765 766
    return colorScheme;
}

Jekyll Wu's avatar
Jekyll Wu committed
767
void ViewManager::applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile)
768
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
769
    Q_ASSERT(profile);
770

771
    emit setSaveGeometryOnExitRequest(profile->saveGeometryOnExit());
772

773 774
    emit updateWindowIcon();

775
    // load color scheme
776
    ColorEntry table[TABLE_COLORS];
777
    const ColorScheme* colorScheme = colorSchemeForProfile(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
778
    colorScheme->getColorTable(table , view->randomSeed());
779
    view->setColorTable(table);
780
    view->setOpacity(colorScheme->opacity());
781
    view->setWallpaper(colorScheme->wallpaper());
782

Kurt Hindenburg's avatar
Kurt Hindenburg committed
783
    // load font
784 785
    view->setAntialias(profile->antiAliasFonts());
    view->setBoldIntense(profile->boldIntense());