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

    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 25
// System
#include <assert.h>

26
// Qt
27
#include <QtCore/QDateTime>
Dirk Mueller's avatar
Dirk Mueller committed
28
#include <QtCore/QSignalMapper>
29

30 31
// KDE
#include <kdebug.h>
32
#include <KGlobal>
33 34 35 36 37
#include <KLocale>
#include <KToggleAction>
#include <KXMLGUIFactory>

// Konsole
38
#include "ColorScheme.h"
39
#include "Session.h"
40
#include "TerminalDisplay.h"
41 42 43 44 45
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"

46 47
using namespace Konsole;

48 49
ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
    : QObject(parent)
50
    , _viewSplitter(0)
51 52
    , _actionCollection(collection)
    , _containerSignalMapper(new QSignalMapper(this))
53
    , _navigationMethod(TabbedNavigation)
54
{
55 56 57 58 59 60 61 62 63
    // create main view area
    _viewSplitter = new ViewSplitter(0);   
    // 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
    // mode 
    _viewSplitter->setRecursiveSplitting(false);
64
    _viewSplitter->setFocusPolicy(Qt::NoFocus);
65

66 67 68 69 70 71
    // setup actions which relating to the view
    setupActions();

    // emit a signal when all of the views held by this view manager are destroyed
    connect( _viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()) );
    connect( _viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()) );
72 73 74 75

    // listen for addition or removal of views from associated containers
    connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this , 
            SLOT(containerViewsChanged(QObject*)) ); 
76 77 78 79

    // listen for profile changes
    connect( SessionManager::instance() , SIGNAL(profileChanged(const QString&)) , this,
            SLOT(profileChanged(const QString&)) );
80 81
    connect( SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
            SLOT(updateViewsForSession(Session*)) );
82 83 84 85 86
}

ViewManager::~ViewManager()
{
}
87 88 89 90 91 92 93 94 95 96 97 98
QWidget* ViewManager::activeView() const
{
    ViewContainer* container = _viewSplitter->activeContainer();
    if ( container )
    {
        return container->activeView();
    }
    else
    {
        return 0;
    }
}
99

100 101 102 103 104
QWidget* ViewManager::widget() const
{
    return _viewSplitter;
}

105 106
void ViewManager::setupActions()
{
107
    KActionCollection* collection = _actionCollection;
108

109 110 111
    KAction* nextViewAction = new KAction( i18n("Next View") , this );
    KAction* previousViewAction = new KAction( i18n("Previous View") , this );
    QAction* nextContainerAction = new QAction( i18n("Next View Container") , this);
112 113 114 115
  
    QAction* moveViewLeftAction = new QAction( i18n("Move View Left") , this );
    QAction* moveViewRightAction = new QAction( i18n("Move View Right") , this );

116 117 118 119 120
    // list of actions that should only be enabled when there are multiple view
    // containers open
    QList<QAction*> multiViewOnlyActions;
    multiViewOnlyActions << nextContainerAction;

121 122 123
    if ( collection )
    {
        KAction* splitLeftRightAction = new KAction( KIcon("view-left-right"),
Thomas Reitelbach's avatar
Thomas Reitelbach committed
124
                                                      i18nc("@action:inmenu", "Split View Left/Right"),
125 126 127 128 129 130
                                                      this );
        splitLeftRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_L) );
        collection->addAction("split-view-left-right",splitLeftRightAction);
        connect( splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()) );

        KAction* splitTopBottomAction = new KAction( KIcon("view-top-bottom") , 
Thomas Reitelbach's avatar
Thomas Reitelbach committed
131
                                             i18nc("@action:inmenu", "Split View Top/Bottom"),this);
132 133 134 135
        splitTopBottomAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_T) );
        collection->addAction("split-view-top-bottom",splitTopBottomAction);
        connect( splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));

Thomas Reitelbach's avatar
Thomas Reitelbach committed
136
        KAction* closeActiveAction = new KAction( i18nc("@action:inmenu Close Active View", "Close Active") , this );
Pino Toscano's avatar
Pino Toscano committed
137
        closeActiveAction->setIcon(KIcon("view-close"));
138
        closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
139
        closeActiveAction->setEnabled(false);
