2013-04-27 15:27:27 +00:00
|
|
|
#include "modeldelegates.h"
|
|
|
|
#include "../dive.h"
|
|
|
|
#include "../divelist.h"
|
|
|
|
#include "starwidget.h"
|
|
|
|
#include "models.h"
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QPainter>
|
2013-04-28 11:45:22 +00:00
|
|
|
#include <QSortFilterProxyModel>
|
2013-05-02 22:27:36 +00:00
|
|
|
#include <QStyle>
|
|
|
|
#include <QStyleOption>
|
2013-05-22 17:11:49 +00:00
|
|
|
#include <QComboBox>
|
2013-05-23 18:33:20 +00:00
|
|
|
#include <QCompleter>
|
|
|
|
#include <QLineEdit>
|
2013-05-02 22:27:36 +00:00
|
|
|
|
|
|
|
StarWidgetsDelegate::StarWidgetsDelegate(QWidget* parent):
|
|
|
|
QStyledItemDelegate(parent),
|
|
|
|
parentWidget(parent)
|
|
|
|
{
|
|
|
|
}
|
2013-04-27 15:27:27 +00:00
|
|
|
|
|
|
|
void StarWidgetsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
2013-05-02 22:27:36 +00:00
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
2013-05-02 23:32:57 +00:00
|
|
|
|
2013-05-02 05:00:08 +00:00
|
|
|
if (!index.isValid())
|
2013-04-27 15:27:27 +00:00
|
|
|
return;
|
|
|
|
|
2013-06-17 21:59:50 +00:00
|
|
|
QVariant value = index.model()->data(index, DiveTripModel::STAR_ROLE);
|
2013-05-02 21:19:15 +00:00
|
|
|
|
2013-05-02 05:00:08 +00:00
|
|
|
if (!value.isValid())
|
2013-05-02 02:51:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int rating = value.toInt();
|
|
|
|
|
2013-04-27 15:27:27 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
|
|
|
for(int i = 0; i < rating; i++)
|
|
|
|
painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y(), StarWidget::starActive());
|
|
|
|
|
|
|
|
for(int i = rating; i < TOTALSTARS; i++)
|
|
|
|
painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y(), StarWidget::starInactive());
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS-1), IMG_SIZE);
|
|
|
|
}
|
2013-05-22 17:11:49 +00:00
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject* parent): QStyledItemDelegate(parent), model(model)
|
2013-05-22 17:11:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
2013-05-23 18:33:20 +00:00
|
|
|
{
|
|
|
|
QComboBox *c = qobject_cast<QComboBox*>(editor);
|
|
|
|
QString data = index.model()->data(index, Qt::DisplayRole).toString();
|
|
|
|
int i = c->findText(data);
|
|
|
|
if (i != -1)
|
|
|
|
c->setCurrentIndex(i);
|
|
|
|
else
|
|
|
|
c->setEditText(data);
|
|
|
|
}
|
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
QComboBox *comboDelegate = new QComboBox(parent);
|
|
|
|
comboDelegate->setModel(model);
|
|
|
|
comboDelegate->setEditable(true);
|
|
|
|
comboDelegate->setAutoCompletion(true);
|
|
|
|
comboDelegate->setAutoCompletionCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
return comboDelegate;
|
|
|
|
}
|
|
|
|
|
2013-06-16 15:33:27 +00:00
|
|
|
void ComboBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
QRect defaultRect = option.rect;
|
|
|
|
defaultRect.setX( defaultRect.x() -1);
|
|
|
|
defaultRect.setY( defaultRect.y() -1);
|
|
|
|
defaultRect.setWidth( defaultRect.width() + 2);
|
|
|
|
defaultRect.setHeight( defaultRect.height() + 2);
|
|
|
|
editor->setGeometry(defaultRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-24 05:56:12 +00:00
|
|
|
void TankInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
|
2013-05-23 18:33:20 +00:00
|
|
|
{
|
2013-05-24 05:56:12 +00:00
|
|
|
QComboBox *c = qobject_cast<QComboBox*>(editor);
|
2013-05-23 20:31:46 +00:00
|
|
|
CylindersModel *mymodel = qobject_cast<CylindersModel *>(model);
|
2013-05-23 18:59:12 +00:00
|
|
|
TankInfoModel *tanks = TankInfoModel::instance();
|
2013-05-24 05:56:12 +00:00
|
|
|
QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, c->currentText());
|
|
|
|
int row;
|
|
|
|
if (matches.isEmpty()) {
|
|
|
|
// we need to add this
|
|
|
|
tanks->insertRows(tanks->rowCount(), 1);
|
|
|
|
tanks->setData(tanks->index(tanks->rowCount() -1, 0), c->currentText());
|
|
|
|
row = tanks->rowCount() - 1;
|
|
|
|
} else {
|
|
|
|
row = matches.first().row();
|
|
|
|
}
|
|
|
|
int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt();
|
|
|
|
int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt();
|
|
|
|
|
|
|
|
mymodel->setData(model->index(thisindex.row(), CylindersModel::TYPE), c->currentText(), Qt::EditRole);
|
|
|
|
mymodel->passInData(model->index(thisindex.row(), CylindersModel::WORKINGPRESS), tankPressure);
|
|
|
|
mymodel->passInData(model->index(thisindex.row(), CylindersModel::SIZE), tankSize);
|
2013-05-23 18:33:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
TankInfoDelegate::TankInfoDelegate(QObject* parent): ComboBoxDelegate(TankInfoModel::instance(), parent)
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-24 05:56:12 +00:00
|
|
|
void WSInfoDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& thisindex) const
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
2013-05-24 05:56:12 +00:00
|
|
|
QComboBox *c = qobject_cast<QComboBox*>(editor);
|
2013-05-24 01:40:16 +00:00
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(model);
|
2013-05-24 05:56:12 +00:00
|
|
|
WSInfoModel *wsim = WSInfoModel::instance();
|
|
|
|
QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, c->currentText());
|
|
|
|
int row;
|
|
|
|
if (matches.isEmpty()) {
|
|
|
|
// we need to add this puppy
|
|
|
|
wsim->insertRows(wsim->rowCount(), 1);
|
|
|
|
wsim->setData(wsim->index(wsim->rowCount() - 1, 0), c->currentText());
|
|
|
|
row = wsim->rowCount() - 1;
|
|
|
|
} else {
|
|
|
|
row = matches.first().row();
|
|
|
|
}
|
|
|
|
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
|
|
|
|
QVariant v = QString(c->currentText());
|
|
|
|
mymodel->setData(model->index(thisindex.row(), WeightModel::TYPE), v, Qt::EditRole);
|
|
|
|
mymodel->passInData(model->index(thisindex.row(), WeightModel::WEIGHT), grams);
|
2013-05-24 01:40:16 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
WSInfoDelegate::WSInfoDelegate(QObject* parent): ComboBoxDelegate(WSInfoModel::instance(), parent)
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
|
|
|
}
|