diff --git a/file_templates/CMakeLists.txt b/file_templates/CMakeLists.txt --- a/file_templates/CMakeLists.txt +++ b/file_templates/CMakeLists.txt @@ -3,6 +3,7 @@ classes/private_pointer classes/qt_widget classes/qobject + classes/qobject_pimpl classes/python_basic classes/c_gobject classes/c_gobject_private @@ -32,8 +33,12 @@ common/class_method_declaration_apidox_cpp.txt common/class_property_setter_declaration_cpp.txt common/class_property_setter_declaration_apidox_cpp.txt + common/class_property_setter_definition_cpp.txt common/class_property_getter_declaration_cpp.txt common/class_property_getter_declaration_apidox_cpp.txt + common/class_property_getter_definition_cpp.txt + common/class_property_signal_declaration_cpp.txt + common/class_qproperty_declaration_cpp.txt common/cpp_header.h common/cpp_header_onlyfunctions.h common/cpp_implementation.cpp diff --git a/file_templates/classes/qobject/class.h b/file_templates/classes/qobject/class.h --- a/file_templates/classes/qobject/class.h +++ b/file_templates/classes/qobject/class.h @@ -22,7 +22,7 @@ {% block class_body %} Q_OBJECT {% for property in members %} - Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }}) + {% include "class_qproperty_declaration_cpp.txt" %} {% endfor %} @@ -52,6 +52,13 @@ {% include "class_property_setter_declaration_cpp.txt" %} {% endfor %} + +Q_SIGNALS: + {% for property in members %} + + {% include "class_property_signal_declaration_cpp.txt" %} + + {% endfor %} {% endif %} diff --git a/file_templates/classes/qobject/class.cpp b/file_templates/classes/qobject/class.cpp --- a/file_templates/classes/qobject/class.cpp +++ b/file_templates/classes/qobject/class.cpp @@ -8,11 +8,11 @@ {% include "method_definition_cpp.txt" %} { + {% if method.type %} return {{ method.default_return_value }}; {% endif %} - } {% endwith %} @@ -27,8 +27,10 @@ {% include "method_definition_cpp.txt" %} { + {% if method.type %}return {{ method.default_return_value }}; {% endif %} + } {% endwith %} @@ -39,24 +41,33 @@ {% include "method_definition_cpp.txt" %} { + {% if method.type %}return {{ method.default_return_value }}; {% endif %} + } {% endwith %} {% endfor %} {% for property in members %} -{{ property.type }} {{ name }}::{{ property.name }}() const + +{% include "class_property_getter_definition_cpp.txt" %} { return m_{{ property.name }}; } -void {{ name }}::set{{ property.name|upper_first }}({{ property.type|arg_type }} {{ property.name }}) +{% include "class_property_setter_definition_cpp.txt" %} { + if (m_{{ property.name }} == {{ property.name }}) { + return; + } + + m_{{ property.name }} = {{ property.name }}; + emit {{ property.name }}Changed(m_{{ property.name }}); } {% endfor %} diff --git a/file_templates/classes/qobject/class.h b/file_templates/classes/qobject_pimpl/class.h copy from file_templates/classes/qobject/class.h copy to file_templates/classes/qobject_pimpl/class.h --- a/file_templates/classes/qobject/class.h +++ b/file_templates/classes/qobject_pimpl/class.h @@ -22,7 +22,7 @@ {% block class_body %} Q_OBJECT {% for property in members %} - Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }}) + {% include "class_qproperty_declaration_cpp.txt" %} {% endfor %} @@ -52,6 +52,13 @@ {% include "class_property_setter_declaration_cpp.txt" %} {% endfor %} + +Q_SIGNALS: + {% for property in members %} + + {% include "class_property_signal_declaration_cpp.txt" %} + + {% endfor %} {% endif %} @@ -66,17 +73,7 @@ {% endif %} -{% if private_functions or members %} private: - {% for method in private_functions %} - - {% include "class_method_declaration_apidox_cpp.txt" %} - {% include "class_method_declaration_cpp.txt" %} - - {% endfor %} - - {% for property in members %} - {{property.type}} m_{{property.name}}; - {% endfor %} -{% endif %} + class {{ name }}Private* const d_ptr; + Q_DECLARE_PRIVATE({{ name }}) {% endblock class_body %} diff --git a/file_templates/classes/qobject_pimpl/class.cpp b/file_templates/classes/qobject_pimpl/class.cpp new file mode 100644 --- /dev/null +++ b/file_templates/classes/qobject_pimpl/class.cpp @@ -0,0 +1,112 @@ +{% extends "cpp_implementation.cpp" %} +{% load kdev_filters %} + +{% block includes %} +#include "{{ output_file_header }}" +#include "{{ output_file_privateheader }}" +{% endblock includes %} + + +{% block extra_definitions %} + +{% for method in private_functions %} +{% with name|add:"Private" as name %} + +{% include "method_definition_cpp.txt" %} +{ + + {% if method.type %} + return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% endblock extra_definitions %} + +{% block function_definitions %} + +{% for method in public_functions %} +{% with method.arguments as arguments %} + +{% include "method_definition_cpp.txt" %} +{% if method.isConstructor %} + {% with arguments|first as argFirst %} + {# copy constructor? #} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + : d_ptr(new {{ name }}Private(*{{ argFirst.name }}.d_ptr)) + {% else %} + : d_ptr(new {{ name }}Private()) + {% endif %} + {% endwith %} +{% endif %} +{ + + {% if method.isDestructor %} + delete d_ptr; + {% endif %} + {% if method.type %}return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% for method in protected_functions %} +{% with method.arguments as arguments %} + +{% include "method_definition_cpp.txt" %} +{% if method.isConstructor %} + {% with arguments|first as argFirst %} + {# copy constructor? #} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + : d_ptr(new {{ name }}Private(*{{ argFirst.name }}.d_ptr)) + {% else %} + : d_ptr(new {{ name }}Private()) + {% endif %} + {% endwith %} +{% endif %} +{ + + {% if method.isDestructor %} + delete d_ptr; + {% endif %} + {% if method.type %}return {{ method.default_return_value }}; + {% endif %} + +} + +{% endwith %} +{% endfor %} + +{% for property in members %} + + +{% include "class_property_getter_definition_cpp.txt" %} +{ + Q_D(const {{ name }}); + return d->{{ property.name }}; +} + + +{% include "class_property_setter_definition_cpp.txt" %} +{ + Q_D({{ name }}); + if (d->{{ property.name }} == {{ property.name }}) { + return; + } + + + d->{{ property.name }} = {{ property.name }}; + emit {{ property.name }}Changed(d->{{ property.name }}); +} + +{% endfor %} + +{% endblock function_definitions %} + +{% block bottom %} +{% endblock bottom %} diff --git a/file_templates/classes/qobject_pimpl/class_p.h b/file_templates/classes/qobject_pimpl/class_p.h new file mode 100644 --- /dev/null +++ b/file_templates/classes/qobject_pimpl/class_p.h @@ -0,0 +1,43 @@ +{% extends "cpp_header.h" %} +{% load kdev_filters %} + + +{% block include_guard_open %} +{% with "_P_H" as include_guard_suffix %} +#ifndef {% include "include_guard_cpp.txt" %} +#define {% include "include_guard_cpp.txt" %} +{% endwith %} +{% endblock include_guard_open %} + + +{% block includes %} +{% endblock includes %} + + +{% block class_declaration_open %} +{% include "class_declaration_apidox_cpp.txt" %} +class {{ name }}Private +{ +{% endblock class_declaration_open %} + +{% block class_body %} +{% if members or private_functions %} +public: + {% for method in private_functions %} + + {% include "class_method_declaration_apidox_cpp.txt" %} + {% include "class_method_declaration_cpp.txt" %} + + {% endfor %} + + {% for property in members %} + {{property.type}} {{property.name}}; + {% endfor %} +{% endif %} +{% endblock class_body %} + +{% block include_guard_close %} +{% with "_P_H" as include_guard_suffix %} +#endif // {% include "include_guard_cpp.txt" %} +{% endwith %} +{% endblock include_guard_close %} diff --git a/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop b/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop new file mode 100644 --- /dev/null +++ b/file_templates/classes/qobject_pimpl/qobject_pimpl.desktop @@ -0,0 +1,23 @@ +[General] +Name=QObject pimpl subclass +Comment=QObject subclass with private implementation +Category=C++/Qt +Language=C++ +Type=Class +BaseClasses=public QObject +Files=Header,PrivateHeader,Implementation + +[Header] +Name=Header +File=class.h +OutputFile={{ name }}.h + +[PrivateHeader] +Name=Private Header +File=class_p.h +OutputFile={{ name }}_p.h + +[Implementation] +Name=Implementation +File=class.cpp +OutputFile={{ name }}.cpp diff --git a/file_templates/common/class_method_declaration_apidox_cpp.txt b/file_templates/common/class_method_declaration_apidox_cpp.txt --- a/file_templates/common/class_method_declaration_apidox_cpp.txt +++ b/file_templates/common/class_method_declaration_apidox_cpp.txt @@ -1,9 +1,20 @@ {# Template for api dox in front of class method declaration #} +{% load kdev_filters %} {% with method.arguments as arguments %} {# standard four spaces indentation to match context #} /** {% if method.isConstructor %} - * {% if not arguments %}Default constructor{% else %}Constructor{% endif %} + {% if not arguments %} + * Default constructor + {% else %} + {% with arguments|first as argFirst %} + {% if arguments|length == 1 and argFirst.type == method.name|arg_type %} + * Copy Constructor + {% else %} + * Constructor + {% endif %} + {% endwith %} + {% endif %} {% elif method.isDestructor %} * Destructor {% else %} diff --git a/file_templates/common/class_property_getter_definition_cpp.txt b/file_templates/common/class_property_getter_definition_cpp.txt new file mode 100644 --- /dev/null +++ b/file_templates/common/class_property_getter_definition_cpp.txt @@ -0,0 +1 @@ +{{ property.type }} {{ name }}::{{ property.name }}() const diff --git a/file_templates/common/class_property_setter_definition_cpp.txt b/file_templates/common/class_property_setter_definition_cpp.txt new file mode 100644 --- /dev/null +++ b/file_templates/common/class_property_setter_definition_cpp.txt @@ -0,0 +1,2 @@ +{% load kdev_filters %} +void {{ name }}::set{{ property.name|upper_first }}({{ property.type|arg_type }} {{ property.name }}) diff --git a/file_templates/common/class_property_signal_declaration_cpp.txt b/file_templates/common/class_property_signal_declaration_cpp.txt new file mode 100644 --- /dev/null +++ b/file_templates/common/class_property_signal_declaration_cpp.txt @@ -0,0 +1,4 @@ +{# Template for class property signal method declaration #} +{% load kdev_filters %} + {# standard four spaces indentation to match context #} + void {{ property.name }}Changed({{ property.type|arg_type }} {{ property.name }}); diff --git a/file_templates/common/class_qproperty_declaration_cpp.txt b/file_templates/common/class_qproperty_declaration_cpp.txt new file mode 100644 --- /dev/null +++ b/file_templates/common/class_qproperty_declaration_cpp.txt @@ -0,0 +1,4 @@ +{# Template for class qproperty declaration #} +{% load kdev_filters %} + {# standard four spaces indentation to match context #} + Q_PROPERTY({{ property.type }} {{ property.name }} READ {{ property.name }} WRITE set{{ property.name|upper_first }} NOTIFY {{ property.name }}Changed)