tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / scripting / examples / python / Capitalise.py
blob0590ce8246c8fde991445dc4bf45a0e2dcfc2952
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 def getNewString(theString):
21 """helper function"""
22 if not theString:
23 return ""
25 # should we tokenize on "."?
26 if len(theString) >= 2 and theString[:2].isupper():
27 # first two chars are UC => first UC, rest LC
28 newString = theString[0].upper() + theString[1:].lower()
30 elif theString[0].isupper():
31 # first char UC => all to LC
32 newString = theString.lower()
34 else:
35 # all to UC.
36 newString = theString.upper()
38 return newString
41 def capitalisePython():
42 """Change the case of the selected or current word(s).
43 If at least the first two characters are "UPpercase, then it is changed
44 to first char "Uppercase".
45 If the first character is "Uppercase", then it is changed to
46 all "lowercase".
47 Otherwise, all are changed to "UPPERCASE".
48 """
49 # The context variable is of type XScriptContext and is available to
50 # all Python scripts executed by the Script Framework
51 # ask linters to therefore ignore it:
52 # # noqa: F821
53 xModel = XSCRIPTCONTEXT.getDocument() # NOQA
54 # the writer controller impl supports the css.view.XSelectionSupplier
55 # interface
56 xSelectionSupplier = xModel.getCurrentController()
58 # see section 7.5.1 of developers' guide
59 xIndexAccess = xSelectionSupplier.getSelection()
60 count = xIndexAccess.getCount()
62 if count >= 1: # ie we have a selection
63 i = 0
65 while i < count:
66 xTextRange = xIndexAccess.getByIndex(i)
67 theString = xTextRange.getString()
68 # print("theString")
69 if len(theString) == 0:
70 # sadly we can have a selection where nothing is selected
71 # in this case we get the XWordCursor and make a selection!
72 xText = xTextRange.getText()
73 xWordCursor = xText.createTextCursorByRange(xTextRange)
75 if not xWordCursor.isStartOfWord():
76 xWordCursor.gotoStartOfWord(False)
78 xWordCursor.gotoNextWord(True)
79 theString = xWordCursor.getString()
80 newString = getNewString(theString)
82 if newString:
83 xWordCursor.setString(newString)
84 xSelectionSupplier.select(xWordCursor)
85 else:
86 newString = getNewString(theString)
87 if newString:
88 xTextRange.setString(newString)
89 xSelectionSupplier.select(xTextRange)
90 i += 1
93 # lists the scripts, that shall be visible inside OOo. Can be omitted, if
94 # all functions shall be visible, however here getNewString shall be suppressed
95 g_exportedScripts = (capitalisePython,)
97 # vim: set shiftwidth=4 softtabstop=4 expandtab: