Serenity Operating System
at master 96 lines 2.1 kB view raw
1/* 2 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibGUI/PersistentModelIndex.h> 8 9namespace GUI { 10 11PersistentModelIndex::PersistentModelIndex(ModelIndex const& index) 12{ 13 if (!index.is_valid()) 14 return; 15 16 auto* model = const_cast<Model*>(index.model()); 17 m_handle = model->register_persistent_index({}, index); 18} 19 20int PersistentModelIndex::row() const 21{ 22 if (!has_valid_handle()) 23 return -1; 24 return m_handle->m_index.row(); 25} 26 27int PersistentModelIndex::column() const 28{ 29 if (!has_valid_handle()) 30 return -1; 31 return m_handle->m_index.column(); 32} 33 34PersistentModelIndex PersistentModelIndex::parent() const 35{ 36 if (!has_valid_handle()) 37 return {}; 38 return { m_handle->m_index.parent() }; 39} 40 41PersistentModelIndex PersistentModelIndex::sibling_at_column(int column) const 42{ 43 if (!has_valid_handle()) 44 return {}; 45 46 return { m_handle->m_index.sibling_at_column(column) }; 47} 48 49Variant PersistentModelIndex::data(ModelRole role) const 50{ 51 if (!has_valid_handle()) 52 return {}; 53 return { m_handle->m_index.data(role) }; 54} 55 56PersistentModelIndex::operator ModelIndex() const 57{ 58 if (!has_valid_handle()) 59 return {}; 60 else 61 return m_handle->m_index; 62} 63 64bool PersistentModelIndex::operator==(PersistentModelIndex const& other) const 65{ 66 bool is_this_valid = has_valid_handle(); 67 bool is_other_valid = other.has_valid_handle(); 68 69 if (!is_this_valid && !is_other_valid) 70 return true; 71 if (is_this_valid != is_other_valid) 72 return false; 73 74 return m_handle->m_index == other.m_handle->m_index; 75} 76 77bool PersistentModelIndex::operator!=(PersistentModelIndex const& other) const 78{ 79 return !(*this == other); 80} 81 82bool PersistentModelIndex::operator==(ModelIndex const& other) const 83{ 84 if (!has_valid_handle()) { 85 return !other.is_valid(); 86 } 87 88 return m_handle->m_index == other; 89} 90 91bool PersistentModelIndex::operator!=(ModelIndex const& other) const 92{ 93 return !(*this == other); 94} 95 96}