ViewManager.cpp 39.8 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>
29
#include <QAction>
Jekyll Wu's avatar
Jekyll Wu committed
30
#include <QtDBus/QtDBus>
31

32
// KDE
Robert Knight's avatar
Robert Knight committed
33
#include <KAcceleratorManager>
34
#include <KLocalizedString>
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
87 88
    connect(_viewSplitter.data() , &Konsole::ViewSplitter::allContainersEmpty , this , &Konsole::ViewManager::empty);
    connect(_viewSplitter.data() , &Konsole::ViewSplitter::empty , this , &Konsole::ViewManager::empty);
89 90

    // listen for addition or removal of views from associated containers
Laurent Montel's avatar
Laurent Montel committed
91
    connect(_containerSignalMapper , static_cast<void(QSignalMapper::*)(QObject*)>(&QSignalMapper::mapped) , this , &Konsole::ViewManager::containerViewsChanged);
92 93

    // listen for profile changes
Laurent Montel's avatar
Laurent Montel committed
94 95
    connect(ProfileManager::instance() , &Konsole::ProfileManager::profileChanged , this, &Konsole::ViewManager::profileChanged);
    connect(SessionManager::instance() , &Konsole::SessionManager::sessionUpdated , this, &Konsole::ViewManager::updateViewsForSession);
96 97

    //prepare DBus communication
98 99
    new WindowAdaptor(this);
    // TODO: remove this obsolete and bad name
100 101
    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 110 111 112 113 114

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

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

125 126 127 128 129
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

