Warning, /graphics/krita/HACKING is written in an unsupported language. File is not indexed.
0001 Since 1999, people have been hacking on Krita. Everyone brought their 0002 own coding style, their own code conventions, their own likes and 0003 dislikes. Me, (Halla that is), I like indents of four spaces, and 0004 no scope prefixes for variables. However, in the interests of 0005 consistency, these are the rules new code should adhere to: 0006 0007 See also https://community.kde.org/Policies/Frameworks_Coding_Style -- 0008 that document is leading. 0009 0010 Qt vs STD vs Boost: 0011 0012 In general, use the Qt classes wherever possible, even if someone tells you 0013 that the STD class is better or whatever. We're dealing with a big project 0014 with lots of developers here and we should keep the code as consistent and 0015 easy to grasp as possible. Since people need to know Qt in the first place, 0016 keep to Qt. Discuss deviations on #krita 0017 0018 C++11 and C++14 0019 0020 Yes, but. 0021 0022 * Prefer normal functions over lambdas, unless really needed (e.g. when replacing 0023 the use of QSignalMapper) 0024 0025 * Try to avoid auto as a shortcut for the type name, except when used 0026 in range-based for-loops and iterators. Using auto as a replacement 0027 for a templated code (or inside templated code) is perfectly fine. 0028 0029 * Generic lambdas. Use generic lambdas only when you want to solve a 0030 problem that would otherwise require templates, not just as a shortcut for 0031 the argument typenames. 0032 0033 * auto as a deduced return type of functions that would otherwise require 0034 templates is perfectly fine 0035 0036 * In some cases it might be okay to use auto as a shortcut for the typename, 0037 e.g. when using very long types returned by boost. But better discuss that 0038 on #krita. 0039 0040 * Avoid the new sig/slot connection syntax _unless_ you are porting all of 0041 Krita to the new syntax. Sure, it has some advantages, but having two different 0042 ways of doing the same thing is begging for trouble and comprehension problems. 0043 0044 * For now, keep using Q_FOREACH, we're using it all over the place, and it has 0045 different constness semantics over the standard range-based for-loop. 0046 0047 * Use nullptr in new code. When changing existing code, make sure that 1) the whole 0048 .cpp/.h files pair uses the same style, 2) the change of the style outweigh the 0049 loss of the git-blame history (that is, your changes do already change the file 0050 significantly) 0051 0052 * Before using other new features, discuss on #krita so we can expand this list. 0053 0054 0055 Indentation 0056 0057 With four spaces. Use the default kdelibs indentation 0058 (http://techbase.kde.org/Policies/Kdelibs_Coding_Style) 0059 0060 Includes 0061 0062 Avoid as much as possible #includes in header files; use forward declarations 0063 of classes. 0064 0065 Initializers 0066 0067 Avoid as much as possible initializers in the body of the constructor. Use 0068 initializer lists instead. Write the initializers as follows 0069 0070 Class(A a, B b) 0071 : Subclass(a) 0072 , m_b(b) 0073 { 0074 } 0075 0076 Note the location of the colon and comma. 0077 0078 It is also preferred to use {}-initialization for class members and 0079 global namespace scope variables, e.g. 0080 0081 class Foo 0082 { 0083 int m_value {0}; 0084 }; 0085 0086 namespace Bar 0087 { 0088 int SomeGlobalValue {0}; 0089 }; 0090 0091 Since Krita has a long history of usage =-initialization everywhere, it is **not** 0092 recommended to use {}-initialization in function bodies, especially, when it results 0093 in a mix of styles in the same file. You can still use that as an exception to solve 0094 some specific problems like the most vexing parse. 0095 0096 Scope prefixes 0097 0098 Use only m_ for class-level variables. No other scope prefixes; no g_, l_, 0099 no 'p' for pointer variables. 0100 0101 Shared pointers 0102 0103 Use shared pointers wherever possible. Prefer Qt's shared pointer classes 0104 to our home-grown shared pointer classes. 0105 0106 Getter/setter 0107 0108 Krita doesn't use Qt's properties -- yet. If you want to introduce use of 0109 properties, convert any and all classes in Krita before committing. 0110 0111 Getter/setters are named 'x() for getters and setX(int x) for setters. If you 0112 come across violations of this rule, change the code. 0113 0114 Class naming 0115 0116 If you use a well-known design pattern, name the class according to the design 0117 pattern. All files should start with 'Kis', all classes with the 'Kis' prefix. 0118 This filename should be the same as the classname: KisNewClass.h, KisNewClass. 0119 0120 Filenames in plugins do not start with Kis; only in libraries. Do not make new 0121 classes that start with Ko. 0122 0123 Function naming 0124 0125 Functions should be named in camelBackedFashion, to conform to Qt's standards. 0126 If you encounter functions in c_style_like_this, feel free to rename. Also: 0127 verbNoun -- i.e., rotateLayer, not layer_rotate. The latter is a true c-ism, 0128 introduced by a language that needs to prefix the 'class' name to every function 0129 in order to have something that's not quite OO. 0130 0131 Variable/Parameter names 0132 0133 Variable/parameter names start with a lower case letter. A name composed of different 0134 words is done in camelBackedStyle. 0135 0136 Try to avoid abbreviations except for the most common cases, like cs for KoColorSpace. 0137 It's okay for variable names to be long and explicit and derived from the type name. 0138 0139 Designer 0140 0141 Krita has started to use designer. All dialogs and all widgets that have a layout 0142 manager must be done in designer. Do not add code or signal/slot connections 0143 in designer. 0144 0145 Enums 0146 0147 All enums should be prefixed with 'enum'. 0148 0149 Namespaces 0150 0151 Currently, we only use anonymous namespaces for things like undo 0152 commands. For the rest, some classes have a 'Kis' prefix, others don't. This should 0153 be made consistent, and we might want to use namespaces to keep all of Krita 0154 inside. 0155 0156 Files and classes 0157 0158 It's preferred (and strongly preferred) to have only one class per .h/.cpp file. 0159 (Which is logical, because otherwise you won't be able to keep to the naming scheme.) 0160 0161 Spaces 0162 0163 Keep the source airy and open. In particular, there should be empty lines between function 0164 declarations and definitions. 0165 0166 Slots and signals 0167 0168 Prefix slots with slot and signals with sig: slotUpdateSelection, sigSelectionUpdated. 0169 0170 Boolean operators 0171 0172 Use the standard !, !=, ==, && etc style, not the "not", "and" etc. style. Keep krita code 0173 using one, easily recognizable, C++ style. 0174 0175 Static initializers 0176 0177 Sometimes we need to declare a static object that performs some actions on Krita 0178 loading, e.g. to register Qt's metatype for a Krita type. We have a special macro 0179 for that: 0180 0181 KIS_DECLARE_STATIC_INITIALIZER { 0182 qRegisterMetaType<KoResourceSP>("KoResourceSP"); 0183 } 0184 0185 What the macro does is basically defining a static variable that is initialized 0186 on loading of the library: 0187 0188 struct KoResourceSPStaticInitializer { 0189 KoResourceSPStaticInitializer() { 0190 qRegisterMetaType<KoResourceSP>("KoResourceSP"); 0191 } 0192 }; 0193 static KoResourceSPStaticInitializer __initializer1; 0194 0195 This macro is usually added to the same .cpp file as the class itself. 0196 0197 WARNING: do not use this pattern in **plugins**. All Krita's plugins are 0198 actually static libraries that are pulled into dynamic .so libraries at the 0199 linking stage. It means that all "unused" objects will never be pulled 0200 into the final plugin. Instead, do all the initialization in the "plugin" 0201 class (the one that is passed to registerPlugin<>() in 0202 K_PLUGIN_FACTORY_WITH_JSON). 0203 0204 The problem can be worked around with `$<LINK_LIBRARY:WHOLE_ARCHIVE,...>` 0205 CMake feature, but it is available only since CMake 3.24, so for now 0206 just avoid using initializers in plugins. 0207 0208 With Krita now supporting Python scripting, we need guidelines for these as well. 0209 These guidelines are preliminary and may be further refined in the future. 0210 0211 To keep it simple, we have chosen to follow the style guide suggested by Python: PEP8. 0212 0213 All rules should be followed, except the max limit of 79 characters per line. As this 0214 can reduce readability in some cases, this rule is optional. 0215 0216 The full PEP8 specification is available here: https://www.python.org/dev/peps/pep-0008/ 0217 0218 To check compliance you can run pep8.py against the code. 0219 You can also use autopep8.py to automatically fix detected compliance issues. 0220 0221 pep8.py can be downloaded via Python's package manager (pip) [https://pypi.python.org/pypi/pep8], 0222 or your distribution's package manager. 0223 autopep8.py can also be downloaded via Python's package manager [https://pypi.python.org/pypi/autopep8], 0224 or your distribution's package manager. 0225 0226 Both of these scripts come bundled with the PyDev plugin, which is available for Eclipse and other IDEs. 0227 The PyDev integration can be configured to visually highlight portions of the code which is not in compliance, 0228 as well as run autopep8 via shortcuts. 0229 0230 pep8.py and autopep8.py can suppress select rules via the "--ignore" command line argument. 0231 To ignore the 79 characters per line rule, pep8.py can be called like this: 0232 0233 pep8.py --ignore=E501 0234 0235 You can read more about the error codes and what they mean here: 0236 http://pep8.readthedocs.io/en/release-1.7.x/intro.html#error-codes