mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
Ladybird: Allow replacing underlying model of ModelTranslator
This commit is contained in:
parent
419dea0996
commit
0313814d3b
Notes:
sideshowbarker
2024-07-17 02:37:12 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/0313814d3b Pull-request: https://github.com/SerenityOS/serenity/pull/16665
2 changed files with 15 additions and 6 deletions
|
@ -12,20 +12,19 @@
|
||||||
|
|
||||||
namespace Ladybird {
|
namespace Ladybird {
|
||||||
|
|
||||||
ModelTranslator::ModelTranslator(NonnullRefPtr<GUI::Model> model)
|
|
||||||
: m_model(move(model))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ModelTranslator::~ModelTranslator() = default;
|
ModelTranslator::~ModelTranslator() = default;
|
||||||
|
|
||||||
int ModelTranslator::columnCount(QModelIndex const& parent) const
|
int ModelTranslator::columnCount(QModelIndex const& parent) const
|
||||||
{
|
{
|
||||||
|
if (!m_model)
|
||||||
|
return 0;
|
||||||
return m_model->column_count(to_gui(parent));
|
return m_model->column_count(to_gui(parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
int ModelTranslator::rowCount(QModelIndex const& parent) const
|
int ModelTranslator::rowCount(QModelIndex const& parent) const
|
||||||
{
|
{
|
||||||
|
if (!m_model)
|
||||||
|
return 0;
|
||||||
return m_model->row_count(to_gui(parent));
|
return m_model->row_count(to_gui(parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +46,7 @@ static QVariant convert_variant(GUI::Variant const& value)
|
||||||
|
|
||||||
QVariant ModelTranslator::data(QModelIndex const& index, int role) const
|
QVariant ModelTranslator::data(QModelIndex const& index, int role) const
|
||||||
{
|
{
|
||||||
|
VERIFY(m_model);
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
return convert_variant(m_model->data(to_gui(index), GUI::ModelRole::Display));
|
return convert_variant(m_model->data(to_gui(index), GUI::ModelRole::Display));
|
||||||
|
@ -59,11 +59,13 @@ QVariant ModelTranslator::data(QModelIndex const& index, int role) const
|
||||||
|
|
||||||
QModelIndex ModelTranslator::index(int row, int column, QModelIndex const& parent) const
|
QModelIndex ModelTranslator::index(int row, int column, QModelIndex const& parent) const
|
||||||
{
|
{
|
||||||
|
VERIFY(m_model);
|
||||||
return to_qt(m_model->index(row, column, to_gui(parent)));
|
return to_qt(m_model->index(row, column, to_gui(parent)));
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex ModelTranslator::parent(QModelIndex const& index) const
|
QModelIndex ModelTranslator::parent(QModelIndex const& index) const
|
||||||
{
|
{
|
||||||
|
VERIFY(m_model);
|
||||||
return to_qt(m_model->parent_index(to_gui(index)));
|
return to_qt(m_model->parent_index(to_gui(index)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +78,7 @@ QModelIndex ModelTranslator::to_qt(GUI::ModelIndex const& index) const
|
||||||
|
|
||||||
GUI::ModelIndex ModelTranslator::to_gui(QModelIndex const& index) const
|
GUI::ModelIndex ModelTranslator::to_gui(QModelIndex const& index) const
|
||||||
{
|
{
|
||||||
|
VERIFY(m_model);
|
||||||
if (!index.isValid())
|
if (!index.isValid())
|
||||||
return {};
|
return {};
|
||||||
return m_model->unsafe_create_index(index.row(), index.column(), index.internalPointer());
|
return m_model->unsafe_create_index(index.row(), index.column(), index.internalPointer());
|
||||||
|
|
|
@ -14,9 +14,15 @@ namespace Ladybird {
|
||||||
class ModelTranslator final : public QAbstractItemModel {
|
class ModelTranslator final : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ModelTranslator(NonnullRefPtr<GUI::Model>);
|
|
||||||
virtual ~ModelTranslator() override;
|
virtual ~ModelTranslator() override;
|
||||||
|
|
||||||
|
void set_underlying_model(RefPtr<GUI::Model> model)
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
m_model = model;
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
virtual int columnCount(QModelIndex const& parent) const override;
|
virtual int columnCount(QModelIndex const& parent) const override;
|
||||||
virtual int rowCount(QModelIndex const& parent) const override;
|
virtual int rowCount(QModelIndex const& parent) const override;
|
||||||
virtual QVariant data(QModelIndex const&, int role) const override;
|
virtual QVariant data(QModelIndex const&, int role) const override;
|
||||||
|
|
Loading…
Reference in a new issue