ViewManager.cpp 39.6 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
#include <config-konsole.h>

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

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

// Konsole
39
#include <windowadaptor.h>
40

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

53 54
using namespace Konsole;

55 56
int ViewManager::lastManagerId = 0;

57 58
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
59
    , _viewSplitter(0)
60 61
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
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

    //prepare DBus communication
101 102
    new WindowAdaptor(this);
    // TODO: remove this obsolete and bad name
103 104
    QDBusConnection::sessionBus().registerObject(QLatin1String("/Konsole"), this);

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

ViewManager::~ViewManager()
{
111
}
112 113 114 115 116 117

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

118 119 120
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
121
    if (container) {
122
        return container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
123
    } else {
124 125 126
        return 0;
    }
}
127

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
137 138 139 140
    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);
141

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

145 146 147 148 149
    // 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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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
165
        closeActiveAction->setIcon(KIcon("view-close"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
166
        closeActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
167
        closeActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
168
        collection->addAction("close-active-view", closeActiveAction);
169
        connect(closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveContainer()));
170 171

        multiViewOnlyActions << closeActiveAction;
172

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

        multiViewOnlyActions << closeOtherAction;
180

181
        // Expand & Shrink Active View
Kurt Hindenburg's avatar
Kurt Hindenburg committed
182 183
        KAction* expandActiveAction = new KAction(i18nc("@action:inmenu", "Expand View") , this);
        expandActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_BracketRight));
184
        expandActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
185
        collection->addAction("expand-active-view", expandActiveAction);
186
        connect(expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveContainer()));
187

188 189
        multiViewOnlyActions << expandActiveAction;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
190 191
        KAction* shrinkActiveAction = new KAction(i18nc("@action:inmenu", "Shrink View") , this);
        shrinkActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_BracketLeft));
192
        shrinkActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
193
        collection->addAction("shrink-active-view", shrinkActiveAction);
194
        connect(shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveContainer()));
195

196 197
        multiViewOnlyActions << shrinkActiveAction;

198
#if defined(ENABLE_DETACHING)
Alex Richardson's avatar
Alex Richardson committed
199
        QAction* detachViewAction = collection->addAction("detach-view");
200 201 202 203 204 205 206 207
        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()));
208
#endif
209

210
        // Next / Previous View , Next Container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
211 212 213 214 215 216
        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);
217 218

        // Switch to tab N shortcuts
219
        const int SWITCH_TO_TAB_COUNT = 19;
220
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
221 222 223 224 225
        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,
226
                    SLOT(map()));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
            collection->addAction(QString("switch-to-tab-%1").arg(i), switchToTabAction);
228
        }
229
    }
Robert Knight's avatar
 
Robert Knight committed
230

Kurt Hindenburg's avatar
Kurt Hindenburg committed
231
    foreach(QAction* action, multiViewOnlyActions) {
232
        connect(this , SIGNAL(splitViewToggle(bool)) , action , SLOT(setEnabled(bool)));
233 234
    }

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
240 241
    previousViewAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Left));
    connect(previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()));
242
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
243

Kurt Hindenburg's avatar
Kurt Hindenburg committed
244 245
    nextContainerAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Tab));
    connect(nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()));
246
    _viewSplitter->addAction(nextContainerAction);
247

Kurt Hindenburg's avatar
Kurt Hindenburg committed
248 249
    moveViewLeftAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Left));
    connect(moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()));
250
    _viewSplitter->addAction(moveViewLeftAction);
251

Kurt Hindenburg's avatar
Kurt Hindenburg committed
252 253
    moveViewRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Right));
    connect(moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()));
254
    _viewSplitter->addAction(moveViewRightAction);
255

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

Jekyll Wu's avatar
Jekyll Wu committed
274 275 276
    const bool splitView = _viewSplitter->containers().count() >= 2;
    const bool shouldEnable = splitView ||
                              _viewSplitter->activeContainer()->views().count() >= 2;
277

278
    QAction* detachAction = _actionCollection->action("detach-view");
279

Kurt Hindenburg's avatar
Kurt Hindenburg committed
280
    if (detachAction && shouldEnable != detachAction->isEnabled())
281
        detachAction->setEnabled(shouldEnable);
282
}
283 284 285
void ViewManager::moveActiveViewLeft()
{
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
286 287
    Q_ASSERT(container);
    container->moveActiveView(ViewContainer::MoveViewLeft);
288 289 290 291
}
void ViewManager::moveActiveViewRight()
{
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
292 293
    Q_ASSERT(container);
    container->moveActiveView(ViewContainer::MoveViewRight);
294
}
Robert Knight's avatar
 
Robert Knight committed
295 296 297 298 299 300 301 302 303
void ViewManager::nextContainer()
{
    _viewSplitter->activateNextContainer();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
304
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
305 306 307 308 309 310 311 312

    container->activateNextView();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
313
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
314 315

    container->activatePreviousView();
316
}
317

318 319 320 321
void ViewManager::lastView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
322
    Q_ASSERT(container);
323 324 325 326

    container->activateLastView();
}