140 141
        collection->addAction("close-active-view",closeActiveAction);
        connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
142 143 144
      
        multiViewOnlyActions << closeActiveAction; 

Thomas Reitelbach's avatar
Thomas Reitelbach committed
145
        KAction* closeOtherAction = new KAction( i18nc("@action:inmenu Close Other Views", "Close Others") , this );
146
        closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
147
        closeOtherAction->setEnabled(false);
148 149
        collection->addAction("close-other-views",closeOtherAction);
        connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
150 151

        multiViewOnlyActions << closeOtherAction;
152

153
        KAction* detachViewAction = collection->addAction("detach-view");
Pino Toscano's avatar
Pino Toscano committed
154
        detachViewAction->setIcon( KIcon("tab-detach") );
155 156 157 158
        detachViewAction->setText( i18n("&Detach View") );
        // 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) );
159 160
        
        multiViewOnlyActions << detachViewAction;
161 162

        connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
163 164
   
        // Expand & Shrink Active View
Thomas Reitelbach's avatar
Thomas Reitelbach committed
165
        KAction* expandActiveAction = new KAction( i18nc("@action:inmenu", "Expand View") , this );
166 167 168 169
        expandActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketRight) );
        collection->addAction("expand-active-view",expandActiveAction);
        connect( expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()) );

170 171
        multiViewOnlyActions << expandActiveAction;

Thomas Reitelbach's avatar
Thomas Reitelbach committed
172
        KAction* shrinkActiveAction = new KAction( i18nc("@action:inmenu", "Shrink View") , this );
173 174 175 176
        shrinkActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketLeft) );
        collection->addAction("shrink-active-view",shrinkActiveAction);
        connect( shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()) );

177 178
        multiViewOnlyActions << shrinkActiveAction;

179
        // Next / Previous View , Next Container
180 181 182
        collection->addAction("next-view",nextViewAction);
        collection->addAction("previous-view",previousViewAction);
        collection->addAction("next-container",nextContainerAction);
183 184
        collection->addAction("move-view-left",moveViewLeftAction);
        collection->addAction("move-view-right",moveViewRightAction);
185
    }
Robert Knight's avatar
 
Robert Knight committed
186

187 188 189 190 191 192
    QListIterator<QAction*> iter(multiViewOnlyActions);
    while ( iter.hasNext() )
    {
        connect( this , SIGNAL(splitViewToggle(bool)) , iter.next() , SLOT(setEnabled(bool)) );
    }

193
    // keyboard shortcut only actions
194
    KShortcut nextViewShortcut = nextViewAction->shortcut();
195 196
    nextViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
    nextViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageUp) );
197
    nextViewAction->setShortcut(nextViewShortcut); 
Robert Knight's avatar
 
Robert Knight committed
198
    connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
199
    _viewSplitter->addAction(nextViewAction);
Robert Knight's avatar
 
Robert Knight committed
200

201 202 203 204
    KShortcut previousViewShortcut = previousViewAction->shortcut();
    previousViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Left) );
    previousViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageDown) );
    previousViewAction->setShortcut(previousViewShortcut);
Robert Knight's avatar
 
Robert Knight committed
205
    connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
206
    _viewSplitter->addAction(previousViewAction);
Robert Knight's avatar
 
Robert Knight committed
207 208 209

    nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
    connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
210
    _viewSplitter->addAction(nextContainerAction);
211 212 213 214 215 216 217

    moveViewLeftAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Left) );
    connect( moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()) );
    _viewSplitter->addAction(moveViewLeftAction);
    moveViewRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Right) );
    connect( moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()) );
    _viewSplitter->addAction(moveViewRightAction);
Robert Knight's avatar
 
Robert Knight committed
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233
void ViewManager::moveActiveViewLeft()
{
    qDebug() << "Moving active view to the left";
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewLeft );
}
void ViewManager::moveActiveViewRight()
{
    qDebug() << "Moving active view to the right";
    ViewContainer* container = _viewSplitter->activeContainer();
    Q_ASSERT( container );
    container->moveActiveView( ViewContainer::MoveViewRight );
}
Robert Knight's avatar
 
