示例#1
0
 def AddRTCHandlers(self):
     # make sure we haven't already added them.
     if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML) is not None:
         return
     # This would normally go in your app's OnInit method.  I'm
     # not sure why these file handlers are not loaded by
     # default by the C++ richtext code, I guess it's so you
     # can change the name or extension if you wanted...
     rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler())
     rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())
     # ...like this
     rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Noetext", ext="ntx", type=99))
     # This is needed for the view as HTML option since we tell it
     # to store the images in the memory file system.
     wx.FileSystem.AddHandler(wx.MemoryFSHandler())
示例#2
0
def XMLtoHTML(texteXML):
    """ Converti un texte au format RichText (XML)
            en HTML 
        """
    if texteXML is None:
        return

    out = cStringIO.StringIO()
    handler = rt.RichTextXMLHandler()
    buff = rt.RichTextBuffer()
    out.write(texteXML)
    out.seek(0)
    handler.LoadStream(buff, out)

    # Get an instance of the html file handler, use it to save the
    # document to a StringIO stream
    handler2 = rt.RichTextHTMLHandler()
    handler2.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
    handler2.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100])

    stream = cStringIO.StringIO()
    if not handler2.SaveStream(buff, stream):
        return

    soup = BeautifulSoup(stream.getvalue().decode('utf-8'), "html5lib")

    return soup.html.body.prettify()
示例#3
0
 def set_contents(self, data):
     "load contents into editor"
     shared.log("*** in set_contents: ***")
     shared.log(data)
     self.Clear()
     data = str(data)
     # self.SetValue(data)
     # als ik het onderstaande activeer en de app uitvoer krijg ik (maar niet altijd) een fout
     # gemeld in een popup: "xml parsing error: no element found at line 1" .
     if data.startswith("<?xml"):
         handler = rt.RichTextXMLHandler()
         _buffer = self.GetBuffer()
         _buffer.AddHandler(handler)
         # out = io.StringIO()              -- out moet een OutputStream subclass zijn?
         #                                     bv StringOutputStream maar die zijn er nog niet?
         # out = io.BytesIO()
         # out.write(data)
         # out.seek(0)
         # handler.LoadFile(_buffer, out)
         # handler.ImportXML(_buffer, data)
         with tempfile.NamedTemporaryFile(mode='w+') as out:
             out.write(data)
             out.seek(0)
             handler.LoadFile(_buffer, out.name)
     else:
         self.SetValue(data)  # WriteText(data)
     self.Refresh()
示例#4
0
    def GetValue(self):
        handler = rt.RichTextXMLHandler()
        handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64)

        stream = StringIO()
        handler.SaveStream(self.rtc.GetBuffer(), stream)
        value = stream.getvalue()
        return value
示例#5
0
 def AddRTCHandlers(self):
     if text.RichTextBuffer.FindHandlerByType(
             text.RICHTEXT_TYPE_HTML) is not None:
         return
     text.RichTextBuffer.AddHandler(
         text.RichTextXMLHandler(name="Other XML", ext="ox", type=99))
     text.RichTextBuffer.AddHandler(text.RichTextHTMLHandler())
     wx.FileSystem.AddHandler(wx.MemoryFSHandler())
示例#6
0
    def GetXML(self):
        # Get an instance of the html file handler, use it to save the
        # document to a StringIO stream
        handler = rt.RichTextXMLHandler()
        handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)

        stream = cStringIO.StringIO()
        if not handler.SaveStream(self.rtc.GetBuffer(), stream):
            return

        return stream.getvalue()
示例#7
0
   def GetXMLContent(self):
       handler = rt.RichTextXMLHandler()
       # handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
       # handler.SetFontSizeMapping([7,9,11,12,14,22,100])
 
       import cStringIO
       stream = cStringIO.StringIO()
       if not handler.SaveStream(self.rtc.GetBuffer(), stream):
           return ""
 
       return stream.getvalue()
