ViewManager.cpp 36.9 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
24
#include <QtCore/QDateTime>
Dirk Mueller's avatar
Dirk Mueller committed
25
#include <QtCore/QSignalMapper>
Jekyll Wu's avatar
Jekyll Wu committed
26
#include <QtCore/QStringList>
27
#include <QtGui/QMenu>
28

29
// KDE
Robert Knight's avatar
Robert Knight committed
30
#include <KAcceleratorManager>
31
#include <KGlobal>
32 33
#include <KLocale>
#include <KToggleAction>
34
#include <KActionCollection>
35
#include <KXMLGUIFactory>
Laurent Montel's avatar
Laurent Montel committed
36
#include <KConfigGroup>
37 38

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

41
#include "ColorScheme.h"
42
#include "ProfileList.h"
43
#include "Session.h"
44
#include "TerminalDisplay.h"
45 46 47 48
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"
49
#include "Profile.h"
50

51 52
using namespace Konsole;

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

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

78
    // setup actions which are related to the views
79 80 81
    setupActions();

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

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

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

    //prepare DBus communication
    new KonsoleAdaptor(this);
    QDBusConnection::sessionBus().registerObject(QLatin1String("/Konsole"), this);

99 100 101 102
}

ViewManager::~ViewManager()
{
103 104
    delete _newViewMenu;
}
Kurt Hindenburg's avatar
Kurt Hindenburg committed
105
QMenu* ViewManager::createNewViewMenu()
106 107 108 109 110
{
    if (_newViewMenu)
        return _newViewMenu;

    _newViewMenu = new QMenu(0);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111 112 113 114
    ProfileList* newViewProfiles = new ProfileList(false, _newViewMenu);
    newViewProfiles->syncWidgetActions(_newViewMenu, true);
    connect(newViewProfiles, SIGNAL(profileSelected(Profile::Ptr)), this,
            SIGNAL(newViewRequest(Profile::Ptr)));
115 116

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

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

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

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

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

145 146 147 148 149
    // list of actions that should only be enabled when there are multiple view
    // containers open
    QList<QAction*> multiViewOnlyActions;
    multiViewOnlyActions << nextContainerAction;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    if (collection) {
        KAction* splitLeftRightAction = new KAction(KIcon("view-split-left-right"),
                i18nc("@action:inmenu", "Split View Left/Right"),
                this);
        splitLeftRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_ParenLeft));
        collection->addAction("split-view-left-right", splitLeftRightAction);
        connect(splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()));

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

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

        multiViewOnlyActions << closeActiveAction;
172

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

        multiViewOnlyActions << closeOtherAction;
180

181
        KAction* detachViewAction = collection->addAction("detach-view");
182
        detachViewAction->setIcon(KIcon("tab-detach"));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
183
        detachViewAction->setText(i18nc("@action:inmenu", "D&etach Current Tab"));
184 185
        // 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
186 187
        detachViewAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_H));

Kurt Hindenburg's avatar
Kurt Hindenburg committed
188 189
        connect(this , SIGNAL(splitViewToggle(bool)) , this , SLOT(updateDetachViewState()));
        connect(detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()));
190

191
        // Expand & Shrink Active View
Kurt Hindenburg's avatar
Kurt Hindenburg committed
192 193
        KAction* expandActiveAction = new KAction(i18nc("@action:inmenu", "Expand View") , this);
        expandActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_BracketRight));
194
        expandActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
195 196
        collection->addAction("expand-active-view", expandActiveAction);
        connect(expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()));
197

198 199
        multiViewOnlyActions << expandActiveAction;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
200 201
        KAction* shrinkActiveAction = new KAction(i18nc("@action:inmenu", "Shrink View") , this);
        shrinkActiveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_BracketLeft));
202
        shrinkActiveAction->setEnabled(false);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
203 204
        collection->addAction("shrink-active-view", shrinkActiveAction);
        connect(shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()));
205

206 207
        multiViewOnlyActions << shrinkActiveAction;

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

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

229 230
    foreach (QAction* action, multiViewOnlyActions) {
        connect(this , SIGNAL(splitViewToggle(bool)) , action , SLOT(setEnabled(bool)));
231 232
    }

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

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

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

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

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

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

272

273 274
    bool splitView = _viewSplitter->containers().count() >= 2;
    bool shouldEnable = splitView || _viewSplitter->activeContainer()->views().count() >= 2;
275

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

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

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

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

    container->activateNextView();
}

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

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

    container->activatePreviousView();
314
}
315

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

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

    container->activateLastView();
}

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

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

void ViewManager::detachView(ViewContainer* container, QWidget* widgetView)
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
335
    TerminalDisplay * viewToDetach =
336 337 338
        dynamic_cast<TerminalDisplay*>(widgetView);

    if (!viewToDetach)