Robert Knight committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
void ViewManager::nextContainer()
{
    _viewSplitter->activateNextContainer();
}

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

    Q_ASSERT( container );

    container->activateNextView();
}

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

    Q_ASSERT( container );

    container->activatePreviousView();
255 256 257 258 259
}
void ViewManager::detachActiveView()
{
    // find the currently active view and remove it from its container 
    ViewContainer* container = _viewSplitter->activeContainer();
260
    TerminalDisplay* activeView = dynamic_cast<TerminalDisplay*>(container->activeView());
261 262 263 264 265 266 267 268 269 270

    if (!activeView)
        return;

    emit viewDetached(_sessionMap[activeView]);
    
    _sessionMap.remove(activeView);

    // remove the view from this window
    container->removeView(activeView);
271
    activeView->deleteLater();
272 273 274 275 276 277 278

    // 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
    if ( _viewSplitter->containers().count() > 1 && 
         container->views().count() == 0 )
    {
279
        removeContainer(container);
280 281 282 283
    }

}

284
void ViewManager::sessionFinished()
285
{
286 287
    Session* session = qobject_cast<Session*>(sender());

288 289 290 291 292 293 294 295
    if ( _sessionMap[qobject_cast<TerminalDisplay*>(activeView())] == session )
    {
        // switch to the previous view before deleting the session views to prevent flicker 
        // occurring as a result of an interval between removing the active view and switching
        // to the previous view
        previousView();
    }

296 297
    Q_ASSERT(session);

298
    // close attached views
299
    QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
300

301
    foreach ( TerminalDisplay* view , children )
302 303 304 305
    {
        if ( _sessionMap[view] == session )
        {
            _sessionMap.remove(view);
306
            view->deleteLater();
307
        }
Robert Knight's avatar
 
Robert Knight committed
308
    }
309

Robert Knight's avatar
 
Robert Knight committed
310 311 312 313
}

void ViewManager::focusActiveView()
{
Robert Knight's avatar
Robert Knight committed
314 315 316 317 318 319
    // give the active view in a container the focus.  this ensures 
    // 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

Robert Knight's avatar
 
Robert Knight committed
320 321 322 323 324 325 326 327 328
    ViewContainer* container = _viewSplitter->activeContainer(); 
    if ( container )
    {
        QWidget* activeView = container->activeView();
        if ( activeView )
        {
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
329 330
}

331

332 333
void ViewManager::viewActivated( QWidget* view )
{
334 335 336 337 338 339
    Q_ASSERT( view != 0 );

    // 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);
340 341
}

342
void ViewManager::splitLeftRight()
343
{
344
    splitView(Qt::Horizontal);
345
}
346
void ViewManager::splitTopBottom()
347
{
348
    splitView(Qt::Vertical);
349 350
}

351
void ViewManager::splitView(Qt::Orientation orientation)
352
{
353 354 355 356
    // iterate over each session which has a view in the current active
    // container and create a new view for that session in a new container 
    QListIterator<QWidget*> existingViewIter(_viewSplitter->activeContainer()->views());
    
357
    ViewContainer* container = 0; 
358 359

    while (existingViewIter.hasNext())
360
    {
361
        Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
362
        TerminalDisplay* display = createTerminalDisplay(session);
363
        applyProfile(display,session->profileKey()); 
364
        ViewProperties* properties = createController(session,display);
365

366
        _sessionMap[display] = session;
367

368 369 370 371 372
        // create a container using settings from the first 
        // session in the previous container
        if ( !container )
            container = createContainer(session->profileKey());

373 374 375
        container->addView(display,properties);
        session->addView( display );
    }
376

377 378
    _viewSplitter->addContainer(container,orientation);
    emit splitViewToggle(_viewSplitter->containers().count() > 0);
379

380 381
    // focus the new container
    container->containerWidget()->setFocus();
382 383

    // ensure that the active view is focused after the split / unsplit
384 385 386 387 388
    ViewContainer* activeContainer = _viewSplitter->activeContainer();
    QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;

    if ( activeView )
        activeView->setFocus(Qt::OtherFocusReason);
389
}
390
void ViewManager::removeContainer(ViewContainer* container)
391
{
392 393 394 395 396
    // note that the _viewSplitter->containers().count() will not be updated
    // until the container is deleted when Qt returns to the main event loop,
    // so we take the previous container count and work out what the new count
    // would be.
    int previousCount = _viewSplitter->containers().count();
397 398 399 400 401 402 403 404 405

    // remove session map entries for views in this container
    foreach( QWidget* view , container->views() )
    {
        TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
        Q_ASSERT(display);
        _sessionMap.remove(display);
    } 

406
    container->deleteLater();
407
    emit splitViewToggle( (previousCount-1) > 1);
408
}
409 410 411 412 413 414 415 416
void ViewManager::expandActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),10);
}
void ViewManager::shrinkActiveView()
{
    _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),-10);
}
417 418 419 420 421 422 423 424
void ViewManager::closeActiveView()
{
    // only do something if there is more than one container active
    if ( _viewSplitter->containers().count() > 1 )
    {
        ViewContainer* container = _viewSplitter->activeContainer();

        removeContainer(container);
425

426 427 428 429
        // focus next container so that user can continue typing 
        // without having to manually focus it themselves
        nextContainer();
    }
430 431 432 433 434 435 436 437 438 439
}
void ViewManager::closeOtherViews()
{
    ViewContainer* active = _viewSplitter->activeContainer();

    QListIterator<ViewContainer*> iter(_viewSplitter->containers());
    while ( iter.hasNext() )
    {
        ViewContainer* next = iter.next();
        if ( next != active )
440
            removeContainer(next);
441 442 443
    }
}

