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-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-02-28 10:57:09 +01:00
|
|
|
class GTableView;
|
|
|
|
|
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-02-28 01:43:50 +01:00
|
|
|
class GTableModel {
|
|
|
|
public:
|
2019-02-28 11:27:04 +01:00
|
|
|
struct ColumnMetadata {
|
|
|
|
int preferred_width { 0 };
|
|
|
|
TextAlignment text_alignment { TextAlignment::CenterLeft };
|
|
|
|
};
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
virtual ~GTableModel();
|
2019-02-28 01:43:50 +01:00
|
|
|
|
|
|
|
virtual int row_count() const = 0;
|
|
|
|
virtual int column_count() const = 0;
|
|
|
|
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-02-28 16:20:29 +01:00
|
|
|
virtual GVariant data(int row, int column) const = 0;
|
2019-02-28 01:43:50 +01:00
|
|
|
virtual void update() = 0;
|
2019-03-01 13:48:08 +01:00
|
|
|
virtual void activate(const GModelIndex&) { }
|
2019-02-28 10:20:04 +01:00
|
|
|
|
|
|
|
bool is_valid(GModelIndex index) const
|
|
|
|
{
|
|
|
|
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-04 10:18:05 +01:00
|
|
|
void set_selected_index(const GModelIndex& index) { m_selected_index = index; }
|
2019-03-01 13:03:13 +01:00
|
|
|
GModelIndex selected_index() const { return m_selected_index; }
|
|
|
|
|
2019-02-28 10:57:09 +01:00
|
|
|
void register_view(Badge<GTableView>, GTableView&);
|
|
|
|
void unregister_view(Badge<GTableView>, GTableView&);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GTableModel();
|
|
|
|
|
|
|
|
void for_each_view(Function<void(GTableView&)>);
|
|
|
|
void did_update();
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashTable<GTableView*> m_views;
|
2019-03-01 13:03:13 +01:00
|
|
|
GModelIndex m_selected_index;
|
2019-02-28 01:43:50 +01:00
|
|
|
};
|