################################################################################
# Name : TextNode.py - gimini plugin
# Author : C.Dutoit - dutoitc@hotmail.com - http://www.dutoitc.fr.st
# Goal : Gimini "Text Node" plugin
# Date : Jan 2002
################################################################################
from wxPython.wx import *
from gPluginsDialogs import *
from basePlugin import *
from PluginsAPI import *
from utils import assignID
pluginID="TextNode"
[ID_MENU_ABOUT, ID_ICON_INS_NODE, ID_ICON_MULTIPLE_INS_NODE]=assignID(3)
#>--------------------------------------------------------------------------
class GiminiPlugin:
"A gimini plugin"
def __init__(self, wxParent, pluginsAPI):
"Plugin initialisation"
self._pluginsAPI=pluginsAPI
self._wxParent=wxParent
#create menu
root=wxMenu("", wxMENU_TEAROFF)
root.Append(ID_MENU_ABOUT, "About...")
self._pluginsAPI.addToPluginMenu(pluginID, root)
EVT_MENU(wxParent, ID_MENU_ABOUT, self.CBMnuAbout)
#create toolbar entry
control=wxBitmap('img'+os.sep+'ins_node.bmp', wxBITMAP_TYPE_BMP)
self._pluginsAPI.addToolbarItem( ID_ICON_INS_NODE, control,
"Insert a text node", "Insert a text node")
control=wxBitmap('img'+os.sep+'ins_node_multiple.bmp',wxBITMAP_TYPE_BMP)
self._pluginsAPI.addToolbarItem( ID_ICON_MULTIPLE_INS_NODE, control,
"Insert multiple text node", "Insert multiple text node")
EVT_TOOL(wxParent, ID_ICON_INS_NODE, self.CBInsNodeClick)
EVT_TOOL(wxParent, ID_ICON_MULTIPLE_INS_NODE,
self.CBInsMultipleNodeClick)
self._pluginsAPI.requestKeyboardShortcut(pluginID, WXK_INSERT , \
self.CBInsertNode)
#>--------------------------------------------------------------------------
def CBMnuAbout(self, event):
"Show about box"
dlg=wxMessageDialog(self._wxParent,
"Text node plugin for Gimini", "",wxOK)
dlg.ShowModal()
#>--------------------------------------------------------------------------
def CBInsNodeClick(self, event):
"Handle click on 'insert node' icon"
nodeData=NodeData()
self._pluginsAPI.addChildFromNodeData(nodeData)
self._pluginsAPI.setDefaultPlugin(pluginID)
#>--------------------------------------------------------------------------
def CBInsMultipleNodeClick(self, event):
"Handle click on 'insert node' icon"
dlg=dlgMultiNodes(self._wxParent, "Multinode editing")
if (dlg.ShowModal()<>wxID_OK or len(dlg.getText())==0):
return wxID_CANCEL
#Add all nodes
nodesData=[]
for el in dlg.getText():
oneNodeData=NodeData()
nodesData.append(oneNodeData)
oneNodeData.loadFromString(el)
self._pluginsAPI.addChildrensNodesFromNodeData(nodesData)
self._pluginsAPI.setDefaultPlugin(pluginID)
#>--------------------------------------------------------------------------
def getNodeData(self):
"Get a node data for this plugin"
return NodeData()
#>--------------------------------------------------------------------------
def CBInsertNode(self, event):
"Insert node keyboard callback"
nodeData=NodeData()
self._pluginsAPI.addChildFromNodeData(nodeData)
self._pluginsAPI.setDefaultPlugin(pluginID)
#>--------------------------------------------------------------------------
class NodeData(Mindmap_NodeDatas):
"A node with text data"
#>----------------------------------------------------------------------
def __init__(self):
Mindmap_NodeDatas.__init__(self)
self.__text=[]
#>----------------------------------------------------------------------
def setText(self, text=[]):
"set the node text"
self.__text=text
#>----------------------------------------------------------------------
def saveToString(self):
"Save the node data to a string"
txt=""
for indice in range(len(self.__text)):
txt=txt+self.__text[indice]
if indice<len(self.__text)-1:
txt=txt+"\\\\"
return txt
#>----------------------------------------------------------------------
def loadFromString(self, txt):
"Load the node data from a string"
self.__text=[]
buffer=""
buffer2=""
for el in txt:
if el=='\\':
if buffer2=="":
buffer2=el
else:
buffer2=""
self.__text.append(buffer)
buffer=""
else:
if buffer2<>"":
buffer=buffer+buffer2
buffer2=""
buffer+=el
#if buffer<>"":
#buffer1=buffer2
if buffer<>"":
self.__text.append(buffer)
#>----------------------------------------------------------------------
def saveToList(self):
"Save the datas to a list"
return self.__text
#>----------------------------------------------------------------------
def loadFromList(self, lst):
"Load the node data from a list"
self.__text=lst
#>----------------------------------------------------------------------
def getSmallName(self):
"Get a small.__text wich will represent the node"
if len(self.__text)>1:
return self.__text[0]+"..."
else:
if len(self.__text)==1:
return self.__text[0]
else:
return ""
#>----------------------------------------------------------------------
def getNodeSize(self, dc):
"Get the node size"
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
w=0
for el in self.__text:
(wt, dummy)=dc.GetTextExtent(el)
if wt>w:
w=wt
return (w+2*dc.GetCharWidth(),
(dc.GetCharHeight())*len(self.__text))
#>----------------------------------------------------------------------
def drawRootNode(self, dc, x, y):
"draw a node on a dc at a specified position"
dc.BeginDrawing()
myPen=wxPen(wxBLACK, 1, wxSOLID)
dc.SetPen(myPen)
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
if len(self.__text)>1:
name=self.__text[0]+'...'
else:
if len(self.__text)==1:
name=self.__text[0]
else:
name=''
(w,h)=dc.GetTextExtent(name)
w+=dc.GetCharWidth()*2
dc.DrawRoundedRectangle(x-w/2-3-5, y-h/2-6, w+10+6, h+12, 20)
dc.DrawRoundedRectangle(x-w/2 -5, y-h/2-3, w+10, h+6, 20)
dc.DrawText(name, x-w/2+dc.GetCharWidth(), y-h/2)
dc.EndDrawing()
#>----------------------------------------------------------------------
def getNodeRootSize(self, dc):
"Get the node size when drawed as root"
myPen=wxPen(wxBLACK, 1, wxSOLID)
dc.SetPen(myPen)
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
if len(self.__text)>1:
name=self.__text[0]+'...'
else:
if len(self.__text)==1:
name=self.__text[0]
else:
name=''
(w,h)=dc.GetTextExtent(name)
w+=dc.GetCharWidth()*2
w+=16
h+=12
return (w, h)
#>----------------------------------------------------------------------
def drawNodeDatas(self, dc, x, y):
"Draw the node data; org is bottom-left"
(w, h)=self.getNodeSize(dc)
nbLines=len(self.__text)
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
textHeight=dc.GetCharHeight()
blankWidth=dc.GetCharWidth()
for indice in range(nbLines):
dc.DrawText(self.__text[indice],
x + blankWidth,
y - h + indice*(textHeight))
#>----------------------------------------------------------------------
def getRootLatexCode(self, dc, x, y):
"Return the LaTeX code of the root, drawed as root"
(w, h)=self.getNodeSize(dc)
nbLines=len(self.__text)
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
textHeight=dc.GetCharHeight()
blankWidth=dc.GetCharWidth()
latexCode=""
if len(self.__text)>1:
name=self.__text[0]+'...'
else:
if len(self.__text)==1:
name=self.__text[0]
else:
name=''
(w,h)=dc.GetTextExtent(name)
w+=dc.GetCharWidth()*2
latexCode+="\put("+str(x)+","+str(y)+"){\oval(" + \
str(w+10+6) + "," + str(h+12) + ")}\n"
latexCode+="\put("+str(x)+","+str(y)+"){\oval(" + \
str(w+10) + "," + str(h+6) + ")}\n"
latexCode+="\put("+str(x)+","+str(y)+"){"+ \
"\makebox(0, 0){" + name +"}}\n"
return latexCode
#>----------------------------------------------------------------------
def getLatexCode(self, dc, x, y):
"Return the node LaTeX code"
(w, h)=self.getNodeSize(dc)
nbLines=len(self.__text)
myFont=wxFont(self._fontSize, wxDEFAULT, wxNORMAL, wxNORMAL)
dc.SetFont(myFont)
textHeight=dc.GetCharHeight()
blankWidth=dc.GetCharWidth()
latexCode=""
for indice in range(nbLines):
latexCode+="\put("+str(x+blankWidth)+","+\
str(y+h-(indice+1)*textHeight)+"){"+ \
"\makebox(0, 0)[bl]{" + self.__text[indice]+"}}\n"
return latexCode
#>----------------------------------------------------------------------
def getNodeEditionPanel(self, parent):
"""Return a canvas to integrate to the node edition dialog box wich
will permit to edit the node datas"""
return nodeEditionPanel(parent, self)
#>------------------------------------------------------------------
def getPluginID(self):
"Return the plugin ID"
return pluginID
#>------------------------------------------------------------------
def saveToXML(self):
"Save the datas to xml"
from xml.dom.minidom import Element
data=Element("Data")
data.setAttribute('NodeText', self.saveToString())
return data
#>------------------------------------------------------------------
def loadFromXML(self, XMLdata):
"Load the datas from xml"
self.loadFromString(XMLdata.getAttribute("NodeText"))
#>----------------------------------------------------------------------
class nodeEditionPanel(wxPanel):
"Panel to edit node datas"
#>------------------------------------------------------------------
def __init__(self, parent, nodeDatas):
"Init controls"
self._myNodeDatas=nodeDatas
wxPanel.__init__(self, parent, -1)
mySizer = wxBoxSizer(wxHORIZONTAL)
self._msg=wxStaticText(self, -1, "Node text : ")
self._nodeText=wxTextCtrl(self, 100,
lst2str(self._myNodeDatas.saveToList()),
wxDefaultPosition, wxSize(360, 100),
wxTE_MULTILINE)
mySizer.Add(self._msg, 0, wxEXPAND)
mySizer.Add(self._nodeText, 1, wxEXPAND)
self.SetAutoLayout(true)
self.SetSizer(mySizer)
mySizer.Fit(self)
self._nodeText.SetFocus()
#>------------------------------------------------------------------
def saveDatas(self):
"Save the panel datas"
lst=[]
for i in range(self._nodeText.GetNumberOfLines()):
lst.append(self._nodeText.GetLineText(i))
self._myNodeDatas.loadFromList(lst)
|