Пример #1
0
class ZSpellingPreferencePage(ZApplicationPreferencesPrefPage):
    def __init__(self, parent):
        ZApplicationPreferencesPrefPage.__init__(self, parent)
        self.originalProps = {}
        self.selectedLanguage = None

        self.model = ZSpellingPreferencePageModel()

    # end __init__()

    def createWidgets(self):
        self.dictionaryDownloadPanel = ZDictionaryDownloadPanel(self)
        self.enableSpellCheckCB = wx.CheckBox(
            self, wx.ID_ANY,
            _extstr(u"spellingprefpage.EnableSpellcheck"))  #$NON-NLS-1$
        provider = ZLanguagesListProvider(self.model)
        self.langListBox = ZListView(provider,
                                     self,
                                     style=wx.LC_VIRTUAL | wx.LC_REPORT
                                     | wx.LC_SINGLE_SEL)

    # end createWidgets()

    def populateWidgets(self):
        spellcheckEnabled = self.model.isSpellcheckEnabled()
        currentLanguage = self.model.getActiveLanguage()
        downloadTask = self.model.getDictionaryDownloadTask()
        # FIXME (EPW) there is a problem when closing the pref page and then re-opening it while the download is still in progress (unknown problem at this point)
        if downloadTask is not None and downloadTask.isRunning():
            self.dictionaryDownloadPanel.Show(True)
            self.dictionaryDownloadPanel.setTask(downloadTask)

            self.enableSpellCheckCB.SetValue(True)
            self.enableSpellCheckCB.Enable(False)
            self.langListBox.Enable(False)
        else:
            self.dictionaryDownloadPanel.Show(False)
            self.originalProps[
                u"spellcheck-enabled"] = spellcheckEnabled  #$NON-NLS-1$
            self.originalProps[u"language"] = currentLanguage  #$NON-NLS-1$

            self.enableSpellCheckCB.Enable(True)
            self.langListBox.Enable(True)

            self.enableSpellCheckCB.SetValue(spellcheckEnabled)
            self.langListBox.Enable(spellcheckEnabled)

            if spellcheckEnabled:
                idx = self.model.getLanguageIndex(currentLanguage)
                self.selectedLanguage = currentLanguage
                self.langListBox.Select(idx)
            else:
                currentLocale = self.model.getCurrentLocale()
                localeLang = self.model.findLanguage(currentLocale)
                idx = self.model.getLanguageIndex(localeLang)
                if idx is not None and idx >= 0:
                    self.selectedLanguage = self.model.getLanguages()[idx]
                    self.langListBox.Select(idx)

    # end populateWidgets()

    def bindWidgetEvents(self):
        self.Bind(wx.EVT_CHECKBOX, self.onEnableSpellCheck,
                  self.enableSpellCheckCB)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onLanguageSelected,
                  self.langListBox)

    # end bindWidgetEvents()

    def layoutWidgets(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.dictionaryDownloadPanel, 0, wx.EXPAND | wx.ALL, 2)
        sizer.Add(self.enableSpellCheckCB, 0, wx.EXPAND | wx.ALL, 2)
        sizer.Add(self.langListBox, 1, wx.EXPAND | wx.ALL, 2)

        prefSizer = wx.BoxSizer(wx.VERTICAL)
        prefSizer.AddSizer(sizer, 1, wx.EXPAND | wx.ALL, 5)

        self.SetAutoLayout(True)
        self.SetSizer(prefSizer)
        self.Layout()

    # end layoutWidgets()

    def onEnableSpellCheck(self, event):
        self.langListBox.Enable(event.IsChecked())
        self.getPrefsDialog().onPrefPageChange()

    # end onEnableSpellCheck()

    def onLanguageSelected(self, event):
        self.selectedLanguage = self.model.getLanguages()[
            event.GetItem().GetId()]
        self.getPrefsDialog().onPrefPageChange()
        event.Skip()

    # end onLanguageSelected()

    def onDictionaryDownloadComplete(self, task):
        langCode = task.getLanguageCode()
        self.selectedLanguage = self.model.findLanguage(langCode)
        self.originalProps[u"spellcheck-enabled"] = True  #$NON-NLS-1$
        self.originalProps[u"language"] = self.selectedLanguage  #$NON-NLS-1$

        self.langListBox.refresh()

        self.dictionaryDownloadPanel.Show(False)
        self.enableSpellCheckCB.SetValue(True)
        self.enableSpellCheckCB.Enable(True)
        self.langListBox.Enable(True)

        self.Layout()
        self.getPrefsDialog().onPrefPageChange()

    # end onDictionaryDownloadComplete()

    def onDictionaryDownloadFailed(self):
        pass

    # end onDictionaryDownloadFailed()

    def onDictionaryDownloadCancelled(self):
        self.populateWidgets()
        self.Layout()
        self.getPrefsDialog().onPrefPageChange()

    # end onDictionaryDownloadCancelled()

    def isDirty(self):
        # If we are downloading a dictionary, then we are NOT dirty
        downloadTask = self.model.getDictionaryDownloadTask()
        if downloadTask is not None and downloadTask.isRunning():
            return False

        if self.originalProps[
                u"spellcheck-enabled"] != self.enableSpellCheckCB.IsChecked(
                ):  #$NON-NLS-1$
            return True
        if self.enableSpellCheckCB.IsChecked() and self.originalProps[
                u"language"] != self.selectedLanguage:  #$NON-NLS-1$
            return True
        return False

    # end isDirty()

    def isValid(self):
        if not self.enableSpellCheckCB.IsChecked():
            return True
        else:
            return self.selectedLanguage is not None

    # end isValid()

    def apply(self):
        if self.enableSpellCheckCB.IsChecked():
            if self.model.isDictionaryDownloaded(self.selectedLanguage):
                self._enableSpellCheck()
                return True
            else:
                if ZShowYesNoMessage(
                        self,
                        _extstr(u"spellingprefpage.DownloadVerifyMessage"),
                        _extstr(u"spellingprefpage.DownloadDictionary"
                                )):  #$NON-NLS-2$ #$NON-NLS-1$
                    self._startDictionaryDownload()
                    return True
                else:
                    # Veto the apply() if the user said "no" to the download.
                    return False
        else:
            self.model.disableSpellCheck()
            return True

    # end apply()

    def _enableSpellCheck(self):
        if self.model.enableSpellCheck(self.selectedLanguage):
            ZShowInfoMessage(
                self, _extstr(u"spellingprefpage.AppMustBeRestartedMessage"),
                _extstr(u"spellingprefpage.RestartNeeded"
                        ))  #$NON-NLS-2$ #$NON-NLS-1$
        self.populateWidgets()

    # end _enableSpellCheck()

    def _startDictionaryDownload(self):
        busyIndicator = wx.BusyCursor()
        task = self.model.downloadDictionary(self.selectedLanguage)
        self.dictionaryDownloadPanel.Enable(True)
        self.dictionaryDownloadPanel.setTask(task)
        self.originalProps[u"spellcheck-enabled"] = True  #$NON-NLS-1$
        self.originalProps[u"language"] = self.selectedLanguage  #$NON-NLS-1$
        self.dictionaryDownloadPanel.Show(True)
        self.enableSpellCheckCB.Enable(False)
        self.langListBox.Enable(False)
        self.Layout()
        del busyIndicator

    # end _startDictionaryDownload()

    def rollback(self):
        getLoggerService().debug(u"rollback")  #$NON-NLS-1$
        self.populateWidgets()

    # end rollback()

    def destroy(self):
        ZUserPreferencePage.destroy(self)
        self.dictionaryDownloadPanel.destroy()