130 131
void ViewManager::setupActions()
{
132
    KActionCollection* collection = _actionCollection;
133

134 135 136 137
    QAction* nextViewAction = new QAction(i18nc("@action Shortcut entry", "Next Tab") , this);
    QAction* previousViewAction = new QAction(i18nc("@action Shortcut entry", "Previous Tab") , this);
    QAction* lastViewAction = new QAction(i18nc("@action Shortcut entry", "Switch to Last Tab") , this);
    QAction* nextContainerAction = new QAction(i18nc("@action Shortcut entry", "Next View Container") , this);
138

139 140
    QAction* moveViewLeftAction = new QAction(i18nc("@action Shortcut entry", "Move Tab Left") , this);
    QAction* moveViewRightAction = new QAction(i18nc("@action Shortcut entry", "Move Tab Right") , this);
141

142 143 144 145 146
    // 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
147
    if (collection) {
Michal Humpula's avatar
Michal Humpula committed
148
        QAction* splitLeftRightAction = new QAction(QIcon::fromTheme(QStringLiteral("view-split-left-right")),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
149 150
                i18nc("@action:inmenu", "Split View Left/Right"),
                this);
151
        collection->setDefaultShortcut(splitLeftRightAction, Qt::CTRL + Qt::Key_ParenLeft);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
152
        collection->addAction("split-view-left-right", splitLeftRightAction);
153
        connect(splitLeftRightAction , &QAction::triggered , this , &Konsole::ViewManager::splitLeftRight);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
154

Michal Humpula's avatar
Michal Humpula committed
155
        QAction* splitTopBottomAction = new QAction(QIcon::fromTheme(QStringLiteral("view-split-top-bottom")) ,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
156
                i18nc("@action:inmenu", "Split View Top/Bottom"), this);
157
        collection->setDefaultShortcut(splitTopBottomAction, Qt::CTRL + Qt::Key_ParenRight);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
158
        collection->addAction("split-view-top-bottom", splitTopBottomAction);
159
        connect(splitTopBottomAction , &QAction::triggered , this , &Konsole::ViewManager::splitTopBottom);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
160

161
        QAction* closeActiveAction = new QAction(i18nc("@action:inmenu Close Active View", "Close Active") , this);
Michal Humpula's avatar
Michal Humpula committed
162
        closeActiveAction->setIcon(QIcon::fromTheme(QStringLiteral("view-close")));
163
        collection->setDefaultShortcut(closeActiveAction, Qt::CTRL + Qt::SHIFT + Qt::Key_S);
164
        closeActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
165
        collection->addAction("close-active-view", closeActiveAction);
166
        connect(closeActiveAction , &QAction::triggered , this , &Konsole::ViewManager::closeActiveContainer);
167 168

        multiViewOnlyActions << closeActiveAction;
169

170
        QAction* closeOtherAction = new QAction(i18nc("@action:inmenu Close Other Views", "Close Others") , this);
171
        collection->setDefaultShortcut(closeOtherAction, Qt::CTRL + Qt::SHIFT + Qt::Key_O);
172
        closeOtherAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
173
        collection->addAction("close-other-views", closeOtherAction);
174
        connect(closeOtherAction , &QAction::triggered , this , &Konsole::ViewManager::closeOtherContainers);
175 176

        multiViewOnlyActions << closeOtherAction;
177

178
        // Expand & Shrink Active View
179
        QAction* expandActiveAction = new QAction(i18nc("@action:inmenu", "Expand View") , this);
180
        collection->setDefaultShortcut(expandActiveAction, Qt::CTRL + Qt::SHIFT + Qt::Key_BracketRight);
181
        expandActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
182
        collection->addAction("expand-active-view", expandActiveAction);
183
        connect(expandActiveAction , &QAction::triggered , this , &Konsole::ViewManager::expandActiveContainer);
184

185 186
        multiViewOnlyActions << expandActiveAction;

187
        QAction* shrinkActiveAction = new QAction(i18nc("@action:inmenu", "Shrink View") , this);
188
        collection->setDefaultShortcut(shrinkActiveAction, Qt::CTRL + Qt::SHIFT + Qt::Key_BracketLeft);
189
        shrinkActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
190
        collection->addAction("shrink-active-view", shrinkActiveAction);
191
        connect(shrinkActiveAction , &QAction::triggered , this , &Konsole::ViewManager::shrinkActiveContainer);
192

193 194
        multiViewOnlyActions << shrinkActiveAction;

195
#if defined(ENABLE_DETACHING)
Alex Richardson's avatar
Alex Richardson committed
196
        QAction* detachViewAction = collection->addAction("detach-view");
Michal Humpula's avatar
Michal Humpula committed
197
        detachViewAction->setIcon(QIcon::fromTheme(QStringLiteral("tab-detach")));
198 199 200
        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
201
        collection->setDefaultShortcut(detachViewAction, Qt::CTRL + Qt::SHIFT + Qt::Key_H);
202

203 204
        connect(this , &Konsole::ViewManager::splitViewToggle , this , &Konsole::ViewManager::updateDetachViewState);
        connect(detachViewAction , &QAction::triggered , this , &Konsole::ViewManager::detachActiveView);
205
#endif
206

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

        // Switch to tab N shortcuts
216
        const int SWITCH_TO_TAB_COUNT = 19;
217
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
218
        connect(switchToTabMapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &Konsole::ViewManager::switchToView);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
219
        for (int i = 0; i < SWITCH_TO_TAB_COUNT; i++) {
220
            QAction* switchToTabAction = new QAction(i18nc("@action Shortcut entry", "Switch to Tab %1", i + 1), this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
221
            switchToTabMapper->setMapping(switchToTabAction, i);
Laurent Montel's avatar
Laurent Montel committed
222
            connect(switchToTabAction, &QAction::triggered, switchToTabMapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
223
            collection->addAction(QString("switch-to-tab-%1").arg(i), switchToTabAction);
224
        }
225
    }
Robert Knight's avatar
 
Robert Knight committed
226

Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
    foreach(QAction* action, multiViewOnlyActions) {
228
        connect(this , &Konsole::ViewManager::splitViewToggle , action , &QAction::setEnabled);
229 230
    }

231
    // keyboard shortcut only actions
232
    collection->setDefaultShortcut(nextViewAction, Qt::SHIFT + Qt::Key_Right);
233
    connect(nextViewAction, &QAction::triggered , this , &Konsole::ViewManager::nextView);
234
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
235

236
    collection->setDefaultShortcut(previousViewAction, Qt::SHIFT + Qt::Key_Left);
237
    connect(previousViewAction, &QAction::triggered , this , &Konsole::ViewManager::previousView);
238
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
239

240
    collection->setDefaultShortcut(nextContainerAction, Qt::SHIFT + Qt::Key_Tab);
241
    connect(nextContainerAction , &QAction::triggered , this , &Konsole::ViewManager::nextContainer);
242
    _viewSplitter->addAction(nextContainerAction);
243

244
    collection->setDefaultShortcut(moveViewLeftAction, Qt::CTRL + Qt::SHIFT + Qt::Key_Left);
245
    connect(moveViewLeftAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewLeft);
246
    _viewSplitter->addAction(moveViewLeftAction);
247

248
    collection->setDefaultShortcut(moveViewRightAction, Qt::CTRL + Qt::SHIFT + Qt::Key_Right);
249
    connect(moveViewRightAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewRight);
250
    _viewSplitter->addAction(moveViewRightAction);
251

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

Jekyll Wu's avatar
Jekyll Wu committed
270 271 272
    const bool splitView = _viewSplitter->containers().count() >= 2;
    const bool shouldEnable = splitView ||
                              _viewSplitter->activeContainer()->views().count() >= 2;
273

274
    QAction* detachAction = _actionCollection->action("detach-view");
275

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
300
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
301 302 303 304 305 306 307 308

    container->activateNextView();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
309
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
310 311

    container->activatePreviousView();
312
}
313

314 315 316 317
void ViewManager::lastView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
318
    Q_ASSERT(container);
319 320 321 322

    container->activateLastView();
}

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

328 329 330 331 332
    detachView(container, container->activeView());
}

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
333 334 335 336
#if !defined(ENABLE_DETACHING)
    return;
