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 <QAction>
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>
34
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
35
#include <KConfigGroup>
36 37

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

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

50 51
using namespace Konsole;

52 53
int ViewManager::lastManagerId = 0;

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

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

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

    // emit a signal when all of the views held by this view manager are destroyed
84 85
    connect(_viewSplitter.data() , &Konsole::ViewSplitter::allContainersEmpty , this , &Konsole::ViewManager::empty);
    connect(_viewSplitter.data() , &Konsole::ViewSplitter::empty , this , &Konsole::ViewManager::empty);
86 87

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

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

    //prepare DBus communication
95
    new WindowAdaptor(this);
96

97 98
    _managerId = ++lastManagerId;
    QDBusConnection::sessionBus().registerObject(QLatin1String("/Windows/") + QString::number(_managerId), this);
99 100 101 102
}

ViewManager::~ViewManager()
{
103
}
104 105 106 107 108 109

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

110 111 112
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
113
    if (container) {
114
        return container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
115
    } else {
116 117 118
        return 0;
    }
}
119

120 121 122 123 124
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

125 126
void ViewManager::setupActions()
{
127
    KActionCollection* collection = _actionCollection;
128

129 130 131 132
    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);
133

134 135
    QAction* moveViewLeftAction = new QAction(i18nc("@action Shortcut entry", "Move Tab Left") , this);
    QAction* moveViewRightAction = new QAction(i18nc("@action Shortcut entry", "Move Tab Right") , this);
136

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

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

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

        multiViewOnlyActions << closeActiveAction;
164

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

        multiViewOnlyActions << closeOtherAction;
172

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

180 181
        multiViewOnlyActions << expandActiveAction;

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

188 189
        multiViewOnlyActions << shrinkActiveAction;

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

198 199
        connect(this , &Konsole::ViewManager::splitViewToggle , this , &Konsole::ViewManager::updateDetachViewState);
        connect(detachViewAction , &QAction::triggered , this , &Konsole::ViewManager::detachActiveView);
200
#endif
201

202
        // Next / Previous View , Next Container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
203 204 205 206 207 208
        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);
209 210

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
222
    foreach(QAction* action, multiViewOnlyActions) {
223
        connect(this , &Konsole::ViewManager::splitViewToggle , action , &QAction::setEnabled);
224 225
    }

226
    // keyboard shortcut only actions
227
    collection->setDefaultShortcut(nextViewAction, Qt::SHIFT + Qt::Key_Right);
228
    connect(nextViewAction, &QAction::triggered , this , &Konsole::ViewManager::nextView);
229
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
230

231
    collection->setDefaultShortcut(previousViewAction, Qt::SHIFT + Qt::Key_Left);
232
    connect(previousViewAction, &QAction::triggered , this , &Konsole::ViewManager::previousView);
233
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
234

235
    collection->setDefaultShortcut(nextContainerAction, Qt::SHIFT + Qt::Key_Tab);
236
    connect(nextContainerAction , &QAction::triggered , this , &Konsole::ViewManager::nextContainer);
237
    _viewSplitter->addAction(nextContainerAction);
238

239
    collection->setDefaultShortcut(moveViewLeftAction, Qt::CTRL + Qt::SHIFT + Qt::Key_Left);
240
    connect(moveViewLeftAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewLeft);
241
    _viewSplitter->addAction(moveViewLeftAction);
242

243
    collection->setDefaultShortcut(moveViewRightAction, Qt::CTRL + Qt::SHIFT + Qt::Key_Right);
244
    connect(moveViewRightAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewRight);
245
    _viewSplitter->addAction(moveViewRightAction);
246

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

Jekyll Wu's avatar
Jekyll Wu committed
265
    const bool splitView = _viewSplitter->containers().count() >= 2;
266 267
    auto activeContainer = _viewSplitter->activeContainer();
    const bool shouldEnable = splitView || (activeContainer && activeContainer->views().count() >= 2);
268

269
    QAction* detachAction = _actionCollection->action("detach-view");
270

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
295
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
296 297 298 299 300 301 302 303

    container->activateNextView();
}

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
304
    Q_ASSERT(container);
Robert Knight's avatar
 
Robert Knight committed
305 306

    container->activatePreviousView();
307
}
308

309 310 311 312
void ViewManager::lastView()
{
    ViewContainer* container = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
313
    Q_ASSERT(container);
314 315 316 317

    container->activateLastView();
}

318 319
void ViewManager::detachActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
320
    // find the currently active view and remove it from its container
321 322
    ViewContainer* container = _viewSplitter->activeContainer();

323 324 325 326 327
    detachView(container, container->activeView());
}

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
328 329 330 331
#if !defined(ENABLE_DETACHING)
    return;
#endif

332
    TerminalDisplay * viewToDetach = qobject_cast<TerminalDisplay*>(widgetView);
333 334

    if (!viewToDetach)
335 336
        return;

337
    emit viewDetached(_sessionMap[viewToDetach]);
338

339
    _sessionMap.remove(viewToDetach);
340 341

    // remove the view from this window
342 343
    container->removeView(viewToDetach);
    viewToDetach->deleteLater();
344 345 346 347

    // 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
348 349
    if (_viewSplitter->containers().count() > 1 &&
            container->views().count() == 0) {
350
        removeContainer(container);
351 352 353
    }
}

