瀏覽代碼

LibGUI: Add convenient helpers for getting the sibling of a ModelIndex

Andreas Kling 4 年之前
父節點
當前提交
0c02dfcad5
共有 2 個文件被更改,包括 20 次插入2 次删除
  1. 16 1
      Userland/Libraries/LibGUI/ModelIndex.cpp
  2. 4 1
      Userland/Libraries/LibGUI/ModelIndex.h

+ 16 - 1
Userland/Libraries/LibGUI/ModelIndex.cpp

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * 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);
+}
+
 }

+ 4 - 1
Userland/Libraries/LibGUI/ModelIndex.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * 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)