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-07-16 18:31:44 +00:00
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QAbstractItemView>
|
2013-05-02 22:27:36 +00:00
|
|
|
|
2013-07-18 13:24:02 +00:00
|
|
|
// Gets the index of the model in the currentRow and column.
|
|
|
|
// currCombo is defined below.
|
|
|
|
#define IDX( XX ) mymodel->index(currCombo.currRow, XX)
|
|
|
|
|
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-07-18 14:53:47 +00:00
|
|
|
connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
|
|
|
|
this, SLOT(revertModelData(QWidget*, QAbstractItemDelegate::EndEditHint)));
|
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-07-16 22:13:58 +00:00
|
|
|
struct CurrSelected{
|
|
|
|
QComboBox *comboEditor;
|
|
|
|
int currRow;
|
|
|
|
QString activeText;
|
|
|
|
QAbstractItemModel *model;
|
|
|
|
} currCombo;
|
|
|
|
|
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);
|
2013-07-16 18:31:44 +00:00
|
|
|
comboDelegate->lineEdit()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this)));
|
2013-07-17 14:47:23 +00:00
|
|
|
comboDelegate->view()->installEventFilter( const_cast<QObject*>(qobject_cast<const QObject*>(this)));
|
2013-07-16 22:13:58 +00:00
|
|
|
connect(comboDelegate, SIGNAL(highlighted(QString)), this, SLOT(testActivation(QString)));
|
|
|
|
currCombo.comboEditor = comboDelegate;
|
|
|
|
currCombo.currRow = index.row();
|
|
|
|
currCombo.model = const_cast<QAbstractItemModel*>(index.model());
|
2013-06-16 13:15:19 +00:00
|
|
|
return comboDelegate;
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:13:58 +00:00
|
|
|
void ComboBoxDelegate::testActivation(const QString& s)
|
|
|
|
{
|
|
|
|
currCombo.activeText = s;
|
|
|
|
setModelData(currCombo.comboEditor, currCombo.model, QModelIndex());
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:31:44 +00:00
|
|
|
bool ComboBoxDelegate::eventFilter(QObject* object, QEvent* event)
|
|
|
|
{
|
|
|
|
// Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices.
|
|
|
|
if (event->type() == QEvent::KeyPress){
|
2013-07-17 14:47:23 +00:00
|
|
|
if (object == currCombo.comboEditor){ // the 'LineEdit' part
|
|
|
|
QKeyEvent *ev = static_cast<QKeyEvent*>(event);
|
|
|
|
if(ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down){
|
|
|
|
currCombo.comboEditor->showPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{ // the 'Drop Down Menu' part.
|
|
|
|
QKeyEvent *ev = static_cast<QKeyEvent*>(event);
|
|
|
|
if( ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return
|
|
|
|
|| ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Backtab
|
|
|
|
|| ev->key() == Qt::Key_Escape){
|
|
|
|
// treat Qt as a silly little boy - pretending that the key_return nwas pressed on the combo,
|
|
|
|
// instead of the list of choices. this can be extended later for
|
|
|
|
// other imputs, like tab navigation and esc.
|
|
|
|
QStyledItemDelegate::eventFilter(currCombo.comboEditor, event);
|
|
|
|
}
|
2013-07-16 18:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStyledItemDelegate::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
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-07-18 13:24:02 +00:00
|
|
|
struct RevertCylinderData{
|
|
|
|
QString type;
|
|
|
|
int pressure;
|
|
|
|
int size;
|
|
|
|
} currCylinderData;
|
|
|
|
|
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-07-16 22:13:58 +00:00
|
|
|
CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
|
2013-05-23 18:59:12 +00:00
|
|
|
TankInfoModel *tanks = TankInfoModel::instance();
|
2013-07-16 22:13:58 +00:00
|
|
|
QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, currCombo.activeText);
|
2013-05-24 05:56:12 +00:00
|
|
|
int row;
|
|
|
|
if (matches.isEmpty()) {
|
|
|
|
// we need to add this
|
|
|
|
tanks->insertRows(tanks->rowCount(), 1);
|
2013-07-16 22:13:58 +00:00
|
|
|
tanks->setData(tanks->index(tanks->rowCount() -1, 0), currCombo.activeText);
|
2013-05-24 05:56:12 +00:00
|
|
|
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();
|
|
|
|
|
2013-07-18 12:01:37 +00:00
|
|
|
// don't fuck the other data, jimmy.
|
|
|
|
if ( mymodel->data(thisindex, CylindersModel::TYPE).toString() == currCombo.activeText){
|
|
|
|
return;
|
|
|
|
}
|
2013-07-18 13:24:02 +00:00
|
|
|
|
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, Qt::EditRole);
|
|
|
|
mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), tankPressure);
|
|
|
|
mymodel->passInData(IDX(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-07-18 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TankInfoDelegate::revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint)
|
|
|
|
{
|
|
|
|
if (hint == QAbstractItemDelegate::NoHint || hint == QAbstractItemDelegate::RevertModelCache){
|
|
|
|
CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
|
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), currCylinderData.type, Qt::EditRole);
|
|
|
|
mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure);
|
|
|
|
mymodel->passInData(IDX(CylindersModel::SIZE), currCylinderData.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* TankInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
// ncreate editor needs to be called before because it will populate a few
|
|
|
|
// things in the currCombo global var.
|
|
|
|
QWidget *delegate = ComboBoxDelegate::createEditor(parent, option, index);
|
|
|
|
CylindersModel *mymodel = qobject_cast<CylindersModel *>(currCombo.model);
|
|
|
|
cylinder_t *cyl = mymodel->cylinderAt(index);
|
|
|
|
currCylinderData.type = cyl->type.description;
|
|
|
|
currCylinderData.pressure = cyl->type.workingpressure.mbar;
|
|
|
|
currCylinderData.size = cyl->type.size.mliter;
|
|
|
|
return delegate;
|
2013-05-24 01:40:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:53:47 +00:00
|
|
|
struct RevertWeigthData {
|
|
|
|
QString type;
|
|
|
|
int weigth;
|
|
|
|
} currWeigth;
|
|
|
|
|
|
|
|
void WSInfoDelegate::revertModelData(QWidget* widget, QAbstractItemDelegate::EndEditHint hint)
|
|
|
|
{
|
|
|
|
if (hint == QAbstractItemDelegate::NoHint || hint == QAbstractItemDelegate::RevertModelCache){
|
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
|
|
|
mymodel->setData(IDX(WeightModel::TYPE), currWeigth.type, Qt::EditRole);
|
|
|
|
mymodel->passInData(IDX(WeightModel::WEIGHT), currWeigth.weigth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-16 22:13:58 +00:00
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
2013-05-24 05:56:12 +00:00
|
|
|
WSInfoModel *wsim = WSInfoModel::instance();
|
2013-07-16 22:13:58 +00:00
|
|
|
QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, currCombo.activeText);
|
2013-05-24 05:56:12 +00:00
|
|
|
int row;
|
|
|
|
if (matches.isEmpty()) {
|
|
|
|
// we need to add this puppy
|
|
|
|
wsim->insertRows(wsim->rowCount(), 1);
|
2013-07-16 22:13:58 +00:00
|
|
|
wsim->setData(wsim->index(wsim->rowCount() - 1, 0), currCombo.activeText);
|
2013-05-24 05:56:12 +00:00
|
|
|
row = wsim->rowCount() - 1;
|
|
|
|
} else {
|
|
|
|
row = matches.first().row();
|
|
|
|
}
|
|
|
|
int grams = wsim->data(wsim->index(row, WSInfoModel::GR)).toInt();
|
2013-07-16 22:13:58 +00:00
|
|
|
QVariant v = QString(currCombo.activeText);
|
2013-07-18 12:01:37 +00:00
|
|
|
|
|
|
|
// don't set if it's the same as it was before setting.
|
|
|
|
if (mymodel->data(thisindex, WeightModel::TYPE).toString() == currCombo.activeText){
|
|
|
|
return;
|
|
|
|
}
|
2013-07-18 13:24:02 +00:00
|
|
|
mymodel->setData(IDX(WeightModel::TYPE), v, Qt::EditRole);
|
|
|
|
mymodel->passInData(IDX(WeightModel::WEIGHT), grams);
|
2013-07-16 22:13:58 +00:00
|
|
|
qDebug() << "Fixme, every weigth is 0.0 grams. see:" << 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
|
|
|
{
|
|
|
|
}
|
2013-07-18 13:24:02 +00:00
|
|
|
|
|
|
|
QWidget* WSInfoDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
2013-07-18 14:53:47 +00:00
|
|
|
/* First, call the combobox-create editor, it will setup our globals. */
|
|
|
|
QWidget *editor = ComboBoxDelegate::createEditor(parent, option, index);
|
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
|
|
|
weightsystem_t *ws = mymodel->weightSystemAt(index);
|
|
|
|
currWeigth.type = ws->description;
|
|
|
|
currWeigth.weigth = ws->weight.grams;
|
|
|
|
return editor;
|
2013-07-18 13:24:02 +00:00
|
|
|
}
|