File indexing completed on 2024-04-14 05:37:12

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2006 David Saxton <david@bluehaze.org>             *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #ifndef VIEWCONTAINER_H
0011 #define VIEWCONTAINER_H
0012 
0013 // #include <q3dragobject.h>
0014 #include <QList>
0015 #include <QMap>
0016 #include <QPointer>
0017 #include <QSplitter>
0018 
0019 class KConfigGroup;
0020 class KTechlab;
0021 class View;
0022 class ViewArea;
0023 class ViewContainer;
0024 
0025 class KConfig;
0026 class QHBoxLayout;
0027 class QLayout;
0028 class QSplitter;
0029 
0030 typedef QMap<uint, ViewArea *> ViewAreaMap;
0031 typedef QList<int> IntList;
0032 
0033 /**
0034 Before a ViewAre has been given a view, this is shown.
0035 \author David Saxton
0036 */
0037 class EmptyViewArea : public QWidget
0038 {
0039     Q_OBJECT
0040 public:
0041     EmptyViewArea(ViewArea *parent);
0042     ~EmptyViewArea() override;
0043 
0044 protected slots:
0045     void openDocument();
0046 
0047 protected:
0048     ViewArea *m_pViewArea;
0049 };
0050 
0051 /**
0052 Contains either exactly one View, or two ViewAreas, separated by a QSplitter.
0053 If it contains one view, then the value returned in id() is that of the view.
0054 If it contains two ViewAreas, then the value returned by id() is -1.
0055 @author David Saxton
0056 */
0057 class ViewArea : public QSplitter
0058 {
0059     Q_OBJECT
0060 public:
0061     enum Position { Right, Bottom };
0062 
0063     ViewArea(QWidget *parent, ViewContainer *viewContainer, int id, bool showOpenButton);
0064     ~ViewArea() override;
0065 
0066     ViewContainer *viewContainer() const
0067     {
0068         return p_viewContainer;
0069     }
0070     int id() const
0071     {
0072         return m_id;
0073     }
0074     /**
0075      * Splits this ViewArea into two, and returns a pointer to the new ViewArea
0076      * @param showOpenButton Whether to present the user with the EmptyViewArea
0077      * widget (i.e. the new ViewArea is not destined to be immediately filled
0078      * with a view).
0079      */
0080     ViewArea *createViewArea(Position position, uint id, bool showOpenButton);
0081     /**
0082      * Adds the given View to the main part of the layout
0083      */
0084     void setView(View *view);
0085     /**
0086      * Saves the state of this ViewArea and any contained ViewAreas
0087      */
0088     void saveState(KConfigGroup *config);
0089     /**
0090      * Restores the state of this ViewArea and any contained ViewAreas
0091      * @param groupName e.g. "ViewContainer 1"
0092      */
0093     void restoreState(KConfigGroup *config, int id, const QString &groupName);
0094     /**
0095      * Returns true if this ViewArea can save useful information as to its state
0096      * (i.e. it's children can save useful information about their state, or has
0097      * a view with a url in it)
0098      */
0099     bool canSaveUsefulStateInfo() const;
0100 
0101     static QString fileKey(int id);
0102     static QString containsKey(int id);
0103     static QString orientationKey(int id);
0104 
0105     View *view() const
0106     {
0107         return p_view;
0108     }
0109 
0110 protected slots:
0111     void viewAreaDestroyed(QObject *obj);
0112     void viewDestroyed();
0113 
0114 protected:
0115     int m_id;
0116     EmptyViewArea *m_pEmptyViewArea;
0117     QPointer<View> p_view;
0118     ViewArea *p_viewArea1;
0119     ViewArea *p_viewArea2;
0120     ViewContainer *p_viewContainer;
0121 };
0122 
0123 /**
0124 @author David Saxton
0125 */
0126 class ViewContainer : public QWidget
0127 {
0128     Q_OBJECT
0129 public:
0130     /**
0131      * Constructs a new ViewContainer, along with a default ViewArea ready for
0132      * parenting a View, with an id of 0. parent is only used if ktechlab is
0133      * null; otherwise the parent widget is ktechlab's tabWidget()
0134      */
0135     ViewContainer(const QString &caption, QWidget *parent = nullptr);
0136     ~ViewContainer() override;
0137 
0138     /**
0139      * Returns the view in the ViewArea with the given id
0140      */
0141     View *view(uint id) const;
0142     /**
0143      * Returns the ViewArea with the given id
0144      */
0145     ViewArea *viewArea(uint id) const;
0146     /**
0147      * The active view area is the one that is focused.
0148      */
0149     void setActiveViewArea(uint id);
0150     /**
0151      * Returns the id of the active ViewArea
0152      */
0153     uint activeViewArea() const
0154     {
0155         return m_activeViewArea;
0156     }
0157     /**
0158      * Returns a pointer to the view of the active view area
0159      */
0160     View *activeView() const
0161     {
0162         return view(activeViewArea());
0163     }
0164     /**
0165      * Attempts to close the given viewarea, returning true if successful (i.e
0166      * if the user did not object to the close request )
0167      */
0168     bool closeViewArea(uint id);
0169     /**
0170      * Creates a view area (parent QWidget, splitter et al) ready for inclusion
0171      * of a view.
0172      * @param relativeViewArea the viewarea to position the new viewarea next to, if -1 then is taken to be the active view area
0173      * @param position Top, Right, Bottom or Left of given relativeViewArea
0174      * @returns id of the view area, or -1 if unsucessful
0175      * @param showOpenButton Whether to present the user with the EmptyViewArea
0176      * widget (i.e. the new ViewArea is not destined to be immediately filled
0177      * with a view).
0178      */
0179     int createViewArea(int relativeViewArea, ViewArea::Position position, bool showOpenButton);
0180     /**
0181      * Attempts to close each view area, returning false if any fail to be
0182      * closed
0183      */
0184     bool closeViewContainer();
0185     /**
0186      * @returns number of views in this view container
0187      */
0188     uint viewCount() const
0189     {
0190         return m_viewAreaMap.size();
0191     }
0192     /**
0193      * Sets the pointer to the view area with the given id
0194      */
0195     void setViewAreaId(ViewArea *viewArea, uint id);
0196     /**
0197      * Removes a ViewArea from internal lists
0198      */
0199     void setViewAreaRemoved(uint id);
0200     /**
0201      * Sets the id to be "used"
0202      */
0203     void setIdUsed(int id);
0204     /**
0205      * Writes the state of the View Container (layout of views and view URLs)
0206      * to the given KConfig. Doesn't change the group - so preset it if
0207      * needed!
0208      */
0209     void saveState(KConfigGroup *config);
0210     /**
0211      * Reads in the saved config state (as written by saveState), and restores
0212      * the ViewContainer with all appropriate views open
0213      * @param groupName e.g. "ViewContainer 1"
0214      */
0215     void restoreState(KConfigGroup *config, const QString &groupName);
0216     /**
0217      * Returns a unique id (negative) for a ViewArea that is now a Parent of other ViewAreas
0218      */
0219     int uniqueParentId();
0220     /**
0221      * Returns a unique id (positive) for a new ViewArea
0222      */
0223     int uniqueNewId();
0224     /**
0225      * Returns true if this ViewArea can save useful information as to its state
0226      * (i.e. it's children can save useful information about their state, or has
0227      * a view with a url in it)
0228      */
0229     bool canSaveUsefulStateInfo() const;
0230 
0231 public slots:
0232     /**
0233      * Sets the tab caption et al from the contents of this ViewContainer
0234      */
0235     void updateCaption();
0236 
0237 protected slots:
0238     void baseViewAreaDestroyed(QObject *obj);
0239 
0240 protected:
0241     void restoreViewArea(KConfigGroup *config, int id, ViewArea *viewArea);
0242     void findActiveViewArea();
0243     int m_activeViewArea;
0244     ViewArea *m_baseViewArea;
0245     ViewAreaMap m_viewAreaMap;
0246     IntList m_usedIDs;
0247     bool b_deleted;
0248 };
0249 
0250 #endif