Skip to content

Commit 0313814

Browse files
MacDueADKaster
authored andcommitted
Ladybird: Allow replacing underlying model of ModelTranslator
1 parent 419dea0 commit 0313814

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Ladybird/ModelTranslator.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212

1313
namespace Ladybird {
1414

15-
ModelTranslator::ModelTranslator(NonnullRefPtr<GUI::Model> model)
16-
: m_model(move(model))
17-
{
18-
}
19-
2015
ModelTranslator::~ModelTranslator() = default;
2116

2217
int ModelTranslator::columnCount(QModelIndex const& parent) const
2318
{
19+
if (!m_model)
20+
return 0;
2421
return m_model->column_count(to_gui(parent));
2522
}
2623

2724
int ModelTranslator::rowCount(QModelIndex const& parent) const
2825
{
26+
if (!m_model)
27+
return 0;
2928
return m_model->row_count(to_gui(parent));
3029
}
3130

@@ -47,6 +46,7 @@ static QVariant convert_variant(GUI::Variant const& value)
4746

4847
QVariant ModelTranslator::data(QModelIndex const& index, int role) const
4948
{
49+
VERIFY(m_model);
5050
switch (role) {
5151
case Qt::DisplayRole:
5252
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
5959

6060
QModelIndex ModelTranslator::index(int row, int column, QModelIndex const& parent) const
6161
{
62+
VERIFY(m_model);
6263
return to_qt(m_model->index(row, column, to_gui(parent)));
6364
}
6465

6566
QModelIndex ModelTranslator::parent(QModelIndex const& index) const
6667
{
68+
VERIFY(m_model);
6769
return to_qt(m_model->parent_index(to_gui(index)));
6870
}
6971

@@ -76,6 +78,7 @@ QModelIndex ModelTranslator::to_qt(GUI::ModelIndex const& index) const
7678

7779
GUI::ModelIndex ModelTranslator::to_gui(QModelIndex const& index) const
7880
{
81+
VERIFY(m_model);
7982
if (!index.isValid())
8083
return {};
8184
return m_model->unsafe_create_index(index.row(), index.column(), index.internalPointer());

Ladybird/ModelTranslator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ namespace Ladybird {
1414
class ModelTranslator final : public QAbstractItemModel {
1515
Q_OBJECT
1616
public:
17-
explicit ModelTranslator(NonnullRefPtr<GUI::Model>);
1817
virtual ~ModelTranslator() override;
1918

19+
void set_underlying_model(RefPtr<GUI::Model> model)
20+
{
21+
beginResetModel();
22+
m_model = model;
23+
endResetModel();
24+
}
25+
2026
virtual int columnCount(QModelIndex const& parent) const override;
2127
virtual int rowCount(QModelIndex const& parent) const override;
2228
virtual QVariant data(QModelIndex const&, int role) const override;

0 commit comments

Comments
 (0)