示例#8
0
    def initUI(self):
        panel = wx.Panel(self)
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        font = wx.Font(22, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)
        labelTitle = wx.StaticText(panel, label="YOUR PAYMENT")
        labelTitle.SetFont(font)
        hbox1.Add(labelTitle, flag=wx.ALIGN_CENTRE, proportion=1)
        vbox.Add(hbox1, flag=wx.ALIGN_CENTRE | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=8)
        
        hbox2 = wx.BoxSizer(wx.VERTICAL)
        fontRichText = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
        rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())  # add suppport to read xml for richtext
        wx.FileSystem.AddHandler(wx.MemoryFSHandler())  # add suppport to read xml for richtext
        richText = rt.RichTextCtrl(panel, style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER);
        richText.SetFont(fontRichText)
        path = os.path.abspath(self.instructionFile)
        richText.LoadFile(path, rt.RICHTEXT_TYPE_XML)
        richText.SetEditable(False)
        # richText.SetBackgroundColour(wx.Colour(240, 240, 240))
        hbox2.Add(richText, flag=wx.ALIGN_CENTER | wx.EXPAND, proportion=1)
        vbox.Add(hbox2, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=10, proportion=1)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        boxNext = wx.BoxSizer(wx.VERTICAL)
        buttonNext = wx.Button(panel, label="GO TO THE EXPERIMENT")
        buttonNext.SetFont(font)
        buttonNext.Bind(wx.EVT_BUTTON, self.OnButtonNextClick)
        boxNext.Add(buttonNext, flag=wx.ALIGN_RIGHT)

        boxPrev = wx.BoxSizer(wx.VERTICAL)
        buttonPrev = wx.Button(panel, label="PREV")
        buttonPrev.SetFont(font)
        buttonPrev.Bind(wx.EVT_BUTTON, self.OnButtonPrevClick)
        boxPrev.Add(buttonPrev, flag=wx.ALIGN_LEFT)

        hbox3.Add(boxPrev, flag=wx.ALIGN_LEFT, proportion=1)
        hbox3.Add(boxNext, flag=wx.ALIGN_RIGHT, proportion=1)
        vbox.Add(hbox3, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=8)

        panel.SetSizer(vbox)
示例#9
0
 def Sauver(self, evt=None):
     if self.rtc.GetValue() == "":
         if hasattr(self.objet, "description"):
             self.objet.SetDescription(None)
         else:
             self.objet[0] = u""
     else:
         handler = rt.RichTextXMLHandler()
         handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY)
         stream = cStringIO.StringIO()
         if not handler.SaveStream(self.rtc.GetBuffer(), stream):
             return
         if hasattr(self.objet, "description"):
             self.objet.SetDescription(stream.getvalue())
         else:
             self.objet[0] = stream.getvalue()
     if evt != None:
         evt.Skip()
示例#10
0
 def Ouvrir(self):
     """ Rempli la zone de texte avec le contenu de objet.description
     """
     out = cStringIO.StringIO()
     handler = rt.RichTextXMLHandler()
     buff = self.rtc.GetBuffer()
     #        buff.AddHandler(handler)
     if hasattr(self.objet, "description"):
         if self.objet.description == None:
             out.write(xmlVide)
         else:
             out.write(self.objet.description)
     else:
         if self.objet[0] == u"":
             out.write(xmlVide)
         else:
             out.write(self.objet[0])
     out.seek(0)
     handler.LoadStream(buff, out)
     self.rtc.Refresh()
示例#11
0
 def get_contents(self):
     "return contents from editor"
     # content = self.GetValue()
     handler = rt.RichTextXMLHandler()
     _buffer = self.GetBuffer()
     # print(type(_buffer), type(out))
     # out = io.StringIO()                  -- out moet een OutputStream subclass zijn?
     #                                         bv StringOutputStream maar die zijn er nog niet?
     # out = io.BytesIO()
     # handler.SaveFile(_buffer, out)
     # out.seek(0)
     # content = out.read()
     with tempfile.NamedTemporaryFile(mode='w+') as out:
         handler.SaveFile(_buffer, out.name)
         # handler.ExportXML(_buffer, content)
         # # of moet dit zijn ok = _buffer.SaveFile(_out) ?
         out.seek(0)
         content = out.read()
     shared.log("*** in get_contents: ***")
     shared.log(content)
     return content
示例#12
0
    def __init__(self, parent, id=-1, size=wx.DefaultSize):  #, bgcolor=None):
        wx.Panel.__init__(self, parent, id, size=size)
        #self.Text = wx.TextCtrl ( self, -1,
        #                         style = wx.TE_MULTILINE | wx.TE_PROCESS_ENTER )

        import wx.richtext as rt

        self.Text = rt.RichTextCtrl ( self,
                     style = wx.VSCROLL|wx.HSCROLL\
                             |wx.TE_MULTILINE|wx.TE_PROCESS_ENTER \
                             |wx.WANTS_CHARS )
        # VERY ESSENTIAL TO CATCH ARROW KEYS
        #|wx.NO_BORDER

        # make sure we haven't already added them.
        if not (rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_HTML)):

            # This would normally go in your app's OnInit method.  I'm
            # not sure why these file handlers are not loaded by
            # default by the C++ richtext code, I guess it's so you
            # can change the name or extension if you wanted...
            rt.RichTextBuffer.AddHandler(rt.RichTextHTMLHandler())
            rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())
            """
      # ...like this
      rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler(name="Other XML",
                                                         ext="ox",
                                                         type=99))
      """
            # This is needed for the view as HTML option since we tell it
            # to store the images in the memory file system.
            wx.FileSystem.AddHandler(wx.MemoryFSHandler())

        self.Text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.Text, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