Пример #2
0
class ZLocalePreferencePage(ZApplicationPreferencesPrefPage):

    def __init__(self, parent):
        ZApplicationPreferencesPrefPage.__init__(self, parent)
        self.originalProps = {}
        self.selectedLocale = None
        
        self.model = ZLocalePreferencePageModel()
    # end __init__()

    def createWidgets(self):
        self.localeDetailsStaticBox = wx.StaticBox(self, wx.ID_ANY)
        self.defaultLocaleLabel = wx.StaticText(self, wx.ID_ANY, _extstr(u"localeprefpage.DefaultLocale")) #$NON-NLS-1$
        self.defaultLocaleLabel.SetFont(getDefaultFontBold())
        self.defaultLocale = wx.StaticText(self, wx.ID_ANY, u"") #$NON-NLS-1$
        self.defaultLocaleBitmap = wx.StaticBitmap(self, wx.ID_ANY, wx.NullBitmap)
        self.overrideCB = wx.CheckBox(self, wx.ID_ANY, _extstr(u"localeprefpage.OverrideDefaultLocale")) #$NON-NLS-1$
        provider = self._createLocaleListProvider()
        self.localeList = ZListView(provider, self, style = wx.LC_VIRTUAL | wx.LC_REPORT | wx.LC_SINGLE_SEL)
    # end createWidgets()

    def populateWidgets(self):
        hasLocaleOverride = self.model.hasLocaleOverride()
        currentLocale = self.model.getCurrentLocale()

        self.originalProps[u"override-enabled"] = hasLocaleOverride #$NON-NLS-1$
        self.originalProps[u"locale"] = currentLocale #$NON-NLS-1$

        self.defaultLocale.SetLabel(self.model.getDefaultLocaleName())
        bitmap = getFlagBitmapForLocale(currentLocale)
        if bitmap is not None:
            self.defaultLocaleBitmap.SetBitmap(bitmap)
        self.overrideCB.SetValue(hasLocaleOverride)
        self.localeList.Enable(hasLocaleOverride)
        
        if hasLocaleOverride:
            idx = self.model.getLocaleOverrideIndex()
            self.selectedLocale = currentLocale
            self.localeList.Select(idx)
    # end populateWidgets()

    def bindWidgetEvents(self):
        self.Bind(wx.EVT_CHECKBOX, self.onEnableOverride, self.overrideCB)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onLocaleSelected, self.localeList)
    # end bindWidgetEvents()

    def layoutWidgets(self):
        defaultLocaleSizer = wx.StaticBoxSizer(self.localeDetailsStaticBox, wx.HORIZONTAL)
        defaultLocaleSizer.Add(self.defaultLocaleLabel, 0, wx.EXPAND | wx.ALL, 5)
        defaultLocaleSizer.Add(self.defaultLocaleBitmap, 0, wx.EXPAND)
        defaultLocaleSizer.Add(self.defaultLocale, 1, wx.EXPAND | wx.ALL, 5)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddSizer(defaultLocaleSizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 5)
        sizer.Add(self.overrideCB, 0, wx.EXPAND | wx.ALL, 5)
        sizer.Add(self.localeList, 1, wx.EXPAND | wx.ALL, 5)

        prefSizer = wx.BoxSizer(wx.VERTICAL)
        prefSizer.AddSizer(sizer, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 3)

        self.SetAutoLayout(True)
        self.SetSizer(prefSizer)
        self.Layout()
    # end layoutWidgets()
    
    def _createLocaleListProvider(self):
        return ZLocaleListContentProvider(self.model)
    # end _createLocaleListProvider()
    
    def onEnableOverride(self, event):
        self.localeList.Enable(event.IsChecked())
        self.getPrefsDialog().onPrefPageChange()
        event.Skip()
    # end onEnableOverride()
    
    def onLocaleSelected(self, event):
        self.selectedLocale = self.model.getLanguagePacks()[event.GetItem().GetId()]
        self.getPrefsDialog().onPrefPageChange()
        event.Skip()
    # end onLocaleSelected()
    
    def isDirty(self):
        if self.originalProps[u"override-enabled"] != self.overrideCB.IsChecked(): #$NON-NLS-1$
            return True
        if self.overrideCB.IsChecked() and self.originalProps[u"locale"] != self._getSelectedLocale(): #$NON-NLS-1$
            return True
        return False
    # end isDirty()
    
    def isValid(self):
        if not self.overrideCB.IsChecked():
            return True
        else:
            return self.selectedLocale is not None
    # end isValid()

    def apply(self):
        ZShowInfoMessage(self, _extstr(u"localeprefpage.ChangesWillTakeEffectAfterRestart"), _extstr(u"localeprefpage.LocaleChanged")) #$NON-NLS-2$ #$NON-NLS-1$
        self.model.saveChanges(self.overrideCB.IsChecked(), self.selectedLocale)
        self.populateWidgets()
        return True
    # end apply()

    def rollback(self):
        self.populateWidgets()
    # end rollback()
    
    def _getSelectedLocale(self):
        return self.selectedLocale