Salome HOME
updated copyright message
[modules/gui.git] / tools / dlgfactory / dlgfactory.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2010-2023  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import sys, os
22
23 __descr_str = ""
24 __descr_str += "This script generates a set of files to initiate a dialog Qt window "
25 __descr_str += "(i.e. MyDialog.ui, MyDialog.h and MyDialog.cxx files). "
26 __descr_str += "The dialog window can be a self-consistent class (i.e. depends only "
27 __descr_str += "on Qt classes) or a class that inherits class GenericDialog "
28 __descr_str += "which implementation is provided in this package and "
29 __descr_str += "which design is defined by the GenericDialog.ui file (editable using "
30 __descr_str += "the Qt designer). "
31 __descr_str += "The -t option let you choose between the two possibilities (specify "
32 __descr_str += "\"-t qdialog\" for the first case, \"-t gdialog\" otherwise)."
33
34 __msg_str = """
35 #
36 # ---------------------------------------------------------
37 # Generation rules to create moc files from QObject headers
38 # and form source files from ui files
39 # ---------------------------------------------------------
40
41 %_moc.cxx: %.h
42         $(MOC) $< -o $@
43
44 ui_%.h: %.ui
45         $(UIC) -o $@ $<
46
47
48 # ---------------------------------------------------------
49 # Declaration of form files generated by UIC and MOC files
50 # as BUILT_SOURCES to be used in the building process.
51 # ---------------------------------------------------------
52 #
53 UIC_FILES = \
54         ui___CLASSNAME__.h
55 #
56 MOC_FILES = \
57         __CLASSNAME___moc.cxx
58
59 BUILT_SOURCES = $(UIC_FILES)
60
61 #
62 # ---------------------------------------------------------
63 # Declaration of sources files to the building process
64 # ---------------------------------------------------------
65 # MOC files and UIC files should be added to the list of undistributed
66 # source files with something like (where <MyLibrary> should be
67 # replaced by the name of the product declared by the directive
68 # lib_LTLIBRARIES):
69 #
70 nodist_<MyLibrary>_la_SOURCES += $(MOC_FILES) $(UIC_FILES)
71
72 dist_<MyLibrary>_la_SOURCES += __CLASSNAME__.cxx
73 salomeinclude_HEADERS       += __CLASSNAME__.h
74
75 <MyLibrary>_la_CPPFLAGS = \\
76         $(QT_CXXFLAGS)
77
78 <MyLibrary>_la_LDFLAGS = \\
79         $(QT_LIBS)
80 """ 
81
82 if __name__ == "__main__":
83   from argparse import ArgumentParser
84
85   tool_path = os.path.dirname( os.path.abspath( sys.argv[0] ) )
86
87   parser = ArgumentParser( description = __descr_str )
88   parser.add_argument( "-n", action="store", default="TestDialog", dest="className", metavar="className",
89                        help="specify the name of the class (default is TestDialog)" )
90   parser.add_argument( "-t", action="store", default="qdialog", dest="classType",
91                        choices=["qdialog", "gdialog"], metavar="classType",
92                      help="specify the type of the class (default is qdialog)" )
93   parser.add_argument( "-v", "--verbose", action="store_true", default=True, dest="verbose",
94                        help="verbose mode" )
95   parser.add_argument( "-s", "--silent", action="store_false", dest="verbose",
96                        help="silent mode" )
97
98   args = parser.parse_args()
99   className = args.className
100   classType = args.classType
101
102   for ext in [".cxx", ".h", ".ui"]:
103     file_dest = className + ext 
104     if classType == "qdialog":
105       file_src = os.path.join( tool_path, "__QDIALOG__" + ext )
106       pass
107     else:
108       file_src = os.path.join( tool_path, "__GDIALOG__" + ext )
109       pass
110      
111     with open(file_src, 'r', encoding='utf-8') as fi, \
112          open(file_dest, 'w', encoding='utf-8') as fo:
113                     for line in fi:
114                       line = line[:-1] 
115                       line = line.replace( "__CLASSNAME__", className )
116                       line = line + "\n"
117                       fo.write(line)
118                       pass
119
120     if args.verbose:
121       print("Note that the following directives should be present in your CMakeLists.txt (or something like that): \n")
122       print(__msg_str.replace( "__CLASSNAME__", className ))
123       pass
124   pass
125