示例#13
0
    def initUI(self):
        panel = wx.Panel(self)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox2 = wx.BoxSizer(wx.VERTICAL)
        fontRichText = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
        rt.RichTextBuffer.AddHandler(
            rt.RichTextXMLHandler())  #add suppport to read xml for richtext
        wx.FileSystem.AddHandler(
            wx.MemoryFSHandler())  #add suppport to read xml for richtext
        richText = rt.RichTextCtrl(panel,
                                   style=wx.VSCROLL | wx.HSCROLL
                                   | wx.NO_BORDER)
        richText.SetFont(fontRichText)
        path = os.path.abspath(self.instructionFile)
        richText.LoadFile(path, rt.RICHTEXT_TYPE_XML)
        richText.SetEditable(False)
        richText.SetBackgroundColour(wx.Colour(240, 240, 240))
        hbox2.Add(richText, flag=wx.ALIGN_CENTER | wx.EXPAND, proportion=1)
        vbox.Add(hbox2,
                 flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=10,
                 proportion=1)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        buttonNext = wx.Button(panel, label="CLOSE THE SCREEN")
        font = wx.Font(22, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)
        buttonNext.SetFont(font)
        buttonNext.Bind(wx.EVT_BUTTON, self.OnButtonCloseClick)
        buttonNext.SetFocus()
        hbox3.Add(buttonNext, proportion=1)
        vbox.Add(hbox3,
                 flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=8)

        panel.SetSizer(vbox)
示例#14
0
 def __init__(self, model, view, interactor):
     rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())
     self._presenters = {}
     self._models = { 0:model }
     self._scrollPosition = 0
     self._shouldOpenNewWindow = False
     self._canBeClosed = False
     self._refreshHistoryList = True
     self._searchingBuddhawaj = False
     self._paliDictWindow = None
     self._thaiDictWindow = None
     self._englishDictWindow = None
     self._searchAndCompareWindow = None
     self._delegate = None
     self._model = model        
     self._model.Delegate = self
     self._view = view
     self._view.Delegate = self
     self._bookmarkManager = BookmarkManager(self._view, self._model.Code)
     interactor.Install(self, view)
     self.RefreshHistoryList(0)
     self.CheckNewUpdate()        
     self._view.Start()
     utils.UpdateDatabases()
