forked from LadybirdBrowser/ancient-history
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ModelTranslator to translate a GUI::Model to a QAbstractItemModel
This will be used to expose the DOMTreeModel from LibWebView :^)
- Loading branch information
1 parent
22cd527
commit 289c43e
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2022, Andreas Kling <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#define AK_DONT_REPLACE_STD | ||
|
||
#include "ModelTranslator.h" | ||
#include "Utilities.h" | ||
#include <QIcon> | ||
|
||
namespace Ladybird { | ||
|
||
ModelTranslator::ModelTranslator(NonnullRefPtr<GUI::Model> model) | ||
: m_model(move(model)) | ||
{ | ||
} | ||
|
||
ModelTranslator::~ModelTranslator() = default; | ||
|
||
int ModelTranslator::columnCount(QModelIndex const& parent) const | ||
{ | ||
return m_model->column_count(to_gui(parent)); | ||
} | ||
|
||
int ModelTranslator::rowCount(QModelIndex const& parent) const | ||
{ | ||
return m_model->row_count(to_gui(parent)); | ||
} | ||
|
||
static QVariant convert_variant(GUI::Variant const& value) | ||
{ | ||
if (value.is_string()) | ||
return qstring_from_akstring(value.as_string()); | ||
if (value.is_icon()) { | ||
auto const& gui_icon = value.as_icon(); | ||
auto bitmap = gui_icon.bitmap_for_size(16); | ||
VERIFY(bitmap); | ||
auto qt_image = QImage(bitmap->scanline_u8(0), 16, 16, QImage::Format_ARGB32); | ||
QIcon qt_icon; | ||
qt_icon.addPixmap(QPixmap::fromImage(qt_image.convertToFormat(QImage::Format::Format_ARGB32_Premultiplied))); | ||
return qt_icon; | ||
} | ||
return {}; | ||
} | ||
|
||
QVariant ModelTranslator::data(QModelIndex const& index, int role) const | ||
{ | ||
switch (role) { | ||
case Qt::DisplayRole: | ||
return convert_variant(m_model->data(to_gui(index), GUI::ModelRole::Display)); | ||
case Qt::DecorationRole: | ||
return convert_variant(m_model->data(to_gui(index), GUI::ModelRole::Icon)); | ||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
QModelIndex ModelTranslator::index(int row, int column, QModelIndex const& parent) const | ||
{ | ||
return to_qt(m_model->index(row, column, to_gui(parent))); | ||
} | ||
|
||
QModelIndex ModelTranslator::parent(QModelIndex const& index) const | ||
{ | ||
return to_qt(m_model->parent_index(to_gui(index))); | ||
} | ||
|
||
QModelIndex ModelTranslator::to_qt(GUI::ModelIndex const& index) const | ||
{ | ||
if (!index.is_valid()) | ||
return {}; | ||
return createIndex(index.row(), index.column(), index.internal_data()); | ||
} | ||
|
||
GUI::ModelIndex ModelTranslator::to_gui(QModelIndex const& index) const | ||
{ | ||
if (!index.isValid()) | ||
return {}; | ||
return m_model->create_index_unsafe(index.row(), index.column(), index.internalPointer()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2022, Andreas Kling <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <LibGUI/Model.h> | ||
#include <QAbstractItemModel> | ||
|
||
namespace Ladybird { | ||
|
||
class ModelTranslator final : public QAbstractItemModel { | ||
Q_OBJECT | ||
public: | ||
explicit ModelTranslator(NonnullRefPtr<GUI::Model>); | ||
virtual ~ModelTranslator() override; | ||
|
||
virtual int columnCount(QModelIndex const& parent) const override; | ||
virtual int rowCount(QModelIndex const& parent) const override; | ||
virtual QVariant data(QModelIndex const&, int role) const override; | ||
virtual QModelIndex index(int row, int column, QModelIndex const& parent) const override; | ||
virtual QModelIndex parent(QModelIndex const& index) const override; | ||
|
||
QModelIndex to_qt(GUI::ModelIndex const&) const; | ||
GUI::ModelIndex to_gui(QModelIndex const&) const; | ||
|
||
private: | ||
RefPtr<GUI::Model> m_model; | ||
}; | ||
|
||
} |