Exemplo n.º 1
0
class ZFlickrAuthorizationWizardPage(ZNewMediaStorageWizardPage):
    def __init__(self, model, parent):
        ZNewMediaStorageWizardPage.__init__(self, model, parent)
        self.flickr = flickrapi.FlickrAPI(FLICKR_API_KEY, SECRET)
        self.frob = None
        self.token = None
        self.authorized = False

    # end __init__()

    def _createWidgets(self):
        self.descriptionLabel = wx.StaticText(
            self, wx.ID_ANY,
            _extstr(u"flickrprovider.FlickrAuthDescription"))  #$NON-NLS-1$
        self.stepOneLabel = wx.StaticText(
            self, wx.ID_ANY,
            _extstr(u"flickrprovider.BeginAuthLabel"))  #$NON-NLS-1$
        self.stepOneButton = wx.Button(
            self, wx.ID_ANY, _extstr(u"flickrprovider.Begin"))  #$NON-NLS-1$
        self.stepTwoLabel = wx.StaticText(
            self, wx.ID_ANY,
            _extstr(u"flickrprovider.ClickHereLabel"))  #$NON-NLS-1$
        self.stepTwoButton = wx.Button(
            self, wx.ID_ANY,
            _extstr(u"flickrprovider.Authorized"))  #$NON-NLS-1$

        self.resultLabel = wx.StaticText(
            self, wx.ID_ANY,
            _extstr(u"flickrprovider.NotYetAuthorized"))  #$NON-NLS-1$
        self.completeBitmap = getResourceRegistry().getBitmap(
            u"images/common/check.png")  #$NON-NLS-1$
        self.completeImage = ZStaticBitmap(self, self.completeBitmap)
        self.warningBitmap = getResourceRegistry().getBitmap(
            u"images/common/warning.png")  #$NON-NLS-1$
        self.warningImage = ZStaticBitmap(self, self.warningBitmap)

    # end _createWidgets()

    def _populateWidgets(self):
        self.stepOneButton.Enable(True)
        self.stepTwoButton.Enable(False)
        self.warningImage.Show(True)
        self.completeImage.Show(False)
        self._fireInvalidEvent()

    # end _populateWidgets()

    def _bindWidgetEvents(self):
        self.Bind(wx.EVT_BUTTON, self.onStepOneButton, self.stepOneButton)
        self.Bind(wx.EVT_BUTTON, self.onStepTwoButton, self.stepTwoButton)

    # end _bindWidgetEvents()

    def _layoutWidgets(self):
        flexGridSizer = wx.FlexGridSizer(2, 2, 5, 5)
        flexGridSizer.AddGrowableCol(1)
        flexGridSizer.Add(self.stepOneLabel, 0,
                          wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
                          5)
        flexGridSizer.Add(self.stepOneButton, 0, wx.EXPAND | wx.RIGHT, 5)
        flexGridSizer.Add(self.stepTwoLabel, 0,
                          wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
                          5)
        flexGridSizer.Add(self.stepTwoButton, 0, wx.EXPAND | wx.RIGHT, 5)

        resultSizer = wx.BoxSizer(wx.HORIZONTAL)
        resultSizer.Add(self.warningImage, 0, wx.EXPAND | wx.ALL, 2)
        resultSizer.Add(self.completeImage, 0, wx.EXPAND | wx.ALL, 2)
        resultSizer.Add(self.resultLabel, 1, wx.ALIGN_CENTER_VERTICAL | wx.ALL,
                        2)

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.descriptionLabel, 1, wx.EXPAND | wx.ALL, 10)
        box.AddSizer(flexGridSizer, 1, wx.EXPAND | wx.ALL, 5)
        box.AddSizer(resultSizer, 0, wx.EXPAND | wx.ALL, 10)
        self.SetAutoLayout(True)
        self.SetSizer(box)

    # end _layoutWidgets()

    def onStepOneButton(self, event):
        bc = wx.BusyCursor()
        try:
            flickr = flickrapi.FlickrAPI(FLICKR_API_KEY, SECRET)
            (token,
             frob) = flickr.get_token_part_one(perms=u"delete")  #$NON-NLS-1$
            self.token = token
            self.frob = frob
            if token:
                self._onAuthorized()
            else:
                self.stepOneButton.Enable(False)
                self.stepTwoButton.Enable(True)
        finally:
            del bc
        event.Skip()

    # end onStepOneButton()

    def onStepTwoButton(self, event):
        token = None
        try:
            token = self.flickr.get_token_part_two((None, self.frob))
        except:
            pass
        if token:
            self.token = token
            self._onAuthorized()
        else:
            self.resultLabel.SetLabel(
                _extstr(u"flickrprovider.StillNotAuthorized"))  #$NON-NLS-1$
            self._populateWidgets()
        event.Skip()

    # end onStepTwoButton()

    def onEnter(self, session, eventDirection):  #@UnusedVariable
        self.paramsPageProps = session.getProperty(
            u"params-page.properties")  #$NON-NLS-1$
        if not self.authorized:
            self._fireInvalidEvent()

    # end onEnter()

    def onExit(self, session, eventDirection):  #@UnusedVariable
        if eventDirection == ZWizardPage.NEXT:
            params = session.getProperty(
                u"params-page.properties")  #$NON-NLS-1$
            params[u"token"] = self.token  #$NON-NLS-1$
        return True

    # end onExit()

    def _onAuthorized(self):
        self.resultLabel.SetLabel(
            _extstr(u"flickrprovider.RavenIsAuthorized"))  #$NON-NLS-1$
        self.warningImage.Show(False)
        self.completeImage.Show(True)
        self.Layout()
        self.stepOneButton.Enable(False)
        self.stepTwoButton.Enable(False)
        self._fireValidEvent()

    # end _onAuthorized()

    def getDataProperties(self):
        rval = {}
        if self.token:
            self.paramsPageProps[u"token"] = self.token  #$NON-NLS-1$
            rval[
                u"params-page.properties"] = self.paramsPageProps  #$NON-NLS-1$
        return rval
