2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2018-10-21 16:00:02 +00:00
|
|
|
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/modeldelegates.h"
|
2022-09-02 20:51:19 +00:00
|
|
|
#include "core/sample.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/gettextfromc.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
|
|
|
#include "qt-models/cylindermodel.h"
|
|
|
|
#include "qt-models/models.h"
|
|
|
|
#include "desktop-widgets/starwidget.h"
|
2015-09-03 18:56:37 +00:00
|
|
|
#include "profile-widget/profilewidget2.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/tankinfomodel.h"
|
2018-05-10 15:35:30 +00:00
|
|
|
#include "qt-models/weightsysteminfomodel.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/weightmodel.h"
|
2023-01-21 00:09:44 +00:00
|
|
|
#include "qt-models/diveplannermodel.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/divetripmodel.h"
|
2018-10-25 06:02:06 +00:00
|
|
|
#include "qt-models/divelocationmodel.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/qthelper.h"
|
2019-03-04 22:20:29 +00:00
|
|
|
#include "core/divesite.h"
|
2022-04-04 16:57:28 +00:00
|
|
|
#include "core/selection.h"
|
2018-10-21 16:00:02 +00:00
|
|
|
#include "desktop-widgets/simplewidgets.h"
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2013-05-23 18:33:20 +00:00
|
|
|
#include <QCompleter>
|
2015-02-09 20:58:40 +00:00
|
|
|
#include <QKeyEvent>
|
2015-02-09 22:37:17 +00:00
|
|
|
#include <QTextDocument>
|
2015-07-02 00:04:03 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QColor>
|
2015-07-16 21:08:08 +00:00
|
|
|
#include <QAbstractProxyModel>
|
2018-10-21 16:00:02 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QAbstractItemView>
|
|
|
|
#include <QSpinBox>
|
2013-05-02 22:27:36 +00:00
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
QSize DiveListDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
|
2013-12-11 23:44:05 +00:00
|
|
|
{
|
2018-08-26 21:37:01 +00:00
|
|
|
const QFontMetrics metrics(qApp->font());
|
2024-01-17 21:47:40 +00:00
|
|
|
return QSize(50, std::max(22, metrics.height()));
|
2013-12-11 23:44:05 +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.
|
2014-01-16 04:50:56 +00:00
|
|
|
#define IDX(_XX) mymodel->index(currCombo.currRow, (_XX))
|
2013-07-18 13:24:02 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
StarWidgetsDelegate::StarWidgetsDelegate(QWidget *parent) : QStyledItemDelegate(parent),
|
2013-05-02 22:27:36 +00:00
|
|
|
parentWidget(parent)
|
|
|
|
{
|
2020-05-16 07:17:41 +00:00
|
|
|
const IconMetrics &metrics = defaultIconMetrics();
|
2014-10-15 13:30:52 +00:00
|
|
|
minStarSize = QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small);
|
2013-05-02 22:27:36 +00:00
|
|
|
}
|
2013-04-27 15:27:27 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
2013-04-27 15:27:27 +00:00
|
|
|
{
|
2013-05-02 22:27:36 +00:00
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
2013-05-02 05:00:08 +00:00
|
|
|
if (!index.isValid())
|
2013-04-27 15:27:27 +00:00
|
|
|
return;
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QVariant value = index.model()->data(index, DiveTripModelBase::STAR_ROLE);
|
2013-05-02 05:00:08 +00:00
|
|
|
if (!value.isValid())
|
2013-05-02 02:51:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int rating = value.toInt();
|
2014-02-28 04:09:57 +00:00
|
|
|
int deltaY = option.rect.height() / 2 - StarWidget::starActive().height() / 2;
|
2013-04-27 15:27:27 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
2014-06-19 21:45:26 +00:00
|
|
|
const QPixmap active = QPixmap::fromImage(StarWidget::starActive());
|
|
|
|
const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive());
|
2020-05-16 07:17:41 +00:00
|
|
|
const IconMetrics &metrics = defaultIconMetrics();
|
2014-06-19 21:45:26 +00:00
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
for (int i = 0; i < rating; i++)
|
2014-10-15 13:30:52 +00:00
|
|
|
painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, active);
|
2014-01-16 04:50:56 +00:00
|
|
|
for (int i = rating; i < TOTALSTARS; i++)
|
2014-10-15 13:30:52 +00:00
|
|
|
painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, inactive);
|
2013-04-27 15:27:27 +00:00
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
|
2013-04-27 15:27:27 +00:00
|
|
|
{
|
2014-10-15 13:30:49 +00:00
|
|
|
return minStarSize;
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
const QSize &StarWidgetsDelegate::starSize() const
|
2014-10-15 13:30:49 +00:00
|
|
|
{
|
|
|
|
return minStarSize;
|
2013-04-27 15:27:27 +00:00
|
|
|
}
|
2013-05-22 17:11:49 +00:00
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
ComboBoxDelegate::ComboBoxDelegate(std::function<QAbstractItemModel *(QWidget *)> create_model_func,
|
|
|
|
QObject *parent, bool allowEdit) : QStyledItemDelegate(parent),
|
|
|
|
create_model_func(std::move(create_model_func)),
|
|
|
|
editable(allowEdit)
|
2013-05-22 17:11:49 +00:00
|
|
|
{
|
2020-04-04 17:05:25 +00:00
|
|
|
connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::editorClosed);
|
2013-05-22 17:11:49 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
2013-05-23 18:33:20 +00:00
|
|
|
{
|
2014-02-28 04:09:57 +00:00
|
|
|
QComboBox *c = qobject_cast<QComboBox *>(editor);
|
2013-05-23 18:33:20 +00:00
|
|
|
QString data = index.model()->data(index, Qt::DisplayRole).toString();
|
|
|
|
int i = c->findText(data);
|
|
|
|
if (i != -1)
|
|
|
|
c->setCurrentIndex(i);
|
|
|
|
else
|
|
|
|
c->setEditText(data);
|
2014-07-15 17:58:24 +00:00
|
|
|
c->lineEdit()->setSelection(0, c->lineEdit()->text().length());
|
2013-05-23 18:33:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
|
2013-06-16 13:15:19 +00:00
|
|
|
{
|
|
|
|
QComboBox *comboDelegate = new QComboBox(parent);
|
2024-03-30 12:40:00 +00:00
|
|
|
comboDelegate->setModel(create_model_func(comboDelegate));
|
2013-06-16 13:15:19 +00:00
|
|
|
comboDelegate->setEditable(true);
|
2020-01-06 20:28:18 +00:00
|
|
|
comboDelegate->completer()->setCaseSensitivity(Qt::CaseInsensitive);
|
2013-06-16 13:15:19 +00:00
|
|
|
comboDelegate->completer()->setCompletionMode(QCompleter::PopupCompletion);
|
2020-09-10 08:42:00 +00:00
|
|
|
comboDelegate->completer()->setFilterMode(Qt::MatchContains);
|
Make the Qt ComboBox behave in a Better Way
So, the ComboBox is a beast, and when used on a Delegate
it's very hard to get things right, wich is a pitty, because
I overly like qt. So:
1 - Combobox needs to show the popup when user press ↓ and ↑ keys
2 - Combobox needs to select when user press enter, not twice.
3 - Combobox neesds to select when user selects from the mouse, not
pressing enter after.
4 - Combobox needs to not mess with stuff when moving around.
Everything that I listed there works on a non-delegate combobox,
but for some reason, a delegate missed those, so I reimplemented
all. not nice, but now we have a code that will work, I hope.
*fingers crossed*
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
2013-09-25 19:37:24 +00:00
|
|
|
comboDelegate->view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
|
2014-02-28 04:09:57 +00:00
|
|
|
comboDelegate->lineEdit()->installEventFilter(const_cast<QObject *>(qobject_cast<const QObject *>(this)));
|
2017-03-26 21:47:21 +00:00
|
|
|
comboDelegate->lineEdit()->setEnabled(editable);
|
2014-02-28 04:09:57 +00:00
|
|
|
comboDelegate->view()->installEventFilter(const_cast<QObject *>(qobject_cast<const QObject *>(this)));
|
2014-04-24 18:17:30 +00:00
|
|
|
QAbstractItemView *comboPopup = comboDelegate->lineEdit()->completer()->popup();
|
|
|
|
comboPopup->setMouseTracking(true);
|
2020-10-26 15:32:09 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
connect(comboDelegate, &QComboBox::textHighlighted, this, &ComboBoxDelegate::testActivationString);
|
|
|
|
#else
|
2020-05-16 07:46:30 +00:00
|
|
|
connect(comboDelegate, QOverload<const QString &>::of(&QComboBox::highlighted), this, &ComboBoxDelegate::testActivationString);
|
2020-10-26 15:32:09 +00:00
|
|
|
#endif
|
2020-05-16 07:46:30 +00:00
|
|
|
connect(comboDelegate, QOverload<int>::of(&QComboBox::activated), this, &ComboBoxDelegate::fakeActivation);
|
|
|
|
connect(comboPopup, &QAbstractItemView::entered, this, &ComboBoxDelegate::testActivationIndex);
|
|
|
|
connect(comboPopup, &QAbstractItemView::activated, this, &ComboBoxDelegate::fakeActivation);
|
2013-07-16 22:13:58 +00:00
|
|
|
currCombo.comboEditor = comboDelegate;
|
|
|
|
currCombo.currRow = index.row();
|
2014-02-28 04:09:57 +00:00
|
|
|
currCombo.model = const_cast<QAbstractItemModel *>(index.model());
|
2020-03-15 20:51:27 +00:00
|
|
|
currCombo.activeText = currCombo.model->data(index).toString();
|
2024-05-12 07:09:22 +00:00
|
|
|
currCombo.ignoreSelection = false;
|
2013-09-27 15:52:01 +00:00
|
|
|
|
2013-06-16 13:15:19 +00:00
|
|
|
return comboDelegate;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:59:58 +00:00
|
|
|
/* This Method is being called when the user *writes* something and press enter or tab,
|
|
|
|
* and it`s also called when the mouse walks over the list of choices from the ComboBox,
|
2013-10-03 23:04:51 +00:00
|
|
|
* One thing is important, if the user writes a *new* cylinder or weight type, it will
|
2013-09-26 22:59:58 +00:00
|
|
|
* be ADDED to the list, and the user will need to fill the other data.
|
|
|
|
*/
|
2020-05-16 07:46:30 +00:00
|
|
|
void ComboBoxDelegate::testActivationString(const QString &currText)
|
2013-07-16 22:13:58 +00:00
|
|
|
{
|
2013-09-26 22:59:58 +00:00
|
|
|
currCombo.activeText = currText.isEmpty() ? currCombo.comboEditor->currentText() : currText;
|
2013-07-16 22:13:58 +00:00
|
|
|
setModelData(currCombo.comboEditor, currCombo.model, QModelIndex());
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:46:30 +00:00
|
|
|
void ComboBoxDelegate::testActivationIndex(const QModelIndex &currIndex)
|
2014-04-24 18:17:30 +00:00
|
|
|
{
|
2020-05-16 07:46:30 +00:00
|
|
|
testActivationString(currIndex.data().toString());
|
2014-04-24 18:17:30 +00:00
|
|
|
}
|
|
|
|
|
Make the Qt ComboBox behave in a Better Way
So, the ComboBox is a beast, and when used on a Delegate
it's very hard to get things right, wich is a pitty, because
I overly like qt. So:
1 - Combobox needs to show the popup when user press ↓ and ↑ keys
2 - Combobox needs to select when user press enter, not twice.
3 - Combobox neesds to select when user selects from the mouse, not
pressing enter after.
4 - Combobox needs to not mess with stuff when moving around.
Everything that I listed there works on a non-delegate combobox,
but for some reason, a delegate missed those, so I reimplemented
all. not nice, but now we have a code that will work, I hope.
*fingers crossed*
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
2013-09-25 19:37:24 +00:00
|
|
|
// HACK, send a fake event so Qt thinks we hit 'enter' on the line edit.
|
2014-02-28 04:09:57 +00:00
|
|
|
void ComboBoxDelegate::fakeActivation()
|
|
|
|
{
|
Make the Qt ComboBox behave in a Better Way
So, the ComboBox is a beast, and when used on a Delegate
it's very hard to get things right, wich is a pitty, because
I overly like qt. So:
1 - Combobox needs to show the popup when user press ↓ and ↑ keys
2 - Combobox needs to select when user press enter, not twice.
3 - Combobox neesds to select when user selects from the mouse, not
pressing enter after.
4 - Combobox needs to not mess with stuff when moving around.
Everything that I listed there works on a non-delegate combobox,
but for some reason, a delegate missed those, so I reimplemented
all. not nice, but now we have a code that will work, I hope.
*fingers crossed*
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
2013-09-25 19:37:24 +00:00
|
|
|
/* this test is needed because as soon as I show the selector,
|
|
|
|
* the first item gots selected, this sending an activated signal,
|
|
|
|
* calling this fakeActivation code and setting as the current,
|
|
|
|
* thig that we don't want. so, let's just set the ignoreSelection
|
|
|
|
* to false and be happy, because the next activation ( by click
|
|
|
|
* or keypress) is real.
|
|
|
|
*/
|
2014-01-16 04:50:56 +00:00
|
|
|
if (currCombo.ignoreSelection) {
|
Make the Qt ComboBox behave in a Better Way
So, the ComboBox is a beast, and when used on a Delegate
it's very hard to get things right, wich is a pitty, because
I overly like qt. So:
1 - Combobox needs to show the popup when user press ↓ and ↑ keys
2 - Combobox needs to select when user press enter, not twice.
3 - Combobox neesds to select when user selects from the mouse, not
pressing enter after.
4 - Combobox needs to not mess with stuff when moving around.
Everything that I listed there works on a non-delegate combobox,
but for some reason, a delegate missed those, so I reimplemented
all. not nice, but now we have a code that will work, I hope.
*fingers crossed*
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
2013-09-25 19:37:24 +00:00
|
|
|
currCombo.ignoreSelection = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QKeyEvent ev(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
|
|
|
|
QStyledItemDelegate::eventFilter(currCombo.comboEditor, &ev);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
bool ComboBoxDelegate::eventFilter(QObject *object, QEvent *event)
|
2013-07-16 18:31:44 +00:00
|
|
|
{
|
|
|
|
// Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices.
|
2014-01-16 04:50:56 +00:00
|
|
|
if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
|
|
|
|
if (object == currCombo.comboEditor) { // the 'LineEdit' part
|
2014-02-28 04:09:57 +00:00
|
|
|
QKeyEvent *ev = static_cast<QKeyEvent *>(event);
|
2014-01-16 04:50:56 +00:00
|
|
|
if (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) {
|
Make the Qt ComboBox behave in a Better Way
So, the ComboBox is a beast, and when used on a Delegate
it's very hard to get things right, wich is a pitty, because
I overly like qt. So:
1 - Combobox needs to show the popup when user press ↓ and ↑ keys
2 - Combobox needs to select when user press enter, not twice.
3 - Combobox neesds to select when user selects from the mouse, not
pressing enter after.
4 - Combobox needs to not mess with stuff when moving around.
Everything that I listed there works on a non-delegate combobox,
but for some reason, a delegate missed those, so I reimplemented
all. not nice, but now we have a code that will work, I hope.
*fingers crossed*
Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
2013-09-25 19:37:24 +00:00
|
|
|
currCombo.ignoreSelection = true;
|
2014-05-22 18:40:22 +00:00
|
|
|
if (!currCombo.comboEditor->completer()->popup()->isVisible()) {
|
2014-03-19 21:29:35 +00:00
|
|
|
currCombo.comboEditor->showPopup();
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-17 14:47:23 +00:00
|
|
|
}
|
2020-04-04 21:02:32 +00:00
|
|
|
if (ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return)
|
2014-02-28 04:09:57 +00:00
|
|
|
currCombo.activeText = currCombo.comboEditor->currentText();
|
|
|
|
} else { // the 'Drop Down Menu' part.
|
|
|
|
QKeyEvent *ev = static_cast<QKeyEvent *>(event);
|
2014-01-16 04:50:56 +00:00
|
|
|
if (ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return ||
|
2014-02-28 04:09:57 +00:00
|
|
|
ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Backtab ||
|
2014-01-16 04:50:56 +00:00
|
|
|
ev->key() == Qt::Key_Escape) {
|
2013-07-17 14:47:23 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:50:56 +00:00
|
|
|
return QStyledItemDelegate::eventFilter(object, event);
|
2013-07-16 18:31:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
|
2013-06-16 15:33:27 +00:00
|
|
|
{
|
|
|
|
QRect defaultRect = option.rect;
|
2014-02-28 04:09:57 +00:00
|
|
|
defaultRect.setX(defaultRect.x() - 1);
|
|
|
|
defaultRect.setY(defaultRect.y() - 1);
|
|
|
|
defaultRect.setWidth(defaultRect.width() + 2);
|
|
|
|
defaultRect.setHeight(defaultRect.height() + 2);
|
2014-01-16 04:50:56 +00:00
|
|
|
editor->setGeometry(defaultRect);
|
2013-06-16 15:33:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
void TankInfoDelegate::setModelData(QWidget *, QAbstractItemModel *model, const QModelIndex &index) const
|
2013-05-23 18:33:20 +00:00
|
|
|
{
|
2020-02-27 18:46:48 +00:00
|
|
|
QAbstractItemModel *mymodel = currCombo.model;
|
2020-12-11 21:52:20 +00:00
|
|
|
QString cylinderName = currCombo.activeText.trimmed();
|
2020-12-11 21:56:26 +00:00
|
|
|
if (cylinderName.isEmpty()) {
|
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, CylindersModel::TEMP_ROLE);
|
2020-12-11 21:52:20 +00:00
|
|
|
return;
|
2020-12-11 21:56:26 +00:00
|
|
|
}
|
2013-05-24 05:56:12 +00:00
|
|
|
|
2024-05-03 21:18:45 +00:00
|
|
|
auto [tankSize, tankPressure] = get_tank_info_data(tank_info_table, cylinderName.toStdString());
|
2020-02-28 19:38:04 +00:00
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, CylindersModel::TEMP_ROLE);
|
2024-06-09 04:16:26 +00:00
|
|
|
mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure.mbar, CylindersModel::TEMP_ROLE);
|
|
|
|
mymodel->setData(IDX(CylindersModel::SIZE), tankSize.mliter, CylindersModel::TEMP_ROLE);
|
2013-05-23 18:33:20 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
static QAbstractItemModel *createTankInfoModel(QWidget *parent)
|
|
|
|
{
|
|
|
|
return new TankInfoModel(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
TankInfoDelegate::TankInfoDelegate(QObject *parent) : ComboBoxDelegate(&createTankInfoModel, parent, true)
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
2013-07-18 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void TankInfoDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint hint)
|
2013-07-18 13:24:02 +00:00
|
|
|
{
|
2020-02-28 19:38:04 +00:00
|
|
|
QAbstractItemModel *mymodel = currCombo.model;
|
|
|
|
// Ugly hack: We misuse setData() with COMMIT_ROLE or REVERT_ROLE to commit or
|
|
|
|
// revert the current row. We send in the type, because we may get multiple
|
|
|
|
// end events and thus can prevent multiple commits.
|
|
|
|
if (hint == QAbstractItemDelegate::RevertModelCache)
|
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::REVERT_ROLE);
|
|
|
|
else
|
|
|
|
mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::COMMIT_ROLE);
|
2013-07-18 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 14:55:44 +00:00
|
|
|
TankUseDelegate::TankUseDelegate(QObject *parent) : QStyledItemDelegate(parent), currentdc(nullptr)
|
2014-11-17 14:03:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-17 14:55:44 +00:00
|
|
|
void TankUseDelegate::setCurrentDC(divecomputer *dc)
|
|
|
|
{
|
|
|
|
currentdc = dc;
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
QWidget *TankUseDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
|
2014-11-17 14:03:37 +00:00
|
|
|
{
|
|
|
|
QComboBox *comboBox = new QComboBox(parent);
|
2022-09-17 14:55:44 +00:00
|
|
|
if (!currentdc)
|
2023-01-21 00:09:44 +00:00
|
|
|
return comboBox;
|
2022-09-17 14:55:44 +00:00
|
|
|
bool isCcrDive = currentdc->divemode == CCR;
|
2023-01-21 00:09:44 +00:00
|
|
|
for (int i = 0; i < NUM_GAS_USE; i++) {
|
2022-09-17 14:55:44 +00:00
|
|
|
if (isCcrDive || (i != DILUENT && i != OXYGEN))
|
2023-01-21 00:09:44 +00:00
|
|
|
comboBox->addItem(gettextFromC::tr(cylinderuse_text[i]));
|
|
|
|
}
|
2014-11-17 14:03:37 +00:00
|
|
|
return comboBox;
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void TankUseDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
2014-11-17 14:03:37 +00:00
|
|
|
{
|
2020-05-16 07:17:41 +00:00
|
|
|
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
|
2014-11-17 14:03:37 +00:00
|
|
|
QString indexString = index.data().toString();
|
2018-02-25 12:51:41 +00:00
|
|
|
comboBox->setCurrentIndex(cylinderuse_from_text(qPrintable(indexString)));
|
2014-11-17 14:03:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void TankUseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
2014-11-17 14:03:37 +00:00
|
|
|
{
|
2020-05-16 07:17:41 +00:00
|
|
|
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
|
2023-01-21 00:09:44 +00:00
|
|
|
model->setData(index, cylinderuse_from_text(qPrintable(comboBox->currentText())));
|
2014-11-17 14:03:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 14:55:44 +00:00
|
|
|
SensorDelegate::SensorDelegate(QObject *parent) : QStyledItemDelegate(parent), currentdc(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2022-09-02 20:51:19 +00:00
|
|
|
|
2022-09-17 14:55:44 +00:00
|
|
|
void SensorDelegate::setCurrentDC(divecomputer *dc)
|
2022-09-02 20:51:19 +00:00
|
|
|
{
|
2022-09-17 14:55:44 +00:00
|
|
|
currentdc = dc;
|
2022-09-02 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *SensorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QComboBox *comboBox = new QComboBox(parent);
|
2022-09-17 14:55:44 +00:00
|
|
|
|
|
|
|
if (!currentdc)
|
|
|
|
return comboBox;
|
|
|
|
|
2022-09-02 20:51:19 +00:00
|
|
|
std::vector<int16_t> sensors;
|
2024-05-19 10:38:38 +00:00
|
|
|
for (const auto &sample: currentdc->samples) {
|
2022-09-02 20:51:19 +00:00
|
|
|
for (int s = 0; s < MAX_SENSORS; ++s) {
|
|
|
|
if (sample.pressure[s].mbar) {
|
|
|
|
if (std::find(sensors.begin(), sensors.end(), sample.sensor[s]) == sensors.end())
|
|
|
|
sensors.push_back(sample.sensor[s]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(sensors.begin(), sensors.end());
|
|
|
|
for (auto s : sensors)
|
|
|
|
comboBox->addItem(QString::number(s));
|
|
|
|
|
|
|
|
comboBox->setCurrentIndex(-1);
|
|
|
|
QString indexString = index.data().toString();
|
|
|
|
if (!indexString.isEmpty())
|
|
|
|
comboBox->setCurrentText(indexString);
|
|
|
|
return comboBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
|
|
|
|
model->setData(index, comboBox->currentText());
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void WSInfoDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint hint)
|
2013-07-18 14:53:47 +00:00
|
|
|
{
|
2019-11-04 19:20:32 +00:00
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
|
|
|
if (hint == QAbstractItemDelegate::RevertModelCache)
|
|
|
|
mymodel->clearTempWS();
|
|
|
|
else
|
|
|
|
mymodel->commitTempWS();
|
2013-07-18 14:53:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void WSInfoDelegate::setModelData(QWidget *, QAbstractItemModel *, const QModelIndex &) const
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
2013-07-16 22:13:58 +00:00
|
|
|
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
|
2024-05-29 05:03:03 +00:00
|
|
|
std::string weightName = currCombo.activeText.toStdString();
|
|
|
|
weight_t weight = get_weightsystem_weight(weightName.c_str());
|
2013-07-18 12:01:37 +00:00
|
|
|
|
2024-05-29 05:03:03 +00:00
|
|
|
mymodel->setTempWS(currCombo.currRow, weightsystem_t( weight, std::move(weightName), false ));
|
2013-05-24 01:40:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
static QAbstractItemModel *createWSInfoModel(QWidget *parent)
|
|
|
|
{
|
|
|
|
return new WSInfoModel(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WSInfoDelegate::WSInfoDelegate(QObject *parent) : ComboBoxDelegate(&createWSInfoModel, parent, true)
|
2013-05-24 01:40:16 +00:00
|
|
|
{
|
|
|
|
}
|
2013-07-18 13:24:02 +00:00
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void AirTypesDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint)
|
2013-08-30 10:14:30 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void AirTypesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
2013-08-30 10:14:30 +00:00
|
|
|
{
|
2013-08-30 18:06:26 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2014-02-28 04:09:57 +00:00
|
|
|
QComboBox *combo = qobject_cast<QComboBox *>(editor);
|
2016-07-06 12:40:28 +00:00
|
|
|
model->setData(index, QVariant(combo->currentIndex()));
|
2013-08-30 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
AirTypesDelegate::AirTypesDelegate(const dive &d, QObject *parent) :
|
2024-05-12 06:43:08 +00:00
|
|
|
ComboBoxDelegate([&d] (QWidget *parent) { return new GasSelectionModel(d, parent); },
|
|
|
|
parent, false)
|
2013-08-30 10:14:30 +00:00
|
|
|
{
|
|
|
|
}
|
2013-10-03 14:50:40 +00:00
|
|
|
|
2020-05-16 07:17:41 +00:00
|
|
|
void DiveTypesDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint)
|
2018-05-08 15:26:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTypesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
QComboBox *combo = qobject_cast<QComboBox *>(editor);
|
|
|
|
model->setData(index, QVariant(combo->currentIndex()));
|
|
|
|
}
|
|
|
|
|
2024-03-30 12:40:00 +00:00
|
|
|
static QAbstractItemModel *createDiveTypeSelectionModel(QWidget *parent)
|
|
|
|
{
|
|
|
|
return new DiveTypeSelectionModel(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveTypesDelegate::DiveTypesDelegate(QObject *parent) : ComboBoxDelegate(&createDiveTypeSelectionModel, parent, false)
|
2018-05-08 15:26:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-12 12:24:25 +00:00
|
|
|
SpinBoxDelegate::SpinBoxDelegate(int min, int max, int step, QObject *parent):
|
2014-07-11 20:42:43 +00:00
|
|
|
QStyledItemDelegate(parent),
|
|
|
|
min(min),
|
2014-07-12 12:24:25 +00:00
|
|
|
max(max),
|
|
|
|
step(step)
|
2014-07-11 20:42:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QSpinBox *w = qobject_cast<QSpinBox*>(QStyledItemDelegate::createEditor(parent, option, index));
|
|
|
|
w->setRange(min,max);
|
2014-07-12 12:24:25 +00:00
|
|
|
w->setSingleStep(step);
|
2014-07-11 20:42:43 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2014-07-12 12:24:25 +00:00
|
|
|
DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(double min, double max, double step, QObject *parent):
|
2014-07-11 20:42:43 +00:00
|
|
|
QStyledItemDelegate(parent),
|
|
|
|
min(min),
|
2014-07-12 12:24:25 +00:00
|
|
|
max(max),
|
|
|
|
step(step)
|
2014-07-11 20:42:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QDoubleSpinBox *w = qobject_cast<QDoubleSpinBox*>(QStyledItemDelegate::createEditor(parent, option, index));
|
|
|
|
w->setRange(min,max);
|
2014-07-12 12:24:25 +00:00
|
|
|
w->setSingleStep(step);
|
2014-07-11 20:42:43 +00:00
|
|
|
return w;
|
|
|
|
}
|
2014-07-17 23:10:44 +00:00
|
|
|
|
2024-05-04 17:15:47 +00:00
|
|
|
LocationFilterDelegate::LocationFilterDelegate(QObject *)
|
2015-07-01 22:31:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-25 07:35:46 +00:00
|
|
|
void LocationFilterDelegate::setCurrentLocation(location_t loc)
|
|
|
|
{
|
|
|
|
currentLocation = loc;
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:08:08 +00:00
|
|
|
void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &origIdx) const
|
2015-07-01 22:31:56 +00:00
|
|
|
{
|
2015-07-02 00:04:03 +00:00
|
|
|
QFont fontBigger = qApp->font();
|
|
|
|
QFont fontSmaller = qApp->font();
|
|
|
|
QFontMetrics fmBigger(fontBigger);
|
2017-03-25 15:06:46 +00:00
|
|
|
QStyleOptionViewItem opt = option;
|
2020-05-16 07:17:41 +00:00
|
|
|
const QAbstractProxyModel *proxyModel = dynamic_cast<const QAbstractProxyModel *>(origIdx.model());
|
2017-12-28 18:54:26 +00:00
|
|
|
if (!proxyModel)
|
|
|
|
return;
|
2015-07-16 21:08:08 +00:00
|
|
|
QModelIndex index = proxyModel->mapToSource(origIdx);
|
2015-07-02 00:04:03 +00:00
|
|
|
QStyledItemDelegate::initStyleOption(&opt, index);
|
|
|
|
QString diveSiteName = index.data().toString();
|
2015-07-14 21:43:47 +00:00
|
|
|
QString bottomText;
|
|
|
|
QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
|
2018-10-28 20:16:42 +00:00
|
|
|
struct dive_site *ds =
|
|
|
|
index.model()->data(index.model()->index(index.row(), LocationInformationModel::DIVESITE)).value<dive_site *>();
|
2019-04-25 07:35:46 +00:00
|
|
|
bool currentDiveHasGPS = has_location(¤tLocation);
|
2015-07-14 21:43:47 +00:00
|
|
|
//Special case: do not show name, but instead, show
|
2015-07-16 18:19:31 +00:00
|
|
|
if (index.row() < 2) {
|
2015-07-14 21:43:47 +00:00
|
|
|
diveSiteName = index.data().toString();
|
|
|
|
bottomText = index.data(Qt::ToolTipRole).toString();
|
|
|
|
goto print_part;
|
|
|
|
}
|
|
|
|
|
2015-07-02 00:04:03 +00:00
|
|
|
if (!ds)
|
|
|
|
return;
|
|
|
|
|
2015-07-14 18:44:40 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
if (prefs.geocoding.category[i] == TC_NONE)
|
|
|
|
continue;
|
2024-05-04 11:39:04 +00:00
|
|
|
std::string value = taxonomy_get_value(ds->taxonomy, prefs.geocoding.category[i]);
|
|
|
|
if (!value.empty())
|
2015-07-08 14:09:51 +00:00
|
|
|
continue;
|
|
|
|
if(!bottomText.isEmpty())
|
2015-07-14 18:44:40 +00:00
|
|
|
bottomText += " / ";
|
2024-05-04 11:39:04 +00:00
|
|
|
bottomText += QString::fromStdString(value);
|
2015-07-08 14:09:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 08:05:47 +00:00
|
|
|
if (bottomText.isEmpty())
|
|
|
|
bottomText = printGPSCoords(&ds->location);
|
2015-07-02 00:04:03 +00:00
|
|
|
|
2024-06-30 15:38:36 +00:00
|
|
|
if (ds->has_gps_location() && currentDiveHasGPS) {
|
2015-07-14 18:35:04 +00:00
|
|
|
// so we are showing a completion and both the current dive site and the completion
|
|
|
|
// have a GPS fix... so let's show the distance
|
2024-05-04 21:20:53 +00:00
|
|
|
if (ds->location == currentLocation) {
|
2015-07-14 18:35:04 +00:00
|
|
|
bottomText += tr(" (same GPS fix)");
|
|
|
|
} else {
|
2024-05-11 13:18:37 +00:00
|
|
|
int distanceMeters = get_distance(ds->location, currentLocation);
|
2015-07-14 18:35:04 +00:00
|
|
|
QString distance = distance_string(distanceMeters);
|
2024-05-11 13:01:37 +00:00
|
|
|
size_t nr = ds->nr_of_dives();
|
2015-07-18 20:34:05 +00:00
|
|
|
bottomText += tr(" (~%1 away").arg(distance);
|
2015-10-29 08:24:47 +00:00
|
|
|
bottomText += tr(", %n dive(s) here)", "", nr);
|
2015-07-14 18:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-15 17:37:25 +00:00
|
|
|
if (bottomText.isEmpty()) {
|
2019-04-25 07:35:46 +00:00
|
|
|
if (currentDiveHasGPS)
|
2015-07-15 17:37:25 +00:00
|
|
|
bottomText = tr("(no existing GPS data, add GPS fix from this dive)");
|
|
|
|
else
|
|
|
|
bottomText = tr("(no GPS data)");
|
|
|
|
}
|
|
|
|
bottomText = tr("Pick site: ") + bottomText;
|
|
|
|
|
2015-07-14 22:33:28 +00:00
|
|
|
print_part:
|
|
|
|
|
2015-07-08 13:11:13 +00:00
|
|
|
fontBigger.setPointSize(fontBigger.pointSize() + 1);
|
2015-07-02 00:04:03 +00:00
|
|
|
fontBigger.setBold(true);
|
2015-10-07 14:13:00 +00:00
|
|
|
QPen textPen = QPen(option.state & QStyle::State_Selected ? option.palette.highlightedText().color() : option.palette.text().color(), 1);
|
2015-07-02 00:04:03 +00:00
|
|
|
|
2015-07-08 14:13:06 +00:00
|
|
|
initStyleOption(&opt, index);
|
|
|
|
opt.text = QString();
|
2015-07-14 21:43:47 +00:00
|
|
|
opt.icon = QIcon();
|
2015-07-21 00:21:37 +00:00
|
|
|
painter->setClipRect(option.rect);
|
2015-07-08 14:13:06 +00:00
|
|
|
|
2015-07-02 00:04:03 +00:00
|
|
|
painter->save();
|
2015-10-07 14:13:00 +00:00
|
|
|
if (option.state & QStyle::State_Selected) {
|
|
|
|
painter->setPen(QPen(opt.palette.highlight().color().darker()));
|
|
|
|
painter->setBrush(opt.palette.highlight());
|
2017-03-23 01:13:49 +00:00
|
|
|
const int pad = 1;
|
|
|
|
const int pad2 = pad * 2;
|
|
|
|
const int rounding = 5;
|
2015-10-07 14:13:00 +00:00
|
|
|
painter->drawRoundedRect(option.rect.x() + pad,
|
2017-03-23 01:13:49 +00:00
|
|
|
option.rect.y() + pad,
|
|
|
|
option.rect.width() - pad2,
|
|
|
|
option.rect.height() - pad2,
|
|
|
|
rounding, rounding);
|
2015-10-07 14:13:00 +00:00
|
|
|
}
|
2015-07-21 00:25:10 +00:00
|
|
|
painter->setPen(textPen);
|
2015-07-02 00:04:03 +00:00
|
|
|
painter->setFont(fontBigger);
|
2017-03-23 01:13:49 +00:00
|
|
|
const int textPad = 5;
|
2015-10-07 14:13:00 +00:00
|
|
|
painter->drawText(option.rect.x() + textPad, option.rect.y() + fmBigger.boundingRect("YH").height(), diveSiteName);
|
2015-07-15 17:37:25 +00:00
|
|
|
double pointSize = fontSmaller.pointSizeF();
|
|
|
|
fontSmaller.setPointSizeF(0.9 * pointSize);
|
2015-07-02 00:04:03 +00:00
|
|
|
painter->setFont(fontSmaller);
|
2015-10-07 14:13:00 +00:00
|
|
|
painter->drawText(option.rect.x() + textPad, option.rect.y() + fmBigger.boundingRect("YH").height() * 2, bottomText);
|
2015-07-01 22:31:56 +00:00
|
|
|
painter->restore();
|
2015-07-14 21:43:47 +00:00
|
|
|
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
painter->save();
|
|
|
|
painter->drawPixmap(
|
|
|
|
option.rect.x() + option.rect.width() - 24,
|
|
|
|
option.rect.y() + option.rect.height() - 24, icon.pixmap(20,20));
|
|
|
|
painter->restore();
|
|
|
|
}
|
2015-07-01 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize LocationFilterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
2015-07-02 00:04:03 +00:00
|
|
|
QFont fontBigger = qApp->font();
|
2015-07-08 13:11:13 +00:00
|
|
|
fontBigger.setPointSize(fontBigger.pointSize());
|
2015-07-02 00:04:03 +00:00
|
|
|
fontBigger.setBold(true);
|
|
|
|
|
|
|
|
QFontMetrics fmBigger(fontBigger);
|
|
|
|
|
|
|
|
QFont fontSmaller = qApp->font();
|
|
|
|
QFontMetrics fmSmaller(fontSmaller);
|
|
|
|
|
|
|
|
QSize retSize = QStyledItemDelegate::sizeHint(option, index);
|
2015-07-08 13:11:13 +00:00
|
|
|
retSize.setHeight(
|
|
|
|
fmBigger.boundingRect("Yellow House").height() + 5 /*spacing*/ +
|
|
|
|
fmSmaller.boundingRect("Yellow House").height());
|
|
|
|
|
2015-07-02 00:04:03 +00:00
|
|
|
return retSize;
|
2015-07-01 22:31:56 +00:00
|
|
|
}
|