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

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

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

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

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

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

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

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

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

51 52
using namespace Konsole;

53 54
int ViewManager::lastManagerId = 0;

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

72 73 74 75 76
    // the ViewSplitter class supports both recursive and non-recursive splitting,
    // in non-recursive mode, all containers are inserted into the same top-level splitter
    // widget, and all the divider lines between the containers have the same orientation
    //
    // the ViewManager class is not currently able to handle a ViewSplitter in recursive-splitting
Kurt Hindenburg's avatar
Kurt Hindenburg committed
77
    // mode
78
    _viewSplitter->setRecursiveSplitting(false);
79
    _viewSplitter->setFocusPolicy(Qt::NoFocus);
80

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

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

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

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

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

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

ViewManager::~ViewManager()
{
109
}
110 111 112 113 114 115

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
135 136 137 138
    KAction* nextViewAction = new KAction(i18nc("@action Shortcut entry", "Next Tab") , this);
    KAction* previousViewAction = new KAction(i18nc("@action Shortcut entry", "Previous Tab") , this);
    KAction* lastViewAction = new KAction(i18nc("@action Shortcut entry", "Switch to Last Tab") , this);
    KAction* nextContainerAction = new KAction(i18nc("@action Shortcut entry", "Next View Container") , this);
139

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

143 144 145 146 147
    // 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    if (collection) {
        KAction* splitLeftRightAction = new KAction(KIcon("view-split-left-right"),
                i18nc("@action:inmenu", "Split View Left/Right"),
                this);
        splitLeftRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_ParenLeft));
        collection->addAction("split-view-left-right", splitLeftRightAction);
        connect(splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()));

        KAction* splitTopBottomAction = new KAction(KIcon("view-split-top-bottom") ,
                i18nc("@action:inmenu", "Split View Top/Bottom"), this);
        splitTopBottomAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_ParenRight));
        collection->addAction("split-view-top-bottom", splitTopBottomAction);
        connect(splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));

        KAction* closeActiveAction = new KAction(i18nc("@action:inmenu Close Active View", "Close Active") , this);
Pino Toscano's avatar
Pino Toscano committed
163
        closeActiveAction->setIcon(KIcon("view-close"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
164
        closeActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S));
165
        closeActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
166
        collection->addAction("close-active-view", closeActiveAction);
167
        connect(closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveContainer()));
168 169

        multiViewOnlyActions << closeActiveAction;
170

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

        multiViewOnlyActions << closeOtherAction;
178

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

186 187
        multiViewOnlyActions << expandActiveAction;

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

194 195
        multiViewOnlyActions << shrinkActiveAction;

Alex Richardson's avatar
Alex Richardson committed
196
        KAction* detachViewAction = collection->addAction("detach-view");
197 198 199 200 201 202 203 204 205
        detachViewAction->setIcon(KIcon("tab-detach"));
        detachViewAction->setText(i18nc("@action:inmenu", "D&etach Current Tab"));
        // Ctrl+Shift+D is not used as a shortcut by default because it is too close
        // to Ctrl+D - which will terminate the session in many cases
        detachViewAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_H));

        connect(this , SIGNAL(splitViewToggle(bool)) , this , SLOT(updateDetachViewState()));
        connect(detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()));

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

        // Switch to tab N shortcuts
215
        const int SWITCH_TO_TAB_COUNT = 19;
216
        QSignalMapper* switchToTabMapper = new QSignalMapper(this);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
217 218 219 220 221
        connect(switchToTabMapper, SIGNAL(mapped(int)), this, SLOT(switchToView(int)));
        for (int i = 0; i < SWITCH_TO_TAB_COUNT; i++) {
            KAction* switchToTabAction = new KAction(i18nc("@action Shortcut entry", "Switch to Tab %1", i + 1), this);
            switchToTabMapper->setMapping(switchToTabAction, i);
            connect(switchToTabAction, SIGNAL(triggered()), switchToTabMapper,
222
                    SLOT(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 , SIGNAL(splitViewToggle(bool)) , action , SLOT(setEnabled(bool)));
229 230
    }

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
236 237
    previousViewAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Left));
    connect(previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()));
238
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
239

Kurt Hindenburg's avatar
Kurt Hindenburg committed
240 241
    nextContainerAction->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Tab));
    connect(nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()));
242
    _viewSplitter->addAction(nextContainerAction);
