[Companion][InputsPanel] Fix "ghost" input names being left over when moving items (closes #5740); Cut/paste and drag-move will now also copy the input name if destination input is nameless and source input becomes empty (will not duplicate names); Clean up/simplify some code. (#5942)

This commit is contained in:
Max Paperno
2018-06-03 23:16:20 +02:00
committed by Bertrand Songis
parent c19b1a65bf
commit 487c0de90f
4 changed files with 113 additions and 125 deletions
+3 -10
View File
@@ -151,7 +151,7 @@ QVector<const MixData *> ModelData::mixes(int channel) const
return result; return result;
} }
void ModelData::removeInput(const int idx) void ModelData::removeInput(const int idx, bool clearName)
{ {
unsigned int chn = expoData[idx].chn; unsigned int chn = expoData[idx].chn;
@@ -159,15 +159,8 @@ void ModelData::removeInput(const int idx)
expoData[CPN_MAX_EXPOS-1].clear(); expoData[CPN_MAX_EXPOS-1].clear();
//also remove input name if removing last line for this input //also remove input name if removing last line for this input
bool found = false; if (clearName && !expos(chn).size())
for (int i=0; i<CPN_MAX_EXPOS; i++) { inputNames[chn][0] = 0;
if (expoData[i].mode==0) continue;
if (expoData[i].chn==chn) {
found = true;
break;
}
}
if (!found) inputNames[chn][0] = 0;
} }
void ModelData::clearInputs() void ModelData::clearInputs()
+1 -1
View File
@@ -136,7 +136,7 @@ class ModelData {
void convert(RadioDataConversionState & cstate); void convert(RadioDataConversionState & cstate);
ExpoData * insertInput(const int idx); ExpoData * insertInput(const int idx);
void removeInput(const int idx); void removeInput(const int idx, bool clearName = true);
bool isInputValid(const unsigned int idx) const; bool isInputValid(const unsigned int idx) const;
bool hasExpos(uint8_t inputIdx) const; bool hasExpos(uint8_t inputIdx) const;
+103 -110
View File
@@ -70,33 +70,20 @@ InputsPanel::~InputsPanel()
void InputsPanel::update() void InputsPanel::update()
{ {
lock = true; lock = true;
// curDest -> destination channel
// i -> mixer number
QByteArray qba;
ExposlistWidget->clear(); ExposlistWidget->clear();
int curDest = -1; for (int i=0; i < inputsCount; ++i) {
bool filled = false;
for (int i=0; i<CPN_MAX_EXPOS; i++) { for (uint j=0; j < CPN_MAX_EXPOS; ++j) {
ExpoData *md = &model->expoData[i]; const ExpoData & ed = model->expoData[j];
if ((int)ed.chn == i && ed.mode) {
if (!md->mode) AddInputLine(j);
break; filled = true;
}
while (curDest < (int)md->chn-1) {
curDest++;
AddInputLine(-curDest-1);
} }
if (AddInputLine(i)) { if (!filled) {
curDest++; AddInputLine(-i-1);
} }
} }
while (curDest < inputsCount-1) {
curDest++;
AddInputLine(-curDest-1);
}
lock = false; lock = false;
} }
@@ -109,65 +96,52 @@ void InputsPanel::update()
@param[in] dest defines which input line to create. @param[in] dest defines which input line to create.
If dest < 0 then create empty input slot for input -dest ( dest=-6 -> Input05) If dest < 0 then create empty input slot for input -dest ( dest=-6 -> Input05)
if dest >=0 then create used input based on model input data from slot dest (dest=4 -> model expoData[4]) if dest >=0 then create used input based on model input data from slot dest (dest=4 -> model expoData[4])
@retval true created input number is different from the previous list item
false created input number is the same as previous list item
*/ */
bool InputsPanel::AddInputLine(int dest) void InputsPanel::AddInputLine(int dest)
{ {
bool new_ch;
QString str = getInputText(dest, &new_ch);
QListWidgetItem *itm = new QListWidgetItem(str);
QByteArray qba(1, (quint8)dest); QByteArray qba(1, (quint8)dest);
unsigned destId = abs(dest); unsigned destId = abs(dest);
bool newChan = false;
bool hasSibs = false; bool hasSibs = false;
if (dest >= 0) { if (dest >= 0 && dest < CPN_MAX_EXPOS) {
//add input data //add input data
ExpoData *md = &model->expoData[dest]; const ExpoData &md = model->expoData[dest];
qba.append((const char*)md, sizeof(ExpoData)); qba.append((const char*)&md, sizeof(ExpoData));
destId = md->chn + 1; destId = md.chn + 1;
hasSibs = (dest < CPN_MAX_EXPOS && model->expoData[dest+1].chn == md->chn); const QVector<const ExpoData *> expos = model->expos(md.chn);
newChan = (expos.constFirst() == &md);
hasSibs = (expos.constLast() != &md);
} }
QListWidgetItem *itm = new QListWidgetItem(getInputText(dest, newChan));
itm->setData(Qt::UserRole, qba); itm->setData(Qt::UserRole, qba);
ExposlistWidget->addItem(itm, destId, new_ch, hasSibs); ExposlistWidget->addItem(itm, destId, newChan, hasSibs);
//qDebug() << "InputsPanel::AddInputLine(): dest" << dest << "text" << str;
return new_ch;
} }
/** /**
@brief Returns HTML formated input representation @brief Returns HTML formated input representation
@param[in] dest defines which input line to create. @param dest defines which input line to create.
If dest < 0 then create empty input slot for input -dest ( dest=-6 -> Input05) If dest < 0 then create empty input slot for input -dest ( dest=-6 -> Input05)
if dest >=0 then create used input based on model input data from slot dest (dest=4 -> model expoData[4]) if dest >=0 then create used input based on model input data from slot dest (dest=4 -> model expoData[4])
@param newChan If true, prints the full channel name in leading column, otherwise pads the column with spaces. Assumed true if @a dest < 0.
@retval string input line in HTML @retval string input line in HTML
*/ */
QString InputsPanel::getInputText(int dest, bool * new_ch) QString InputsPanel::getInputText(int dest, bool newChan)
{ {
QString str; QString str;
if (new_ch)
*new_ch = false;
if (dest < 0) { if (dest < 0) {
str = modelPrinter.printInputName(-dest-1); str = modelPrinter.printInputName(-dest-1);
if (new_ch)
*new_ch = true;
} }
else { else {
ExpoData & input = model->expoData[dest]; const ExpoData & input = model->expoData[dest];
int nameChars = (firmware->getCapability(VirtualInputs) ? 10 : 4); const int nameChars = (firmware->getCapability(VirtualInputs) ? 10 : 4);
if (newChan)
if ((dest == 0) || (model->expoData[dest-1].chn != input.chn)) {
if (new_ch)
*new_ch = true;
str = QString("%1").arg(modelPrinter.printInputName(input.chn), -nameChars, QChar(' ')); str = QString("%1").arg(modelPrinter.printInputName(input.chn), -nameChars, QChar(' '));
} else
else {
str = QString(nameChars, QChar(' ')); str = QString(nameChars, QChar(' '));
}
str.replace(" ", "&nbsp;"); str.replace(" ", "&nbsp;");
str += modelPrinter.printInputLine(input); str += modelPrinter.printInputLine(input);
} }
@@ -191,9 +165,9 @@ bool InputsPanel::gm_insertExpo(int idx)
return true; return true;
} }
void InputsPanel::gm_deleteExpo(int index) void InputsPanel::gm_deleteExpo(int index, bool clearName)
{ {
model->removeInput(index); model->removeInput(index, clearName);
} }
void InputsPanel::gm_openExpo(int index) void InputsPanel::gm_openExpo(int index)
@@ -266,7 +240,7 @@ void InputsPanel::exposDelete(bool ask)
if ((ret == QMessageBox::Yes) || (!ask)) { if ((ret == QMessageBox::Yes) || (!ask)) {
exposDeleteList(list); exposDeleteList(list, ask);
emit modified(); emit modified();
update(); update();
} }
@@ -300,7 +274,7 @@ void InputsPanel::mimeExpoDropped(int index, const QMimeData *data, Qt::DropActi
} }
else if (action==Qt::MoveAction) { else if (action==Qt::MoveAction) {
QList<int> list = createExpoListFromSelected(); QList<int> list = createExpoListFromSelected();
exposDeleteList(list); exposDeleteList(list, false);
foreach (const int del, list) { foreach (const int del, list) {
if (del < idx) if (del < idx)
--idx; --idx;
@@ -332,7 +306,9 @@ void InputsPanel::pasteExpoMimeData(const QMimeData * mimeData, int destIdx)
break; break;
ExpoData *md = &model->expoData[idx]; ExpoData *md = &model->expoData[idx];
memcpy(md, mxData.mid(i, sizeof(ExpoData)).constData(), sizeof(ExpoData)); memcpy(md, mxData.mid(i, sizeof(ExpoData)).constData(), sizeof(ExpoData));
const int oldChan = md->chn;
md->chn = dch; md->chn = dch;
maybeCopyInputName(oldChan, md->chn);
i += sizeof(ExpoData); i += sizeof(ExpoData);
} }
@@ -354,8 +330,8 @@ void InputsPanel::exposPaste()
void InputsPanel::exposDuplicate() void InputsPanel::exposDuplicate()
{ {
exposCopy(); exposCopy();
exposPaste(); exposPaste();
} }
@@ -394,7 +370,6 @@ void InputsPanel::expoAdd()
gm_openExpo(index); gm_openExpo(index);
} }
void InputsPanel::expolistWidget_customContextMenuRequested(QPoint pos) void InputsPanel::expolistWidget_customContextMenuRequested(QPoint pos)
{ {
QPoint globalPos = ExposlistWidget->mapToGlobal(pos); QPoint globalPos = ExposlistWidget->mapToGlobal(pos);
@@ -438,80 +413,81 @@ void InputsPanel::expolistWidget_KeyPress(QKeyEvent *event)
ExposlistWidget->setCurrentRow(ExposlistWidget->currentRow()-1); ExposlistWidget->setCurrentRow(ExposlistWidget->currentRow()-1);
} }
int InputsPanel::gm_moveExpo(int idx, bool dir) //true=inc=down false=dec=up int InputsPanel::gm_moveExpo(int idx, bool dir) //true=inc=down false=dec=up
{ {
if (idx >= CPN_MAX_EXPOS || idx < 0 || (dir && idx >= CPN_MAX_EXPOS - 1) || (!dir && !idx))
return -1;
if(idx>CPN_MAX_EXPOS || (idx==CPN_MAX_EXPOS && dir)) return idx; const int tdx = dir ? idx+1 : idx-1;
ExpoData temp;
ExpoData &src = model->expoData[idx];
ExpoData &tgt = model->expoData[tdx];
int tdx = dir ? idx+1 : idx-1; if (memcmp(&src, &temp, sizeof(ExpoData)) == 0) {
ExpoData temp; // src expo is empty
temp.clear(); return -1;
ExpoData &src=model->expoData[idx]; }
ExpoData &tgt=model->expoData[tdx];
if (!dir && tdx<0 && src.chn>0) { bool tgtempty = (memcmp(&tgt, &temp, sizeof(ExpoData)) == 0);
if (tgt.chn != src.chn || tgtempty) {
const int oldChan = src.chn;
if (dir && src.chn < unsigned(inputsCount-1))
src.chn++;
else if (!dir && src.chn > 0)
src.chn--; src.chn--;
return idx; maybeCopyInputName(oldChan, src.chn);
} return idx;
else if (!dir && tdx<0) { }
return idx;
}
if (memcmp(&src, &temp, sizeof(ExpoData)) == 0) { // flip between src and tgt
return idx; memcpy(&temp, &src, sizeof(ExpoData));
memcpy(&src, &tgt, sizeof(ExpoData));
memcpy(&tgt, &temp, sizeof(ExpoData));
return tdx;
}
void InputsPanel::moveExpoList(bool down)
{
QList<int> list = createExpoListFromSelected();
QList<int> highlightList;
bool mod = false;
foreach(int idx, list) {
const int newIdx = gm_moveExpo(idx, down);
if (newIdx > -1) {
highlightList << newIdx;
mod = true;
} }
}
bool tgtempty = (memcmp(&tgt, &temp, sizeof(ExpoData)) == 0); if (mod) {
emit modified();
if (tgt.chn!=src.chn || tgtempty) { update();
if ((dir) && (src.chn<unsigned(inputsCount-1))) src.chn++; }
if ((!dir) && (src.chn>0)) src.chn--; setSelectedByExpoList(highlightList);
return idx;
}
// flip between idx and tgt
memcpy(&temp, &src, sizeof(ExpoData));
memcpy(&src, &tgt, sizeof(ExpoData));
memcpy(&tgt, &temp, sizeof(ExpoData));
return tdx;
} }
void InputsPanel::moveExpoUp() void InputsPanel::moveExpoUp()
{ {
QList<int> list = createExpoListFromSelected(); moveExpoList(false);
QList<int> highlightList;
foreach(int idx, list) {
highlightList << gm_moveExpo(idx, false);
}
emit modified();
update();
setSelectedByExpoList(highlightList);
} }
void InputsPanel::moveExpoDown() void InputsPanel::moveExpoDown()
{ {
QList<int> list = createExpoListFromSelected(); moveExpoList(true);
QList<int> highlightList;
foreach(int idx, list) {
highlightList << gm_moveExpo(idx, true);
}
emit modified();
update();
setSelectedByExpoList(highlightList);
} }
void InputsPanel::expolistWidget_doubleClicked(QModelIndex index) void InputsPanel::expolistWidget_doubleClicked(QModelIndex index)
{ {
expoOpen(ExposlistWidget->item(index.row())); expoOpen(ExposlistWidget->item(index.row()));
} }
void InputsPanel::exposDeleteList(QList<int> list) void InputsPanel::exposDeleteList(QList<int> list, bool clearName)
{ {
qSort(list.begin(), list.end()); qSort(list.begin(), list.end());
int iDec = 0; int iDec = 0;
foreach(int idx, list) { foreach(int idx, list) {
gm_deleteExpo(idx-iDec); gm_deleteExpo(idx-iDec, clearName);
iDec++; iDec++;
} }
} }
@@ -524,3 +500,20 @@ void InputsPanel::clearExpos()
update(); update();
} }
} }
void InputsPanel::maybeCopyInputName(int srcChan, int destChan)
{
if (srcChan == destChan)
return;
// check to see if source input channel is now empty
const bool srcEmpty = (model->expos(srcChan).size() == 0);
if (srcEmpty) {
// if destination input name is empty, copy it from source expo input
if (!strlen(model->inputNames[destChan]))
strncpy(model->inputNames[destChan], model->inputNames[srcChan], 5);
// clear the emptry source channel name
model->inputNames[srcChan][0] = 0;
}
}
+6 -4
View File
@@ -37,6 +37,7 @@ class InputsPanel : public ModelPanel
private slots: private slots:
void clearExpos(); void clearExpos();
void moveExpoList(bool down);
void moveExpoUp(); void moveExpoUp();
void moveExpoDown(); void moveExpoDown();
void mimeExpoDropped(int index, const QMimeData *data, Qt::DropAction action); void mimeExpoDropped(int index, const QMimeData *data, Qt::DropAction action);
@@ -50,6 +51,7 @@ class InputsPanel : public ModelPanel
void exposDuplicate(); void exposDuplicate();
void expoOpen(QListWidgetItem *item = NULL); void expoOpen(QListWidgetItem *item = NULL);
void expoAdd(); void expoAdd();
void maybeCopyInputName(int srcChan, int destChan);
private: private:
bool expoInserted; bool expoInserted;
@@ -59,15 +61,15 @@ class InputsPanel : public ModelPanel
int getExpoIndex(unsigned int dch); int getExpoIndex(unsigned int dch);
bool gm_insertExpo(int idx); bool gm_insertExpo(int idx);
void gm_deleteExpo(int index); void gm_deleteExpo(int index, bool clearName = true);
void gm_openExpo(int index); void gm_openExpo(int index);
int gm_moveExpo(int idx, bool dir); int gm_moveExpo(int idx, bool dir);
void exposDeleteList(QList<int> list); void exposDeleteList(QList<int> list, bool clearName = true);
QList<int> createExpoListFromSelected(); QList<int> createExpoListFromSelected();
void setSelectedByExpoList(QList<int> list); void setSelectedByExpoList(QList<int> list);
void pasteExpoMimeData(const QMimeData * mimeData, int destIdx); void pasteExpoMimeData(const QMimeData * mimeData, int destIdx);
bool AddInputLine(int dest); void AddInputLine(int dest);
QString getInputText(int dest, bool * new_ch); QString getInputText(int dest, bool newChan);
}; };