| 1 |
Since 1999, people have been hacking on Krita. Everyone brought their |
| 2 |
own coding style, their own code conventions, their own likes and |
| 3 |
dislikes. Me, (Halla that is), I like indents of four spaces, and |
| 4 |
no scope prefixes for variables. However, in the interests of |
| 5 |
consistency, these are the rules new code should adhere to: |
| 6 |
|
| 7 |
See also https://community.kde.org/Policies/Frameworks_Coding_Style -- |
| 8 |
that document is leading. |
| 9 |
|
| 10 |
Qt vs STD vs Boost: |
| 11 |
|
| 12 |
In general, use the Qt classes wherever possible, even if someone tells you |
| 13 |
that the STD class is better or whatever. We're dealing with a big project |
| 14 |
with lots of developers here and we should keep the code as consistent and |
| 15 |
easy to grasp as possible. Since people need to know Qt in the first place, |
| 16 |
keep to Qt. Discuss deviations on #krita |
| 17 |
|
| 18 |
C++11 and C++14 |
| 19 |
|
| 20 |
Yes, but. |
| 21 |
|
| 22 |
* Prefer normal functions over lambdas, unless really needed (e.g. when replacing |
| 23 |
the use of QSignalMapper) |
| 24 |
|
| 25 |
* Try to avoid auto as a shortcut for the type name, except when used |
| 26 |
in range-based for-loops and iterators. Using auto as a replacement |
| 27 |
for a templated code (or inside templated code) is perfectly fine. |
| 28 |
|
| 29 |
* Generic lambdas. Use generic lambdas only when you want to solve a |
| 30 |
problem that would otherwise require templates, not just as a shortcut for |
| 31 |
the argument typenames. |
| 32 |
|
| 33 |
* auto as a deduced return type of functions that would otherwise require |
| 34 |
templates is perfectly fine |
| 35 |
|
| 36 |
* In some cases it might be okay to use auto as a shortcut for the typename, |
| 37 |
e.g. when using very long types returned by boost. But better discuss that |
| 38 |
on #krita. |
| 39 |
|
| 40 |
* Avoid the new sig/slot connection syntax _unless_ you are porting all of |
| 41 |
Krita to the new syntax. Sure, it has some advantages, but having two different |
| 42 |
ways of doing the same thing is begging for trouble and comprehension problems. |
| 43 |
|
| 44 |
* For now, keep using Q_FOREACH, we're using it all over the place, and it has |
| 45 |
different constness semantics over the standard range-based for-loop. |
| 46 |
|
| 47 |
* Use nullptr in new code. When changing existing code, make sure that 1) the whole |
| 48 |
.cpp/.h files pair uses the same style, 2) the change of the style outweigh the |
| 49 |
loss of the git-blame history (that is, your changes do already change the file |
| 50 |
significantly) |
| 51 |
|
| 52 |
* Before using other new features, discuss on #krita so we can expand this list. |
| 53 |
|
| 54 |
|
| 55 |
Indentation |
| 56 |
|
| 57 |
With four spaces. Use the default kdelibs indentation |
| 58 |
(http://techbase.kde.org/Policies/Kdelibs_Coding_Style) |
| 59 |
|
| 60 |
Indentation of Lambdas |
| 61 |
|
| 62 |
For indentation of lambdas refer to the `LambdaBodyIndentation: Signature` method used |
| 63 |
by clang-format, i.e. "align lambda body relative to the lambda signature" |
| 64 |
|
| 65 |
someMethod( |
| 66 |
[](SomeReallyLongLambdaSignatureArgument foo) { |
| 67 |
return; |
| 68 |
}); |
| 69 |
|
| 70 |
You can use automated "Format Selection" actions of QtCreator and VSCode to format |
| 71 |
lambdas. They will user the necessary settings from .clang-format file present in |
| 72 |
the Krita's source tree. |
| 73 |
|
| 74 |
Includes |
| 75 |
|
| 76 |
Avoid as much as possible #includes in header files; use forward declarations |
| 77 |
of classes. |
| 78 |
|
| 79 |
Initializers |
| 80 |
|
| 81 |
Avoid as much as possible initializers in the body of the constructor. Use |
| 82 |
initializer lists instead. Write the initializers as follows |
| 83 |
|
| 84 |
Class(A a, B b) |
| 85 |
: Subclass(a) |
| 86 |
, m_b(b) |
| 87 |
{ |
| 88 |
} |
| 89 |
|
| 90 |
Note the location of the colon and comma. |
| 91 |
|
| 92 |
It is also preferred to use {}-initialization for class members and |
| 93 |
global namespace scope variables, e.g. |
| 94 |
|
| 95 |
class Foo |
| 96 |
{ |
| 97 |
int m_value {0}; |
| 98 |
}; |
| 99 |
|
| 100 |
namespace Bar |
| 101 |
{ |
| 102 |
int SomeGlobalValue {0}; |
| 103 |
}; |
| 104 |
|
| 105 |
Since Krita has a long history of usage =-initialization everywhere, it is **not** |
| 106 |
recommended to use {}-initialization in function bodies, especially, when it results |
| 107 |
in a mix of styles in the same file. You can still use that as an exception to solve |
| 108 |
some specific problems like the most vexing parse. |
| 109 |
|
| 110 |
Scope prefixes |
| 111 |
|
| 112 |
Use only m_ for class-level variables. No other scope prefixes; no g_, l_, |
| 113 |
no 'p' for pointer variables. |
| 114 |
|
| 115 |
Shared pointers |
| 116 |
|
| 117 |
Use shared pointers wherever possible. Prefer Qt's shared pointer classes |
| 118 |
to our home-grown shared pointer classes. |
| 119 |
|
| 120 |
QScopedPointer is being deprecated by Qt |
| 121 |
|
| 122 |
We use QScopedPointer **only** for a d-ptr pattern in simple classes not |
| 123 |
supporting move-semantics. For all other usages, use std::unique_ptr. |
| 124 |
|
| 125 |
Getter/setter |
| 126 |
|
| 127 |
Krita doesn't use Qt's properties -- yet. If you want to introduce use of |
| 128 |
properties, convert any and all classes in Krita before committing. |
| 129 |
|
| 130 |
Getter/setters are named 'x() for getters and setX(int x) for setters. If you |
| 131 |
come across violations of this rule, change the code. |
| 132 |
|
| 133 |
Class naming |
| 134 |
|
| 135 |
If you use a well-known design pattern, name the class according to the design |
| 136 |
pattern. All files should start with 'Kis', all classes with the 'Kis' prefix. |
| 137 |
This filename should be the same as the classname: KisNewClass.h, KisNewClass. |
| 138 |
|
| 139 |
Filenames in plugins do not start with Kis; only in libraries. Do not make new |
| 140 |
classes that start with Ko. |
| 141 |
|
| 142 |
Function naming |
| 143 |
|
| 144 |
Functions should be named in camelBackedFashion, to conform to Qt's standards. |
| 145 |
If you encounter functions in c_style_like_this, feel free to rename. Also: |
| 146 |
verbNoun -- i.e., rotateLayer, not layer_rotate. The latter is a true c-ism, |
| 147 |
introduced by a language that needs to prefix the 'class' name to every function |
| 148 |
in order to have something that's not quite OO. |
| 149 |
|
| 150 |
Variable/Parameter names |
| 151 |
|
| 152 |
Variable/parameter names start with a lower case letter. A name composed of different |
| 153 |
words is done in camelBackedStyle. |
| 154 |
|
| 155 |
Try to avoid abbreviations except for the most common cases, like cs for KoColorSpace. |
| 156 |
It's okay for variable names to be long and explicit and derived from the type name. |
| 157 |
|
| 158 |
Designer |
| 159 |
|
| 160 |
Krita has started to use designer. All dialogs and all widgets that have a layout |
| 161 |
manager must be done in designer. Do not add code or signal/slot connections |
| 162 |
in designer. |
| 163 |
|
| 164 |
Enums |
| 165 |
|
| 166 |
All enums should be prefixed with 'enum'. |
| 167 |
|
| 168 |
Namespaces |
| 169 |
|
| 170 |
Currently, we only use anonymous namespaces for things like undo |
| 171 |
commands. For the rest, some classes have a 'Kis' prefix, others don't. This should |
| 172 |
be made consistent, and we might want to use namespaces to keep all of Krita |
| 173 |
inside. |
| 174 |
|
| 175 |
Files and classes |
| 176 |
|
| 177 |
It's preferred (and strongly preferred) to have only one class per .h/.cpp file. |
| 178 |
(Which is logical, because otherwise you won't be able to keep to the naming scheme.) |
| 179 |
|
| 180 |
Spaces |
| 181 |
|
| 182 |
Keep the source airy and open. In particular, there should be empty lines between function |
| 183 |
declarations and definitions. |
| 184 |
|
| 185 |
Slots and signals |
| 186 |
|
| 187 |
Prefix slots with slot and signals with sig: slotUpdateSelection, sigSelectionUpdated. |
| 188 |
|
| 189 |
Boolean operators |
| 190 |
|
| 191 |
Use the standard !, !=, ==, && etc style, not the "not", "and" etc. style. Keep krita code |
| 192 |
using one, easily recognizable, C++ style. |
| 193 |
|
| 194 |
Conversion of QScopedPointer and QSharedPointer to booleans |
| 195 |
|
| 196 |
With the update to Qt6 we cannot longer use implicit conversions of smart pointers |
| 197 |
to booleans. Instead we should use convert the result to boolean explicitly: |
| 198 |
|
| 199 |
Qt5 way: |
| 200 |
|
| 201 |
QSharedPointer<KisPaintOpPreset> preset = <...>; |
| 202 |
const bool hasPreset = preset; // does not work anymore! |
| 203 |
|
| 204 |
Qt6 way: |
| 205 |
|
| 206 |
QSharedPointer<KisPaintOpPreset> preset = <...>; |
| 207 |
const bool hasPreset = bool(preset); // convert to bool explicitly! |
| 208 |
|
| 209 |
Please try to avoid constructions involving `!preset.isNull()`, they |
| 210 |
are hard to read. |
| 211 |
|
| 212 |
Static initializers |
| 213 |
|
| 214 |
Sometimes we need to declare a static object that performs some actions on Krita |
| 215 |
loading, e.g. to register Qt's metatype for a Krita type. We have a special macro |
| 216 |
for that: |
| 217 |
|
| 218 |
KIS_DECLARE_STATIC_INITIALIZER { |
| 219 |
qRegisterMetaType<KoResourceSP>("KoResourceSP"); |
| 220 |
} |
| 221 |
|
| 222 |
What the macro does is basically defining a static variable that is initialized |
| 223 |
on loading of the library: |
| 224 |
|
| 225 |
struct KoResourceSPStaticInitializer { |
| 226 |
KoResourceSPStaticInitializer() { |
| 227 |
qRegisterMetaType<KoResourceSP>("KoResourceSP"); |
| 228 |
} |
| 229 |
}; |
| 230 |
static KoResourceSPStaticInitializer __initializer1; |
| 231 |
|
| 232 |
This macro is usually added to the same .cpp file as the class itself. |
| 233 |
|
| 234 |
WARNING: do not use this pattern in **plugins**. All Krita's plugins are |
| 235 |
actually static libraries that are pulled into dynamic .so libraries at the |
| 236 |
linking stage. It means that all "unused" objects will never be pulled |
| 237 |
into the final plugin. Instead, do all the initialization in the "plugin" |
| 238 |
class (the one that is passed to registerPlugin<>() in |
| 239 |
K_PLUGIN_FACTORY_WITH_JSON). |
| 240 |
|
| 241 |
The problem can be worked around with `$<LINK_LIBRARY:WHOLE_ARCHIVE,...>` |
| 242 |
CMake feature, but it is available only since CMake 3.24, so for now |
| 243 |
just avoid using initializers in plugins. |
| 244 |
|
| 245 |
The usage of timers, compressors and QTimer::singleShot() |
| 246 |
|
| 247 |
We should be carefult about allocation of timers in Krita. The number of precision |
| 248 |
timers is limited on some platforms (e.g. Windows). When Qt runs out of precision |
| 249 |
timers it silently degrades all newly created timers into non-precision ones. That |
| 250 |
can affect timers that are used for the canvas framerate stabilization and cause |
| 251 |
tearing and FPS drops. |
| 252 |
|
| 253 |
Rules of thumb: |
| 254 |
|
| 255 |
1) When using QTimer::singleShot(), either use **null** timeout to just push the |
| 256 |
event through the queue, or pass Qt::CoarseTimer as the second argument: |
| 257 |
|
| 258 |
QTimer::singleShot(0, ...); |
| 259 |
|
| 260 |
or |
| 261 |
|
| 262 |
QTimer::singleShot(<some non-zero value>, Qt::CoarseTimer, ...); |
| 263 |
|
| 264 |
If you really-really-really need a precise single-shot event, then pass |
| 265 |
Qt::PreciseTimer explicitly. |
| 266 |
|
| 267 |
The problem is that QTimer::singleShot() creates a precise timer by default, |
| 268 |
which can consume all the available precise timers in the system for the app. |
| 269 |
|
| 270 |
2) When creating timers or using KisSignalCompressor, set timeout to values |
| 271 |
strictly higher than 20 ms (unless you really know what you are doing). Value |
| 272 |
`25 ms` is a good default to compress updates for the lightweight widgets, |
| 273 |
and value `100 ms` is a good default to compress updates for heavy widgets. |
| 274 |
|
| 275 |
The problem is that Qt internally converts all the timers into precise |
| 276 |
ones, if their timeout is less or equal 20. Which can also consume all |
| 277 |
the available precise timers. |
| 278 |
|
| 279 |
Class template argument deduction (CTAD) (since C++17) |
| 280 |
|
| 281 |
C++17 allows automatical deduction of class template arguments for the class |
| 282 |
constructors. I.e. you can write |
| 283 |
|
| 284 |
QList list({QPointF(), QPointF(), QPointF()}); |
| 285 |
|
| 286 |
and template arguments for the QList template will be deduced automatically. |
| 287 |
|
| 288 |
You can use this feature in new code, given the style of the whole file is |
| 289 |
consistent. |
| 290 |
|
| 291 |
WARNING for Qt6 and QVector: |
| 292 |
|
| 293 |
Do **NOT** use CTAD for QVector until we switch to C++20! In Qt6 QVector is |
| 294 |
declared as an alias for QList, and CTAD for aliases is a C++20 feature! |
| 295 |
|
| 296 |
The code **will** compile with GCC on Linux even with `--std=c++17`, but |
| 297 |
will fail to compile with Clang on Windows, Android and MacOS. |
| 298 |
|
| 299 |
The usage of custom patches in Qt |
| 300 |
|
| 301 |
In Krita we use a lot of custom patches on Qt. An important part of this works |
| 302 |
is to make sure Krita still works with unpatches Qt, so that Linux distributions |
| 303 |
could package it. |
| 304 |
|
| 305 |
To make Qt patches optional and maintainable, please use the follwoing guideline: |
| 306 |
|
| 307 |
1) Detect Qt patches in main CMakeLists.txt file using `check_cxx_source_compiles` |
| 308 |
or `check_cxx_symbol_exists`: |
| 309 |
|
| 310 |
# modify includes and libraries if necessary |
| 311 |
set(CMAKE_REQUIRED_INCLUDES ${Qt6Gui_INCLUDE_DIRS} ${Qt6Gui_PRIVATE_INCLUDE_DIRS}) |
| 312 |
set(CMAKE_REQUIRED_LIBRARIES ${Qt6Gui_LIBRARIES}) |
| 313 |
|
| 314 |
CHECK_CXX_SOURCE_COMPILES(" |
| 315 |
#include <QtGui/rhi/qrhi.h> |
| 316 |
int main(int, char *[]) { |
| 317 |
bool (QRhi::*x)() const; |
| 318 |
x = &QRhi::isLastFrameCompletedOnGPU; |
| 319 |
Q_UNUSED(x); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
" |
| 323 |
KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH |
| 324 |
) |
| 325 |
|
| 326 |
# reset modified environment |
| 327 |
unset(CMAKE_REQUIRED_INCLUDES) |
| 328 |
unset(CMAKE_REQUIRED_LIBRARIES) |
| 329 |
|
| 330 |
2) [optional] If the patch is important enough, or if the developer might want |
| 331 |
to disable it for some reason, add it to the list of features: |
| 332 |
|
| 333 |
option(KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH |
| 334 |
"Qt has a custom patch for limiting FPS to the screen framerate" |
| 335 |
ON) |
| 336 |
add_feature_info("Qt: Compressed updates patch" |
| 337 |
KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH |
| 338 |
"Krita will compress the screen updates to the maximum FPS of available displays") |
| 339 |
|
| 340 |
3) Add the definition for the detection variable into `config-qt-patches-present.h.cmake` |
| 341 |
|
| 342 |
#cmakedefine01 KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH |
| 343 |
|
| 344 |
4) At the point of usage just include the auto-generated include and check for the |
| 345 |
feature to exist |
| 346 |
|
| 347 |
#include <config-qt-patches-present.h> |
| 348 |
|
| 349 |
#if KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH |
| 350 |
// do your work |
| 351 |
#endif /* KRITA_QT_HAS_UPDATE_COMPRESSION_PATCH */ |
| 352 |
|
| 353 |
Notes: |
| 354 |
|
| 355 |
1) Try to avoid inclusion of `config-qt-patches-present.h` into header files. Include |
| 356 |
that into .cpp only (just to avid too much compile dependencies). |
| 357 |
|
| 358 |
2) If you don't add your patch into the list of features, its status can still |
| 359 |
be tracked on CI via `CMakeConfigureLog.yaml` file, which is available as an |
| 360 |
artifact of the build. |
| 361 |
|
| 362 |
With Krita now supporting Python scripting, we need guidelines for these as well. |
| 363 |
These guidelines are preliminary and may be further refined in the future. |
| 364 |
|
| 365 |
To keep it simple, we have chosen to follow the style guide suggested by Python: PEP8. |
| 366 |
|
| 367 |
All rules should be followed, except the max limit of 79 characters per line. As this |
| 368 |
can reduce readability in some cases, this rule is optional. |
| 369 |
|
| 370 |
The full PEP8 specification is available here: https://www.python.org/dev/peps/pep-0008/ |
| 371 |
|
| 372 |
To check compliance you can run pep8.py against the code. |
| 373 |
You can also use autopep8.py to automatically fix detected compliance issues. |
| 374 |
|
| 375 |
pep8.py can be downloaded via Python's package manager (pip) [https://pypi.python.org/pypi/pep8], |
| 376 |
or your distribution's package manager. |
| 377 |
autopep8.py can also be downloaded via Python's package manager [https://pypi.python.org/pypi/autopep8], |
| 378 |
or your distribution's package manager. |
| 379 |
|
| 380 |
Both of these scripts come bundled with the PyDev plugin, which is available for Eclipse and other IDEs. |
| 381 |
The PyDev integration can be configured to visually highlight portions of the code which is not in compliance, |
| 382 |
as well as run autopep8 via shortcuts. |
| 383 |
|
| 384 |
pep8.py and autopep8.py can suppress select rules via the "--ignore" command line argument. |
| 385 |
To ignore the 79 characters per line rule, pep8.py can be called like this: |
| 386 |
|
| 387 |
pep8.py --ignore=E501 |
| 388 |
|
| 389 |
You can read more about the error codes and what they mean here: |
| 390 |
http://pep8.readthedocs.io/en/release-1.7.x/intro.html#error-codes |
| 391 |
|
| 392 |
QML Notes: |
| 393 |
|
| 394 |
While the hacking file is not a good place to discuss human interface design, there are some technical |
| 395 |
oddities with QML to take into account: |
| 396 |
|
| 397 |
- Height, Implicit Height and Content Height can get very confusing. In theory, implicit height is only used |
| 398 |
when height isn't set, but this is complicated by delegates never using the height property, and only using |
| 399 |
implicit height. Furthermore, contentHeight, and contentItem.implicitHeight are only relevant when the item |
| 400 |
has a contentItem, like with AbstractButtons and PopUps, but commonly, the implicit height of these |
| 401 |
is already set to the contentHeight, so doesn't need to be set (and can in fact lead to slowdowns). |
| 402 |
Conversely, if no implicit height is set on the content item, the content item will take the height |
| 403 |
and width from the parent button or popup. |
| 404 |
- Similarly, ListViews are not supposed to instantiate delegates outside of their views, but if a ListViews |
| 405 |
is instantiated with their height set to contentHeight, then, even as the ListView resizes itself later, it |
| 406 |
will instantiate a delegate for every single item it could lay out upon the ListView itself be instantiated. |
| 407 |
It's better to set the default height of the ListView to something bounded, like the fontSize * 3, and then |
| 408 |
ensure it is resized later. |
| 409 |
- Comboboxes expect their delegates to be a ItemDelegate or AbstractButton. The Qt Docs imply this is because |
| 410 |
those call activated(), but Combobox.highlightedIndex being set on hover also only works together ItemDelegate. |