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_SUBLIMEAREA_H 0008 #define KDEVPLATFORM_SUBLIMEAREA_H 0009 0010 #include <QObject> 0011 0012 #include "sublimeexport.h" 0013 0014 #include "areaindex.h" 0015 #include "sublimedefs.h" 0016 0017 #include <KConfigGroup> 0018 0019 class QAction; 0020 namespace Sublime { 0021 0022 class AreaIndex; 0023 class RootAreaIndex; 0024 class Controller; 0025 class View; 0026 class AreaPrivate; 0027 0028 /** 0029 @short Area - the universal view container 0030 0031 Area contains views and tool views, knows about their positions 0032 and provides functionality to add new (tool)views and remove existing. 0033 0034 Area takes care of all placement/configuration details so that 0035 in order for @ref MainWindow to show the area it just needs to 0036 reconstruct itself according to the area's rules. 0037 0038 Usual way of creating an area is: 0039 @code 0040 Controller *controller = new Controller(); 0041 ... //document creation code here 0042 Area *area = new Area(controller, "My Area"); 0043 area->addView(document->createView()); 0044 MainWindow *mw = new MainWindow(controller); 0045 controller->show(area, mw); 0046 @endcode 0047 */ 0048 class KDEVPLATFORMSUBLIME_EXPORT Area: public QObject { 0049 Q_OBJECT 0050 public: 0051 /**Creates and area with given @p name and adds it to the @p controller. 0052 @param controller is the controller in which this area will be available. 0053 @param name should identify this area and be unique for all areas in the controller. 0054 @ref QObject::objectName shall be used to get this name after area creation. 0055 @param title is the user-visible (translatable) title for the area. 0056 Use @ref title and @ref setTitle to operate on the title. 0057 This parameter can be omitted and then name will be used as title.*/ 0058 Area(Controller *controller, const QString &name, const QString &title = {}); 0059 Area(const Area &area); 0060 ~Area() override; 0061 0062 QString title() const; 0063 void setTitle(const QString &title); 0064 0065 QString iconName() const; 0066 void setIconName(const QString &iconName); 0067 0068 0069 /**Adds the @p view to the list of views at the given area index, 0070 after the given view @p after. 0071 0072 If @p after == 0 && controller()->openAfterCurrent(): 0073 @p view is inserted after current view 0074 If @p after == 0 && !controller()->openAfterCurrent(): 0075 @p view is inserted at the last position. 0076 */ 0077 void addView(View *view, AreaIndex *index, View *after = nullptr); 0078 0079 /**Adds the @p view to the list of views at the current area index.*/ 0080 void addView(View *view, View *after = nullptr); 0081 /**Adds the @p view to the area splitting the @p viewToSplit using 0082 given @p orientation. 0083 @p view Will be in the second child index of the area-index containing the view. 0084 */ 0085 void addView(View *view, View *viewToSplit, Qt::Orientation orientation); 0086 /**Adds the @p view to the area splitting the area index @p indexToSplit using 0087 given @p orientation. 0088 @p view Will be in the <b>second</b> child index of the area-index containing the view. 0089 */ 0090 void addView(View *view, AreaIndex *indexToSplit, Qt::Orientation orientation); 0091 /**Removes the @p view from the area. Does not delete it. */ 0092 View* removeView(View *view); 0093 /**@return the list of all views in this area in no particular order. To 0094 process the views in ordered manner (tree walk) use @ref walkViews method. 0095 This method is added only for convenience.*/ 0096 QList<View*> views(); 0097 0098 /** Removes all views from this area and deletes them. 0099 * If an open document has changes, and it is the last view of that document, 0100 * the user may push 'Cancel', and the view will stay active. 0101 * @param silent If this is true, the user is never asked for feedback. */ 0102 void clearViews(bool silent = false); 0103 0104 /** 0105 * Returns the view that was last stored through setActiveView(view), or zero 0106 * if the view was deleted or it was never set. 0107 */ 0108 View* activeView() const; 0109 0110 /** 0111 * Allows marking a view as active that can later be retrieved through activeView() 0112 */ 0113 void setActiveView(View* view); 0114 0115 /** Closes and deletes the view, asking the user for feedback if needed. 0116 * Closes the document if it is the last view. 0117 * Does allow breaking the closing process. 0118 * If it is the last view of the document that has changes, and the user pushed 'Cancel', 0119 * false will be returned, and the view will not be closed. 0120 * @param silent If this is false, the user will be asked for feedback. Otherwise he won't. 0121 */ 0122 bool closeView(View* view, bool silent = false); 0123 0124 /**@return the index of view or 0 if it can not be found.*/ 0125 AreaIndex *indexOf(View *view); 0126 /**@return the root index of the area. Root index always exists so this 0127 method will never return 0.*/ 0128 RootAreaIndex *rootIndex() const; 0129 0130 /**Adds the tool view to the area. Area will use its configuration and restore 0131 the proper position for the tool view when necessary. If it has no configuration 0132 for this view, it will use @p defaultPosition.*/ 0133 void addToolView(View *toolView, Position defaultPosition); 0134 0135 /**Removes the tool view from the area.*/ 0136 View* removeToolView(View *toolView); 0137 0138 /**Moves the tool view to a different position. */ 0139 void moveToolView(View *toolView, Position newPosition); 0140 0141 /**Raise tool view.*/ 0142 void raiseToolView(View *toolView); 0143 /**@return the list of tool views in the area. No particular sort order is guaranteed.*/ 0144 const QList<View*> &toolViews() const; 0145 /**@return the current position of @p toolView in the area.*/ 0146 Position toolViewPosition(View *toolView) const; 0147 0148 /* Returns true if this area actively desires to show a tool view 0149 with id of 'id'. The area, of course, will show any tool view 0150 added with 'addToolView', however, this method can be used 0151 to guess a set of tool views that make most sense to be added. */ 0152 bool wantToolView(const QString& id); 0153 void setShownToolViews(Sublime::Position pos, const QStringList& ids); 0154 QStringList shownToolViews(Sublime::Position pos) const; 0155 void setDesiredToolViews( 0156 const QMap<QString, Sublime::Position>& desiredToolViews); 0157 0158 void save(KConfigGroup& group) const; 0159 void load(const KConfigGroup& group); 0160 0161 /**@return the controller for this area.*/ 0162 Controller *controller() const; 0163 0164 ///Returns the currently set working-set for this area. 0165 QString workingSet() const; 0166 ///Returns if the currently set working-set is persistent. 0167 ///@note This is just a hint and not synchronized with the actual working set. 0168 bool workingSetPersistent() const; 0169 ///Sets the working-set for this area. The working-set is just a marker, and does nothing 0170 ///within Area. 0171 ///The actual view management has to be implemented in the entity that knows more 0172 ///about possible views, documents, etc. (kdevplatform/shell) 0173 ///@warning (KDevelop): Before calling this, make sure that all views are saved! (see IDocumentController::saveAllDocumentsForWindow) 0174 void setWorkingSet(const QString &name, bool persistent = true, Area *oldArea = nullptr); 0175 0176 /**Walker mode to determine the behavior of area walkers.*/ 0177 enum WalkerMode { 0178 StopWalker, /**< Stop after processing this area index or tool view */ 0179 ContinueWalker /**< Continue walking */ 0180 }; 0181 0182 /**Walks the tree of area indices and executes the operator. It will always walk the 0183 tree of views from top to bottom from left to right. 0184 0185 Operator should be the class with <i>WalkerResult operator()(AreaIndex *index)</i> 0186 method. That method should return Area::StopWalker if the walker has to stop at current index 0187 or Area::ContinueWalker to continue. 0188 0189 Example (operator to print the indices, assumes hypothetical operator <<()): 0190 @code 0191 struct MyOperator { 0192 WalkerMode operator()(AreaIndex *index) { 0193 std::cerr << index << std::endl; 0194 return Area::ContinueWalker; 0195 } 0196 }; 0197 ... 0198 MyOperator op; 0199 walkViews(op, rootIndex()) 0200 @endcode*/ 0201 template <typename Operator> 0202 void walkViews(Operator &op, AreaIndex *index); 0203 0204 /**Walks the list of tool views. The order in which tool views are walked is not specified. 0205 0206 Operator should be the class with <i>bool operator()(View *view, Sublime::Position position)</i> 0207 method. That method should return Area::StopWalker if the walker has to stop at current index 0208 or Area::ContinueWalker to continue. 0209 0210 Example (operator to print the list of views): 0211 @code 0212 struct MyOperator { 0213 WalkerMode operator()(View *view, Sublime::Position position) { 0214 std::cerr << view << " at position " << position << std::endl; 0215 return Area::ContinueWalker; 0216 } 0217 }; 0218 ... 0219 MyOperator op; 0220 walkToolViews(op, Sublime::AllPositions) 0221 @endcode 0222 */ 0223 template <typename Operator> 0224 void walkToolViews(Operator &op, Positions positions); 0225 0226 /** Adds an action to the area. They will be made available from different places, like the Area Display*/ 0227 void addAction(QAction* action); 0228 0229 /** @returns the actions related to the area */ 0230 QList<QAction*> actions() const; 0231 0232 /** 0233 * Closes all the views and requests the working set to be cleared. 0234 * Works even though the area isn't opened yet 0235 */ 0236 void clearDocuments(); 0237 0238 Q_SIGNALS: 0239 /**Emitted when a new view is added to the area.*/ 0240 void viewAdded(Sublime::AreaIndex*, Sublime::View*); 0241 /**Emitted when a view is going to be removed from the area.*/ 0242 void aboutToRemoveView(Sublime::AreaIndex*, Sublime::View*); 0243 /**Emitted when a view was removed from the area.*/ 0244 void viewRemoved(Sublime::AreaIndex*, Sublime::View*); 0245 /**Emitted when a new tool view is added to the area.*/ 0246 void toolViewAdded(Sublime::View*, Sublime::Position); 0247 /**Emitted when a tool view is requesting to be raised.*/ 0248 void requestToolViewRaise(Sublime::View*); 0249 /**Emitted when a tool view is going to be removed from the area.*/ 0250 void aboutToRemoveToolView(Sublime::View*, Sublime::Position); 0251 /**Emitted when a tool view is moved to a different position.*/ 0252 void toolViewMoved(Sublime::View*, Sublime::Position); 0253 /**Emitted before the working-set is changed.*/ 0254 void changingWorkingSet(Sublime::Area* area, Sublime::Area* oldArea, const QString& from, const QString& to); 0255 /**Emitted after the working-set was changed.*/ 0256 void changedWorkingSet(Sublime::Area* area, Sublime::Area* oldArea, const QString& from, const QString& to); 0257 /** notifies the working set that it should clear */ 0258 void clearWorkingSet(Sublime::Area* area); 0259 0260 private Q_SLOTS: 0261 void positionChanged(Sublime::View*, int); 0262 void actionDestroyed(QObject* action); 0263 0264 private: 0265 template <typename Operator> 0266 WalkerMode walkViewsInternal(Operator &op, AreaIndex *index); 0267 0268 void initialize(); 0269 0270 private: 0271 const QScopedPointer<class AreaPrivate> d_ptr; 0272 Q_DECLARE_PRIVATE(Area) 0273 }; 0274 0275 } 0276 0277 #include "areawalkers.h" 0278 0279 #endif 0280