2019-02-28 01:43:50 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/AKString.h>
|
2019-02-28 10:57:09 +01:00
|
|
|
#include <AK/Badge.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashTable.h>
|
2019-04-19 00:07:33 +02:00
|
|
|
#include <AK/Retainable.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 11:27:04 +01:00
|
|
|
#include <SharedGraphics/TextAlignment.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-03-09 13:33:52 +01:00
|
|
|
enum class GSortOrder { None, Ascending, Descending };
|
|
|
|
|
2019-02-28 21:30:17 +01:00
|
|
|
class GModelNotification {
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
Invalid = 0,
|
|
|
|
ModelUpdated,
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit GModelNotification(Type type, const GModelIndex& index = GModelIndex())
|
|
|
|
: m_type(type)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
|
|
|
GModelIndex index() const { return m_index; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type m_type { Invalid };
|
|
|
|
GModelIndex m_index;
|
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:49 +01:00
|
|
|
class GModel : public Retainable<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 };
|
2019-02-28 11:27:04 +01:00
|
|
|
};
|
|
|
|
|
2019-03-23 03:53:51 +01:00
|
|
|
enum class Role { Display, Sort, Custom, ForegroundColor, BackgroundColor, Icon };
|
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-02-28 01:43:50 +01:00
|
|
|
virtual String row_name(int) const { return { }; }
|
|
|
|
virtual String column_name(int) const { return { }; }
|
2019-02-28 11:27:04 +01:00
|
|
|
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-03-29 17:03:30 +01:00
|
|
|
virtual GModelIndex parent_index(const GModelIndex&) const { return { }; }
|
|
|
|
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; }
|
|
|
|
virtual void set_data(const GModelIndex&, const GVariant&) { }
|
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
|
|
|
{
|
|
|
|
return index.row() >= 0 && index.row() < row_count() && index.column() >= 0 && index.column() < column_count();
|
|
|
|
}
|
2019-02-28 10:57:09 +01:00
|
|
|
|
2019-03-15 16:25:30 +01:00
|
|
|
void set_selected_index(const GModelIndex&);
|
2019-03-01 13:03:13 +01:00
|
|
|
GModelIndex selected_index() const { return m_selected_index; }
|
|
|
|
|
2019-03-09 13:33:52 +01:00
|
|
|
virtual int key_column() const { return -1; }
|
|
|
|
virtual GSortOrder sort_order() const { return GSortOrder::None; }
|
|
|
|
virtual void set_key_column_and_sort_order(int, GSortOrder) { }
|
|
|
|
|
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-03-23 01:42:49 +01:00
|
|
|
Function<void(GModel&)> on_model_update;
|
2019-03-15 16:25:30 +01:00
|
|
|
Function<void(const GModelIndex&)> on_selection_changed;
|
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-03-29 04:58:15 +01:00
|
|
|
GModelIndex create_index(int row, int column, void* data = nullptr) const;
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
private:
|
2019-03-23 02:04:31 +01:00
|
|
|
HashTable<GAbstractView*> m_views;
|
2019-03-01 13:03:13 +01:00
|
|
|
GModelIndex m_selected_index;
|
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();
|
|
|
|
}
|