Added a second bound property (nExpectedBiddings) & cleaned up the code a little.
This commit is contained in:
@ -9,22 +9,44 @@
|
||||
SummaryWidget::SummaryWidget(std::shared_ptr<ModelSummary> modelSummary, QWidget* parent)
|
||||
: QWidget{parent}
|
||||
, m_modelSummary(modelSummary) {
|
||||
/// Layouting
|
||||
QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
||||
|
||||
QHBoxLayout* rowCountLayout = new QHBoxLayout();
|
||||
QLabel* rowCountLabel = new QLabel("Row count:");
|
||||
m_rowCountValueLabel = new QLabel("");
|
||||
rowCountLayout->addWidget(rowCountLabel);
|
||||
rowCountLayout->addWidget(m_rowCountValueLabel);
|
||||
|
||||
QHBoxLayout* nExpectedBiddingsLayout = new QHBoxLayout();
|
||||
QLabel* nExpectedBiddingsLabel = new QLabel("Expected biddings:");
|
||||
m_nExpectedBiddingsValueLabel = new QLabel("");
|
||||
nExpectedBiddingsLayout->addWidget(nExpectedBiddingsLabel);
|
||||
nExpectedBiddingsLayout->addWidget(m_nExpectedBiddingsValueLabel);
|
||||
|
||||
mainLayout->addLayout(rowCountLayout);
|
||||
mainLayout->addLayout(nExpectedBiddingsLayout);
|
||||
|
||||
setLayout(mainLayout);
|
||||
|
||||
setupBindableProperties();
|
||||
}
|
||||
|
||||
void SummaryWidget::setupBindableProperties() {
|
||||
// TODO figure out how to encapsulate each property binding into a dedicated function:
|
||||
// "bindProperty(&bindable, &signal, &getter, widget)"
|
||||
/// nRows
|
||||
QProperty<int> nRows(-1);
|
||||
|
||||
QHBoxLayout* footerLayout = new QHBoxLayout();
|
||||
QLabel* rowCountLabel = new QLabel("Row count:");
|
||||
m_rowCoundValueLabel = new QLabel(QString::number(nRows));
|
||||
|
||||
QObject::connect(m_modelSummary.get(), &ModelSummary::rowCountChanged, [&]() {
|
||||
m_rowCoundValueLabel->setText(QString::number(m_modelSummary->rowCount()));
|
||||
m_rowCountValueLabel->setText(QString::number(m_modelSummary->rowCount()));
|
||||
});
|
||||
m_modelSummary->bindableRowCount().setBinding([&]() { return nRows.value(); });
|
||||
|
||||
footerLayout->addWidget(rowCountLabel);
|
||||
footerLayout->addWidget(m_rowCoundValueLabel);
|
||||
mainLayout->addLayout(footerLayout);
|
||||
|
||||
setLayout(mainLayout);
|
||||
/// nExpectedBiddings
|
||||
QProperty<int> nExpectedBiddings(-1);
|
||||
QObject::connect(m_modelSummary.get(), &ModelSummary::nExpectedBiddingsChanged, [&]() {
|
||||
m_nExpectedBiddingsValueLabel->setText(QString::number(m_modelSummary->nExpectedBiddings()));
|
||||
});
|
||||
m_modelSummary->bindableNExpectedBiddings().setBinding(
|
||||
[&]() { return nExpectedBiddings.value(); });
|
||||
}
|
||||
|
||||
@ -15,7 +15,10 @@ class SummaryWidget : public QWidget {
|
||||
private:
|
||||
std::shared_ptr<ModelSummary> m_modelSummary;
|
||||
|
||||
QLabel* m_rowCoundValueLabel;
|
||||
QLabel* m_rowCountValueLabel;
|
||||
QLabel* m_nExpectedBiddingsValueLabel;
|
||||
|
||||
void setupBindableProperties();
|
||||
};
|
||||
|
||||
#endif // SUMMARYWIDGET_H
|
||||
|
||||
@ -7,6 +7,9 @@ ModelSummary::ModelSummary(std::shared_ptr<TableModel> model, QObject* parent)
|
||||
, m_model(model) {
|
||||
Q_ASSERT(model);
|
||||
connect(m_model.get(), &TableModel::rowCountChanged, this, &ModelSummary::rowCountChanged);
|
||||
// TODO ? use existing model signals (dataChanged(role),...) instead of special signals
|
||||
connect(m_model.get(), &TableModel::nExpectedBiddingsChanged, this,
|
||||
&ModelSummary::nExpectedBiddingsChanged);
|
||||
}
|
||||
|
||||
ModelSummary::~ModelSummary() {}
|
||||
@ -20,3 +23,10 @@ QBindable<int> ModelSummary::bindableRowCount() {
|
||||
m_rowCount = m_model->rowCount();
|
||||
return &m_rowCount;
|
||||
}
|
||||
|
||||
int ModelSummary::nExpectedBiddings() const { return m_model->nExpectedBiddings(); }
|
||||
|
||||
QBindable<int> ModelSummary::bindableNExpectedBiddings() {
|
||||
m_nExpectedBiddings = m_model->nExpectedBiddings();
|
||||
return &m_nExpectedBiddings;
|
||||
}
|
||||
|
||||
@ -12,6 +12,8 @@ class ModelSummary : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged BINDABLE bindableRowCount)
|
||||
Q_PROPERTY(int nExpectedBiddings READ nExpectedBiddings NOTIFY nExpectedBiddingsChanged BINDABLE
|
||||
bindableNExpectedBiddings)
|
||||
|
||||
public:
|
||||
ModelSummary(shared_ptr<TableModel> model, QObject* parent = nullptr);
|
||||
@ -19,14 +21,21 @@ class ModelSummary : public QObject {
|
||||
|
||||
int rowCount() const;
|
||||
QBindable<int> bindableRowCount();
|
||||
int nExpectedBiddings() const;
|
||||
QBindable<int> bindableNExpectedBiddings();
|
||||
|
||||
signals:
|
||||
void rowCountChanged();
|
||||
|
||||
void nExpectedBiddingsChanged();
|
||||
private:
|
||||
shared_ptr<TableModel> m_model;
|
||||
|
||||
Q_OBJECT_BINDABLE_PROPERTY(ModelSummary, int, m_rowCount, &ModelSummary::rowCountChanged);
|
||||
Q_OBJECT_BINDABLE_PROPERTY(ModelSummary,
|
||||
int,
|
||||
m_nExpectedBiddings,
|
||||
&ModelSummary::nExpectedBiddingsChanged);
|
||||
};
|
||||
|
||||
#endif // MODELSUMMARY_H
|
||||
|
||||
@ -290,8 +290,19 @@ void TableModel::onRowCountChanged(const QModelIndex& parent, int first, int las
|
||||
return;
|
||||
}
|
||||
emit rowCountChanged();
|
||||
emit nExpectedBiddingsChanged();
|
||||
}
|
||||
|
||||
int TableModel::nExpectedBiddings() const {
|
||||
int result = 0;
|
||||
for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) {
|
||||
const QString biddingType = (*i)->data(BiddingTypeRole).toString();
|
||||
if (!biddingType.isEmpty()) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
|
||||
const int nRows = valueList.size();
|
||||
qDebug() << "Inserting" << nRows << "items...";
|
||||
@ -326,6 +337,9 @@ void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& chan
|
||||
QList<int> roles = changedValues.keys();
|
||||
roles.insert(0, Qt::DisplayRole);
|
||||
emit dataChanged(firstIndex, lastIndex, roles.toVector());
|
||||
|
||||
// NEXT check which roles are changed & trigger only changed properties
|
||||
emit nExpectedBiddingsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user