Exemplo n.º 2
0
class ZAdvancedTextBox(wx.Panel):
    def __init__(self, parent, bitmap, choices, multiSelect=False):
        self.bitmap = bitmap
        self.choices = choices
        self.selectedChoices = []
        self.multiSelect = multiSelect
        self.backgroundColor = getDefaultControlBackgroundColor()
        self.borderColor = getDefaultControlBorderColor()

        wx.Panel.__init__(self, parent, wx.ID_ANY, style=wx.NO_BORDER)

        self.SetBackgroundColour(self.backgroundColor)

        self._createWidgets()
        self._layoutWidgets()
        self._bindWidgetEvents()

    # end __init__()

    def GetValue(self):
        return self.textBox.GetValue()

    # end GetValue()

    def SetValue(self, value):
        self.textBox.SetValue(value)

    # end SetValue()

    def SetToolTipString(self, tooltip):
        self.textBox.SetToolTipString(tooltip)

    # end SetToolTipString()

    def setCurrentChoice(self, choiceId):
        self.selectedChoices = [choiceId]

    # end setCurrentChoice()

    def setCurrentChoices(self, choiceIds):
        self.selectedChoices = choiceIds

    # end setCurrentChoices()

    def getCurrentChoice(self):
        if self.multiSelect:
            raise ZAppFrameworkException(
                u"Attempted to get a single choice from an advanced text box configured for multiple selection."
            )  #$NON-NLS-1$
        if self.selectedChoices:
            return self.selectedChoices[0]
        return None

    # end getCurrentChoice()

    def getCurrentChoices(self):
        if not self.multiSelect:
            raise ZAppFrameworkException(
                u"Attempted to get multiple choices from an advanced text box configured for single selection."
            )  #$NON-NLS-1$
        return self.selectedChoices

    # end getCurrentChoice()

    def _createWidgets(self):
        self.textBox = wx.TextCtrl(self,
                                   wx.ID_ANY,
                                   style=wx.NO_BORDER | wx.TE_PROCESS_ENTER)
        hasChoices = len(self.choices) > 1
        self.bitmapButton = ZImageButton(self, self.bitmap, hasChoices, None,
                                         hasChoices)
        self.staticBitmap = ZStaticBitmap(self, self.bitmap)
        self.clearButton = self._createClearButton()
        self._createChoiceMenu()

    # end _createWidgets()

    def _createChoiceMenu(self):
        menuModel = self._createChoiceMenuModel()
        menuContext = ZMenuActionContext(self)
        contentProvider = ZModelBasedMenuContentProvider(
            menuModel, menuContext)
        eventHandler = ZModelBasedMenuEventHandler(menuModel, menuContext)
        self.menu = ZMenu(self, menuModel.getRootNode(), contentProvider,
                          eventHandler)

    # end _createChoiceMenu()

    def _createChoiceMenuModel(self):
        model = ZMenuModel()
        for (label, bitmap, id) in self.choices:
            action = ZTextBoxChoiceAction(id)
            menuId = model.addMenuItemWithAction(label, 0, action)
            model.setMenuItemCheckbox(menuId, True)
            model.setMenuItemBitmap(menuId, bitmap)
        return model

    # end _createChoiceMenuModel()

    def _createClearButton(self):
        registry = getResourceRegistry()
        clearBmp = registry.getBitmap(
            u"images/widgets/textbox/clear.png")  #$NON-NLS-1$
        clearHoverBmp = registry.getBitmap(
            u"images/widgets/textbox/clear-hover.png")  #$NON-NLS-1$
        return ZImageButton(self, clearBmp, False, clearHoverBmp, False)

    # end _createClearButton()

    def _layoutWidgets(self):
        (w, h) = getTextDimensions(u"Zoundry",
                                   self.textBox)  #$NON-NLS-1$ @UnusedVariable
        self.textBox.SetSizeHints(-1, h + 2)

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(self.staticBitmap, 0, wx.ALIGN_CENTER | wx.RIGHT | wx.LEFT, 3)
        box.Add(self.bitmapButton, 0, wx.ALIGN_CENTER | wx.RIGHT | wx.LEFT, 3)
        box.Add((3, -1))
        box.Add(self.textBox, 1, wx.EXPAND | wx.TOP, 3)
        box.Add(self.clearButton, 0, wx.ALIGN_CENTER | wx.RIGHT | wx.LEFT, 3)

        self.clearButton.Show(False)
        if self.choices is None or len(self.choices) == 0:
            self.staticBitmap.Show(True)
            self.bitmapButton.Show(False)
        else:
            self.staticBitmap.Show(False)
            self.bitmapButton.Show(True)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddSizer(box, 1, wx.EXPAND | wx.ALL, 2)
        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        self.Layout()

    # end _layoutWidgets()

    def _bindWidgetEvents(self):
        self.Bind(wx.EVT_PAINT, self.onPaint, self)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.onEraseBackground, self)
        self.Bind(wx.EVT_BUTTON, self.onButtonClicked, self.bitmapButton)
        self.Bind(wx.EVT_TEXT, self.onText, self.textBox)
        self.Bind(wx.EVT_TEXT_ENTER, self.onPropagateEvent, self.textBox)
        self.Bind(wx.EVT_BUTTON, self.onClearButtonClicked, self.clearButton)

    # end _bindWidgetEvents()

    def onButtonClicked(self, event):
        if len(self.choices) > 1:
            (w, h) = self.GetSizeTuple()  #@UnusedVariable
            pos = wx.Point(1, h - 2)
            self.menu.refresh()
            self.PopupMenu(self.menu, pos)
        event.Skip

    # end onButtonClicked()

    def onClearButtonClicked(self, event):
        self.SetValue(u"")  #$NON-NLS-1$
        self.clearButton.Show(False)
        txtEvent = wx.CommandEvent(wxEVT_COMMAND_TEXT_ENTER, self.GetId())
        self.GetEventHandler().AddPendingEvent(txtEvent)
        event.Skip()

    # end onClearButtonClicked()

    def onText(self, event):
        if self.clearButton.IsShown() and not self.textBox.GetValue():
            self.clearButton.Show(False)
            self.Layout()
        elif not self.clearButton.IsShown() and self.textBox.GetValue():
            self.clearButton.Show(True)
            self.Layout()
        self.onPropagateEvent(event)

    # end onText()

    def onPropagateEvent(self, event):
        # Propagate the event
        event = event.Clone()
        event.SetId(self.GetId())
        self.GetEventHandler().AddPendingEvent(event)

    # end onPropagateEvent()

    def onEraseBackground(self, event):  #@UnusedVariable
        pass

    # end onEraseBackground()

    def onPaint(self, event):
        (width, height) = self.GetSizeTuple()
        paintDC = wx.BufferedPaintDC(self)
        paintDC.SetBackground(
            wx.Brush(wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE),
                     wx.SOLID))
        paintDC.Clear()

        # Draw the background white
        brush = wx.Brush(self.backgroundColor)
        paintDC.SetPen(wx.Pen(self.borderColor, 1, wx.SOLID))
        paintDC.SetBrush(brush)
        paintDC.DrawRectangle(0, 0, width, height)

        del paintDC
        event.Skip()

    # end onPaint

    def onChoice(self, choiceId):
        # Update the internal selection state.
        if self.multiSelect:
            if not choiceId in self.selectedChoices:
                self.selectedChoices.append(choiceId)
        else:
            self.selectedChoices = [choiceId]

        event = ZAdvTextBoxOptionEvent(self.GetId(), choiceId)
        self.GetEventHandler().AddPendingEvent(event)

    # end onChoice()

    def onUnChoice(self, choiceId):
        if choiceId in self.selectedChoices:
            self.selectedChoices.remove(choiceId)
