From c3328ce0388eae40987ed0d4ec32e4673fa6521b Mon Sep 17 00:00:00 2001 From: Till Theato Date: Thu, 1 Jul 2010 15:39:20 +0000 Subject: [PATCH] cleanup keyframewidget (remove code duplications) svn path=/trunk/kdenlive/; revision=4561 --- src/keyframeedit.cpp | 188 ++++++++++++++++++------------------------- src/keyframeedit.h | 17 +++- 2 files changed, 95 insertions(+), 110 deletions(-) diff --git a/src/keyframeedit.cpp b/src/keyframeedit.cpp index 8f14b44f..c46de550 100644 --- a/src/keyframeedit.cpp +++ b/src/keyframeedit.cpp @@ -74,7 +74,8 @@ KeyframeEdit::~KeyframeEdit() while ((child = m_slidersLayout->takeAt(0)) != 0) { QWidget *wid = child->widget(); delete child; - if (wid) delete wid; + if (wid) + delete wid; } //delete m_delegate; } @@ -100,9 +101,7 @@ void KeyframeEdit::addParameter(QDomElement e) bool found = false; int j; for (j = 0; j < keyframe_list->rowCount(); j++) { - int currentPos; - if (KdenliveSettings::frametimecode()) currentPos = keyframe_list->verticalHeaderItem(j)->text().toInt(); - else currentPos = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(j)->text()); + int currentPos = getPos(j); if (frame == currentPos) { keyframe_list->setItem(j, columnId, new QTableWidgetItem(frames.at(i).section(':', 1, 1))); found = true; @@ -113,10 +112,7 @@ void KeyframeEdit::addParameter(QDomElement e) } if (!found) { keyframe_list->insertRow(j); - QString currentPos; - if (KdenliveSettings::frametimecode()) currentPos = QString::number(frame); - else currentPos = m_timecode.getTimecodeFromFrames(frame); - keyframe_list->setVerticalHeaderItem(j, new QTableWidgetItem(currentPos)); + keyframe_list->setVerticalHeaderItem(j, new QTableWidgetItem(getPosString(frame))); keyframe_list->setItem(j, columnId, new QTableWidgetItem(frames.at(i).section(':', 1, 1))); keyframe_list->resizeRowToContents(j); } @@ -150,10 +146,7 @@ void KeyframeEdit::setupParam() for (int i = 0; i < frames.count(); i++) { keyframe_list->insertRow(i); int currentpos = frames.at(i).section(':', 0, 0).toInt(); - QString framePos; - if (KdenliveSettings::frametimecode()) framePos = QString::number(currentpos); - else framePos = m_timecode.getTimecodeFromFrames(currentpos); - keyframe_list->setVerticalHeaderItem(i, new QTableWidgetItem(framePos)); + keyframe_list->setVerticalHeaderItem(i, new QTableWidgetItem(getPosString(currentpos))); keyframe_list->setItem(i, col, new QTableWidgetItem(frames.at(i).section(':', 1, 1))); if ((m_active_keyframe > -1) && (m_active_keyframe == currentpos)) { keyframe_list->setCurrentCell(i, 0); @@ -171,7 +164,8 @@ void KeyframeEdit::setupParam() void KeyframeEdit::slotDeleteKeyframe() { - if (keyframe_list->rowCount() < 2) return; + if (keyframe_list->rowCount() < 2) + return; int col = keyframe_list->currentColumn(); int row = keyframe_list->currentRow(); keyframe_list->removeRow(keyframe_list->currentRow()); @@ -189,17 +183,12 @@ void KeyframeEdit::slotAddKeyframe() int row = keyframe_list->currentRow(); int col = keyframe_list->currentColumn(); int newrow = row; - int pos1; - if (KdenliveSettings::frametimecode()) pos1 = keyframe_list->verticalHeaderItem(row)->text().toInt(); - else pos1 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(row)->text()); + int pos1 = getPos(row); int result; kDebug() << "// ADD KF: " << row << ", MAX: " << keyframe_list->rowCount() << ", POS: " << pos1; if (row < (keyframe_list->rowCount() - 1)) { - int pos2; - if (KdenliveSettings::frametimecode()) pos2 = keyframe_list->verticalHeaderItem(row + 1)->text().toInt(); - else pos2 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(row + 1)->text()); - result = pos1 + (pos2 - pos1) / 2; + result = pos1 + (getPos(row + 1) - pos1) / 2; newrow++; } else if (row == 0) { if (pos1 == m_min) { @@ -209,20 +198,15 @@ void KeyframeEdit::slotAddKeyframe() result = m_min; } } else { - int pos2; - if (KdenliveSettings::frametimecode()) pos2 = keyframe_list->verticalHeaderItem(row - 1)->text().toInt(); - else pos2 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(row - 1)->text()); + int pos2 = getPos(row - 1); result = pos2 + (pos1 - pos2) / 2; } keyframe_list->insertRow(newrow); - QString currentPos; - if (KdenliveSettings::frametimecode()) currentPos = QString::number(result); - else currentPos = m_timecode.getTimecodeFromFrames(result); - keyframe_list->setVerticalHeaderItem(newrow, new QTableWidgetItem(currentPos)); - for (int i = 0; i < keyframe_list->columnCount(); i++) { + keyframe_list->setVerticalHeaderItem(newrow, new QTableWidgetItem(getPosString(result))); + for (int i = 0; i < keyframe_list->columnCount(); i++) keyframe_list->setItem(newrow, i, new QTableWidgetItem(keyframe_list->item(item->row(), i)->text())); - } + keyframe_list->resizeRowsToContents(); //keyframe_list->resizeRowToContents(newrow); slotAdjustKeyframeInfo(); @@ -239,37 +223,29 @@ void KeyframeEdit::slotGenerateParams(int row, int column) if (column == -1) { // position of keyframe changed QTableWidgetItem *item = keyframe_list->item(row, 0); - if (item == NULL) return; - QString val = keyframe_list->verticalHeaderItem(row)->text(); - int pos; - if (KdenliveSettings::frametimecode()) pos = val.toInt(); - else pos = m_timecode.getFrameCount(val); + if (item == NULL) + return; - if (pos <= m_min) { + int pos = getPos(row); + if (pos <= m_min) pos = m_min; - if (KdenliveSettings::frametimecode()) val = QString::number(pos); - else val = m_timecode.getTimecodeFromFrames(pos); - } - if (pos > m_max) { + if (pos > m_max) pos = m_max; - if (KdenliveSettings::frametimecode()) val = QString::number(pos); - else val = m_timecode.getTimecodeFromFrames(pos); - } - if (val != keyframe_list->verticalHeaderItem(row)->text()) keyframe_list->verticalHeaderItem(row)->setText(val); + QString val = getPosString(pos); + if (val != keyframe_list->verticalHeaderItem(row)->text()) + keyframe_list->verticalHeaderItem(row)->setText(val); for (int col = 0; col < keyframe_list->horizontalHeader()->count(); col++) { item = keyframe_list->item(row, col); int v = item->text().toInt(); - if (v >= m_params.at(col).attribute("max").toInt()) item->setText(m_params.at(col).attribute("max")); - if (v <= m_params.at(col).attribute("min").toInt()) item->setText(m_params.at(col).attribute("min")); + if (v >= m_params.at(col).attribute("max").toInt()) + item->setText(m_params.at(col).attribute("max")); + if (v <= m_params.at(col).attribute("min").toInt()) + item->setText(m_params.at(col).attribute("min")); QString keyframes; for (int i = 0; i < keyframe_list->rowCount(); i++) { - if (keyframe_list->item(i, col)) { - int pos3; - if (KdenliveSettings::frametimecode()) pos3 = keyframe_list->verticalHeaderItem(i)->text().toInt(); - else pos3 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(i)->text()); - keyframes.append(QString::number(pos3) + ':' + keyframe_list->item(i, col)->text() + ';'); - } + if (keyframe_list->item(i, col)) + keyframes.append(QString::number(getPos(i)) + ':' + keyframe_list->item(i, col)->text() + ';'); } m_params[col].setAttribute("keyframes", keyframes); } @@ -279,44 +255,35 @@ void KeyframeEdit::slotGenerateParams(int row, int column) } QTableWidgetItem *item = keyframe_list->item(row, column); - if (item == NULL) return; - QString val = keyframe_list->verticalHeaderItem(row)->text(); - ; - int pos; - if (KdenliveSettings::frametimecode()) pos = val.toInt(); - else pos = m_timecode.getFrameCount(val); - - if (pos <= m_min) { + if (item == NULL) + return; + + int pos = getPos(row); + if (pos <= m_min) pos = m_min; - if (KdenliveSettings::frametimecode()) val = QString::number(pos); - else val = m_timecode.getTimecodeFromFrames(pos); - } - if (pos > m_max) { + if (pos > m_max) pos = m_max; - if (KdenliveSettings::frametimecode()) val = QString::number(pos); - else val = m_timecode.getTimecodeFromFrames(pos); - } /*QList duplicates = keyframe_list->findItems(val, Qt::MatchExactly, 0); duplicates.removeAll(item); if (!duplicates.isEmpty()) { // Trying to insert a keyframe at existing value, revert it val = m_timecode.getTimecodeFromFrames(m_previousPos); }*/ - if (val != keyframe_list->verticalHeaderItem(row)->text()) keyframe_list->verticalHeaderItem(row)->setText(val); + QString val = getPosString(pos); + if (val != keyframe_list->verticalHeaderItem(row)->text()) + keyframe_list->verticalHeaderItem(row)->setText(val); int v = item->text().toInt(); - if (v >= m_params.at(column).attribute("max").toInt()) item->setText(m_params.at(column).attribute("max")); - if (v <= m_params.at(column).attribute("min").toInt()) item->setText(m_params.at(column).attribute("min")); + if (v >= m_params.at(column).attribute("max").toInt()) + item->setText(m_params.at(column).attribute("max")); + if (v <= m_params.at(column).attribute("min").toInt()) + item->setText(m_params.at(column).attribute("min")); slotAdjustKeyframeInfo(false); QString keyframes; for (int i = 0; i < keyframe_list->rowCount(); i++) { - if (keyframe_list->item(i, column)) { - int pos3; - if (KdenliveSettings::frametimecode()) pos3 = keyframe_list->verticalHeaderItem(i)->text().toInt(); - else pos3 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(i)->text()); - keyframes.append(QString::number(pos3) + ':' + keyframe_list->item(i, column)->text() + ';'); - } + if (keyframe_list->item(i, column)) + keyframes.append(QString::number(getPos(i)) + ':' + keyframe_list->item(i, column)->text() + ';'); } m_params[column].setAttribute("keyframes", keyframes); emit parameterChanged(); @@ -327,12 +294,8 @@ void KeyframeEdit::generateAllParams() for (int col = 0; col < keyframe_list->columnCount(); col++) { QString keyframes; for (int i = 0; i < keyframe_list->rowCount(); i++) { - if (keyframe_list->item(i, col)) { - int pos3; - if (KdenliveSettings::frametimecode()) pos3 = keyframe_list->verticalHeaderItem(i)->text().toInt(); - else pos3 = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(i)->text()); - keyframes.append(QString::number(pos3) + ':' + keyframe_list->item(i, col)->text() + ';'); - } + if (keyframe_list->item(i, col)) + keyframes.append(QString::number(getPos(i)) + ':' + keyframe_list->item(i, col)->text() + ';'); } m_params[col].setAttribute("keyframes", keyframes); } @@ -345,7 +308,8 @@ const QString KeyframeEdit::getValue(const QString &name) QDomNode na = m_params.at(col).firstChildElement("name"); QString paramName = i18n(na.toElement().text().toUtf8().data()); kDebug() << paramName << " == " << name; - if (paramName == name) return m_params.at(col).attribute("keyframes"); + if (paramName == name) + return m_params.at(col).attribute("keyframes"); } return QString(); } @@ -353,48 +317,40 @@ const QString KeyframeEdit::getValue(const QString &name) void KeyframeEdit::slotAdjustKeyframeInfo(bool seek) { QTableWidgetItem *item = keyframe_list->currentItem(); - if (!item) return; + if (!item) + return; int min = m_min; int max = m_max; QTableWidgetItem *above = keyframe_list->item(item->row() - 1, item->column()); QTableWidgetItem *below = keyframe_list->item(item->row() + 1, item->column()); - if (above) { - if (KdenliveSettings::frametimecode()) min = keyframe_list->verticalHeaderItem(above->row())->text().toInt(); - else min = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(above->row())->text()); - min++; - } - if (below) { - if (KdenliveSettings::frametimecode()) max = keyframe_list->verticalHeaderItem(below->row())->text().toInt(); - else max = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(below->row())->text()); - max--; - } + if (above) + min = getPos(above->row()) + 1; + if (below) + max = getPos(below->row()) - 1; + keyframe_pos->blockSignals(true); keyframe_pos->setRange(min, max); - int pos; - if (KdenliveSettings::frametimecode()) pos = keyframe_list->verticalHeaderItem(item->row())->text().toInt(); - else pos = m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(item->row())->text()); - - keyframe_pos->setValue(pos); + keyframe_pos->setValue(getPos(item->row())); keyframe_pos->blockSignals(false); for (int col = 0; col < keyframe_list->columnCount(); col++) { QSlider *sl = static_cast (m_slidersLayout->itemAtPosition(col, 1)->widget()); - if (!sl) continue; + if (!sl) + continue; sl->blockSignals(true); sl->setValue(keyframe_list->item(item->row(), col)->text().toInt()); sl->blockSignals(false); } - if (KdenliveSettings::keyframeseek() && seek) emit seekToPos(keyframe_pos->value() - m_min); + if (KdenliveSettings::keyframeseek() && seek) + emit seekToPos(keyframe_pos->value() - m_min); } void KeyframeEdit::slotAdjustKeyframePos(int value) { QTableWidgetItem *item = keyframe_list->currentItem(); - QString val; - if (KdenliveSettings::frametimecode()) val = QString::number(value); - else val = m_timecode.getTimecodeFromFrames(value); - keyframe_list->verticalHeaderItem(item->row())->setText(val); + keyframe_list->verticalHeaderItem(item->row())->setText(getPosString(value)); slotGenerateParams(item->row(), -1); - if (KdenliveSettings::keyframeseek()) emit seekToPos(value - m_min); + if (KdenliveSettings::keyframeseek()) + emit seekToPos(value - m_min); } void KeyframeEdit::slotAdjustKeyframeValue(int /*value*/) @@ -402,17 +358,33 @@ void KeyframeEdit::slotAdjustKeyframeValue(int /*value*/) QTableWidgetItem *item = keyframe_list->currentItem(); for (int col = 0; col < keyframe_list->columnCount(); col++) { QSlider *sl = static_cast (m_slidersLayout->itemAtPosition(col, 1)->widget()); - if (!sl) continue; + if (!sl) + continue; int val = sl->value(); QTableWidgetItem *nitem = keyframe_list->item(item->row(), col); - if (nitem->text().toInt() != val) { + if (nitem->text().toInt() != val) nitem->setText(QString::number(val)); - } } //keyframe_list->item(item->row() - 1, item->column()); } +int KeyframeEdit::getPos(int row) +{ + if (KdenliveSettings::frametimecode()) + return keyframe_list->verticalHeaderItem(row)->text().toInt(); + else + return m_timecode.getFrameCount(keyframe_list->verticalHeaderItem(row)->text()); +} + +QString KeyframeEdit::getPosString(int pos) +{ + if (KdenliveSettings::frametimecode()) + return QString::number(pos); + else + return m_timecode.getTimecodeFromFrames(pos); +} + void KeyframeEdit::slotSetSeeking(int state) { KdenliveSettings::setKeyframeseek(state == Qt::Checked); diff --git a/src/keyframeedit.h b/src/keyframeedit.h index 3df18eb7..5a903755 100644 --- a/src/keyframeedit.h +++ b/src/keyframeedit.h @@ -43,7 +43,9 @@ public: connect(spin, SIGNAL(valueChanged(int)), this, SLOT(commitEditorData())); connect(spin, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); return spin; - } else return QItemDelegate::createEditor(parent, option, index); + } else { + return QItemDelegate::createEditor(parent, option, index); + } } @@ -52,7 +54,9 @@ public: QSpinBox *spin = qobject_cast< QSpinBox* >(editor); spin->setRange(m_min, m_max); spin->setValue(index.model()->data(index).toInt()); - } else QItemDelegate::setEditorData(editor, index); + } else { + QItemDelegate::setEditorData(editor, index); + } } private slots: @@ -94,6 +98,13 @@ private: QGridLayout *m_slidersLayout; int m_active_keyframe; + /** @brief Gets the position of a keyframe from the table. + * @param row Row of the keyframe in the table */ + int getPos(int row); + /** @brief Converts a frame value to timecode considering the frames vs. HH:MM:SS:FF setting. + * @return timecode */ + QString getPosString(int pos); + public slots: @@ -104,6 +115,8 @@ private slots: void slotAdjustKeyframeInfo(bool seek = true); void slotAdjustKeyframePos(int value); void slotAdjustKeyframeValue(int value); + /** @brief Turns the seek to keyframe position setting on/off. + * @param state State of the associated checkbox */ void slotSetSeeking(int state); //void slotSaveCurrentParam(QTreeWidgetItem *item, int column); -- 2.39.2