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/.
9 # Takes a LibreOffice .ui file and provides linting tips for maintaining
10 # a consistent look for dialogs
13 import xml
.etree
.ElementTree
as ET
15 DEFAULT_WARNING_STR
= 'Lint assertion failed'
17 POSSIBLE_TOP_LEVEL_WIDGETS
= ['GtkDialog', 'GtkMessageDialog', 'GtkBox', 'GtkFrame', 'GtkGrid']
18 IGNORED_TOP_LEVEL_WIDGETS
= ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer']
20 BUTTON_BOX_SPACING
= '12'
21 ALIGNMENT_TOP_PADDING
= '6'
22 #https://developer.gnome.org/hig-book/3.0/windows-alert.html.en#alert-spacing
23 MESSAGE_BOX_SPACING
= '24'
24 MESSAGE_BORDER_WIDTH
= '12'
26 def lint_assert(predicate
, warning
=DEFAULT_WARNING_STR
):
28 print(" * " + warning
)
30 def check_top_level_widget(element
):
32 widget_type
= element
.attrib
['class']
33 lint_assert(widget_type
in POSSIBLE_TOP_LEVEL_WIDGETS
,
34 "Top level widget should be 'GtkDialog', 'GtkFrame', 'GtkBox', or 'GtkGrid'")
36 # check border_width property
37 border_width_properties
= element
.findall("property[@name='border_width']")
38 if len(border_width_properties
) < 1:
39 lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH
)
40 if len(border_width_properties
) == 1:
41 border_width
= border_width_properties
[0]
42 if widget_type
== "GtkMessageDialog":
43 lint_assert(border_width
.text
== MESSAGE_BORDER_WIDTH
,
44 "Top level 'border_width' property should be " + MESSAGE_BORDER_WIDTH
)
46 lint_assert(border_width
.text
== BORDER_WIDTH
,
47 "Top level 'border_width' property should be " + BORDER_WIDTH
)
49 def check_button_box_spacing(element
):
50 spacing
= element
.findall("property[@name='spacing']")[0]
51 lint_assert(spacing
.text
== BUTTON_BOX_SPACING
,
52 "Button box 'spacing' should be " + BUTTON_BOX_SPACING
)
54 def check_message_box_spacing(element
):
55 spacing
= element
.findall("property[@name='spacing']")[0]
56 lint_assert(spacing
.text
== MESSAGE_BOX_SPACING
,
57 "Button box 'spacing' should be " + MESSAGE_BOX_SPACING
)
59 def check_frames(root
):
60 frames
= [element
for element
in root
.findall('.//object') if element
.attrib
['class'] == 'GtkFrame']
62 frame_alignments
= frame
.findall("./child/object[@class='GtkAlignment']")
63 assert len(frame_alignments
) <= 1
64 if len(frame_alignments
) < 1:
65 lint_assert(False, "No GtkAlignment in GtkFrame with id = '" + frame
.attrib
['id'] + "'")
66 if len(frame_alignments
) == 1:
67 alignment
= frame_alignments
[0]
68 check_alignment_top_padding(alignment
)
70 def check_alignment_top_padding(alignment
):
71 top_padding_properties
= alignment
.findall("./property[@name='top_padding']")
72 assert len(top_padding_properties
) <= 1
73 if len(top_padding_properties
) < 1:
74 lint_assert(False, "No GtkAlignment 'top_padding' set. Should probably be " + ALIGNMENT_TOP_PADDING
)
75 if len(top_padding_properties
) == 1:
76 top_padding
= top_padding_properties
[0]
77 lint_assert(top_padding
.text
== ALIGNMENT_TOP_PADDING
,
78 "GtkAlignment 'top_padding' should be " + ALIGNMENT_TOP_PADDING
)
81 print(" == " + sys
.argv
[1] + " ==")
82 tree
= ET
.parse(sys
.argv
[1])
85 top_level_widgets
= [element
for element
in root
.findall('object') if element
.attrib
['class'] not in IGNORED_TOP_LEVEL_WIDGETS
]
86 assert len(top_level_widgets
) == 1
88 top_level_widget
= top_level_widgets
[0]
89 check_top_level_widget(top_level_widget
)
91 # TODO - only do this if we have a GtkDialog?
92 # check button box spacing
93 button_box
= top_level_widget
.findall("./child/object[@id='dialog-vbox1']")
94 if len(button_box
) > 0:
95 element
= button_box
[0]
96 check_button_box_spacing(element
)
98 message_box
= top_level_widget
.findall("./child/object[@id='messagedialog-vbox']")
99 if len(message_box
) > 0:
100 element
= message_box
[0]
101 check_message_box_spacing(element
)
105 if __name__
== "__main__":