refactor(drawtypes): Cleanup and fixes

This commit is contained in:
Michael Carlberg 2016-10-25 07:10:03 +02:00
parent a0f0fc8723
commit d2187f44e0
17 changed files with 257 additions and 189 deletions
include/drawtypes

View file

@ -61,7 +61,7 @@ namespace drawtypes {
m_tokenized = string_util::replace_all(m_tokenized, token, replacement);
}
void replace_defined_values(label_t label) {
void replace_defined_values(const label_t& label) {
if (!label->m_foreground.empty())
m_foreground = label->m_foreground;
if (!label->m_background.empty())
@ -72,43 +72,81 @@ namespace drawtypes {
m_overline = label->m_overline;
}
void copy_undefined(const label_t& label) {
if (m_foreground.empty() && !label->m_foreground.empty())
m_foreground = label->m_foreground;
if (m_background.empty() && !label->m_background.empty())
m_background = label->m_background;
if (m_underline.empty() && !label->m_underline.empty())
m_underline = label->m_underline;
if (m_overline.empty() && !label->m_overline.empty())
m_overline = label->m_overline;
if (m_font == 0 && label->m_font != 0)
m_font = label->m_font;
if (m_padding == 0 && label->m_padding != 0)
m_padding = label->m_padding;
if (m_margin == 0 && label->m_margin != 0)
m_margin = label->m_margin;
if (m_maxlen == 0 && label->m_maxlen != 0) {
m_maxlen = label->m_maxlen;
m_ellipsis = label->m_ellipsis;
}
}
private:
string m_text, m_tokenized;
};
inline label_t get_config_label(const config& conf, string section, string name = "label",
bool required = true, string def = "") {
string text;
/**
* Create a label by loading values from the configuration
*/
inline label_t load_label(
const config& conf, string section, string name, bool required = true, string def = "") {
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
string text;
if (required)
text = conf.get<string>(section, name);
else
text = conf.get<string>(section, name, def);
return label_t{new label(text, conf.get<string>(section, name + "-foreground", ""),
// clang-format off
return label_t{new label_t::element_type(text,
conf.get<string>(section, name + "-foreground", ""),
conf.get<string>(section, name + "-background", ""),
conf.get<string>(section, name + "-underline", ""),
conf.get<string>(section, name + "-overline", ""),
conf.get<int>(section, name + "-font", 0), conf.get<int>(section, name + "-padding", 0),
conf.get<int>(section, name + "-margin", 0), conf.get<size_t>(section, name + "-maxlen", 0),
conf.get<int>(section, name + "-font", 0),
conf.get<int>(section, name + "-padding", 0),
conf.get<int>(section, name + "-margin", 0),
conf.get<size_t>(section, name + "-maxlen", 0),
conf.get<bool>(section, name + "-ellipsis", true))};
// clang-format on
}
inline label_t get_optional_config_label(
const config& conf, string section, string name = "label", string def = "") {
return get_config_label(conf, section, name, false, def);
/**
* Create a label by loading optional values from the configuration
*/
inline label_t load_optional_label(
const config& conf, string section, string name, string def = "") {
return load_label(conf, section, name, false, def);
}
inline icon_t get_config_icon(const config& conf, string section, string name = "icon",
bool required = true, string def = "") {
return get_config_label(conf, section, name, required, def);
/**
* Create an icon by loading values from the configuration
*/
inline icon_t load_icon(
const config& conf, string section, string name, bool required = true, string def = "") {
return load_label(conf, section, name, required, def);
}
inline icon_t get_optional_config_icon(
const config& conf, string section, string name = "icon", string def = "") {
return get_config_icon(conf, section, name, false, def);
/**
* Create an icon by loading optional values from the configuration
*/
inline icon_t load_optional_icon(
const config& conf, string section, string name, string def = "") {
return load_icon(conf, section, name, false, def);
}
}