339 340
        return;

341
    emit viewDetached(_sessionMap[viewToDetach]);
342

343
    _sessionMap.remove(viewToDetach);
344 345

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

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

}

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

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

368 369
    // We're using setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab)
    // so no need to manually select next tab.
370

371 372
    Q_ASSERT(session);

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

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

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

void ViewManager::focusActiveView()
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
391
    // give the active view in a container the focus.  this ensures
Robert Knight's avatar
Robert Knight committed
392 393 394 395 396
    // that controller associated with that view is activated and the session-specific
    // menu items are replaced with the ones for the newly focused view

    // see the viewFocused() method

Kurt Hindenburg's avatar
Kurt Hindenburg committed
397 398
    ViewContainer* container = _viewSplitter->activeContainer();
    if (container) {
Robert Knight's avatar
 
Robert Knight committed
399
        QWidget* activeView = container->activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
400
        if (activeView) {
Robert Knight's avatar
 
Robert Knight committed
401 402 403
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
404 405
}

406

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

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

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

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

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

439
        _sessionMap[display] = session;
440

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

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

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

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

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

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

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

        removeContainer(container);
487

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

497 498 499
    foreach (ViewContainer* container, _viewSplitter->containers()) {
        if (container != active)
            removeContainer(container);
500 501 502
    }
}

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

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

520 521 522
    return controller;
}

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

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

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

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

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

544 545
void ViewManager::createView(Session* session, ViewContainer* container, int index)
{
546
    // notify this view manager when the session finishes so that its view
547
    // can be deleted
548 549
    //
    // TODO - Find a more efficient a way to avoid multiple connections
Kurt Hindenburg's avatar
Kurt Hindenburg committed
550 551
    disconnect(session , SIGNAL(finished()) , this , SLOT(sessionFinished()));
    connect(session , SIGNAL(finished()) , this , SLOT(sessionFinished()));
552

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
557 558
    // set initial size
    display->setSize(80, 40);
559

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
562 563 564
    _sessionMap[display] = session;
    container->addView(display, properties, index);
    session->addView(display);
565

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
569 570 571 572
    if (container == _viewSplitter->activeContainer()) {
        container->setActiveView(display);
        display->setFocus(Qt::OtherFocusReason);
    }
573

Kurt Hindenburg's avatar
Kurt Hindenburg committed
574
    updateDetachViewState();
575
}
576

577
void ViewManager::createView(Session* session)
578
{
579
    // create the default container
Kurt Hindenburg's avatar
Kurt Hindenburg committed
580
    if (_viewSplitter->containers().count() == 0) {
581 582
        ViewContainer* container = createContainer();
        _viewSplitter->addContainer(container, Qt::Vertical);
583
        emit splitViewToggle(false);
584 585
    }

586
    // new tab will be put at the end by default.
587 588
    int index = -1;

589
    if (_newTabBehavior == PutNewTabAfterCurrentTab) {
590
        QWidget* view = activeView();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
591
        if (view) {
592 593 594
            QList<QWidget*> views =  _viewSplitter->activeContainer()->views();
            index = views.indexOf(view) + 1;
        }
595 596
    }

Robert Knight's avatar
Robert Knight committed
597 598
    // 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
599
    // a controller for the session/display pair
600
    foreach ( ViewContainer* container,  _viewSplitter->containers() ) {
601
        createView(session, container, index);
602 603 604
    }
}