327 328
void ViewManager::detachActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
329
    // find the currently active view and remove it from its container
330 331
    ViewContainer* container = _viewSplitter->activeContainer();

332 333 334 335 336
    detachView(container, container->activeView());
}

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
337 338 339 340
#if !defined(ENABLE_DETACHING)
    return;
#endif

341
    TerminalDisplay * viewToDetach = qobject_cast<TerminalDisplay*>(widgetView);
342 343

    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

    // 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
357 358
    if (_viewSplitter->containers().count() > 1 &&
            container->views().count() == 0) {
359
        removeContainer(container);
360 361 362
    }
}

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

370 371 372
    Session* session = qobject_cast<Session*>(sender());
    Q_ASSERT(session);

373
    // close attached views
374
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
375

Kurt Hindenburg's avatar
Kurt Hindenburg committed
376
    foreach(TerminalDisplay* view , children) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
377
        if (_sessionMap[view] == session) {
378
            _sessionMap.remove(view);
379
            view->deleteLater();
380
        }
Robert Knight's avatar
 
Robert Knight committed
381
    }
382

Kurt Hindenburg's avatar
Kurt Hindenburg committed
383
    // This is needed to remove this controller from factory() in
384 385 386
    // order to prevent BUG: 185466 - disappearing menu popup
    if (_pluggedController)
        emit unplugController(_pluggedController);
Robert Knight's avatar
 
Robert Knight committed
387 388 389 390
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
406
void ViewManager::viewActivated(QWidget* view)
407
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
408
    Q_ASSERT(view != 0);
409 410 411 412 413

    // 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);
414 415
}

416
void ViewManager::splitLeftRight()
417
{
418
    splitView(Qt::Horizontal);
419
}
420
void ViewManager::splitTopBottom()
421
{
422
    splitView(Qt::Vertical);
423 424
}

425
void ViewManager::splitView(Qt::Orientation orientation)
426
{
427 428
    ViewContainer* container = createContainer();

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

438
        _sessionMap[display] = session;
439

Kurt Hindenburg's avatar
Kurt Hindenburg committed
440 441
        container->addView(display, properties);
        session->addView(display);
442
    }
443

Kurt Hindenburg's avatar
Kurt Hindenburg committed
444
    _viewSplitter->addContainer(container, orientation);
445
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
446

447 448
    // focus the new container
    container->containerWidget()->setFocus();
449 450

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

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

466
    _viewSplitter->removeContainer(container);
467
    container->deleteLater();
468

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

        removeContainer(container);
486

Kurt Hindenburg's avatar
Kurt Hindenburg committed
487
        // focus next container so that user can continue typing
488 489 490
        // without having to manually focus it themselves
        nextContainer();
    }
491
}
492
void ViewManager::closeOtherContainers()
493 494 495
{
    ViewContainer* active = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
496
    foreach(ViewContainer* container, _viewSplitter->containers()) {
497 498
        if (container != active)
            removeContainer(container);
499 500 501
    }
}

502
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
503
{
Robert Knight's avatar
Robert Knight committed
504 505
    // 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
506 507 508 509 510
    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)));
511
    connect(session , SIGNAL(selectionChanged(QString)) ,
Jekyll Wu's avatar
Jekyll Wu committed
512
            controller , SLOT(selectionChanged(QString)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
513
    connect(view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
514

515 516 517
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
518

519 520 521
    return controller;
}

522 523
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
524
    if (controller == _pluggedController)
525
        return;
526

527
    _viewSplitter->setFocusProxy(controller->view());
528

529 530
    _pluggedController = controller;
    emit activeViewChanged(controller);
531 532 533 534
}

SessionController* ViewManager::activeViewController() const
{
535
    return _pluggedController;
536
}
537 538 539 540 541 542

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

543 544
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
545
    // notify this view manager when the session finishes so that its view
546
    // can be deleted
547
    //
548 549
    // Use Qt::UniqueConnection to avoid duplicate connection
    connect(session, SIGNAL(finished()), this, SLOT(sessionFinished()), Qt::UniqueConnection);
550

Kurt Hindenburg's avatar
Kurt Hindenburg committed
551 552 553
    TerminalDisplay* display = createTerminalDisplay(session);
    const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    applyProfileToView(display, profile);
554

Kurt Hindenburg's avatar
Kurt Hindenburg committed
555
    // set initial size
556 557
    const QSize& preferredSize = session->preferredSize();
    // FIXME: +1 is needed here for getting the expected rows
558 559 560 561
    // Note that the display shouldn't need to take into account the tabbar.
    // However, it appears that taking into account the tabbar is needed.
    // If tabbar is not visible, no +1 is needed here; however, depending on
    // settings/tabbar style, +2 might be needed.
562 563 564 565 566 567
    // 1st attempt at fixing the above:
    // Guess if tabbar will NOT be visible; ignore ShowNavigationAsNeeded
    int heightAdjustment = 0;
    if (_navigationVisibility != ViewContainer::AlwaysHideNavigation) {
        heightAdjustment = 2;
    }
568

569
    display->setSize(preferredSize.width(), preferredSize.height() + heightAdjustment);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
570
    ViewProperties* properties = createController(session, display);
571

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

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

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

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

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

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

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

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

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

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

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

635 636 637 638 639 640
    // FIXME: these code feels duplicated
    container->setNavigationVisibility(_navigationVisibility);
    container->setNavigationPosition(_navigationPosition);
    container->setStyleSheet(_navigationStyleSheet);
    if (_showQuickButtons) {
        container->setFeatures(container->features()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
641 642
                               | ViewContainer::QuickNewView
                               | ViewContainer::QuickCloseView);
643 644
    } else {
        container->setFeatures(container->features()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
645 646
                               & ~ViewContainer::QuickNewView
                               & ~ViewContainer::QuickCloseView);
647
    }
648

649
    // connect signals and slots
Kurt Hindenburg's avatar
Kurt Hindenburg committed
650
    connect(container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
651 652 653 654 655 656
            SLOT(map()));
    connect(container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
            SLOT(map()));
    _containerSignalMapper->setMapping(container, container);

    connect(container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()));
