From 0c02dfcad58fdfbf2b3b08812c1285faf966bf14 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 11 Apr 2021 12:14:46 +0200 Subject: [PATCH] LibGUI: Add convenient helpers for getting the sibling of a ModelIndex --- Userland/Libraries/LibGUI/ModelIndex.cpp | 17 ++++++++++++++++- Userland/Libraries/LibGUI/ModelIndex.h | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGUI/ModelIndex.cpp b/Userland/Libraries/LibGUI/ModelIndex.cpp index bde5177596..da208d9a66 100644 --- a/Userland/Libraries/LibGUI/ModelIndex.cpp +++ b/Userland/Libraries/LibGUI/ModelIndex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,4 +39,19 @@ Variant ModelIndex::data(ModelRole role) const return model()->data(*this, role); } +ModelIndex ModelIndex::sibling(int row, int column) const +{ + if (!is_valid()) + return {}; + VERIFY(model()); + return model()->index(row, column, parent()); +} + +ModelIndex ModelIndex::sibling_at_column(int column) const +{ + if (!is_valid()) + return {}; + return sibling(row(), column); +} + } diff --git a/Userland/Libraries/LibGUI/ModelIndex.h b/Userland/Libraries/LibGUI/ModelIndex.h index 9a1ff1d6fd..30a85b43e8 100644 --- a/Userland/Libraries/LibGUI/ModelIndex.h +++ b/Userland/Libraries/LibGUI/ModelIndex.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -61,6 +61,9 @@ public: Variant data(ModelRole = ModelRole::Display) const; + ModelIndex sibling(int row, int column) const; + ModelIndex sibling_at_column(int column) const; + private: ModelIndex(const Model& model, int row, int column, void* internal_data) : m_model(&model)