Stellarium 0.11.4 | |||
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · File Structure |
The increasing number of contributors require that we clearly define coding rules and guidelines. Although for historical reasons the current code of Stellarium does not always comply to these rules, they should now be respected for any addition or modification of the code.
#define MIN_WIDTH 3 static const QString VERSION = "0.10.1";
[--tab-][--tab-][--tab-]someFunction(param, ... [--tab-][--tab-][--tab-][ spaces ]moreparams, ...);
void MyClass::myMethod(int x) { if (x>10) { cout << "You won." << endl; } }
enums should follow the Qt conventions. i.e. CamelCase with First letter capitalization for both enum type and enum values. Document with doxygen. The //!< tag can be used to add descriptions on the same line as an enum value, e.g.
//! @enum EnumName Here is a description of the enum enum EnumName { EnumValueOne, //!< Some doxygen description of EnumValueOne EnumValueTwo, //!< Some doxygen description of EnumValueTwo EnumValueThree //!< Some doxygen description of EnumValueThree };
astyle --style=ansi -tU source.cpp
The extensions are .hpp/.cpp for C++ headers/code, .h/.c for C headers/code. C++ files should have the same name and case than the class they contain. For example class StelFontMgr should be declared in file StelFontMgr.hpp and implemented in StelFontMgr.cpp.
Stellarium source code should be documented with Doxygen. From Doxygen webpage:
"Doxygen is a documentation system for C++, C, Java, [...] It can generate an on-line documentation browser (in HTML) and/or an off-line reference manual (in LaTeX) from a set of documented source files. [...] The documentation is extracted directly from the sources, which makes it much easier to keep the documentation consistent with the source code. [...] You can also visualize the relations between the various elements by means of include dependency graphs, inheritance diagrams, and collaboration diagrams, which are all generated automatically.
All public and protected classes and methods from Stellarium should be fully documented in the headers (.hpp).
There are different ways to comment C++ code with Doxygen, in Stellarium use the following for headers files:
//! Find and return the list of at most maxNbItem objects auto-completing the passed object I18n name. //! @param objPrefix the case insensitive first letters of the searched object. //! @param maxNbItem the maximum number of returned object names. //! @return a vector of matching object name by order of relevance, or an empty vector if nothing match. QList<QString> listMatchingObjectsI18n(const QString& objPrefix, unsigned int maxNbItem=5) const;
Brief descriptions are single line only, and stop at the first full stop (period). Any subsequent sentences which occur before @param or a similar tag are considered to be part of a detailed description.
For methods definitions in .cpp files, a simpler comment for each method is sufficient:
// Find and return the list of at most maxNbItem objects auto-completing the // passed object I18n name. QList<QString> listMatchingObjectsI18n(const QString& objPrefix, unsigned int maxNbItem=5) const { etc..
Use C++ replacement for C functions and Qt replacements for C++ functions/STL wherever possible.
std::string
or char *
fopen()
#include <stdio.h> // Bad #include <cstdio> // Good #include <QString> // Good
std::vector
or std::map
, they are extremely efficient. Documentation is there. #define RADIUS 12 // Bad static const int RADIUS = 12; // Good
double cosLat = cos(lat); // Bad double cosLat = std::cos(lat); // Good
Translatable strings are translated using the StelTranslator class, which is a C++ wrapper around gettext. The strings should be marked using the q_()
macro: it takes a QString in English and returns the translation as a QString using the current global language. Note that you can also call q_() with a QString object to return a translation. In cases when a string literal needs to be marked for translation without returning a string, use the N_() macro.
QString::arg()
instead. Concatenated strings are very hard (or even impossible) to translate. When using Qt format strings, if a letter follows a digit immediately, xgettext might erroneously mark the extracted string as a C format string. ''Depending on the actual translation, this might cause errors when uploading the message catalog to Rosetta, or when compiling it to binary format.'' To prevent this, add an xgettext:no-c-format
comment to the line preceding the format string:
// xgettext:no-c-format text = q_("Distance: %1AU").arg(distance);
Translatable text should obey English typographic conventions. For example, there should be no space before the colon:
You can add clarifying comments for the translators. They will be automatically extracted and visible in the .po files and in the web interface:
// TRANSLATORS: Message displayed when an error occurs. QString errorMessage(q_("Loading failed."));
Strings in Qt .ui files (used for GUI windows) are marked for translation, unless the "translate" flag is unchecked. Note that these strings are actually extracted from the files generated by the .ui file compiler (uic), and not from the .ui files themselves, so translation comments in the .ui files will be ignored.
std::cout << "Error while opening file " << qPrintable(myFileName) << "." << std::endl; // Bad std::cerr << "Error while opening file " << qPrintable(myFileName) << "." << std::endl; // Good