444
SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
445
{
Robert Knight's avatar
Robert Knight committed
446 447
    // create a new controller for the session, and ensure that this view manager
    // is notified when the view gains the focus
448
    SessionController* controller = new SessionController(session,view,this);
449
    connect( controller , SIGNAL(focused(SessionController*)) , this , SIGNAL(activeViewChanged(SessionController*)) );
450 451
    connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
    connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
452 453
    connect( controller , SIGNAL(sendInputToAll(bool)) , this , SLOT(sendInputToAll()) );

454 455 456
    return controller;
}

457
void ViewManager::createView(Session* session)
458
{
459 460 461
    // create the default container
    if (_viewSplitter->containers().count() == 0)
    {
462 463
        _viewSplitter->addContainer( createContainer(session->profileKey()) , 
                                     Qt::Vertical );
464
        emit splitViewToggle(false);
465 466
    }

Robert Knight's avatar
Robert Knight committed
467 468
    // notify this view manager when the session finishes so that its view
    // can be deleted
469
    connect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
Robert Knight's avatar
Robert Knight committed
470 471 472 473
   
    // 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
    // a controller for the session/display pair 
474 475 476 477 478 479
    ViewContainer* const activeContainer = _viewSplitter->activeContainer();
    QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());

    while ( containerIter.hasNext() )
    {
        ViewContainer* container = containerIter.next();
480
        TerminalDisplay* display = createTerminalDisplay(session);
481
        applyProfile(display,session->profileKey());
482 483
        
        // set initial size
484 485
        display->setSize(80,40);

Robert Knight's avatar
 
Robert Knight committed
486
        ViewProperties* properties = createController(session,display);
487 488

        _sessionMap[display] = session; 
Robert Knight's avatar
 
Robert Knight committed
489
        container->addView(display,properties);
490 491
        session->addView(display);

492 493 494
        // tell the session whether it has a light or dark background
        session->setDarkBackground( colorSchemeForProfile(session->profileKey())->hasDarkBackground() );

495 496
        if ( container == activeContainer ) 
        {
497
            container->setActiveView(display);
498 499
            display->setFocus( Qt::OtherFocusReason );
        }
500 501 502
    }
}

503
ViewContainer* ViewManager::createContainer(const QString& profileKey)
504
{
505 506 507 508 509 510 511 512 513 514
    const Profile* info = SessionManager::instance()->profile(profileKey);

    Q_ASSERT( info );

    const int tabPosition = info->property(Profile::TabBarPosition).value<int>();

    ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ?
                                                   ViewContainer::NavigationPositionTop :
                                                   ViewContainer::NavigationPositionBottom;

515 516 517 518 519 520 521 522 523 524 525
    ViewContainer* container = 0;

    switch ( _navigationMethod )
    {
        case TabbedNavigation:    
            container = new TabbedViewContainerV2(position,_viewSplitter);
            break;
        case NoNavigation:
        default:
            container = new StackedViewContainer(_viewSplitter);
    }
526

527
    // connect signals and slots
528 529 530 531 532 533
    connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
           SLOT(map()) );
    connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
           SLOT(map()) ); 
    _containerSignalMapper->setMapping(container,container);

