Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

add label class #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import xmind
from xmind.core import workbook,saver
from xmind.core import workbook, saver
from xmind.core.topic import TopicElement

w = xmind.load("test.xmind") # load an existing file or create a new workbook if nothing is found
w = xmind.load("test.xmind") # load an existing file or create a new workbook if nothing is found

s1=w.getPrimarySheet() # get the first sheet
s1.setTitle("first sheet") # set its title
r1=s1.getRootTopic() # get the root topic of this sheet
r1.setTitle("we don't care of this sheet") # set its title
s1 = w.getPrimarySheet() # get the first sheet
s1.setTitle("first sheet") # set its title
r1 = s1.getRootTopic() # get the root topic of this sheet
r1.setTitle("we don't care of this sheet") # set its title

s2=w.createSheet() # create a new sheet
s2 = w.createSheet() # create a new sheet
s2.setTitle("second sheet")
r2=s2.getRootTopic()
r2 = s2.getRootTopic()
r2.setTitle("root node")

t1 = TopicElement() # create a new element
t1.setTopicHyperlink(s1.getID()) # set a link from this topic to the first sheet given by s1.getID()
t1.setTitle("redirection to the first sheet") # set its title

t1=TopicElement() # create a new element
t1.setTopicHyperlink(s1.getID()) # set a link from this topic to the first sheet given by s1.getID()
t1.setTitle("redirection to the first sheet") # set its title

t2=TopicElement()
t2 = TopicElement()
t2.setTitle("second node")
t2.setURLHyperlink("https://xmind.net") # set an hyperlink
t2.setLabels("first label") # set notes (F3 in XMind)
t2.setURLHyperlink("https://xmind.net") # set an hyperlink

t3=TopicElement()
t3 = TopicElement()
t3.setTitle("third node")
t3.setPlainNotes("notes for this topic") # set notes (F4 in XMind)
t3.setPlainNotes("notes for this topic") # set notes (F4 in XMind)
t3.setLabels("second label") # set notes (F3 in XMind)
t3.setTitle("topic with \n notes")

t4=TopicElement()
t4.setFileHyperlink("logo.jpeg") # set a file hyperlink
t4 = TopicElement()
t4.setFileHyperlink("logo.jpeg") # set a file hyperlink
t4.setTitle("topic with a file")


# then the topics must be added to the root element

r2.addSubTopic(t1)
r2.addSubTopic(t2)
r2.addSubTopic(t3)
r2.addSubTopic(t4)

topics=r2.getSubTopics() # to loop on the subTopics
topics = r2.getSubTopics() # to loop on the subTopics
for topic in topics:
topic.addMarker("yes")

w.addSheet(s2) # the second sheet is now added to the workbook
rel=s2.createRelationship(t1.getID(),t2.getID(),"test") # create a relationship
s2.addRelationship(rel) # and add to the sheet
w.addSheet(s2) # the second sheet is now added to the workbook
rel = s2.createRelationship(t1.getID(), t2.getID(), "test") # create a relationship
s2.addRelationship(rel) # and add to the sheet

xmind.save(w,"test2.xmind") # and we save
xmind.save(w, "test2.xmind") # and we save
Binary file added test.xmind
Binary file not shown.
2 changes: 2 additions & 0 deletions xmind/core/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
TAG_POSITION = "position"
TAG_CHILDREN = "children"
TAG_NOTES = "notes"
TAG_LABELS = "labels"
TAG_RELATIONSHIP = "relationship"
TAG_RELATIONSHIPS = "relationships"
TAG_MARKERREFS = "marker-refs"
Expand Down Expand Up @@ -75,3 +76,4 @@

HTML_FORMAT_NOTE = "html"
PLAIN_FORMAT_NOTE = "plain"
PLAIN_FORMAT_LABEL = "label"
76 changes: 76 additions & 0 deletions xmind/core/label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
xmind.core.labels
~~~~~~~~~~~~~~~~

:copyright:
:license:

"""

__author__ = "evgeny2900@gmail.com <Evgenii T.>"

from . import const

from .mixin import TopicMixinElement


class LabelsElement(TopicMixinElement):
TAG_NAME = const.TAG_LABELS

def __init__(self, node=None, ownerTopic=None):
super(LabelsElement, self).__init__(node, ownerTopic)

def getContent(self, format=const.PLAIN_FORMAT_LABEL):
""" Get labels content

:parma format: specified returned content format, plain text
by default.
"""

content = self.getFirstChildNodeByTagName(format)

if not content:
return

content = Labels(node=content, ownerTopic=self.getOwnerTopic())

return content.getTextContent()


class _LabelContentElement(TopicMixinElement):
def __init__(self, node=None, ownerTopic=None):
super(_LabelContentElement, self).__init__(node, ownerTopic)

def getFormat(self):
return self.getImplementation().tagName


class Labels(_LabelContentElement):
""" Plain text label

:param content: utf8 plain text.
:param node: `xml.dom.Element` object`
:param ownerTopic: `xmind.core.topic.TopicElement` object

"""

TAG_NAME = const.PLAIN_FORMAT_LABEL

def __init__(self, content=None, node=None, ownerTopic=None):
super(Labels, self).__init__(node, ownerTopic)
if content is not None:
self.setTextContent(content)

def setContent(self, content):
self.setTextContent(content)


def main():
pass


if __name__ == '__main__':
main()
35 changes: 35 additions & 0 deletions xmind/core/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .title import TitleElement
from .position import PositionElement
from .notes import NotesElement, PlainNotes
from xmind.core.label import LabelsElement, Labels
from .markerref import MarkerRefElement
from .markerref import MarkerRefsElement
from .markerref import MarkerId
Expand Down Expand Up @@ -331,6 +332,17 @@ def getNotes(self):
if notes is not None:
return NotesElement(notes, self)

def getLabels(self):
"""
Return `NotesElement` object` and invoke
`NotesElement.getContent()` to get labels content.
"""

labels = self.getFirstChildNodeByTagName(const.TAG_LABELS)

if labels is not None:
return NotesElement(labels, self)

def _set_notes(self):
notes = self.getNotes()

Expand All @@ -340,6 +352,15 @@ def _set_notes(self):

return notes

def _set_labels(self):
labels = self.getLabels()

if labels is None:
labels = LabelsElement(ownerTopic=self)
self.appendChild(labels)

return labels

def setPlainNotes(self, content):
""" Set plain text notes to topic

Expand All @@ -355,6 +376,20 @@ def setPlainNotes(self, content):

notes.appendChild(new)

def setLabels(self, content):
"""
Set plain text label to topic
:param content:
"""
labels = self._set_labels()
new = Labels(content, None, self)

old = labels.getFirstChildNodeByTagName(new.getFormat())
if old is not None:
labels.getImplementation().removeChild(old)

labels.appendChild(new)


class ChildrenElement(WorkbookMixinElement):
TAG_NAME = const.TAG_CHILDREN
Expand Down