diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-13 12:35:19 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-13 12:39:20 +0200 |
commit | 0f4050903d48da88127157366eef9c50a941ffa2 (patch) | |
tree | 372c37637d1b3cf173cd9966a93800ee3e6afbeb | |
parent | 4df360be8caf49851ead137c4fefa5a38c4bbab1 (diff) | |
download | serenity-0f4050903d48da88127157366eef9c50a941ffa2.zip |
GVariant: Add to_bool(), to_int() and to_color().
-rw-r--r-- | LibGUI/GVariant.h | 35 | ||||
-rw-r--r-- | SharedGraphics/Point.h | 2 |
2 files changed, 37 insertions, 0 deletions
diff --git a/LibGUI/GVariant.h b/LibGUI/GVariant.h index 08c9301021..b4855de43a 100644 --- a/LibGUI/GVariant.h +++ b/LibGUI/GVariant.h @@ -60,12 +60,40 @@ public: return m_value.as_bool; } + bool to_bool() const + { + if (type() == Type::Bool) + return as_bool(); + if (type() == Type::String) + return !!m_value.as_string; + if (type() == Type::Int) + return m_value.as_int != 0; + if (type() == Type::Rect) + return !as_rect().is_null(); + if (type() == Type::Size) + return !as_size().is_null(); + if (type() == Type::Point) + return !as_point().is_null(); + return is_valid(); + } + int as_int() const { ASSERT(type() == Type::Int); return m_value.as_int; } + int to_int() const + { + if (is_int()) + return as_int(); + if (is_bool()) + return as_bool() ? 1 : 0; + if (is_float()) + return (int)as_float(); + return 0; + } + float as_float() const { ASSERT(type() == Type::Float); @@ -111,6 +139,13 @@ public: return Color::from_rgba(m_value.as_color); } + Color to_color() const + { + if (is_color()) + return as_color(); + return Color(); + } + Color to_color(Color default_value) const { if (type() == Type::Color) diff --git a/SharedGraphics/Point.h b/SharedGraphics/Point.h index 87680ce22f..b9fa85d994 100644 --- a/SharedGraphics/Point.h +++ b/SharedGraphics/Point.h @@ -61,6 +61,8 @@ public: operator WSAPI_Point() const; String to_string() const { return String::format("[%d,%d]", x(), y()); } + bool is_null() const { return !m_x && !m_y; } + private: int m_x { 0 }; int m_y { 0 }; |