Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Some QML related refactoring #143

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions kitmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ QDataStream &operator>>(QDataStream &in, KitModel &kits)
unsigned int version;
in >> version;

kits.beginResetModel();

if(version == 1)
in >> kits.kits >> kits.nextID;
else
qWarning() << "Unknown KitModel serialization version " << version;

kits.endResetModel();

return in;
}

Expand All @@ -81,12 +85,8 @@ QHash<int, QByteArray> KitModel::roleNames() const

QVariant KitModel::data(const QModelIndex &index, int role) const
{
return getDataRow(index.row(), role);
}

QVariant KitModel::getDataRow(const int row, int role) const
{
if(row < 0 || row >= kits.count())
int row = index.row();
if(!index.isValid() || row < 0 || row >= kits.count())
return QVariant();

switch(role)
Expand All @@ -108,9 +108,10 @@ QVariant KitModel::getDataRow(const int row, int role) const
}
}

bool KitModel::setDataRow(const int row, const QVariant &value, int role)
bool KitModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(row < 0 || row >= kits.count())
int row = index.row();
if(!index.isValid() || row < 0 || row >= kits.count())
return false;

switch(role)
Expand Down Expand Up @@ -142,10 +143,10 @@ bool KitModel::setDataRow(const int row, const QVariant &value, int role)
{
// Refresh type as well
kits[row].type = typeForFlash(value.toString());
emit dataChanged(index(row), index(row), QVector<int>({FlashRole, TypeRole}));
emit dataChanged(index, index, QVector<int>({FlashRole, TypeRole}));
}
else
emit dataChanged(index(row), index(row), QVector<int>({role}));
emit dataChanged(index, index, QVector<int>({role}));

emit anythingChanged();
return true;
Expand All @@ -166,17 +167,18 @@ bool KitModel::copy(const int row)
return true;
}

bool KitModel::remove(const int row)
bool KitModel::removeRows(int row, int count, const QModelIndex &)
{
if(row < 0 || row >= kits.count())
if(row < 0 || row + count - 1 >= kits.count() || count < 1)
return false;

// Do not remove the last remaining kit
if(kits.count() == 1)
if(kits.count() == count)
return false;

beginRemoveRows({}, row, row);
kits.removeAt(row);
beginRemoveRows({}, row, row + count - 1);
while(count--)
kits.removeAt(row);
endRemoveRows();

emit anythingChanged();
Expand All @@ -194,11 +196,11 @@ void KitModel::addKit(QString name, QString boot1, QString flash, QString snapsh
emit anythingChanged();
}

int KitModel::indexForID(const unsigned int id)
QModelIndex KitModel::indexForID(const unsigned int id) const
{
auto it = std::find_if(kits.begin(), kits.end(),
[&] (const Kit &kit) { return kit.id == id; });
return it == kits.end() ? -1 : (it - kits.begin());
return it == kits.end() ? QModelIndex() : index(it - kits.begin());
}

bool KitModel::allKitsEmpty() const
Expand Down
9 changes: 4 additions & 5 deletions kitmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class KitModel : public QAbstractListModel
Q_OBJECT
public:
enum Role {
NameRole = Qt::DisplayRole,
IDRole = Qt::UserRole + 1,
NameRole,
TypeRole,
FlashRole,
Boot1Role,
Expand All @@ -30,12 +30,11 @@ class KitModel : public QAbstractListModel
Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_INVOKABLE virtual QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Q_INVOKABLE QVariant getDataRow(const int row, int role = Qt::DisplayRole) const;
Q_INVOKABLE bool setDataRow(const int row, const QVariant &value, int role = Qt::EditRole);
Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool copy(const int row);
Q_INVOKABLE bool remove(const int row);
Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE void addKit(QString name, QString boot1, QString flash, QString snapshot_path);
Q_INVOKABLE int indexForID(const unsigned int id);
Q_INVOKABLE QModelIndex indexForID(const unsigned int id) const;
Q_INVOKABLE bool allKitsEmpty() const;

QString typeForFlash(QString flash);
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ int main(int argc, char **argv)
qmlRegisterType<QMLFramebuffer>("Firebird.Emu", 1, 0, "EmuScreen");
// Register KitModel
qmlRegisterType<KitModel>("Firebird.Emu", 1, 0, "KitModel");
// Register Qt's stuff
qmlRegisterType<QSortFilterProxyModel>("Firebird.Emu", 1, 0, "QSortFilterProxyModel");

#ifndef MOBILE_UI
MainWindow mw;
Expand Down
8 changes: 4 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,16 +821,16 @@ void MainWindow::refillKitMenus()
action = ui->menuBoot_Diags_with_Kit->addAction(kit.name);
action->setData(kit.id);
connect(action, SIGNAL(triggered()), this, SLOT(startKitDiags()));
}
}
}