354
void ViewManager::sessionFinished()
355
{
356 357 358 359
    // if this slot is called after the view manager's main widget
    // has been destroyed, do nothing
    if (!_viewSplitter)
        return;
360

361 362 363
    Session* session = qobject_cast<Session*>(sender());
    Q_ASSERT(session);

364
    // close attached views
365
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
366

Kurt Hindenburg's avatar
Kurt Hindenburg committed
367
    foreach(TerminalDisplay* view , children) {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
368
        if (_sessionMap[view] == session) {
369
            _sessionMap.remove(view);
370
            view->deleteLater();
371
        }
Robert Knight's avatar
 
Robert Knight committed
372
    }
373

Kurt Hindenburg's avatar
Kurt Hindenburg committed
374
    // This is needed to remove this controller from factory() in
375 376 377
    // order to prevent BUG: 185466 - disappearing menu popup
    if (_pluggedController)
        emit unplugController(_pluggedController);
Robert Knight's avatar
 
Robert Knight committed
378 379
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
380
void ViewManager::viewActivated(QWidget* view)
381
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
382
    Q_ASSERT(view != 0);
383 384 385 386 387

    // 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);
388 389
}

390
void ViewManager::splitLeftRight()
391
{
392
    splitView(Qt::Horizontal);
393
}
394
void ViewManager::splitTopBottom()
395
{
396
    splitView(Qt::Vertical);
397 398
}

399
void ViewManager::splitView(Qt::Orientation orientation)
400
{
401 402
    ViewContainer* container = createContainer();

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

412
        _sessionMap[display] = session;
413

Kurt Hindenburg's avatar
Kurt Hindenburg committed
414 415
        container->addView(display, properties);
        session->addView(display);
416
    }
417

Kurt Hindenburg's avatar
Kurt Hindenburg committed
418
    _viewSplitter->addContainer(container, orientation);
419
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
420

421 422
    // focus the new container
    container->containerWidget()->setFocus();
423 424

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

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

440
    _viewSplitter->removeContainer(container);
441
    container->deleteLater();
442

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

        removeContainer(container);
460

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
470
    foreach(ViewContainer* container, _viewSplitter->containers()) {
471 472
        if (container != active)
            removeContainer(container);
473 474 475
    }
}

476
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
477
{
Robert Knight's avatar
Robert Knight committed
478 479
    // 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
480
    SessionController* controller = new SessionController(session, view, this);
481 482
    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
483 484
    connect(session , &Konsole::Session::primaryScreenInUse , controller , &Konsole::SessionController::setupPrimaryScreenSpecificActions);
    connect(session , &Konsole::Session::selectionChanged , controller , &Konsole::SessionController::selectionChanged);
485
    connect(view , &Konsole::TerminalDisplay::destroyed , controller , &Konsole::SessionController::deleteLater);
486

487 488 489
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
490

491 492 493
    return controller;
}

494 495
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
496
    if (controller == _pluggedController)
497
        return;
498

499
    _viewSplitter->setFocusProxy(controller->view());
500

501 502
    _pluggedController = controller;
    emit activeViewChanged(controller);
503 504 505 506
}

SessionController* ViewManager::activeViewController() const
{
507
    return _pluggedController;
508
}
509 510 511 512 513 514

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
523 524 525
    TerminalDisplay* display = createTerminalDisplay(session);
    const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    applyProfileToView(display, profile);
526

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
544 545 546
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
547

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
551 552 553 554
    if (container == _viewSplitter->activeContainer()) {
        container->setActiveView(display);
        display->setFocus(Qt::OtherFocusReason);
    }
555

Kurt Hindenburg's avatar
Kurt Hindenburg committed
556
    updateDetachViewState();
557
}
558

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

568
    // new tab will be put at the end by default.
569 570
    int index = -1;

571
    if (_newTabBehavior == PutNewTabAfterCurrentTab) {
572
        QWidget* view = activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
573
        if (view) {
574 575 576
            QList<QWidget*> views =  _viewSplitter->activeContainer()->views();
            index = views.indexOf(view) + 1;
        }
577 578
    }

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

587
ViewContainer* ViewManager::createContainer()
588
{
589 590
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
591 592
    switch (_navigationMethod) {
    case TabbedNavigation: {
593 594
        TabbedViewContainer* tabbedContainer = new TabbedViewContainer(_navigationPosition, this, _viewSplitter);
        container = tabbedContainer;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
595

Laurent Montel's avatar
Laurent Montel committed
596 597
        connect(tabbedContainer, &TabbedViewContainer::detachTab, this, &ViewManager::detachView);
        connect(tabbedContainer, &TabbedViewContainer::closeTab, this, &ViewManager::closeTabFromContainer);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
598 599 600 601 602
    }
    break;
    case NoNavigation:
    default:
        container = new StackedViewContainer(_viewSplitter);
603
    }
604

605 606 607 608 609 610
    // 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
611 612
                               | ViewContainer::QuickNewView
                               | ViewContainer::QuickCloseView);
613 614
    } else {
        container->setFeatures(container->features()
Kurt Hindenburg's avatar
Kurt Hindenburg committed
615 616
                               & ~ViewContainer::QuickNewView
                               & ~ViewContainer::QuickCloseView);
617
    }
618

619
    // connect signals and slots
Laurent Montel's avatar
Laurent Montel committed
620 621
    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
622 623
    _containerSignalMapper->setMapping(container, container);

624 625
    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
626
    connect(container, &Konsole::ViewContainer::moveViewRequest, this , &Konsole::ViewManager::containerMoveViewRequest);
627 628
    connect(container , &Konsole::ViewContainer::viewRemoved , this , &Konsole::ViewManager::viewDestroyed);
    connect(container , &Konsole::ViewContainer::activeViewChanged , this , &Konsole::ViewManager::viewActivated);
629

630
    return container;
631
}
632 633

void ViewManager::containerMoveViewRequest(int index, int id, bool& moved, TabbedViewContainer* sourceTabbedContainer)
634
{
635 636
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
637

638 639
    if (!controller)
        return;
640

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

662 663 664 665 666 667
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

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

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

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

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

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

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

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

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