2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-04-11 12:14:46 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-02-28 01:43:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-01-21 20:55:37 +01:00
|
|
|
#include <AK/Format.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <AK/Traits.h>
|
|
|
|
|
#include <LibGUI/Forward.h>
|
2020-08-16 16:02:01 +02:00
|
|
|
#include <LibGUI/ModelRole.h>
|
2019-08-18 10:14:26 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-03-29 04:58:15 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class ModelIndex {
|
|
|
|
|
friend class Model;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2019-02-28 01:43:50 +01:00
|
|
|
public:
|
2021-09-15 23:57:01 -07:00
|
|
|
ModelIndex() = default;
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2020-08-16 16:25:42 +02:00
|
|
|
bool is_valid() const { return m_model && m_row != -1 && m_column != -1; }
|
2019-02-28 01:43:50 +01:00
|
|
|
int row() const { return m_row; }
|
|
|
|
|
int column() const { return m_column; }
|
|
|
|
|
|
2019-03-29 04:58:15 +01:00
|
|
|
void* internal_data() const { return m_internal_data; }
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
ModelIndex parent() const;
|
2022-04-01 20:58:27 +03:00
|
|
|
bool is_parent_of(ModelIndex const&) const;
|
2019-03-29 14:46:53 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(ModelIndex const& other) const
|
2019-03-29 20:36:15 +01:00
|
|
|
{
|
|
|
|
|
return m_model == other.m_model && m_row == other.m_row && m_column == other.m_column && m_internal_data == other.m_internal_data;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator!=(ModelIndex const& other) const
|
2019-03-29 20:36:15 +01:00
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
2019-03-15 16:25:30 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Model const* model() const { return m_model; }
|
2020-08-13 20:27:54 +02:00
|
|
|
|
2020-08-16 16:02:01 +02:00
|
|
|
Variant data(ModelRole = ModelRole::Display) const;
|
2021-04-11 12:14:46 +02:00
|
|
|
|
|
|
|
|
ModelIndex sibling(int row, int column) const;
|
|
|
|
|
ModelIndex sibling_at_column(int column) const;
|
2020-08-16 16:02:01 +02:00
|
|
|
|
2019-02-28 01:43:50 +01:00
|
|
|
private:
|
2022-04-01 20:58:27 +03:00
|
|
|
ModelIndex(Model const& model, int row, int column, void* internal_data)
|
2019-03-29 04:58:15 +01:00
|
|
|
: m_model(&model)
|
|
|
|
|
, m_row(row)
|
|
|
|
|
, m_column(column)
|
|
|
|
|
, m_internal_data(internal_data)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Model const* m_model { nullptr };
|
2019-02-28 01:43:50 +01:00
|
|
|
int m_row { -1 };
|
|
|
|
|
int m_column { -1 };
|
2019-03-29 04:58:15 +01:00
|
|
|
void* m_internal_data { nullptr };
|
2019-02-28 01:43:50 +01:00
|
|
|
};
|
2019-08-18 10:14:26 +02:00
|
|
|
|
|
|
|
|
}
|
2019-09-07 19:17:03 +02:00
|
|
|
|
|
|
|
|
namespace AK {
|
2020-10-05 18:16:10 +02:00
|
|
|
|
|
|
|
|
template<>
|
2021-01-09 01:00:22 +01:00
|
|
|
struct Formatter<GUI::ModelIndex> : Formatter<FormatString> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, GUI::ModelIndex const& value)
|
2021-01-09 01:00:22 +01:00
|
|
|
{
|
|
|
|
|
if (value.internal_data())
|
2022-07-11 17:32:29 +00:00
|
|
|
return Formatter<FormatString>::format(builder, "ModelIndex({},{},{})"sv, value.row(), value.column(), value.internal_data());
|
|
|
|
|
return Formatter<FormatString>::format(builder, "ModelIndex({},{})"sv, value.row(), value.column());
|
2021-01-09 01:00:22 +01:00
|
|
|
}
|
2020-10-05 18:16:10 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-07 19:17:03 +02:00
|
|
|
template<>
|
2020-02-02 15:07:41 +01:00
|
|
|
struct Traits<GUI::ModelIndex> : public GenericTraits<GUI::ModelIndex> {
|
2021-05-13 09:07:43 +00:00
|
|
|
static unsigned hash(const GUI::ModelIndex& index)
|
|
|
|
|
{
|
|
|
|
|
return pair_int_hash(pair_int_hash(index.row(), index.column()), reinterpret_cast<FlatPtr>(index.internal_data()));
|
|
|
|
|
}
|
2019-09-07 19:17:03 +02:00
|
|
|
};
|
2020-10-05 18:16:10 +02:00
|
|
|
|
2019-09-07 19:17:03 +02:00
|
|
|
}
|