ViewManager.cpp 37 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>
34
#include <KXMLGUIFactory>
Laurent Montel's avatar
Laurent Montel committed
35
#include <KConfigGroup>
36 37

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

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

51 52
using namespace Konsole;

53 54
int ViewManager::lastManagerId = 0;

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

72 73 74 75 76
    // 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
77
    // mode
78
    _viewSplitter->setRecursiveSplitting(false);
79
    _viewSplitter->setFocusPolicy(Qt::NoFocus);
80

81
    // setup actions which are related to the views
82 83 84
    setupActions();

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

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

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

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

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

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

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

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

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

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

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

144 145
void ViewManager::setupActions()
{
146
    KActionCollection* collection = _actionCollection;
147

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

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

156 157 158 159 160
    // 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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
176
        closeActiveAction->setIcon(KIcon("view-close"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
177
        closeActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
178
        closeActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
179
        collection->addAction("close-active-view", closeActiveAction);
180
        connect(closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveContainer()));
181 182

        multiViewOnlyActions << closeActiveAction;
183

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

        multiViewOnlyActions << closeOtherAction;
191

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

199 200
        multiViewOnlyActions << expandActiveAction;

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

207 208
        multiViewOnlyActions << shrinkActiveAction;

209 210 211 212 213 214 215 216 217 218
        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()));

219
        // Next / Previous View , Next Container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
220 221 222 223 224 225
        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);
226 227 228 229

        // Switch to tab N shortcuts
        const int SWITCH_TO_TAB_COUNT = 10;
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
230 231 232 233 234
        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,
235
                    SLOT(map()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
236
            collection->addAction(QString("switch-to-tab-%1").arg(i), switchToTabAction);
237
        }
238
    }
Robert Knight's avatar
 
Robert Knight committed
239

240 241
    foreach (QAction* action, multiViewOnlyActions) {
        connect(this , SIGNAL(splitViewToggle(bool)) , action , SLOT(setEnabled(bool)));
242 243
    }

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

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

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

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

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

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

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

287
    QAction* detachAction = _actionCollection->action("detach-view");
288

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

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

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

    container->activateNextView();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
322
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
323 324

    container->activatePreviousView();
325
}
326

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
331
    Q_ASSERT(container);
332 333 334 335

    container->activateLastView();
}

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

341 342 343 344 345
    detachView(container, container->activeView());
}

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

    if (!viewToDetach)
349 350
        return;

351
    emit viewDetached(_sessionMap[viewToDetach]);
352

353
    _sessionMap.remove(viewToDetach);
354 355

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

    // 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
362 363
    if (_viewSplitter->containers().count() > 1 &&
            container->views().count() == 0) {
364
        removeContainer(container);
365 366 367 368
    }

}

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

376 377 378
    Session* session = qobject_cast<Session*>(sender());
    Q_ASSERT(session);

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

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

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

void ViewManager::focusActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
397
    // give the active view in a container the focus.  this ensures
Robert Knight's avatar
Robert Knight committed
398 399 400 401 402
    // 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
403 404
    ViewContainer* container = _viewSplitter->activeContainer();
    if (container) {
Robert Knight's avatar
 
Robert Knight committed
405
        QWidget* activeView = container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
406
        if (activeView) {
Robert Knight's avatar
 
Robert Knight committed
407 408 409
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
410 411
}

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

    // 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);
420 421
}

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

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

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

444
        _sessionMap[display] = session;
445

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

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

453 454
    // focus the new container
    container->containerWidget()->setFocus();
455 456

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

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

472
    _viewSplitter->removeContainer(container);
473
    container->deleteLater();
474

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

        removeContainer(container);
492

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

502 503 504
    foreach (ViewContainer* container, _viewSplitter->containers()) {
        if (container != active)
            removeContainer(container);
505 506 507
    }
}

508
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
509
{
Robert Knight's avatar
Robert Knight committed
510 511
    // 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
512 513 514 515 516 517 518 519
    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)));
    connect(session , SIGNAL(selectedText(QString)) ,
            controller , SLOT(updateCopyAction(QString)));
    connect(view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
520

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

525 526 527
    return controller;
}

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

