feat: Companion - New model curve tab layout, curve edit dialog and mix edit curve view (#1484)
Add curve image and double click to edit to input edit, mix edit and outputs
This commit is contained in:
@@ -14,6 +14,12 @@ set(shared_NAMES
|
||||
autoslider
|
||||
autospinbox
|
||||
autotimeedit
|
||||
curveimagewidget
|
||||
curvedialog
|
||||
)
|
||||
|
||||
set(shared_UIS
|
||||
curvedialog.ui
|
||||
)
|
||||
|
||||
foreach(name ${shared_NAMES})
|
||||
@@ -21,13 +27,16 @@ foreach(name ${shared_NAMES})
|
||||
set(shared_HDRS ${shared_HDRS} ${name}.h)
|
||||
endforeach()
|
||||
|
||||
qt5_wrap_ui(shared_SRCS ${shared_UIS})
|
||||
qt5_wrap_cpp(shared_SRCS ${shared_HDRS})
|
||||
|
||||
# add after moc generation to stop warnings
|
||||
set(shared_SRCS ${shared_SRCS}
|
||||
autowidget.h
|
||||
autowidget.cpp
|
||||
curveimage.h
|
||||
curveimage.cpp
|
||||
)
|
||||
|
||||
add_library(shared ${shared_SRCS})
|
||||
target_link_libraries(shared PRIVATE Qt5::Widgets ${CPN_COMMON_LIB})
|
||||
target_link_libraries(shared PRIVATE datamodels Qt5::Widgets ${CPN_COMMON_LIB})
|
||||
|
||||
@@ -0,0 +1,599 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "curvedialog.h"
|
||||
#include "ui_curvedialog.h"
|
||||
#include "../modeledit/node.h"
|
||||
#include "../modeledit/edge.h"
|
||||
#include "helpers.h"
|
||||
#include "filtereditemmodels.h"
|
||||
#include "eeprominterface.h"
|
||||
|
||||
float curveLinear(float x, float coeff, float yMin, float yMid, float yMax)
|
||||
{
|
||||
float a = (yMax - yMin) / 200.0;
|
||||
return yMin + a * (x + 100.0);
|
||||
}
|
||||
|
||||
float curveexpou(float point, float coeff)
|
||||
{
|
||||
float x = point * 1024.0 / 100.0;
|
||||
float k = coeff * 256.0 / 100.0;
|
||||
return ((k * x * x * x / (1024 * 1024) + x * (256 - k) + 128) / 256) / 1024.0 * 100;
|
||||
}
|
||||
|
||||
float curveExpo(float x, float coeff, float yMin, float yMid, float yMax)
|
||||
{
|
||||
float a = (yMax - yMin) / 100.0;
|
||||
|
||||
x += 100.0;
|
||||
x /= 2.0;
|
||||
|
||||
if (coeff >= 0) {
|
||||
return round(curveexpou(x, coeff)*a + yMin);
|
||||
}
|
||||
else {
|
||||
coeff = -coeff;
|
||||
x = 100 - x;
|
||||
return round((100.0 - curveexpou(x, coeff)) * a + yMin);
|
||||
}
|
||||
}
|
||||
|
||||
float curveSymmetricalY(float x, float coeff, float yMin, float yMid, float yMax)
|
||||
{
|
||||
bool invert;
|
||||
if (x < 0) {
|
||||
x = -x;
|
||||
invert = 1;
|
||||
}
|
||||
else {
|
||||
invert = 0;
|
||||
}
|
||||
|
||||
float y;
|
||||
if (coeff >= 0) {
|
||||
y = round(curveexpou(x, coeff) * (yMax / 100.0));
|
||||
}
|
||||
else {
|
||||
coeff = -coeff;
|
||||
x = 100.0 - x;
|
||||
y = round((100.0 - curveexpou(x, coeff)) * (yMax / 100.0));
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
y = -y;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
float curveSymmetricalX(float x, float coeff, float yMin, float yMid, float yMax)
|
||||
{
|
||||
float a = (yMax - yMid) / 100.0;
|
||||
|
||||
if (x < 0)
|
||||
x = -x;
|
||||
|
||||
float y;
|
||||
if (coeff >= 0) {
|
||||
y = round(curveexpou(x, coeff) * a + yMid);
|
||||
}
|
||||
else {
|
||||
coeff = -coeff;
|
||||
x = 100 - x;
|
||||
y = round((100.0 - curveexpou(x, coeff)) * a + yMid);
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
#define GFX_MARGIN 16
|
||||
|
||||
#define CURVE_COEFF_ENABLE 1
|
||||
#define CURVE_YMID_ENABLE 2
|
||||
#define CURVE_YMIN_ENABLE 4
|
||||
|
||||
/*
|
||||
CurveDialog
|
||||
*/
|
||||
|
||||
CurveDialog::CurveDialog(QWidget * parent, ModelData & model, const int curveIdx, Firmware * firmware,
|
||||
CompoundItemModelFactory * sharedItemModels) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::CurveDialog),
|
||||
model(model),
|
||||
curve(model.curves[curveIdx]),
|
||||
curveIdx(curveIdx),
|
||||
sharedItemModels(sharedItemModels)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
if (curve.isEmpty())
|
||||
curve.init();
|
||||
|
||||
lock = true;
|
||||
|
||||
maxCurves = firmware->getCapability(NumCurves);
|
||||
maxPoints = firmware->getCapability(NumCurvePoints);
|
||||
|
||||
ui->curveName->setField(curve.name, CURVEDATA_NAME_LEN);
|
||||
connect(ui->curveName, &AutoLineEdit::currentDataChanged, this, &CurveDialog::on_curveNameChanged);
|
||||
on_curveNameChanged();
|
||||
|
||||
dialogFilterFactory = new FilteredItemModelFactory();
|
||||
int id = dialogFilterFactory->registerItemModel(new FilteredItemModel(CurveData::typeItemModel()),
|
||||
"Curve Type");
|
||||
ui->curveType->setModel(dialogFilterFactory->getItemModel(id));
|
||||
ui->curveType->setField((int &)curve.type);
|
||||
connect(ui->curveType, &AutoComboBox::currentDataChanged, this, [&] () { on_curveTypeChanged(ui->curveType->currentIndex()) ;});
|
||||
|
||||
id = dialogFilterFactory->registerItemModel(new FilteredItemModel(CurveData::pointsItemModel()),
|
||||
"Curve Points");
|
||||
|
||||
ui->curvePoints->setModel(dialogFilterFactory->getItemModel(id));
|
||||
ui->curvePoints->setField(curve.count);
|
||||
connect(ui->curvePoints, &AutoComboBox::currentDataChanged, this, [&] () { on_curvePointsChanged(ui->curvePoints->currentIndex()) ;});
|
||||
|
||||
ui->curveSmooth->setField(curve.smooth);
|
||||
connect(ui->curveSmooth, &AutoCheckBox::currentDataChanged, this, [&] () { update() ;});
|
||||
|
||||
scene = new CustomScene(ui->curvePreview);
|
||||
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
connect(scene, &CustomScene::newPoint, this, &CurveDialog::onSceneNewPoint);
|
||||
|
||||
ui->curvePreview->setScene(scene);
|
||||
|
||||
for (int i = 0; i < CPN_MAX_POINTS; i++) {
|
||||
spnx[i] = new QSpinBox(this);
|
||||
spnx[i]->setProperty("index", i);
|
||||
spnx[i]->setMinimum(-100);
|
||||
spnx[i]->setMaximum(+100);
|
||||
spnx[i]->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
|
||||
spnx[i]->setAccelerated(true);
|
||||
connect(spnx[i], QOverload<int>::of(&QSpinBox::valueChanged), this, &CurveDialog::onPointEdited);
|
||||
ui->pointsLayout->addWidget(spnx[i], i, 0, 1, 1);
|
||||
|
||||
spny[i] = new QSpinBox(this);
|
||||
spny[i]->setProperty("index", i);
|
||||
spny[i]->setMinimum(-100);
|
||||
spny[i]->setMaximum(+100);
|
||||
spny[i]->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
|
||||
spny[i]->setAccelerated(true);
|
||||
connect(spny[i], QOverload<int>::of(&QSpinBox::valueChanged), this, &CurveDialog::onPointEdited);
|
||||
ui->pointsLayout->addWidget(spny[i], i, 1, 1, 1);
|
||||
}
|
||||
|
||||
addTemplate(tr("Linear"), CURVE_YMIN_ENABLE, curveLinear);
|
||||
addTemplate(tr("Single Expo"), CURVE_COEFF_ENABLE | CURVE_YMIN_ENABLE, curveExpo);
|
||||
addTemplate(tr("Symmetrical f(x)=-f(-x)"), CURVE_COEFF_ENABLE, curveSymmetricalY);
|
||||
addTemplate(tr("Symmetrical f(x)=f(-x)"), CURVE_COEFF_ENABLE | CURVE_YMID_ENABLE, curveSymmetricalX);
|
||||
on_curveTemplateChanged(0);
|
||||
|
||||
ui->pointSize->setValue(10);
|
||||
ui->pointSize->setMinimum(3);
|
||||
ui->pointSize->setMaximum(20);
|
||||
connect(ui->pointSize, QOverload<int>::of(&QSpinBox::valueChanged), this, [&] () { if (!lock) update(); });
|
||||
|
||||
connect(ui->curveTemplate, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CurveDialog::on_curveTemplateChanged);
|
||||
connect(ui->curveApply, &QPushButton::clicked, this, &CurveDialog::on_curveApplyClicked);
|
||||
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
||||
|
||||
connect(this, &QDialog::accepted, this, &CurveDialog::onDialogAccepted);
|
||||
|
||||
update();
|
||||
shrink();
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
CurveDialog::~CurveDialog()
|
||||
{
|
||||
delete ui;
|
||||
delete dialogFilterFactory;
|
||||
}
|
||||
|
||||
void CurveDialog::onDialogAccepted()
|
||||
{
|
||||
model.curves[curveIdx] = curve;
|
||||
if (sharedItemModels)
|
||||
sharedItemModels->update(AbstractItemModel::IMUE_Curves);
|
||||
}
|
||||
|
||||
void CurveDialog::update()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
ui->curveName->update();
|
||||
updateCurveType();
|
||||
updateCurve();
|
||||
updateCurvePoints();
|
||||
shrink();
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
void CurveDialog::updateCurveType()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
ui->curveType->update();
|
||||
ui->curvePoints->update();
|
||||
ui->curveSmooth->update();
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
void CurveDialog::updateCurve()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
Node * nodel = 0;
|
||||
Node * nodex = 0;
|
||||
QColor color;
|
||||
|
||||
scene->clear();
|
||||
|
||||
qreal width = scene->sceneRect().width();
|
||||
qreal height = scene->sceneRect().height();
|
||||
|
||||
qreal leftX = scene->sceneRect().left() + width / 4 * 1;
|
||||
qreal centerX = scene->sceneRect().left() + width / 2;
|
||||
qreal rightX = scene->sceneRect().left() + width / 4 * 3;
|
||||
qreal topY = scene->sceneRect().top() + height / 4 * 1;
|
||||
qreal centerY = scene->sceneRect().top() + height / 2;
|
||||
qreal bottomY = scene->sceneRect().top() + height / 4 * 3;
|
||||
|
||||
scene->addLine(leftX, GFX_MARGIN, leftX, height + GFX_MARGIN);
|
||||
scene->addLine(centerX, GFX_MARGIN, centerX, height + GFX_MARGIN);
|
||||
scene->addLine(rightX, GFX_MARGIN, rightX, height + GFX_MARGIN);
|
||||
scene->addLine(GFX_MARGIN, topY, width + GFX_MARGIN, topY);
|
||||
scene->addLine(GFX_MARGIN, centerY, width + GFX_MARGIN, centerY);
|
||||
scene->addLine(GFX_MARGIN, bottomY, width + GFX_MARGIN, bottomY);
|
||||
|
||||
QPen pen;
|
||||
pen.setWidth(1);
|
||||
pen.setStyle(Qt::SolidLine);
|
||||
|
||||
pen.setColor(colors[0]); // needed????
|
||||
|
||||
int numpoints = curve.count;
|
||||
|
||||
for (int i = 0; i < numpoints - 1; i++) {
|
||||
if (curve.type == CurveData::CURVE_TYPE_CUSTOM)
|
||||
scene->addLine(centerX + (qreal)curve.points[i].x * width / 200,
|
||||
centerY - (qreal)curve.points[i].y * height / 200,
|
||||
centerX + (qreal)curve.points[i + 1].x * width / 200,
|
||||
centerY - (qreal)curve.points[i + 1].y * height / 200, pen);
|
||||
else
|
||||
scene->addLine(GFX_MARGIN + i * width / (numpoints - 1),
|
||||
centerY - (qreal)curve.points[i].y * height / 200,
|
||||
GFX_MARGIN + (i + 1) * width / (numpoints - 1),
|
||||
centerY - (qreal)curve.points[i + 1].y * height / 200, pen);
|
||||
}
|
||||
|
||||
for (int i = 0; i < numpoints; i++) {
|
||||
nodel = nodex;
|
||||
nodex = new Node();
|
||||
nodex->setProperty("index", i);
|
||||
nodex->setColor(colors[0]);
|
||||
nodex->setBallSize(ui->pointSize->value());
|
||||
nodex->setBallHeight(0);
|
||||
if (curve.type == CurveData::CURVE_TYPE_CUSTOM) {
|
||||
if (i > 0 && i < numpoints - 1) {
|
||||
nodex->setFixedX(false);
|
||||
nodex->setMinX(curve.points[i - 1].x);
|
||||
nodex->setMaxX(curve.points[i + 1].x);
|
||||
}
|
||||
else {
|
||||
nodex->setFixedX(true);
|
||||
}
|
||||
nodex->setPos(centerX + (qreal)curve.points[i].x * width / 200,
|
||||
centerY - (qreal)curve.points[i].y * height / 200);
|
||||
}
|
||||
else {
|
||||
nodex->setFixedX(true);
|
||||
nodex->setPos(GFX_MARGIN + i * width / (numpoints - 1),
|
||||
centerY - (qreal)curve.points[i].y * height / 200);
|
||||
}
|
||||
connect(nodex, SIGNAL(moved(int, int)), this, SLOT(onNodeMoved(int, int)));
|
||||
connect(nodex, SIGNAL(focus()), this, SLOT(onNodeFocus()));
|
||||
connect(nodex, SIGNAL(unfocus()), this, SLOT(onNodeUnfocus()));
|
||||
connect(nodex, SIGNAL(deleteMe()), this, SLOT(onNodeDelete()));
|
||||
scene->addItem(nodex);
|
||||
if (i > 0)
|
||||
scene->addItem(new Edge(nodel, nodex));
|
||||
}
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
void CurveDialog::updateCurvePoints()
|
||||
{
|
||||
lock = true;
|
||||
|
||||
int count = curve.count;
|
||||
for (int i = 0; i < count; i++) {
|
||||
spny[i]->show();
|
||||
spny[i]->setValue(curve.points[i].y);
|
||||
if (curve.type == CurveData::CURVE_TYPE_CUSTOM) {
|
||||
spnx[i]->show();
|
||||
if (i == 0 || i == count - 1) {
|
||||
spnx[i]->setDisabled(true);
|
||||
spnx[i]->setMaximum(+100);
|
||||
spnx[i]->setMinimum(-100);
|
||||
}
|
||||
else {
|
||||
spnx[i]->setDisabled(false);
|
||||
spnx[i]->setMaximum(curve.points[i + 1].x);
|
||||
spnx[i]->setMinimum(curve.points[i - 1].x);
|
||||
}
|
||||
spnx[i]->setValue(curve.points[i].x);
|
||||
}
|
||||
else {
|
||||
spnx[i]->hide();
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = count; i < CPN_MAX_POINTS; i++) {
|
||||
spny[i]->hide();
|
||||
spnx[i]->hide();
|
||||
}
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
void CurveDialog::onPointEdited(int i)
|
||||
{
|
||||
if (!lock) {
|
||||
int index = sender()->property("index").toInt();
|
||||
curve.points[index].x = spnx[index]->value();
|
||||
curve.points[index].y = spny[index]->value();
|
||||
updateCurve();
|
||||
updateCurvePoints();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::onNodeMoved(int x, int y)
|
||||
{
|
||||
if (!lock) {
|
||||
lock = true;
|
||||
int index = sender()->property("index").toInt();
|
||||
curve.points[index].x = x;
|
||||
curve.points[index].y = y;
|
||||
spnx[index]->setValue(x);
|
||||
spny[index]->setValue(y);
|
||||
if (index > 0)
|
||||
spnx[index - 1]->setMaximum(x);
|
||||
if (index < curve.count - 1)
|
||||
spnx[index + 1]->setMinimum(x);
|
||||
lock = false;
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::onNodeFocus()
|
||||
{
|
||||
int index = sender()->property("index").toInt();
|
||||
spny[index]->setFocus();
|
||||
}
|
||||
|
||||
void CurveDialog::onNodeUnfocus()
|
||||
{
|
||||
int index = sender()->property("index").toInt();
|
||||
spny[index]->clearFocus();
|
||||
updateCurve();
|
||||
}
|
||||
|
||||
bool CurveDialog::allowCurveType(int points, CurveData::CurveType type)
|
||||
{
|
||||
int totalpoints = 0;
|
||||
for (int i = 0; i < maxCurves; i++) {
|
||||
int cvPoints = (i == curveIdx ? points : model.curves[i].count);
|
||||
CurveData::CurveType cvType = (i == curveIdx ? type : model.curves[i].type);
|
||||
totalpoints += cvPoints + (cvType == CurveData::CURVE_TYPE_CUSTOM ? cvPoints - 2 : 0);
|
||||
}
|
||||
|
||||
if (totalpoints > maxPoints) {
|
||||
QMessageBox::warning(this, CPN_STR_APP_NAME, tr("Not enough free points in global points array to store the curve."));
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::on_curveNameChanged()
|
||||
{
|
||||
setWindowTitle(tr("Editing curve %1: %2").arg(curveIdx + 1).arg(QString(curve.name)));
|
||||
}
|
||||
|
||||
void CurveDialog::on_curvePointsChanged(int index)
|
||||
{
|
||||
if (!lock) {
|
||||
int numpoints = ((QComboBox *)sender())->itemData(index).toInt();
|
||||
|
||||
if (allowCurveType(numpoints, curve.type)) {
|
||||
curve.count = numpoints;
|
||||
curve.init();
|
||||
update();
|
||||
}
|
||||
else {
|
||||
updateCurveType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::on_curveTypeChanged(int index)
|
||||
{
|
||||
if (!lock) {
|
||||
CurveData::CurveType type = (CurveData::CurveType)index;
|
||||
int numpoints = ui->curvePoints->itemData(ui->curvePoints->currentIndex()).toInt();
|
||||
|
||||
if (allowCurveType(numpoints, type)) {
|
||||
//curve.clear(numpoints);
|
||||
curve.type = type;
|
||||
//curve.init();
|
||||
update();
|
||||
}
|
||||
else {
|
||||
updateCurveType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QRect qr = ui->curvePreview->contentsRect();
|
||||
ui->curvePreview->scene()->setSceneRect(GFX_MARGIN, GFX_MARGIN, qr.width() - GFX_MARGIN * 2, qr.height() - GFX_MARGIN * 2);
|
||||
updateCurve();
|
||||
}
|
||||
|
||||
void CurveDialog::on_curveTemplateChanged(int index)
|
||||
{
|
||||
unsigned int flags = templates[index].flags;
|
||||
ui->curveCoeffLabel->setVisible(flags & CURVE_COEFF_ENABLE);
|
||||
ui->curveCoeff->setVisible(flags & CURVE_COEFF_ENABLE);
|
||||
ui->yMax->setValue(100);
|
||||
ui->yMidLabel->setVisible(flags & CURVE_YMID_ENABLE);
|
||||
ui->yMid->setVisible(flags & CURVE_YMID_ENABLE);
|
||||
ui->yMid->setValue(0);
|
||||
ui->yMinLabel->setVisible(flags & CURVE_YMIN_ENABLE);
|
||||
ui->yMin->setVisible(flags & CURVE_YMIN_ENABLE);
|
||||
ui->yMin->setValue(-100);
|
||||
}
|
||||
|
||||
void CurveDialog::addTemplate(QString name, unsigned int flags, curveFunction function)
|
||||
{
|
||||
CurveCreatorTemplate tmpl;
|
||||
tmpl.name = name;
|
||||
tmpl.flags = flags;
|
||||
tmpl.function = function;
|
||||
templates.append(tmpl);
|
||||
ui->curveTemplate->addItem(name);
|
||||
}
|
||||
|
||||
void CurveDialog::on_curveApplyClicked()
|
||||
{
|
||||
int index = ui->curveTemplate->currentIndex();
|
||||
int numpoints = curve.count;
|
||||
|
||||
for (int i = 0; i < numpoints; i++) {
|
||||
float x;
|
||||
if (curve.type == CurveData::CURVE_TYPE_CUSTOM)
|
||||
x = curve.points[i].x;
|
||||
else
|
||||
x = -100.0 + (200.0 / (numpoints - 1)) * i;
|
||||
|
||||
bool apply = false;
|
||||
switch (ui->curveSide->currentIndex()) {
|
||||
case 0:
|
||||
apply = true;
|
||||
break;
|
||||
case 1:
|
||||
if (x >= 0)
|
||||
apply = true;
|
||||
break;
|
||||
case 2:
|
||||
if (x < 0)
|
||||
apply = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (apply) {
|
||||
curve.points[i].y = templates[index].function(x, ui->curveCoeff->value(), ui->yMin->value(), ui->yMid->value(),
|
||||
ui->yMax->value());
|
||||
}
|
||||
}
|
||||
|
||||
updateCurve();
|
||||
updateCurvePoints();
|
||||
}
|
||||
|
||||
void CurveDialog::onNodeDelete()
|
||||
{
|
||||
int index = sender()->property("index").toInt();
|
||||
int numpoints = curve.count;
|
||||
if ((curve.type == CurveData::CURVE_TYPE_CUSTOM) && (index > 0) && (index < numpoints - 1)) {
|
||||
spny[index]->clearFocus();
|
||||
for (int i = index + 1; i < numpoints; i++) {
|
||||
curve.points[i - 1] = curve.points[i];
|
||||
}
|
||||
numpoints--;
|
||||
curve.points[numpoints].x = 0;
|
||||
curve.points[numpoints].y = 0;
|
||||
curve.count = numpoints;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::onSceneNewPoint(int x, int y)
|
||||
{
|
||||
if ((curve.type == CurveData::CURVE_TYPE_CUSTOM) && (curve.count < CPN_MAX_POINTS)) {
|
||||
int newidx = 0;
|
||||
int numpoints = curve.count;
|
||||
if (x < curve.points[0].x) {
|
||||
newidx = 0;
|
||||
}
|
||||
else if (x > curve.points[numpoints - 1].x) {
|
||||
newidx = numpoints;
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < numpoints; i++) {
|
||||
if (x < curve.points[i].x) {
|
||||
newidx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
numpoints++;
|
||||
curve.count = numpoints;
|
||||
for (int idx = (numpoints - 1); idx > newidx; idx--) {
|
||||
curve.points[idx] = curve.points[idx - 1];
|
||||
}
|
||||
curve.points[newidx].x = x;
|
||||
curve.points[newidx].y = y;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveDialog::shrink()
|
||||
{
|
||||
resize(0, 0);
|
||||
}
|
||||
|
||||
CustomScene::CustomScene(QGraphicsView * view) :
|
||||
QGraphicsScene(view)
|
||||
{
|
||||
}
|
||||
|
||||
void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) {
|
||||
QRectF rect = sceneRect();
|
||||
QPointF pos = event->scenePos();
|
||||
QPointF p;
|
||||
p.setX(-100 + ((pos.x() - rect.left()) * 200) / rect.width());
|
||||
p.setY(100 + (rect.top() - pos.y()) * 200 / rect.height());
|
||||
QGraphicsScene::mouseReleaseEvent(event);
|
||||
emit newPoint((int)p.x(), (int)p.y());
|
||||
}
|
||||
else
|
||||
QGraphicsScene::mouseReleaseEvent(event);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "constants.h"
|
||||
#include "modeldata.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsView>
|
||||
|
||||
class Firmware;
|
||||
class FilteredItemModelFactory;
|
||||
class CompoundItemModelFactory;
|
||||
|
||||
namespace Ui {
|
||||
class CurveDialog;
|
||||
}
|
||||
|
||||
typedef float (*curveFunction) (float x, float coeff, float yMin, float yMid, float yMax);
|
||||
|
||||
struct CurveCreatorTemplate {
|
||||
QString name;
|
||||
unsigned int flags;
|
||||
curveFunction function;
|
||||
};
|
||||
|
||||
class CustomScene : public QGraphicsScene
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CustomScene(QGraphicsView * view);
|
||||
|
||||
signals:
|
||||
void newPoint(int, int);
|
||||
|
||||
protected:
|
||||
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event) override;
|
||||
};
|
||||
|
||||
class CurveDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CurveDialog(QWidget *parent, ModelData & model, const int curveIdx, Firmware * firmware, CompoundItemModelFactory * sharedItemModels);
|
||||
virtual ~CurveDialog();
|
||||
|
||||
private slots:
|
||||
void shrink();
|
||||
void on_curveNameChanged();
|
||||
void on_curvePointsChanged(int index);
|
||||
void on_curveTypeChanged(int index);
|
||||
void onPointEdited(int i);
|
||||
void onNodeMoved(int x, int y);
|
||||
void onNodeFocus();
|
||||
void onNodeUnfocus();
|
||||
void on_curveTemplateChanged(int index);
|
||||
void on_curveApplyClicked();
|
||||
void onSceneNewPoint(int x, int y);
|
||||
void onNodeDelete();
|
||||
void onDialogAccepted();
|
||||
|
||||
protected:
|
||||
virtual void resizeEvent(QResizeEvent *event);
|
||||
void addTemplate(QString name, unsigned int flags, curveFunction function);
|
||||
|
||||
private:
|
||||
Ui::CurveDialog *ui;
|
||||
ModelData &model;
|
||||
CurveData curve; // copy initialised in constructor and copied back to model if dialog is accepted
|
||||
const int curveIdx;
|
||||
CompoundItemModelFactory *sharedItemModels;
|
||||
|
||||
QSpinBox *spnx[CPN_MAX_POINTS];
|
||||
QSpinBox *spny[CPN_MAX_POINTS];
|
||||
QVector<CurveCreatorTemplate> templates;
|
||||
CustomScene *scene;
|
||||
int maxCurves;
|
||||
int maxPoints;
|
||||
bool lock;
|
||||
FilteredItemModelFactory * dialogFilterFactory;
|
||||
|
||||
void update();
|
||||
void updateCurve();
|
||||
void updateCurveType();
|
||||
void updateCurvePoints();
|
||||
bool allowCurveType(int points, CurveData::CurveType type);
|
||||
void setPointY(int i, int x, int y);
|
||||
};
|
||||
@@ -0,0 +1,371 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CurveDialog</class>
|
||||
<widget class="QDialog" name="CurveDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>658</width>
|
||||
<height>523</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="sizeGripEnabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="modal">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
</property>
|
||||
<item row="0" column="0" rowspan="2">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QGraphicsView" name="curvePreview">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>400</width>
|
||||
<height>400</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="curveNameLabel">
|
||||
<property name="text">
|
||||
<string>Name</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="AutoLineEdit" name="curveName">
|
||||
<property name="maxLength">
|
||||
<number>3</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="curveTypeLabel">
|
||||
<property name="text">
|
||||
<string>Type</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="AutoComboBox" name="curveType"/>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="AutoComboBox" name="curvePoints"/>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="AutoCheckBox" name="curveSmooth">
|
||||
<property name="text">
|
||||
<string>Smooth</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<layout class="QGridLayout" name="pointsLayout"/>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="0" column="2" rowspan="2">
|
||||
<widget class="QGroupBox" name="curveCreator">
|
||||
<property name="font">
|
||||
<font>
|
||||
<weight>50</weight>
|
||||
<bold>false</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="autoFillBackground">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="title">
|
||||
<string>Curve Creator</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="curveTemplateLabel">
|
||||
<property name="text">
|
||||
<string>Template</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="curveTemplate"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="curveCoeffLabel">
|
||||
<property name="text">
|
||||
<string>Coefficient</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QSpinBox" name="curveCoeff">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>-100</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="yMinLabel">
|
||||
<property name="text">
|
||||
<string>Y at X=-100</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QSpinBox" name="yMin">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>-100</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="yMidLabel">
|
||||
<property name="text">
|
||||
<string>Y at X=0</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QSpinBox" name="yMid">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>-100</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="yMaxLabel">
|
||||
<property name="text">
|
||||
<string>Y at X=100</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QSpinBox" name="yMax">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>-100</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>100</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0">
|
||||
<widget class="QLabel" name="curveSideLabel">
|
||||
<property name="text">
|
||||
<string>Side</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="1">
|
||||
<widget class="QComboBox" name="curveSide">
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Both</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>x>0</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>x<0</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0">
|
||||
<widget class="QPushButton" name="curveApply">
|
||||
<property name="text">
|
||||
<string>Apply</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="7" column="0" colspan="2">
|
||||
<widget class="Line" name="line">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="8" column="0">
|
||||
<widget class="QLabel" name="pointSizeLabel">
|
||||
<property name="text">
|
||||
<string>Point size</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="8" column="1">
|
||||
<widget class="QSpinBox" name="pointSize"/>
|
||||
</item>
|
||||
<item row="9" column="0">
|
||||
<spacer name="verticalSpacer_3">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>1</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<spacer name="verticalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>AutoLineEdit</class>
|
||||
<extends>QLineEdit</extends>
|
||||
<header>autolineedit.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>AutoComboBox</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>autocombobox.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>AutoCheckBox</class>
|
||||
<extends>QCheckBox</extends>
|
||||
<header>autocheckbox.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<tabstops>
|
||||
<tabstop>curvePreview</tabstop>
|
||||
<tabstop>curvePoints</tabstop>
|
||||
<tabstop>curveTemplate</tabstop>
|
||||
<tabstop>curveCoeff</tabstop>
|
||||
<tabstop>yMin</tabstop>
|
||||
<tabstop>yMid</tabstop>
|
||||
<tabstop>yMax</tabstop>
|
||||
<tabstop>curveSide</tabstop>
|
||||
<tabstop>curveApply</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "curveimage.h"
|
||||
#include "curvedata.h"
|
||||
|
||||
constexpr int IMAGE_SIZE = { 200 };
|
||||
|
||||
CurveImage::CurveImage(QColor gridColor, qreal gridWidth):
|
||||
size(IMAGE_SIZE),
|
||||
image(size + 1, size + 1, QImage::Format_RGB32),
|
||||
painter(&image)
|
||||
{
|
||||
painter.setBrush(QBrush(Qt::white));
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawRect(0, 0, size, size);
|
||||
|
||||
painter.setPen(QPen(gridColor, gridWidth));
|
||||
const int step = size / 4;
|
||||
painter.drawLine(0, step * 1, size, step * 1);
|
||||
painter.drawLine(0, step * 2, size, step * 2);
|
||||
painter.drawLine(0, step * 3, size, step * 3);
|
||||
painter.drawLine(step * 1, 0, step * 1, size);
|
||||
painter.drawLine(step * 2, 0, step * 2, size);
|
||||
painter.drawLine(step * 3, 0, step * 3, size);
|
||||
}
|
||||
|
||||
void CurveImage::drawCurve(const CurveData & curve, QColor color, qreal width)
|
||||
{
|
||||
painter.setPen(QPen(color, width, Qt::SolidLine));
|
||||
|
||||
for (int j = 1; j < curve.count; j++) {
|
||||
if (curve.type == CurveData::CURVE_TYPE_CUSTOM)
|
||||
painter.drawLine(size / 2 + (size * curve.points[j - 1].x) / IMAGE_SIZE,
|
||||
size / 2 - (size * curve.points[j - 1].y) / IMAGE_SIZE,
|
||||
size / 2 + (size * curve.points[j].x) / IMAGE_SIZE,
|
||||
size / 2 - (size * curve.points[j].y) / IMAGE_SIZE);
|
||||
else
|
||||
painter.drawLine(size * (j-1) / (curve.count - 1),
|
||||
size / 2 - (size * curve.points[j - 1].y) / IMAGE_SIZE,
|
||||
size * (j) / (curve.count - 1),
|
||||
size / 2 - (size * curve.points[j].y) / IMAGE_SIZE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
class CurveData;
|
||||
|
||||
class CurveImage
|
||||
{
|
||||
public:
|
||||
CurveImage(QColor gridColor = Qt::darkGray, qreal gridWidth = 1);
|
||||
|
||||
void drawCurve(const CurveData & curve, QColor color = Qt::black, qreal width = 2);
|
||||
const QImage & get() const { return image; }
|
||||
|
||||
protected:
|
||||
int size;
|
||||
QImage image;
|
||||
QPainter painter;
|
||||
};
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "curveimagewidget.h"
|
||||
#include "curveimage.h"
|
||||
#include "curvedialog.h"
|
||||
#include "compounditemmodels.h"
|
||||
#include "modeldata.h"
|
||||
#include "eeprominterface.h"
|
||||
|
||||
CurveImageWidget::CurveImageWidget(QWidget * parent) :
|
||||
QLabel(parent)
|
||||
{
|
||||
setToolTip(tr("Double click to edit"));
|
||||
}
|
||||
|
||||
void CurveImageWidget::set(ModelData * model, Firmware * firmware, CompoundItemModelFactory * sharedItemModels, int index, QColor penColor, qreal penWidth)
|
||||
{
|
||||
this->model = model;
|
||||
this->firmware = firmware;
|
||||
this->sharedItemModels = sharedItemModels;
|
||||
this->penWidth = penWidth;
|
||||
this->penColor = penColor;
|
||||
setIndex(index);
|
||||
setGrid();
|
||||
}
|
||||
|
||||
void CurveImageWidget::setIndex(int index)
|
||||
{
|
||||
this->index = (index >= 0 && index < CPN_MAX_CURVES) ? index : -1;
|
||||
}
|
||||
|
||||
void CurveImageWidget::setGrid(QColor color, qreal width)
|
||||
{
|
||||
gridColor = color;
|
||||
gridWidth = width;
|
||||
}
|
||||
|
||||
void CurveImageWidget::setPen(QColor color, qreal width)
|
||||
{
|
||||
penColor = color;
|
||||
penWidth = width;
|
||||
}
|
||||
|
||||
int CurveImageWidget::edit()
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (index >= 0 && index < CPN_MAX_CURVES) {
|
||||
CurveDialog *dlg = new CurveDialog(this, *model, index, firmware, sharedItemModels);
|
||||
ret = dlg->exec();
|
||||
delete dlg;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CurveImageWidget::draw()
|
||||
{
|
||||
if (index >= 0 && index < CPN_MAX_CURVES) {
|
||||
CurveImage *curveImage = new CurveImage(gridColor, gridWidth);
|
||||
curveImage->drawCurve(model->curves[index], penColor, penWidth);
|
||||
|
||||
QImage image = curveImage->get();
|
||||
setPixmap(QPixmap::fromImage(image.scaled(height(), width())));
|
||||
|
||||
delete curveImage;
|
||||
}
|
||||
else {
|
||||
CurveImage *curveImage = new CurveImage(gridColor, gridWidth);
|
||||
QImage image = curveImage->get();
|
||||
setPixmap(QPixmap::fromImage(image.scaled(height(), width())));
|
||||
}
|
||||
}
|
||||
|
||||
void CurveImageWidget::mouseDoubleClickEvent(QMouseEvent * event)
|
||||
{
|
||||
QLabel::mouseDoubleClickEvent(event);
|
||||
emit doubleClicked();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) OpenTX
|
||||
*
|
||||
* Based on code named
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QLabel>
|
||||
|
||||
class ModelData;
|
||||
class Firmware;
|
||||
class CompoundItemModelFactory;
|
||||
|
||||
class CurveImageWidget : public QLabel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CurveImageWidget(QWidget * parent);
|
||||
virtual ~CurveImageWidget() {}
|
||||
|
||||
void set(ModelData * model, Firmware * firmware, CompoundItemModelFactory * sharedItemModels, int index, QColor penColor = Qt::black, qreal penWidth = 2);
|
||||
void setIndex(int index);
|
||||
void setGrid(QColor color = Qt::darkGray, qreal width = 1);
|
||||
void setPen(QColor color = Qt::black, qreal width = 2);
|
||||
int edit();
|
||||
void draw();
|
||||
|
||||
signals:
|
||||
void doubleClicked();
|
||||
|
||||
protected:
|
||||
virtual void mouseDoubleClickEvent(QMouseEvent * event);
|
||||
|
||||
private:
|
||||
ModelData *model = nullptr;
|
||||
Firmware *firmware = nullptr;
|
||||
CompoundItemModelFactory *sharedItemModels;
|
||||
int index;
|
||||
qreal penWidth;
|
||||
QColor penColor;
|
||||
qreal gridWidth;
|
||||
QColor gridColor;
|
||||
};
|
||||
Reference in New Issue
Block a user