fix: improve code readability

Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
This commit is contained in:
Sefa Eyeoglu 2022-10-01 18:13:52 +02:00
parent 493a951ecb
commit 1e8c0ac36a
No known key found for this signature in database
GPG Key ID: C10411294912A422
2 changed files with 30 additions and 18 deletions

View File

@ -23,6 +23,7 @@
*/ */
#include "InstanceView.h" #include "InstanceView.h"
#include <qabstractitemmodel.h>
#include "InstanceList.h" #include "InstanceList.h"
#include "ui/instanceview/InstanceProxyModel.h" #include "ui/instanceview/InstanceProxyModel.h"
@ -76,33 +77,38 @@ void InstanceView::createTable() {
m_table->setColumnWidth(InstanceList::Icon, m_rowHeight + 3 + 3); // padding left and right m_table->setColumnWidth(InstanceList::Icon, m_rowHeight + 3 + 3); // padding left and right
m_table->verticalHeader()->setDefaultSectionSize(m_rowHeight + 1 + 1); // padding top and bottom m_table->verticalHeader()->setDefaultSectionSize(m_rowHeight + 1 + 1); // padding top and bottom
connect(m_table, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) { connect(m_table, &QTableView::doubleClicked, this, &InstanceView::activateInstance);
int row = m_proxy->mapToSource(idx).row();
emit instanceActivated(m_instances->at(row));
});
connect(m_table->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &InstanceView::currentRowChanged); connect(m_table->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &InstanceView::currentRowChanged);
connect(m_table->selectionModel(), &QItemSelectionModel::currentColumnChanged, this, &InstanceView::selectNameColumn); connect(m_table->selectionModel(), &QItemSelectionModel::currentColumnChanged, this, &InstanceView::selectNameColumn);
} }
InstancePtr InstanceView::currentInstance() { InstancePtr InstanceView::currentInstance() {
auto current = m_table->selectionModel()->currentIndex(); auto current = m_table->selectionModel()->currentIndex();
int row = m_proxy->mapToSource(current).row(); if (current.isValid()) {
return m_instances->at(row); int row = mappedIndex(current).row();
return m_instances->at(row);
}
return nullptr;
}
void InstanceView::activateInstance(const QModelIndex &index) {
if (index.isValid()) {
int row = mappedIndex(index).row();
emit instanceActivated(m_instances->at(row));
}
} }
void InstanceView::currentRowChanged(const QModelIndex &current, const QModelIndex &previous) { void InstanceView::currentRowChanged(const QModelIndex &current, const QModelIndex &previous) {
{ InstancePtr inst1, inst2;
InstancePtr inst1, inst2; if (current.isValid()) {
if (current.isValid()) { int row = mappedIndex(current).row();
int row = m_proxy->mapToSource(current).row(); inst1 = m_instances->at(row);
inst1 = m_instances->at(row);
}
if (previous.isValid()) {
int row = m_proxy->mapToSource(previous).row();
inst2 = m_instances->at(row);
}
emit currentInstanceChanged(inst1, inst2);
} }
if (previous.isValid()) {
int row = mappedIndex(previous).row();
inst2 = m_instances->at(row);
}
emit currentInstanceChanged(inst1, inst2);
} }
void InstanceView::selectNameColumn(const QModelIndex &current, const QModelIndex &previous) { void InstanceView::selectNameColumn(const QModelIndex &current, const QModelIndex &previous) {
@ -116,8 +122,12 @@ void InstanceView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bo
QItemSelection foo(topLeft, bottomRight); QItemSelection foo(topLeft, bottomRight);
if (foo.contains(current)) { if (foo.contains(current)) {
int row = m_proxy->mapToSource(current).row(); int row = mappedIndex(current).row();
InstancePtr inst = m_instances->at(row); InstancePtr inst = m_instances->at(row);
emit currentInstanceChanged(inst, inst); emit currentInstanceChanged(inst, inst);
} }
} }
QModelIndex InstanceView::mappedIndex(const QModelIndex& index) const {
return m_proxy->mapToSource(index);
}

View File

@ -43,6 +43,7 @@ signals:
void currentInstanceChanged(InstancePtr current, InstancePtr previous); void currentInstanceChanged(InstancePtr current, InstancePtr previous);
private slots: private slots:
void activateInstance(const QModelIndex& index);
void currentRowChanged(const QModelIndex& current, const QModelIndex& previous); void currentRowChanged(const QModelIndex& current, const QModelIndex& previous);
void selectNameColumn(const QModelIndex& current, const QModelIndex& previous); void selectNameColumn(const QModelIndex& current, const QModelIndex& previous);
// emits currentRowChanged if a data update affected the current instance // emits currentRowChanged if a data update affected the current instance
@ -51,6 +52,7 @@ private slots:
private: private:
void createTable(); void createTable();
void prepareModel(); void prepareModel();
QModelIndex mappedIndex(const QModelIndex& index) const;
int m_rowHeight = 48; int m_rowHeight = 48;