tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / Text / HardFormatting.py
blob388947ff1efc44986adf06df9fbbd041805b09f1
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import officehelper
11 import sys
12 import traceback
14 from com.sun.star.awt.FontWeight import BOLD
15 from com.sun.star.beans.PropertyState import AMBIGUOUS_VALUE
16 from com.sun.star.beans.PropertyState import DEFAULT_VALUE
17 from com.sun.star.beans.PropertyState import DIRECT_VALUE
18 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
21 def get_desktop():
22 desktop = None
23 try:
24 remote_context = officehelper.bootstrap()
25 srv_mgr = remote_context.getServiceManager()
26 if srv_mgr is None:
27 print("Can't create a desktop. No connection, no remote office servicemanager available!")
28 else:
29 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
30 except Exception:
31 traceback.print_exc()
32 sys.exit(1)
33 return desktop
36 def main():
37 desktop = get_desktop()
38 if desktop is None:
39 return
41 try:
42 doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, tuple())
44 text = doc.getText()
45 cursor = text.createTextCursor()
47 text.insertString(cursor, "Headline", False)
48 text.insertControlCharacter(cursor, PARAGRAPH_BREAK, False)
49 text.insertString(cursor, "A very short paragraph for illustration only", False)
51 # Start 'Hard formatting'
52 # the text range not the cursor contains the 'parastyle' property
53 text_range = text.getEnd()
54 text_range.gotoStart(False)
55 text_range.gotoEndOfParagraph(True)
57 # Later, we will go through words in this text range
58 text_range = text_range.getText().getStart()
59 # Display the current text attributes
60 print("Parastyle:", text_range.getPropertyValue("ParaStyleName"))
61 print("Fontname: ", text_range.getPropertyValue("CharFontName"))
62 print("Weight: ", text_range.getPropertyValue("CharWeight"))
64 # Move around
65 text_range.gotoNextWord(False)
66 text_range.gotoNextWord(False)
67 text_range.gotoEndOfWord(True)
68 # And set text attributes
69 text_range.setPropertyValue("CharWeight", BOLD)
70 text_range.setPropertyValue("CharColor", 255)
71 # Then, display the text attributes
72 print("Parastyle:", text_range.getPropertyValue("ParaStyleName"))
73 print("Fontname: ", text_range.getPropertyValue("CharFontName"))
74 print("Weight: ", text_range.getPropertyValue("CharWeight"))
76 # the PropertyState contains information where the attribute is set,
77 # is a text part hard formatted or not.
78 check_property_state(text_range, text_range.getPropertyState("CharWeight"))
80 print("Increase the selection with three characters")
81 text_range.goRight(3, True)
82 check_property_state(text_range, text_range.getPropertyState("CharWeight"))
84 print("Set the default value on the selection")
85 text_range.setPropertyToDefault("CharWeight")
86 # Then, check again
87 check_property_state(text_range, text_range.getPropertyState("CharWeight"))
88 except Exception:
89 traceback.print_exc()
90 sys.exit(1)
92 print("Done")
95 def check_property_state(text_range, prop_state):
96 if prop_state == DIRECT_VALUE:
97 print("-> The selection", f"'{text_range.getString()}'", "completely hard formatted")
98 elif prop_state == DEFAULT_VALUE:
99 print("-> The selection", f"'{text_range.getString()}'", "isn't hard formatted")
100 elif prop_state == AMBIGUOUS_VALUE:
101 print("-> The selection", f"'{text_range.getString()}'", "isn't completely hard formatted")
102 else:
103 print("No PropertyState found")
106 if __name__ == "__main__":
107 main()
109 # vim: set shiftwidth=4 softtabstop=4 expandtab: