Skip to content

Commit

Permalink
MiscTableModel and more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Young committed May 28, 2023
1 parent f89c64c commit 3fc7500
Show file tree
Hide file tree
Showing 45 changed files with 809 additions and 3,502 deletions.
2 changes: 1 addition & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ void MainWindow::setupTables()
miscTableModel = new MiscTableModel(miscTable);
miscTableProxy = new MiscSortFilterProxyModel(miscTable,false);
miscTableProxy->setSourceModel(miscTableModel);
miscTable->setItemDelegate(new MiscItemDelegate(miscTable));
miscTable->setItemDelegate(new MiscItemDelegate(miscTable, *miscTableModel));
miscTable->setModel(miscTableProxy);
connect( miscTable, &QTableView::doubleClicked, this, [&](const QModelIndex &idx) {
if (idx.column() == 0)
Expand Down
20 changes: 10 additions & 10 deletions src/measurement/UnitSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,23 @@ Measurement::Amount Measurement::UnitSystem::qstringToSI(QString qstr, Unit cons
// match to a unit in another UnitSystem for the same PhysicalQuantity. If there are no matches that way, it will
// return nullptr;
unitToUse = Unit::getUnit(unitName, *this, true);
if (unitToUse) {
qDebug() << Q_FUNC_INFO << this->uniqueName << ":" << unitName << "interpreted as" << unitToUse->name;
} else {
qDebug() <<
Q_FUNC_INFO << this->uniqueName << ":" << unitName << "not recognised for" << this->pimpl->physicalQuantity;
}
// if (unitToUse) {
// qDebug() << Q_FUNC_INFO << this->uniqueName << ":" << unitName << "interpreted as" << unitToUse->name;
// } else {
// qDebug() <<
// Q_FUNC_INFO << this->uniqueName << ":" << unitName << "not recognised for" << this->pimpl->physicalQuantity;
// }
}

if (!unitToUse) {
qDebug() << Q_FUNC_INFO << "Defaulting to" << defUnit;
// qDebug() << Q_FUNC_INFO << "Defaulting to" << defUnit;
unitToUse = &defUnit;
}

Measurement::Amount siAmount = unitToUse->toCanonical(amt);
qDebug() <<
Q_FUNC_INFO << this->uniqueName << ": " << qstr << "is" << amt << " " << unitToUse->name << "=" <<
siAmount.quantity() << "in" << siAmount.unit()->name;
// qDebug() <<
// Q_FUNC_INFO << this->uniqueName << ": " << qstr << "is" << amt << " " << unitToUse->name << "=" <<
// siAmount.quantity() << "in" << siAmount.unit()->name;

return siAmount;
}
Expand Down
12 changes: 3 additions & 9 deletions src/model/Fermentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,6 @@ void Fermentable::setBetaGlucanWithUnits(std::optional<MassOrVolumeConcentration
return;
}

void Fermentable::setInventoryAmount(double num) {
InventoryUtils::setAmount(*this, num);
return;
}

double Fermentable::inventory() const {
return InventoryUtils::getAmount(*this);
}

Recipe * Fermentable::getOwningRecipe() {
return ObjectStoreWrapper::findFirstMatching<Recipe>( [this](Recipe * rec) {return rec->uses(*this);} );
}
Expand All @@ -465,3 +456,6 @@ bool fermentablesLessThanByWeight(Fermentable const * const lhs, Fermentable con
// Yes. I know. This seems silly, but I want the returned list in descending not ascending order.
return lhs->amount() > rhs->amount();
}

// Insert the boiler-plate stuff for inventory
INVENTORY_COMMON_CODE(Fermentable)
7 changes: 2 additions & 5 deletions src/model/Fermentable.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,6 @@ class Fermentable : public NamedEntityWithInventory {
bool isExtract () const;
bool isSugar () const;

virtual double inventory() const;

//============================================ "SETTER" MEMBER FUNCTIONS ============================================
void setType (Type const val);
void setAmount (double const val);
Expand Down Expand Up @@ -557,9 +555,8 @@ class Fermentable : public NamedEntityWithInventory {
void setFanWithUnits (std::optional<MassOrVolumeConcentrationAmt> const val);
void setBetaGlucanWithUnits(std::optional<MassOrVolumeConcentrationAmt> const val);

virtual void setInventoryAmount(double amount);

void save();
// Insert boiler-plate declarations for inventory
INVENTORY_COMMON_HEADER_DEFNS

virtual Recipe * getOwningRecipe();

Expand Down
10 changes: 3 additions & 7 deletions src/model/Hop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,6 @@ std::optional<double> Hop::pinene_pct() const { return this->m_
std::optional<double> Hop::polyphenols_pct() const { return this->m_polyphenols_pct; }
std::optional<double> Hop::xanthohumol_pct() const { return this->m_xanthohumol_pct; }

double Hop::inventory() const {
return InventoryUtils::getAmount(*this);
}

//============================================= "SETTER" MEMBER FUNCTIONS ==============================================
void Hop::setAlpha_pct (double const val) { this->setAndNotify(PropertyNames::Hop::alpha_pct, this->m_alpha_pct, this->enforceMinAndMax(val, "alpha", 0.0, 100.0)); }
void Hop::setAmount_kg (double const val) { this->setAndNotify(PropertyNames::Hop::amount_kg, this->m_amount_kg, this->enforceMin (val, "amount") ); }
Expand Down Expand Up @@ -329,9 +325,6 @@ void Hop::setPinene_pct (std::optional<double > const val) { th
void Hop::setPolyphenols_pct (std::optional<double > const val) { this->setAndNotify(PropertyNames::Hop::polyphenols_pct, this->m_polyphenols_pct, this->enforceMinAndMax(val, "polyphenols_pct", 0.0, 100.0)); }
void Hop::setXanthohumol_pct (std::optional<double > const val) { this->setAndNotify(PropertyNames::Hop::xanthohumol_pct, this->m_xanthohumol_pct, this->enforceMinAndMax(val, "xanthohumol_pct", 0.0, 100.0)); }

void Hop::setInventoryAmount(double num) { InventoryUtils::setAmount(*this, num); }


Recipe * Hop::getOwningRecipe() {
return ObjectStoreWrapper::findFirstMatching<Recipe>( [this](Recipe * rec) {return rec->uses(*this);} );
}
Expand All @@ -345,3 +338,6 @@ bool hopLessThanByTime(Hop const * const lhs, Hop const * const rhs) {
}
return lhs->use() < rhs->use();
}

// Insert the boiler-plate stuff for inventory
INVENTORY_COMMON_CODE_MO(Hop)
5 changes: 2 additions & 3 deletions src/model/Hop.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ class Hop : public NamedEntityWithInventory {
std::optional<double > polyphenols_pct () const;
std::optional<double > xanthohumol_pct () const;

virtual double inventory() const;

//============================================ "SETTER" MEMBER FUNCTIONS ============================================
void setAlpha_pct (double const val);
void setAmount_kg (double const val);
Expand Down Expand Up @@ -293,7 +291,8 @@ class Hop : public NamedEntityWithInventory {
void setPolyphenols_pct (std::optional<double > const val);
void setXanthohumol_pct (std::optional<double > const val);

virtual void setInventoryAmount(double const val);
// Insert boiler-plate declarations for inventory
INVENTORY_COMMON_HEADER_DEFNS

virtual Recipe * getOwningRecipe();

Expand Down
11 changes: 3 additions & 8 deletions src/model/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,10 @@ void Misc::setAmountWithUnits(MassOrVolumeAmt const val) {
}

//========================OTHER METHODS=========================================
double Misc::inventory() const {
return InventoryUtils::getAmount(*this);
}

void Misc::setInventoryAmount(double var) {
InventoryUtils::setAmount(*this, var);
return;
}

Recipe * Misc::getOwningRecipe() {
return ObjectStoreWrapper::findFirstMatching<Recipe>( [this](Recipe * rec) {return rec->uses(*this);} );
}

// Insert the boiler-plate stuff for inventory
INVENTORY_COMMON_CODE(Misc)
7 changes: 2 additions & 5 deletions src/model/Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ class Misc : public NamedEntityWithInventory {
QString producer () const;
QString productId () const;

virtual double inventory() const;

//============================================ "SETTER" MEMBER FUNCTIONS ============================================
void setType (Type const val);
void setUse (std::optional<Use> const val);
Expand All @@ -177,9 +175,8 @@ class Misc : public NamedEntityWithInventory {
void setProducer (QString const & val);
void setProductId (QString const & val);

//! \brief The amount in inventory in either kg or L, depending on \c amountIsWeight().
virtual void setInventoryAmount( double var );

// Insert boiler-plate declarations for inventory
INVENTORY_COMMON_HEADER_DEFNS

virtual Recipe * getOwningRecipe();

Expand Down
3 changes: 3 additions & 0 deletions src/model/NamedEntityWithInventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ TypeLookup const NamedEntityWithInventory::typeLookup {
{
// PROPERTY_TYPE_LOOKUP_ENTRY(PropertyNames::NamedEntityWithInventory::inventory, NamedEntityWithInventory::m_inventory , Measurement::PqEitherMassOrVolume ),
PROPERTY_TYPE_LOOKUP_ENTRY(PropertyNames::NamedEntityWithInventory::inventoryId, NamedEntityWithInventory::m_inventory_id),

PROPERTY_TYPE_LOOKUP_ENTRY_NO_MV(PropertyNames::NamedEntityWithInventory::inventoryWithUnits, NamedEntityWithInventory, inventoryWithUnits, Measurement::PqEitherMassOrVolume ),

},
// Parent class lookup
&NamedEntity::typeLookup
Expand Down
39 changes: 39 additions & 0 deletions src/model/NamedEntityWithInventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define AddPropertyName(property) namespace PropertyNames::NamedEntityWithInventory { BtStringConst const property{#property}; }
AddPropertyName(inventory )
AddPropertyName(inventoryId)
AddPropertyName(inventoryWithUnits)
#undef AddPropertyName
//=========================================== End of property name constants ===========================================
//======================================================================================================================
Expand Down Expand Up @@ -55,6 +56,14 @@ class NamedEntityWithInventory : public NamedEntity {
//! \brief The inventory table id, needed for signals
Q_PROPERTY(int inventoryId READ inventoryId WRITE setInventoryId )

/**
* \brief Amounts of \c Fermentable and \c Misc can be measured by mass or by volume (depending usually on what it
* is).
*
* NOTE: This property \b cannot be used to change between mass and volume.
*/
Q_PROPERTY(MassOrVolumeAmt inventoryWithUnits READ inventoryWithUnits WRITE setInventoryWithUnits)

/**
* \brief Override \c NamedEntity::makeChild() as we have additional work to do for objects with inventory.
* Specifically, a child object needs to have the same inventory as its parent.
Expand All @@ -66,12 +75,42 @@ class NamedEntityWithInventory : public NamedEntity {

virtual double inventory() const = 0;
int inventoryId() const;
virtual MassOrVolumeAmt inventoryWithUnits() const = 0;

virtual void setInventoryAmount(double amount) = 0;
void setInventoryId(int key);
virtual void setInventoryWithUnits(MassOrVolumeAmt const val) = 0;

protected:
int m_inventory_id;
};

/**
* \brief Derived classes should include this in their header file
*/
#define INVENTORY_COMMON_HEADER_DEFNS \
virtual double inventory() const; \
virtual MassOrVolumeAmt inventoryWithUnits() const; \
virtual void setInventoryAmount(double amount); \
virtual void setInventoryWithUnits(MassOrVolumeAmt const val);

/**
* \brief Derived classes should include this in their implementation file if they support measuring by volume and by
* mass
*/
#define INVENTORY_COMMON_CODE(NeName) \
double NeName::inventory() const { return InventoryUtils::getAmount(*this); } \
MassOrVolumeAmt NeName::inventoryWithUnits() const { return MassOrVolumeAmt{InventoryUtils::getAmount(*this), this->amountIsWeight() ? Measurement::Units::kilograms : Measurement::Units::liters}; } \
void NeName::setInventoryAmount(double num) { InventoryUtils::setAmount(*this, num); return; } \
void NeName::setInventoryWithUnits(MassOrVolumeAmt const val) { this->setInventoryAmount(val.quantity()); return; }

/**
* \brief Derived classes should include this in their implementation file if they support measuring by mass only
*/
#define INVENTORY_COMMON_CODE_MO(NeName) \
double NeName::inventory() const { return InventoryUtils::getAmount(*this); } \
MassOrVolumeAmt NeName::inventoryWithUnits() const { return MassOrVolumeAmt{InventoryUtils::getAmount(*this), Measurement::Units::kilograms}; } \
void NeName::setInventoryAmount(double num) { InventoryUtils::setAmount(*this, num); return; } \
void NeName::setInventoryWithUnits(MassOrVolumeAmt const val) { this->setInventoryAmount(val.quantity()); return; }

#endif
12 changes: 3 additions & 9 deletions src/model/Yeast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ double Yeast::maxTemperature_c() const { return m_maxTemperature_c; }

double Yeast::attenuation_pct() const { return m_attenuation_pct; }

double Yeast::inventory() const {
return InventoryUtils::getAmount(*this);
}

int Yeast::timesCultured() const { return m_timesCultured; }

int Yeast::maxReuse() const { return m_maxReuse; }
Expand Down Expand Up @@ -234,11 +230,6 @@ void Yeast::setAmount(double var) {
this->enforceMin(var, "amount", 0.0));
}

void Yeast::setInventoryAmount(double var) {
InventoryUtils::setAmount(*this, var);
return;
}

// .:TBD:. I'm not wild about using "quanta" here (presumably to mean number of packets or number of cultures)
// Storing an int in a double is safe, so, for now, just leave this in place but as a wrapper around the more
// generic setInventoryAmount().
Expand Down Expand Up @@ -312,3 +303,6 @@ void Yeast::setAddToSecondary( bool var ) {
Recipe * Yeast::getOwningRecipe() {
return ObjectStoreWrapper::findFirstMatching<Recipe>( [this](Recipe * rec) {return rec->uses(*this);} );
}

// Insert the boiler-plate stuff for inventory
INVENTORY_COMMON_CODE(Yeast)
5 changes: 3 additions & 2 deletions src/model/Yeast.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class Yeast : public NamedEntityWithInventory {
void setType( Type t);
void setForm( Form f);
void setAmount( double var);
virtual void setInventoryAmount(double var);
void setInventoryQuanta(int var);
void setAmountIsWeight( bool var);
void setLaboratory( const QString& var);
Expand All @@ -163,7 +162,6 @@ class Yeast : public NamedEntityWithInventory {
const QString formString() const;
const QString formStringTr() const;
double amount() const;
virtual double inventory() const;
bool amountIsWeight() const;
QString laboratory() const;
QString productID() const;
Expand All @@ -179,6 +177,9 @@ class Yeast : public NamedEntityWithInventory {
int maxReuse() const;
bool addToSecondary() const;

// Insert boiler-plate declarations for inventory
INVENTORY_COMMON_HEADER_DEFNS

virtual Recipe * getOwningRecipe();

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/tableModels/BtTableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ class BtTableModelRecipeObserver : public BtTableModel {
std::initializer_list<ColumnInfo> columnInfos);
~BtTableModelRecipeObserver();

protected:
Recipe* recObs;
// Normally this would be protected, but it needs to be public for TableModelBase to access
Recipe * recObs;
};

/**
Expand Down
Loading

0 comments on commit 3fc7500

Please sign in to comment.