533
    _viewSplitter->setFocusProxy(controller->view());
534

535 536
    _pluggedController = controller;
    emit activeViewChanged(controller);
537 538 539 540
}

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
562 563
    // set initial size
    display->setSize(80, 40);
564

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
567 568 569
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
570

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
574 575 576 577
    if (container == _viewSplitter->activeContainer()) {
        container->setActiveView(display);
        display->setFocus(Qt::OtherFocusReason);
    }
578

Kurt Hindenburg's avatar
Kurt Hindenburg committed
579
    updateDetachViewState();
580
}
581

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

591
    // new tab will be put at the end by default.
592 593
    int index = -1;

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

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

610
ViewContainer* ViewManager::createContainer()
611
{
612 613
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
614 615
    switch (_navigationMethod) {
    case TabbedNavigation: {
Jekyll Wu's avatar
Jekyll Wu committed
616
        container = new TabbedViewContainer(_navigationPosition, _viewSplitter);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
617

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

630
    applyNavigationOptions(container);
631

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

    connect(container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()));
640 641
    connect(container, SIGNAL(moveViewRequest(int,int,bool&)),
            this , SLOT(containerMoveViewRequest(int,int,bool&)));
642
    connect(container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewDestroyed(QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
643
    connect(container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
644

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

652 653
    if (!controller)
        return;
654

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

    KActionCollection* collection = _actionCollection;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
665
    if (collection) {
666 667 668 669 670 671 672 673 674
        // 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.

675
        const bool enable = (_navigationMethod != NoNavigation);
676 677
        QAction* action;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
678 679
        action = collection->action("next-view");
        if (action) action->setEnabled(enable);
680

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
684 685
        action = collection->action("last-tab");
        if (action) action->setEnabled(enable);
686

Kurt Hindenburg's avatar
Kurt Hindenburg committed
687 688
        action = collection->action("split-view-left-right");
        if (action) action->setEnabled(enable);
689

Kurt Hindenburg's avatar
Kurt Hindenburg committed
690 691
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
692

Kurt Hindenburg's avatar
Kurt Hindenburg committed
693 694
        action = collection->action("rename-session");
        if (action) action->setEnabled(enable);
695

Kurt Hindenburg's avatar
Kurt Hindenburg committed
696 697
        action = collection->action("move-view-left");
        if (action) action->setEnabled(enable);
698

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
704 705 706 707
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
708

709 710
void ViewManager::containerViewsChanged(QObject* container)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
711 712 713
    if (_viewSplitter && container == _viewSplitter->activeContainer()) {
        emit viewPropertiesChanged(viewProperties());
    }
714 715
}

716
void ViewManager::viewDestroyed(QWidget* view)
717
{
718 719 720
    // 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);
721 722
    Q_ASSERT(display);

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

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

744
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
745
{
746 747
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
748

749
    return display;
750 751
}

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

760 761 762
    return colorScheme;
}

Jekyll Wu's avatar
Jekyll Wu committed
763
void ViewManager::applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile)
764
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
765
    Q_ASSERT(profile);
766

Kurt Hindenburg's avatar
Kurt Hindenburg committed
767
    emit setSaveGeometryOnExitRequest(profile->property<bool>(Profile::SaveGeometryOnExit));
768

769 770
    emit updateWindowIcon();

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
779
    // load font
Jekyll Wu's avatar
Jekyll Wu committed
780 781 782
    view->setAntialias(profile->property<bool>(Profile::AntiAliasFonts));
    view->setBoldIntense(profile->property<bool>(Profile::BoldIntense));
    view->setVTFont(profile->font());
783 784

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
787 788 789 790 791 792
    if (scrollBarPosition == Profile::ScrollBarLeft)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
    else if (scrollBarPosition == Profile::ScrollBarRight)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
    else if (scrollBarPosition == Profile::ScrollBarHidden)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarHidden);
793

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