2020-11-25 23:47:29 +08:00
|
|
|
#include "tracematheditdialog.h"
|
|
|
|
#include "ui_tracematheditdialog.h"
|
2020-11-28 20:57:22 +08:00
|
|
|
#include <QHeaderView>
|
2020-11-25 23:47:29 +08:00
|
|
|
|
2020-11-29 02:32:18 +08:00
|
|
|
#include "ui_newtracemathdialog.h"
|
|
|
|
namespace Ui {
|
|
|
|
class NewTraceMathDialog;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-11-28 20:57:22 +08:00
|
|
|
QHeaderView *headerView = ui->view->horizontalHeader();
|
|
|
|
headerView->setSectionResizeMode(MathModel::ColIndexDescription, QHeaderView::Stretch);
|
|
|
|
headerView->setSectionResizeMode(MathModel::ColIndexStatus, QHeaderView::ResizeToContents);
|
|
|
|
headerView->setSectionResizeMode(MathModel::ColIndexDomain, QHeaderView::ResizeToContents);
|
|
|
|
|
2020-11-26 04:20:31 +08:00
|
|
|
connect(ui->view->selectionModel(), &QItemSelectionModel::currentRowChanged, [=](const QModelIndex ¤t, const QModelIndex &previous){
|
2020-11-29 02:32:18 +08:00
|
|
|
Q_UNUSED(previous)
|
2020-11-26 04:20:31 +08:00
|
|
|
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-28 20:57:22 +08:00
|
|
|
connect(ui->view, &QTableView::doubleClicked, [&](const QModelIndex &index) {
|
|
|
|
if(index.isValid()) {
|
|
|
|
auto math = t.getMathOperations().at(index.row()).math;
|
|
|
|
math->edit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-27 23:31:05 +08:00
|
|
|
connect(ui->bAdd, &QPushButton::clicked, [=](){
|
2020-11-29 02:32:18 +08:00
|
|
|
auto d = new QDialog();
|
|
|
|
auto ui = new Ui::NewTraceMathDialog();
|
|
|
|
ui->setupUi(d);
|
|
|
|
for(int i = 0; i < (int) TraceMath::Type::Last;i++) {
|
|
|
|
auto info = TraceMath::getInfo(static_cast<TraceMath::Type>(i));
|
|
|
|
ui->list->addItem(info.name);
|
|
|
|
if(!info.explanationWidget) {
|
|
|
|
info.explanationWidget = new QWidget();
|
|
|
|
}
|
|
|
|
ui->stack->addWidget(info.explanationWidget);
|
|
|
|
}
|
|
|
|
// always show the widget for the selected function
|
|
|
|
connect(ui->list, &QListWidget::currentRowChanged, ui->stack, &QStackedWidget::setCurrentIndex);
|
|
|
|
|
|
|
|
connect(ui->buttonBox, &QDialogButtonBox::accepted, [=](){
|
|
|
|
auto newMath = TraceMath::createMath(static_cast<TraceMath::Type>(ui->list->currentRow()));
|
|
|
|
if(newMath) {
|
|
|
|
model->addOperation(newMath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ui->list->setCurrentRow(0);
|
|
|
|
ui->stack->setCurrentIndex(0);
|
|
|
|
|
|
|
|
d->show();
|
2020-11-27 23:31:05 +08:00
|
|
|
});
|
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);
|
|
|
|
ui->view->setCurrentIndex(index.sibling(index.row() - 1, 0));
|
|
|
|
});
|
|
|
|
connect(ui->bMoveDown, &QPushButton::clicked, [&](){
|
|
|
|
auto index = ui->view->currentIndex();
|
|
|
|
t.swapMathOrder(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-29 02:32:18 +08:00
|
|
|
Q_UNUSED(parent);
|
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
|
|
|
|
{
|
2020-11-29 02:32:18 +08:00
|
|
|
Q_UNUSED(parent);
|
2020-11-25 23:47:29 +08:00
|
|
|
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-28 20:57:22 +08:00
|
|
|
case ColIndexStatus:
|
|
|
|
if(role == Qt::DecorationRole) {
|
|
|
|
switch(math.math->getStatus()) {
|
|
|
|
case TraceMath::Status::Ok:
|
|
|
|
return QApplication::style()->standardIcon(QStyle::SP_DialogApplyButton);
|
|
|
|
case TraceMath::Status::Warning:
|
|
|
|
return QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
|
|
|
|
case TraceMath::Status::Error:
|
|
|
|
return QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
|
|
|
|
}
|
|
|
|
} else if(role == Qt::ToolTipRole) {
|
|
|
|
if(math.math->getStatus() != TraceMath::Status::Ok) {
|
|
|
|
return math.math->getStatusDescription();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-11-25 23:47:29 +08:00
|
|
|
case ColIndexDescription:
|
|
|
|
if(role == Qt::DisplayRole) {
|
|
|
|
return math.math->description();
|
|
|
|
}
|
2020-11-28 20:57:22 +08:00
|
|
|
// else if(role == Qt::CheckStateRole){
|
|
|
|
// return math.enabled ? Qt::Checked : Qt::Unchecked;
|
|
|
|
// }
|
2020-11-25 23:47:29 +08:00
|
|
|
break;
|
2020-11-28 20:57:22 +08:00
|
|
|
case ColIndexDomain:
|
|
|
|
if(role == Qt::DisplayRole) {
|
|
|
|
switch(math.math->getDataType()) {
|
|
|
|
case TraceMath::DataType::Time:
|
|
|
|
return "Time";
|
|
|
|
case TraceMath::DataType::Frequency:
|
|
|
|
return "Frequency";
|
|
|
|
case TraceMath::DataType::Invalid:
|
|
|
|
return "Invalid";
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 23:47:29 +08:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MathModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch(section) {
|
2020-11-28 20:57:22 +08:00
|
|
|
case ColIndexStatus: return "Status"; break;
|
2020-11-25 23:47:29 +08:00
|
|
|
case ColIndexDescription: return "Description"; break;
|
2020-11-28 20:57:22 +08:00
|
|
|
case ColIndexDomain: return "Output domain"; break;
|
2020-11-25 23:47:29 +08:00
|
|
|
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
|
|
|
}
|
2020-11-28 20:57:22 +08:00
|
|
|
// switch(index.column()) {
|
|
|
|
// case ColIndexDescription: flags |= Qt::ItemIsUserCheckable; break;
|
|
|
|
// default:
|
|
|
|
// break;
|
|
|
|
// }
|
2020-11-25 23:47:29 +08:00
|
|
|
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-28 20:57:22 +08:00
|
|
|
// open the editor for the newly added operation
|
|
|
|
math->edit();
|
2020-11-27 23:31:05 +08:00
|
|
|
}
|
|
|
|
|
2020-11-26 04:20:31 +08:00
|
|
|
void MathModel::deleteRow(unsigned int row)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
t.removeMathOperation(row);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|