mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 12:02:43 +00:00 
			
		
		
		
	 d73116e5d5
			
		
	
	
		d73116e5d5
		
	
	
	
	
		
			
			This patch adds persistent indices to models. A PersistentModelIndex is a ModelIndex that will survive most model updates (provided that the data the PersistentModelIndex points to has not been removed by the model's data store). PersistentModelIndex objects can be safely held by objects outside the model they originated from.
		
			
				
	
	
		
			96 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			96 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibGUI/PersistentModelIndex.h>
 | |
| 
 | |
| namespace GUI {
 | |
| 
 | |
| PersistentModelIndex::PersistentModelIndex(ModelIndex const& index)
 | |
| {
 | |
|     if (!index.is_valid())
 | |
|         return;
 | |
| 
 | |
|     auto* model = const_cast<Model*>(index.model());
 | |
|     m_handle = model->register_persistent_index({}, index);
 | |
| }
 | |
| 
 | |
| int PersistentModelIndex::row() const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return -1;
 | |
|     return m_handle->m_index.row();
 | |
| }
 | |
| 
 | |
| int PersistentModelIndex::column() const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return -1;
 | |
|     return m_handle->m_index.column();
 | |
| }
 | |
| 
 | |
| PersistentModelIndex PersistentModelIndex::parent() const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return {};
 | |
|     return { m_handle->m_index.parent() };
 | |
| }
 | |
| 
 | |
| PersistentModelIndex PersistentModelIndex::sibling_at_column(int column) const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return {};
 | |
| 
 | |
|     return { m_handle->m_index.sibling_at_column(column) };
 | |
| }
 | |
| 
 | |
| Variant PersistentModelIndex::data(ModelRole role) const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return {};
 | |
|     return { m_handle->m_index.data(role) };
 | |
| }
 | |
| 
 | |
| PersistentModelIndex::operator ModelIndex() const
 | |
| {
 | |
|     if (!has_valid_handle())
 | |
|         return {};
 | |
|     else
 | |
|         return m_handle->m_index;
 | |
| }
 | |
| 
 | |
| bool PersistentModelIndex::operator==(PersistentModelIndex const& other) const
 | |
| {
 | |
|     bool is_this_valid = has_valid_handle();
 | |
|     bool is_other_valid = other.has_valid_handle();
 | |
| 
 | |
|     if (!is_this_valid && !is_other_valid)
 | |
|         return true;
 | |
|     if (is_this_valid != is_other_valid)
 | |
|         return false;
 | |
| 
 | |
|     return m_handle->m_index == other.m_handle->m_index;
 | |
| }
 | |
| 
 | |
| bool PersistentModelIndex::operator!=(PersistentModelIndex const& other) const
 | |
| {
 | |
|     return !(*this == other);
 | |
| }
 | |
| 
 | |
| bool PersistentModelIndex::operator==(ModelIndex const& other) const
 | |
| {
 | |
|     if (!has_valid_handle()) {
 | |
|         return !other.is_valid();
 | |
|     }
 | |
| 
 | |
|     return m_handle->m_index == other;
 | |
| }
 | |
| 
 | |
| bool PersistentModelIndex::operator!=(ModelIndex const& other) const
 | |
| {
 | |
|     return !(*this == other);
 | |
| }
 | |
| 
 | |
| }
 |