657
    connect(container, SIGNAL(newViewRequest(Profile::Ptr)), this, SIGNAL(newViewRequest(Profile::Ptr)));
658 659
    connect(container, SIGNAL(moveViewRequest(int,int,bool&,TabbedViewContainer*)),
            this , SLOT(containerMoveViewRequest(int,int,bool&,TabbedViewContainer*)));
660
    connect(container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewDestroyed(QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
661
    connect(container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
662

663
    return container;
664
}
665 666

void ViewManager::containerMoveViewRequest(int index, int id, bool& moved, TabbedViewContainer* sourceTabbedContainer)
667
{
668 669
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
670

671 672
    if (!controller)
        return;
673

Kurt Hindenburg's avatar
Kurt Hindenburg committed
674
    // do not move the last tab in a split view.
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    if (sourceTabbedContainer) {
        QPointer<ViewContainer> sourceContainer = qobject_cast<ViewContainer*>(sourceTabbedContainer);

        if (_viewSplitter->containers().contains(sourceContainer)) {
            return;
        } else {
            ViewManager* sourceViewManager = sourceTabbedContainer->connectedViewManager();

            // do not remove the last tab on the window
            if (qobject_cast<ViewSplitter*>(sourceViewManager->widget())->containers().size() > 1) {
                return;
            }
        }
    }

Kurt Hindenburg's avatar
Kurt Hindenburg committed
690
    createView(controller->session(), container, index);
691
    controller->session()->refresh();
692
    moved = true;
693
}
694

695 696 697 698 699 700
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
701
    if (collection) {
702 703 704 705 706 707 708 709 710
        // 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.

711
        const bool enable = (_navigationMethod != NoNavigation);
712 713
        QAction* action;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
714 715
        action = collection->action("next-view");
        if (action) action->setEnabled(enable);
716

Kurt Hindenburg's avatar
Kurt Hindenburg committed
717 718
        action = collection->action("previous-view");
        if (action) action->setEnabled(enable);
719

Kurt Hindenburg's avatar
Kurt Hindenburg committed
720 721
        action = collection->action("last-tab");
        if (action) action->setEnabled(enable);
722

Kurt Hindenburg's avatar
Kurt Hindenburg committed
723 724
        action = collection->action("split-view-left-right");
        if (action) action->setEnabled(enable);
725

Kurt Hindenburg's avatar
Kurt Hindenburg committed
726 727
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
728

Kurt Hindenburg's avatar
Kurt Hindenburg committed
729 730
        action = collection->action("rename-session");
        if (action) action->setEnabled(enable);
731

Kurt Hindenburg's avatar
Kurt Hindenburg committed
732 733
        action = collection->action("move-view-left");
        if (action) action->setEnabled(enable);
734

Kurt Hindenburg's avatar
Kurt Hindenburg committed
735 736
        action = collection->action("move-view-right");
        if (action) action->setEnabled(enable);
737 738 739
    }
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
740 741 742 743
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
744

745 746
void ViewManager::containerViewsChanged(QObject* container)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
747 748 749
    if (_viewSplitter && container == _viewSplitter->activeContainer()) {
        emit viewPropertiesChanged(viewProperties());
    }
750 751
}

752
void ViewManager::viewDestroyed(QWidget* view)
753
{
754 755 756
    // 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);
757 758
    Q_ASSERT(display);

759 760
    // 1. detach view from session
    // 2. if the session has no views left, close it
761
    Session* session = _sessionMap[ display ];
762
    _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
763
    if (session) {
764
        display->deleteLater();
765

Kurt Hindenburg's avatar
Kurt Hindenburg committed
766
        if (session->views().count() == 0)
767
            session->close();
768
    }
769 770 771 772 773
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        focusActiveView();
        updateDetachViewState();
    }
774
    // The below causes the menus  to be messed up
775
    // Only happens when using the tab bar close button
776 777
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
778 779
}

780
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
781
{
782 783
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
784

785
    return display;
786 787
}

788
const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr profile)
789
{
790
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
Kurt Hindenburg's avatar
Kurt Hindenburg committed
791 792 793