2 This file is part of the Konsole Terminal.
4 Copyright 2006-2008 Robert Knight <robertknight@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #ifndef VIEWCONTAINER_H
23 #define VIEWCONTAINER_H
26 #include <QtCore/QObject>
27 #include <QtCore/QPointer>
28 #include <QtCore/QHash>
29 #include <QtCore/QList>
30 #include <QtGui/QBoxLayout>
34 #include <KPushButton>
41 // TabbedViewContainer
60 class IncrementalSearchBar
;
63 * An interface for container widgets which can hold one or more views.
65 * The container widget typically displays a list of the views which
66 * it has and provides a means of switching between them.
68 * Subclasses should reimplement the addViewWidget() and removeViewWidget() functions
69 * to actually add or remove view widgets from the container widget, as well
70 * as updating any navigation aids.
72 class ViewContainer
: public QObject
79 * This enum describes the options for positioning the
80 * container's navigation widget.
82 enum NavigationPosition
84 /** Position the navigation widget above the views. */
85 NavigationPositionTop
,
86 /** Position the navigation widget below the views. */
87 NavigationPositionBottom
,
88 /** Position the navigation widget to the left of the views. */
89 NavigationPositionLeft
,
90 /** Position the navigation widget to the right of the views. */
91 NavigationPositionRight
95 * Constructs a new view container with the specified parent.
97 * @param position The initial position of the navigation widget
98 * @param parent The parent object of the container
100 ViewContainer(NavigationPosition position
, QObject
* parent
);
103 * Called when the ViewContainer is destroyed. When reimplementing this in
104 * subclasses, use object->deleteLater() to delete any widgets or other objects
105 * instead of 'delete object'.
107 virtual ~ViewContainer();
109 /** Returns the widget which contains the view widgets */
110 virtual QWidget
* containerWidget() const = 0;
113 * This enum describes the options for showing or hiding the
114 * container's navigation widget.
116 enum NavigationDisplayMode
118 /** Always show the navigation widget. */
119 AlwaysShowNavigation
,
120 /** Always hide the navigation widget. */
121 AlwaysHideNavigation
,
122 /** Show the navigation widget only when the container has more than one view. */
123 ShowNavigationAsNeeded
126 * Sets the visibility of the view container's navigation widget.
128 * The ViewContainer sub-class is responsible for ensuring that this
129 * setting is respected as views are added or removed from the
132 * ViewContainer sub-classes should reimplement the
133 * navigationDisplayModeChanged() method to respond to changes
136 void setNavigationDisplayMode(NavigationDisplayMode mode
);
138 * Returns the current mode for controlling the visibility of the
139 * the view container's navigation widget.
141 NavigationDisplayMode
navigationDisplayMode() const;
144 * Sets the position of the navigation widget with
145 * respect to the main content area.
147 * Depending on the ViewContainer subclass, not all
148 * positions from the NavigationPosition enum may be
149 * supported. A list of supported positions can be
150 * obtained by calling supportedNavigationPositions()
152 * ViewContainer sub-classes should re-implement the
153 * navigationPositionChanged() method to respond
154 * to changes of this property.
156 void setNavigationPosition(NavigationPosition position
);
159 * Returns the position of the navigation widget with
160 * respect to the main content area.
162 NavigationPosition
navigationPosition() const;
165 * Returns the list of supported navigation positions.
166 * The supported positions will depend upon the type of the
167 * navigation widget used by the ViewContainer subclass.
169 * The base implementation returns one item, NavigationPositionTop
171 virtual QList
<NavigationPosition
> supportedNavigationPositions() const;
173 /** Adds a new view to the container widget */
174 void addView(QWidget
* view
, ViewProperties
* navigationItem
, int index
= -1);
176 /** Removes a view from the container */
177 void removeView(QWidget
* view
);
179 /** Returns the ViewProperties instance associated with a particular view in the container */
180 ViewProperties
* viewProperties( QWidget
* view
);
182 /** Returns a list of the contained views */
183 const QList
<QWidget
*> views();
186 * Returns the view which currently has the focus or 0 if none
187 * of the child views have the focus.
189 virtual QWidget
* activeView() const = 0;
192 * Changes the focus to the specified view and updates
193 * navigation aids to reflect the change.
195 virtual void setActiveView(QWidget
* widget
) = 0;
198 * @return the search widget for this view
200 IncrementalSearchBar
* searchBar();
202 /** Changes the active view to the next view */
203 void activateNextView();
205 /** Changes the active view to the previous view */
206 void activatePreviousView();
209 * This enum describes the directions
210 * in which views can be re-arranged within the container
211 * using the moveActiveView() method.
215 /** Moves the view to the left. */
217 /** Moves the view to the right. */
222 * Moves the active view within the container and
223 * updates the order in which the views are shown
224 * in the container's navigation widget.
226 * The default implementation does nothing.
228 void moveActiveView( MoveDirection direction
);
230 /** Enum describing extra UI features which can be
231 * provided by the container. */
234 /** Provides a button which can be clicked to create new views quickly.
235 * When the button is clicked, a newViewRequest() signal is emitted. */
237 /** Provides a button which can be clicked to close views quickly. */
240 Q_DECLARE_FLAGS(Features
,Feature
)
242 * Sets which additional features are enabled in this container.
243 * The default implementation does thing. Sub-classes should re-implement this
244 * to hide or show the relevant parts of their UI
246 virtual void setFeatures(Features features
);
247 /** Returns a bitwise-OR of enabled extra UI features. See setFeatures() */
248 Features
features() const;
249 /** Returns a bitwise-OR of supported extra UI features. The default
250 * implementation returns 0 (no extra features) */
251 virtual Features
supportedFeatures() const
253 /** Sets the menu to be shown when the new view button is clicked.
254 * Only valid if the QuickNewView feature is enabled.
255 * The default implementation does nothing. */
256 virtual void setNewViewMenu(QMenu
* menu
) { Q_UNUSED(menu
); }
259 /** Emitted when the container is deleted */
260 void destroyed(ViewContainer
* container
);
262 /** Emitted when the container has no more children */
263 void empty(ViewContainer
* container
);
265 /** Emitted when the user requests to duplicate a view */
266 void duplicateRequest( ViewProperties
* properties
);
268 /** Emitted when the user requests to close a view */
269 void closeRequest(QWidget
* activeView
);
271 /** Emitted when the user requests to open a new view */
272 void newViewRequest();
275 * Emitted when the user requests to move a view from another container
276 * into this container. If 'success' is set to true by a connected slot
277 * then the original view will be removed.
279 * @param index Index at which to insert the new view in the container or -1
280 * to append it. This index should be passed to addView() when the new view
282 * @param id The identifier of the view.
283 * @param success The slot handling this signal should set this to true if the
284 * new view was successfully created.
286 void moveViewRequest(int index
,int id
,bool& success
);
288 /** Emitted when the active view changes */
289 void activeViewChanged( QWidget
* view
);
291 /** Emitted when a view is added to the container. */
292 void viewAdded(QWidget
* view
, ViewProperties
* properties
);
294 /** Emitted when a view is removed from the container. */
295 void viewRemoved(QWidget
* view
);
299 * Performs the task of adding the view widget
300 * to the container widget.
302 virtual void addViewWidget(QWidget
* view
,int index
) = 0;
304 * Performs the task of removing the view widget
305 * from the container widget.
307 virtual void removeViewWidget(QWidget
* view
) = 0;
310 * Called when the navigation display mode changes.
311 * See setNavigationDisplayMode
313 virtual void navigationDisplayModeChanged(NavigationDisplayMode
) {}
316 * Called when the navigation position changes to re-layout
317 * the container and place the navigation widget in the
318 * specified position.
320 virtual void navigationPositionChanged(NavigationPosition
) {}
322 /** Returns the widgets which are associated with a particular navigation item */
323 QList
<QWidget
*> widgetsForItem( ViewProperties
* item
) const;
326 * Rearranges the order of widgets in the container.
328 * @param fromIndex Current index of the widget to move
329 * @param toIndex New index for the widget
331 virtual void moveViewWidget( int fromIndex
, int toIndex
);
334 void viewDestroyed(QObject
* view
);
335 void searchBarDestroyed();
338 NavigationDisplayMode _navigationDisplayMode
;
339 NavigationPosition _navigationPosition
;
340 QList
<QWidget
*> _views
;
341 QHash
<QWidget
*,ViewProperties
*> _navigation
;
343 IncrementalSearchBar
* _searchBar
;
345 Q_DECLARE_OPERATORS_FOR_FLAGS(ViewContainer::Features
)
347 class TabbedViewContainer
;
350 // to allow for tweaks to the tab bar required by TabbedViewContainer.
351 class ViewContainerTabBar
: public KTabBar
356 ViewContainerTabBar(QWidget
* parent
,TabbedViewContainer
* container
);
358 // returns a pixmap image of a tab for use with QDrag
359 QPixmap
dragDropPixmap(int tab
);
362 virtual QSize
tabSizeHint(int index
) const;
363 virtual void dragEnterEvent(QDragEnterEvent
* event
);
364 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
365 virtual void dragMoveEvent(QDragMoveEvent
* event
);
366 virtual void dropEvent(QDropEvent
* event
);
369 // show the indicator arrow which shows where a dropped tab will
370 // be inserted at 'index'
371 void setDropIndicator(int index
, bool drawDisabled
= false);
372 // returns the index at which a tab will be inserted if the mouse
373 // in a drag-drop operation is released at 'pos'
374 int dropIndex(const QPoint
& pos
) const;
375 // returns true if the tab to be dropped in a drag-drop operation
376 // is the same as the tab at the drop location
377 bool proposedDropIsSameTab(const QDropEvent
* event
) const;
379 TabbedViewContainer
* _container
;
380 QLabel
* _dropIndicator
;
381 int _dropIndicatorIndex
;
382 bool _drawIndicatorDisabled
;
386 // this class provides a work-around for a problem in Qt 4.x
387 // where the insertItem() method only has protected access -
388 // and the TabbedViewContainer class needs to call it.
390 // and presumably for binary compatibility reasons will
391 // not be fixed until Qt 5.
392 class TabbedViewContainerLayout
: public QVBoxLayout
395 void insertItemAt( int index
, QLayoutItem
* item
)
397 insertItem(index
,item
);
402 * An alternative tabbed view container which uses a QTabBar and QStackedWidget
403 * combination for navigation instead of QTabWidget
405 class TabbedViewContainer
: public ViewContainer
409 friend class ViewContainerTabBar
;
413 * Constructs a new tabbed view container. Supported positions
414 * are NavigationPositionTop and NavigationPositionBottom.
416 TabbedViewContainer(NavigationPosition position
, QObject
* parent
);
417 virtual ~TabbedViewContainer();
419 virtual QWidget
* containerWidget() const;
420 virtual QWidget
* activeView() const;
421 virtual void setActiveView(QWidget
* view
);
422 virtual QList
<NavigationPosition
> supportedNavigationPositions() const;
423 virtual void setFeatures(Features features
);
424 virtual Features
supportedFeatures() const;
425 virtual void setNewViewMenu(QMenu
* menu
);
428 virtual void addViewWidget(QWidget
* view
, int index
);
429 virtual void removeViewWidget(QWidget
* view
);
430 virtual void navigationDisplayModeChanged(NavigationDisplayMode mode
);
431 virtual void navigationPositionChanged(NavigationPosition position
);
432 virtual void moveViewWidget( int fromIndex
, int toIndex
);
435 void updateTitle(ViewProperties
* item
);
436 void updateIcon(ViewProperties
* item
);
437 void updateActivity(ViewProperties
* item
);
438 void currentTabChanged(int index
);
439 void closeTab(int index
);
440 void closeCurrentTab();
441 void wheelScrolled(int delta
);
443 void tabDoubleClicked(int index
);
445 void startTabDrag(int index
);
447 void dynamicTabBarVisibility();
448 void setTabBarVisible(bool visible
);
449 void setTabActivity(int index
,bool activity
);
451 ViewContainerTabBar
* _tabBar
;
452 QPointer
<QStackedWidget
> _stackWidget
;
453 QPointer
<QWidget
> _containerWidget
;
454 QSpacerItem
* _tabBarSpacer
;
455 TabbedViewContainerLayout
* _layout
;
456 QHBoxLayout
* _tabBarLayout
;
457 KPushButton
* _newTabButton
;
458 KPushButton
* _closeTabButton
;
460 static const int TabBarSpace
= 2;
463 /** A plain view container with no navigation display */
464 class StackedViewContainer
: public ViewContainer
467 StackedViewContainer(QObject
* parent
);
468 virtual ~StackedViewContainer();
470 virtual QWidget
* containerWidget() const;
471 virtual QWidget
* activeView() const;
472 virtual void setActiveView(QWidget
* view
);
475 virtual void addViewWidget( QWidget
* view
, int index
);
476 virtual void removeViewWidget( QWidget
* view
);
479 QPointer
<QWidget
> _containerWidget
;
480 QPointer
<QStackedWidget
> _stackWidget
;
484 * A view container which uses a list instead of tabs to provide navigation
487 class ListViewContainer
: public ViewContainer
492 ListViewContainer(NavigationPosition position
, QObject
* parent
);
493 virtual ~ListViewContainer();
495 virtual QWidget
* containerWidget() const;
496 virtual QWidget
* activeView() const;
497 virtual void setActiveView(QWidget
* view
);
500 virtual void addViewWidget( QWidget
* view
, int index
);
501 virtual void removeViewWidget( QWidget
* view
);
504 void rowChanged( int row
);
505 void updateTitle( ViewProperties
* );
506 void updateIcon( ViewProperties
* );
509 QBrush
randomItemBackground(int randomIndex
);
511 QPointer
<QStackedWidget
> _stackWidget
;
512 QSplitter
* _splitter
;
513 QListWidget
* _listWidget
;
517 #endif //VIEWCONTAINER_H