PLGTextNode.py :  » Business-Application » Gimini » gimini » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » Gimini 
Gimini » gimini » PLGTextNode.py
################################################################################
# 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)


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.