Beispiel #1
0
class FrameExtended(wx.Frame):
    def __init__(self, parent, id, title, appUrl):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
                          wx.Size(800, 600))
        #wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(800, 600), wx.DEFAULT_FRAME_STYLE^(wx.RESIZE_BORDER | wx.MINIMIZE_BOX |wx.MAXIMIZE_BOX))
        #self.SetIcon(wx.Icon('ico/sharedpaint.ico', wx.BITMAP_TYPE_ICO))

        self.appUrl = appUrl

        # <AGtk code>
        reactor.interleave(wx.CallAfter)

        # Create shared application client
        self.sharedAppClient = SharedAppClient("SharedPaint")
        self.log = self.sharedAppClient.InitLogging()
        self.log.debug("GroupPaint.__init__: Started Group Paint")

        # Get client profile
        try:
            self.clientProfileFile = os.path.join(
                UserConfig.instance().GetConfigDir(), "profile")
            self.clientProfile = ClientProfile(self.clientProfileFile)

        except:
            self.log.info(
                "SharedQuestionTool.__init__: Could not load client profile, set clientProfile = None"
            )
            self.clientProfile = None

        # Join the application session
        self.sharedAppClient.Join(self.appUrl, self.clientProfile)
        self.publicId = self.sharedAppClient.GetPublicId()
        self.user = self.clientProfile.GetName()
        self.id = self.sharedAppClient.GetPublicId()

        # Register event callback
        self.sharedAppClient.RegisterEventCallback(events.CONSOLE_EVENT,
                                                   self.GetConsoleEventData)
        self.sharedAppClient.RegisterEventCallback(events.CLEAR_EVENT,
                                                   self.GetClearEvent)
        #self.sharedAppClient.RegisterEventCallback(events.SET_BG_EVENT, self.GetSetBGEvent)
        # </AGtk code>
        """
        Setting status bar
        """
        self.CreateStatusBar()

        workspace = wx.BoxSizer(wx.VERTICAL)
        imagePanel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
        """
        Painter
        """
        self.painter = DoodleWindow(imagePanel, -1, self.clientProfile,
                                    self.sharedAppClient)

        imageBox = wx.BoxSizer(wx.VERTICAL)
        imageBox.Add(self.painter, 1, wx.EXPAND | wx.ALL, 1)
        imagePanel.SetSizer(imageBox)
        """
        Console settings
        """
        consolePanel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)

        self.console = wx.TextCtrl(consolePanel, -1, style=wx.TE_MULTILINE)
        self.console.SetEditable(0)

        consoleBox = wx.BoxSizer(wx.VERTICAL)
        consoleBox.Add(self.console, 1, wx.EXPAND | wx.ALL, 1)
        consolePanel.SetSizer(consoleBox)

        workspace.Add(imagePanel, 4, wx.EXPAND | wx.ALL, 1)
        workspace.Add(consolePanel, 1, wx.EXPAND | wx.ALL, 1)

        self.SetSizer(workspace)

    ## Makes the menu bar
    # @param self The object pointer
    def MakeMenu(self):

        menu = wx.MenuBar()
        options = wx.Menu()
        """
        Adding menu item
        """
        options.Append(101, "&Open", "Open background image")
        options.AppendSeparator()
        options.Append(102, "Clear background", "Clear background image")
        options.Append(103, "Clear drawing", "Clear drawing area")
        options.AppendSeparator()
        options.Append(104, "Help/About", "Further information")
        options.AppendSeparator()
        close = wx.MenuItem(options, 105, "&Close\tCtrl+C",
                            "Close application")
        """
        Adding an icon to the close menu option
        """
        #close.SetBitmap(wx.Image('./pixmaps/frame.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap())
        options.AppendItem(close)
        """
        Adding menu items
        """
        menu.Append(options, "Options")
        """
        Setting  menu bar
        """
        self.SetMenuBar(menu)

        self.Bind(wx.EVT_MENU, self.OnOpenFile, id=101)
        self.Bind(wx.EVT_MENU, self.OnClearBackground, id=102)
        self.Bind(wx.EVT_MENU, self.OnClearDrawing, id=103)
        self.Bind(wx.EVT_MENU, self.OnHelp, id=104)
        self.Bind(wx.EVT_MENU, self.OnClose, id=105)

    ## Initialize the console object
    # @param self The object pointer
    def LoadConsole(self):
        self.PrintStartMessage()

    ## Open an image file
    # @param self The object pointer
    # @event event Menu event
    def OnOpenFile(self, event):
        dlg = wx.FileDialog(self, "Choose a image file", os.getcwd(), "",
                            "*.jpg", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            mypath = os.path.basename(path)
            self.SetStatusText("Image file: %s" % mypath)
            self.painter.SetBackground(path)
            self.ConsoleMessage("File " + mypath + " loaded on background")

        dlg.Destroy()

    ## Close the application
    # @param self The object pointer
    # @event event Menu event
    def OnClose(self, event):
        dlg = wx.MessageDialog(self,
                               "Are you sure you want to close SharedPaint?",
                               "Confirm close", wx.YES_NO | wx.ICON_QUESTION)
        if dlg.ShowModal() == wx.ID_YES:
            dlg.Destroy()
            self.Close()
        else:
            dlg.Destroy()

    ## Clear the background image
    # @param self The object pointer
    # @event event Menu event
    def OnClearBackground(self, event):
        self.painter.SetBackground(None)
        # <AGtk event>
        self.ConsoleMessage("Background image unloaded")

    # </AGtk event>

    ## Clear the user drawing
    # @param self The object pointer
    # @event event Menu event
    def OnClearDrawing(self, event):
        self.sharedAppClient.SendEvent(events.CLEAR_EVENT, "")

    ## Display help frame
    # @param self The object pointer
    # @event event Menu event
    def OnHelp(self, event):
        helpFrame = HTMLFrame(self, -1, 'SharedPaint HELP')
        helpFrame.Centre()
        helpFrame.Show()

    ## Send a console message
    # @param self The object pointer
    # @param message The message
    def ConsoleMessage(self, _message):
        message = " " + self.user + " >> " + _message
        self.sharedAppClient.SendEvent(events.CONSOLE_EVENT, message)

    ## Print welcome message
    # @param self The object pointer
    def PrintStartMessage(self):
        startMessage = "*** AGTk - SharedPaint 1.2, Department of Systems and Computing Engineering, Universidad de los Andes ***" + "\n" + "See Help/About for further information" + "\n"
        self.console.WriteText(startMessage)

    ## Capture the console event
    # @param self The object pointer
    # @param event Event data
    def GetConsoleEventData(self, event):
        message = event.data
        dt = datetime.datetime.now()
        timestamp = "[" + dt.isoformat() + "]: "
        line = timestamp + message
        self.console.WriteText("\n" + line)

    ## Capture the set background event
    # @param self The object pointer
    # @param event Event data
    def GetSetBGEvent(self, event):
        pass

    ## Capture the clear drawing event
    # @param self The object pointer
    # @param event Event data
    def GetClearEvent(self, event):
        self.painter.SetLinesData([])
        self.ConsoleMessage("Drawing area cleared")
Beispiel #2
0
class DoodleFrame(wx.Frame):
    """
    A DoodleFrame contains a DoodleWindow and a ControlPanel and manages
    their layout with a wx.BoxSizer.  A menu and associated event handlers
    provides for saving a doodle to a file, etc.
    """
    title = "Do a doodle"

    def __init__(self, parent):
        wx.Frame.__init__(self,
                          parent,
                          -1,
                          self.title,
                          size=(800, 600),
                          style=wx.DEFAULT_FRAME_STYLE
                          | wx.NO_FULL_REPAINT_ON_RESIZE)
        self.CreateStatusBar()
        self.MakeMenu()
        self.filename = None

        self.doodle = DoodleWindow(self, -1)
        cPanel = ControlPanel(self, -1, self.doodle)

        # Create a sizer to layout the two windows side-by-side.
        # Both will grow vertically, the doodle window will grow
        # horizontally as well.
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(cPanel, 0, wx.EXPAND)
        box.Add(self.doodle, 1, wx.EXPAND)

        # Tell the frame that it should layout itself in response to
        # size events.
        self.SetAutoLayout(True)
        self.SetSizer(box)

    def SaveFile(self):
        if self.filename:
            data = self.doodle.GetLinesData()
            f = open(self.filename, 'w')
            cPickle.dump(data, f)
            f.close()

    def ReadFile(self):
        if self.filename:
            try:
                f = open(self.filename, 'r')
                data = cPickle.load(f)
                f.close()
                self.doodle.SetLinesData(data)
            except cPickle.UnpicklingError:
                wx.MessageBox("%s is not a doodle file." % self.filename,
                              "oops!",
                              style=wx.OK | wx.ICON_EXCLAMATION)

    def MakeMenu(self):
        # create the file menu
        menu1 = wx.Menu()

        # Using the "\tKeyName" syntax automatically creates a
        # wx.AcceleratorTable for this frame and binds the keys to
        # the menu items.
        menu1.Append(idOPEN, "&Open\tCtrl-O", "Open a doodle file")
        menu1.Append(idSAVE, "&Save\tCtrl-S", "Save the doodle")
        menu1.Append(idSAVEAS, "Save &As", "Save the doodle in a new file")
        menu1.AppendSeparator()
        menu1.Append(idCLEAR, "&Clear", "Clear the current doodle")
        menu1.AppendSeparator()
        menu1.Append(idEXIT, "E&xit", "Terminate the application")

        # and the help menu
        menu2 = wx.Menu()
        menu2.Append(idABOUT, "&About\tCtrl-H",
                     "Display the gratuitous 'about this app' thingamajig")

        # and add them to a menubar
        menuBar = wx.MenuBar()
        menuBar.Append(menu1, "&File")
        menuBar.Append(menu2, "&Help")
        self.SetMenuBar(menuBar)

        wx.EVT_MENU(self, idOPEN, self.OnMenuOpen)
        wx.EVT_MENU(self, idSAVE, self.OnMenuSave)
        wx.EVT_MENU(self, idSAVEAS, self.OnMenuSaveAs)
        wx.EVT_MENU(self, idCLEAR, self.OnMenuClear)
        wx.EVT_MENU(self, idEXIT, self.OnMenuExit)
        wx.EVT_MENU(self, idABOUT, self.OnMenuAbout)

    wildcard = "Doodle files (*.ddl)|*.ddl|All files (*.*)|*.*"

    def OnMenuOpen(self, event):
        dlg = wx.FileDialog(self,
                            "Open doodle file...",
                            os.getcwd(),
                            style=wx.OPEN,
                            wildcard=self.wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetPath()
            self.ReadFile()
            self.SetTitle(self.title + ' -- ' + self.filename)
        dlg.Destroy()

    def OnMenuSave(self, event):
        if not self.filename:
            self.OnMenuSaveAs(event)
        else:
            self.SaveFile()

    def OnMenuSaveAs(self, event):
        dlg = wx.FileDialog(self,
                            "Save doodle as...",
                            os.getcwd(),
                            style=wx.SAVE | wx.OVERWRITE_PROMPT,
                            wildcard=self.wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            if not os.path.splitext(filename)[1]:
                filename = filename + '.ddl'
            self.filename = filename
            self.SaveFile()
            self.SetTitle(self.title + ' -- ' + self.filename)
        dlg.Destroy()

    def OnMenuClear(self, event):
        self.doodle.SetLinesData([])
        self.SetTitle(self.title)

    def OnMenuExit(self, event):
        self.Close()

    def OnMenuAbout(self, event):
        dlg = DoodleAbout(self)
        dlg.ShowModal()
        dlg.Destroy()