534
    connect( container, SIGNAL(newViewRequest()), this , SIGNAL(newViewRequest()) );
535
    connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
536
    connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
537
    connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
538
    
539
    return container;
540 541
}

542 543 544
void ViewManager::setNavigationMethod(NavigationMethod method) { _navigationMethod = method; }
ViewManager::NavigationMethod ViewManager::navigationMethod() const { return _navigationMethod; }

545 546
void ViewManager::containerViewsChanged(QObject* container)
{
Robert Knight's avatar
 
Robert Knight committed
547
    //qDebug() << "Container views changed";
548 549 550 551 552 553 554

    if ( container == _viewSplitter->activeContainer() )
    {
        emit viewPropertiesChanged( viewProperties() );
    } 
}

555 556
void ViewManager::viewCloseRequest(QWidget* view)
{
557 558 559 560 561
    //FIXME Check that this cast is actually legal
    TerminalDisplay* display = (TerminalDisplay*)view;
  
    Q_ASSERT(display);

562 563
    // 1. detach view from session
    // 2. if the session has no views left, close it
564
    Session* session = _sessionMap[ display ];
565
    _sessionMap.remove(display);
566 567
    if ( session )
    {
568
        display->deleteLater();
569

570
        if ( session->views().count() == 0 )
571
            session->close();
572
    }
573
        
574
    focusActiveView();
575 576
}

577
TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
578
{
579
   TerminalDisplay* display = new TerminalDisplay(0);
580 581 582 583 584 585

   //TODO Temporary settings used here
   display->setBellMode(0);
   display->setTerminalSizeHint(false);
   display->setCutToBeginningOfLine(true);
   display->setTerminalSizeStartup(false);
586
   display->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
587 588
   display->setRandomSeed(session->sessionId() * 31);

589 590 591
   return display;
}

592
const ColorScheme* ViewManager::colorSchemeForProfile(const QString& profileKey) const
593
{
594 595
    Profile* info = SessionManager::instance()->profile(profileKey);

596 597
    const ColorScheme* colorScheme = ColorSchemeManager::instance()->
                                            findColorScheme(info->colorScheme());
598 599
    if ( !colorScheme )
       colorScheme = ColorSchemeManager::instance()->defaultColorScheme(); 
600 601
    Q_ASSERT( colorScheme );

602 603 604 605 606 607 608 609 610 611 612
    return colorScheme;
}