#endif

337
    TerminalDisplay * viewToDetach = qobject_cast<TerminalDisplay*>(widgetView);
338 339

    if (!viewToDetach)
340 341
        return;

342
    emit viewDetached(_sessionMap[viewToDetach]);
343

344
    _sessionMap.remove(viewToDetach);
345 346

    // remove the view from this window
347 348
    container->removeView(viewToDetach);
    viewToDetach->deleteLater();
349 350 351 352

    // 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
353 354
    if (_viewSplitter->containers().count() > 1 &&
            container->views().count() == 0) {
355
        removeContainer(container);
356 357 358
    }
}

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

366 367 368
    Session* session = qobject_cast<Session*>(sender());
    Q_ASSERT(session);

369
    // close attached views
370
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
371

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
379
    // This is needed to remove this controller from factory() in
380 381 382
    // order to prevent BUG: 185466 - disappearing menu popup
    if (_pluggedController)
        emit unplugController(_pluggedController);
Robert Knight's avatar
 
Robert Knight committed
383 384
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
385
void ViewManager::viewActivated(QWidget* view)
386
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
387
    Q_ASSERT(view != 0);
388 389 390 391 392

    // 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);
393 394
}

395
void ViewManager::splitLeftRight()
396
{
397
    splitView(Qt::Horizontal);
398
}
399
void ViewManager::splitTopBottom()
400
{
401
    splitView(Qt::Vertical);
402 403
}

404
void ViewManager::splitView(Qt::Orientation orientation)
405
{
406 407
    ViewContainer* container = createContainer();

408
    // iterate over each session which has a view in the current active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
409
    // container and create a new view for that session in a new container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
410
    foreach(QWidget* view,  _viewSplitter->activeContainer()->views()) {
411
        Session* session = _sessionMap[qobject_cast<TerminalDisplay*>(view)];
412
        TerminalDisplay* display = createTerminalDisplay(session);
Jekyll Wu's avatar
Jekyll Wu committed
413 414
        const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
        applyProfileToView(display, profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
415
        ViewProperties* properties = createController(session, display);
416

417
        _sessionMap[display] = session;
418

Kurt Hindenburg's avatar
Kurt Hindenburg committed
419 420
        container->addView(display, properties);
        session->addView(display);
421
    }
422

Kurt Hindenburg's avatar
Kurt Hindenburg committed
423
    _viewSplitter->addContainer(container, orientation);
424
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
425

426 427
    // focus the new container
    container->containerWidget()->setFocus();
428 429

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
433
    if (activeView)
434
        activeView->setFocus(Qt::OtherFocusReason);
435
}
436
void ViewManager::removeContainer(ViewContainer* container)
437
{
438
    // remove session map entries for views in this container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
439
    foreach(QWidget* view , container->views()) {
440 441 442
        TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
        Q_ASSERT(display);
        _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
443
    }
444

445
    _viewSplitter->removeContainer(container);
446
    container->deleteLater();
447

Kurt Hindenburg's avatar
Kurt Hindenburg committed
448
    emit splitViewToggle(_viewSplitter->containers().count() > 1);
449
}
450
void ViewManager::expandActiveContainer()
451
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
452
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), 10);
453
}
454
void ViewManager::shrinkActiveContainer()
455
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
456
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), -10);
457
}
458
void ViewManager::closeActiveContainer()
459 460
{
    // only do something if there is more than one container active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
461
    if (_viewSplitter->containers().count() > 1) {
462 463 464
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
465

Kurt Hindenburg's avatar
Kurt Hindenburg committed
466
        // focus next container so that user can continue typing
467 468 469
        // without having to manually focus it themselves
        nextContainer();
    }
470
}
471
void ViewManager::closeOtherContainers()
472 473 474
{
    ViewContainer* active = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
475
    foreach(ViewContainer* container, _viewSplitter->containers()) {
476 477
        if (container != active)
            removeContainer(container);
478 479 480
    }
}

