mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 06:48:49 +00:00
LibGUI: Add ModelClient abstract class and allow registering clients
This solves a problem where the SortingProxyModel doesn't receive the on_update call because other code overwrote the handler later on.
This commit is contained in:
parent
0e10a92ebc
commit
b778804d20
Notes:
sideshowbarker
2024-07-19 04:51:04 +09:00
Author: https://github.com/tomuta
Commit: b778804d20
Pull-request: https://github.com/SerenityOS/serenity/pull/2761
Reviewed-by: https://github.com/Dexesttp
13 changed files with 144 additions and 60 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/TemporaryChange.h>
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibGUI/AbstractView.h>
|
||||
#include <LibGUI/SortingProxyModel.h>
|
||||
|
@ -36,13 +37,22 @@ SortingProxyModel::SortingProxyModel(NonnullRefPtr<Model>&& target)
|
|||
: m_target(move(target))
|
||||
, m_key_column(-1)
|
||||
{
|
||||
m_target->on_update = [this] {
|
||||
resort();
|
||||
};
|
||||
// Since the target model already called Model::did_update we can't
|
||||
// assume we will get another call. So, we need to register for further
|
||||
// updates and just call resort() right away, otherwise requests
|
||||
// to this model won't work because there are no indices to map
|
||||
m_target->register_client(*this);
|
||||
resort();
|
||||
}
|
||||
|
||||
SortingProxyModel::~SortingProxyModel()
|
||||
{
|
||||
m_target->unregister_client(*this);
|
||||
}
|
||||
|
||||
void SortingProxyModel::on_model_update(unsigned flags)
|
||||
{
|
||||
resort(flags);
|
||||
}
|
||||
|
||||
int SortingProxyModel::row_count(const ModelIndex& index) const
|
||||
|
@ -100,15 +110,16 @@ void SortingProxyModel::set_key_column_and_sort_order(int column, SortOrder sort
|
|||
resort();
|
||||
}
|
||||
|
||||
void SortingProxyModel::resort()
|
||||
void SortingProxyModel::resort(unsigned flags)
|
||||
{
|
||||
TemporaryChange change(m_sorting, true);
|
||||
auto old_row_mappings = m_row_mappings;
|
||||
int row_count = target().row_count();
|
||||
m_row_mappings.resize(row_count);
|
||||
for (int i = 0; i < row_count; ++i)
|
||||
m_row_mappings[i] = i;
|
||||
if (m_key_column == -1) {
|
||||
did_update(Model::UpdateFlag::DontInvalidateIndexes);
|
||||
did_update(flags);
|
||||
return;
|
||||
}
|
||||
quick_sort(m_row_mappings, [&](auto row1, auto row2) -> bool {
|
||||
|
@ -123,24 +134,25 @@ void SortingProxyModel::resort()
|
|||
is_less_than = data1 < data2;
|
||||
return m_sort_order == SortOrder::Ascending ? is_less_than : !is_less_than;
|
||||
});
|
||||
did_update(Model::UpdateFlag::DontInvalidateIndexes);
|
||||
for_each_view([&](AbstractView& view) {
|
||||
auto& selection = view.selection();
|
||||
Vector<ModelIndex> selected_indexes_in_target;
|
||||
selection.for_each_index([&](const ModelIndex& index) {
|
||||
selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
|
||||
});
|
||||
view.selection().change_from_model({}, [&](ModelSelection& selection) {
|
||||
Vector<ModelIndex> selected_indexes_in_target;
|
||||
selection.for_each_index([&](const ModelIndex& index) {
|
||||
selected_indexes_in_target.append(target().index(old_row_mappings[index.row()], index.column()));
|
||||
});
|
||||
|
||||
selection.clear();
|
||||
for (auto& index : selected_indexes_in_target) {
|
||||
for (size_t i = 0; i < m_row_mappings.size(); ++i) {
|
||||
if (m_row_mappings[i] == index.row()) {
|
||||
selection.add(this->index(i, index.column()));
|
||||
continue;
|
||||
selection.clear();
|
||||
for (auto& index : selected_indexes_in_target) {
|
||||
for (size_t i = 0; i < m_row_mappings.size(); ++i) {
|
||||
if (m_row_mappings[i] == index.row()) {
|
||||
selection.add(this->index(i, index.column()));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
did_update(flags);
|
||||
}
|
||||
|
||||
bool SortingProxyModel::is_column_sortable(int column_index) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue