TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
Tool.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 Copyright (c) 2008 Deepak Chandran
00004 Contact: Deepak Chandran (dchandran1@gmail.com)
00005 See COPYRIGHT.TXT
00006 
00007 The tool class is the parent class for all plugins.
00008 A Tool is a Qt Widget with a name and pointer to the Tinkercell MainWindow.
00009 
00010 
00011 ****************************************************************************/
00012 
00013 #include "MainWindow.h"
00014 #include "TextEditor.h"
00015 #include "NetworkHandle.h"
00016 #include "GraphicsScene.h"
00017 #include "Tool.h"
00018 #include "GlobalSettings.h"
00019 
00020 namespace Tinkercell
00021 {
00022         ToolGraphicsItem::ToolGraphicsItem(Tool * t) : tool(t)
00023         {
00024         }
00025 
00026         void ToolGraphicsItem::select()
00027         {
00028                 if (tool)
00029                 {
00030                         int k = tool->graphicsItems.indexOf(this);
00031                         if (k > -1)
00032                                 tool->select(k);
00033                 }
00034         }
00035 
00036         void ToolGraphicsItem::deselect()
00037         {
00038                 if (tool)
00039                 {
00040                         int k = tool->graphicsItems.indexOf(this);
00041                         if (k > -1)
00042                                 tool->deselect(k);
00043                 }
00044         }
00045 
00046         Tool::~Tool()
00047         {
00048                 for (int i=0; i < graphicsItems.size(); ++i)
00049                         if (graphicsItems[i] && !MainWindow::invalidPointers.contains((void*)(graphicsItems[i])))
00050                         {
00051                                 if (graphicsItems[i]->scene())
00052                                         graphicsItems[i]->scene()->removeItem(graphicsItems[i]);
00053                                         
00054                                 MainWindow::invalidPointers[(void*)(graphicsItems[i])] = true;
00055                                 delete graphicsItems[i];
00056                         }
00057         }
00058 
00059         Tool::Tool() : QWidget(), actionsGroup(this)
00060         {
00061                 setMouseTracking(true);
00062                 name = category = tr("");
00063                 mainWindow = 0;
00064         }
00065 
00066         Tool::Tool(const QString& Name, const QString& Cat, QWidget * parent): QWidget(parent), name(Name), category(Cat), mainWindow(0), actionsGroup(this)
00067         {               
00068         }
00069 
00070         bool Tool::setMainWindow(MainWindow * main)
00071         {
00072                 if (mainWindow == main)
00073                         return true;
00074                 mainWindow = main;
00075                 connect(&actionsGroup,SIGNAL(triggered(QAction*)),this,SLOT(actionTriggered(QAction*)));
00076                 if (main)
00077                 {
00078                         if (!main->tool(name))
00079                                 main->addTool(this);
00080                         return true;
00081                 }
00082                 return false;
00083         }
00084         
00085         void Tool::actionTriggered( QAction * action )
00086         {
00087                 int k = actionsGroup.actions().indexOf(action);
00088                 if (k > -1)
00089                         select(k);
00090         }
00091 
00092         void Tool::select(int)
00093         {
00094                 if (parentWidget() && !parentWidget()->isVisible())
00095                         parentWidget()->show();
00096                 show();
00097                 emit selected();
00098         }
00099 
00100         void Tool::deselect(int)
00101         {
00102                 hide();
00103                 emit deselected();
00104         }
00105 
00106         void ToolGraphicsItem::visible(bool b)
00107         {
00108                 QGraphicsItemGroup::setVisible(b);
00109         }
00110         
00111         NetworkWindow* Tool::currentWindow() const
00112         {
00113                 if (mainWindow)
00114                         return mainWindow->currentWindow();
00115                 return 0;               
00116         }
00117 
00118         GraphicsScene* Tool::currentScene() const
00119         {
00120                 if (mainWindow)
00121                         return mainWindow->currentScene();
00122                 return 0;
00123         }
00124 
00125         TextEditor* Tool::currentTextEditor() const
00126         {
00127                 if (mainWindow)
00128                         return mainWindow->currentTextEditor();
00129                 return 0;
00130         }
00131 
00132         NetworkHandle * Tool::currentNetwork() const
00133         {
00134                 if (mainWindow)
00135                         return mainWindow->currentNetwork();
00136                 return 0;
00137         }
00138         
00139         QString Tool::homeDir()
00140         {
00141                 return GlobalSettings::homeDir();
00142         }
00143         
00144         QString Tool::tempDir()
00145         {
00146                 return GlobalSettings::tempDir();
00147         }
00148 
00149         ToolGraphicsItem* ToolGraphicsItem::cast(QGraphicsItem* q)
00150         {
00151                 return qgraphicsitem_cast<ToolGraphicsItem*>(q);
00152         }
00153 
00154         ConsoleWindow* Tool::console()
00155         {
00156             if (mainWindow)
00157             return mainWindow->console();
00158             return 0;
00159         }
00160         
00161         void Tool::addAction(const QIcon& icon, const QString& text, const QString& tooltip)
00162         {
00163                 QAction * action = new QAction(icon,text,this);
00164                 action->setToolTip(tooltip);
00165                 actionsGroup.addAction(action);
00166         }
00167         
00168         void Tool::addGraphicsItem(ToolGraphicsItem * item)
00169         {
00170                 if (item && !graphicsItems.contains(item))
00171                         graphicsItems << item;
00172         }
00173         
00174         QPair< QList<ItemHandle*>, QList<QGraphicsItem*> > Tool::getItemsFromFile(const QString& filename)
00175         {
00176                 if (mainWindow)
00177                         return mainWindow->getItemsFromFile(filename);
00178                 return QPair< QList<ItemHandle*>, QList<QGraphicsItem*> >(QList<ItemHandle*>(),QList<QGraphicsItem*>());
00179         }
00180 }
00181 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines