2018-10-13 11:52:08 +02:00
|
|
|
#include "desktop-widgets/filterwidget2.h"
|
2020-05-18 07:20:09 +02:00
|
|
|
#include "desktop-widgets/filterconstraintwidget.h"
|
2018-10-13 11:52:08 +02:00
|
|
|
#include "desktop-widgets/simplewidgets.h"
|
2019-01-25 22:41:07 +01:00
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2019-01-20 21:01:00 +01:00
|
|
|
#include "core/qthelper.h"
|
2019-11-17 16:23:00 +01:00
|
|
|
#include "core/divelist.h"
|
2019-01-20 21:01:00 +01:00
|
|
|
#include "core/settings/qPrefUnit.h"
|
2018-10-13 11:52:08 +02:00
|
|
|
|
|
|
|
#include <QDoubleSpinBox>
|
|
|
|
|
2019-02-18 12:43:34 -08:00
|
|
|
FilterWidget2::FilterWidget2(QWidget* parent) :
|
|
|
|
QWidget(parent),
|
2020-05-18 07:20:09 +02:00
|
|
|
ignoreSignal(false),
|
|
|
|
validFilter(false)
|
2018-10-13 11:52:08 +02:00
|
|
|
{
|
2019-01-17 23:01:57 +01:00
|
|
|
ui.setupUi(this);
|
2018-12-06 20:07:47 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
QMenu *newConstraintMenu = new QMenu(this);
|
|
|
|
QStringList constraintTypes = filter_constraint_type_list_translated();
|
|
|
|
for (int i = 0; i < constraintTypes.size(); ++i) {
|
|
|
|
filter_constraint_type type = filter_constraint_type_from_index(i);
|
|
|
|
newConstraintMenu->addAction(constraintTypes[i], [this, type]() { addConstraint(type); });
|
|
|
|
}
|
|
|
|
ui.addConstraintButton->setMenu(newConstraintMenu);
|
|
|
|
ui.addConstraintButton->setPopupMode(QToolButton::InstantPopup);
|
|
|
|
ui.constraintTable->setColumnStretch(4, 1); // The fifth column is were the actual constraint resides - stretch that.
|
|
|
|
|
|
|
|
connect(ui.clear, &QToolButton::clicked, this, &FilterWidget2::clearFilter);
|
|
|
|
connect(ui.close, &QToolButton::clicked, this, &FilterWidget2::closeFilter);
|
|
|
|
connect(ui.fullText, &QLineEdit::textChanged, this, &FilterWidget2::updateFilter);
|
|
|
|
connect(ui.fulltextStringMode, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &FilterWidget2::updateFilter);
|
|
|
|
|
|
|
|
connect(&constraintModel, &FilterConstraintModel::rowsInserted, this, &FilterWidget2::constraintAdded);
|
|
|
|
connect(&constraintModel, &FilterConstraintModel::rowsRemoved, this, &FilterWidget2::constraintRemoved);
|
|
|
|
connect(&constraintModel, &FilterConstraintModel::dataChanged, this, &FilterWidget2::constraintChanged);
|
|
|
|
connect(&constraintModel, &FilterConstraintModel::modelReset, this, &FilterWidget2::constraintsReset);
|
2019-01-20 21:15:46 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
clearFilter();
|
|
|
|
}
|
2019-01-01 18:49:56 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
FilterWidget2::~FilterWidget2()
|
|
|
|
{
|
|
|
|
}
|
2019-01-20 21:01:00 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
void FilterWidget2::constraintAdded(const QModelIndex &parent, int first, int last)
|
|
|
|
{
|
|
|
|
if (parent.isValid() || last < first)
|
|
|
|
return; // We only support one level
|
|
|
|
constraintWidgets.reserve(constraintWidgets.size() + 1 + last - first);
|
|
|
|
for (int i = last + 1; i < (int)constraintWidgets.size(); ++i)
|
|
|
|
constraintWidgets[i]->moveToRow(i);
|
|
|
|
for (int i = first; i <= last; ++i) {
|
|
|
|
QModelIndex idx = constraintModel.index(i, 0);
|
|
|
|
constraintWidgets.emplace(constraintWidgets.begin() + i, new FilterConstraintWidget(&constraintModel, idx, ui.constraintTable));
|
|
|
|
}
|
|
|
|
updateFilter();
|
|
|
|
}
|
2020-04-22 00:05:52 +02:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
void FilterWidget2::constraintRemoved(const QModelIndex &parent, int first, int last)
|
|
|
|
{
|
|
|
|
if (parent.isValid() || last < first)
|
|
|
|
return; // We only support one level
|
|
|
|
constraintWidgets.erase(constraintWidgets.begin() + first, constraintWidgets.begin() + last + 1);
|
|
|
|
for (int i = first; i < (int)constraintWidgets.size(); ++i)
|
|
|
|
constraintWidgets[i]->moveToRow(i);
|
|
|
|
updateFilter();
|
|
|
|
}
|
2019-01-22 09:38:57 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
void FilterWidget2::constraintChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
|
|
|
|
{
|
|
|
|
// Note: this may appear strange, but we don't update the widget if we get
|
|
|
|
// a constraint-changed signal from the model. The reason being that the user
|
|
|
|
// is currently editing the constraint and we don't want to bother them by
|
|
|
|
// overwriting strings with canonicalized data.
|
|
|
|
updateFilter();
|
|
|
|
}
|
2019-01-25 21:30:43 +01:00
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
void FilterWidget2::constraintsReset()
|
|
|
|
{
|
|
|
|
constraintWidgets.clear();
|
|
|
|
int count = constraintModel.rowCount(QModelIndex());
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
QModelIndex idx = constraintModel.index(i, 0);
|
|
|
|
constraintWidgets.emplace_back(new FilterConstraintWidget(&constraintModel, idx, ui.constraintTable));
|
|
|
|
}
|
|
|
|
updateFilter();
|
2019-01-25 22:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilterWidget2::clearFilter()
|
|
|
|
{
|
2020-05-18 07:20:09 +02:00
|
|
|
ignoreSignal = true; // Prevent signals to force filter recalculation (TODO: check if necessary)
|
|
|
|
ui.fulltextStringMode->setCurrentIndex((int)StringFilterMode::STARTSWITH);
|
|
|
|
ui.fullText->clear();
|
2019-01-25 22:11:30 +01:00
|
|
|
ignoreSignal = false;
|
2020-05-18 07:20:09 +02:00
|
|
|
constraintModel.reload({}); // Causes a filter reload
|
2019-01-20 21:01:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 22:41:07 +01:00
|
|
|
void FilterWidget2::closeFilter()
|
|
|
|
{
|
2019-05-10 19:51:43 +02:00
|
|
|
MainWindow::instance()->setApplicationState(ApplicationState::Default);
|
2019-01-25 22:41:07 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 11:52:08 +02:00
|
|
|
void FilterWidget2::updateFilter()
|
|
|
|
{
|
2019-01-25 22:11:30 +01:00
|
|
|
if (ignoreSignal)
|
|
|
|
return;
|
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
FilterData filterData;
|
2020-02-16 22:31:06 +01:00
|
|
|
filterData.fulltextStringMode = (StringFilterMode)ui.fulltextStringMode->currentIndex();
|
2020-05-18 07:20:09 +02:00
|
|
|
filterData.fullText = ui.fullText->text();
|
|
|
|
filterData.constraints = constraintModel.getConstraints();
|
|
|
|
validFilter = filterData.validFilter();
|
|
|
|
DiveFilter::instance()->setFilter(filterData);
|
2019-01-01 18:49:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 15:24:11 +01:00
|
|
|
void FilterWidget2::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::showEvent(event);
|
2020-02-18 11:54:57 +01:00
|
|
|
ui.fullText->setFocus();
|
2020-05-18 07:20:09 +02:00
|
|
|
updateFilter();
|
2018-11-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilterWidget2::hideEvent(QHideEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::hideEvent(event);
|
2018-12-16 20:33:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:20:09 +02:00
|
|
|
void FilterWidget2::addConstraint(filter_constraint_type type)
|
2018-12-16 20:33:37 +01:00
|
|
|
{
|
2020-05-18 07:20:09 +02:00
|
|
|
constraintModel.addConstraint(type);
|
2019-02-18 12:47:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FilterWidget2::shownText()
|
|
|
|
{
|
2020-05-18 07:20:09 +02:00
|
|
|
if (validFilter)
|
2019-11-17 16:23:00 +01:00
|
|
|
return tr("%L1/%L2 shown").arg(shown_dives).arg(dive_table.nr);
|
2019-02-18 12:47:20 -08:00
|
|
|
else
|
|
|
|
return tr("%L1 dives").arg(dive_table.nr);
|
2018-10-13 11:52:08 +02:00
|
|
|
}
|