summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-16 16:14:39 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-16 16:44:09 +0200
commit9102b624ac6b83d4e4418fecefd491f67d4603f8 (patch)
tree31b52c008e1b69fa1465913451270d45868dc853 /Libraries
parent96f98b1fc92fec77d222c5b7b356b278c660429b (diff)
downloadserenity-9102b624ac6b83d4e4418fecefd491f67d4603f8.zip
LibGUI+DevTools+Applications: Use ModelIndex::data() in many places
This way you don't have to keep track of which model it came from.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGUI/AbstractTableView.cpp2
-rw-r--r--Libraries/LibGUI/AbstractView.cpp10
-rw-r--r--Libraries/LibGUI/ColumnsView.cpp6
-rw-r--r--Libraries/LibGUI/ComboBox.cpp4
-rw-r--r--Libraries/LibGUI/FilteringProxyModel.cpp4
-rw-r--r--Libraries/LibGUI/IconView.cpp18
-rw-r--r--Libraries/LibGUI/ListView.cpp8
-rw-r--r--Libraries/LibGUI/ProcessChooser.cpp7
-rw-r--r--Libraries/LibGUI/SortingProxyModel.cpp4
-rw-r--r--Libraries/LibGUI/TableView.cpp8
-rw-r--r--Libraries/LibGUI/TreeView.cpp14
11 files changed, 42 insertions, 43 deletions
diff --git a/Libraries/LibGUI/AbstractTableView.cpp b/Libraries/LibGUI/AbstractTableView.cpp
index 40d6cac65c..797ac8a57e 100644
--- a/Libraries/LibGUI/AbstractTableView.cpp
+++ b/Libraries/LibGUI/AbstractTableView.cpp
@@ -74,7 +74,7 @@ void AbstractTableView::update_column_sizes()
header_width += font().width(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
int column_width = header_width;
for (int row = 0; row < row_count; ++row) {
- auto cell_data = model.data(model.index(row, column));
+ auto cell_data = model.index(row, column).data();
int cell_width = 0;
if (cell_data.is_icon()) {
cell_width = item_height();
diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp
index 0dd19e8f29..255519628f 100644
--- a/Libraries/LibGUI/AbstractView.cpp
+++ b/Libraries/LibGUI/AbstractView.cpp
@@ -134,7 +134,7 @@ void AbstractView::begin_editing(const ModelIndex& index)
ASSERT(aid_create_editing_delegate);
m_editing_delegate = aid_create_editing_delegate(index);
m_editing_delegate->bind(*model(), index);
- m_editing_delegate->set_value(model()->data(index, ModelRole::Display));
+ m_editing_delegate->set_value(index.data());
m_edit_widget = m_editing_delegate->widget();
add_child(*m_edit_widget);
m_edit_widget->move_to_back();
@@ -187,7 +187,7 @@ NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) c
if (!model())
return font();
- auto font_data = model()->data(index, ModelRole::Font);
+ auto font_data = index.data(ModelRole::Font);
if (font_data.is_font())
return font_data.as_font();
@@ -280,19 +280,19 @@ void AbstractView::mousemove_event(MouseEvent& event)
StringBuilder data_builder;
bool first = true;
m_selection.for_each_index([&](auto& index) {
- auto text_data = m_model->data(index);
+ auto text_data = index.data();
if (!first)
text_builder.append(", ");
text_builder.append(text_data.to_string());
- auto drag_data = m_model->data(index, ModelRole::DragData);
+ auto drag_data = index.data(ModelRole::DragData);
data_builder.append(drag_data.to_string());
data_builder.append('\n');
first = false;
if (!bitmap) {
- Variant icon_data = model()->data(index, ModelRole::Icon);
+ Variant icon_data = index.data(ModelRole::Icon);
if (icon_data.is_icon())
bitmap = icon_data.as_icon().bitmap_for_size(32);
}
diff --git a/Libraries/LibGUI/ColumnsView.cpp b/Libraries/LibGUI/ColumnsView.cpp
index 2c27ce1312..b722c17dc6 100644
--- a/Libraries/LibGUI/ColumnsView.cpp
+++ b/Libraries/LibGUI/ColumnsView.cpp
@@ -125,7 +125,7 @@ void ColumnsView::paint_event(PaintEvent& event)
Gfx::IntRect row_rect { column_x, row * item_height(), column.width, item_height() };
painter.fill_rect(row_rect, background_color);
- auto icon = model()->data(index, ModelRole::Icon);
+ auto icon = index.data(ModelRole::Icon);
Gfx::IntRect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() };
icon_rect.center_vertically_within(row_rect);
if (icon.is_icon()) {
@@ -141,7 +141,7 @@ void ColumnsView::paint_event(PaintEvent& event)
icon_rect.right() + 1 + icon_spacing(), row * item_height(),
column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - s_arrow_bitmap_width - icon_spacing(), item_height()
};
- auto text = model()->data(index).to_string();
+ auto text = index.data().to_string();
painter.draw_text(text_rect, text, Gfx::TextAlignment::CenterLeft, text_color);
bool expandable = model()->row_count(index) > 0;
@@ -203,7 +203,7 @@ void ColumnsView::update_column_sizes()
for (int row = 0; row < row_count; row++) {
ModelIndex index = model()->index(row, m_model_column, column.parent_index);
ASSERT(index.is_valid());
- auto text = model()->data(index).to_string();
+ auto text = index.data().to_string();
int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
if (row_width > column.width)
column.width = row_width;
diff --git a/Libraries/LibGUI/ComboBox.cpp b/Libraries/LibGUI/ComboBox.cpp
index 7e594be0e5..e04ec4f09f 100644
--- a/Libraries/LibGUI/ComboBox.cpp
+++ b/Libraries/LibGUI/ComboBox.cpp
@@ -118,7 +118,7 @@ ComboBox::ComboBox()
m_list_view->on_selection = [this](auto& index) {
ASSERT(model());
m_list_view->set_activates_on_selection(true);
- auto new_value = model()->data(index).to_string();
+ auto new_value = index.data().to_string();
m_editor->set_text(new_value);
if (!m_only_allow_values_from_model)
m_editor->select_all();
@@ -178,7 +178,7 @@ void ComboBox::open()
int longest_item_width = 0;
for (int i = 0; i < model()->row_count(); ++i) {
auto index = model()->index(i);
- auto item_text = model()->data(index).to_string();
+ auto item_text = index.data().to_string();
longest_item_width = max(longest_item_width, m_list_view->font().width(item_text));
}
Gfx::IntSize size {
diff --git a/Libraries/LibGUI/FilteringProxyModel.cpp b/Libraries/LibGUI/FilteringProxyModel.cpp
index fe746a4c36..a1ca2960cb 100644
--- a/Libraries/LibGUI/FilteringProxyModel.cpp
+++ b/Libraries/LibGUI/FilteringProxyModel.cpp
@@ -61,7 +61,7 @@ Variant FilteringProxyModel::data(const ModelIndex& index, ModelRole role) const
if ((size_t)index.row() > m_matching_indices.size() || index.row() < 0)
return 0;
- return m_model.data(m_matching_indices[index.row()], role);
+ return m_matching_indices[index.row()].data(role);
}
void FilteringProxyModel::update()
@@ -84,7 +84,7 @@ void FilteringProxyModel::filter()
auto filter_matches = m_model.data_matches(index, m_filter_term);
bool matches = filter_matches == TriState::True;
if (filter_matches == TriState::Unknown) {
- auto data = m_model.data(index, ModelRole::Display);
+ auto data = index.data();
if (data.is_string() && data.as_string().contains(m_filter_term))
matches = true;
}
diff --git a/Libraries/LibGUI/IconView.cpp b/Libraries/LibGUI/IconView.cpp
index 8723f73f91..1f24efad5d 100644
--- a/Libraries/LibGUI/IconView.cpp
+++ b/Libraries/LibGUI/IconView.cpp
@@ -95,7 +95,7 @@ void IconView::reinit_item_cache() const
auto& item_data = m_item_data_cache[i];
// TODO: It's unfortunate that we have no way to know whether any
// data actually changed, so we have to invalidate *everyone*
- if (item_data.is_valid()/* && !model()->is_valid(item_data.index)*/)
+ if (item_data.is_valid() /* && !model()->is_valid(item_data.index)*/)
item_data.invalidate();
if (item_data.selected && i < (size_t)m_first_selected_hint)
m_first_selected_hint = (int)i;
@@ -114,7 +114,7 @@ auto IconView::get_item_data(int item_index) const -> ItemData&
return item_data;
item_data.index = model()->index(item_index, model_column());
- item_data.data = model()->data(item_data.index);
+ item_data.data = item_data.index.data();
get_item_rects(item_index, item_data, font_for_index(item_data.index));
item_data.valid = true;
return item_data;
@@ -338,7 +338,7 @@ void IconView::mousemove_event(MouseEvent& event)
scroll_out_of_view_timer_fired();
};
}
-
+
m_out_of_view_position = event.position();
if (!m_out_of_view_timer->is_active())
m_out_of_view_timer->start();
@@ -373,7 +373,7 @@ void IconView::scroll_out_of_view_timer_fired()
else if (m_out_of_view_position.x() < in_view_rect.left())
adjust_x = -(SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + max(-SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.x() - in_view_rect.left());
- ScrollableWidget::scroll_into_view({scroll_to.translated(adjust_x, adjust_y), {1, 1}}, true, true);
+ ScrollableWidget::scroll_into_view({ scroll_to.translated(adjust_x, adjust_y), { 1, 1 } }, true, true);
update_rubber_banding(m_out_of_view_position);
}
@@ -423,7 +423,7 @@ void IconView::paint_event(PaintEvent& event)
Painter painter(*this);
painter.add_clip_rect(widget_inner_rect());
painter.add_clip_rect(event.rect());
-
+
if (fill_with_background_color())
painter.fill_rect(event.rect(), widget_background_color);
painter.translate(frame_thickness(), frame_thickness());
@@ -438,8 +438,8 @@ void IconView::paint_event(PaintEvent& event)
background_color = widget_background_color;
}
- auto icon = model()->data(item_data.index, ModelRole::Icon);
- auto item_text = model()->data(item_data.index, ModelRole::Display);
+ auto icon = item_data.index.data(ModelRole::Icon);
+ auto item_text = item_data.index.data();
if (icon.is_icon()) {
if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
@@ -458,7 +458,7 @@ void IconView::paint_event(PaintEvent& event)
if (item_data.selected)
text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
else
- text_color = model()->data(item_data.index, ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
+ text_color = item_data.index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
painter.fill_rect(item_data.text_rect, background_color);
painter.draw_text(item_data.text_rect, item_text.to_string(), font_for_index(item_data.index), Gfx::TextAlignment::Center, text_color, Gfx::TextElision::Right);
@@ -482,7 +482,7 @@ void IconView::did_update_selection()
AbstractView::did_update_selection();
if (m_changing_selection)
return;
-
+
// Selection was modified externally, we need to synchronize our cache
do_clear_selection();
selection().for_each_index([&](const ModelIndex& index) {
diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp
index 73bea486f1..a4572f0e39 100644
--- a/Libraries/LibGUI/ListView.cpp
+++ b/Libraries/LibGUI/ListView.cpp
@@ -59,7 +59,7 @@ void ListView::update_content_size()
int content_width = 0;
for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
- auto text = model()->data(model()->index(row, m_model_column), ModelRole::Display);
+ auto text = model()->index(row, m_model_column).data();
content_width = max(content_width, font().width(text.to_string()));
}
@@ -150,7 +150,7 @@ void ListView::paint_event(PaintEvent& event)
Gfx::IntRect row_rect(0, y, content_width(), item_height());
painter.fill_rect(row_rect, background_color);
auto index = model()->index(row_index, m_model_column);
- auto data = model()->data(index);
+ auto data = index.data();
auto font = font_for_index(index);
if (data.is_bitmap()) {
painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
@@ -162,11 +162,11 @@ void ListView::paint_event(PaintEvent& event)
if (is_selected_row)
text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
else
- text_color = model()->data(index, ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
+ text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
auto text_rect = row_rect;
text_rect.move_by(horizontal_padding(), 0);
text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
- auto text_alignment = model()->data(index, ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
+ auto text_alignment = index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
painter.draw_text(text_rect, data.to_string(), font, text_alignment, text_color);
}
diff --git a/Libraries/LibGUI/ProcessChooser.cpp b/Libraries/LibGUI/ProcessChooser.cpp
index 7d3df57fd1..cfc4d2fd47 100644
--- a/Libraries/LibGUI/ProcessChooser.cpp
+++ b/Libraries/LibGUI/ProcessChooser.cpp
@@ -96,7 +96,7 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView&
m_refresh_timer->on_timeout = [this] {
auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
if (!m_table_view->selection().is_empty()) {
- auto pid_as_variant = m_table_view->model()->data(m_table_view->selection().first(), GUI::ModelRole::Custom);
+ auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom);
previous_selected_pid = pid_as_variant.as_i32();
}
@@ -111,7 +111,7 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView&
auto column_index = 1; // Corresponds to PID column in the m_table_view.
for (int row_index = 0; row_index < row_count; ++row_index) {
auto cell_index = model->index(row_index, column_index);
- auto pid_as_variant = model->data(cell_index, GUI::ModelRole::Custom);
+ auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom);
if (previous_selected_pid == pid_as_variant.as_i32()) {
m_table_view->selection().set(cell_index); // Set only if PIDs are matched.
}
@@ -121,8 +121,7 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView&
void ProcessChooser::set_pid_from_index_and_close(const ModelIndex& index)
{
- auto pid_as_variant = m_table_view->model()->data(index, GUI::ModelRole::Custom);
- m_pid = pid_as_variant.as_i32();
+ m_pid = index.data(GUI::ModelRole::Custom).as_i32();
done(ExecOK);
}
diff --git a/Libraries/LibGUI/SortingProxyModel.cpp b/Libraries/LibGUI/SortingProxyModel.cpp
index 440dd338e8..1eccc363bd 100644
--- a/Libraries/LibGUI/SortingProxyModel.cpp
+++ b/Libraries/LibGUI/SortingProxyModel.cpp
@@ -130,8 +130,8 @@ StringView SortingProxyModel::drag_data_type() const
bool SortingProxyModel::less_than(const ModelIndex& index1, const ModelIndex& index2) const
{
- auto data1 = index1.model() ? index1.model()->data(index1, m_sort_role) : Variant();
- auto data2 = index2.model() ? index2.model()->data(index2, m_sort_role) : Variant();
+ auto data1 = index1.data(m_sort_role);
+ auto data2 = index2.data(m_sort_role);
if (data1.is_string() && data2.is_string())
return data1.as_string().to_lowercase() < data2.as_string().to_lowercase();
return data1 < data2;
diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp
index 92951d19ae..6e1ba7fe4b 100644
--- a/Libraries/LibGUI/TableView.cpp
+++ b/Libraries/LibGUI/TableView.cpp
@@ -113,7 +113,7 @@ void TableView::paint_event(PaintEvent& event)
if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
delegate->paint(painter, cell_rect, palette(), cell_index);
} else {
- auto data = model()->data(cell_index);
+ auto data = cell_index.data();
if (data.is_bitmap()) {
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
} else if (data.is_icon()) {
@@ -128,13 +128,13 @@ void TableView::paint_event(PaintEvent& event)
if (is_selected_row)
text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
else
- text_color = model()->data(cell_index, ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
+ text_color = cell_index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
if (!is_selected_row) {
- auto cell_background_color = model()->data(cell_index, ModelRole::BackgroundColor);
+ auto cell_background_color = cell_index.data(ModelRole::BackgroundColor);
if (cell_background_color.is_valid())
painter.fill_rect(cell_rect_for_fill, cell_background_color.to_color(background_color));
}
- auto text_alignment = model()->data(cell_index, ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
+ auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right);
}
}
diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp
index 270eec79db..d228ad7abf 100644
--- a/Libraries/LibGUI/TreeView.cpp
+++ b/Libraries/LibGUI/TreeView.cpp
@@ -171,7 +171,7 @@ void TreeView::traverse_in_paint_order(Callback callback) const
if (index.is_valid()) {
auto& metadata = ensure_metadata_for_index(index);
int x_offset = tree_column_x_offset + horizontal_padding() + indent_level * indent_width_in_pixels();
- auto node_text = model.data(index, ModelRole::Display).to_string();
+ auto node_text = index.data().to_string();
Gfx::IntRect rect = {
x_offset, y_offset,
icon_size() + icon_spacing() + text_padding() + font_for_index(index)->width(node_text) + text_padding(), item_height()
@@ -293,7 +293,7 @@ void TreeView::paint_event(PaintEvent& event)
if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
delegate->paint(painter, cell_rect, palette(), cell_index);
} else {
- auto data = model.data(cell_index);
+ auto data = cell_index.data();
if (data.is_bitmap()) {
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
@@ -302,15 +302,15 @@ void TreeView::paint_event(PaintEvent& event)
painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
} else {
if (!is_selected_row)
- text_color = model.data(cell_index, ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
- auto text_alignment = model.data(cell_index, ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
+ text_color = cell_index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
+ auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
painter.draw_text(cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, text_color, Gfx::TextElision::Right);
}
}
} else {
// It's the tree column!
Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
- auto icon = model.data(index, ModelRole::Icon);
+ auto icon = index.data(ModelRole::Icon);
if (icon.is_icon()) {
if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row())
@@ -323,7 +323,7 @@ void TreeView::paint_event(PaintEvent& event)
icon_rect.right() + 1 + icon_spacing(), rect.y(),
rect.width() - icon_size() - icon_spacing(), rect.height()
};
- auto node_text = model.data(index, ModelRole::Display).to_string();
+ auto node_text = index.data().to_string();
painter.draw_text(text_rect, node_text, font_for_index(index), Gfx::TextAlignment::Center, text_color);
auto index_at_indent = index;
for (int i = indent_level; i > 0; --i) {
@@ -544,7 +544,7 @@ void TreeView::update_column_sizes()
int column_width = header_width;
for (int row = 0; row < row_count; ++row) {
- auto cell_data = model.data(model.index(row, column));
+ auto cell_data = model.index(row, column).data();
int cell_width = 0;
if (cell_data.is_bitmap()) {
cell_width = cell_data.as_bitmap().width();