def replace(self, oldString, newString): changesMade = 0 search = self.unoObjs.document.createSearchDescriptor() search.SearchString = oldString search.SearchAll = True search.SearchWords = True search.SearchCaseSensitive = False selsFound = self.unoObjs.document.findAll(search) self.selsCount = selsFound.getCount() if selsFound.getCount() == 0: logger.debug("Did not find anything.") return changesMade for selIndex, oSel in enumerate(iteruno.byIndex(selsFound)): logger.debug("Found selection %s", selIndex) if self.askEach: self.unoObjs.viewcursor.gotoRange(oSel.getStart(), False) self.unoObjs.viewcursor.gotoRange(oSel.getEnd(), True) result = self.msgboxFour.display( "Make this change? (%s -> %s)", oldString, newString) if result == "yes": # keep going pass elif result == "no": continue elif result == "yesToAll": self.askEach = False else: raise exceptions.UserInterrupt() self.unoObjs.viewcursor.goRight(0, False) # deselect oTextCursTmp = oSel.getText().createTextCursorByRange(oSel) changeString(oTextCursTmp, newString) changesMade += 1 return changesMade
def doSearch(self, search): logger.debug(util.funcName('begin')) self.ranger.resetRanges() selsFound = self.unoObjs.document.findAll(search) self.selsCount = selsFound.getCount() if self.selsCount == 0: logger.debug("Did not find anything.") return progressRange = ProgressRange(start=self.progressBar.val, stop=90, ops=self.selsCount, pbar=self.progressBar) progressRange.partSize = 2 progressRange.updatePart(1) # show some movement for selIndex, selectionFound in enumerate(iteruno.byIndex(selsFound)): logger.debug("Found selection %d", selIndex) self.ranger.addRangesForCursor(selectionFound) progressRange.update(selIndex) if (self.config.matchesLimit > 0 and selIndex >= self.config.matchesLimit): # Stop here. This may help with large documents, doing a # little at a time. logger.debug("Stopping at this match") return
def resizeNumberingCol(self, colWidthText, prevColWidth): """ Resize the width of the column that contains example numbering. Size is an integer percentage of the page width. @param string colWidthText @param int prevColWidth throws exceptions.ChoiceProblem It would be nice if there were such a thing as table styles. Then this function would presumably not be needed. """ logger.debug(util.funcName('begin')) if colWidthText == "": raise exceptions.ChoiceProblem( "Please enter a value for column width.") try: newVal = int(colWidthText) except: raise exceptions.ChoiceProblem("Column width is not a number.") if newVal == prevColWidth: logger.debug("No need to change.") return if newVal > 50: # more than 50% is unreasonable raise exceptions.ChoiceProblem( "Value %d for column width is too high.", newVal) elif newVal <= 0: raise exceptions.ChoiceProblem( "Value for column width must be more than zero.") PERCENT_TO_SEP = 100 # Separator width 10,000 is 100%. # The user enters a number like 5 meaning 5%. # So 5 * 100 would be 500 which is 5% of 10,000 MARGIN_OF_ERROR = 2 prevVal = prevColWidth * PERCENT_TO_SEP newVal = newVal * PERCENT_TO_SEP tables = self.unoObjs.document.getTextTables() logger.debug( "looping through %d tables. prevVal = %d", tables.getCount(), prevVal) for table in iteruno.byIndex(tables): separators = table.getPropertyValue("TableColumnSeparators") if separators is None: logger.debug( "No separators set for table %s", table.getName()) continue sep0Pos = separators[0].Position logger.debug( "table %s separator is %d", table.getName(), sep0Pos) if (sep0Pos > prevVal - MARGIN_OF_ERROR and sep0Pos < prevVal + MARGIN_OF_ERROR): separators[0].Position = newVal table.TableColumnSeparators = separators logger.debug("changed to %d", newVal) self.userVars.store("NumberingColWidth", str(newVal // PERCENT_TO_SEP)) logger.debug(util.funcName('end'))
def footnotes(self): textSections = [] logger.debug("looking for footnotes") footnotes = self.unoObjs.document.getFootnotes() endnotes = self.unoObjs.document.getEndnotes() for notes in (footnotes, endnotes): for oNote in iteruno.byIndex(notes): for oPar in iteruno.byEnum(oNote): if oPar.supportsService("com.sun.star.text.Paragraph"): for oTextPortion in iteruno.byEnum(oPar): if oTextPortion.TextPortionType == "Text": # TextPortions include the TextRange service. textSections.append(oTextPortion) return textSections
def getListOfStyles(familyName, unoObjs): """Returns a list of tuples (display name, underlying name) The display name may be localized or changed for readability. In the rest of this module, the underlying name is used. """ logger.debug(util.funcName()) families = unoObjs.document.getStyleFamilies() styleObjs = families.getByName(familyName) styleNames = [] for style in iteruno.byIndex(styleObjs): # DisplayName is in the com.sun.star.style.Style service. styleNames.append( (style.getPropertyValue("DisplayName"), style.getName())) # sort by display name styleNames.sort(key=itemgetter(0)) logger.debug("styleNames has %d elements", len(styleNames)) return styleNames
def assignAction(self, oButtonModel, sDestination): """assign sScriptURL event as css.awt.XActionListener::actionPerformed. event is assigned to the control described by the nIndex in the oForm container """ logger.debug(util.funcName('begin')) logger.debug("specify which is the main document") if not self.calledSetMainDoc: sMacro = ( 'macro:///LingToolsBasic.ModuleMain.setMainDocURL("%s")' % self.mainDoc.url) logger.debug(sMacro) self.mainDoc.dispatcher.executeDispatch(self.writerDoc.frame, sMacro, "", 0, ()) self.calledSetMainDoc = True logger.debug("getting index of button") oForm = self.writerDoc.document.getDrawPage().getForms().getByIndex(0) logger.debug("looking for button '%s'", oButtonModel.getName()) logger.debug("Form has %d elements", oForm.getCount()) nIndex = -1 for formElemIndex, formElem in enumerate(iteruno.byIndex(oForm)): logger.debug(formElem.getName()) if formElem.getName() == oButtonModel.getName(): nIndex = formElemIndex logger.debug("nIndex=%d", nIndex) logger.debug("assigning action") oButtonModel.HelpText = sDestination # a trick to pass the parameter sScriptURL = ("vnd.sun.star.script:" "LingToolsBasic.ModuleMain.GoToTableInOtherDoc?" "language=Basic&location=application") aEvent = ScriptEventDescriptor() aEvent.AddListenerParam = "" aEvent.EventMethod = "actionPerformed" aEvent.ListenerType = "XActionListener" aEvent.ScriptCode = sScriptURL aEvent.ScriptType = "Script" oForm.registerScriptEvent(nIndex, aEvent) logger.debug(util.funcName('end'))
def scopeSelection(self): """Search the currently selected text.""" logger.debug(util.funcName('begin')) self.ranger.resetRanges() oSels = self.unoObjs.controller.getSelection() if oSels is None: raise exceptions.RangeError("No text is selected.") if not oSels.supportsService("com.sun.star.text.TextRanges"): # When cells are selected rather than text, # the selection is a TextTableCursor and has no text ranges. raise exceptions.RangeError( "Please do not select individual table cells.") logger.debug("getCount() = %s", oSels.getCount()) if oSels.getCount() == 1: oSel = oSels.getByIndex(0) if oSel.supportsService("com.sun.star.text.TextRange"): cursor = oSel.getText().createTextCursorByRange(oSel) if cursor.isCollapsed(): raise exceptions.RangeError("No text is selected.") for oSel in iteruno.byIndex(oSels): if oSel.supportsService("com.sun.star.text.TextRange"): self.ranger.addRangesForCursor(oSel) logger.debug(util.funcName('end'))