def test_01(): factory = Factory() boxes = factory.create_boxes(TextModel("123").texel) assert listtools.calc_length(boxes) == 3 boxes = factory.create_boxes(TextModel("123\n567").texel) assert listtools.calc_length(boxes) == 7 paragraphs = create_paragraphs(boxes) assert len(paragraphs) == 2 assert len(paragraphs[0]) == 4 assert len(paragraphs[1]) == 3 assert listtools.calc_length(paragraphs) == 7
def readcomp(filename): """Loads a text model from file and saves it to compare_model.""" global compare_model model_name = filename.split("/")[-1].split("\\")[-1] model = TextModel(model_name) model.readModelFromFiles("models/" + filename + ".model") tkMessageBox.showinfo("Success", "Success! Model " + filename + ".model has been loaded.") print model compare_model = model compare_model_name.set("Comparison model: " + model_name) enable_sample_2_buttons()
def read(filename): """Loads a text model from file and saves it to global_model.""" global global_model model_name = filename.split("/")[-1].split("\\")[-1] model = TextModel(model_name) model.readModelFromFiles("models/" + filename + ".model") print model tkMessageBox.showinfo("Success", "Success! Model " + filename + ".model has been loaded.") current_model_name.set("Current model: " + model_name) if global_model == None: enable_sample_1_buttons() global_model = model
def xxtest_01(): app = wx.App(redirect = False) model = TextModel(testtext) layout = Layout(model) n = len(model) assert len(layout) == n model.insert(10, TextModel('XXX')) assert len(model) == n+3 layout.inserted(10, 3) assert len(layout) == n+3
def generate(filename): """Generates a text model from raw text and saves it to global_model.""" global global_model, root model_name = filename.split("/")[-1].split("\\")[-1] model = TextModel(model_name) model.addTextFromFile("texts/" + filename + ".txt") print model model.saveModelToFiles("models/" + filename + ".model") tkMessageBox.showinfo("Success", "Success! Model saved to " + filename + ".model.") current_model_name.set("Current model: " + model_name) if global_model == None: enable_sample_1_buttons() global_model = model
def test_07(): # Problem: Wenn man recht neben eine Zeile klickt springt der # Cursor nicht an die letzte, sondern an die vorletzte Position. factory = Factory() model = TextModel("123\n567") updater = Updater(model, factory, maxw=0) layout = updater.layout assert layout.get_index(100, 0.5) == 3
def test_06(): factory = Factory() model = TextModel("123\n") updater = Updater(model, factory, maxw=0) layout = updater.layout assert layout.get_info(4, 0, 0) assert str(layout.get_info(3, 0, 0)) == "(NL, 0, 3, 0.0)" assert layout.get_info(3, 0, 0)[-2:] == (3, 0) assert layout.get_info(4, 0, 0)[-2:] == (0, 1)
def test_03(): "Factory" factory = Factory() texel = TextModel("123\n\n567890 2 4 6 8 0").texel boxes = factory.create_boxes(texel) assert str(boxes) == "(TB('123'), NL, NL, TB('567890 2 4 6 8 0'))" paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert str(stack.childs) == "(Paragraph[Row[TB('123'), NL]], " \ "Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0')]])" paragraphs = create_paragraphs(boxes, 5) assert repr(paragraphs) == "[Paragraph[Row[TB('123'), NL]], " \ "Paragraph[Row[NL]], Paragraph[Row[TB('56789')], " \ "Row[TB('0 2 4')], Row[TB(' 6 8 ')], Row[TB('0')]]]" texel = TextModel("123\t\t567890 2 4 6 8 0").texel boxes = factory.create_boxes(texel)
def test_11(): # Problem: if the click position is right of the line, the cursor # should jump to the last index position in that line. ns = init_testing(redirect=False) model = ns['model'] view = ns['view'] #view.layout.dump_boxes(0, 0, 0) model.remove(0, len(model)) model.insert(0, TextModel("123\n")) assert view.layout.get_index(100, 0) == 3
def test_11(): # 1. Problem: Wenn man recht neben eine Zeile klickt springt der # Cursor nicht an die letzte, sondern an die vorletzte Position. ns = init_testing(redirect=False) model = ns['model'] layout = ns['view'].updater.layout model.remove(0, len(model)) model.insert(0, TextModel("123\n")) assert layout.get_index(100, 0) == 3 # 2. Problem: wenn der Text mit einem NL endet, dann funktioniert # get_index für diese letzte Zeile nicht. assert layout.get_index(0, 1000) == len(model)
def test_14(): "join_undo" ns = init_testing(redirect=False) view = ns['view'] for i, text in enumerate('abcd'): view.add_undo(view.insert(i, TextModel(text))) assert len(view._undoinfo) == 1 view._undoinfo = [] # reset undo # emulate backspace view.add_undo(view.remove(10, 11)) view.add_undo(view.remove(9, 10)) assert len(view._undoinfo) == 1
def demo_02(): "empty text" app = wx.App(redirect = True) frame = wx.Frame(None) win = wx.Panel(frame, -1) view = WXTextView(win, -1, style=wx.SUNKEN_BORDER) box = wx.BoxSizer(wx.VERTICAL) box.Add(view, 1, wx.ALL|wx.GROW, 1) win.SetSizer(box) win.SetAutoLayout(True) model = TextModel(u'') view.set_model(model) frame.Show() app.MainLoop()
def test_02(): "ParagraphStack" factory = Factory() texel = TextModel("123\n567").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 7 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) texel = TextModel("123\n\n5\n67").texel boxes = factory.create_boxes(texel) assert listtools.calc_length(boxes) == 9 paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert check_box(stack, texel) assert stack.extra_height == 0 texel = TextModel("123\n").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert stack.childs[-1].has_newline() assert stack.extra_height == 1.0 par = stack.childs[-1] assert len(par) == 4 assert len(stack) == 4 assert stack.get_info(3, 0, 0)[-2:] == (3, 0) assert stack.get_info(4, 0, 0)[-2:] == (0, 1) texel = TextModel("").texel boxes = factory.create_boxes(texel) paragraphs = create_paragraphs(boxes) stack = ParagraphStack(paragraphs) assert stack.extra_height > 0
def test_10(): ns = init_testing(redirect=False) model = ns['model'] view = ns['view'] model.remove(0, len(model)) model.insert(0, TextModel("123\n")) updater = view.updater updater.set_maxw(100) layout = view.layout assert layout.get_info(4, 0, 0) x, y = layout.get_info(3, 0, 0)[-2:] u, v = layout.get_info(4, 0, 0)[-2:] # Cursorposition 4 must be at the begining of next line assert u < x # 3 must be left of 4 assert v > y # and 4 must be below 3
def xxtest_00(): from layout import Paragraph app = wx.App(redirect = False) model = TextModel(testtext) layout = Layout(model) i1 = 0 for paragraph in layout.childs: i2 = i1+paragraph.length assert isinstance(paragraph, Paragraph) #print i1, i2, repr(model.get_text(i1, i2)) j1 = i1 for row in paragraph.childs: j2 = j1+row.length j1 = j2 i1 = i2 assert i2 == len(model)
def test_05(): device = TESTDEVICE factory = Factory(device) model = TextModel("123\n\n567890 2 4 6 8 0") updater = Updater(model, factory, maxw=0) layout = updater.layout def check(box): if not box.device is device: print box assert box.device is device if hasattr(box, 'iter'): for j1, j2, x1, y1, child in box.riter(0, 0, 0): check(child) check(layout)
def test_10(): ns = init_testing(redirect=False) model = ns['model'] view = ns['view'] model.remove(0, len(model)) model.insert(0, TextModel("123\n")) updater = view.updater updater.set_maxw(100) factory = Factory() layout = updater.layout assert layout.get_info(4, 0, 0) x, y = layout.get_info(3, 0, 0)[-2:] u, v = layout.get_info(4, 0, 0)[-2:] # Cursorposition 4 muss sich am Anfang der nächstne Zeile befinden assert u<x # weiter Links als Position 3 assert v>y # weiter gegen Ende
def paste(self): if self.has_selection(): s1, s2 = self.get_selected()[0] # XXXX self.model.remove(s1, s2) self.index = s1 if wx.TheClipboard.IsOpened(): # may crash, otherwise return pickled = wx.CustomDataObject("pytextmodel") plain = wx.TextDataObject() textmodel = None wx.TheClipboard.Open() if wx.TheClipboard.GetData(pickled): textmodel = cPickle.loads(pickled.GetData()) elif wx.TheClipboard.GetData(plain): textmodel = TextModel(plain.GetText()) wx.TheClipboard.Close() if textmodel is not None: self.insert(self.index, textmodel)
def init_testing(redirect=True): app = wx.App(redirect=redirect) model = TextModel(testtext) model.set_properties(15, 24, fontsize=14) model.set_properties(249, 269, fontsize=14) frame = wx.Frame(None) win = wx.Panel(frame, -1) view = WXTextView(win, -1, style=wx.SUNKEN_BORDER) view.model = model box = wx.BoxSizer(wx.VERTICAL) box.Add(view, 1, wx.ALL | wx.GROW, 1) win.SetSizer(box) win.SetAutoLayout(True) frame.Show() return locals()
def benchmark_00(): text = "" for i in range(1000): text += "Copy #%d \n" % i model = TextModel(u'Hello World!') model.set_properties(6, 11, fontsize=14) model.set_properties(0, 11, bgcolor='yellow') model.insert(len(model), TextModel(text)) app = wx.App(False) frame = wx.Frame(None) view = WXTextView(frame, -1) view.model = model frame.Show() for i in range(100): model.insert_text(1000, "TEXT")
def test_01(): # 0.3s !!! # alternative way to create text t = '' for i in range(10000): t += "Line %i: test test test test\n" % i TextModel(t)
def test_04(): "insert/remove" factory = Factory() model = TextModel("123\n\n567890 2 4 6 8 0") boxes = factory.create_boxes(model.texel) paragraphs = create_paragraphs(boxes) updater = Updater(model, factory, maxw=0) layout = updater.layout assert repr(layout) == "ParagraphStack[Paragraph[Row[TB('123'), " \ "NL]], Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0')]]]" assert len(layout) == len(model) assert layout.height == 3 ins = TextModel("xyz\n") model.insert(2, ins) updater.inserted(2, len(ins)) assert len(layout) == len(model) assert repr(layout) == "ParagraphStack[Paragraph[Row[TB('12xyz'),"\ " NL]], Paragraph[Row[TB('3'), NL]], Paragraph[Row[NL]], "\ "Paragraph[Row[TB('567890 2 4 6 8 0')]]]" assert layout.height == 4 model.remove(2, 2 + len(ins)) updater.removed(2, len(ins)) assert len(layout) == len(model) assert repr(layout) == "ParagraphStack[Paragraph[Row[TB('123'), " \ "NL]], Paragraph[Row[NL]], Paragraph[Row[TB('567890 2 4 6 8 0')]]]" assert layout.height == 3 factory = Factory() model = TextModel("123") updater = Updater(model, factory, maxw=0) layout = updater.layout ins = TextModel("xyz\n") i = len(model) model.insert(i, ins) updater.inserted(i, len(ins)) for c in "abc": ins = TextModel(c) i = len(model) model.insert(i, ins) updater.inserted(i, len(ins)) assert str(layout) == "ParagraphStack[Paragraph[Row[TB('123xyz'), NL]], " \ "Paragraph[Row[TB('abc')]]]"
def handle_action(self, action, shift=False): model = self.model index = self.index layout = self.updater.layout if index == len(model): if index == 0: style = {} # wird zum defaultstyle ergänzt else: style = model.get_style(index-1) else: style = model.get_style(index) row, col = self.current_position() rect = layout.get_rect(index, 0, 0) x = rect.x1 y = rect.y1 def del_selection(): if self.has_selection(): s1, s2 = sorted(self.selection) s1, s2 = layout.extend_range(s1, s2) self.remove(s1, s2) if action == 'dump_info': n = min(100, len(model.texel)) model.texel[0:n].dump() row, col = model.index2position(index) print "row=", row print "col=", col elif action == 'move_word_end': i = index n = len(model) try: while not model.get_text(i, i+1).isalnum(): i = i+1 while model.get_text(i, i+1).isalnum(): i = i+1 except IndexError: i = n self.set_index(i, shift) elif action == 'move_right': self.set_index(index+1, shift) elif action == 'move_word_begin': i = index try: while not model.get_text(i-1, i).isalnum(): i = i-1 while model.get_text(i-1, i).isalnum(): i = i-1 except IndexError: i = 0 self.set_index(i, shift) elif action == 'move_left': self.set_index(index-1, shift) elif action == 'move_paragraph_end': i = row linelengths = model.texel.get_linelengths() try: while linelengths[i] == 1: i += 1 while linelengths[i] > 1: i += 1 self.move_cursor_to(i, 0, shift) except IndexError: self.set_index(len(model), shift) elif action == 'move_down': self.move_cursor_to(row+1, col, shift) elif action == 'move_paragraph_begin': i = row-1 linelengths = model.texel.get_linelengths() while linelengths[i] == 1 and i>=0: i -= 1 while linelengths[i] > 1 and i>=0: i -= 1 self.move_cursor_to(i+1, 0, shift) elif action == 'move_up': self.move_cursor_to(row-1, col, shift) elif action == 'move_line_start': self.set_index(model.linestart(row), shift) elif action == 'move_line_end': self.set_index(model.linestart(row)+model.linelength(row)-1, shift) elif action == 'move_page_down': width, height = self.GetClientSize() i = self.compute_index(x, y+height) self.set_index(i, shift) elif action == 'move_page_up': width, height = self.GetClientSize() i = self.compute_index(x, y-height) self.set_index(i, shift) elif action == 'select_all': self.selection = (0, len(model)) elif action == 'insert_newline': self.insert(index, TextModel('\n', **style)) elif action == 'backspace': del_selection() i = self.index if i>0: j1, j2 = layout.extend_range(i-1, i) self.remove(j1, j2) elif action == 'copy': self.copy() elif action == 'paste': self.paste() elif action == 'cut': self.cut() elif action == 'delete': if self.has_selection(): del_selection() else: i = self.index if i < len(self.model): j1, j2 = layout.extend_range(i, i+1) self.remove(j1, j2) elif action == 'undo': self.undo() elif action == 'redo': self.redo() elif action == 'del_word_left': # find the beginning of the word i = index try: while not model.get_text(i-1, i).isalnum(): i = i-1 while model.get_text(i-1, i).isalnum(): i = i-1 except IndexError: i = 0 self.remove(i, index) else: #print keycode assert len(action) == 1 # single character del_selection() index = self.index s = TextModel(action, **style) n = len(model) assert len(s) == 1 self.insert(index, s) self.Refresh()
def __init__(self): ViewBase.__init__(self) self._undoinfo = [] self._redoinfo = [] self.create_factory() self.set_model(TextModel(''))
def create_text(): # Create a long text global text text = TextModel() for i in range(10000): text.append(TextModel("Line %i: test test test test\n" % i))
import sys sys.path.insert(0, '..') sys.path.insert(0, '../../textmodel') from textmodel import TextModel from wxtextview import WXTextView import wx app = wx.App() model = TextModel(u'Hello World!') model.set_properties(6, 11, fontsize=14) model.set_properties(0, 11, bgcolor='yellow') instructions = """ You can edit this text as you like. Undo is ctrl-z and redo ctrl-r. The second window displays exactly the same text and follows the changes. """ model.insert(len(model), TextModel(instructions)) # display the texmodel in a view frame = wx.Frame(None) view = WXTextView(frame) view.model = model
def _mk_textmodel(texel): model = TextModel() model.texel = texel return model
import gc import sys sys.path.insert(0, '..') from textmodel import TextModel from textmodel.treebase import depth, count # Create a long text text = TextModel() for i in range(10000): text.append(TextModel("Line %i\n" % i)) # Colorize it! colors = 'white', 'red' i1 = 0 i2 = 10 while i2<len(text): text.set_properties(i1, i2, bgcolor=colors[i1%2]) i1 = i2 i2 += 10 # Remove garbage so that the statistics are correct gc.collect() # Print some statistics print "Length of text:", len(text) print "Number of lines:", text.nlines() print "Depth of texel tree:", depth(text.texel) print "Number of texels in tree:", count(text.texel)
import sys sys.path.insert(0, '..') from textmodel import TextModel from wxtextview import WXTextView import wx model = TextModel(u'Hello World!') model.set_properties(6, 11, fontsize=14) model.set_properties(6, 11, bgcolor='yellow') app = wx.App() frame = wx.Frame(None) view = WXTextView(frame, -1) view.model = model view.cursor = 5 view.selection = 0, 5 frame.Show() app.MainLoop()
def get_emptychars(self): return '(;)' class Root(Container): def __init__(self, content, **kwds): self.content = content Container.__init__(self, **kwds) def get_content(self): return [self.content] def mk_textmodel(texel): model = TextModel() model.texel = texel return model frac = Fraction(Characters(u'3'), Characters(u'4')) root = Root(Characters(u'2')) text = TextModel(u"A text which contains some math\n\n") text.append(u'f = ') text.append(mk_textmodel(frac)) text.append(u'\n\n') text.append(mk_textmodel(root)) text.append(u' = 1.414214 ...') text.texel.dump()