Refactor to Model level ui datamodels for sources, switches and curves

This commit is contained in:
elecpower
2020-09-17 14:30:20 +10:00
parent 6ad75996e6
commit 1e0e22955d
34 changed files with 994 additions and 623 deletions
+52 -43
View File
@@ -108,7 +108,7 @@ float curveSymmetricalX(float x, float coeff, float yMin, float yMid, float yMax
return y;
}
Curves::Curves(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware):
CurvesPanel::CurvesPanel(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware):
ModelPanel(parent, model, generalSettings, firmware),
ui(new Ui::Curves),
currentCurve(0)
@@ -230,12 +230,12 @@ Curves::Curves(QWidget * parent, ModelData & model, GeneralSettings & generalSet
lock = false;
}
Curves::~Curves()
CurvesPanel::~CurvesPanel()
{
delete ui;
}
void Curves::editCurve()
void CurvesPanel::editCurve()
{
QPushButton *button = (QPushButton *)sender();
int index = button->property("index").toInt();
@@ -243,7 +243,7 @@ void Curves::editCurve()
update();
}
void Curves::plotCurve(bool checked)
void CurvesPanel::plotCurve(bool checked)
{
QCheckBox *chk = (QCheckBox *)sender();
int index = chk->property("index").toInt();
@@ -251,7 +251,7 @@ void Curves::plotCurve(bool checked)
updateCurve();
}
void Curves::update()
void CurvesPanel::update()
{
lock = true;
@@ -266,12 +266,12 @@ void Curves::update()
lock = false;
}
void Curves::setCurrentCurve(int index)
void CurvesPanel::setCurrentCurve(int index)
{
currentCurve = index;
}
void Curves::updateCurveType()
void CurvesPanel::updateCurveType()
{
lock = true;
@@ -297,7 +297,7 @@ void Curves::updateCurveType()
lock = false;
}
void Curves::updateCurve()
void CurvesPanel::updateCurve()
{
lock = true;
@@ -371,7 +371,7 @@ void Curves::updateCurve()
lock = false;
}
void Curves::updateCurvePoints()
void CurvesPanel::updateCurvePoints()
{
lock = true;
@@ -405,7 +405,7 @@ void Curves::updateCurvePoints()
lock = false;
}
void Curves::onPointEdited()
void CurvesPanel::onPointEdited()
{
if (!lock) {
int index = sender()->property("index").toInt();
@@ -416,7 +416,7 @@ void Curves::onPointEdited()
}
}
void Curves::onNodeMoved(int x, int y)
void CurvesPanel::onNodeMoved(int x, int y)
{
if (!lock) {
lock = true;
@@ -434,20 +434,20 @@ void Curves::onNodeMoved(int x, int y)
}
}
void Curves::onNodeFocus()
void CurvesPanel::onNodeFocus()
{
int index = sender()->property("index").toInt();
spny[index]->setFocus();
}
void Curves::onNodeUnfocus()
void CurvesPanel::onNodeUnfocus()
{
int index = sender()->property("index").toInt();
spny[index]->clearFocus();
updateCurve();
}
bool Curves::allowCurveType(int points, CurveData::CurveType type)
bool CurvesPanel::allowCurveType(int points, CurveData::CurveType type)
{
int totalpoints = 0;
for (int i = 0; i < maxCurves; i++) {
@@ -465,7 +465,7 @@ bool Curves::allowCurveType(int points, CurveData::CurveType type)
}
}
void Curves::on_curvePoints_currentIndexChanged(int index)
void CurvesPanel::on_curvePoints_currentIndexChanged(int index)
{
if (!lock) {
int numpoints = ((QComboBox *)sender())->itemData(index).toInt();
@@ -488,7 +488,7 @@ void Curves::on_curvePoints_currentIndexChanged(int index)
}
}
void Curves::on_curveCustom_currentIndexChanged(int index)
void CurvesPanel::on_curveCustom_currentIndexChanged(int index)
{
if (!lock) {
CurveData::CurveType type = (CurveData::CurveType)index;
@@ -512,20 +512,23 @@ void Curves::on_curveCustom_currentIndexChanged(int index)
}
}
void Curves::on_curveSmooth_currentIndexChanged(int index)
void CurvesPanel::on_curveSmooth_currentIndexChanged(int index)
{
model->curves[currentCurve].smooth = index;
update();
}
void Curves::on_curveName_editingFinished()
void CurvesPanel::on_curveName_editingFinished()
{
memset(model->curves[currentCurve].name, 0, sizeof(model->curves[currentCurve].name));
strcpy(model->curves[currentCurve].name, ui->curveName->text().toLatin1());
emit modified();
if (ui->curveName->text() != model->curves[currentCurve].name) {
memset(model->curves[currentCurve].name, 0, sizeof(model->curves[currentCurve].name));
strcpy(model->curves[currentCurve].name, ui->curveName->text().toLatin1());
emit updateDataModels();
emit modified();
}
}
void Curves::resizeEvent(QResizeEvent *event)
void CurvesPanel::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);
@@ -533,7 +536,7 @@ void Curves::resizeEvent(QResizeEvent *event)
ModelPanel::resizeEvent(event);
}
void Curves::on_curveType_currentIndexChanged(int index)
void CurvesPanel::on_curveType_currentIndexChanged(int index)
{
unsigned int flags = templates[index].flags;
ui->curveCoeffLabel->setVisible(flags & CURVE_COEFF_ENABLE);
@@ -547,7 +550,7 @@ void Curves::on_curveType_currentIndexChanged(int index)
ui->yMin->setValue(-100);
}
void Curves::addTemplate(QString name, unsigned int flags, curveFunction function)
void CurvesPanel::addTemplate(QString name, unsigned int flags, curveFunction function)
{
CurveCreatorTemplate tmpl;
tmpl.name = name;
@@ -557,7 +560,7 @@ void Curves::addTemplate(QString name, unsigned int flags, curveFunction functio
ui->curveType->addItem(name);
}
void Curves::on_curveApply_clicked()
void CurvesPanel::on_curveApply_clicked()
{
int index = ui->curveType->currentIndex();
int numpoints = model->curves[currentCurve].count;
@@ -594,14 +597,14 @@ void Curves::on_curveApply_clicked()
emit modified();
}
void Curves::onPointSizeEdited()
void CurvesPanel::onPointSizeEdited()
{
if (!lock) {
update();
}
}
void Curves::onNodeDelete()
void CurvesPanel::onNodeDelete()
{
int index = sender()->property("index").toInt();
int numpoints = model->curves[currentCurve].count;
@@ -619,7 +622,7 @@ void Curves::onNodeDelete()
}
}
void Curves::onSceneNewPoint(int x, int y)
void CurvesPanel::onSceneNewPoint(int x, int y)
{
if ((model->curves[currentCurve].type == CurveData::CURVE_TYPE_CUSTOM) && (model->curves[currentCurve].count < CPN_MAX_POINTS)) {
int newidx = 0;
@@ -650,7 +653,7 @@ void Curves::onSceneNewPoint(int x, int y)
}
}
void Curves::onCustomContextMenuRequested(QPoint pos)
void CurvesPanel::onCustomContextMenuRequested(QPoint pos)
{
QPushButton *button = (QPushButton *)sender();
selectedIndex = button->property("index").toInt();
@@ -672,7 +675,7 @@ void Curves::onCustomContextMenuRequested(QPoint pos)
contextMenu.exec(globalPos);
}
bool Curves::hasClipboardData(QByteArray * data) const
bool CurvesPanel::hasClipboardData(QByteArray * data) const
{
const QClipboard * clipboard = QApplication::clipboard();
const QMimeData * mimeData = clipboard->mimeData();
@@ -684,22 +687,22 @@ bool Curves::hasClipboardData(QByteArray * data) const
return false;
}
bool Curves::insertAllowed() const
bool CurvesPanel::insertAllowed() const
{
return ((selectedIndex < maxCurves - 1) && (model->curves[maxCurves - 1].isEmpty()));
}
bool Curves::moveDownAllowed() const
bool CurvesPanel::moveDownAllowed() const
{
return selectedIndex < maxCurves - 1;
}
bool Curves::moveUpAllowed() const
bool CurvesPanel::moveUpAllowed() const
{
return selectedIndex > 0;
}
void Curves::cmClear(bool prompt)
void CurvesPanel::cmClear(bool prompt)
{
if (prompt) {
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Clear Curve. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
@@ -709,10 +712,11 @@ void Curves::cmClear(bool prompt)
model->curves[selectedIndex].clear();
model->updateAllReferences(ModelData::REF_UPD_TYPE_CURVE, ModelData::REF_UPD_ACT_CLEAR, selectedIndex);
update();
emit updateDataModels();
emit modified();
}
void Curves::cmClearAll()
void CurvesPanel::cmClearAll()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Clear all Curves. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -722,10 +726,11 @@ void Curves::cmClearAll()
model->updateAllReferences(ModelData::REF_UPD_TYPE_CURVE, ModelData::REF_UPD_ACT_CLEAR, i);
}
update();
emit updateDataModels();
emit modified();
}
void Curves::cmCopy()
void CurvesPanel::cmCopy()
{
QByteArray data;
data.append((char*)&model->curves[selectedIndex], sizeof(CurveData));
@@ -734,7 +739,7 @@ void Curves::cmCopy()
QApplication::clipboard()->setMimeData(mimeData,QClipboard::Clipboard);
}
void Curves::cmCut()
void CurvesPanel::cmCut()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Cut Curve. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -742,7 +747,7 @@ void Curves::cmCut()
cmClear(false);
}
void Curves::cmDelete()
void CurvesPanel::cmDelete()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Delete Curve. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -751,40 +756,43 @@ void Curves::cmDelete()
model->curves[maxCurves - 1].clear();
model->updateAllReferences(ModelData::REF_UPD_TYPE_CURVE, ModelData::REF_UPD_ACT_SHIFT, selectedIndex, 0, -1);
update();
emit updateDataModels();
emit modified();
}
void Curves::cmInsert()
void CurvesPanel::cmInsert()
{
memmove(&model->curves[selectedIndex + 1], &model->curves[selectedIndex], (CPN_MAX_CURVES - (selectedIndex + 1)) * sizeof(CurveData));
model->curves[selectedIndex].clear();
model->updateAllReferences(ModelData::REF_UPD_TYPE_CURVE, ModelData::REF_UPD_ACT_SHIFT, selectedIndex, 0, 1);
update();
emit updateDataModels();
emit modified();
}
void Curves::cmMoveDown()
void CurvesPanel::cmMoveDown()
{
swapData(selectedIndex, selectedIndex + 1);
}
void Curves::cmMoveUp()
void CurvesPanel::cmMoveUp()
{
swapData(selectedIndex, selectedIndex - 1);
}
void Curves::cmPaste()
void CurvesPanel::cmPaste()
{
QByteArray data;
if (hasClipboardData(&data)) {
CurveData *cd = &model->curves[selectedIndex];
memcpy(cd, data.constData(), sizeof(CurveData));
update();
emit updateDataModels();
emit modified();
}
}
void Curves::swapData(int idx1, int idx2)
void CurvesPanel::swapData(int idx1, int idx2)
{
if ((idx1 != idx2) && (!model->curves[idx1].isEmpty() || !model->curves[idx2].isEmpty())) {
CurveData cdtmp = model->curves[idx2];
@@ -794,6 +802,7 @@ void Curves::swapData(int idx1, int idx2)
memcpy(cd1, &cdtmp, sizeof(CurveData));
model->updateAllReferences(ModelData::REF_UPD_TYPE_CURVE, ModelData::REF_UPD_ACT_SWAP, idx1, idx2);
update();
emit updateDataModels();
emit modified();
}
}