File indexing completed on 2024-04-28 04:37:30

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_SUBLIMEAREAINDEX_H
0008 #define KDEVPLATFORM_SUBLIMEAREAINDEX_H
0009 
0010 #include <Qt>
0011 #include <QList>
0012 #include <QScopedPointer>
0013 
0014 #include "sublimeexport.h"
0015 
0016 namespace Sublime {
0017 
0018 class View;
0019 class AreaIndexPrivate;
0020 
0021 /**
0022 @short Index denotes the position of the view in the split area.
0023 
0024 B-Tree alike structure is used to represent an area with split
0025 views. Area has a root index which can either contain one view or contain
0026 two child nodes (@p first and @p second). In the later case area
0027 is considered to be split into two parts. Each of those parts can
0028 in turn contain a view or be split (with first/second children).
0029 
0030 When a view at given index is split, then its index becomes an index of the splitter
0031 and the original view goes into the @p first part of the splitter. The new view goes as
0032 @p second part.
0033 
0034 For example, consider an area which was split once horizontally
0035 and then the second part of it was split vertically:
0036 @code
0037  1. initial state: one view in the area
0038  |----------------|
0039  |                |
0040  |       1        |
0041  |                |
0042  |----------------|
0043 
0044  Indices:
0045     root_index (view 1)
0046 
0047  2. the view is split horizontally
0048  |----------------|
0049  |       |        |
0050  |   1   |   2    |
0051  |       |        |
0052  |----------------|
0053 
0054  Indices:
0055         root_index (no view)
0056              |
0057      ----------------
0058      |              |
0059    view 1         view 2
0060 
0061  3. the second view is split vertically
0062  |----------------|
0063  |       |   2    |
0064  |   1   |--------|
0065  |       |   3    |
0066  |----------------|
0067 
0068  Indices:
0069         root_index (horizontal splitter)
0070              |
0071      ----------------
0072      |              |
0073    view 1   vertical_splitter
0074                     |
0075             -----------------
0076             |                |
0077           view 2           view 3
0078 @endcode
0079 
0080 It is possible that several "stacked" views will have the same area index.
0081 Those views can be considered as the view stack from which only one view
0082 is visible at the time.
0083 @code
0084  |----------------|
0085  |                |
0086  |    1,2,3,4     |
0087  |                |
0088  |----------------|
0089 
0090  Indices:
0091         root_index (view1, view2, view3, view4)
0092 @endcode
0093 */
0094 class KDEVPLATFORMSUBLIME_EXPORT AreaIndex {
0095 public:
0096     ~AreaIndex();
0097     AreaIndex(const AreaIndex &index);
0098 
0099     /**@return the parent index, returns 0 for root index.*/
0100     AreaIndex *parent() const;
0101 
0102     /**@return the first child index if there're any.*/
0103     AreaIndex *first() const;
0104     /**@return the second child index if there're any.*/
0105     AreaIndex *second() const;
0106     /**@return true if the index is split.*/
0107     bool isSplit() const;
0108     /**@return the orientation of the splitter for this index.*/
0109     Qt::Orientation orientation() const;
0110     /**Set the orientation of the splitter for this index.*/
0111     void setOrientation(Qt::Orientation orientation);
0112 
0113     /**Adds view to the list of views in this position.
0114     Does nothing if the view is already split.
0115     @param after if not 0, new view will be placed after this one.
0116     @param view the view to be added.*/
0117     void add(View *view, View *after = nullptr);
0118     /**Removes view and unsplits the parent index when no views
0119     are left at the current index.*/
0120     void remove(View *view);
0121     /**Splits the view in this position by given @p orientation
0122     and adds the @p newView into the splitter.
0123     Does nothing if the view is already split.
0124     @p newView will be in the <b>second</b> child index.*/
0125     void split(View *newView, Qt::Orientation orientation);
0126     /**Splits the view in this position by given @p orientation.
0127      * @p moveViewsToSecondChild Normally, the existing views in this index are moved to the first sub-index.
0128      *                           If this is true, the views are moved to the _second_ sub-index instead.
0129      * Does nothing if the view is already split.*/
0130     void split(Qt::Orientation orientation, bool moveViewsToSecondChild = false);
0131     /**Unsplits the index removing the given @p childToRemove and moving the contents
0132     of another child to this index.*/
0133     void unsplit(AreaIndex *childToRemove);
0134 
0135     /** Returns a text-representation of the architecture of this area index and sub-indices. */
0136     QString print() const;
0137     
0138     /**@return the stacked view in @p position,
0139     returns 0 for splitter's indices and when there's no view at the @p position.*/
0140     View *viewAt(int position) const;
0141     /**@return the number of stacked views.*/
0142     int viewCount() const;
0143     /**@return true if there's a stacked @p view at this index.*/
0144     bool hasView(View *view) const;
0145     /**@return the list of views at this index.*/
0146     const QList<View*>& views() const;
0147     /** Moves @p view to position @p newPos */
0148     void moveViewPosition(View* view, int newPos);
0149 
0150 protected:
0151     /**Constructor for Root index.*/
0152     AreaIndex();
0153 
0154 private:
0155     /**Constructor for indices other than root.*/
0156     explicit AreaIndex(AreaIndex *parent);
0157 
0158     /**Sets the parent for this index.*/
0159     void setParent(AreaIndex *parent);
0160 
0161     /**Copies the data from this index to @p target.*/
0162     void moveViewsTo(AreaIndex *target);
0163     /**Copies the children indices from this index to @p target.*/
0164     void copyChildrenTo(AreaIndex *target);
0165 
0166 private:
0167     const QScopedPointer<class AreaIndexPrivate> d_ptr;
0168     Q_DECLARE_PRIVATE(AreaIndex)
0169 };
0170 
0171 /**
0172 @short Root Area Index
0173 
0174 This is the special index class returned by @ref Area::rootIndex().
0175 Doesn't provide any additional functionality beyond AreaIndex.
0176 */
0177 class KDEVPLATFORMSUBLIME_EXPORT RootAreaIndex: public AreaIndex {
0178 public:
0179     RootAreaIndex();
0180 private:
0181     class RootAreaIndexPrivate* const d = nullptr;
0182 };
0183 
0184 }
0185 
0186 #endif
0187