ViewManager.h 13.2 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
20
21
22
23

    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.
*/

#ifndef VIEWMANAGER_H
#define VIEWMANAGER_H

// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24
25
26
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QPointer>
27

28
29
// Konsole
#include "Profile.h"
30
#include "ViewContainer.h"
31

32
class QSignalMapper;
33
class QMenu;
34
class KActionCollection;
Laurent Montel's avatar
Laurent Montel committed
35
class KConfigGroup;
36

37
38
39
namespace Konsole
{

40
class ColorScheme;
41
class IncrementalSearchBar;
42
class Session;
43
class TerminalDisplay;
44
45

class SessionController;
46
class ViewProperties;
47
48
class ViewSplitter;

Jekyll Wu's avatar
Jekyll Wu committed
49
/**
50
 * Manages the terminal display widgets in a Konsole window or part.
Robert Knight's avatar
Robert Knight committed
51
 *
Jekyll Wu's avatar
Jekyll Wu committed
52
 * When a view manager is created, it constructs a splitter widget ( accessed via
53
54
55
 * widget() ) to hold one or more view containers.  Each view container holds
 * one or more terminal displays and a navigation widget ( eg. tabs or a list )
 * to allow the user to navigate between the displays in that container.
56
 *
Robert Knight's avatar
   
Robert Knight committed
57
 * The view manager provides menu actions ( defined in the 'konsoleui.rc' XML file )
58
59
 * to manipulate the views and view containers - for example, actions to split the view
 * left/right or top/bottom, detach a view from the current window and navigate between
Jekyll Wu's avatar
Jekyll Wu committed
60
 * views and containers.  These actions are added to the collection specified in the
Robert Knight's avatar
   
Robert Knight committed
61
62
63
64
65
66
 * ViewManager's constructor.
 *
 * The view manager provides facilities to construct display widgets for a terminal
 * session and also to construct the SessionController which provides the menus and other
 * user interface elements specific to each display/session pair.
 *
67
 */
68
class KONSOLEPRIVATE_EXPORT ViewManager : public QObject
69
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
70
71
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.kde.konsole.Konsole")
72
73

public:
Jekyll Wu's avatar
Jekyll Wu committed
74
75
    /**
     * Constructs a new view manager with the specified @p parent.
76
     * View-related actions defined in 'konsoleui.rc' are created
Jekyll Wu's avatar
Jekyll Wu committed
77
     * and added to the specified @p collection.
78
     */
79
    ViewManager(QObject* parent , KActionCollection* collection);
80
81
82
83
    ~ViewManager();

    /**
     * Creates a new view to display the outout from and deliver input to @p session.
84
     * Constructs a new container to hold the views if no container has yet been created.
85
     */
86
    void createView(Session* session);
87

88
89
    /**
     * Applies the view-specific settings associated with specified @p profile
90
     * to the terminal display @p view.
91
     */
92
93
    void applyProfileToView(TerminalDisplay* view , const Profile::Ptr profile);

Jekyll Wu's avatar
Jekyll Wu committed
94
    /**
95
96
97
     * Return the main widget for the view manager which
     * holds all of the views managed by this ViewManager instance.
     */
98
99
    QWidget* widget() const;

100
101
102
103
104
105
106
107
108
    /**
     * Returns the view manager's active view.
     */
    QWidget* activeView() const;

    /**
     * Returns the list of view properties for views in the active container.
     * Each view widget is associated with a ViewProperties instance which
     * provides access to basic information about the session being
Jekyll Wu's avatar
Jekyll Wu committed
109
110
     * displayed in the view, such as title, current directory and
     * associated icon.
111
112
113
     */
    QList<ViewProperties*> viewProperties() const;

Jekyll Wu's avatar
Jekyll Wu committed
114
115
    /**
     * This enum describes the available types of navigation widget
116
117
118
     * which newly created containers can provide to allow navigation
     * between open sessions.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
119
    enum NavigationMethod {
Jekyll Wu's avatar
Jekyll Wu committed
120
        /**
121
122
123
124
125
126
127
128
         * Each container has a row of tabs (one per session) which the user
         * can click on to navigate between open sessions.
         */
        TabbedNavigation,
        /** The container has no navigation widget. */
        NoNavigation
    };

129
130
131
132
133
134
135
136
137
138
139
    /**
     * This enum describes where newly created tab should be placed.
     */
    enum NewTabBehavior {
        /** Put newly created tab at the end. */
        PutNewTabAtTheEnd = 0,
        /** Put newly created tab right after current tab. */
        PutNewTabAfterCurrentTab = 1
    };

    struct NavigationOptions {
140
141
142
143
        int visibility;
        int position;
        int newTabBehavior;
        bool showQuickButtons;
144
145
    };

