File indexing completed on 2024-04-21 05:44:00

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef ITEMGROUP_H
0012 #define ITEMGROUP_H
0013 
0014 #include <QPointer>
0015 
0016 class Item;
0017 class ICNDocument;
0018 class ItemDocument;
0019 class DoubleSpinBox;
0020 class ItemGroup;
0021 class MechanicsDocument;
0022 class Variant;
0023 
0024 typedef QList<QPointer<Item>> ItemList;
0025 
0026 class KtlQCanvasItem;
0027 class KtlQCanvasItemList;
0028 
0029 /**
0030 Generic base class for controlling a selection of Item. Provides
0031 some functionality such as for dealing with item data
0032 @author David Saxton
0033 */
0034 class ItemGroup : public QObject
0035 {
0036     Q_OBJECT
0037 public:
0038     ItemGroup(ItemDocument *view);
0039     ~ItemGroup() override;
0040 
0041     /**
0042      * Returns a pointer to the "active" CNItem - i.e. the last CNItem
0043      * to be added to the CNItemGroup. This will always return a pointer to
0044      * a single item, unless there are no CNItems in the group
0045      */
0046     Item *activeItem() const
0047     {
0048         return m_activeItem;
0049     }
0050     uint itemCount() const
0051     {
0052         return m_itemList.count();
0053     }
0054     virtual bool addQCanvasItem(KtlQCanvasItem *qcanvasItem) = 0;
0055     virtual void setItems(KtlQCanvasItemList list) = 0;
0056     virtual void removeQCanvasItem(KtlQCanvasItem *qcanvasItem) = 0;
0057     virtual bool contains(KtlQCanvasItem *qcanvasItem) const = 0;
0058     virtual uint count() const = 0;
0059     bool isEmpty() const
0060     {
0061         return (count() == 0);
0062     }
0063     virtual void mergeGroup(ItemGroup *group) = 0;
0064     virtual void removeAllItems() = 0;
0065     virtual void deleteAllItems() = 0;
0066     /**
0067      * Returns a list of all the Items in the group.
0068      * @param excludeParented whether to return items whose (grand-) parents are
0069      * already in the list.
0070      */
0071     ItemList items(bool excludeParented = true) const;
0072     /**
0073      * Sets the selected state of all items in the group
0074      */
0075     virtual void setSelected(bool sel) = 0;
0076 
0077     /**
0078      * Returns true iff either there are no items, or itemsAreSameType and the
0079      * value of each data (excluding hidden data) for each item is the same
0080      */
0081     bool itemsHaveSameData() const;
0082     /**
0083      * Returns truee iff either there are no items, or itemsAreSameType and the
0084      * value of the data with the given id is the same for each item
0085      */
0086     bool itemsHaveSameDataValue(const QString &id) const;
0087     /**
0088      * Returns true iff all the iff itemsHaveSameData() returns true and the
0089      * value of the data are the defaults
0090      */
0091     bool itemsHaveDefaultData() const;
0092     /**
0093      * Returns true if all the items in the group are the same (e.g.
0094      * resistors). This is checked for by looking at the ids of the items,
0095      * and seeing if the string before "__#" is the same Note: if there are zero
0096      * items in the group, then this will return true
0097      */
0098     bool itemsAreSameType() const
0099     {
0100         return b_itemsAreSameType;
0101     }
0102 
0103 public slots:
0104     /**
0105      * Align the selected items horizontally so that their positions have the
0106      * same y coordinate.
0107      */
0108     void slotAlignHorizontally();
0109     /**
0110      * Align the selected items horizontally so that their positions have the
0111      * same x coordinate.
0112      */
0113     void slotAlignVertically();
0114     /**
0115      * Distribute the selected items horizontally so that they have the same
0116      * spacing in the horizontal direction.
0117      */
0118     void slotDistributeHorizontally();
0119     /**
0120      * Distribute the selected items vertically so that they have the same
0121      * spacing in the vertical direction.
0122      */
0123     void slotDistributeVertically();
0124 
0125 signals:
0126     void itemAdded(Item *item);
0127     void itemRemoved(Item *item);
0128 
0129 protected:
0130     /**
0131      * Subclasses must call this to register the item with the data interface
0132      */
0133     void registerItem(Item *item);
0134     /**
0135      * Subclasses must call this to unregister the item with the data interface
0136      */
0137     void unregisterItem(Item *item);
0138     void updateAreSameStatus();
0139 
0140     ItemList m_itemList;
0141     bool b_itemsAreSameType;
0142     ItemDocument *p_view;
0143 
0144     ICNDocument *p_icnDocument;
0145     MechanicsDocument *p_mechanicsDocument;
0146     Item *m_activeItem;
0147 
0148 private slots:
0149     void getViewPtrs();
0150 };
0151 
0152 #endif