Skip to content

Commit

Permalink
Merge pull request #85 from theophae/improvement/weirdCheckBox
Browse files Browse the repository at this point in the history
Fixes #81: Improvement/weird check box
  • Loading branch information
rocketman768 committed Nov 1, 2015
2 parents f4f3268 + b06ec8a commit 1d1a3c0
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 42 deletions.
106 changes: 68 additions & 38 deletions src/FermentableTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <QWidget>
#include <QModelIndex>
#include <QVariant>
#include <QCheckBox>
#include <QItemEditorFactory>
#include <QStyle>
#include <QRect>
Expand All @@ -37,6 +36,7 @@
#include "brewtarget.h"
#include <QSize>
#include <QComboBox>
#include <QListWidget>
#include <QLineEdit>
#include <QString>
#include <QVector>
Expand Down Expand Up @@ -298,22 +298,17 @@ QVariant FermentableTableModel::data( const QModelIndex& index, int role ) const

return QVariant( Brewtarget::displayAmount(row->amount_kg(), Units::kilograms, 3, unit, scale) );
case FERMISMASHEDCOL:
if( role == Qt::CheckStateRole )
return QVariant( row->isMashed() ? Qt::Checked : Qt::Unchecked);
else if( role == Qt::DisplayRole )
{
if( row->type() == Fermentable::Grain)
return row->isMashed() ? tr("Mashed") : tr("Steeped");
else
return row->isMashed() ? tr("Mashed") : tr("Not mashed");
}
if( role == Qt::DisplayRole )
return QVariant(row->additionMethodStringTr());
else if( role == Qt::UserRole )
return QVariant(row->additionMethod());
else
return QVariant();
case FERMAFTERBOIL:
if( role == Qt::CheckStateRole )
return QVariant( row->addAfterBoil() ? Qt::Checked : Qt::Unchecked );
else if( role == Qt::DisplayRole )
return row->addAfterBoil()? tr("Late") : tr("Normal");
if( role == Qt::DisplayRole )
return QVariant(row->additionTimeStringTr());
else if( role == Qt::UserRole )
return QVariant(row->additionTime());
else
return QVariant();
case FERMYIELDCOL:
Expand Down Expand Up @@ -350,9 +345,9 @@ QVariant FermentableTableModel::headerData( int section, Qt::Orientation orienta
case FERMAMOUNTCOL:
return QVariant(tr("Amount"));
case FERMISMASHEDCOL:
return QVariant(tr("Mashed"));
return QVariant(tr("Method"));
case FERMAFTERBOIL:
return QVariant(tr("Late Addition"));
return QVariant(tr("Addition"));
case FERMYIELDCOL:
return QVariant(tr("Yield %"));
case FERMCOLORCOL:
Expand Down Expand Up @@ -384,16 +379,16 @@ Qt::ItemFlags FermentableTableModel::flags(const QModelIndex& index ) const
case FERMISMASHEDCOL:
// Ensure that being mashed and being a late addition are mutually exclusive.
if( !row->addAfterBoil() )
return (defaults | (editable ? Qt::ItemIsUserCheckable : Qt::NoItemFlags));
return (defaults | Qt::ItemIsSelectable | (editable ? Qt::ItemIsEditable : Qt::NoItemFlags) | Qt::ItemIsDragEnabled);
else
return (editable ? Qt::ItemIsUserCheckable : Qt::NoItemFlags);
return Qt::ItemIsSelectable | (editable ? Qt::ItemIsEditable : Qt::NoItemFlags) | Qt::ItemIsDragEnabled;
break;
case FERMAFTERBOIL:
// Ensure that being mashed and being a late addition are mutually exclusive.
if( !row->isMashed() )
return (defaults | (editable ? Qt::ItemIsUserCheckable : Qt::NoItemFlags));
return (defaults | Qt::ItemIsSelectable | (editable ? Qt::ItemIsEditable : Qt::NoItemFlags) | Qt::ItemIsDragEnabled);
else
return (editable ? Qt::ItemIsUserCheckable : Qt::NoItemFlags);
return Qt::ItemIsSelectable | (editable ? Qt::ItemIsEditable : Qt::NoItemFlags) | Qt::ItemIsDragEnabled;
break;
case FERMNAMECOL:
return (defaults | Qt::ItemIsSelectable);
Expand Down Expand Up @@ -624,14 +619,14 @@ bool FermentableTableModel::setData( const QModelIndex& index, const QVariant& v
}
break;
case FERMISMASHEDCOL:
retVal = (role == Qt::CheckStateRole && value.canConvert(QVariant::Int));
retVal = value.canConvert(QVariant::Int);
if( retVal )
row->setIsMashed( ((Qt::CheckState)value.toInt()) == Qt::Checked );
row->setAdditionMethod(static_cast<Fermentable::AdditionMethod>(value.toInt()));
break;
case FERMAFTERBOIL:
retVal = (role == Qt::CheckStateRole && value.canConvert(QVariant::Int));
retVal = value.canConvert(QVariant::Int);
if( retVal )
row->setAddAfterBoil( ((Qt::CheckState)value.toInt()) == Qt::Checked );
row->setAdditionTime(static_cast<Fermentable::AdditionTime>(value.toInt()));
break;
case FERMYIELDCOL:
retVal = value.canConvert(QVariant::Double);
Expand Down Expand Up @@ -673,15 +668,54 @@ QWidget* FermentableItemDelegate::createEditor(QWidget *parent, const QStyleOpti
box->addItem(tr("Extract"));
box->addItem(tr("Dry Extract"));
box->addItem(tr("Adjunct"));

box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
box->setFocusPolicy(Qt::StrongFocus);

return box;
}
else if( index.column() == FERMISMASHEDCOL )
{
QCheckBox* box = new QCheckBox(parent);
QComboBox* box = new QComboBox(parent);
QListWidget* list = new QListWidget(parent);
list->setResizeMode(QListWidget::Adjust);

list->addItem(tr("Mashed"));
list->addItem(tr("Steeped"));
list->addItem(tr("Not mashed"));
box->setModel(list->model());
box->setView(list);

box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
box->setFocusPolicy(Qt::StrongFocus);

// Can we access to the data model into FermentableItemDelegate ? Yes we can !
int type = index.model()->index(index.row(), FERMTYPECOL).data(Qt::UserRole).toInt();

// Hide the unsuitable item keeping the same enumeration
if(type == Fermentable::Grain)
{
list->item(Fermentable::Not_Mashed)->setHidden(true);
}
else
{
list->item(Fermentable::Steeped)->setHidden(true);
}

return box;
}
else if( index.column() == FERMAFTERBOIL )
{
QComboBox* box = new QComboBox(parent);

box->addItem(tr("Normal"));
box->addItem(tr("Late"));

box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
box->setFocusPolicy(Qt::StrongFocus);
box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

return box;
}
Expand All @@ -693,20 +727,13 @@ void FermentableItemDelegate::setEditorData(QWidget *editor, const QModelIndex &
{
int col = index.column();

if( col == FERMTYPECOL )
if( col == FERMTYPECOL || col == FERMISMASHEDCOL || col == FERMAFTERBOIL)
{
QComboBox* box = (QComboBox*)editor;
int ndx = index.model()->data(index, Qt::UserRole).toInt();

box->setCurrentIndex(ndx);
}
else if( col == FERMISMASHEDCOL || col == FERMAFTERBOIL )
{
QCheckBox* checkBox = (QCheckBox*)editor;
Qt::CheckState checkState = (Qt::CheckState)index.model()->data(index, Qt::CheckStateRole).toInt();

checkBox->setCheckState( checkState );
}
else
{
QLineEdit* line = (QLineEdit*)editor;
Expand All @@ -719,7 +746,7 @@ void FermentableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *
{
int col = index.column();

if( col == FERMTYPECOL )
if( col == FERMTYPECOL || col == FERMISMASHEDCOL || col == FERMAFTERBOIL )
{
QComboBox* box = qobject_cast<QComboBox*>(editor);
int value = box->currentIndex();
Expand All @@ -731,10 +758,13 @@ void FermentableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *
}
else if( col == FERMISMASHEDCOL || col == FERMAFTERBOIL )
{
QCheckBox* checkBox = qobject_cast<QCheckBox*>(editor);
bool checked = (checkBox->checkState() == Qt::Checked);
QComboBox* box = qobject_cast<QComboBox*>(editor);
int value = box->currentIndex();
int ndx = model->data(index, Qt::UserRole).toInt();

model->setData(index, checked, Qt::EditRole);
// Only do something when something needs to be done
if ( value != ndx )
model->setData(index, value, Qt::EditRole);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/HopTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ QWidget* HopItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt
box->addItem(tr("Boil"));
box->addItem(tr("Aroma"));
box->addItem(tr("Dry Hop"));
box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);

return box;
Expand All @@ -584,7 +585,7 @@ QWidget* HopItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt
box->addItem(tr("Leaf"));
box->addItem(tr("Pellet"));
box->addItem(tr("Plug"));

box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);

return box;
Expand Down
1 change: 1 addition & 0 deletions src/MashStepTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ QWidget* MashStepItemDelegate::createEditor(QWidget *parent, const QStyleOptionV
box->addItem("Infusion");
box->addItem("Temperature");
box->addItem("Decoction");
box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);

return box;
Expand Down
2 changes: 2 additions & 0 deletions src/MiscTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ QWidget* MiscItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
box->addItem(tr("Herb"));
box->addItem(tr("Flavor"));
box->addItem(tr("Other"));
box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
return box;
}
Expand All @@ -561,6 +562,7 @@ QWidget* MiscItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
box->addItem(tr("Primary"));
box->addItem(tr("Secondary"));
box->addItem(tr("Bottling"));
box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
return box;
}
Expand Down
4 changes: 3 additions & 1 deletion src/YeastTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ QWidget* YeastItemDelegate::createEditor(QWidget *parent, const QStyleOptionView
box->addItem(tr("Wheat"));
box->addItem(tr("Wine"));
box->addItem(tr("Champagne"));
box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);

return box;
Expand All @@ -531,7 +532,8 @@ QWidget* YeastItemDelegate::createEditor(QWidget *parent, const QStyleOptionView
box->addItem(tr("Dry"));
box->addItem(tr("Slant"));
box->addItem(tr("Culture"));

box->setMinimumWidth(box->minimumSizeHint().width());
box->setSizeAdjustPolicy(QComboBox::AdjustToContents);
return box;
}
else
Expand Down
69 changes: 69 additions & 0 deletions src/fermentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ void Fermentable::setDefaults()
// Get
const QString Fermentable::name() const { return get("name").toString(); }
const Fermentable::Type Fermentable::type() const { return static_cast<Fermentable::Type>(types.indexOf(get("ftype").toString())); }
const Fermentable::AdditionMethod Fermentable::additionMethod() const
{
Fermentable::AdditionMethod additionMethod;
if(isMashed())
additionMethod = Fermentable::Mashed;
else
{
if(type() == Fermentable::Grain)
additionMethod = Fermentable::Steeped;
else
additionMethod = Fermentable::Not_Mashed;
}
return additionMethod;
}
const Fermentable::AdditionTime Fermentable::additionTime() const
{
Fermentable::AdditionTime additionTime;
if(addAfterBoil())
additionTime = Fermentable::Late;
else
additionTime = Fermentable::Normal;

return additionTime;
}
const QString Fermentable::typeString() const
{
return types.at(type());
Expand All @@ -222,6 +246,34 @@ const QString Fermentable::typeStringTr() const
return typesTr.at(type());
}

const QString Fermentable::additionMethodStringTr() const
{
QString retString;

if(isMashed())
retString = tr("Mashed");
else
{
if(type() == Fermentable::Grain)
retString = tr("Steeped");
else
retString = tr("Not mashed");
}
return retString;
}

const QString Fermentable::additionTimeStringTr() const
{
QString retString;

if(addAfterBoil())
retString = tr("Late");
else
retString = tr("Normal");

return retString;
}

double Fermentable::amount_kg() const { return get("amount").toDouble(); }
double Fermentable::yield_pct() const { return get("yield").toDouble(); }
double Fermentable::color_srm() const { return get("color").toDouble(); }
Expand Down Expand Up @@ -271,11 +323,28 @@ void Fermentable::setName( const QString& str )
set("name", "name", str);
emit changedName(str);
}

void Fermentable::setType( Type t )
{
set("type", "ftype", types.at(t));
}

void Fermentable::setAdditionMethod( Fermentable::AdditionMethod m )
{
if( m == Fermentable::Mashed )
setIsMashed(true);
else
setIsMashed(false);
}

void Fermentable::setAdditionTime( Fermentable::AdditionTime t )
{
if( t == Fermentable::Late )
setAddAfterBoil(true);
else
setAddAfterBoil(false);
}

void Fermentable::setAmount_kg( double num )
{
if( num < 0.0 )
Expand Down
Loading

0 comments on commit 1d1a3c0

Please sign in to comment.