1 /*************************************************************************** 2 * Copyright (C) 2005 by David Saxton * 3 * david@bluehaze.org * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 ***************************************************************************/ 10 11 #ifndef ITEMGROUP_H 12 #define ITEMGROUP_H 13 14 #include <QPointer> 15 16 class Item; 17 class ICNDocument; 18 class ItemDocument; 19 class DoubleSpinBox; 20 class ItemGroup; 21 class MechanicsDocument; 22 class Variant; 23 24 typedef QList<QPointer<Item> > ItemList; 25 26 class KtlQCanvasItem; 27 class KtlQCanvasItemList; 28 29 /** 30 Generic base class for controlling a selection of Item. Provides 31 some functionality such as for dealing with item data 32 @author David Saxton 33 */ 34 class ItemGroup : public QObject 35 { 36 Q_OBJECT 37 public: 38 ItemGroup( ItemDocument *view, const char *name = nullptr ); 39 ~ItemGroup() override; 40 41 /** 42 * Returns a pointer to the "active" CNItem - i.e. the last CNItem 43 * to be added to the CNItemGroup. This will always return a pointer to 44 * a single item, unless there are no CNItems in the group 45 */ activeItem()46 Item *activeItem() const { return m_activeItem; } itemCount()47 uint itemCount() const { return m_itemList.count(); } 48 virtual bool addQCanvasItem( KtlQCanvasItem *qcanvasItem ) = 0; 49 virtual void setItems( KtlQCanvasItemList list ) = 0; 50 virtual void removeQCanvasItem( KtlQCanvasItem *qcanvasItem ) = 0; 51 virtual bool contains( KtlQCanvasItem *qcanvasItem ) const = 0; 52 virtual uint count() const = 0; isEmpty()53 bool isEmpty() const { return (count() == 0); } 54 virtual void mergeGroup( ItemGroup *group ) = 0; 55 virtual void removeAllItems() = 0; 56 virtual void deleteAllItems() = 0; 57 /** 58 * Returns a list of all the Items in the group. 59 * @param excludeParented whether to return items whose (grand-) parents are 60 * already in the list. 61 */ 62 ItemList items( bool excludeParented = true ) const; 63 /** 64 * Sets the selected state of all items in the group 65 */ 66 virtual void setSelected( bool sel ) = 0; 67 68 /** 69 * Returns true iff either there are no items, or itemsAreSameType and the 70 * value of each data (excluding hidden data) for each item is the same 71 */ 72 bool itemsHaveSameData() const; 73 /** 74 * Returns truee iff either there are no items, or itemsAreSameType and the 75 * value of the data with the given id is the same for each item 76 */ 77 bool itemsHaveSameDataValue( const QString &id ) const; 78 /** 79 * Returns true iff all the iff itemsHaveSameData() returns true and the 80 * value of the data are the defaults 81 */ 82 bool itemsHaveDefaultData() const; 83 /** 84 * Returns true if all the items in the group are the same (e.g. 85 * resistors). This is checked for by looking at the ids of the items, 86 * and seeing if the string before "__#" is the same Note: if there are zero 87 * items in the group, then this will return true 88 */ itemsAreSameType()89 bool itemsAreSameType() const { return b_itemsAreSameType; } 90 91 public slots: 92 /** 93 * Align the selected items horizontally so that their positions have the 94 * same y coordinate. 95 */ 96 void slotAlignHorizontally(); 97 /** 98 * Align the selected items horizontally so that their positions have the 99 * same x coordinate. 100 */ 101 void slotAlignVertically(); 102 /** 103 * Distribute the selected items horizontally so that they have the same 104 * spacing in the horizontal direction. 105 */ 106 void slotDistributeHorizontally(); 107 /** 108 * Distribute the selected items vertically so that they have the same 109 * spacing in the vertical direction. 110 */ 111 void slotDistributeVertically(); 112 113 signals: 114 void itemAdded( Item *item ); 115 void itemRemoved( Item *item ); 116 117 protected: 118 /** 119 * Subclasses must call this to register the item with the data interface 120 */ 121 void registerItem( Item *item ); 122 /** 123 * Subclasses must call this to unregister the item with the data interface 124 */ 125 void unregisterItem( Item *item ); 126 void updateAreSameStatus(); 127 128 ItemList m_itemList; 129 bool b_itemsAreSameType; 130 ItemDocument * p_view; 131 132 ICNDocument *p_icnDocument; 133 MechanicsDocument *p_mechanicsDocument; 134 Item *m_activeItem; 135 136 private slots: 137 void getViewPtrs(); 138 }; 139 140 #endif 141