Resolves tdf#138127 - Use document color for border widget
[LibreOffice.git] / bin / lint-ui.py
blob84916229c9a04a771cdb21b43b776ebb3ff5dd0a
1 #!/usr/bin/env python3
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
12 import sys
13 # Force python XML parser not faster C accelerators
14 # because we can't hook the C implementation
15 sys.modules['_elementtree'] = None
16 import xml.etree.ElementTree as ET
17 import re
19 DEFAULT_WARNING_STR = 'Lint assertion failed'
21 POSSIBLE_TOP_LEVEL_WIDGETS = ['GtkDialog', 'GtkMessageDialog', 'GtkBox', 'GtkFrame', 'GtkGrid',
22 'GtkAssistant', 'GtkToolbar', 'GtkNotebook', 'GtkPopover', 'GtkWindow', 'GtkPaned', 'GtkScrolledWindow']
23 IGNORED_TOP_LEVEL_WIDGETS = ['GtkAdjustment', 'GtkImage', 'GtkListStore', 'GtkSizeGroup', 'GtkMenu', 'GtkTextBuffer', 'GtkTreeStore']
24 BORDER_WIDTH = '6'
25 BUTTON_BOX_SPACING = '12'
26 ALIGNMENT_TOP_PADDING = '6'
27 #https://developer.gnome.org/hig-book/3.0/windows-alert.html.en#alert-spacing
28 MESSAGE_BOX_SPACING = '24'
29 MESSAGE_BORDER_WIDTH = '12'
31 IGNORED_WORDS = ['the', 'of', 'to', 'for', 'a', 'and', 'as', 'from', 'on', 'into', 'by', 'at', 'or', 'do', 'in', 'when', 'no']
33 # Hook the XML parser and add line number attributes
34 class LineNumberingParser(ET.XMLParser):
35 def _start(self, *args, **kwargs):
36 # Here we assume the default XML parser which is expat
37 # and copy its element position attributes into output Elements
38 element = super(self.__class__, self)._start(*args, **kwargs)
39 element._start_line_number = self.parser.CurrentLineNumber
40 element._start_column_number = self.parser.CurrentColumnNumber
41 element._start_byte_index = self.parser.CurrentByteIndex
42 return element
44 def _end(self, *args, **kwargs):
45 element = super(self.__class__, self)._end(*args, **kwargs)
46 element._end_line_number = self.parser.CurrentLineNumber
47 element._end_column_number = self.parser.CurrentColumnNumber
48 element._end_byte_index = self.parser.CurrentByteIndex
49 return element
52 def lint_assert(predicate, warning=DEFAULT_WARNING_STR, node=None):
53 if not predicate:
54 if not(node is None):
55 print(sys.argv[1] + ":" + str(node._start_line_number) + ": " + warning)
56 else:
57 print(sys.argv[1] + ": " + warning)
59 def check_top_level_widget(element):
60 # check widget type
61 widget_type = element.attrib['class']
62 if not(widget_type in POSSIBLE_TOP_LEVEL_WIDGETS):
63 return
65 # check border_width property
66 border_width_properties = element.findall("property[@name='border_width']")
67 # TODO reenable when we are ready to fix
68 #if len(border_width_properties) < 1:
69 # lint_assert(False, "No border_width set on top level widget. Should probably be " + BORDER_WIDTH)
70 #if len(border_width_properties) == 1:
71 # border_width = border_width_properties[0]
72 # if widget_type == "GtkMessageDialog":
73 # lint_assert(border_width.text == MESSAGE_BORDER_WIDTH,
74 # "Top level 'border_width' property should be " + MESSAGE_BORDER_WIDTH, border_width)
75 # else:
76 # lint_assert(border_width.text == BORDER_WIDTH,
77 # "Top level 'border_width' property should be " + BORDER_WIDTH, border_width)
79 # check that any widget which has 'has-default' also has 'can-default'
80 for widget in element.findall('.//object'):
81 if not widget.attrib['class']:
82 continue
83 widget_type = widget.attrib['class']
84 has_defaults = widget.findall("./property[@name='has_default']")
85 if len(has_defaults) > 0 and has_defaults[0].text == "True":
86 can_defaults = widget.findall("./property[@name='can_default']")
87 lint_assert(len(can_defaults)>0 and can_defaults[0].text == "True",
88 "has_default without can_default in " + widget_type + " with id = '" + widget.attrib['id'] + "'", widget)
90 def check_button_box_spacing(element):
91 spacing = element.findall("property[@name='spacing']")
92 lint_assert(len(spacing) > 0 and spacing[0].text == BUTTON_BOX_SPACING,
93 "Button box 'spacing' should be " + BUTTON_BOX_SPACING,
94 element)
96 def check_message_box_spacing(element):
97 spacing = element.findall("property[@name='spacing']")
98 lint_assert(len(spacing) > 0 and spacing[0].text == MESSAGE_BOX_SPACING,
99 "Message box 'spacing' should be " + MESSAGE_BOX_SPACING,
100 element)
102 def check_radio_buttons(root):
103 radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkRadioButton']
104 for radio in radios:
105 radio_underlines = radio.findall("./property[@name='use_underline']")
106 assert len(radio_underlines) <= 1
107 if len(radio_underlines) < 1:
108 lint_assert(False, "No use_underline in GtkRadioButton with id = '" + radio.attrib['id'] + "'", radio)
110 def check_adjustments(root):
111 adjustments = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkAdjustment']
112 for adjustment in adjustments:
113 uppers = adjustment.findall("./property[@name='upper']")
114 assert len(uppers) <= 1
115 if len(uppers) < 1:
116 lint_assert(False, "No upper in GtkAdjustment with id = '" + adjustment.attrib['id'] + "'", adjustment)
118 def check_menu_buttons(root):
119 buttons = [element for element in root.findall('.//object') if element.attrib['class'] == "GtkMenuButton"]
120 for button in buttons:
121 labels = button.findall("./property[@name='label']")
122 images = button.findall("./property[@name='image']")
123 assert(len(labels) <= 1)
124 if len(labels) < 1 and len(images) < 1:
125 if sys.argv[1] == "vcl/uiconfig/ui/combobox.ui": and button.attrib['id'] == "overlaybutton":
126 pass
127 else:
128 lint_assert(False, "No label in GtkMenuButton with id = '" + button.attrib['id'] + "'", button)
130 def check_check_buttons(root):
131 radios = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkCheckButton']
132 for radio in radios:
133 radio_underlines = radio.findall("./property[@name='use_underline']")
134 assert len(radio_underlines) <= 1
135 if len(radio_underlines) < 1:
136 lint_assert(False, "No use_underline in GtkCheckButton with id = '" + radio.attrib['id'] + "'", radio)
138 def check_frames(root):
139 frames = [element for element in root.findall('.//object') if element.attrib['class'] == 'GtkFrame']
140 for frame in frames:
141 frame_alignments = frame.findall("./child/object[@class='GtkAlignment']")
142 assert len(frame_alignments) <= 1
143 if len(frame_alignments) < 1:
144 lint_assert(False, "No GtkAlignment in GtkFrame with id = '" + frame.attrib['id'] + "'", frame)
145 if len(frame_alignments) == 1:
146 alignment = frame_alignments[0]
147 check_alignment_top_padding(alignment)
149 def check_alignment_top_padding(alignment):
150 top_padding_properties = alignment.findall("./property[@name='top_padding']")
151 assert len(top_padding_properties) <= 1
152 # TODO reenable when we are ready to fix
153 # if len(top_padding_properties) < 1:
154 # lint_assert(False, "No GtkAlignment 'top_padding' set. Should probably be " + ALIGNMENT_TOP_PADDING, alignment)
155 #if len(top_padding_properties) == 1:
156 # top_padding = top_padding_properties[0]
157 # lint_assert(top_padding.text == ALIGNMENT_TOP_PADDING,
158 # "GtkAlignment 'top_padding' should be " + ALIGNMENT_TOP_PADDING, alignment)
160 def check_title_labels(root):
161 labels = root.findall(".//child[@type='label']")
162 for label in labels:
163 title = label.find(".//property[@name='label']")
164 if title is None:
165 continue
166 words = re.split(r'[^a-zA-Z0-9:_-]', title.text)
167 first = True
168 for word in words:
169 if len(word) and word[0].islower() and (word not in IGNORED_WORDS) and not first:
170 context = title.attrib['context']
171 # exclude a couple of whole sentences
172 if sys.argv[1] == "cui/uiconfig/ui/optpathspage.ui" and context == "optpathspage|label1":
173 pass
174 elif sys.argv[1] == "dbaccess/uiconfig/ui/password.ui" and context == "password|label1":
175 pass
176 elif sys.argv[1] == "sc/uiconfig/scalc/ui/datastreams.ui" and context == "datastreams|label4":
177 pass
178 elif sys.argv[1] == "sc/uiconfig/scalc/ui/scgeneralpage.ui" and context == "scgeneralpage|label6":
179 pass
180 elif sys.argv[1] == "sfx2/uiconfig/ui/documentfontspage.ui" and context == "documentfontspage|fontScriptFrameLabel":
181 pass
182 elif sys.argv[1] == "sw/uiconfig/swriter/ui/testmailsettings.ui" and context == "testmailsettings|label8":
183 pass
184 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcomparison.ui" and context == "optcomparison|setting":
185 pass
186 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcompatpage.ui" and context == "optcompatpage|label11":
187 pass
188 elif sys.argv[1] == "sw/uiconfig/swriter/ui/optcaptionpage.ui" and context == "optcaptionpage|label1":
189 pass
190 elif sys.argv[1] == "sw/uiconfig/swriter/ui/mmresultemaildialog.ui" and context == "mmresultemaildialog|attachft":
191 pass
192 elif sys.argv[1] == "sw/uiconfig/swriter/ui/mailmerge.ui" and context == "mailmerge|singledocument":
193 pass
194 elif sys.argv[1] == "cui/uiconfig/ui/acorexceptpage.ui" and context == "acorexceptpage|label2":
195 pass
196 elif sys.argv[1] == "dbaccess/uiconfig/ui/dbwizmysqlintropage.ui" and context == "dbwizmysqlintropage|label1":
197 pass
198 else:
199 lint_assert(False, "The word '" + word + "' should be capitalized", label)
200 first = False
202 def main():
203 tree = ET.parse(sys.argv[1], parser=LineNumberingParser())
204 root = tree.getroot()
206 if sys.argv[1] != "libreofficekit/qa/gtktiledviewer/gtv.ui":
207 lint_assert('domain' in root.attrib, "interface needs to specify translation domain")
209 top_level_widgets = [element for element in root.findall('object') if element.attrib['class'] not in IGNORED_TOP_LEVEL_WIDGETS]
210 # eg. one file contains only a Menu, which we don't check
211 if len(top_level_widgets) == 0:
212 return
214 for top_level_widget in top_level_widgets:
215 check_top_level_widget(top_level_widget)
217 # TODO - only do this if we have a GtkDialog?
218 # check button box spacing
219 button_box = top_level_widget.findall("./child/object[@id='dialog-vbox1']")
220 if len(button_box) > 0:
221 element = button_box[0]
222 # TODO reenable when we are ready to fix
223 #check_button_box_spacing(element)
225 message_box = top_level_widget.findall("./child/object[@id='messagedialog-vbox']")
226 if len(message_box) > 0:
227 element = message_box[0]
228 # TODO reenable when we are ready to fix
229 #check_message_box_spacing(element)
231 check_frames(root)
233 # TODO reenable when we are ready to fix
234 #check_radio_buttons(root)
236 check_menu_buttons(root)
238 # TODO reenable when we are ready to fix
239 #check_check_buttons(root)
241 check_title_labels(root)
243 if __name__ == "__main__":
244 main()