• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

ViewManager.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "ViewManager.h"
00022 
00023 // System
00024 #include <assert.h>
00025 
00026 // Qt
00027 #include <QtCore/QDateTime>
00028 #include <QtCore/QSignalMapper>
00029 #include <QtGui/QMenu>
00030 
00031 // KDE
00032 #include <kdebug.h>
00033 #include <KAcceleratorManager>
00034 #include <KGlobal>
00035 #include <KLocale>
00036 #include <KToggleAction>
00037 #include <KXMLGUIFactory>
00038 
00039 // Konsole
00040 #include "ColorScheme.h"
00041 #include "ProfileList.h"
00042 #include "Session.h"
00043 #include "TerminalDisplay.h"
00044 #include "SessionController.h"
00045 #include "SessionManager.h"
00046 #include "ViewContainer.h"
00047 #include "ViewSplitter.h"
00048 
00049 using namespace Konsole;
00050 
00051 ViewManager::ViewManager(QObject* parent , KActionCollection* collection)
00052     : QObject(parent)
00053     , _viewSplitter(0)
00054     , _actionCollection(collection)
00055     , _containerSignalMapper(new QSignalMapper(this))
00056     , _navigationMethod(TabbedNavigation)
00057     , _newViewMenu(0)
00058 {
00059     // create main view area
00060     _viewSplitter = new ViewSplitter(0);  
00061     KAcceleratorManager::setNoAccel(_viewSplitter);
00062 
00063     // the ViewSplitter class supports both recursive and non-recursive splitting,
00064     // in non-recursive mode, all containers are inserted into the same top-level splitter
00065     // widget, and all the divider lines between the containers have the same orientation
00066     //
00067     // the ViewManager class is not currently able to handle a ViewSplitter in recursive-splitting
00068     // mode 
00069     _viewSplitter->setRecursiveSplitting(false);
00070     _viewSplitter->setFocusPolicy(Qt::NoFocus);
00071 
00072     // setup actions which relating to the view
00073     setupActions();
00074 
00075     // emit a signal when all of the views held by this view manager are destroyed
00076     connect( _viewSplitter , SIGNAL(allContainersEmpty()) , this , SIGNAL(empty()) );
00077     connect( _viewSplitter , SIGNAL(empty(ViewSplitter*)) , this , SIGNAL(empty()) );
00078 
00079     // listen for addition or removal of views from associated containers
00080     connect( _containerSignalMapper , SIGNAL(mapped(QObject*)) , this , 
00081             SLOT(containerViewsChanged(QObject*)) ); 
00082 
00083     // listen for profile changes
00084     connect( SessionManager::instance() , SIGNAL(profileChanged(Profile::Ptr)) , this,
00085             SLOT(profileChanged(Profile::Ptr)) );
00086     connect( SessionManager::instance() , SIGNAL(sessionUpdated(Session*)) , this,
00087             SLOT(updateViewsForSession(Session*)) );
00088 }
00089 
00090 ViewManager::~ViewManager()
00091 {
00092     delete _newViewMenu;
00093 }
00094 QMenu* ViewManager::createNewViewMenu() 
00095 {
00096     if (_newViewMenu)
00097         return _newViewMenu;
00098 
00099     _newViewMenu = new QMenu(0);
00100     ProfileList* newViewProfiles = new ProfileList(false,_newViewMenu);
00101     newViewProfiles->syncWidgetActions(_newViewMenu,true);
00102     connect(newViewProfiles,SIGNAL(profileSelected(Profile::Ptr)),this,
00103         SIGNAL(newViewRequest(Profile::Ptr)));
00104 
00105     return _newViewMenu;
00106 }
00107 QWidget* ViewManager::activeView() const
00108 {
00109     ViewContainer* container = _viewSplitter->activeContainer();
00110     if ( container )
00111     {
00112         return container->activeView();
00113     }
00114     else
00115     {
00116         return 0;
00117     }
00118 }
00119 
00120 QWidget* ViewManager::widget() const
00121 {
00122     return _viewSplitter;
00123 }
00124 
00125 void ViewManager::setupActions()
00126 {
00127     KActionCollection* collection = _actionCollection;
00128 
00129     KAction* nextViewAction = new KAction( i18n("Next View") , this );
00130     KAction* previousViewAction = new KAction( i18n("Previous View") , this );
00131     QAction* nextContainerAction = new QAction( i18n("Next View Container") , this);
00132   
00133     QAction* moveViewLeftAction = new QAction( i18n("Move View Left") , this );
00134     QAction* moveViewRightAction = new QAction( i18n("Move View Right") , this );
00135 
00136     // list of actions that should only be enabled when there are multiple view
00137     // containers open
00138     QList<QAction*> multiViewOnlyActions;
00139     multiViewOnlyActions << nextContainerAction;
00140 
00141     if ( collection )
00142     {
00143         KAction* splitLeftRightAction = new KAction( KIcon("view-split-left-right"),
00144                                                       i18nc("@action:inmenu", "Split View Left/Right"),
00145                                                       this );
00146         splitLeftRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_L) );
00147         collection->addAction("split-view-left-right",splitLeftRightAction);
00148         connect( splitLeftRightAction , SIGNAL(triggered()) , this , SLOT(splitLeftRight()) );
00149 
00150         KAction* splitTopBottomAction = new KAction( KIcon("view-split-top-bottom") , 
00151                                              i18nc("@action:inmenu", "Split View Top/Bottom"),this);
00152         splitTopBottomAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_T) );
00153         collection->addAction("split-view-top-bottom",splitTopBottomAction);
00154         connect( splitTopBottomAction , SIGNAL(triggered()) , this , SLOT(splitTopBottom()));
00155 
00156         KAction* closeActiveAction = new KAction( i18nc("@action:inmenu Close Active View", "Close Active") , this );
00157         closeActiveAction->setIcon(KIcon("view-close"));
00158         closeActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S) );
00159         closeActiveAction->setEnabled(false);
00160         collection->addAction("close-active-view",closeActiveAction);
00161         connect( closeActiveAction , SIGNAL(triggered()) , this , SLOT(closeActiveView()) );
00162       
00163         multiViewOnlyActions << closeActiveAction; 
00164 
00165         KAction* closeOtherAction = new KAction( i18nc("@action:inmenu Close Other Views", "Close Others") , this );
00166         closeOtherAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_O) );
00167         closeOtherAction->setEnabled(false);
00168         collection->addAction("close-other-views",closeOtherAction);
00169         connect( closeOtherAction , SIGNAL(triggered()) , this , SLOT(closeOtherViews()) );
00170 
00171         multiViewOnlyActions << closeOtherAction;
00172 
00173         KAction* detachViewAction = collection->addAction("detach-view");
00174         detachViewAction->setIcon( KIcon("tab-detach") );
00175         detachViewAction->setText( i18n("&Detach View") );
00176         // Ctrl+Shift+D is not used as a shortcut by default because it is too close
00177         // to Ctrl+D - which will terminate the session in many cases
00178         detachViewAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_H) );
00179        
00180         connect( this , SIGNAL(splitViewToggle(bool)) , this , SLOT(updateDetachViewState()) ); 
00181         connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );
00182    
00183         // Expand & Shrink Active View
00184         KAction* expandActiveAction = new KAction( i18nc("@action:inmenu", "Expand View") , this );
00185         expandActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketRight) );
00186         collection->addAction("expand-active-view",expandActiveAction);
00187         connect( expandActiveAction , SIGNAL(triggered()) , this , SLOT(expandActiveView()) );
00188 
00189         multiViewOnlyActions << expandActiveAction;
00190 
00191         KAction* shrinkActiveAction = new KAction( i18nc("@action:inmenu", "Shrink View") , this );
00192         shrinkActiveAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_BracketLeft) );
00193         collection->addAction("shrink-active-view",shrinkActiveAction);
00194         connect( shrinkActiveAction , SIGNAL(triggered()) , this , SLOT(shrinkActiveView()) );
00195 
00196         multiViewOnlyActions << shrinkActiveAction;
00197 
00198         // Next / Previous View , Next Container
00199         collection->addAction("next-view",nextViewAction);
00200         collection->addAction("previous-view",previousViewAction);
00201         collection->addAction("next-container",nextContainerAction);
00202         collection->addAction("move-view-left",moveViewLeftAction);
00203         collection->addAction("move-view-right",moveViewRightAction);
00204     }
00205 
00206     QListIterator<QAction*> iter(multiViewOnlyActions);
00207     while ( iter.hasNext() )
00208     {
00209         connect( this , SIGNAL(splitViewToggle(bool)) , iter.next() , SLOT(setEnabled(bool)) );
00210     }
00211 
00212     // keyboard shortcut only actions
00213     KShortcut nextViewShortcut = nextViewAction->shortcut();
00214     nextViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Right) );
00215     nextViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageUp) );
00216     nextViewAction->setShortcut(nextViewShortcut); 
00217     connect( nextViewAction, SIGNAL(triggered()) , this , SLOT(nextView()) );
00218     _viewSplitter->addAction(nextViewAction);
00219 
00220     KShortcut previousViewShortcut = previousViewAction->shortcut();
00221     previousViewShortcut.setPrimary( QKeySequence(Qt::SHIFT+Qt::Key_Left) );
00222     previousViewShortcut.setAlternate( QKeySequence(Qt::CTRL+Qt::Key_PageDown) );
00223     previousViewAction->setShortcut(previousViewShortcut);
00224     connect( previousViewAction, SIGNAL(triggered()) , this , SLOT(previousView()) );
00225     _viewSplitter->addAction(previousViewAction);
00226 
00227     nextContainerAction->setShortcut( QKeySequence(Qt::SHIFT+Qt::Key_Tab) );
00228     connect( nextContainerAction , SIGNAL(triggered()) , this , SLOT(nextContainer()) );
00229     _viewSplitter->addAction(nextContainerAction);
00230 
00231     moveViewLeftAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Left) );
00232     connect( moveViewLeftAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewLeft()) );
00233     _viewSplitter->addAction(moveViewLeftAction);
00234     moveViewRightAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_Right) );
00235     connect( moveViewRightAction , SIGNAL(triggered()) , this , SLOT(moveActiveViewRight()) );
00236     _viewSplitter->addAction(moveViewRightAction);
00237 }
00238 
00239 void ViewManager::updateDetachViewState()
00240 {
00241     if (!_actionCollection)
00242         return;
00243 
00244 
00245     bool splitView = _viewSplitter->containers().count() >= 2;
00246     bool shouldEnable = splitView || _viewSplitter->activeContainer()->views().count() >= 2;
00247 
00248     QAction* detachAction = _actionCollection->action("detach-view");
00249 
00250     if ( detachAction && shouldEnable != detachAction->isEnabled() )
00251         detachAction->setEnabled(shouldEnable);
00252 }
00253 void ViewManager::moveActiveViewLeft()
00254 {
00255     ViewContainer* container = _viewSplitter->activeContainer();
00256     Q_ASSERT( container );
00257     container->moveActiveView( ViewContainer::MoveViewLeft );
00258 }
00259 void ViewManager::moveActiveViewRight()
00260 {
00261     ViewContainer* container = _viewSplitter->activeContainer();
00262     Q_ASSERT( container );
00263     container->moveActiveView( ViewContainer::MoveViewRight );
00264 }
00265 void ViewManager::nextContainer()
00266 {
00267     _viewSplitter->activateNextContainer();
00268 }
00269 
00270 void ViewManager::nextView()
00271 {
00272     ViewContainer* container = _viewSplitter->activeContainer();
00273 
00274     Q_ASSERT( container );
00275 
00276     container->activateNextView();
00277 }
00278 
00279 void ViewManager::previousView()
00280 {
00281     ViewContainer* container = _viewSplitter->activeContainer();
00282 
00283     Q_ASSERT( container );
00284 
00285     container->activatePreviousView();
00286 }
00287 void ViewManager::detachActiveView()
00288 {
00289     // find the currently active view and remove it from its container 
00290     ViewContainer* container = _viewSplitter->activeContainer();
00291     TerminalDisplay* activeView = dynamic_cast<TerminalDisplay*>(container->activeView());
00292 
00293     if (!activeView)
00294         return;
00295 
00296     emit viewDetached(_sessionMap[activeView]);
00297     
00298     _sessionMap.remove(activeView);
00299 
00300     // remove the view from this window
00301     container->removeView(activeView);
00302     activeView->deleteLater();
00303 
00304     // if the container from which the view was removed is now empty then it can be deleted,
00305     // unless it is the only container in the window, in which case it is left empty
00306     // so that there is always an active container
00307     if ( _viewSplitter->containers().count() > 1 && 
00308          container->views().count() == 0 )
00309     {
00310         removeContainer(container);
00311     }
00312 
00313 }
00314 
00315 void ViewManager::sessionFinished()
00316 {
00317     // if this slot is called after the view manager's main widget
00318     // has been destroyed, do nothing
00319     if (!_viewSplitter)
00320         return;
00321 
00322     Session* session = qobject_cast<Session*>(sender());
00323 
00324     if ( _sessionMap[qobject_cast<TerminalDisplay*>(activeView())] == session )
00325     {
00326         // switch to the previous view before deleting the session views to prevent flicker 
00327         // occurring as a result of an interval between removing the active view and switching
00328         // to the previous view
00329         previousView();
00330     }
00331 
00332     Q_ASSERT(session);
00333 
00334     // close attached views
00335     QList<TerminalDisplay*> children = _viewSplitter->findChildren<TerminalDisplay*>();
00336 
00337     foreach ( TerminalDisplay* view , children )
00338     {
00339         if ( _sessionMap[view] == session )
00340         {
00341             _sessionMap.remove(view);
00342             view->deleteLater();
00343         }
00344     }
00345 
00346 }
00347 
00348 void ViewManager::focusActiveView()
00349 {
00350     // give the active view in a container the focus.  this ensures 
00351     // that controller associated with that view is activated and the session-specific
00352     // menu items are replaced with the ones for the newly focused view
00353 
00354     // see the viewFocused() method
00355 
00356     ViewContainer* container = _viewSplitter->activeContainer(); 
00357     if ( container )
00358     {
00359         QWidget* activeView = container->activeView();
00360         if ( activeView )
00361         {
00362             activeView->setFocus(Qt::MouseFocusReason);
00363         }
00364     }
00365 }
00366 
00367 
00368 void ViewManager::viewActivated( QWidget* view )
00369 {
00370     Q_ASSERT( view != 0 );
00371 
00372     // focus the activated view, this will cause the SessionController
00373     // to notify the world that the view has been focused and the appropriate UI
00374     // actions will be plugged in.
00375     view->setFocus(Qt::OtherFocusReason);
00376 }
00377 
00378 void ViewManager::splitLeftRight()
00379 {
00380     splitView(Qt::Horizontal);
00381 }
00382 void ViewManager::splitTopBottom()
00383 {
00384     splitView(Qt::Vertical);
00385 }
00386 
00387 void ViewManager::splitView(Qt::Orientation orientation)
00388 {
00389     // iterate over each session which has a view in the current active
00390     // container and create a new view for that session in a new container 
00391     QListIterator<QWidget*> existingViewIter(_viewSplitter->activeContainer()->views());
00392     
00393     ViewContainer* container = 0; 
00394 
00395     while (existingViewIter.hasNext())
00396     {
00397         Session* session = _sessionMap[(TerminalDisplay*)existingViewIter.next()];
00398         TerminalDisplay* display = createTerminalDisplay(session);
00399         applyProfile(display,SessionManager::instance()->sessionProfile(session),false);
00400         ViewProperties* properties = createController(session,display);
00401 
00402         _sessionMap[display] = session;
00403 
00404         // create a container using settings from the first 
00405         // session in the previous container
00406         if ( !container )
00407             container = createContainer(SessionManager::instance()->sessionProfile(session));
00408 
00409         container->addView(display,properties);
00410         session->addView( display );
00411     }
00412 
00413     _viewSplitter->addContainer(container,orientation);
00414     emit splitViewToggle(_viewSplitter->containers().count() > 0);
00415 
00416     // focus the new container
00417     container->containerWidget()->setFocus();
00418 
00419     // ensure that the active view is focused after the split / unsplit
00420     ViewContainer* activeContainer = _viewSplitter->activeContainer();
00421     QWidget* activeView = activeContainer ? activeContainer->activeView() : 0;
00422 
00423     if ( activeView )
00424         activeView->setFocus(Qt::OtherFocusReason);
00425 }
00426 void ViewManager::removeContainer(ViewContainer* container)
00427 {
00428     // remove session map entries for views in this container
00429     foreach( QWidget* view , container->views() )
00430     {
00431         TerminalDisplay* display = qobject_cast<TerminalDisplay*>(view);
00432         Q_ASSERT(display);
00433         _sessionMap.remove(display);
00434     } 
00435 
00436     _viewSplitter->removeContainer(container);
00437     container->deleteLater();
00438 
00439     emit splitViewToggle( _viewSplitter->containers().count() > 1 );
00440 }
00441 void ViewManager::expandActiveView()
00442 {
00443     _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),10);
00444 }
00445 void ViewManager::shrinkActiveView()
00446 {
00447     _viewSplitter->adjustContainerSize(_viewSplitter->activeContainer(),-10);
00448 }
00449 void ViewManager::closeActiveView()
00450 {
00451     // only do something if there is more than one container active
00452     if ( _viewSplitter->containers().count() > 1 )
00453     {
00454         ViewContainer* container = _viewSplitter->activeContainer();
00455 
00456         removeContainer(container);
00457 
00458         // focus next container so that user can continue typing 
00459         // without having to manually focus it themselves
00460         nextContainer();
00461     }
00462 }
00463 void ViewManager::closeOtherViews()
00464 {
00465     ViewContainer* active = _viewSplitter->activeContainer();
00466 
00467     QListIterator<ViewContainer*> iter(_viewSplitter->containers());
00468     while ( iter.hasNext() )
00469     {
00470         ViewContainer* next = iter.next();
00471         if ( next != active )
00472             removeContainer(next);
00473     }
00474 }
00475 
00476 SessionController* ViewManager::createController(Session* session , TerminalDisplay* view)
00477 {
00478     // create a new controller for the session, and ensure that this view manager
00479     // is notified when the view gains the focus
00480     SessionController* controller = new SessionController(session,view,this);
00481     connect( controller , SIGNAL(focused(SessionController*)) , this , SLOT(controllerChanged(SessionController*)) );
00482     connect( session , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
00483     connect( view , SIGNAL(destroyed()) , controller , SLOT(deleteLater()) );
00484 
00485     // if this is the first controller created then set it as the active controller
00486     if (!_pluggedController)
00487         controllerChanged(controller);
00488 
00489     return controller;
00490 }
00491 
00492 void ViewManager::controllerChanged(SessionController* controller)
00493 {
00494     if ( controller == _pluggedController )
00495         return;
00496 
00497     _viewSplitter->setFocusProxy(controller->view());
00498 
00499     _pluggedController = controller;
00500     emit activeViewChanged(controller);
00501 }
00502 
00503 SessionController* ViewManager::activeViewController() const
00504 {
00505     return _pluggedController;
00506 }
00507 void ViewManager::createView(Session* session, ViewContainer* container, int index)
00508 {
00509     // notify this view manager when the session finishes so that its view
00510     // can be deleted
00511     //
00512     // TODO - Find a more efficient a way to avoid multiple connections
00513     disconnect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
00514     connect( session , SIGNAL(finished()) , this , SLOT(sessionFinished()) );
00515 
00516      bool isFirst = _sessionMap.isEmpty();
00517      TerminalDisplay* display = createTerminalDisplay(session);
00518      applyProfile(display,SessionManager::instance()->sessionProfile(session),isFirst);
00519      
00520      // set initial size
00521      display->setSize(80,40);
00522 
00523      ViewProperties* properties = createController(session,display);
00524 
00525      _sessionMap[display] = session; 
00526      container->addView(display,properties,index);
00527      session->addView(display);
00528 
00529      // tell the session whether it has a light or dark background
00530     const Profile::Ptr profile = SessionManager::instance()->sessionProfile(session);
00531      session->setDarkBackground( colorSchemeForProfile(profile)->hasDarkBackground() );
00532 
00533      if ( container == _viewSplitter->activeContainer() ) 
00534      {
00535          container->setActiveView(display);
00536          display->setFocus( Qt::OtherFocusReason );
00537      }
00538     
00539      updateDetachViewState();
00540 }
00541 
00542 void ViewManager::createView(Session* session)
00543 {
00544     // create the default container
00545     if (_viewSplitter->containers().count() == 0)
00546     {
00547         _viewSplitter->addContainer( createContainer(SessionManager::instance()->sessionProfile(session)) , 
00548                                      Qt::Vertical );
00549         emit splitViewToggle(false);
00550     }
00551 
00552        
00553     // iterate over the view containers owned by this view manager
00554     // and create a new terminal display for the session in each of them, along with
00555     // a controller for the session/display pair 
00556     QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());
00557 
00558     while ( containerIter.hasNext() )
00559     {
00560         ViewContainer* container = containerIter.next();
00561         createView(session,container,-1);
00562     }
00563 
00564 }
00565 
00566 ViewContainer* ViewManager::createContainer(const Profile::Ptr info)
00567 {
00568     Q_ASSERT( info );
00569 
00570     const int tabPosition = info->property<int>(Profile::TabBarPosition);
00571 
00572     ViewContainer::NavigationPosition position = ( tabPosition == Profile::TabBarTop ) ?
00573                                                    ViewContainer::NavigationPositionTop :
00574                                                    ViewContainer::NavigationPositionBottom;
00575 
00576     ViewContainer* container = 0;
00577 
00578     switch ( _navigationMethod )
00579     {
00580         case TabbedNavigation:    
00581             container = new TabbedViewContainerV2(position,_viewSplitter);
00582             break;
00583         case NoNavigation:
00584         default:
00585             container = new StackedViewContainer(_viewSplitter);
00586     }
00587 
00588     // connect signals and slots
00589     connect( container , SIGNAL(viewAdded(QWidget*,ViewProperties*)) , _containerSignalMapper ,
00590            SLOT(map()) );
00591     connect( container , SIGNAL(viewRemoved(QWidget*)) , _containerSignalMapper ,
00592            SLOT(map()) ); 
00593     _containerSignalMapper->setMapping(container,container);
00594 
00595     connect( container, SIGNAL(newViewRequest()), this, SIGNAL(newViewRequest()) );
00596     connect( container, SIGNAL(moveViewRequest(int,int,bool&)), 
00597     this , SLOT(containerMoveViewRequest(int,int,bool&)) );
00598     connect( container , SIGNAL(viewRemoved(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
00599     connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
00600     connect( container , SIGNAL(activeViewChanged(QWidget*)) , this , SLOT(viewActivated(QWidget*)));
00601     
00602     return container;
00603 }
00604 void ViewManager::containerMoveViewRequest(int index, int id, bool& moved)
00605 {
00606     ViewContainer* container = qobject_cast<ViewContainer*>(sender());
00607     SessionController* controller = qobject_cast<SessionController*>(ViewProperties::propertiesById(id));
00608 
00609     if (!controller)
00610         return;
00611 
00612     createView(controller->session(),container,index);
00613     moved = true;
00614 }
00615 void ViewManager::setNavigationMethod(NavigationMethod method)
00616 {
00617     _navigationMethod = method;
00618 
00619     KActionCollection* collection = _actionCollection;
00620 
00621     if ( collection )
00622     {
00623         QAction* action;
00624 
00625         action = collection->action( "next-view" );
00626         if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
00627 
00628         action = collection->action( "previous-view" );
00629         if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
00630 
00631         action = collection->action( "split-view-left-right" );
00632         if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
00633 
00634         action = collection->action( "split-view-top-bottom" );
00635         if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
00636 
00637         action = collection->action( "rename-session" );
00638         if ( action ) action->setEnabled( _navigationMethod != NoNavigation );
00639     }
00640 }
00641 
00642 ViewManager::NavigationMethod ViewManager::navigationMethod() const { return _navigationMethod; }
00643 
00644 void ViewManager::containerViewsChanged(QObject* container)
00645 {
00646     if ( container == _viewSplitter->activeContainer() )
00647     {
00648         emit viewPropertiesChanged( viewProperties() );
00649     } 
00650 }
00651 
00652 void ViewManager::viewCloseRequest(QWidget* view)
00653 {
00654     //FIXME Check that this cast is actually legal
00655     TerminalDisplay* display = (TerminalDisplay*)view;
00656   
00657     Q_ASSERT(display);
00658 
00659     // 1. detach view from session
00660     // 2. if the session has no views left, close it
00661     Session* session = _sessionMap[ display ];
00662     _sessionMap.remove(display);
00663     if ( session )
00664     {
00665         display->deleteLater();
00666 
00667         if ( session->views().count() == 0 )
00668             session->close();
00669     }
00670         
00671     focusActiveView();
00672     updateDetachViewState();
00673 }
00674 
00675 TerminalDisplay* ViewManager::createTerminalDisplay(Session* session)
00676 {
00677    TerminalDisplay* display = new TerminalDisplay(0);
00678 
00679    //TODO Temporary settings used here
00680    display->setBellMode(TerminalDisplay::NotifyBell);
00681    display->setTerminalSizeHint(true);
00682    display->setTripleClickMode(TerminalDisplay::SelectWholeLine);
00683    display->setTerminalSizeStartup(true);
00684    display->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
00685    display->setRandomSeed(session->sessionId() * 31);
00686 
00687    return display;
00688 }
00689 
00690 const ColorScheme* ViewManager::colorSchemeForProfile(const Profile::Ptr info) const
00691 {
00692     const ColorScheme* colorScheme = ColorSchemeManager::instance()->
00693                                             findColorScheme(info->colorScheme());
00694     if ( !colorScheme )
00695        colorScheme = ColorSchemeManager::instance()->defaultColorScheme(); 
00696     Q_ASSERT( colorScheme );
00697 
00698     return colorScheme;
00699 }
00700 
00701 void ViewManager::applyProfile(TerminalDisplay* view , const Profile::Ptr info, 
00702                                bool applyContainerSettings) 
00703 {
00704     Q_ASSERT( info );
00705     
00706     const ColorScheme* colorScheme = colorSchemeForProfile(info);
00707 
00708     // menu bar visibility
00709     emit setMenuBarVisibleRequest( info->property<bool>(Profile::ShowMenuBar) );
00710 
00711     // tab bar visibility
00712     if (applyContainerSettings)
00713     {
00714         ViewContainer* container = _viewSplitter->activeContainer();
00715         int tabBarMode = info->property<int>(Profile::TabBarMode);
00716         int tabBarPosition = info->property<int>(Profile::TabBarPosition);
00717         bool showNewCloseButtons = info->property<bool>(Profile::ShowNewAndCloseTabButtons);
00718 
00719         if ( tabBarMode == Profile::AlwaysHideTabBar )
00720             container->setNavigationDisplayMode(ViewContainer::AlwaysHideNavigation);
00721         else if ( tabBarMode == Profile::AlwaysShowTabBar )
00722             container->setNavigationDisplayMode(ViewContainer::AlwaysShowNavigation);
00723         else if ( tabBarMode == Profile::ShowTabBarAsNeeded )
00724             container->setNavigationDisplayMode(ViewContainer::ShowNavigationAsNeeded);
00725 
00726         ViewContainer::NavigationPosition position = container->navigationPosition();
00727 
00728         if ( tabBarPosition == Profile::TabBarTop )
00729             position = ViewContainer::NavigationPositionTop;
00730         else if ( tabBarPosition == Profile::TabBarBottom )
00731             position = ViewContainer::NavigationPositionBottom; 
00732 
00733         if ( container->supportedNavigationPositions().contains(position) )
00734             container->setNavigationPosition(position);
00735        
00736         if (showNewCloseButtons)
00737         {
00738             container->setFeatures(container->features() 
00739                                | ViewContainer::QuickNewView | ViewContainer::QuickCloseView);
00740             container->setNewViewMenu(createNewViewMenu());
00741         }
00742         else
00743             container->setFeatures(container->features() 
00744                             & ~ViewContainer::QuickNewView & ~ViewContainer::QuickCloseView);
00745     }
00746 
00747     // load colour scheme
00748     ColorEntry table[TABLE_COLORS];
00749     
00750     colorScheme->getColorTable(table , view->randomSeed() );
00751     view->setColorTable(table);
00752     view->setOpacity(colorScheme->opacity());
00753   
00754     // load font 
00755     view->setAntialias(info->property<bool>(Profile::AntiAliasFonts));
00756     view->setVTFont(info->font());
00757 
00758     // set scroll-bar position
00759     int scrollBarPosition = info->property<int>(Profile::ScrollBarPosition);
00760 
00761     if ( scrollBarPosition == Profile::ScrollBarHidden )
00762        view->setScrollBarPosition(TerminalDisplay::NoScrollBar);
00763     else if ( scrollBarPosition == Profile::ScrollBarLeft )
00764        view->setScrollBarPosition(TerminalDisplay::ScrollBarLeft);
00765     else if ( scrollBarPosition == Profile::ScrollBarRight )
00766        view->setScrollBarPosition(TerminalDisplay::ScrollBarRight);
00767 
00768     // terminal features
00769     bool blinkingCursor = info->property<bool>(Profile::BlinkingCursorEnabled);
00770     view->setBlinkingCursor(blinkingCursor);  
00771 
00772     bool bidiEnabled = info->property<bool>(Profile::BidiRenderingEnabled);
00773     view->setBidiEnabled(bidiEnabled);
00774 
00775     // cursor shape
00776     int cursorShape = info->property<int>(Profile::CursorShape);
00777 
00778     if ( cursorShape == Profile::BlockCursor )
00779         view->setKeyboardCursorShape(TerminalDisplay::BlockCursor);  
00780     else if ( cursorShape == Profile::IBeamCursor )
00781         view->setKeyboardCursorShape(TerminalDisplay::IBeamCursor);
00782     else if ( cursorShape == Profile::UnderlineCursor )
00783         view->setKeyboardCursorShape(TerminalDisplay::UnderlineCursor);
00784 
00785     // cursor color
00786     bool useCustomColor = info->property<bool>(Profile::UseCustomCursorColor);
00787     const QColor& cursorColor = info->property<QColor>(Profile::CustomCursorColor);
00788         
00789     view->setKeyboardCursorColor(!useCustomColor,cursorColor);
00790 
00791     // word characters
00792     view->setWordCharacters( info->property<QString>(Profile::WordCharacters) );
00793 }
00794 
00795 void ViewManager::updateViewsForSession(Session* session)
00796 {
00797     QListIterator<TerminalDisplay*> iter(_sessionMap.keys(session));
00798     while ( iter.hasNext() )
00799     {
00800         applyProfile(iter.next(),SessionManager::instance()->sessionProfile(session),false);
00801     }
00802 }
00803 
00804 void ViewManager::profileChanged(Profile::Ptr profile)
00805 {
00806     QHashIterator<TerminalDisplay*,Session*> iter(_sessionMap);
00807 
00808     while ( iter.hasNext() )
00809     {
00810         iter.next();
00811 
00812         // if session uses this profile, update the display
00813         if ( iter.key() != 0 && 
00814              iter.value() != 0 && 
00815              SessionManager::instance()->sessionProfile(iter.value()) == profile ) 
00816         {
00817             applyProfile(iter.key(),profile,true);
00818         }
00819     }
00820 }
00821 
00822 QList<ViewProperties*> ViewManager::viewProperties() const
00823 {
00824     QList<ViewProperties*> list;
00825 
00826     ViewContainer* container = _viewSplitter->activeContainer();
00827 
00828     Q_ASSERT( container );
00829 
00830     QListIterator<QWidget*> viewIter(container->views());
00831     while ( viewIter.hasNext() )
00832     {
00833         ViewProperties* properties = container->viewProperties(viewIter.next()); 
00834         Q_ASSERT( properties );
00835         list << properties; 
00836     } 
00837 
00838     return list;
00839 }
00840 
00841 uint qHash(QPointer<TerminalDisplay> display)
00842 {
00843     return qHash((TerminalDisplay*)display);
00844 }
00845 
00846 #include "ViewManager.moc"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal