File indexing completed on 2024-04-14 05:36:58

0001 /***************************************************************************
0002  *   Copyright (C) 2003-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 CNITEM_H
0012 #define CNITEM_H
0013 
0014 #include "ciwidgetmgr.h"
0015 #include "item.h"
0016 
0017 class Button;
0018 class CNItem;
0019 class ICNDocument;
0020 class Connector;
0021 class DoubleSpinBox;
0022 class LibraryItem;
0023 class Node;
0024 class QSlider;
0025 class QString;
0026 class QToolButton;
0027 class Slider;
0028 class Text;
0029 
0030 class NodeInfo
0031 {
0032 public:
0033     NodeInfo();
0034 
0035     QString id;      // External id (ICNDocument scope)
0036     Node *node;      // Pointer to the node
0037     double x;        // X position relative to item
0038     double y;        // Y position relative to item
0039     int orientation; // Orientation relative to item
0040 };
0041 
0042 typedef QMap<QString, QString> StringMap;
0043 typedef QMap<QString, NodeInfo> NodeInfoMap; // Internal id, node info
0044 typedef QList<QPointer<Connector>> ConnectorList;
0045 typedef QMap<QString, QPointer<Text>> TextMap;
0046 
0047 /**
0048 Essentially, all items that live on ICNDocument should inherit from this class.
0049 This class provides much functionality (moving items, creation of associated nodes,
0050 saving and editing of associated data, cutting / copying, etc)
0051 @short Base class for all components/flowparts/etc
0052 @author Daniel Clarke
0053 @author David Saxton
0054 */
0055 class CNItem : public Item, public CIWidgetMgr
0056 {
0057     Q_OBJECT
0058 public:
0059     CNItem(ICNDocument *_icnView, bool newItem, const QString &id);
0060     ~CNItem() override;
0061 
0062     /**
0063      * Creates a node which is attached to the item. The node will be moved
0064      * about with the item, and destroyed along with the item. The position
0065      * coordinates of the node are relative to the upper left corner of the item.
0066      * @param type See Node::node_type
0067      */
0068     Node *createNode(double _x, double _y, int orientation, const QString &name, uint type);
0069     /**
0070      * Removes a child node. You should use this function if you want to remove
0071      * any nodes during the lifetime of the CNItem.
0072      */
0073     bool removeNode(const QString &name);
0074     /**
0075      * Returns the closest node that is associated with the CNItem
0076      */
0077     Node *getClosestNode(const QPoint &pos);
0078     /**
0079      * Returns a list of connectors associated with the CNItem
0080      */
0081     ConnectorList connectorList();
0082     bool preResize(QRect sizeRect) override;
0083     bool mousePressEvent(const EventInfo &eventInfo) override;
0084     bool mouseReleaseEvent(const EventInfo &eventInfo) override;
0085     bool mouseDoubleClickEvent(const EventInfo &eventInfo) override;
0086     bool mouseMoveEvent(const EventInfo &eventInfo) override;
0087     bool wheelEvent(const EventInfo &eventInfo) override;
0088     void enterEvent(QEvent *) override;
0089     void leaveEvent(QEvent *) override;
0090     /**
0091      * ICNDocument needs to know what 'cells' a CNItem is present in,
0092      * so that connection mapping can be done to avoid CNItems.
0093      * This function will add the hit penalty to the cells pointed to
0094      * by ICNDocument::cells()
0095      */
0096     virtual void updateConnectorPoints(bool add);
0097     /**
0098      * Converts the id used to internally identify a node to the global
0099      * ICNDocument node id. eg "vss" might return "node__13".
0100      */
0101     QString nodeId(const QString &internalNodeId);
0102     /**
0103      * Returns a pointer to the node with the given internal (child) id
0104      */
0105     Node *childNode(const QString &childId);
0106     /**
0107      * Returns the node map used:
0108      * QMap<QString, NodeInfo> NodeInfoMap
0109      * It's probably best to cache this data
0110      */
0111     NodeInfoMap nodeMap() const
0112     {
0113         return m_nodeMap;
0114     }
0115     /**
0116      * Returns the TextMap used for canvas text
0117      */
0118     TextMap textMap() const
0119     {
0120         return m_textMap;
0121     }
0122     void setVisible(bool yes) override;
0123     void updateZ(int baseZ) override;
0124 
0125     ItemData itemData() const override;
0126     void restoreFromItemData(const ItemData &itemData) override;
0127     virtual void updateNodeLevels();
0128     void drawShape(QPainter &p) override;
0129 
0130 signals:
0131     /**
0132      * Emitted when the angle or flipped'ness changes. Note that CNItem doesn't
0133      * actually emit this signal - instead, Component and FlowPart classes do.
0134      */
0135     void orientationChanged();
0136 
0137 public slots:
0138     /**
0139      * Moves item - use this instead of moveBy() so that associated Nodes also get moved
0140      */
0141     void moveBy(double dx, double dy) override;
0142     /**
0143      * Remove the item and associated nodes. It appends the item to the
0144      * ICNDocument's delete list, so you must call ICNDocument::flushDeleteList()
0145      * after calling this (and possible ICNDocument::clearDeleteList() befor
0146      * calling it) The virtual function void handleRemove() is called to allow
0147      * any child classes to clear up any neccessary data (which doesn't do
0148      * anything by default), before CNItem does the rest
0149      */
0150     void removeItem() override;
0151     /**
0152      * This item has been resized, so update the nodes relative positions
0153      */
0154     virtual void updateAttachedPositioning();
0155 
0156 protected:
0157     void reparented(Item *oldParent, Item *newParent) override;
0158     void postResize() override;
0159     /**
0160      * CNItem handles drawing of text associated with the CNItem.
0161      * @param id is a unique identifier that can be used to change the text displayed.
0162      * @param pos is the position that the text occupies relative to the top left corner of the CNItem.
0163      * @param display is the actual text to be displayed.
0164      * @param internal is used to determine the z-level of the text - whether it should be below or above the item
0165      * @param flags Text alignment flags - Qt::AlignmentFlags and Qt::TextFlags OR'd together.
0166      */
0167     Text *addDisplayText(const QString &id, const QRect &pos, const QString &display, bool internal = true, int flags = Qt::AlignHCenter | Qt::AlignVCenter);
0168     void setDisplayText(const QString &id, const QString &display);
0169     /**
0170      * Remove the display text with the given id
0171      */
0172     void removeDisplayText(const QString &id);
0173     /**
0174      * Sets the right colour if selected, transforms the matrix of the painter
0175      */
0176     virtual void initPainter(QPainter &p);
0177 
0178     QPointer<ICNDocument> p_icnDocument;
0179     TextMap m_textMap;
0180     NodeInfoMap m_nodeMap;
0181     QColor m_selectedCol;
0182     QColor m_brushCol;
0183     bool b_pointsAdded;
0184 };
0185 typedef QList<CNItem *> CNItemList;
0186 
0187 #endif