243

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
252
    connect(lastViewAction, SIGNAL(triggered()) , this , SLOT(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
    TerminalDisplay * viewToDetach = qobject_cast<TerminalDisplay*>(widgetView);
334 335

    if (!viewToDetach)
336 337
        return;

338
    emit viewDetached(_sessionMap[viewToDetach]);
339

340
    _sessionMap.remove(viewToDetach);
341 342

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

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

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

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

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

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

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

void ViewManager::focusActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
383
    // give the active view in a container the focus.  this ensures
Robert Knight's avatar
Robert Knight committed
384 385 386 387 388
    // that controller associated with that view is activated and the session-specific
    // menu items are replaced with the ones for the newly focused view

    // see the viewFocused() method

Kurt Hindenburg's avatar
Kurt Hindenburg committed
389 390
    ViewContainer* container = _viewSplitter->activeContainer();
    if (container) {
Robert Knight's avatar
 
Robert Knight committed
391
        QWidget* activeView = container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
392
        if (activeView) {
Robert Knight's avatar
 
Robert Knight committed
393 394 395
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
396 397
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
398
void ViewManager::viewActivated(QWidget* view)
399
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
400
    Q_ASSERT(view != 0);
401 402 403 404 405

    // 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);
406 407
}

408
void ViewManager::splitLeftRight()
409
{
410
    splitView(Qt::Horizontal);
411
}
412
void ViewManager::splitTopBottom()
413
{
414
    splitView(Qt::Vertical);
415 416
}

417
void ViewManager::splitView(Qt::Orientation orientation)
418
{
419 420
    ViewContainer* container = createContainer();

421
    // iterate over each session which has a view in the current active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
422
    // container and create a new view for that session in a new container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
423
    foreach(QWidget* view,  _viewSplitter->activeContainer()->views()) {
424
        Session* session = _sessionMap[qobject_cast<TerminalDisplay*>(view)];
425
        TerminalDisplay* display = createTerminalDisplay(session);
Jekyll Wu's avatar
Jekyll Wu committed
426 427
        const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
        applyProfileToView(display, profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
428
        ViewProperties* properties = createController(session, display);
429

430
        _sessionMap[display] = session;
431

Kurt Hindenburg's avatar
Kurt Hindenburg committed
432 433
        container->addView(display, properties);
        session->addView(display);
434
    }
435

Kurt Hindenburg's avatar
Kurt Hindenburg committed
436
    _viewSplitter->addContainer(container, orientation);
437
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
438

439 440
    // focus the new container
    container->containerWidget()->setFocus();
441 442

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
446
    if (activeView)
447
        activeView->setFocus(Qt::OtherFocusReason);
448
}
449
void ViewManager::removeContainer(ViewContainer* container)
450
{
451
    // remove session map entries for views in this container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
452
    foreach(QWidget* view , container->views()) {
453 454 455
        TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
        Q_ASSERT(display);
        _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
456
    }
457

458
    _viewSplitter->removeContainer(container);
459
    container->deleteLater();
460

Kurt Hindenburg's avatar
Kurt Hindenburg committed
461
    emit splitViewToggle(_viewSplitter->containers().count() > 1);
462
}
463
void ViewManager::expandActiveContainer()
464
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
465
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), 10);
466
}
467
void ViewManager::shrinkActiveContainer()
468
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
469
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(), -10);
470
}
471
void ViewManager::closeActiveContainer()
472 473
{
    // only do something if there is more than one container active
Kurt Hindenburg's avatar
Kurt Hindenburg committed
474
    if (_viewSplitter->containers().count() > 1) {
475 476 477
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
478

Kurt Hindenburg's avatar
Kurt Hindenburg committed
479
        // focus next container so that user can continue typing
480 481 482
        // without having to manually focus it themselves
        nextContainer();
    }
483
}
484
void ViewManager::closeOtherContainers()
485 486 487
{
    ViewContainer* active = _viewSplitter->activeContainer();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
488
    foreach(ViewContainer* container, _viewSplitter->containers()) {
489 490
        if (container != active)
            removeContainer(container);
491 492 493
    }
}