481
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
482
{
Robert Knight's avatar
Robert Knight committed
483 484
    // 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
485
    SessionController* controller = new SessionController(session, view, this);
486 487
    connect(controller , &Konsole::SessionController::focused , this , &Konsole::ViewManager::controllerChanged);
    connect(session , &Konsole::Session::destroyed , controller , &Konsole::SessionController::deleteLater);
Laurent Montel's avatar
Laurent Montel committed
488 489
    connect(session , &Konsole::Session::primaryScreenInUse , controller , &Konsole::SessionController::setupPrimaryScreenSpecificActions);
    connect(session , &Konsole::Session::selectionChanged , controller , &Konsole::SessionController::selectionChanged);
490
    connect(view , &Konsole::TerminalDisplay::destroyed , controller , &Konsole::SessionController::deleteLater);
491

492 493 494
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
495

496 497 498
    return controller;
}

499 500
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
501
    if (controller == _pluggedController)
502
        return;
503

504
    _viewSplitter->setFocusProxy(controller->view());
505

506 507
    _pluggedController = controller;
    emit activeViewChanged(controller);
508 509 510 511
}

SessionController* ViewManager::activeViewController() const
{
512
    return _pluggedController;
513
}
514 515 516 517 518 519

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

520 521
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
522
    // notify this view manager when the session finishes so that its view
523
    // can be deleted
524
    //
525
    // Use Qt::UniqueConnection to avoid duplicate connection
526
    connect(session, &Konsole::Session::finished, this, &Konsole::ViewManager::sessionFinished, Qt::UniqueConnection);
527

Kurt Hindenburg's avatar
Kurt Hindenburg committed
528 529 530
    TerminalDisplay* display = createTerminalDisplay(session);
    const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    applyProfileToView(display, profile);
531

Kurt Hindenburg's avatar
Kurt Hindenburg committed
532
    // set initial size
533 534
    const QSize& preferredSize = session->preferredSize();
    // FIXME: +1 is needed here for getting the expected rows
535 536 537 538
    // 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.
539 540 541 542 543 544
    // 1st attempt at fixing the above:
    // Guess if tabbar will NOT be visible; ignore ShowNavigationAsNeeded
    int heightAdjustment = 0;
    if (_navigationVisibility != ViewContainer::AlwaysHideNavigation) {
        heightAdjustment = 2;
    }
545

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
549 550 551
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
552

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
556 557 558 559
    if (container == _viewSplitter->activeContainer()) {
        container->setActiveView(display);
        display->setFocus(Qt::OtherFocusReason);
    }
560

Kurt Hindenburg's avatar
Kurt Hindenburg committed
561
    updateDetachViewState();
562
}
563

564
void ViewManager::createView(Session* session)
565
{
566
    // create the default container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
567
    if (_viewSplitter->containers().count() == 0) {
568 569
        ViewContainer* container = createContainer();
        _viewSplitter->addContainer(container, Qt::Vertical);
570
        emit splitViewToggle(false);
571 572
    }

573
    // new tab will be put at the end by default.
574 575
    int index = -1;

576
    if (_newTabBehavior == PutNewTabAfterCurrentTab) {
577
        QWidget* view = activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
578
        if (view) {
579 580 581
            QList<QWidget*> views =  _viewSplitter->activeContainer()->views();
            index = views.indexOf(view) + 1;
        }
582 583
    }

Robert Knight's avatar
Robert Knight committed
584 585
    // 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
586
    // a controller for the session/display pair
Kurt Hindenburg's avatar
Kurt Hindenburg committed
587
    foreach(ViewContainer* container,  _viewSplitter->containers()) {
588
        createView(session, container, index);
589 590 591
    }
}

