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
+68 -49
View File
@@ -20,6 +20,7 @@
#include "channels.h"
#include "helpers.h"
#include "rawitemfilteredmodel.h"
LimitsGroup::LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row, int col, int & value, const ModelData & model, int min, int max, int deflt, ModelPanel * panel):
firmware(firmware),
@@ -95,15 +96,16 @@ void LimitsGroup::updateMinMax(int max)
}
}
}
Channels::Channels(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware):
ChannelsPanel::ChannelsPanel(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware, CurveItemModel * curveItemModel):
ModelPanel(parent, model, generalSettings, firmware)
{
Stopwatch s1("Channels");
chnCapability = firmware->getCapability(Outputs);
int channelNameMaxLen = firmware->getCapability(ChannelsName);
curveModel = new RawItemFilteredModel(curveItemModel, RawItemFilteredModel::AllFilter, this);
connect(curveModel, &RawItemFilteredModel::dataAboutToBeUpdated, this, &ChannelsPanel::onModelDataAboutToBeUpdated);
connect(curveModel, &RawItemFilteredModel::dataUpdateComplete, this, &ChannelsPanel::onModelDataUpdateComplete);
QStringList headerLabels;
headerLabels << "#";
if (channelNameMaxLen > 0) {
@@ -118,8 +120,6 @@ Channels::Channels(QWidget * parent, ModelData & model, GeneralSettings & genera
headerLabels << tr("Linear Subtrim");
TableLayout *tableLayout = new TableLayout(this, chnCapability, headerLabels);
s1.report("header");
for (int i = 0; i < chnCapability; i++) {
int col = 0;
@@ -165,6 +165,7 @@ Channels::Channels(QWidget * parent, ModelData & model, GeneralSettings & genera
if (IS_HORUS_OR_TARANIS(firmware->getBoard())) {
curveCB[i] = new QComboBox(this);
curveCB[i]->setProperty("index", i);
curveCB[i]->setModel(curveModel);
connect(curveCB[i], SIGNAL(currentIndexChanged(int)), this, SLOT(curveEdited()));
tableLayout->addWidget(i, col++, curveCB[i]);
}
@@ -193,15 +194,13 @@ Channels::Channels(QWidget * parent, ModelData & model, GeneralSettings & genera
}
}
update();
s1.report("add elements");
disableMouseScrolling();
tableLayout->resizeColumnsToContents();
tableLayout->pushRowsUp(chnCapability+1);
s1.report("end");
}
Channels::~Channels()
ChannelsPanel::~ChannelsPanel()
{
// compiler warning if delete[]
for (int i = 0; i < CPN_MAX_CHNOUT; i++) {
@@ -214,9 +213,10 @@ Channels::~Channels()
delete centerSB[i];
delete symlimitsChk[i];
}
delete curveModel;
}
void Channels::symlimitsEdited()
void ChannelsPanel::symlimitsEdited()
{
if (!lock) {
QCheckBox *ckb = qobject_cast<QCheckBox*>(sender());
@@ -226,17 +226,20 @@ void Channels::symlimitsEdited()
}
}
void Channels::nameEdited()
void ChannelsPanel::nameEdited()
{
if (!lock) {
QLineEdit *le = qobject_cast<QLineEdit*>(sender());
int index = le->property("index").toInt();
strcpy(model->limitData[index].name, le->text().toLatin1());
emit modified();
if (model->limitData[index].name != le->text()) {
strcpy(model->limitData[index].name, le->text().toLatin1());
emit updateDataModels();
emit modified();
}
}
}
void Channels::refreshExtendedLimits()
void ChannelsPanel::refreshExtendedLimits()
{
int channelMax = model->getChannelsMax();
@@ -248,7 +251,7 @@ void Channels::refreshExtendedLimits()
emit modified();
}
void Channels::invEdited()
void ChannelsPanel::invEdited()
{
if (!lock) {
QComboBox *cb = qobject_cast<QComboBox*>(sender());
@@ -258,17 +261,20 @@ void Channels::invEdited()
}
}
void Channels::curveEdited()
void ChannelsPanel::curveEdited()
{
if (!lock) {
QComboBox *cb = qobject_cast<QComboBox*>(sender());
int index = cb->property("index").toInt();
model->limitData[index].curve = CurveReference(CurveReference::CURVE_REF_CUSTOM, cb->itemData(cb->currentIndex()).toInt());
emit modified();
// ignore unnecessary updates that could be triggered by updates to the data model
if (model->limitData[index].curve != CurveReference(CurveReference::CURVE_REF_CUSTOM, cb->itemData(cb->currentIndex()).toInt())) {
model->limitData[index].curve = CurveReference(CurveReference::CURVE_REF_CUSTOM, cb->itemData(cb->currentIndex()).toInt());
emit modified();
}
}
}
void Channels::ppmcenterEdited()
void ChannelsPanel::ppmcenterEdited()
{
if (!lock) {
QSpinBox *sb = qobject_cast<QSpinBox*>(sender());
@@ -278,51 +284,49 @@ void Channels::ppmcenterEdited()
}
}
void Channels::update()
void ChannelsPanel::update()
{
for (int i = 0; i < chnCapability; i++) {
updateLine(i);
}
}
void Channels::updateLine(int i)
void ChannelsPanel::updateLine(int i)
{
lock = true;
LimitData &chn = model->limitData[i];
if (firmware->getCapability(ChannelsName) > 0) {
name[i]->setText(model->limitData[i].name);
name[i]->setText(chn.name);
}
chnOffset[i]->setValue(model->limitData[i].offset);
chnMin[i]->setValue(model->limitData[i].min);
chnMax[i]->setValue(model->limitData[i].max);
invCB[i]->setCurrentIndex((model->limitData[i].revert) ? 1 : 0);
chnOffset[i]->setValue(chn.offset);
chnMin[i]->setValue(chn.min);
chnMax[i]->setValue(chn.max);
invCB[i]->setCurrentIndex((chn.revert) ? 1 : 0);
if (IS_HORUS_OR_TARANIS(firmware->getBoard())) {
int numcurves = firmware->getCapability(NumCurves);
curveCB[i]->clear();
for (int j = -numcurves; j <= numcurves; j++) {
curveCB[i]->addItem(CurveReference(CurveReference::CURVE_REF_CUSTOM, j).toString(model, false), j);
}
curveCB[i]->setCurrentIndex(model->limitData[i].curve.value + numcurves);
curveCB[i]->setCurrentIndex(curveCB[i]->findData(chn.curve.value));
}
if (firmware->getCapability(PPMCenter)) {
centerSB[i]->setValue(model->limitData[i].ppmCenter + 1500);
centerSB[i]->setValue(chn.ppmCenter + 1500);
}
if (firmware->getCapability(SYMLimits)) {
symlimitsChk[i]->setChecked(model->limitData[i].symetrical);
symlimitsChk[i]->setChecked(chn.symetrical);
}
lock = false;
}
void Channels::cmPaste()
void ChannelsPanel::cmPaste()
{
QByteArray data;
if (hasClipboardData(&data)) {
memcpy(&model->limitData[selectedIndex], data.constData(), sizeof(LimitData));
updateLine(selectedIndex);
emit updateDataModels();
emit modified();
}
}
void Channels::cmDelete()
void ChannelsPanel::cmDelete()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Delete Channel. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -335,10 +339,11 @@ void Channels::cmDelete()
updateLine(i);
}
emit updateDataModels();
emit modified();
}
void Channels::cmCopy()
void ChannelsPanel::cmCopy()
{
QByteArray data;
data.append((char*)&model->limitData[selectedIndex], sizeof(LimitData));
@@ -347,7 +352,7 @@ void Channels::cmCopy()
QApplication::clipboard()->setMimeData(mimeData,QClipboard::Clipboard);
}
void Channels::cmCut()
void ChannelsPanel::cmCut()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Cut Channel. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -356,7 +361,7 @@ void Channels::cmCut()
cmClear(false);
}
void Channels::onCustomContextMenuRequested(QPoint pos)
void ChannelsPanel::onCustomContextMenuRequested(QPoint pos)
{
QLabel *label = (QLabel *)sender();
selectedIndex = label->property("index").toInt();
@@ -378,7 +383,7 @@ void Channels::onCustomContextMenuRequested(QPoint pos)
contextMenu.exec(globalPos);
}
bool Channels::hasClipboardData(QByteArray * data) const
bool ChannelsPanel::hasClipboardData(QByteArray * data) const
{
const QClipboard * clipboard = QApplication::clipboard();
const QMimeData * mimeData = clipboard->mimeData();
@@ -390,32 +395,32 @@ bool Channels::hasClipboardData(QByteArray * data) const
return false;
}
bool Channels::insertAllowed() const
bool ChannelsPanel::insertAllowed() const
{
return ((selectedIndex < chnCapability - 1) && (model->limitData[chnCapability - 1].isEmpty()));
}
bool Channels::moveDownAllowed() const
bool ChannelsPanel::moveDownAllowed() const
{
return selectedIndex < chnCapability - 1;
}
bool Channels::moveUpAllowed() const
bool ChannelsPanel::moveUpAllowed() const
{
return selectedIndex > 0;
}
void Channels::cmMoveUp()
void ChannelsPanel::cmMoveUp()
{
swapData(selectedIndex, selectedIndex - 1);
}
void Channels::cmMoveDown()
void ChannelsPanel::cmMoveDown()
{
swapData(selectedIndex, selectedIndex + 1);
}
void Channels::cmClear(bool prompt)
void ChannelsPanel::cmClear(bool prompt)
{
if (prompt) {
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Clear Channel. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
@@ -425,10 +430,11 @@ void Channels::cmClear(bool prompt)
model->limitData[selectedIndex].clear();
model->updateAllReferences(ModelData::REF_UPD_TYPE_CHANNEL, ModelData::REF_UPD_ACT_CLEAR, selectedIndex);
updateLine(selectedIndex);
emit updateDataModels();
emit modified();
}
void Channels::cmClearAll()
void ChannelsPanel::cmClearAll()
{
if (QMessageBox::question(this, CPN_STR_APP_NAME, tr("Clear all Channels. Are you sure?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
@@ -438,19 +444,21 @@ void Channels::cmClearAll()
model->updateAllReferences(ModelData::REF_UPD_TYPE_CHANNEL, ModelData::REF_UPD_ACT_CLEAR, i);
updateLine(i);
}
emit updateDataModels();
emit modified();
}
void Channels::cmInsert()
void ChannelsPanel::cmInsert()
{
memmove(&model->limitData[selectedIndex + 1], &model->limitData[selectedIndex], (CPN_MAX_CHNOUT - (selectedIndex + 1)) * sizeof(LimitData));
model->limitData[selectedIndex].clear();
model->updateAllReferences(ModelData::REF_UPD_TYPE_CHANNEL, ModelData::REF_UPD_ACT_SHIFT, selectedIndex, 0, 1);
update();
emit updateDataModels();
emit modified();
}
void Channels::swapData(int idx1, int idx2)
void ChannelsPanel::swapData(int idx1, int idx2)
{
if ((idx1 != idx2) && (!model->limitData[idx1].isEmpty() || !model->limitData[idx2].isEmpty())) {
LimitData chntmp = model->limitData[idx2];
@@ -461,6 +469,17 @@ void Channels::swapData(int idx1, int idx2)
model->updateAllReferences(ModelData::REF_UPD_TYPE_CHANNEL, ModelData::REF_UPD_ACT_SWAP, idx1, idx2);
updateLine(idx1);
updateLine(idx2);
emit updateDataModels();
emit modified();
}
}
void ChannelsPanel::onModelDataAboutToBeUpdated()
{
lock = true;
}
void ChannelsPanel::onModelDataUpdateComplete()
{
update();
}