494
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
495
{
Robert Knight's avatar
Robert Knight committed
496 497
    // 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
498 499 500 501 502
    SessionController* controller = new SessionController(session, view, this);
    connect(controller , SIGNAL(focused(SessionController*)) , this , SLOT(controllerChanged(SessionController*)));
    connect(session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
    connect(session , SIGNAL(primaryScreenInUse(bool)) ,
            controller , SLOT(setupPrimaryScreenSpecificActions(bool)));
503
    connect(session , SIGNAL(selectionChanged(QString)) ,
Jekyll Wu's avatar
Jekyll Wu committed
504
            controller , SLOT(selectionChanged(QString)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
505
    connect(view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()));
506

507 508 509
    // if this is the first controller created then set it as the active controller
    if (!_pluggedController)
        controllerChanged(controller);
510

511 512 513
    return controller;
}

514 515
void ViewManager::controllerChanged(SessionController* controller)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
516
    if (controller == _pluggedController)
517
        return;
518

519
    _viewSplitter->setFocusProxy(controller->view());
520

521 522
    _pluggedController = controller;
    emit activeViewChanged(controller);
523 524 525 526
}

SessionController* ViewManager::activeViewController() const
{
527
    return _pluggedController;
528
}
529 530 531 532 533 534

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

535 536
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
537
    // notify this view manager when the session finishes so that its view
538
    // can be deleted
539
    //
540 541
    // Use Qt::UniqueConnection to avoid duplicate connection
    connect(session, SIGNAL(finished()), this, SLOT(sessionFinished()), Qt::UniqueConnection);
542

Kurt Hindenburg's avatar
Kurt Hindenburg committed
543 544 545
    TerminalDisplay* display = createTerminalDisplay(session);
    const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
    applyProfileToView(display, profile);
546

Kurt Hindenburg's avatar
Kurt Hindenburg committed
547
    // set initial size
548 549 550
    const QSize& preferredSize = session->preferredSize();
    // FIXME: +1 is needed here for getting the expected rows
    display->setSize(preferredSize.width(), preferredSize.height() + 1);
551

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
566
    updateDetachViewState();
567
}
568

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

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

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

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

597
ViewContainer* ViewManager::createContainer()
598
{
599 600
    ViewContainer* container = 0;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
605 606
        connect(container, SIGNAL(detachTab(ViewContainer*,QWidget*)),
                this, SLOT(detachView(ViewContainer*,QWidget*))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
607
               );
Kurt Hindenburg's avatar
Kurt Hindenburg committed
608 609
        connect(container, SIGNAL(closeTab(ViewContainer*,QWidget*)),
                this, SLOT(closeTabFromContainer(ViewContainer*,QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
610 611 612 613 614
    }
    break;
    case NoNavigation:
    default:
        container = new StackedViewContainer(_viewSplitter);
615
    }
616

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

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

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

645
    return container;
646
}
647 648

void ViewManager::containerMoveViewRequest(int index, int id, bool& moved, TabbedViewContainer* sourceTabbedContainer)
649
{
650 651
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
652

653 654
    if (!controller)
        return;
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    // do not move the last tab in a splitted view.
    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
672
    createView(controller->session(), container, index);
673
    controller->session()->refresh();
674
    moved = true;
675
}
676

677 678 679 680 681 682
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
683
    if (collection) {
684 685 686 687 688 689 690 691 692
        // 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.

693
        const bool enable = (_navigationMethod != NoNavigation);
694 695
        QAction* action;

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
699 700
        action = collection->action("previous-view");
        if (action) action->setEnabled(enable);
701

Kurt Hindenburg's avatar
Kurt Hindenburg committed
702 703
        action = collection->action("last-tab");
        if (action) action->setEnabled(enable);
704

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
708 709
        action = collection->action("split-view-top-bottom");
        if (action) action->setEnabled(enable);
710

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
717 718
        action = collection->action("move-view-right");
        if (action) action->setEnabled(enable);
719 720 721
    }
}

Kurt Hindenburg's avatar
Kurt Hindenburg committed
722 723 724 725
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
726

727 728
void ViewManager::containerViewsChanged(QObject* container)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
729 730 731
    if (_viewSplitter && container == _viewSplitter->activeContainer()) {
        emit viewPropertiesChanged(viewProperties());
    }
732 733
}

734
void ViewManager::viewDestroyed(QWidget* view)
735
{
736 737 738
    // 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);
739 740
    Q_ASSERT(display);

741 742
    // 1. detach view from session
    // 2. if the session has no views left, close it
743
    Session* session = _sessionMap[ display ];
744
    _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
745
    if (session) {
746
        display->deleteLater();
747

Kurt Hindenburg's avatar
Kurt Hindenburg committed
748
        if (session->views().count() == 0)
749
            session->close();
750
    }
751 752 753 754 755
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        focusActiveView();
        updateDetachViewState();
    }
756
    // The below causes the menus  to be messed up
757
    // Only happens when using the tab bar close button
758 759
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
760 761
}

762
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
763
{
764 765
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
766

767
    return display;
768 769
}

770
const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr profile)
771
{
772
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
Kurt Hindenburg's avatar
Kurt Hindenburg committed
773 774 775 776
                                     findColorScheme(profile->colorScheme());
    if (!colorScheme)
        colorScheme = ColorSchemeManager::instance()->defaultColorScheme();
    Q_ASSERT(colorScheme);
777

778 779 780
    return colorScheme;
}

Jekyll Wu's avatar
Jekyll Wu committed
781
void ViewManager::applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile)
782
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
783
    Q_ASSERT(profile);
784

785
    emit setSaveGeometryOnExitRequest(profile->saveGeometryOnExit());
786

787 788
    emit updateWindowIcon();