diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-21 19:36:09 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-21 19:55:44 +0200 |
commit | 2e03bded43344d4dd2d4f19d4d963646aaa77e40 (patch) | |
tree | 28f044c3485c240307abb8c289cbdb28831dfb80 /Libraries | |
parent | 3c819b8ff4d8c1cefceeb65ee0e89e3858a11bb5 (diff) | |
download | serenity-2e03bded43344d4dd2d4f19d4d963646aaa77e40.zip |
LibGUI: Add Model::Role::TextAlignment and remove from ColumnMetadata
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/FileSystemModel.cpp | 38 | ||||
-rw-r--r-- | Libraries/LibGUI/JsonArrayModel.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGUI/ListView.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibGUI/Model.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/TableView.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/TreeView.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibGUI/Variant.cpp | 32 | ||||
-rw-r--r-- | Libraries/LibGUI/Variant.h | 11 |
8 files changed, 83 insertions, 19 deletions
diff --git a/Libraries/LibGUI/FileSystemModel.cpp b/Libraries/LibGUI/FileSystemModel.cpp index 654d3c0f9a..9a4aea44ba 100644 --- a/Libraries/LibGUI/FileSystemModel.cpp +++ b/Libraries/LibGUI/FileSystemModel.cpp @@ -345,6 +345,26 @@ ModelIndex FileSystemModel::parent_index(const ModelIndex& index) const Variant FileSystemModel::data(const ModelIndex& index, Role role) const { ASSERT(index.is_valid()); + + if (role == Role::TextAlignment) { + switch (index.column()) { + case Column::Icon: + return Gfx::TextAlignment::Center; + case Column::Size: + case Column::Inode: + return Gfx::TextAlignment::CenterRight; + case Column::Name: + case Column::Owner: + case Column::Group: + case Column::ModificationTime: + case Column::Permissions: + case Column::SymlinkTarget: + return Gfx::TextAlignment::CenterLeft; + default: + ASSERT_NOT_REACHED(); + } + } + auto& node = this->node(index); if (role == Role::Custom) { @@ -552,23 +572,23 @@ Model::ColumnMetadata FileSystemModel::column_metadata(int column) const { switch (column) { case Column::Icon: - return { 16, Gfx::TextAlignment::Center, Model::ColumnMetadata::Sortable::False }; + return { 16, Model::ColumnMetadata::Sortable::False }; case Column::Name: - return { 120, Gfx::TextAlignment::CenterLeft }; + return { 120 }; case Column::Size: - return { 80, Gfx::TextAlignment::CenterRight }; + return { 80 }; case Column::Owner: - return { 50, Gfx::TextAlignment::CenterLeft }; + return { 50 }; case Column::Group: - return { 50, Gfx::TextAlignment::CenterLeft }; + return { 50 }; case Column::ModificationTime: - return { 110, Gfx::TextAlignment::CenterLeft }; + return { 110 }; case Column::Permissions: - return { 65, Gfx::TextAlignment::CenterLeft }; + return { 65 }; case Column::Inode: - return { 60, Gfx::TextAlignment::CenterRight }; + return { 60 }; case Column::SymlinkTarget: - return { 120, Gfx::TextAlignment::CenterLeft }; + return { 120 }; } ASSERT_NOT_REACHED(); } diff --git a/Libraries/LibGUI/JsonArrayModel.cpp b/Libraries/LibGUI/JsonArrayModel.cpp index 9e158212e3..9dd75001da 100644 --- a/Libraries/LibGUI/JsonArrayModel.cpp +++ b/Libraries/LibGUI/JsonArrayModel.cpp @@ -94,7 +94,7 @@ bool JsonArrayModel::remove(int row) Model::ColumnMetadata JsonArrayModel::column_metadata(int column) const { ASSERT(column < static_cast<int>(m_fields.size())); - return { 100, m_fields[column].text_alignment }; + return { 100 }; } Variant JsonArrayModel::data(const ModelIndex& index, Role role) const @@ -102,6 +102,10 @@ Variant JsonArrayModel::data(const ModelIndex& index, Role role) const auto& field_spec = m_fields[index.column()]; auto& object = m_array.at(index.row()).as_object(); + if (role == Model::Role::TextAlignment) { + return field_spec.text_alignment; + } + if (role == Model::Role::Display) { auto& json_field_name = field_spec.json_field_name; auto data = object.get(json_field_name); diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp index 68592a0f79..b16b012134 100644 --- a/Libraries/LibGUI/ListView.cpp +++ b/Libraries/LibGUI/ListView.cpp @@ -143,8 +143,6 @@ void ListView::paint_event(PaintEvent& event) } } - auto column_metadata = model()->column_metadata(m_model_column); - Gfx::Rect row_rect(0, y, content_width(), item_height()); painter.fill_rect(row_rect, background_color); auto index = model()->index(row_index, m_model_column); @@ -164,7 +162,8 @@ void ListView::paint_event(PaintEvent& event) auto text_rect = row_rect; text_rect.move_by(horizontal_padding(), 0); text_rect.set_width(text_rect.width() - horizontal_padding() * 2); - painter.draw_text(text_rect, data.to_string(), font, column_metadata.text_alignment, text_color); + auto text_alignment = model()->data(index, Model::Role::TextAlignment).to_text_alignment(Gfx::TextAlignment::Center); + painter.draw_text(text_rect, data.to_string(), font, text_alignment, text_color); } ++painted_item_index; diff --git a/Libraries/LibGUI/Model.h b/Libraries/LibGUI/Model.h index 8cd34935d1..baf56aad32 100644 --- a/Libraries/LibGUI/Model.h +++ b/Libraries/LibGUI/Model.h @@ -48,7 +48,6 @@ class Model : public RefCounted<Model> { public: struct ColumnMetadata { int preferred_width { 0 }; - Gfx::TextAlignment text_alignment { Gfx::TextAlignment::CenterLeft }; enum class Sortable { False, True, @@ -70,6 +69,7 @@ public: Icon, Font, DragData, + TextAlignment, }; virtual ~Model(); diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp index 262ad7ff45..70128e9415 100644 --- a/Libraries/LibGUI/TableView.cpp +++ b/Libraries/LibGUI/TableView.cpp @@ -103,7 +103,6 @@ void TableView::paint_event(PaintEvent& event) for (int column_index = 0; column_index < model()->column_count(); ++column_index) { if (is_column_hidden(column_index)) continue; - auto column_metadata = model()->column_metadata(column_index); int column_width = this->column_width(column_index); bool is_key_column = model()->key_column() == column_index; Gfx::Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height()); @@ -136,7 +135,8 @@ void TableView::paint_event(PaintEvent& event) if (cell_background_color.is_valid()) painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color)); } - painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), column_metadata.text_alignment, text_color, Gfx::TextElision::Right); + auto text_alignment = model()->data(cell_index, Model::Role::TextAlignment).to_text_alignment(Gfx::TextAlignment::Center); + painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right); } } x_offset += column_width + horizontal_padding() * 2; diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index a6e7cbb280..51b9ae8456 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -273,7 +273,6 @@ void TreeView::paint_event(PaintEvent& event) for (int column_index = 0; column_index < model.column_count(); ++column_index) { if (is_column_hidden(column_index)) continue; - auto column_metadata = model.column_metadata(column_index); int column_width = this->column_width(column_index); painter.draw_rect(toggle_rect, text_color); @@ -295,7 +294,8 @@ void TreeView::paint_event(PaintEvent& event) } else { if (!is_selected_row) text_color = model.data(cell_index, Model::Role::ForegroundColor).to_color(palette().color(foreground_role())); - painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), column_metadata.text_alignment, text_color, Gfx::TextElision::Right); + auto text_alignment = model.data(cell_index, Model::Role::TextAlignment).to_text_alignment(Gfx::TextAlignment::Center); + painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right); } } } else { diff --git a/Libraries/LibGUI/Variant.cpp b/Libraries/LibGUI/Variant.cpp index 09c6f583be..88917abac3 100644 --- a/Libraries/LibGUI/Variant.cpp +++ b/Libraries/LibGUI/Variant.cpp @@ -61,6 +61,8 @@ const char* to_string(Variant::Type type) return "Rect"; case Variant::Type::Font: return "Font"; + case Variant::Type::TextAlignment: + return "TextAlignment"; } ASSERT_NOT_REACHED(); } @@ -94,6 +96,12 @@ void Variant::clear() m_value.as_string = nullptr; } +Variant::Variant(Gfx::TextAlignment value) + : m_type(Type::TextAlignment) +{ + m_value.as_text_alignment = value; +} + Variant::Variant(i32 value) : m_type(Type::Int32) { @@ -307,6 +315,9 @@ void Variant::copy_from(const Variant& other) case Type::Rect: m_value.as_rect = other.m_value.as_rect; break; + case Type::TextAlignment: + m_value.as_text_alignment = other.m_value.as_text_alignment; + break; case Type::Invalid: break; } @@ -343,6 +354,8 @@ bool Variant::operator==(const Variant& other) const return as_rect() == other.as_rect(); case Type::Font: return &as_font() == &other.as_font(); + case Type::TextAlignment: + return m_value.as_text_alignment == other.m_value.as_text_alignment; case Type::Invalid: return true; } @@ -378,6 +391,7 @@ bool Variant::operator<(const Variant& other) const case Type::Size: case Type::Rect: case Type::Font: + case Type::TextAlignment: // FIXME: Figure out how to compare these. ASSERT_NOT_REACHED(); case Type::Invalid: @@ -415,9 +429,25 @@ String Variant::to_string() const return as_rect().to_string(); case Type::Font: return String::format("[Font: %s]", as_font().name().characters()); + case Type::TextAlignment: { + switch (m_value.as_text_alignment) { + case Gfx::TextAlignment::Center: + return "Gfx::TextAlignment::Center"; + case Gfx::TextAlignment::CenterLeft: + return "Gfx::TextAlignment::CenterLeft"; + case Gfx::TextAlignment::CenterRight: + return "Gfx::TextAlignment::CenterRight"; + case Gfx::TextAlignment::TopLeft: + return "Gfx::TextAlignment::TopLeft"; + case Gfx::TextAlignment::TopRight: + return "Gfx::TextAlignment::TopRight"; + default: + ASSERT_NOT_REACHED(); + } + return ""; + } case Type::Invalid: return "[null]"; - break; } ASSERT_NOT_REACHED(); } diff --git a/Libraries/LibGUI/Variant.h b/Libraries/LibGUI/Variant.h index 03fecec550..07b20e3e34 100644 --- a/Libraries/LibGUI/Variant.h +++ b/Libraries/LibGUI/Variant.h @@ -50,6 +50,7 @@ public: Variant(const Gfx::Size&); Variant(const Gfx::Rect&); Variant(const Gfx::Font&); + Variant(const Gfx::TextAlignment); Variant(const AK::JsonValue&); Variant(Color); @@ -77,6 +78,7 @@ public: Size, Rect, Font, + TextAlignment, }; bool is_valid() const { return m_type != Type::Invalid; } @@ -93,6 +95,7 @@ public: bool is_size() const { return m_type == Type::Size; } bool is_rect() const { return m_type == Type::Rect; } bool is_font() const { return m_type == Type::Font; } + bool is_text_alignment() const { return m_type == Type::TextAlignment; } Type type() const { return m_type; } bool as_bool() const @@ -226,6 +229,13 @@ public: return *m_value.as_font; } + Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const + { + if (type() != Type::TextAlignment) + return default_value; + return m_value.as_text_alignment; + } + Color to_color(Color default_value = {}) const { if (type() == Type::Color) @@ -272,6 +282,7 @@ private: unsigned as_uint; float as_float; Gfx::RGBA32 as_color; + Gfx::TextAlignment as_text_alignment; RawPoint as_point; RawSize as_size; RawRect as_rect; |