Index: gc_lang/fr/oxt/Lexicographer/Enumerator.py ================================================================== --- gc_lang/fr/oxt/Lexicographer/Enumerator.py +++ gc_lang/fr/oxt/Lexicographer/Enumerator.py @@ -11,10 +11,22 @@ import grammalecte.graphspell as sc from com.sun.star.task import XJobExecutor from com.sun.star.awt import XActionListener +from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK +# BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY +# DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE +from com.sun.star.awt.MessageBoxType import INFOBOX, ERRORBOX # MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX + +def MessageBox (xDocument, sMsg, sTitle, nBoxType=INFOBOX, nBoxButtons=BUTTONS_OK): + xParentWin = xDocument.CurrentController.Frame.ContainerWindow + ctx = uno.getComponentContext() + xToolkit = ctx.ServiceManager.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx) + xMsgBox = xToolkit.createMessageBox(xParentWin, nBoxType, nBoxButtons, sTitle, sMsg) + return xMsgBox.execute() + def hexToRBG (sHexa): r = int(sHexa[:2], 16) g = int(sHexa[2:4], 16) b = int(sHexa[4:], 16) @@ -54,10 +66,11 @@ self.xDocument = self.xDesktop.getCurrentComponent() self.xContainer = None self.xDialog = None self.oSpellChecker = None self.oTokenizer = None + self.dWord = {} def _addWidget (self, name, wtype, x, y, w, h, **kwargs): xWidget = self.xDialog.createInstance('com.sun.star.awt.UnoControl%sModel' % wtype) xWidget.Name = name xWidget.PositionX = x @@ -125,15 +138,16 @@ self._addWidget('unknown_button', 'Button', nX+150, nY1+12, 70, 11, Label = self.dUI.get('unknown_button', "#err")) self.xGridModel = self._addGrid("list_grid", nX, nY1+25, nWidth, 181, \ [ {"Title": self.dUI.get("words", "#err"), "ColumnWidth": 175}, {"Title": "Occurrences", "ColumnWidth": 45} ], \ SelectionModel = uno.Enum("com.sun.star.view.SelectionType", "MULTI") \ ) - self._addWidget('num_of_entries', 'FixedText', nX, nY1+210, 60, nHeight, Label = self.dUI.get('num_of_entries', "#err"), Align = 2) - self.xNumWord = self._addWidget('num_of_entries_res', 'FixedText', nX+65, nY1+210, 25, nHeight, Label = "—") - self._addWidget('tot_of_entries', 'FixedText', nX+90, nY1+210, 60, nHeight, Label = self.dUI.get('tot_of_entries', "#err"), Align = 2) - self.xTotWord = self._addWidget('tot_of_entries_res', 'FixedText', nX+155, nY1+210, 30, nHeight, Label = "—") - self.xSearch = self._addWidget('search_button', 'Button', nX+190, nY1+210, 30, nHeight, Label = ">>>", Enabled = False) + self._addWidget('num_of_entries', 'FixedText', nX, nY1+210, 30, nHeight, Label = self.dUI.get('num_of_entries', "#err"), Align = 2) + self.xNumWord = self._addWidget('num_of_entries_res', 'FixedText', nX+35, nY1+210, 25, nHeight, Label = "—") + self._addWidget('tot_of_entries', 'FixedText', nX+60, nY1+210, 30, nHeight, Label = self.dUI.get('tot_of_entries', "#err"), Align = 2) + self.xTotWord = self._addWidget('tot_of_entries_res', 'FixedText', nX+95, nY1+210, 30, nHeight, Label = "—") + self.xSearch = self._addWidget('search_button', 'Button', nX+145, nY1+210, 30, nHeight, Label = ">>>", Enabled = False) + self.xExport = self._addWidget('export_button', 'Button', nX+180, nY1+210, 40, nHeight, Label = self.dUI.get('export', "#err"), Enabled = False) # Tag # Note: the only way to group RadioButtons is to create them successively self._addWidget("charstyle_section", 'FixedLine', nX, nY2, 200, nHeight, Label = self.dUI.get("charstyle_section", "#err"), FontDescriptor = xFDTitle) self.xAccent = self._addWidget('emphasis', 'RadioButton', nX, nY2+12, 55, nHeight, Label = self.dUI.get('emphasis', "#err")) @@ -160,10 +174,12 @@ self.xContainer.getControl('count2_button').setActionCommand('CountByLemma') self.xContainer.getControl('unknown_button').addActionListener(self) self.xContainer.getControl('unknown_button').setActionCommand('UnknownWords') self.xContainer.getControl('search_button').addActionListener(self) self.xContainer.getControl('search_button').setActionCommand('Search') + self.xContainer.getControl('export_button').addActionListener(self) + self.xContainer.getControl('export_button').setActionCommand('Export') self.xContainer.getControl('tag_button').addActionListener(self) self.xContainer.getControl('tag_button').setActionCommand('Tag') self.xContainer.getControl('close_button').addActionListener(self) self.xContainer.getControl('close_button').setActionCommand('Close') self.xContainer.setVisible(True) # True for non modal dialog @@ -176,24 +192,29 @@ try: if xActionEvent.ActionCommand == "Count": self.count(self.dUI.get("words", "#err")) self.xTag.Enabled = True self.xSearch.Enabled = True + self.xExport.Enabled = True elif xActionEvent.ActionCommand == "CountByLemma": self.count(self.dUI.get("lemmas", "#err"), bByLemma=True) self.xTag.Enabled = False self.xSearch.Enabled = False + self.xExport.Enabled = True elif xActionEvent.ActionCommand == "UnknownWords": self.count(self.dUI.get("unknown_words", "#err"), bOnlyUnknownWords=True) self.xTag.Enabled = True self.xSearch.Enabled = True + self.xExport.Enabled = True elif xActionEvent.ActionCommand == "Search": if not self.xGridControl.hasSelectedRows(): return lRow = self.xGridControl.getSelectedRows() aWord = set([ self.xGridModel.GridDataModel.getCellData(0, n) for n in lRow ]) self.gotoWord(aWord) + elif xActionEvent.ActionCommand == "Export": + self.export() elif xActionEvent.ActionCommand == "Tag": if not self.xGridControl.hasSelectedRows(): return lRow = self.xGridControl.getSelectedRows() aWord = set([ self.xGridModel.GridDataModel.getCellData(0, n) for n in lRow ]) @@ -250,26 +271,51 @@ self._setTitleOfFirstColumn(sTitle) self.xProgressBar.ProgressValueMax = self._countParagraph() * 2 self.xProgressBar.ProgressValue = 0 xGridDataModel = self.xGridModel.GridDataModel xGridDataModel.removeAllRows() - dWord = {} + self.dWord.clear() for sParagraph in self._getParagraphsFromText(): - dWord = self.oSpellChecker.countWordsOccurrences(sParagraph, bByLemma, bOnlyUnknownWords, dWord) + self.dWord = self.oSpellChecker.countWordsOccurrences(sParagraph, bByLemma, bOnlyUnknownWords, self.dWord) self.xProgressBar.ProgressValue += 1 - self.xProgressBar.ProgressValueMax += len(dWord) + self.xProgressBar.ProgressValueMax += len(self.dWord) i = 0 nTotOccur = 0 - for k, w in sorted(dWord.items(), key=lambda t: t[1], reverse=True): - xGridDataModel.addRow(i, (k, w)) + for sWord, nOccur in sorted(self.dWord.items(), key=lambda t: t[1], reverse=True): + xGridDataModel.addRow(i, (sWord, nOccur)) self.xProgressBar.ProgressValue += 1 i += 1 - nTotOccur += w + nTotOccur += nOccur self.xProgressBar.ProgressValue = self.xProgressBar.ProgressValueMax self.xNumWord.Label = str(i) self.xTotWord.Label = nTotOccur + def export (self): + if not self.dWord: + return + sText = "" + for sWord, nOccur in sorted(self.dWord.items(), key=lambda t: t[1], reverse=True): + sText += sWord + "\t" + str(nOccur) + "\n" + try: + xFilePicker = self.xSvMgr.createInstanceWithContext('com.sun.star.ui.dialogs.FilePicker', self.ctx) # other possibility: com.sun.star.ui.dialogs.SystemFilePicker + xFilePicker.initialize([uno.getConstantByName("com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE")]) # seems useless + xFilePicker.appendFilter("Supported files", "*.txt") + xFilePicker.setDefaultName("word_count.txt") # useless, doesn’t work + xFilePicker.setDisplayDirectory("") + xFilePicker.setMultiSelectionMode(False) + nResult = xFilePicker.execute() + if nResult == 1: + # lFile = xFilePicker.getSelectedFiles() + lFile = xFilePicker.getFiles() + spfExported = lFile[0][8:] # remove file:/// + #spfExported = os.path.join(os.path.expanduser("~"), "fr.personal.json") + with open(spfExported, "w", encoding="utf-8") as hDst: + hDst.write(sText) + except: + sMessage = traceback.format_exc() + MessageBox(self.xDocument, sMessage, self.dUI.get('export_title', "#err"), ERRORBOX) + @_waitPointer def tagText (self, aWord, sAction=""): if not sAction: return self.xProgressBar.ProgressValueMax = self._countParagraph() Index: gc_lang/fr/oxt/Lexicographer/enum_strings.py ================================================================== --- gc_lang/fr/oxt/Lexicographer/enum_strings.py +++ gc_lang/fr/oxt/Lexicographer/enum_strings.py @@ -9,12 +9,13 @@ "list_section": "Énumération des occurrences", "count_button": "Compter tout", "count2_button": "Compter par lemme", "unknown_button": "Mots inconnus", - "num_of_entries": "Nombre d’entrées :", - "tot_of_entries": "Total des entrées :", + "num_of_entries": "Entrées :", + "tot_of_entries": "Total :", + "export": "Exporter", "words": "Mots", "lemmas": "Lemmes", "unknown_words": "Mots inconnus", @@ -25,21 +26,24 @@ "emphasis": "Accentuation", "strong_emphasis": "Accentuation forte", "nostyle": "Aucun", "tag_button": "Taguer", + "export_title": "Exportation des données", + "close_button": "Fermer", }, "en": { "title": "Grammalecte · Enumerator of words", "list_section": "Occurrences enumeration", "count_button": "Count all", "count2_button": "Count by lemma", "unknown_button": "Unknown words", - "num_of_entries": "Number of entries:", - "tot_of_entries": "Total of words:", + "num_of_entries": "Entries:", + "tot_of_entries": "Total:", + "export": "Export", "words": "Words", "lemmas": "Lemmas", "unknown_words": "Unknown words", @@ -49,9 +53,11 @@ "nounderline": "Erase", "emphasis": "Emphasis", "strong_emphasis": "Strong emphasis", "nostyle": "None", "tag_button": "Tag", + + "export_title": "Data export", "close_button": "Close", }, }