more code cleanup for properties dialog
[debian/gnuradio] / grc / base / Element.py
1 """
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 class Element(object):
21
22         def __init__(self, parent=None):
23                 self._parent = parent
24
25         ##################################################
26         # Element Validation API
27         ##################################################
28         def validate(self):
29                 """
30                 Validate this element and call validate on all children.
31                 Call this base method before adding error messages in the subclass.
32                 """
33                 self._error_messages = list()
34                 for child in self.get_children(): child.validate()
35
36         def is_valid(self):
37                 """
38                 Is this element valid?
39                 @return true when the element is enabled and has no error messages
40                 """
41                 return not self.get_error_messages() or not self.get_enabled()
42
43         def add_error_message(self, msg):
44                 """
45                 Add an error message to the list of errors.
46                 @param msg the error message string
47                 """
48                 self._error_messages.append(msg)
49
50         def get_error_messages(self):
51                 """
52                 Get the list of error messages from this element and all of its children.
53                 Cleverly indent the children error messages for printing purposes.
54                 @return a list of error message strings
55                 """
56                 error_messages = list(self._error_messages) #make a copy
57                 for child in self.get_children():
58                         for msg in child.get_error_messages():
59                                 error_messages.append("%s:\n\t%s"%(child, msg.replace("\n", "\n\t")))
60                 return error_messages
61
62         def rewrite(self):
63                 """
64                 Rewrite this element and call rewrite on all children.
65                 Call this base method before rewriting the element.
66                 """
67                 for child in self.get_children(): child.rewrite()
68
69         def get_enabled(self): return True
70
71         ##############################################
72         ## Tree-like API
73         ##############################################
74         def get_parent(self): return self._parent
75         def get_children(self): return list()
76
77         ##############################################
78         ## Type testing methods
79         ##############################################
80         def is_element(self): return True
81         def is_platform(self): return False
82         def is_flow_graph(self): return False
83         def is_connection(self): return False
84         def is_block(self): return False
85         def is_source(self): return False
86         def is_sink(self): return False
87         def is_port(self): return False
88         def is_param(self): return False