ViewManager.cpp 40.4 KB
Newer Older
1
/*
2
    Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20 21 22
// Own
#include "ViewManager.h"

23 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, Konsole::ACCEL + 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, Konsole::ACCEL + 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, Konsole::ACCEL + Qt::SHIFT + Qt::Key_X);
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, Konsole::ACCEL + 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, Konsole::ACCEL + 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, Konsole::ACCEL + 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, Konsole::ACCEL + 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;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
212
        auto 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 240 241 242 243
#ifdef Q_OS_OSX
    collection->setDefaultShortcut(moveViewLeftAction, Konsole::ACCEL + Qt::SHIFT + Qt::Key_BracketLeft);
#else
    collection->setDefaultShortcut(moveViewLeftAction, Konsole::ACCEL + Qt::SHIFT + Qt::Key_Left);
#endif
244
    connect(moveViewLeftAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewLeft);
245
    _viewSplitter->addAction(moveViewLeftAction);
246

247 248 249 250 251
#ifdef Q_OS_OSX
    collection->setDefaultShortcut(moveViewRightAction, Konsole::ACCEL + Qt::SHIFT + Qt::Key_BracketRight);
#else
    collection->setDefaultShortcut(moveViewRightAction, Konsole::ACCEL + Qt::SHIFT + Qt::Key_Right);
#endif
252
    connect(moveViewRightAction , &QAction::triggered , this , &Konsole::ViewManager::moveActiveViewRight);
253
    _viewSplitter->addAction(moveViewRightAction);
254

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

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

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

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

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

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

    container->activateNextView();
}

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

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

    container->activatePreviousView();
315
}
316

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

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

    container->activateLastView();
}

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

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

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

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

    if (!viewToDetach)
343 344
        return;

345
    emit viewDetached(_sessionMap[viewToDetach]);
346

347
    _sessionMap.remove(viewToDetach);
348 349

    // remove the view from this window
350 351
    container->removeView(viewToDetach);
    viewToDetach->deleteLater();
352 353 354 355

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

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

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

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

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

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

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

    // 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);
396 397
}

398
void ViewManager::splitLeftRight()
399
{
400
    splitView(Qt::Horizontal);
401
}
402
void ViewManager::splitTopBottom()
403
{
404
    splitView(Qt::Vertical);
405 406
}

407
void ViewManager::splitView(Qt::Orientation orientation)
408
{
409 410
    ViewContainer* container = createContainer();

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

420
        _sessionMap[display] = session;
421

Kurt Hindenburg's avatar
Kurt Hindenburg committed
422 423
        container->addView(display, properties);
        session->addView(display);
424
    }
425

Kurt Hindenburg's avatar
Kurt Hindenburg committed
426
    _viewSplitter->addContainer(container, orientation);
427
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
428

429 430
    // focus the new container
    container->containerWidget()->setFocus();
431 432

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

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

448
    _viewSplitter->removeContainer(container);
449
    container->deleteLater();
450

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

        removeContainer(container);
468

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

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

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

495 496 497
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
498

499 500 501
    return controller;
}

502 503
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
504
    if (controller == _pluggedController)
505
        return;
506

507
    _viewSplitter->setFocusProxy(controller->view());
508

509 510
    _pluggedController = controller;
    emit activeViewChanged(controller);
511 512 513 514
}

SessionController* ViewManager::activeViewController() const
{
515
    return _pluggedController;
516
}
517 518 519 520 521 522

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

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
552 553 554
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
555

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
564
    updateDetachViewState();
565
}
566

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

576
    // new tab will be put at the end by default.
577 578
    int index = -1;

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

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

595
ViewContainer* ViewManager::createContainer()
596
{
597 598
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
599 600
    switch (_navigationMethod) {
    case TabbedNavigation: {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
601
        auto tabbedContainer = new TabbedViewContainer(_navigationPosition, this, _viewSplitter);
602
        container = tabbedContainer;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
603

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

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

627
    // connect signals and slots
Laurent Montel's avatar
Laurent Montel committed
628 629
    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
630 631
    _containerSignalMapper->setMapping(container, container);

632 633
    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
634
    connect(container, &Konsole::ViewContainer::moveViewRequest, this , &Konsole::ViewManager::containerMoveViewRequest);
635 636
    connect(container , &Konsole::ViewContainer::viewRemoved , this , &Konsole::ViewManager::viewDestroyed);
    connect(container , &Konsole::ViewContainer::activeViewChanged , this , &Konsole::ViewManager::viewActivated);
637

638
    return container;
639
}
640 641

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

646 647
    if (!controller)
        return;
648

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

670 671 672 673 674 675
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

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

686
        const bool enable = (_navigationMethod != NoNavigation);
687 688
        QAction* action;

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
701 702
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
703

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
715 716 717 718
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
719

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

727
void ViewManager::viewDestroyed(QWidget* view)
728
{
729 730
    // Note: the received QWidget has already been destroyed, so
    // using dynamic_cast<> or qobject_cast<> does not work here
731 732
    // We only need the pointer address to look it up below
    TerminalDisplay* display = reinterpret_cast<TerminalDisplay*>(view);
733

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