summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/PersistentModelIndex.cpp
blob: 0312bf123e14182ca03222021c4a51aa9565f5ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * 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);
}

}