void ViewManager::applyProfile(TerminalDisplay* view , const QString& profileKey)
{
    Profile* info = SessionManager::instance()->profile(profileKey);

    Q_ASSERT( info );
    
    const ColorScheme* colorScheme = colorSchemeForProfile(profileKey);

613
    // menu bar visibility
614
    emit setMenuBarVisibleRequest( info->property(Profile::ShowMenuBar).value<bool>() );
615 616 617 618

    // tab bar visibility
    ViewContainer* container = _viewSplitter->activeContainer();
    int tabBarMode = info->property(Profile::TabBarMode).value<int>();
619 620
    int tabBarPosition = info->property(Profile::TabBarPosition).value<int>();

621 622 623 624 625 626 627
    if ( tabBarMode == Profile::AlwaysHideTabBar )
        container->setNavigationDisplayMode(ViewContainer::AlwaysHideNavigation);
    else if ( tabBarMode == Profile::AlwaysShowTabBar )
        container->setNavigationDisplayMode(ViewContainer::AlwaysShowNavigation);
    else if ( tabBarMode == Profile::ShowTabBarAsNeeded )
        container->setNavigationDisplayMode(ViewContainer::ShowNavigationAsNeeded);

628 629
    ViewContainer::NavigationPosition position = container->navigationPosition();

630
    if ( tabBarPosition == Profile::TabBarTop )
631
        position = ViewContainer::NavigationPositionTop;
632
    else if ( tabBarPosition == Profile::TabBarBottom )
633 634 635 636
        position = ViewContainer::NavigationPositionBottom; 

    if ( container->supportedNavigationPositions().contains(position) )
        container->setNavigationPosition(position);
637

638
    // load colour scheme
639 640 641 642
    ColorEntry table[TABLE_COLORS];
    
    colorScheme->getColorTable(table , view->randomSeed() );
    view->setColorTable(table);
643
    view->setOpacity(colorScheme->opacity());
644
  
645
    // load font 
646
    view->setVTFont(info->font());
647 648 649 650 651

    // set scroll-bar position
    int scrollBarPosition = info->property(Profile::ScrollBarPosition).value<int>();

    if ( scrollBarPosition == Profile::ScrollBarHidden )
652
       view->setScrollBarPosition(TerminalDisplay::NoScrollBar);
653
    else if ( scrollBarPosition == Profile::ScrollBarLeft )
654
       view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
655
    else if ( scrollBarPosition == Profile::ScrollBarRight )
656
       view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
657 658 659

    // terminal features
    bool blinkingCursor = info->property(Profile::BlinkingCursorEnabled).value<bool>();
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    view->setBlinkingCursor(blinkingCursor);  

    // cursor shape
    int cursorShape = info->property(Profile::CursorShape).value<int>();

    if ( cursorShape == Profile::BlockCursor )
        view->setKeyboardCursorShape(TerminalDisplay::BlockCursor);  
    else if ( cursorShape == Profile::IBeamCursor )
        view->setKeyboardCursorShape(TerminalDisplay::IBeamCursor);
    else if ( cursorShape == Profile::UnderlineCursor )
        view->setKeyboardCursorShape(TerminalDisplay::UnderlineCursor);

    // cursor color
    bool useCustomColor = info->property(Profile::UseCustomCursorColor).value<bool>();
    const QColor& cursorColor = info->property(Profile::CustomCursorColor).value<QColor>();
        
    view->setKeyboardCursorColor(!useCustomColor,cursorColor);

    // word characters
    view->setWordCharacters( info->property(Profile::WordCharacters).value<QString>() );
680 681
}

682 683
void ViewManager::updateViewsForSession(Session* session)
{
684
    QListIterator<TerminalDisplay*> iter(_sessionMap.keys(session));
685 686 687 688 689 690
    while ( iter.hasNext() )
    {
        applyProfile(iter.next(),session->profileKey());
    }
}

691 692
void ViewManager::profileChanged(const QString& key)
{
693
    QHashIterator<TerminalDisplay*,Session*> iter(_sessionMap);
694 695 696 697 698 699

    while ( iter.hasNext() )
    {
        iter.next();

        // if session uses this profile, update the display
700
        if ( iter.key() != 0 && iter.value() != 0 && iter.value()->profileKey() == key )
701
        {
702
            applyProfile(iter.key(),key);
703 704
        }
    }
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
}

QList<ViewProperties*> ViewManager::viewProperties() const
{
    QList<ViewProperties*> list;

    ViewContainer* container = _viewSplitter->activeContainer();

    Q_ASSERT( container );

    QListIterator<QWidget*> viewIter(container->views());
    while ( viewIter.hasNext() )
    {
        ViewProperties* properties = container->viewProperties(viewIter.next());        Q_ASSERT( properties );
        list << properties; 
    } 
721

722
    return list;
723 724
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
void ViewManager::sendInputToAll()
{
    SessionGroup* group = new SessionGroup();
    group->setMasterMode( SessionGroup::CopyInputToAll );

    Session* activeSession = _sessionMap[qobject_cast<TerminalDisplay*>(activeView())];
    if ( activeSession != 0 )
    {
        QListIterator<Session*> iter( SessionManager::instance()->sessions() );
        while ( iter.hasNext() )
            group->addSession(iter.next());

        group->setMasterStatus(activeSession,true);
    }  
}

741 742 743 744 745
uint qHash(QPointer<TerminalDisplay> display)
{
    return qHash((TerminalDisplay*)display);
}

746
#include "ViewManager.moc"