diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 04:54:07 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 04:54:07 +0100 |
commit | d466f2634d94df44402c5779395a8f3798151d45 (patch) | |
tree | c7624d7de825913772aba6f8d304f19901459cb1 /LibGUI | |
parent | f4b8e4966ffa07f250e4952060791b1606aa10f2 (diff) | |
download | serenity-d466f2634d94df44402c5779395a8f3798151d45.zip |
LibGUI: Add GTableModel::Role::ForegroundColor.
This makes it possible to specify the text color for each table cell.
Use this to make the IRCClient show unread window list items in red.
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GTableModel.h | 2 | ||||
-rw-r--r-- | LibGUI/GTableView.cpp | 21 | ||||
-rw-r--r-- | LibGUI/GVariant.cpp | 16 | ||||
-rw-r--r-- | LibGUI/GVariant.h | 17 |
4 files changed, 47 insertions, 9 deletions
diff --git a/LibGUI/GTableModel.h b/LibGUI/GTableModel.h index 781986d880..659ddae91a 100644 --- a/LibGUI/GTableModel.h +++ b/LibGUI/GTableModel.h @@ -42,7 +42,7 @@ public: const Font* font { nullptr }; }; - enum class Role { Display, Sort, Custom }; + enum class Role { Display, Sort, Custom, ForegroundColor, BackgroundColor }; virtual ~GTableModel(); diff --git a/LibGUI/GTableView.cpp b/LibGUI/GTableView.cpp index c8aa9e6b49..9d4b528b5b 100644 --- a/LibGUI/GTableView.cpp +++ b/LibGUI/GTableView.cpp @@ -116,15 +116,14 @@ void GTableView::paint_event(GPaintEvent& event) int y_offset = header_height(); for (int row_index = 0; row_index < m_model->row_count(); ++row_index) { + bool is_selected_row = row_index == m_model->selected_index().row(); int y = y_offset + painted_item_index * item_height(); Color background_color; Color key_column_background_color; - Color text_color; - if (row_index == m_model->selected_index().row()) { + if (is_selected_row) { background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060); key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060); - text_color = Color::White; } else { if (alternating_row_colors() && (painted_item_index % 2)) { background_color = Color(210, 210, 210); @@ -133,10 +132,9 @@ void GTableView::paint_event(GPaintEvent& event) background_color = Color::White; key_column_background_color = Color(235, 235, 235); } - text_color = Color::Black; } - painter.fill_rect(row_rect(painted_item_index), background_color); + int x_offset = 0; for (int column_index = 0; column_index < m_model->column_count(); ++column_index) { auto column_metadata = m_model->column_metadata(column_index); @@ -148,11 +146,18 @@ void GTableView::paint_event(GPaintEvent& event) auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0); painter.fill_rect(cell_rect_for_fill, key_column_background_color); } - auto data = m_model->data({ row_index, column_index }); - if (data.is_bitmap()) + GModelIndex cell_index(row_index, column_index); + auto data = m_model->data(cell_index); + if (data.is_bitmap()) { painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); - else + } else { + Color text_color; + if (is_selected_row) + text_color = Color::White; + else + text_color = m_model->data(cell_index, GTableModel::Role::ForegroundColor).to_color(Color::Black); painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color); + } x_offset += column_width + horizontal_padding() * 2; } ++painted_item_index; diff --git a/LibGUI/GVariant.cpp b/LibGUI/GVariant.cpp index 7def775885..1e8b7d187a 100644 --- a/LibGUI/GVariant.cpp +++ b/LibGUI/GVariant.cpp @@ -1,5 +1,9 @@ #include <LibGUI/GVariant.h> +GVariant::GVariant() +{ +} + GVariant::~GVariant() { switch (m_type) { @@ -48,6 +52,12 @@ GVariant::GVariant(const GraphicsBitmap& value) AK::retain_if_not_null(m_value.as_bitmap); } +GVariant::GVariant(Color color) + : m_type(Type::Color) +{ + m_value.as_color = color.value(); +} + bool GVariant::operator==(const GVariant& other) const { if (m_type != other.m_type) @@ -63,6 +73,8 @@ bool GVariant::operator==(const GVariant& other) const return as_string() == other.as_string(); case Type::Bitmap: return m_value.as_bitmap == other.m_value.as_bitmap; + case Type::Color: + return m_value.as_color == other.m_value.as_color; case Type::Invalid: break; } @@ -85,6 +97,8 @@ bool GVariant::operator<(const GVariant& other) const case Type::Bitmap: // FIXME: Maybe compare bitmaps somehow differently? return m_value.as_bitmap < other.m_value.as_bitmap; + case Type::Color: + return m_value.as_color < other.m_value.as_color; case Type::Invalid: break; } @@ -104,6 +118,8 @@ String GVariant::to_string() const return as_string(); case Type::Bitmap: return "[GraphicsBitmap]"; + case Type::Color: + return as_color().to_string(); case Type::Invalid: break; } diff --git a/LibGUI/GVariant.h b/LibGUI/GVariant.h index 87c7f27175..1346188015 100644 --- a/LibGUI/GVariant.h +++ b/LibGUI/GVariant.h @@ -11,6 +11,7 @@ public: GVariant(int); GVariant(const String&); GVariant(const GraphicsBitmap&); + GVariant(Color); ~GVariant(); enum class Type { @@ -20,6 +21,7 @@ public: Float, String, Bitmap, + Color, }; bool is_valid() const { return m_type != Type::Invalid; } @@ -28,6 +30,7 @@ public: bool is_float() const { return m_type == Type::Float; } bool is_string() const { return m_type == Type::String; } bool is_bitmap() const { return m_type == Type::Bitmap; } + bool is_color() const { return m_type == Type::Color; } Type type() const { return m_type; } bool as_bool() const @@ -60,6 +63,19 @@ public: return *m_value.as_bitmap; } + Color as_color() const + { + ASSERT(type() == Type::Color); + return Color::from_rgba(m_value.as_color); + } + + Color to_color(Color default_value) const + { + if (type() == Type::Color) + return as_color(); + return default_value; + } + String to_string() const; bool operator==(const GVariant&) const; @@ -72,6 +88,7 @@ private: bool as_bool; int as_int; float as_float; + RGBA32 as_color; } m_value; Type m_type { Type::Invalid }; |