ViewManager.cpp 10.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
// System
#include <assert.h>

23
24
25
26
27
28
29
30
31
32
// KDE
#include <kdebug.h>
#include <KLocale>
#include <KToggleAction>
#include <KXMLGUIFactory>

// Konsole
#include "KonsoleMainWindow.h"
#include "TESession.h"
#include "TEWidget.h"
33
#include "schema.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "SessionController.h"
#include "SessionManager.h"
#include "ViewContainer.h"
#include "ViewSplitter.h"
#include "ViewManager.h"

ViewManager::ViewManager(KonsoleMainWindow* mainWindow)
    : QObject(mainWindow)
    , _mainWindow(mainWindow)
    , _viewSplitter(0)
    , _pluggedController(0)
{
    // setup actions which relating to the view
    setupActions();

    // create main view area
    _viewSplitter = new ViewSplitter(_mainWindow);
    _mainWindow->setCentralWidget(_viewSplitter);


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

ViewManager::~ViewManager()
{
}

void ViewManager::setupActions()
{
65
66
    KActionCollection* collection = _mainWindow->actionCollection();

67
    _splitViewAction = new KToggleAction( KIcon("view_top_bottom"),i18n("&Split View") , this);
68
    _splitViewAction->setCheckedState( KGuiItem(i18n("&Remove Split") , KIcon("view_remove") ) );
69
    collection->addAction("split-view",_splitViewAction);
70
    connect( _splitViewAction , SIGNAL(toggled(bool)) , this , SLOT(splitView(bool)));
71
72


73
74
75
    QAction* detachViewAction = collection->addAction("detach-view");
    detachViewAction->setIcon( KIcon("view_remove") );
    detachViewAction->setText( i18n("&Detach View") );
76
77
78

    connect( detachViewAction , SIGNAL(triggered()) , this , SLOT(detachActiveView()) );

79
80
81
    QAction* mergeAction = collection->addAction("merge-windows");
    mergeAction->setText( i18n("&Merge Windows") );
        
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    connect( mergeAction , SIGNAL(triggered()) , _mainWindow , SLOT(mergeWindows()) );
}

void ViewManager::detachActiveView()
{
    // find the currently active view and remove it from its container 
    ViewContainer* container = _viewSplitter->activeContainer();
    TEWidget* activeView = dynamic_cast<TEWidget*>(container->activeView());

    if (!activeView)
        return;

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

    // remove the view from this window
    container->removeView(activeView);
    delete activeView;


    // 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 )
    {
        delete container;
110
111
112
113

        // this will need to be removed if Konsole is modified so the menu item to
        // split the view is no longer one toggle-able item
        _splitViewAction->setChecked(false);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    }

}

void ViewManager::sessionFinished( TESession* session )
{
    QList<TEWidget*> children = _viewSplitter->findChildren<TEWidget*>();

    foreach ( TEWidget* view , children )
    {
        if ( _sessionMap[view] == session )
        {
            _sessionMap.remove(view);
            delete view;
        }
Robert Knight's avatar
   
Robert Knight committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    }

    focusActiveView(); 
}

void ViewManager::focusActiveView()
{
    ViewContainer* container = _viewSplitter->activeContainer(); 
    if ( container )
    {
        QWidget* activeView = container->activeView();
        if ( activeView )
        {
            activeView->setFocus(Qt::MouseFocusReason);
        }
    }
145
146
}

147
148
149
150
151
void ViewManager::viewActivated( QWidget* view )
{
    view->setFocus(Qt::MouseFocusReason);
}

152
153
154
155
156
157
158
void ViewManager::viewFocused( SessionController* controller )
{
    if ( _pluggedController != controller )
    {
        if ( _pluggedController )
            _mainWindow->guiFactory()->removeClient(_pluggedController);

159
160
        // update the menus in the main window to use the actions from the active
        // controller 
161
        _mainWindow->guiFactory()->addClient(controller);
162
163
        // update the caption of the main window to match that of the focused session
        _mainWindow->setPlainCaption( controller->session()->displayTitle() );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

        _pluggedController = controller;
    }
}

void ViewManager::splitView(bool splitView)
{
    if (splitView)
    {
        // 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());
        
        ViewContainer* container = createContainer(); 

        while (existingViewIter.hasNext())
        {
            TESession* session = _sessionMap[(TEWidget*)existingViewIter.next()];
182
183
            TEWidget* display = createTerminalDisplay();
            loadViewSettings(display,session); 
Robert Knight's avatar
   
Robert Knight committed
184
            ViewProperties* properties = createController(session,display);
185
186
187

            _sessionMap[display] = session;

Robert Knight's avatar
   
Robert Knight committed
188
            container->addView(display,properties);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
            session->addView( display );
        }

        _viewSplitter->addContainer(container,Qt::Vertical);
    }
    else
    {
        // delete the active container when unsplitting the view unless it is the last
        // one
        if ( _viewSplitter->containers().count() > 1 )
        {
            ViewContainer* container = _viewSplitter->activeContainer();
        
            delete container;
        }
    }
205
206
207

    // ensure that the active view is focused after the split / unsplit
    _viewSplitter->activeContainer()->activeView()->setFocus(Qt::OtherFocusReason);
208
209
210
211
212
213
214
215
216
217
218
219
}

SessionController* ViewManager::createController(TESession* session , TEWidget* view)
{
    SessionController* controller = new SessionController(session,view,this);
    connect( controller , SIGNAL(focused(SessionController*)) , this , SLOT(viewFocused(SessionController*)) );

    return controller;
}

void ViewManager::createView(TESession* session)
{
220
221
222
223
224
225
    // create the default container
    if (_viewSplitter->containers().count() == 0)
    {
        _viewSplitter->addContainer( createContainer() , Qt::Vertical );
    }

226
227
228
229
230
231
232
233
234
    connect( session , SIGNAL(done(TESession*)) , this , SLOT(sessionFinished(TESession*)) );
    
    ViewContainer* const activeContainer = _viewSplitter->activeContainer();
    QListIterator<ViewContainer*> containerIter(_viewSplitter->containers());

    while ( containerIter.hasNext() )
    {
        ViewContainer* container = containerIter.next();
        TEWidget* display = createTerminalDisplay();
235
        loadViewSettings(display,session);
Robert Knight's avatar
   
Robert Knight committed
236
        ViewProperties* properties = createController(session,display);
237
238

        _sessionMap[display] = session; 
Robert Knight's avatar
   
Robert Knight committed
239
        container->addView(display,properties);
240
241
242
243
244
245
246
247
248
249
250
        session->addView(display);

        display->setFocus(Qt::MouseFocusReason);

        if ( container == activeContainer )
            container->setActiveView(display);
    }
}

ViewContainer* ViewManager::createContainer()
{
251
252
    /*TabbedViewContainer* container = new TabbedViewContainer(_viewSplitter); 

253
254
255
256
257
258
259
260
261
    if ( _mainWindow->factory() )
    {
        QMenu* menu = (QMenu*)_mainWindow->factory()->container("new-session-popup",_mainWindow);
        
        if ( menu )
            container->setNewSessionMenu(menu);
    }
    else
    {
262
       kDebug() << __FILE__ << __LINE__ << ": ViewManager attempted to create a view before" <<
263
264
265
          " the main window GUI was created - unable to create popup menus for container." << endl;  
    }

266
267
268
269
    // connect signals and slots
    connect( container , SIGNAL(closeRequest(QWidget*)) , this , SLOT(viewCloseRequest(QWidget*)) );
*/
    ViewContainer* container = new ListViewContainer(_viewSplitter);
270
    return container;
271
272
}

273
274
275
276
void ViewManager::viewCloseRequest(QWidget* view)
{
    // 1. detach view from session
    // 2. if the session has no views left, close it
277
    
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    TEWidget* display = (TEWidget*)view;
    TESession* session = _sessionMap[ display ];
    if ( session )
    {
        delete display;
        
        if ( session->views().count() == 0 )
            session->closeSession();
    }
    else
    {
        kDebug() << __FILE__ << __LINE__ << ": received close request from unknown view." << endl;
    }
}

293
294
295
296
297
298
299
300
301
302
303
void ViewManager::merge(ViewManager* otherManager)
{
    ViewSplitter* otherSplitter = otherManager->_viewSplitter;
    ViewContainer* otherContainer = otherSplitter->activeContainer();

    QListIterator<QWidget*> otherViewIter(otherContainer->views());

    ViewContainer* activeContainer = _viewSplitter->activeContainer();

    while ( otherViewIter.hasNext() )
    {
304
305
306
307
        TEWidget* view = dynamic_cast<TEWidget*>(otherViewIter.next());
        
        assert(view);

Robert Knight's avatar
   
Robert Knight committed
308
        ViewProperties* properties = otherContainer->viewProperties(view);
309
        otherContainer->removeView(view);
Robert Knight's avatar
   
Robert Knight committed
310
        activeContainer->addView(view,properties);
311
312
313
314

        // transfer the session map entries
        _sessionMap.insert(view,otherManager->_sessionMap[view]);
        otherManager->_sessionMap.remove(view);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    } 
}

TEWidget* ViewManager::createTerminalDisplay()
{
   TEWidget* display = new TEWidget(0);

   //TODO Temporary settings used here
   display->setBellMode(0);
   display->setVTFont( QFont("Monospace") );
   display->setTerminalSizeHint(false);
   display->setCutToBeginningOfLine(true);
   display->setTerminalSizeStartup(false);
   display->setSize(80,40);
   display->setScrollbarLocation(TEWidget::SCRRIGHT);

   return display;
}

334
335
336
337
338
339
340
void ViewManager::loadViewSettings(TEWidget* view , TESession* session)
{
    // load colour scheme
    view->setColorTable( session->schema()->table() );

}

341
#include "ViewManager.moc"