605
ViewContainer* ViewManager::createContainer()
606
{
607 608
    ViewContainer::NavigationPosition position =
        static_cast<ViewContainer::NavigationPosition>(_navigationPosition);
609

610 611
    ViewContainer* container = 0;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
612 613 614 615 616 617
    switch (_navigationMethod) {
    case TabbedNavigation: {
        container =
            new TabbedViewContainer(position, _viewSplitter);

        connect(container,
618
                SIGNAL(detachTab(ViewContainer*,QWidget*)),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
619
                this,
620
                SLOT(detachView(ViewContainer*,QWidget*))
Kurt Hindenburg's avatar
Kurt Hindenburg committed
621 622
               );
        connect(container,
623
                SIGNAL(closeTab(ViewContainer*,QWidget*)),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
624
                this,
625
                SLOT(closeTabFromContainer(ViewContainer*,QWidget*)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
626 627 628 629 630 631

    }
    break;
    case NoNavigation:
    default:
        container = new StackedViewContainer(_viewSplitter);
632
    }
633

634
    applyNavigationOptions(container);
635

636
    // connect signals and slots
637
    connect(container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
638 639 640 641 642 643
            SLOT(map()));
    connect(container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
            SLOT(map()));
    _containerSignalMapper->setMapping(container, container);

    connect(container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()));
644 645
    connect(container, SIGNAL(moveViewRequest(int,int,bool&)),
            this , SLOT(containerMoveViewRequest(int,int,bool&)));
Kurt Hindenburg's avatar
Kurt Hindenburg committed
646 647 648
    connect(container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)));
    connect(container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)));
    connect(container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
649

650
    return container;
651
}
652 653
void ViewManager::containerMoveViewRequest(int index, int id, bool& moved)
{
654 655
    ViewContainer* container = qobject_cast<ViewContainer*>(sender());
    SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
656

657 658
    if (!controller)
        return;
659

Kurt Hindenburg's avatar
Kurt Hindenburg committed
660
    createView(controller->session(), container, index);
661
    controller->session()->refresh();
662
    moved = true;
663
}
664 665 666 667 668 669
void ViewManager::setNavigationMethod(NavigationMethod method)
{
    _navigationMethod = method;

    KActionCollection* collection = _actionCollection;

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

680
        const bool enable = (_navigationMethod != NoNavigation) ;
681 682
        QAction* action;

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

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

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

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

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

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

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

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

Kurt Hindenburg's avatar
Kurt Hindenburg committed
709 710 711 712
ViewManager::NavigationMethod ViewManager::navigationMethod() const
{
    return _navigationMethod;
}
713

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

721 722
void ViewManager::viewCloseRequest(QWidget* view)
{
723 724
    //FIXME Check that this cast is actually legal
    TerminalDisplay* display = (TerminalDisplay*)view;
725

726 727
    Q_ASSERT(display);

728 729
    // 1. detach view from session
    // 2. if the session has no views left, close it
730
    Session* session = _sessionMap[ display ];
731
    _sessionMap.remove(display);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
732
    if (session) {
733
        display->deleteLater();
734

Kurt Hindenburg's avatar
Kurt Hindenburg committed
735
        if (session->views().count() == 0)
736
            session->close();
737
    }
738 739 740 741 742
    //we only update the focus if the splitter is still alive
    if (_viewSplitter) {
        focusActiveView();
        updateDetachViewState();
    }
743 744 745 746
    // The below causes the menus  to be messed up
    // Only happenss when using the tab bar close button
//    if (_pluggedController)
//        emit unplugController(_pluggedController);
747 748
}

749
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
750
{
751 752
    TerminalDisplay* display = new TerminalDisplay(0);
    display->setRandomSeed(session->sessionId() * 31);
753

754
    return display;
755 756
}

Jekyll Wu's avatar
Jekyll Wu committed
757
const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr profile) const
758
{
759
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
Kurt Hindenburg's avatar
Kurt Hindenburg committed
760 761 762 763
                                     findColorScheme(profile->colorScheme());
    if (!colorScheme)
        colorScheme = ColorSchemeManager::instance()->defaultColorScheme();
    Q_ASSERT(colorScheme);
764

765 766 767
    return colorScheme;
}

Jekyll Wu's avatar
Jekyll Wu committed
768
void ViewManager::applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile)
769
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
770
    Q_ASSERT(profile);
771

Kurt Hindenburg's avatar
Kurt Hindenburg committed
772
    emit setSaveGeometryOnExitRequest(profile->property<bool>(Profile::SaveGeometryOnExit));
773

774 775
    emit updateWindowIcon();

776
    // load color scheme
777
    ColorEntry table[TABLE_COLORS];
778
    const ColorScheme* colorScheme = colorSchemeForProfile(profile);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
779
    colorScheme->getColorTable(table , view->randomSeed());
780
    view->setColorTable(table);
781
    view->setOpacity(colorScheme->opacity());
782
    view->setWallpaper(colorScheme->wallpaper());
783

Kurt Hindenburg's avatar
Kurt Hindenburg committed
784
    // load font
Jekyll Wu's avatar
Jekyll Wu committed
785 786 787
    view->setAntialias(profile->property<bool>(Profile::AntiAliasFonts));
    view->setBoldIntense(profile->property<bool>(Profile::BoldIntense));
    view->setVTFont(profile->font());
788 789

    // set scroll-bar position
Jekyll Wu's avatar
Jekyll Wu committed
790
    int scrollBarPosition = profile->property<int>(Profile::ScrollBarPosition);
791

Kurt Hindenburg's avatar
Kurt Hindenburg committed
792 793 794 795 796 797
    if (scrollBarPosition == Profile::ScrollBarLeft)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
    else if (scrollBarPosition == Profile::ScrollBarRight)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
    else if (scrollBarPosition == Profile::ScrollBarHidden)
        view->setScrollBarPosition(TerminalDisplay::ScrollBarHidden);
798

799
    // show hint about termianl size after resizing
Jekyll Wu's avatar
Jekyll Wu committed
800
    view->setShowTerminalSizeHint(profile->property<bool>(Profile::ShowTerminalSizeHint));