Jekyll Wu's avatar
Jekyll Wu committed
146
147
    /**
     * Sets the type of widget provided to navigate between open sessions
148
149
150
     * in a container.  The changes will only apply to newly created containers.
     *
     * The default method is TabbedNavigation.  To disable navigation widgets, call
Jekyll Wu's avatar
Jekyll Wu committed
151
     * setNavigationMethod(ViewManager::NoNavigation) before creating any sessions.
152
153
154
     */
    void setNavigationMethod(NavigationMethod method);

Jekyll Wu's avatar
Jekyll Wu committed
155
156
157
    /**
     * Returns the type of navigation widget created in new containers.
     * See setNavigationMethod()
158
159
160
     */
    NavigationMethod navigationMethod() const;

Jekyll Wu's avatar
Jekyll Wu committed
161
162
    /**
     * Returns the controller for the active view.  activeViewChanged() is
Robert Knight's avatar
   
Robert Knight committed
163
     * emitted when this changes.
164
165
     */
    SessionController* activeViewController() const;
166

167
168
169
170
171
    /**
     * Returns the search bar.
     */
    IncrementalSearchBar* searchBar() const;

Robert Knight's avatar
   
Robert Knight committed
172
173
174
175
176
177
    /**
     * Session management
     */
    void saveSessions(KConfigGroup& group);
    void restoreSessions(const KConfigGroup& group);

178
179
180
    void updateNavigationOptions(NavigationOptions options);
    void applyNavigationOptions(ViewContainer* container);

181
182
    int managerId() const;

183
184
185
186
187
signals:
    /** Emitted when the last view is removed from the view manager */
    void empty();

    /** Emitted when a session is detached from a view owned by this ViewManager */
188
    void viewDetached(Session* session);
189

Jekyll Wu's avatar
Jekyll Wu committed
190
191
192
    /**
     * Emitted when the active view changes.
     * @param controller The controller associated with the active view
193
194
195
     */
    void activeViewChanged(SessionController* controller);

Jekyll Wu's avatar
Jekyll Wu committed
196
    /**
197
     * Emitted when the current session needs unplugged from factory().
Jekyll Wu's avatar
Jekyll Wu committed
198
     * @param controller The controller associated with the active view
199
200
201
     */
    void unplugController(SessionController* controller);

202
203
    /**
     * Emitted when the list of view properties ( as returned by viewProperties() ) changes.
Jekyll Wu's avatar
Jekyll Wu committed
204
     * This occurs when views are added to or removed from the active container, or
205
206
207
208
     * if the active container is changed.
     */
    void viewPropertiesChanged(const QList<ViewProperties*>& propertiesList);

Jekyll Wu's avatar
Jekyll Wu committed
209
    /**
210
211
212
213
     * Emitted when the number of views containers changes.  This is used to disable or
     * enable menu items which can only be used when there are one or multiple containers
     * visible.
     *
Jekyll Wu's avatar
Jekyll Wu committed
214
     * @param multipleViews True if there are multiple view containers open or false if there is
215
216
     * just a single view.
     */
217
218
    void splitViewToggle(bool multipleViews);

Constantin Berzan's avatar
APIDOX    
Constantin Berzan committed
219
220
221
222
    /**
     * Emitted when menu bar visibility changes because a profile that requires so is
     * activated.
     */
223
    void setMenuBarVisibleRequest(bool);
224
    void setSaveGeometryOnExitRequest(bool);
225
    void updateWindowIcon();
226

227
    /** Requests creation of a new view with the default profile. */
228
    void newViewRequest();
229
230
    /** Requests creation of a new view, with the selected profile. */
    void newViewRequest(Profile::Ptr);
231

232
233
234
235
public slots:
    /** DBus slot that returns the number of sessions in the current view. */
    int sessionCount();

236
237
238
239
    /** DBus slot that returns the current (active) session window */
    int currentSession();

    /** DBus slot that creates a new session in the current view.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
240
241
     * @param profile the name of the profile to be used
     * @param directory the working directory where the session is
242
243
244
245
     * started.
     */
    int newSession(QString profile, QString directory);

246
247
248
    // DBus slot that returns the name of default profile
    QString defaultProfile();

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    // DBus slot that returns a string list of defined (known) profiles
    QStringList profileList();

    /** DBus slot that creates a new session in the current view with the associated
      * default profile and the default working directory
      */
    int newSession();

    /** DBus slot that changes the view port to the next session */
    void nextSession();

    /** DBus slot that changes the view port to the previous session */
    void prevSession();

    /** DBus slot that switches the current session (as returned by
      * currentSession()) with the left (or previous) one in the
      * navigation tab.
      */
    void moveSessionLeft();

    /** DBus slot that Switches the current session (as returned by
      * currentSession()) with the right (or next) one in the navigation
      * tab.
      */
    void moveSessionRight();

275
276
277
    /** DBus slot that sets ALL tabs' width to match their text */
    void setTabWidthToText(bool);

278
private slots:
279
280
281
282
283
    // called when the "Split View Left/Right" menu item is selected
    void splitLeftRight();
    void splitTopBottom();
    void closeActiveView();
    void closeOtherViews();
