QSapecNG
 All Classes Functions Enumerations Properties
schematicscene.h
00001 /*
00002     QSapecNG - Qt based SapecNG GUI front-end
00003     Copyright (C) 2009, Michele Caini
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef SCHEMATICSCENE_H
00021 #define SCHEMATICSCENE_H
00022 
00023 
00024 #include <QtGui/QGraphicsScene>
00025 
00026 #include <QtCore/QList>
00027 #include <QtCore/QRect>
00028 #include <QtCore/QHash>
00029 #include <QtCore/QPointF>
00030 #include <QtCore/QVariant>
00031 #include <QtCore/QMetaType>
00032 #include <QtCore/QPointer>
00033 
00034 #include <memory>
00035 
00036 
00037 class QMenu;
00038 class QEvent;
00039 class QPainter;
00040 class QKeyEvent;
00041 class QUndoStack;
00042 class QUndoCommand;
00043 class QGraphicsItem;
00044 class QGraphicsSceneMouseEvent;
00045 class QGraphicsSceneDragDropEvent;
00046 class QGraphicsSceneContextMenuEvent;
00047 
00048 class QtBoolPropertyManager;
00049 class QtGroupPropertyManager;
00050 class QtStringPropertyManager;
00051 
00052 class QtLineEditFactory;
00053 class QtCheckBoxFactory;
00054 
00055 class QtAbstractPropertyBrowser;
00056 class QtProperty;
00057 
00058 
00059 namespace qsapecng
00060 {
00061 
00062 
00063 class Component;
00064 class Item;
00065 class Wire;
00066 
00067 
00068 class SchematicScene: public QGraphicsScene
00069 {
00070 
00071   Q_OBJECT
00072 
00073 public:
00074   enum { Ground = 0 };
00075   enum { GridStep = 10 };
00076 
00077   enum SupportedItemType {
00078     NilItemType = 0,
00079     
00080     UserDefItemType,
00081 
00082     CapacitorItemType,
00083     CCCSItemType,
00084     CCVSItemType,
00085     ConductanceItemType,
00086     CurrentSourceItemType,
00087     InductorItemType,
00088     OpAmplItemType,
00089     ResistorItemType,
00090     VCCSItemType,
00091     VCVSItemType,
00092     VoltageSourceItemType,
00093     TransformerItemType,
00094     MutualInductanceItemType,
00095 
00096     VoltmeterItemType,
00097     AmmeterItemType,
00098 
00099     GroundItemType,
00100     PortItemType,
00101     OutItemType,
00102     WireItemType,
00103 
00104     LabelItemType,
00105   };
00106 
00107   static QPainterPath nilPath();
00108 
00109   static QPainterPath groundPath();
00110   static QPainterPath portPath();
00111   static QPainterPath outPath();
00112   static QPainterPath voltmeterPath();
00113   static QPainterPath ammeterPath();
00114 
00115   static QPainterPath capacitorPath();
00116   static QPainterPath cccsPath();
00117   static QPainterPath ccvsPath();
00118   static QPainterPath conductancePath();
00119   static QPainterPath currentSourcePath();
00120   static QPainterPath inductorPath();
00121   static QPainterPath opAmplPath();
00122   static QPainterPath resistorPath();
00123   static QPainterPath vccsPath();
00124   static QPainterPath vcvsPath();
00125   static QPainterPath voltageSourcePath();
00126   static QPainterPath transformerPath();
00127   static QPainterPath mutualInductancePath();
00128 
00129   static QtProperty* itemProperties(QGraphicsItem* item);
00130   static SchematicScene::SupportedItemType itemType(QGraphicsItem* item);
00131   static Item* itemByType(SupportedItemType type);
00132   static QString itemNameByType(SchematicScene::SupportedItemType type);
00133   static QChar itemIdByType(SchematicScene::SupportedItemType type);
00134 
00135   static QPainterPath userDefPath(uint ports);
00136   static QList<QPointF> userDefNodes(uint ports);
00137 
00138 public:
00139   SchematicScene(QObject* parent = 0);
00140   ~SchematicScene();
00141 
00142   QPointF closestGridPoint(const QPointF& pos) const;
00143   inline bool gridVisible() const { return gridVisible_; }
00144   inline bool isGridPoint(QPointF point) const
00145     { return (point == closestGridPoint(point)); }
00146 
00147   void setActiveItem(SupportedItemType item);
00148   inline SupportedItemType activeItem() const { return activeItem_; }
00149   QList<Item*> activeItems() const;
00150 
00151   QByteArray registerUserDef(const SchematicScene& scene);
00152   std::string queryUserDef(QByteArray md5);
00153   void setUserDefRequest();
00154 
00155   void addItems(QList<QGraphicsItem*> items);
00156   void addItems(QGraphicsItem* item);
00157   void addSupportedItem(QGraphicsItem* gItem, bool init = true);
00158   void removeSupportedItem(QGraphicsItem* gItem);
00159   void clearSupportedItem(QGraphicsItem* gItem);
00160 
00161   void setWireSessionRequest(bool connectedWire);
00162   void joinWires(Wire* w1, Wire* w2);
00163   void modifyWire(Wire* wire);
00164 
00165   void createLabel();
00166 
00167   void moveSelectedItems(QPointF pos);
00168   void cutSelectedItems();
00169   void copySelectedItems();
00170   void pasteItems();
00171   void rotateSelectedItems();
00172   void mirrorSelectedItems();
00173   void binSelectedItems();
00174   void bringToFrontSelectedItem();
00175   void sendToBackSelectedItem();
00176   void clearSchematicScene();
00177 
00178   inline void setContextMenu(QMenu* menu) { contextMenu_ = menu; }
00179   inline QMenu* contextMenu() const { return contextMenu_; }
00180 
00181   inline QUndoStack* undoRedoStack() const { return undoRedoStack_; }
00182 
00183   void initializeBrowser(QtAbstractPropertyBrowser* browser);
00184   inline QtProperty* properties() const { return properties_; }
00185 
00186   QVector<int> ports();
00187   inline int size() { return portList_.size(); }
00188   void assignNodes(int seed = SchematicScene::Ground + 1);
00189 
00190 public slots:
00191   void resetNodes();
00192   void setGridVisible(bool visible = true);
00193   void resetStatus();
00194 
00195 signals:
00196   void showUserDef(SchematicScene& scene);
00197   void propertyChanged();
00198 
00199 protected:
00200   void drawBackground(QPainter* painter, const QRectF& rect);
00201   void mousePressEvent(QGraphicsSceneMouseEvent* event);
00202   void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
00203   void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
00204   void contextMenuEvent(QGraphicsSceneContextMenuEvent* event);
00205   void keyPressEvent(QKeyEvent* event);
00206   bool event(QEvent* event);
00207 
00208   void dragEnterEvent(QGraphicsSceneDragDropEvent* event);
00209   void dragMoveEvent(QGraphicsSceneDragDropEvent* event);
00210   void dragLeaveEvent(QGraphicsSceneDragDropEvent* event);
00211   void dropEvent(QGraphicsSceneDragDropEvent* event);
00212 
00213 private:
00214   void init();
00215   void setupProperties();
00216 
00217   inline bool hasActiveItem() const { return (activeItem_ != NilItemType); }
00218   inline bool hasUserDefReq() const { return userDefSessionRequested_; }
00219   inline bool hasPendingWire() const { return wireSessionRequested_; }
00220   void setPrePostWireLine(QPointF toPoint);
00221   void addWires();
00222 
00223 private:
00224   bool gridVisible_;
00225   QMenu* contextMenu_;
00226   QPointF lastMousePressPos_;
00227   bool validNode_;
00228   
00229   SupportedItemType activeItem_;
00230   Item* item_;
00231 
00232   bool wireSessionRequested_;
00233   bool horizontalFirst_;
00234   bool hasValidFromPoint_;
00235   bool connectedWire_;
00236   QPointF fromPoint_;
00237   QGraphicsLineItem* preWireItem_;
00238   QGraphicsLineItem* postWireItem_;
00239   Wire* wireInProgress_;
00240   QPen wirePen_;
00241 
00242   Item* out_;
00243   QList<Item*> outList_;
00244   QList<Item*> portList_;
00245   QList<Item*> groundList_;
00246   QList<Item*> standardList_;
00247   QList<Item*> wireList_;
00248   QList<Item*> labelList_;
00249   QList<Item*> userDefList_;
00250   
00251   int userDefSize_;
00252   QByteArray userDefMD5_;
00253   QHash<QByteArray, std::string> userDefMap_;
00254   bool userDefSessionRequested_;
00255 
00256   QUndoStack* undoRedoStack_;
00257 
00258   QtAbstractPropertyBrowser* browser_;
00259   QtBoolPropertyManager* boolManager_;
00260   QtGroupPropertyManager* groupManager_;
00261   QtStringPropertyManager* stringManager_;
00262   QtLineEditFactory* lineEditFactory_;
00263   QtCheckBoxFactory* checkBoxFactory_;
00264 
00265   QtProperty* properties_;
00266   QHash<SupportedItemType, unsigned int> itemCntMap_;
00267   QHash<SupportedItemType, QtProperty*> typeRootMap_;
00268 
00269 };
00270 
00271 
00272 }
00273 
00274 
00275 Q_DECLARE_METATYPE(QPointer<qsapecng::SchematicScene>);
00276 Q_DECLARE_METATYPE(QtProperty*);
00277 
00278 
00279 #endif // SCHEMATICSCENE_H
 All Classes Functions Enumerations Properties