示例#15
0
    def __init__(self, *args, **kw):

        if 'filename' not in kw:
            raise Exception('Error: No filename provided!')

        if 'mainLogic' not in kw:
            raise Exception('Error: No mainLogic provided!')

        if 'resetCallback' not in kw:
            raise Exception('Error: No resetCallback provided!')

        if 'undoResetCallback' not in kw:
            raise Exception('Error: No undoResetCallback provided!')

        self.filename = kw.pop('filename')
        self.mainLogic = kw.pop('mainLogic')
        self.resetCallback = kw.pop('resetCallback')
        self.undoResetCallback = kw.pop('undoResetCallback')
        self.editModel = kw.pop('editModel', False)

        wx.Frame.__init__(self, *args, **kw)

        if rt.RichTextBuffer.FindHandlerByType(rt.RICHTEXT_TYPE_XML) is None:
            rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())

        self.styleChangeMode = 'character'

        self.MakeMenuBar()
        self.MakeToolBar()
        #self.CreateStatusBar()
        #self.SetStatusText("")

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        self.rtc = rt.RichTextCtrl(self,
                                   style=wx.VSCROLL | wx.HSCROLL
                                   | wx.NO_BORDER)
        sizer.Add(self.rtc, 1, wx.EXPAND)

        buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonSizer.AddStretchSpacer()

        from mainlogic import _

        insertButton = wx.Button(self, -1, _("Insert variable"))
        insertButton.Bind(wx.EVT_BUTTON, self.OnInsertVariable)
        buttonSizer.Add(insertButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        removeButton = wx.Button(self, -1, _("Remove variable"))
        removeButton.Bind(wx.EVT_BUTTON, self.OnRemoveVariable)
        buttonSizer.Add(removeButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        addImageButton = wx.Button(self, -1, _("Add image"))
        addImageButton.Bind(wx.EVT_BUTTON, self.OnAddImage)
        buttonSizer.Add(addImageButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        sizer.AddSpacer(10)
        sizer.Add(buttonSizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20)
        sizer.AddSpacer(10)

        buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonSizer.AddStretchSpacer()

        saveButton = wx.Button(self, wx.ID_SAVE)
        saveButton.Bind(wx.EVT_BUTTON, self.OnFileSave)
        buttonSizer.Add(saveButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        resetButton = wx.Button(self, -1, _("Reset"))
        resetButton.Bind(wx.EVT_BUTTON, self.OnReset)
        buttonSizer.Add(resetButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        undoResetButton = wx.Button(self, -1, _("Undo reset"))
        undoResetButton.Bind(wx.EVT_BUTTON, self.OnUndoReset)
        buttonSizer.Add(undoResetButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        printButton = wx.Button(self, wx.ID_PRINT)
        printButton.Bind(wx.EVT_BUTTON, self.OnPrint)
        buttonSizer.Add(printButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        #printPreviewButton = wx.Button(self,wx.ID_PREVIEW)
        #printPreviewButton.Bind(wx.EVT_BUTTON,self.OnPrintPreview)
        #buttonSizer.Add(printPreviewButton,0,wx.ALIGN_RIGHT|wx.RIGHT,5)

        closeButton = wx.Button(self, wx.ID_CLOSE)
        closeButton.Bind(wx.EVT_BUTTON, self.OnFileExit)
        buttonSizer.Add(closeButton, 0, wx.ALIGN_RIGHT | wx.RIGHT, 5)

        sizer.Add(buttonSizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 20)
        sizer.AddSpacer(10)

        self.rtcFeedbackEnabled = True

        self.tagList = []
        self.modified = False

        hframe = wx.Frame(self)
        hframe.Hide()
        self.ortc = rt.RichTextCtrl(hframe,
                                    style=wx.VSCROLL | wx.HSCROLL
                                    | wx.NO_BORDER)
        #self.ortc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER);
        #sizer.Add(self.ortc,1,wx.EXPAND)

        if not self.editModel:
            insertButton.Hide()
            #removeButton.Hide()
        self.LoadFile()

        self.rtc.Bind(rt.EVT_RICHTEXT_CONTENT_INSERTED, self.OnContentInserted)
        self.rtc.Bind(rt.EVT_RICHTEXT_CONTENT_DELETED, self.OnContentDeleted)
        self.rtc.Bind(rt.EVT_RICHTEXT_STYLE_CHANGED, self.OnStyleChanged)
        self.rtc.Bind(wx.EVT_CHAR, self.OnEditEvent)
        self.rtc.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        wx.CallAfter(self.rtc.SetFocus)
示例#16
0
    def initUI(self):
        panel = wx.Panel(self)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        fontRichText = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
        rt.RichTextBuffer.AddHandler(rt.RichTextXMLHandler())  # add suppport to read xml for richtext
        wx.FileSystem.AddHandler(wx.MemoryFSHandler())  # add suppport to read xml for richtext
        richText = rt.RichTextCtrl(panel, style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER, size=(-1, 100));
        richText.SetFont(fontRichText)
        path = os.path.abspath(self.instructionFile)
        richText.LoadFile(path, rt.RICHTEXT_TYPE_XML)
        richText.SetEditable(False)
        richText.SetFocus()
        # richText.SetBackgroundColour(wx.Colour(240,240,240))
        hbox1.Add(richText, flag=wx.EXPAND, proportion=1)
        vbox.Add(hbox1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=8)

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        self.grid = gr.Grid(panel, -1)
        grid = self.grid
        grid.CreateGrid(0, 0)

        csvPath = os.path.abspath(self.sheetFile)
        with open(csvPath, 'rb') as csvfile:
            csvReader = csv.reader(csvfile, delimiter=',')
            nCol = len(next(csvReader))
            grid.AppendCols(numCols=nCol)
            csvfile.seek(0)
            y = 0
            for row in csvReader:
                if y > COL_HEADER:
                    grid.AppendRows(1)
                x = 0
                for cell in row:
                    if y == COL_HEADER:
                        grid.SetColLabelValue(x, cell)
                    if y > COL_HEADER:
                        grid.SetCellValue(y - 1, x, cell)
                        if x == 0:
                            grid.SetCellBackgroundColour(y - 1, 0, wx.Colour(240, 240, 240))
                    x += 1
                y += 1

        grid.EnableEditing(False)
        grid.AutoSizeColumns()
        grid.SetCellHighlightColour(wx.Colour(200, 200, 200))
        grid.SetSelectionBackground(wx.Colour(200, 200, 200))
        grid.SetCellHighlightPenWidth(10)
        grid.Bind(gr.EVT_GRID_SELECT_CELL, self.OnCellSelect)
        grid.Bind(gr.EVT_GRID_RANGE_SELECT, self.OnGridRangeSelect)

        hbox2.Add(grid, flag=wx.GROW | wx.ALL, proportion=1)
        vbox.Add(hbox2, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=10, proportion=1)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        font = wx.Font(22, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)

        boxNext = wx.BoxSizer(wx.VERTICAL)
        buttonNext = wx.Button(panel, label="NEXT")
        buttonNext.SetFont(font)
        buttonNext.Bind(wx.EVT_BUTTON, self.OnButtonNextClick)
        boxNext.Add(buttonNext, flag=wx.ALIGN_RIGHT)

        boxConfirm = wx.BoxSizer(wx.VERTICAL)
        buttonConfirm = wx.Button(panel, label="CONFIRM")
        buttonConfirm.SetFont(font)
        buttonConfirm.Bind(wx.EVT_BUTTON, self.OnButtonConfirmClick)
        boxConfirm.Add(buttonConfirm, flag=wx.ALIGN_CENTER)

        boxPrev = wx.BoxSizer(wx.VERTICAL)
        buttonPrev = wx.Button(panel, label="PREV")
        buttonPrev.SetFont(font)
        buttonPrev.Bind(wx.EVT_BUTTON, self.OnButtonPrevClick)
        boxPrev.Add(buttonPrev, flag=wx.ALIGN_LEFT)

        hbox3.Add(boxPrev, flag=wx.ALIGN_LEFT, proportion=1)
        hbox3.Add(boxConfirm, flag=wx.EXPAND | wx.ALIGN_CENTRE, proportion=1)
        hbox3.Add(boxNext, flag=wx.ALIGN_RIGHT, proportion=1)
        vbox.Add(hbox3, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM, border=8)

        panel.SetSizer(vbox)

        if self.type == self.TYPE_EXAMPLE:
            buttonConfirm.Hide()
        elif self.type == self.TYPE_REAL:
            buttonNext.Hide()
            buttonPrev.Hide()
        elif self.type == self.TYPE_REAL_FINAL:
            buttonNext.Hide()
            buttonPrev.Hide()
            buttonConfirm.SetLabelText("FINISH THE EXPERIMENT")
示例#17
0
    def initUI(self):  # define a panel for the preference page

        decrement = 0.25
        v1, v2, p1, p2, pn, v3, p3 = self.v1, self.v2, self.p1, self.p2, self.pn, self.v3, self.p3
        row, col = 0, 0

        panel = wx.Panel(self)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        fontRichText = wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
        rt.RichTextBuffer.AddHandler(
            rt.RichTextXMLHandler())  # add suppport to read xml for richtext
        wx.FileSystem.AddHandler(
            wx.MemoryFSHandler())  # add suppport to read xml for richtext
        richText = rt.RichTextCtrl(panel,
                                   style=wx.VSCROLL | wx.HSCROLL
                                   | wx.NO_BORDER,
                                   size=(-1, 190))
        richText.SetFont(fontRichText)
        path = os.path.abspath(self.instructionFile)
        richText.LoadFile(path, rt.RICHTEXT_TYPE_XML)

        # reading the xml from the richtext
        handler = wx.richtext.RichTextXMLHandler()
        buffer = richText.GetBuffer()
        inputOutput = StringIO()
        handler.DoSaveFile(buffer, inputOutput)
        inputOutput.seek(0)
        text = inputOutput.read()
        # replace the variable markups with the real values
        text = text.replace("[pn]", ('%.0f' % self.pn))
        if (p1 == -1 or p1 == 0) and (p2 == -1 or p2 == 0) and (p3 != -1
                                                                and p3 != 0):
            text = text.replace("[var1]", ('%.2f' % self.v3))
            text = text.replace("[pro1]", ('%.2f' % self.p3))
        elif (p1 == -1 or p1 == 0) and (p2 != -1 and p2 != 0) and (p3 == -1
                                                                   or p3 == 0):
            text = text.replace("[var1]", ('%.2f' % self.v2))
            text = text.replace("[pro1]", ('%.2f' % self.p2))
        elif (p1 == -1 or p1
              == 0) and (p2 != -1 and p2 != 0) and (p3 != -1 and p3 != 0):
            text = text.replace("[var1]", ('%.2f' % self.v2))
            text = text.replace("[pro1]", ('%.2f' % self.p2))
            text = text.replace("[var2]", ('%.2f' % self.v3))
            text = text.replace("[pro2]", ('%.2f' % self.p3))
        elif (p1 != -1 and p1 != 0) and (p2 == -1 or p2 == 0) and (p3 == -1
                                                                   or p3 == 0):
            text = text.replace("[var1]", ('%.2f' % self.v1))
            text = text.replace("[pro1]", ('%.2f' % self.p1))
        elif (p1 != -1 and p1 != 0) and (p2 == -1 or p2
                                         == 0) and (p3 != -1 and p3 != 0):
            text = text.replace("[var1]", ('%.2f' % self.v1))
            text = text.replace("[pro1]", ('%.2f' % self.p1))
            text = text.replace("[var2]", ('%.2f' % self.v3))
            text = text.replace("[pro2]", ('%.2f' % self.p3))
        elif (p1 != -1 and p1 != 0) and (p2 != -1 and p2 != 0) and (p3 == -1 or
                                                                    p3 == 0):
            text = text.replace("[var1]", ('%.2f' % self.v1))
            text = text.replace("[pro1]", ('%.2f' % self.p1))
            text = text.replace("[var2]", ('%.2f' % self.v2))
            text = text.replace("[pro2]", ('%.2f' % self.p2))
        elif (p1 != -1 and p1 != 0) and (p2 != -1 and
                                         p2 != 0) and (p3 != -1 and p3 != 0):
            text = text.replace("[var1]", ('%.2f' % self.v1))
            text = text.replace("[pro1]", ('%.2f' % self.p1))
            text = text.replace("[var2]", ('%.2f' % self.v2))
            text = text.replace("[pro2]", ('%.2f' % self.p2))
            text = text.replace("[var3]", ('%.2f' % self.v3))
            text = text.replace("[pro3]", ('%.2f' % self.p3))

        # rewrite the xml back to the richtext
        handler2 = wx.richtext.RichTextXMLHandler()
        buffer2 = richText.GetBuffer()
        inputOutput2 = StringIO()
        buffer2.AddHandler(handler2)
        inputOutput2.write(text)
        inputOutput2.seek(0)
        handler.DoLoadFile(buffer2, inputOutput2)
        richText.Refresh()

        richText.SetEditable(False)
        # richText.SetBackgroundColour(wx.Colour(240,240,240))
        hbox1.Add(richText, flag=wx.EXPAND, proportion=1)
        vbox.Add(hbox1,
                 flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=8)

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        self.grid = gr.Grid(panel, -1)
        grid = self.grid
        grid.CreateGrid(0, 0)

        if NUM_OF_COLS == 3:
            grid.AppendCols(4)

            grid.SetColLabelValue(0, "Proposed Certain\n Money")
            grid.SetColLabelValue(1, "I choose Option A")
            grid.SetColLabelValue(2, "I am not sure what to choose")
            grid.SetColLabelValue(3, "I choose Option B")

        elif NUM_OF_COLS == 5:
            grid.AppendCols(6)
            grid.SetColLabelValue(0, "Proposed Certain\n Money")
            grid.SetColLabelValue(1, "I choose Option A")
            grid.SetColLabelValue(
                2, "I think I prefer Option A\nbut I'm not sure")
            grid.SetColLabelValue(3, "I am not sure what to choose")
            grid.SetColLabelValue(
                4, "I think I prefer Option B\nbut I'm not sure")
            grid.SetColLabelValue(5, "I choose Option B")

        topValue = v1
        if v2 > topValue:
            topValue = v2
        if v3 > topValue:
            topValue = v3

        bottomValue = v1
        if v2 < bottomValue and v2 != -1:
            bottomValue = v2
        if v3 < bottomValue and v3 != -1:
            bottomValue = v3

        while topValue >= bottomValue:
            grid.AppendRows(1)
            grid.SetCellValue(row, col,
                              "For " + unichr(163) + ('%.2f' % topValue))
            grid.SetCellFont(
                row, col,
                wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL,
                        wx.FONTWEIGHT_BOLD))
            grid.SetCellBackgroundColour(row, col, wx.Colour(240, 240, 240))
            grid.SetCellAlignment(row, col, wx.ALIGN_CENTRE, wx.ALIGN_TOP)
            topValue -= decrement
            row += 1

        grid.EnableEditing(False)
        grid.Bind(gr.EVT_GRID_SELECT_CELL, self.OnCellSelect)

        hbox2.Add(grid, flag=wx.GROW | wx.ALL, proportion=1)
        vbox.Add(hbox2,
                 flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=10,
                 proportion=1)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        font = wx.Font(22, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD)

        self.buttonConfirm = wx.Button(panel, label="CONFIRM")
        self.buttonConfirm.SetFont(font)
        self.buttonConfirm.Bind(wx.EVT_BUTTON, self.OnButtonConfirmClick)
        self.buttonConfirm.Disable()

        buttonNext = wx.Button(panel, label="NEXT")
        buttonNext.SetFont(font)
        buttonNext.Bind(wx.EVT_BUTTON, self.OnButtonNextClick)

        buttonClear = wx.Button(panel, label="CLEAR")
        buttonClear.SetFont(font)
        buttonClear.Bind(wx.EVT_BUTTON, self.OnButtonClearCLick)

        buttonPrev = wx.Button(panel, label="PREV")
        buttonPrev.SetFont(font)
        buttonPrev.Bind(wx.EVT_BUTTON, self.OnButtonPrevClick)

        self.buttonCon = wx.Button(panel, label="CONFIRM")
        self.buttonCon.SetFont(font)
        self.buttonCon.Bind(wx.EVT_BUTTON, self.OnButtonNextClick)
        self.buttonCon.Disable()

        if self.type == self.TYPE_EXAMPLE:
            leftBox = wx.BoxSizer(wx.VERTICAL)
            leftBox.Add(buttonPrev, flag=wx.ALIGN_LEFT)
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonNext, flag=wx.ALIGN_RIGHT)
            hbox3.Add(leftBox, flag=wx.ALIGN_LEFT, proportion=1)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT, proportion=1)
            self.buttonConfirm.Hide()
            buttonClear.Hide()
            self.buttonCon.Hide()

        elif self.type == self.TYPE_DEMO:
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonNext, flag=wx.ALIGN_RIGHT)
            centerBox = wx.BoxSizer(wx.VERTICAL)
            centerBox.Add(self.buttonConfirm, flag=wx.EXPAND | wx.ALIGN_CENTER)
            leftBox = wx.BoxSizer(wx.VERTICAL)
            leftBox.Add(buttonPrev, flag=wx.ALIGN_LEFT)
            hbox3.Add(leftBox, flag=wx.ALIGN_LEFT)
            hbox3.Add(centerBox, flag=wx.ALIGN_CENTRE, proportion=1)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT)
            buttonClear.Hide()
            self.buttonCon.Hide()

            for row in range(0, grid.GetNumberRows(), 1):
                for col in range(1, grid.GetNumberCols(), 1):
                    grid.SetCellValue(row, col, "")
                    grid.SetCellBackgroundColour(row, col, self.inactiveColor)

            if NUM_OF_COLS == 3:
                for row in range(0, 21, 1):
                    grid.SetCellValue(row, 1, "")
                    grid.SetCellBackgroundColour(row, 1, self.selectedColor)

                for row in range(21, 35, 1):
                    grid.SetCellValue(row, 2, "")
                    grid.SetCellBackgroundColour(row, 2, self.selectedColor)

                for row in range(35, grid.GetNumberRows(), 1):
                    grid.SetCellValue(row, 3, "")
                    grid.SetCellBackgroundColour(row, 3, self.selectedColor)

            elif NUM_OF_COLS == 5:
                for row in range(0, 8, 1):
                    grid.SetCellValue(row, 1, "")
                    grid.SetCellBackgroundColour(row, 1, self.selectedColor)

                for row in range(8, 13, 1):
                    grid.SetCellValue(row, 2, "")
                    grid.SetCellBackgroundColour(row, 2, self.selectedColor)

                for row in range(13, 17, 1):
                    grid.SetCellValue(row, 3, "")
                    grid.SetCellBackgroundColour(row, 3, self.selectedColor)

                for row in range(17, 20, 1):
                    grid.SetCellValue(row, 4, "")
                    grid.SetCellBackgroundColour(row, 4, self.selectedColor)

                for row in range(20, grid.GetNumberRows(), 1):
                    grid.SetCellValue(row, 5, "")
                    grid.SetCellBackgroundColour(row, 5, self.selectedColor)

        elif self.type == self.TYPE_DEMO2:
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonNext, flag=wx.ALIGN_RIGHT)
            centerBox = wx.BoxSizer(wx.VERTICAL)
            centerBox.Add(self.buttonConfirm, flag=wx.EXPAND | wx.ALIGN_CENTER)
            leftBox = wx.BoxSizer(wx.VERTICAL)
            leftBox.Add(buttonPrev, flag=wx.ALIGN_LEFT)
            hbox3.Add(leftBox, flag=wx.ALIGN_LEFT)
            hbox3.Add(centerBox, flag=wx.ALIGN_CENTRE, proportion=1)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT)
            buttonClear.Hide()
            self.buttonCon.Hide()

            for row in range(0, grid.GetNumberRows(), 1):
                for col in range(1, grid.GetNumberCols(), 1):
                    grid.SetCellValue(row, col, "")
                    grid.SetCellBackgroundColour(row, col, self.inactiveColor)

            if NUM_OF_COLS == 3:
                for row in range(0, 15, 1):
                    grid.SetCellValue(row, 1, "")
                    grid.SetCellBackgroundColour(row, 1, self.selectedColor)

                for row in range(15, 31, 1):
                    grid.SetCellValue(row, 2, "")
                    grid.SetCellBackgroundColour(row, 2, self.selectedColor)

                for row in range(31, 39, 1):
                    grid.SetCellValue(row, 1, "")
                    grid.SetCellBackgroundColour(row, 1, self.selectedColor)

                for row in range(39, 49, 1):
                    grid.SetCellValue(row, 2, "")
                    grid.SetCellBackgroundColour(row, 2, self.selectedColor)

                for row in range(49, grid.GetNumberRows(), 1):
                    grid.SetCellValue(row, 3, "")
                    grid.SetCellBackgroundColour(row, 3, self.selectedColor)

            elif NUM_OF_COLS == 5:
                for row in range(0, 8, 1):
                    grid.SetCellValue(row, 1, "")
                    grid.SetCellBackgroundColour(row, 1, self.selectedColor)

                for row in range(8, 13, 1):
                    grid.SetCellValue(row, 2, "")
                    grid.SetCellBackgroundColour(row, 2, self.selectedColor)

                for row in range(13, 17, 1):
                    grid.SetCellValue(row, 3, "")
                    grid.SetCellBackgroundColour(row, 3, self.selectedColor)

                for row in range(17, 20, 1):
                    grid.SetCellValue(row, 4, "")
                    grid.SetCellBackgroundColour(row, 4, self.selectedColor)

                for row in range(20, grid.GetNumberRows(), 1):
                    grid.SetCellValue(row, 5, "")
                    grid.SetCellBackgroundColour(row, 5, self.selectedColor)

        elif self.type == self.TYPE_PRACTICE:
            centerBox = wx.BoxSizer(wx.VERTICAL)
            centerBox.Add(self.buttonCon, flag=wx.EXPAND | wx.ALIGN_CENTER)
            hbox3.Add(centerBox, flag=wx.ALIGN_CENTRE, proportion=1)
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonClear, flag=wx.ALIGN_RIGHT)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT)
            buttonNext.Hide()
            buttonPrev.Hide()
            self.buttonConfirm.Hide()

        elif self.type == self.TYPE_REAL:
            centerBox = wx.BoxSizer(wx.VERTICAL)
            centerBox.Add(self.buttonConfirm, flag=wx.EXPAND | wx.ALIGN_CENTER)
            hbox3.Add(centerBox, flag=wx.ALIGN_CENTRE, proportion=1)
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonClear, flag=wx.ALIGN_RIGHT)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT)
            buttonNext.Hide()
            buttonPrev.Hide()
            self.buttonCon.Hide()

        elif self.type == self.TYPE_REAL_FINAL:
            self.buttonConfirm.SetLabel("FINISH THE EXPERIMENT")
            centerBox = wx.BoxSizer(wx.VERTICAL)
            centerBox.Add(self.buttonConfirm, flag=wx.EXPAND | wx.ALIGN_CENTER)
            hbox3.Add(centerBox, flag=wx.ALIGN_CENTRE, proportion=1)
            rightBox = wx.BoxSizer(wx.VERTICAL)
            rightBox.Add(buttonClear, flag=wx.ALIGN_RIGHT)
            hbox3.Add(rightBox, flag=wx.ALIGN_RIGHT)
            buttonNext.Hide()
            buttonPrev.Hide()
            self.buttonCon.Hide()

        panel.SetSizer(vbox)
        vbox.Add(hbox3,
                 flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP | wx.BOTTOM,
                 border=8)

        ###Resize columns
        grid.SetDefaultCellOverflow(True)
        grid.SetDefaultEditor(wx.grid.GridCellAutoWrapStringEditor())
        grid.AutoSizeColumns()
        for col in range(1, grid.GetNumberCols(), 1):
            grid.SetCellOverflow(0, col, True)

        total = 0
        widestCol = 0
        for col in range(1, grid.GetNumberCols(), 1):
            if widestCol < grid.GetColSize(col):
                widestCol = grid.GetColSize(col)

        for col in range(1, grid.GetNumberCols(), 1):
            grid.SetColSize(col, widestCol)

        grid.DisableDragColSize()
        grid.DisableDragRowSize()
示例#18
0
    def SetValue(self, value):
        handler = rt.RichTextXMLHandler()
        handler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_BASE64)

        stream = StringIO.StringIO(value)
        handler.LoadStream(self.rtc.GetBuffer(), stream)