284
285
    void expandActiveView();
    void shrinkActiveView();
286

287
    // called when the "Detach View" menu item is selected
288
    void detachActiveView();
289
    void updateDetachViewState();
290

291
292
    // called when a session terminates - the view manager will delete any
    // views associated with the session
293
    void sessionFinished();
294
295
    // called when the container requests to close a particular view
    void viewCloseRequest(QWidget* widget);
296

297
298
299
    // controller detects when an associated view is given the focus
    // and emits a signal.  ViewManager listens for that signal
    // and then plugs the action into the UI
300
    //void viewFocused( SessionController* controller );
301
302
303

    // called when the active view in a ViewContainer changes, so
    // that we can plug the appropriate actions into the UI
Kurt Hindenburg's avatar
Kurt Hindenburg committed
304
    void viewActivated(QWidget* view);
305

Robert Knight's avatar
   
Robert Knight committed
306
307
308
309
310
311
    // called when "Next View" shortcut is activated
    void nextView();

    // called when "Previous View" shortcut is activated
    void previousView();

312
313
314
    // called when "Switch to last tab" shortcut is activated
    void lastView();

Robert Knight's avatar
   
Robert Knight committed
315
316
317
    // called when "Next View Container" shortcut is activated
    void nextContainer();

318
319
320
321
    // called when the views in a container owned by this view manager
    // changes
    void containerViewsChanged(QObject* container);

322
    // called when a profile changes
323
    void profileChanged(Profile::Ptr profile);
324

325
326
    void updateViewsForSession(Session* session);

327
328
329
330
    // moves active view to the left
    void moveActiveViewLeft();
    // moves active view to the right
    void moveActiveViewRight();
Jekyll Wu's avatar
Jekyll Wu committed
331
    // switches to the view at visual position 'index'
332
333
    // in the current container
    void switchToView(int index);
334

335
336
    // called when a SessionController gains focus
    void controllerChanged(SessionController* controller);
337

Jekyll Wu's avatar
Jekyll Wu committed
338
339
    // called when a ViewContainer requests a view be
    // moved
340
    void containerMoveViewRequest(int index, int id, bool& success);
341

342
343
    void detachView(ViewContainer* container, QWidget* view);

344
    void closeTabFromContainer(ViewContainer* container, QWidget* view);
345

346
private:
347
348
    void createView(Session* session, ViewContainer* container, int index);
    const ColorScheme* colorSchemeForProfile(const Profile::Ptr profile) const;
349

350
    void setupActions();
Robert Knight's avatar
   
Robert Knight committed
351
    void focusActiveView();
352
353
    void registerView(TerminalDisplay* view);
    void unregisterView(TerminalDisplay* view);
354

Jekyll Wu's avatar
Jekyll Wu committed
355
    // takes a view from a view container owned by a different manager and places it in
356
    // newContainer owned by this manager
Kurt Hindenburg's avatar
Kurt Hindenburg committed
357
    void takeView(ViewManager* otherManager , ViewContainer* otherContainer, ViewContainer* newContainer, TerminalDisplay* view);
358
    void splitView(Qt::Orientation orientation);
359

Robert Knight's avatar
Robert Knight committed
360
    // creates a new container which can hold terminal displays
361
    ViewContainer* createContainer();
362
363
364
    // removes a container and emits appropriate signals
    void removeContainer(ViewContainer* container);

365
    // creates a new terminal display
366
367
368
    // the 'session' is used so that the terminal display's random seed
    // can be set to something which depends uniquely on that session
    TerminalDisplay* createTerminalDisplay(Session* session = 0);
369

Robert Knight's avatar
Robert Knight committed
370
371
372
    // creates a new controller for a session/display pair which provides the menu
    // actions associated with that view, and exposes basic information
    // about the session ( such as title and associated icon ) to the display.
373
    SessionController* createController(Session* session , TerminalDisplay* display);
374

375
376
    // create menu for 'new tab' button
    QMenu* createNewViewMenu();
377
private:
378
    QPointer<ViewSplitter>          _viewSplitter;
379
    QPointer<SessionController>     _pluggedController;
380

Kurt Hindenburg's avatar
Kurt Hindenburg committed
381
    QHash<TerminalDisplay*, Session*> _sessionMap;
382

383
384
    KActionCollection*                  _actionCollection;
    QSignalMapper*                      _containerSignalMapper;
385
386

    QMenu* _newViewMenu;
387
388
389
390
391
392
393

    NavigationMethod  _navigationMethod;

    ViewContainer::NavigationDisplayMode _navigationVisibility;
    ViewContainer::NavigationPosition _navigationPosition;
    bool _showQuickButtons;
    NewTabBehavior _newTabBehavior;
394
395
396

    int _managerId;
    static int lastManagerId;
397
398
};

Stephan Binner's avatar
Stephan Binner committed
399
}
400

401
#endif