File indexing completed on 2024-05-12 16:39:52

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2008-2010 Jarosław Staniek <staniek@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KFD_WIDGETTREEWIDGET_H
0022 #define KFD_WIDGETTREEWIDGET_H
0023 
0024 #include <QTreeWidget>
0025 
0026 #include "form.h"
0027 
0028 class QContextMenuEvent;
0029 
0030 namespace KFormDesigner
0031 {
0032 
0033 class ObjectTreeItem;
0034 
0035 //! @short An item in WidgetTreeWidget associated with an ObjectTreeItem.
0036 class KFORMDESIGNER_EXPORT WidgetTreeWidgetItem : public QTreeWidgetItem
0037 {
0038 public:
0039     /*! Flags for loadTree() */
0040     enum LoadTreeFlag {
0041         NoLoadTreeFlags = 0,
0042         LoadTreeForAddedTabPage = 1
0043     };
0044     Q_DECLARE_FLAGS(LoadTreeFlags, LoadTreeFlag)
0045 
0046     //! Creates tree item. If @a forcedTabPageIndex >= 0, it is used as index for tab page.
0047     WidgetTreeWidgetItem(WidgetTreeWidgetItem *parent, ObjectTreeItem *data,
0048         LoadTreeFlags loadTreeFlags = NoLoadTreeFlags, int forcedTabPageIndex = -1,
0049         const QString& forcedTabPageName = QString());
0050 
0051     //! For TabStopDialog
0052     explicit WidgetTreeWidgetItem(QTreeWidget *tree, ObjectTreeItem *data = 0,
0053         LoadTreeFlags loadTreeFlags = NoLoadTreeFlags, int forcedTabPageIndex = -1,
0054         const QString& forcedTabPageName = QString());
0055     virtual ~WidgetTreeWidgetItem();
0056 
0057     //! \return the item name, ie the ObjectTreeItem name
0058     QString name() const;
0059 
0060     //! \return the ObjectTreeItem information associated to this item.
0061     ObjectTreeItem* data() const;
0062 
0063     //! Added to unhide.
0064     virtual QVariant data(int column, int role) const override { return QTreeWidgetItem::data(column, role); }
0065 
0066     //! Reimplemented to alter sorting for certain widget types, e.g. tab pages.
0067     virtual bool operator<( const QTreeWidgetItem & other ) const override;
0068 
0069     //! Used to alter sorting for certain widget types, e.g. tab pages.
0070     QString customSortingKey() const;
0071 
0072 protected:
0073     //! Initializes text, icon, selectable flag, custom serting key
0074     void init(int forcedTabPageIndex, const QString& forcedTabPageName);
0075     void initTextAndIcon(int forcedTabPageIndex, const QString& forcedTabPageName);
0076 
0077 
0078 private:
0079     class Private;
0080     Private* const d;
0081 };
0082 
0083 /*! @short A graphical view of Form's ObjectTree.
0084  This is a tree representin hierarchy of form widgets.
0085  The actually selected widgets are written bold
0086  and selected. Clicking on items selects the corresponding widgets on the form.
0087  */
0088 class KFORMDESIGNER_EXPORT WidgetTreeWidget : public QTreeWidget
0089 {
0090     Q_OBJECT
0091 
0092 public:
0093     //! Options for the widget's behaviour or look
0094     enum Option {
0095         NoOptions = 0,
0096         DisableSelection = 1,  //!< disables item selection
0097         DisableContextMenu = 2 //!< disables context menu
0098     };
0099     Q_DECLARE_FLAGS(Options, Option)
0100 
0101     explicit WidgetTreeWidget(QWidget *parent = 0, Options options = NoOptions);
0102 
0103     virtual ~WidgetTreeWidget();
0104 
0105     //! @return selected tree item or 0 if there is no selection or more than one item is selected.
0106     WidgetTreeWidgetItem* selectedItem() const;
0107 
0108     //! \return the pixmap name for a given class, to be shown next to the widget name.
0109     QString iconNameForClass(const QByteArray &classname) const;
0110 
0111     //! @see ObjectTreeItem* WidgetLibrary::selectableItem(ObjectTreeItem*)
0112     ObjectTreeItem* selectableItem(ObjectTreeItem* item);
0113 
0114 public Q_SLOTS:
0115     /*! Sets \a form as the current Form in the list. The list will automatically
0116      be filled with an item for each widget in the Form, and selection will be synced.
0117      Nothing happens if \a form is already the current Form.
0118      */
0119     void setForm(KFormDesigner::Form *form);
0120 
0121     /*! Sets the widget \a w as selected item, so it will be written bold.
0122      It replaces previous selection if \a flags & Form::ReplacePreviousSelection is true. */
0123     void selectWidget(QWidget *w,
0124                       KFormDesigner::Form::WidgetSelectionFlags flags = KFormDesigner::Form::ReplacePreviousSelection);
0125 
0126     /*! Adds the ObjectTreeItem \a item in the list, with the appropriate parent. */
0127     void addItem(KFormDesigner::ObjectTreeItem *item);
0128 
0129     /*! Removess the ObjectTreeItem \a item from the list. */
0130     void removeItem(KFormDesigner::ObjectTreeItem *item);
0131 
0132     /*! Renames the list item from \a oldname to \a newname. */
0133     void renameItem(const QByteArray &oldname, const QByteArray &newname);
0134 
0135 protected Q_SLOTS:
0136     /*! The selected list item has changed. */
0137     void slotSelectionChanged();
0138 
0139     /*! Called before Form object is destroyed. */
0140     void slotBeforeFormDestroyed();
0141 
0142 protected:
0143     //! Internal function to fill the list.
0144     void loadTree(ObjectTreeItem *item, WidgetTreeWidgetItem *parent,
0145         WidgetTreeWidgetItem::LoadTreeFlags flags = WidgetTreeWidgetItem::NoLoadTreeFlags);
0146 
0147     //! @return the item whose name is @a name.
0148     WidgetTreeWidgetItem* findItem(const QString &name);
0149 
0150     //! @return the item whose text in column 0 is @a text.
0151     WidgetTreeWidgetItem* findItemByFirstColumn(const QString& text);
0152 
0153     virtual void contextMenuEvent(QContextMenuEvent* e) override;
0154 
0155     void handleContextMenuEvent(QContextMenuEvent* e);
0156 
0157     void selectWidgetForItem(QTreeWidgetItem *item);
0158 
0159     //! Try to alter selection of the item is nonselectable item clicked and parent item is available.
0160     QTreeWidgetItem* tryToAlterSelection(QTreeWidgetItem* current);
0161 
0162     //! If @a item is (grand)child of tab widget, activate proper tab page.
0163     //! Do it recursively because there may be nested tab widgets.
0164     void activateTabPageIfNeeded(QTreeWidgetItem* item);
0165 
0166 private:
0167 
0168     class Private;
0169     Private* const d;
0170 
0171     friend class TabStopDialog;
0172 };
0173 
0174 Q_DECLARE_OPERATORS_FOR_FLAGS(WidgetTreeWidget::Options)
0175 Q_DECLARE_OPERATORS_FOR_FLAGS(WidgetTreeWidgetItem::LoadTreeFlags)
0176 
0177 }
0178 
0179 #endif