Exemplo n.º 3
0
class ZImagePreviewPanel(ZTransparentPanel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        self._createWidgets()
        self._layoutWidgets()
    # end __init__()

    def _createWidgets(self):
        self.staticBox = wx.StaticBox(self, wx.ID_ANY, _extstr(u"infodetailswidgets.Preview")) #$NON-NLS-1$

        self.generatingMsg = ZProgressLabelCtrl(self, _extstr(u"infodetailswidgets.GeneratingPreview")) #$NON-NLS-1$
        self.previewUnavailableMsg = wx.StaticText(self, wx.ID_ANY, _extstr(u"infodetailswidgets.PreviewUnavailable")) #$NON-NLS-1$
        self.previewBmp = ZStaticBitmap(self, None)
    # end _createWidgets()

    def _layoutWidgets(self):
        staticBoxSizer = wx.StaticBoxSizer(self.staticBox, wx.HORIZONTAL)
        staticBoxSizer.Add(self.generatingMsg, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 3)
        staticBoxSizer.Add(self.previewUnavailableMsg, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 3)
        staticBoxSizer.Add(self.previewBmp, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 3)

        self.SetAutoLayout(True)
        self.SetSizer(staticBoxSizer)
    # end _layoutWidgets()

    def reset(self):
        self.generatingMsg.Show(True)
        self.generatingMsg.start()
        self.previewUnavailableMsg.Show(False)
        self.previewBmp.Show(False)
    # end reset()

    def updateFromError(self, error):
        # Log the error
        getLoggerService().exception(error)
        self.generatingMsg.Show(False)
        self.generatingMsg.stop()
        self.previewUnavailableMsg.Show(True)
        self.previewBmp.Show(False)
    # end updateFromError()

    def updateFromConnectionRespInfo(self, connectionRespInfo): #@UnusedVariable
        # Do nothing...
        pass
    # end updateFromConnectionRespInfo()

    def updateFromConnectionResp(self, connectionResp):
        imagingService = getApplicationModel().getService(IZAppServiceIDs.IMAGING_SERVICE_ID)
        bgColor = self.GetBackgroundColour()
        tnParams = ZThumbnailParams(backgroundColor = (bgColor.Red(), bgColor.Green(), bgColor.Blue()), dropShadow = True)
        tnFile = os.path.join(getApplicationModel().getUserProfile().getTempDirectory(), u"_ZImagePreviewPanel_tn.png") #$NON-NLS-1$
        try:
            imagingService.generateThumbnail(connectionResp.getContentFilename(), tnParams, tnFile)

            image = wx.Image(tnFile, getImageType(tnFile))
            if image is None:
                raise ZException()

            bitmap = image.ConvertToBitmap()
            self.previewBmp.setBitmap(bitmap)
            deleteFile(tnFile)
            self.generatingMsg.Show(False)
            self.generatingMsg.stop()
            self.previewUnavailableMsg.Show(False)
            self.previewBmp.Show(True)
        except Exception, e:
            getLoggerService().exception(e)
            self.updateFromError(e)