2019-02-28 01:43:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
#include <AK/Badge.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashTable.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2020-01-07 22:43:31 +11:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <LibDraw/TextAlignment.h>
|
2019-02-28 01:43:50 +01:00
|
|
|
#include <LibGUI/GModelIndex.h>
|
2019-02-28 16:20:29 +01:00
|
|
|
#include <LibGUI/GVariant.h>
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2019-03-15 17:37:13 +01:00
|
|
|
class Font;
|
2019-03-23 02:04:31 +01:00
|
|
|
class GAbstractView;
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class GSortOrder {
|
2019-05-28 11:53:16 +02:00
|
|
|
None,
|
|
|
|
Ascending,
|
|
|
|
Descending
|
|
|
|
};
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
class GModel : public RefCounted<GModel> {
|
2019-02-28 01:43:50 +01:00
|
|
|
public:
|
2019-02-28 11:27:04 +01:00
|
|
|
struct ColumnMetadata {
|
|
|
|
int preferred_width { 0 };
|
|
|
|
TextAlignment text_alignment { TextAlignment::CenterLeft };
|
2019-03-15 17:37:13 +01:00
|
|
|
const Font* font { nullptr };
|
2020-01-07 22:43:31 +11:00
|
|
|
enum class Sortable {
|
|
|
|
False,
|
|
|
|
True,
|
|
|
|
};
|
|
|
|
Sortable sortable { Sortable::True };
|
2019-02-28 11:27:04 +01:00
|
|
|
};
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class Role {
|
2019-05-28 11:53:16 +02:00
|
|
|
Display,
|
|
|
|
Sort,
|
|
|
|
Custom,
|
|
|
|
ForegroundColor,
|
|
|
|
BackgroundColor,
|
2019-10-22 21:38:04 +02:00
|
|
|
Icon,
|
|
|
|
Font,
|
2019-12-19 20:09:31 +01:00
|
|
|
DragData,
|
2019-05-28 11:53:16 +02:00
|
|
|
};
|
2019-03-09 14:24:34 +01:00
|
|
|
|
2019-03-23 01:42:49 +01:00
|
|
|
virtual ~GModel();
|
2019-02-28 01:43:50 +01:00
|
|
|
|
2019-03-29 03:27:03 +01:00
|
|
|
virtual int row_count(const GModelIndex& = GModelIndex()) const = 0;
|
|
|
|
virtual int column_count(const GModelIndex& = GModelIndex()) const = 0;
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual String row_name(int) const { return {}; }
|
|
|
|
virtual String column_name(int) const { return {}; }
|
|
|
|
virtual ColumnMetadata column_metadata(int) const { return {}; }
|
2019-03-09 14:24:34 +01:00
|
|
|
virtual GVariant data(const GModelIndex&, Role = Role::Display) const = 0;
|
2019-02-28 01:43:50 +01:00
|
|
|
virtual void update() = 0;
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual GModelIndex parent_index(const GModelIndex&) const { return {}; }
|
2019-03-29 17:03:30 +01:00
|
|
|
virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
|
2019-03-29 19:48:15 +01:00
|
|
|
virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
|
2019-04-18 22:27:14 +02:00
|
|
|
virtual bool is_editable(const GModelIndex&) const { return false; }
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual void set_data(const GModelIndex&, const GVariant&) {}
|
2019-12-13 23:36:36 +01:00
|
|
|
virtual int tree_column() const { return 0; }
|
2019-02-28 10:20:04 +01:00
|
|
|
|
2019-03-06 19:56:47 +01:00
|
|
|
bool is_valid(const GModelIndex& index) const
|
2019-02-28 10:20:04 +01:00
|
|
|
{
|
2020-01-10 18:48:27 +03:00
|
|
|
auto parent_index = this->parent_index(index);
|
|
|
|
return index.row() >= 0 && index.row() < row_count(parent_index) && index.column() >= 0 && index.column() < column_count(parent_index);
|
2019-02-28 10:20:04 +01:00
|
|
|
}
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2019-03-09 13:33:52 +01:00
|
|
|
virtual int key_column() const { return -1; }
|
|
|
|
virtual GSortOrder sort_order() const { return GSortOrder::None; }
|
2019-05-28 11:53:16 +02:00
|
|
|
virtual void set_key_column_and_sort_order(int, GSortOrder) {}
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
void register_view(Badge<GAbstractView>, GAbstractView&);
|
|
|
|
void unregister_view(Badge<GAbstractView>, GAbstractView&);
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2019-08-20 19:45:08 +02:00
|
|
|
Function<void()> on_update;
|
2019-03-09 13:33:52 +01:00
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
protected:
|
2019-03-23 01:42:49 +01:00
|
|
|
GModel();
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2019-03-23 02:04:31 +01:00
|
|
|
void for_each_view(Function<void(GAbstractView&)>);
|
2019-02-28 10:57:09 +01:00
|
|
|
void did_update();
|
|
|
|
|
2019-08-18 10:14:53 +02:00
|
|
|
GModelIndex create_index(int row, int column, const void* data = nullptr) const;
|
2019-03-29 04:58:15 +01:00
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
private:
|
2019-03-23 02:04:31 +01:00
|
|
|
HashTable<GAbstractView*> m_views;
|
2019-02-28 01:43:50 +01:00
|
|
|
};
|
2019-03-29 14:46:53 +01:00
|
|
|
|
|
|
|
inline GModelIndex GModelIndex::parent() const
|
|
|
|
{
|
|
|
|
return m_model ? m_model->parent_index(*this) : GModelIndex();
|
|
|
|
}
|