592
ViewContainer* ViewManager::createContainer()
593
{
594 595
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
596 597
    switch (_navigationMethod) {
    case TabbedNavigation: {
598 599
        TabbedViewContainer* tabbedContainer = new TabbedViewContainer(_navigationPosition, this, _viewSplitter);
        container = tabbedContainer;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
600

Laurent Montel's avatar
Laurent Montel committed
601 602
        connect(tabbedContainer, &TabbedViewContainer::detachTab, this, &ViewManager::detachView);
        connect(tabbedContainer, &TabbedViewContainer::closeTab, this, &ViewManager::closeTabFromContainer);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
603 604 605 606 607
    }
    break;
    case NoNavigation:
    default:
        container = new StackedViewContainer(_viewSplitter);
608
    }
609

610 611 612 613 614 615
    // 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
616 617
                               | ViewContainer::QuickNewView
                               | ViewContainer::QuickCloseView);
618 619
    } else {
        container->setFeatures(container->features()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
620 621
                               & ~ViewContainer::QuickNewView
                               & ~ViewContainer::QuickCloseView);
622
    }
623

624
    // connect signals and slots
Laurent Montel's avatar
Laurent Montel committed
625 626
    connect(container , &Konsole::ViewContainer::viewAdded , _containerSignalMapper , static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
    connect(container , &Konsole::ViewContainer::viewRemoved , _containerSignalMapper , static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
627 628
    _containerSignalMapper->setMapping(container, container);

629 630
    connect(container, static_cast<void(ViewContainer::*)()>(&Konsole::ViewContainer::newViewRequest), this, static_cast<void(ViewManager::*)()>(&Konsole::ViewManager::newViewRequest));
    connect(container, static_cast<void(ViewContainer::*)(Profile::Ptr)>(&Konsole::ViewContainer::newViewRequest), this, static_cast<void(ViewManager::*)(Profile::Ptr)>(&Konsole::ViewManager::newViewRequest));
Laurent Montel's avatar
Laurent Montel committed
631
    connect(container, &Konsole::ViewContainer::moveViewRequest, this , &Konsole::ViewManager::containerMoveViewRequest);
632 633
    connect(container , &Konsole::ViewContainer::viewRemoved , this , &Konsole::ViewManager::viewDestroyed);
    connect(container , &Konsole::ViewContainer::activeViewChanged , this , &Konsole::ViewManager::viewActivated);
634

635
    return container;
636
}
637 638

void ViewManager::containerMoveViewRequest(int index, int id, bool& moved, TabbedViewContainer* sourceTabbedContainer)
639
{
640 641
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
642

643 644
    if (!controller)
        return;
645

Kurt Hindenburg's avatar
Kurt Hindenburg committed
646
    // do not move the last tab in a split view.
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    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
662
    createView(controller->session(), container, index);
663
    controller->session()->refresh();
664
    moved = true;
665
}
666

667 668 669 670 671 672
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
673
    if (collection) {
674 675 676 677 678 679 680 681 682
        // 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.

683
        const bool enable = (_navigationMethod != NoNavigation);
684 685
        QAction* action;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
689 690
        action = collection->action("previous-view");
        if (action) action->setEnabled(enable);
691

Kurt Hindenburg's avatar
Kurt Hindenburg committed
692 693
        action = collection->action("last-tab");
        if (action) action->setEnabled(enable);
694

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
698 699
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
700

Kurt Hindenburg's avatar
Kurt Hindenburg committed
701 702
        action = collection->action("rename-session");
        if (action) action->setEnabled(enable);
703

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
707 708
        action = collection->action("move-view-right");
        if (action) action->setEnabled(enable);
709 710 711
    }
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
712 713 714 715
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
716

717 718
void ViewManager::containerViewsChanged(QObject* container)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
719 720 721
    if (_viewSplitter && container == _viewSplitter->activeContainer()) {
        emit viewPropertiesChanged(viewProperties());
    }
722 723
}

724
void ViewManager::viewDestroyed(QWidget* view)
725
{
726 727 728
    // 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);
729 730
    Q_ASSERT(display);

731 732
    // 1. detach view from session
    // 2. if the session has no views left, close it
733
    Session* session = _sessionMap[ display ];
734
    _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
735 736
    if (session) {
        if (session->views().count() == 0)
737
            session->close();
738
    }
739 740 741 742
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        updateDetachViewState();
    }
743
    // The below causes the menus  to be messed up
744
    // Only happens when using the tab bar close button
745 746
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
747 748
}

749
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
750
{