2020-11-25 23:47:29 +08:00
|
|
|
#include "tracematheditdialog.h"
|
|
|
|
#include "ui_tracematheditdialog.h"
|
|
|
|
|
2020-11-27 23:31:05 +08:00
|
|
|
#include "medianfilter.h"
|
|
|
|
|
2020-11-25 23:47:29 +08:00
|
|
|
TraceMathEditDialog::TraceMathEditDialog(Trace &t, QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::TraceMathEditDialog)
|
|
|
|
{
|
|
|
|
auto model = new MathModel(t);
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->view->setModel(model);
|
2020-11-26 04:20:31 +08:00
|
|
|
|
|
|
|
connect(ui->view->selectionModel(), &QItemSelectionModel::currentRowChanged, [=](const QModelIndex ¤t, const QModelIndex &previous){
|
|
|
|
if(!current.isValid()) {
|
|
|
|
ui->bDelete->setEnabled(false);
|
|
|
|
ui->bMoveUp->setEnabled(false);
|
|
|
|
ui->bMoveDown->setEnabled(false);
|
|
|
|
} else {
|
|
|
|
ui->bDelete->setEnabled(true);
|
|
|
|
ui->bMoveUp->setEnabled(current.row() > 1);
|
|
|
|
ui->bMoveDown->setEnabled(current.row() + 1 < model->rowCount());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-27 23:31:05 +08:00
|
|
|
connect(ui->bAdd, &QPushButton::clicked, [=](){
|
|
|
|
model->addOperation(new Math::MedianFilter);
|
|
|
|
});
|
2020-11-26 04:20:31 +08:00
|
|
|
connect(ui->bDelete, &QPushButton::clicked, [=](){
|
|
|
|
model->deleteRow(ui->view->currentIndex().row());
|
|
|
|
});
|
|
|
|
connect(ui->bMoveUp, &QPushButton::clicked, [&](){
|
|
|
|
auto index = ui->view->currentIndex();
|
|
|
|
t.swapMathOrder(index.row() - 1);
|
|
|
|
model->rowsSwapped(index.row() - 1);
|
|
|
|
ui->view->setCurrentIndex(index.sibling(index.row() - 1, 0));
|
|
|
|
});
|
|
|
|
connect(ui->bMoveDown, &QPushButton::clicked, [&](){
|
|
|
|
auto index = ui->view->currentIndex();
|
|
|
|
t.swapMathOrder(index.row());
|
|
|
|
model->rowsSwapped(index.row());
|
|
|
|
ui->view->setCurrentIndex(index.sibling(index.row() + 1, 0));
|
|
|
|
});
|
2020-11-25 23:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TraceMathEditDialog::~TraceMathEditDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
MathModel::MathModel(Trace &t, QObject *parent)
|
|
|
|
: QAbstractTableModel(parent),
|
|
|
|
t(t)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int MathModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2020-11-26 04:20:31 +08:00
|
|
|
return t.getMathOperations().size();
|
2020-11-25 23:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int MathModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return ColIndexLast;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MathModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
2020-11-26 04:20:31 +08:00
|
|
|
auto math = t.getMathOperations().at(index.row());
|
2020-11-25 23:47:29 +08:00
|
|
|
switch(index.column()) {
|
2020-11-26 04:20:31 +08:00
|
|
|
// case ColIndexEnabled:
|
|
|
|
// if(role == Qt::CheckStateRole) {
|
|
|
|
// return math.enabled ? Qt::Checked : Qt::Unchecked;
|
|
|
|
// }
|
|
|
|
// break;
|
2020-11-25 23:47:29 +08:00
|
|
|
case ColIndexDescription:
|
|
|
|
if(role == Qt::DisplayRole) {
|
|
|
|
return math.math->description();
|
2020-11-26 04:20:31 +08:00
|
|
|
} else if(role == Qt::CheckStateRole){
|
|
|
|
return math.enabled ? Qt::Checked : Qt::Unchecked;
|
2020-11-25 23:47:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MathModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch(section) {
|
2020-11-26 04:20:31 +08:00
|
|
|
// case ColIndexEnabled: return "Enabled"; break;
|
2020-11-25 23:47:29 +08:00
|
|
|
case ColIndexDescription: return "Description"; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags MathModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
int flags = Qt::NoItemFlags;
|
2020-11-26 04:20:31 +08:00
|
|
|
if(index.row() >= 1) {
|
2020-11-25 23:47:29 +08:00
|
|
|
// the first entry is always the trace itself and not enabled
|
2020-11-26 04:20:31 +08:00
|
|
|
flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
2020-11-25 23:47:29 +08:00
|
|
|
}
|
|
|
|
switch(index.column()) {
|
2020-11-26 04:20:31 +08:00
|
|
|
case ColIndexDescription: flags |= Qt::ItemIsUserCheckable; break;
|
2020-11-25 23:47:29 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (Qt::ItemFlags) flags;
|
|
|
|
}
|
2020-11-26 04:20:31 +08:00
|
|
|
|
2020-11-27 23:31:05 +08:00
|
|
|
void MathModel::addOperation(TraceMath *math)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), t.getMathOperations().size(), t.getMathOperations().size());
|
|
|
|
t.addMathOperation(math);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2020-11-26 04:20:31 +08:00
|
|
|
void MathModel::deleteRow(unsigned int row)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
t.removeMathOperation(row);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathModel::rowsSwapped(unsigned int top)
|
|
|
|
{
|
|
|
|
// emit dataChanged(createIndex(top, 0), createIndex(top+1, ColIndexLast - 1));
|
|
|
|
}
|