File indexing completed on 2024-05-05 05:46:07

0001 /***************************************************************************
0002  *   Copyright (C) 2004-2006 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 MULTIINPUTGATE_H
0012 #define MULTIINPUTGATE_H
0013 
0014 #include "component.h"
0015 #include "logic.h"
0016 
0017 const int maxGateInput = 256;
0018 
0019 /**
0020 @author David Saxton
0021 */
0022 class MultiInputGate : public CallbackClass, public Component
0023 {
0024 public:
0025     /**
0026      * @param rectangularShapeText is the text displayed in the logic symbol
0027      * when drawing in rectangular mode, e.g. "&" for AND and "=1" for XOR.
0028      * @param invertedOutput true for NAND, NOR, XNOR; false for AND, OR, XOR
0029      * @param baseWidth is the width of the logic gate when drawing in
0030      * distinctive mode.
0031      * @param likeOR true for OR, NOR, XOR, XNOR (specifically, this value
0032      * used in computer the length of input pins, as OR types have a curvy
0033      * base when the shape is distinctive).
0034      */
0035     MultiInputGate(ICNDocument *icnDocument, bool newItem, const char *id, const QString &rectangularShapeText, bool invertedOutput, int baseWidth, bool likeOR);
0036     ~MultiInputGate() override;
0037 
0038 protected:
0039     enum LogicSymbolShape { Distinctive, Rectangular };
0040 
0041     void updateAttachedPositioning() override;
0042     void slotUpdateConfiguration() override;
0043 public: // internal interfaces
0044     virtual void inStateChanged(bool newState) = 0;
0045 protected:
0046     /**
0047      * This will draw the shape if the logic symbol is currently
0048      * rectangular. Distinctive shapes should be drawn in the respective
0049      * subclasses.
0050      */
0051     void drawShape(QPainter &p) override;
0052     void dataChanged() override;
0053     void updateInputs(int newNum);
0054     /**
0055      * @return what the width should be according to the current logic
0056      * symbol shape.
0057      */
0058     int logicSymbolShapeToWidth() const;
0059     /**
0060      * Updates the display text for the logic symbol depending on its shape.
0061      */
0062     void updateSymbolText();
0063 
0064     int m_numInputs;
0065     int m_distinctiveWidth;
0066     LogicIn *inLogic[maxGateInput];
0067     ECNode *inNode[maxGateInput];
0068     LogicOut *m_pOut;
0069     LogicSymbolShape m_logicSymbolShape;
0070     QString m_rectangularShapeText;
0071     bool m_bInvertedOutput;
0072     bool m_bLikeOR;
0073 
0074 private:
0075     /**
0076      * Reads the logic symbol shape from KTLConfig.
0077      */
0078     void updateLogicSymbolShape();
0079 
0080     bool m_bDoneInit;
0081 };
0082 
0083 /**
0084 @short Boolean XNOR
0085 @author David Saxton
0086 */
0087 class ECXnor : public MultiInputGate
0088 {
0089 public:
0090     ECXnor(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0091     ~ECXnor() override;
0092 
0093     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0094     static LibraryItem *libraryItem();
0095 
0096 protected:
0097     void inStateChanged(bool newState) override;
0098     void drawShape(QPainter &p) override;
0099 };
0100 
0101 /**
0102 @short Boolean XOR
0103 @author David Saxton
0104 */
0105 class ECXor : public MultiInputGate
0106 {
0107 public:
0108     ECXor(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0109     ~ECXor() override;
0110 
0111     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0112     static LibraryItem *libraryItem();
0113 
0114 protected:
0115     void inStateChanged(bool newState) override;
0116     void drawShape(QPainter &p) override;
0117 };
0118 
0119 /**
0120 @short Boolean OR
0121 @author David Saxton
0122 */
0123 class ECOr : public MultiInputGate
0124 {
0125 public:
0126     ECOr(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0127     ~ECOr() override;
0128 
0129     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0130     static LibraryItem *libraryItem();
0131 
0132 protected:
0133     void inStateChanged(bool newState) override;
0134     void drawShape(QPainter &p) override;
0135 };
0136 
0137 /**
0138 @short Boolean NOR
0139 @author David Saxton
0140 */
0141 class ECNor : public MultiInputGate
0142 {
0143 public:
0144     ECNor(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0145     ~ECNor() override;
0146 
0147     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0148     static LibraryItem *libraryItem();
0149 
0150 protected:
0151     void inStateChanged(bool newState) override;
0152     void drawShape(QPainter &p) override;
0153 };
0154 
0155 /**
0156 @short Boolean NAND
0157 @author David Saxton
0158 */
0159 class ECNand : public MultiInputGate
0160 {
0161 public:
0162     ECNand(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0163     ~ECNand() override;
0164 
0165     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0166     static LibraryItem *libraryItem();
0167 
0168 protected:
0169     void inStateChanged(bool newState) override;
0170     void drawShape(QPainter &p) override;
0171 };
0172 
0173 /**
0174 @short Boolean AND
0175 @author David Saxton
0176 */
0177 class ECAnd : public MultiInputGate
0178 {
0179 public:
0180     ECAnd(ICNDocument *icnDocument, bool newItem, const char *id = nullptr);
0181     ~ECAnd() override;
0182 
0183     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0184     static LibraryItem *libraryItem();
0185 
0186 protected:
0187     void inStateChanged(bool newState) override;
0188     void drawShape(QPainter &p) override;
0189 };
0190 
0191 #endif