void MainWindow::updateWindowTitle()
{
// Need to update window title if kit is active
int kitIndex = the_qml_bridge->kitIndexForID(the_qml_bridge->getCurrentKitId());
if(kitIndex >= 0)
const QModelIndex kitIndex = the_qml_bridge->getKitModel()->indexForID(the_qml_bridge->getCurrentKitId());
if(kitIndex.isValid())
{
auto name = the_qml_bridge->getKitModel()->getKits()[kitIndex].name;
auto name = the_qml_bridge->getKitModel()->data(kitIndex, KitModel::NameRole).toString();
setWindowTitle(tr("Firebird Emu - %1").arg(name));
}
else
Expand Down
6 changes: 3 additions & 3 deletions qml/ConfigPageEmulation.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ ColumnLayout {
Layout.maximumWidth: parent.parent.width * 0.4
textRole: "name"
model: Emu.kits
currentIndex: Emu.kitIndexForID(Emu.defaultKit)
currentIndex: model.indexForID(Emu.defaultKit).row
onCurrentIndexChanged: {
Emu.defaultKit = model.getDataRow(currentIndex, KitModel.IDRole);
currentIndex = Qt.binding(function() { return Emu.kitIndexForID(Emu.defaultKit); });
Emu.defaultKit = model.data(model.index(currentIndex, 0), KitModel.IDRole);
currentIndex = Qt.binding(function() { return model.indexForID(Emu.defaultKit).row; });
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions qml/ConfigPageKits.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ColumnLayout {
text: kitList.currentItem.myData.name
onTextChanged: {
if(text !== kitList.currentItem.myData.name)
kitModel.setDataRow(kitList.currentIndex, text, KitModel.NameRole);
kitList.currentItem.myData.name = text;
text = Qt.binding(function() { return kitList.currentItem.myData.name; });
}
}
Expand All @@ -69,7 +69,7 @@ ColumnLayout {
filePath: kitList.currentItem.myData.boot1
onFilePathChanged: {
if(filePath !== kitList.currentItem.myData.boot1)
kitModel.setDataRow(kitList.currentIndex, filePath, KitModel.Boot1Role);
kitList.currentItem.myData.boot1 = filePath;
filePath = Qt.binding(function() { return kitList.currentItem.myData.boot1; });
}
}
Expand All @@ -85,7 +85,7 @@ ColumnLayout {
filePath: kitList.currentItem.myData.flash
onFilePathChanged: {
if(filePath !== kitList.currentItem.myData.flash)
kitModel.setDataRow(kitList.currentIndex, filePath, KitModel.FlashRole);
kitList.currentItem.myData.flash = filePath;
filePath = Qt.binding(function() { return kitList.currentItem.myData.flash; });
}
showCreateButton: true
Expand All @@ -95,7 +95,7 @@ ColumnLayout {
FlashDialog {
id: flashDialog
onFlashCreated: {
kitModel.setDataRow(kitList.currentIndex, filePath, KitModel.FlashRole);
kitList.currentItem.myData.flash = filePath;
}
}

Expand All @@ -111,7 +111,7 @@ ColumnLayout {
filePath: kitList.currentItem.myData.snapshot
onFilePathChanged: {
if(filePath !== kitList.currentItem.myData.snapshot)
kitModel.setDataRow(kitList.currentIndex, filePath, KitModel.SnapshotRole);
kitList.currentItem.myData.snapshot = filePath;
filePath = Qt.binding(function() { return kitList.currentItem.myData.snapshot; });
}
}
Expand Down
19 changes: 14 additions & 5 deletions qml/Firebird/UIComponents/KitList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Firebird.Emu 1.0
Rectangle {
property alias currentItem: listView.currentItem
property alias currentIndex: listView.currentIndex
property alias kitModel: listView.model
property alias kitModel: sortedModel.sourceModel

SystemPalette {
id: paletteActive
Expand All @@ -32,17 +32,27 @@ Rectangle {
highlightMoveDuration: 50
highlightResizeDuration: 0

QSortFilterProxyModel {
id: sortedModel
sortRole: KitModel.TypeRole
Component.onCompleted: Emu.sortProxyModel(sortedModel, 0)
}

model: sortedModel

highlight: Rectangle {
color: paletteActive.highlight
anchors.horizontalCenter: parent ? parent.horizontalCenter : undefined
}

delegate: Item {
property variant myData: model
property var myData: model;

height: item.height + 10
width: listView.width - listView.anchors.margins

property int kitIndex: sortedModel.mapToSource(sortedModel.index(index, 0)).row

MouseArea {
anchors.fill: parent
onClicked: function() {
Expand All @@ -66,7 +76,6 @@ Rectangle {
id: item
width: parent.width - 15
anchors.centerIn: parent

kitName: name
flashFile: Emu.basename(flash)
stateFile: Emu.basename(snapshot)
Expand All @@ -83,7 +92,7 @@ Rectangle {
text: qsTr("Remove")
visible: parent.ListView.view.currentIndex === index && parent.ListView.view.count > 1
onClicked: {
kitModel.remove(index)
kitModel.removeRows(kitIndex, 1)
}
}

Expand All @@ -100,7 +109,7 @@ Rectangle {
text: qsTr("Copy")
visible: parent.ListView.view.currentIndex === index
onClicked: {
kitModel.copy(index)
kitModel.copy(kitIndex)
}
}
}
Expand Down
Loading