Component.py

Go to the documentation of this file.
00001 #***************************************************************************
00002 #*                                                                         *
00003 #*   Copyright (c) 2011                                                    *  
00004 #*   Yorik van Havre <yorik@uncreated.net>                                 *  
00005 #*                                                                         *
00006 #*   This program is free software; you can redistribute it and/or modify  *
00007 #*   it under the terms of the GNU General Public License (GPL)            *
00008 #*   as published by the Free Software Foundation; either version 2 of     *
00009 #*   the License, or (at your option) any later version.                   *
00010 #*   for detail see the LICENCE text file.                                 *
00011 #*                                                                         *
00012 #*   This program is distributed in the hope that it will be useful,       *
00013 #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014 #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015 #*   GNU Library General Public License for more details.                  *
00016 #*                                                                         *
00017 #*   You should have received a copy of the GNU Library General Public     *
00018 #*   License along with this program; if not, write to the Free Software   *
00019 #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020 #*   USA                                                                   *
00021 #*                                                                         *
00022 #***************************************************************************
00023 
00024 __title__="FreeCAD Arch Component"
00025 __author__ = "Yorik van Havre"
00026 __url__ = "http://free-cad.sourceforge.net"
00027 
00028 import FreeCAD,FreeCADGui
00029 from PyQt4 import QtGui,QtCore
00030 
00031 def addToComponent(compobject,addobject):
00032     '''addToComponent(compobject,addobject): adds addobject
00033     to the given component'''
00034     if hasattr(compobject,"Additions"):
00035         if not addobject in compobject.Additions:
00036             l = compobject.Additions
00037             l.append(addobject)
00038             compobject.Additions = l
00039     elif hasattr(compobject,"Objects"):
00040         if not addobject in compobject.Objects:
00041             l = compobject.Objects
00042             l.append(addobject)
00043             compobject.Objects = l
00044 
00045 def removeFromComponent(compobject,subobject):
00046     '''removeFromComponent(compobject,subobject): subtracts subobject
00047     from the given component'''
00048     if hasattr(compobject,"Subtractions") and hasattr(compobject,"Additions"):
00049         if subobject in compobject.Subtractions:
00050             l = compobject.Subtractions
00051             l.remove(subobject)
00052             compobject.Subtractions = l
00053             subobject.ViewObject.show()
00054         elif subobject in compobject.Additions:
00055             l = compobject.Additions
00056             l.remove(subobject)
00057             compobject.Additions = l
00058             subobject.ViewObject.show()
00059         else:
00060             l = compobject.Subtractions
00061             l.append(subobject)
00062             compobject.Subtractions = l
00063     elif hasattr(compobject,"Objects"):
00064         if subobject in compobject.Objects:
00065             l = compobject.Objects
00066             l.remove(subobject)
00067             compobject.Objects = l
00068 
00069 class ComponentTaskPanel:
00070     def __init__(self):
00071         self.obj = None
00072         self.form = QtGui.QWidget()
00073         self.form.setObjectName("TaskPanel")
00074         #self.form.resize(210, 260)
00075         self.gridLayout = QtGui.QGridLayout(self.form)
00076         self.gridLayout.setObjectName("gridLayout")
00077         self.title = QtGui.QLabel(self.form)
00078         self.gridLayout.addWidget(self.title, 0, 0, 1, 1)
00079         self.listWidget = QtGui.QListWidget(self.form)
00080         self.listWidget.setObjectName("listWidget")
00081         self.gridLayout.addWidget(self.listWidget, 1, 0, 1, 2)
00082         # add button currently disabled because no way to select other objects
00083         #self.addButton = QtGui.QPushButton(self.form)
00084         #self.addButton.setObjectName("addButton")
00085         #self.addButton.setIcon(QtGui.QIcon(":/icons/Arch_Add.svg"))
00086         #self.gridLayout.addWidget(self.addButton, 2, 0, 1, 1)
00087         self.delButton = QtGui.QPushButton(self.form)
00088         self.delButton.setObjectName("delButton")
00089         self.delButton.setIcon(QtGui.QIcon(":/icons/Arch_Remove.svg"))
00090         self.gridLayout.addWidget(self.delButton, 3, 0, 1, 1)
00091         #QtCore.QObject.connect(self.addButton, QtCore.SIGNAL("clicked()"), self.addElement)
00092         QtCore.QObject.connect(self.delButton, QtCore.SIGNAL("clicked()"), self.removeElement)
00093         self.retranslateUi(self.form)
00094 
00095     def isAllowedAlterSelection(self):
00096         return True
00097 
00098     def isAllowedAlterView(self):
00099         return True
00100 
00101     def getStandardButtons(self):
00102         return int(QtGui.QDialogButtonBox.Ok)
00103         
00104     def addElement(self):
00105         sel = FreeCADGui.Selection.getSelection()
00106         if sel:
00107             for o in sel:
00108                 addToComponent(self.obj,o)
00109         self.fillList()
00110 
00111     def removeElement(self):
00112         it = self.listWidget.currentItem()
00113         if it:
00114             ob2 = FreeCAD.ActiveDocument.getObject(str(it.text()))
00115             removeFromComponent(self.obj,ob2)
00116         else:
00117             sel = FreeCADGui.Selection.getSelection()
00118             if sel:
00119                 for o in sel:
00120                     if o.Name != self.obj.Name:
00121                         removeFromComponent(self.obj,o)
00122         self.fillList()
00123                     
00124     def fillList(self):
00125         self.listWidget.clear()
00126         if hasattr(self.obj,"Base"):
00127             if self.obj.Base:
00128                 i = QtGui.QListWidgetItem(self.listWidget)
00129                 i.setText(self.obj.Base.Name)
00130                 i.setIcon(QtGui.QIcon(":/icons/Tree_Part.svg"))
00131                 self.listWidget.addItem(i)
00132         if hasattr(self.obj,"Additions"):
00133             for o in self.obj.Additions:
00134                 i = QtGui.QListWidgetItem(self.listWidget)
00135                 i.setText(o.Name)
00136                 i.setIcon(QtGui.QIcon(":/icons/Arch_Add.svg"))
00137                 self.listWidget.addItem(i)
00138         if hasattr(self.obj,"Subtractions"):
00139             for o in self.obj.Subtractions:
00140                 i = QtGui.QListWidgetItem(self.listWidget)
00141                 i.setText(o.Name)
00142                 i.setIcon(QtGui.QIcon(":/icons/Arch_Remove.svg"))
00143                 self.listWidget.addItem(i)
00144         if hasattr(self.obj,"Objects"):
00145             for o in self.obj.Objects:
00146                 i = QtGui.QListWidgetItem(self.listWidget)
00147                 i.setText(o.Name)
00148                 i.setIcon(QtGui.QIcon(":/icons/Tree_Part.svg"))
00149 
00150     def retranslateUi(self, TaskPanel):
00151         TaskPanel.setWindowTitle(QtGui.QApplication.translate("Arch", "Components", None, QtGui.QApplication.UnicodeUTF8))
00152         #self.addButton.setText(QtGui.QApplication.translate("Arch", "Append selected objects", None, QtGui.QApplication.UnicodeUTF8))
00153         self.delButton.setText(QtGui.QApplication.translate("Arch", "Remove child", None, QtGui.QApplication.UnicodeUTF8))
00154         self.title.setText(QtGui.QApplication.translate("Arch", "Components of this object", None, QtGui.QApplication.UnicodeUTF8))
00155         
00156 class Component:
00157     "The Component object"
00158     def __init__(self,obj):
00159         obj.addProperty("App::PropertyLink","Base","Base",
00160                         "The base object this component is built upon")
00161         obj.addProperty("App::PropertyLinkList","Additions","Base",
00162                         "Other shapes that are appended to this wall")
00163         obj.addProperty("App::PropertyLinkList","Subtractions","Base",
00164                         "Other shapes that are subtracted from this wall")
00165         obj.addProperty("App::PropertyVector","Normal","Base",
00166                         "The normal extrusion direction of this wall (keep (0,0,0) for automatic normal)")
00167         obj.Proxy = self
00168         self.Type = "Component"
00169         self.Object = obj
00170         self.Subvolume = None
00171         
00172 class ViewProviderComponent:
00173     "A View Provider for Component objects"
00174     def __init__(self,vobj):
00175         vobj.Proxy = self
00176         self.Object = vobj.Object
00177         
00178     def updateData(self,vobj,prop):
00179         return
00180 
00181     def onChanged(self,vobj,prop):
00182         return
00183 
00184     def attach(self,vobj):
00185         self.Object = vobj.Object
00186         return
00187 
00188     def getDisplayModes(self,vobj):
00189         modes=[]
00190         return modes
00191 
00192     def setDisplayMode(self,mode):
00193         return mode
00194 
00195     def __getstate__(self):
00196         return None
00197 
00198     def __setstate__(self,state):
00199         return None
00200 
00201     def claimChildren(self):
00202         return [self.Object.Base]+self.Object.Additions+self.Object.Subtractions
00203 
00204     def setEdit(self,vobj,mode):
00205         taskd = ComponentTaskPanel()
00206         taskd.obj = self.Object
00207         taskd.fillList()
00208         FreeCADGui.Control.showDialog(taskd)
00209         return True
00210     
00211     def unsetEdit(self,vobj,mode):
00212         FreeCADGui.Control.closeDialog()
00213         return True
00214     

Generated on Wed Nov 23 19:00:03 2011 for FreeCAD by  doxygen 1.6.1