X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Feffectslistwidget.cpp;h=4f433cc29e11037a9b8567dc74eebbd4002923e8;hb=26c1e6482f8f2396575e798daa7495d804080d55;hp=fa4ee15093666cf2bae884c099c5a5897638b826;hpb=0e80d47584de89836bdd85543f9e5e1e32ba1285;p=kdenlive diff --git a/src/effectslistwidget.cpp b/src/effectslistwidget.cpp index fa4ee150..4f433cc2 100644 --- a/src/effectslistwidget.cpp +++ b/src/effectslistwidget.cpp @@ -17,122 +17,307 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -#include "QApplication" -#include "QMouseEvent" -#include - -#include "KDebug" #include "effectslistwidget.h" #include "effectslist.h" #include "mainwindow.h" -#define EFFECT_VIDEO 1 -#define EFFECT_AUDIO 2 -#define EFFECT_CUSTOM 3 +#include +#include + +#include +#include +#include + + +static const int EFFECT_VIDEO = 1; +static const int EFFECT_AUDIO = 2; +static const int EFFECT_CUSTOM = 3; +static const int EFFECT_FOLDER = 4; + +const int TypeRole = Qt::UserRole; +const int IdRole = TypeRole + 1; -EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) - : KListWidget(parent), m_menu(menu) { + +EffectsListWidget::EffectsListWidget(QMenu *menu, QWidget *parent) : + QTreeWidget(parent), + m_menu(menu) +{ //setSelectionMode(QAbstractItemView::ExtendedSelection); //setDragDropMode(QAbstractItemView::DragDrop); + setColumnCount(1); setDropIndicatorShown(true); - setAlternatingRowColors(true); - setSortingEnabled(true); + //setAlternatingRowColors(true); setDragEnabled(true); setAcceptDrops(true); + setHeaderHidden(true); + setFrameShape(QFrame::NoFrame); + setAutoFillBackground(false); + setRootIsDecorated(false); + QPalette p = palette(); + p.setBrush(QPalette::Base, Qt::NoBrush); + setPalette(p); initList(); + connect(this, SIGNAL(activated(const QModelIndex &)), this, SLOT(slotExpandItem(const QModelIndex &))); +} + +EffectsListWidget::~EffectsListWidget() +{ } -EffectsListWidget::~EffectsListWidget() { +void EffectsListWidget::slotExpandItem(const QModelIndex & index) +{ + setExpanded(index, !isExpanded(index)); } -void EffectsListWidget::initList() { - clear(); - QStringList names = MainWindow::videoEffects.effectNames(); - QListWidgetItem *item; - foreach(const QString &str, names) { - item = new QListWidgetItem(str, this); - item->setData(Qt::UserRole, QString::number((int) EFFECT_VIDEO)); +void EffectsListWidget::initList() +{ + QString current; + QString currentFolder; + QTreeWidgetItem *item = NULL; + QTreeWidgetItem *parentItem; + bool found = false; + + if (currentItem()) { + current = currentItem()->text(0); + if (currentItem()->parent()) currentFolder = currentItem()->parent()->text(0); + else if (currentItem()->data(0, TypeRole) == EFFECT_FOLDER) currentFolder = currentItem()->text(0); + } + + QString effectName; + QStringList effectInfo; + KIcon videoIcon("kdenlive-show-video"); + KIcon audioIcon("kdenlive-show-audio"); + KIcon customIcon("kdenlive-custom-effect"); + KIcon folderIcon("folder"); + + QString effectCategory = KStandardDirs::locate("config", "kdenliveeffectscategory.rc"); + QDomDocument doc; + QFile file(effectCategory); + doc.setContent(&file, false); + file.close(); + QList folders; + QStringList folderNames; + QDomNodeList groups = doc.documentElement().elementsByTagName("group"); + for (int i = 0; i < groups.count(); i++) { + folderNames << groups.at(i).firstChild().firstChild().nodeValue(); + } + for (int i = 0; i < topLevelItemCount(); i++) { + topLevelItem(i)->takeChildren(); + QString currentName = topLevelItem(i)->text(0); + if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18n("Custom") && !folderNames.contains(currentName)) { + takeTopLevelItem(i); + i--; + } + } + + for (int i = 0; i < groups.count(); i++) { + item = findFolder(folderNames.at(i)); + if (item) { + item->setData(0, IdRole, groups.at(i).toElement().attribute("list")); + } else { + item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i))); + item->setIcon(0, folderIcon); + item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER)); + item->setData(0, IdRole, groups.at(i).toElement().attribute("list")); + insertTopLevelItem(0, item); + } + folders.append(item); + } + + QTreeWidgetItem *misc = findFolder(i18n("Misc")); + if (misc == NULL) { + misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc"))); + misc->setIcon(0, folderIcon); + misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER)); + insertTopLevelItem(0, misc); + } + + QTreeWidgetItem *audio = findFolder(i18n("Audio")); + if (audio == NULL) { + audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio"))); + audio->setIcon(0, folderIcon); + audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER)); + insertTopLevelItem(0, audio); + } + + QTreeWidgetItem *custom = findFolder(i18n("Custom")); + if (custom == NULL) { + custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Custom"))); + custom->setIcon(0, folderIcon); + custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER)); + insertTopLevelItem(0, custom); } - names = MainWindow::audioEffects.effectNames(); - foreach(const QString &str, names) { - item = new QListWidgetItem(str, this); - item->setData(Qt::UserRole, QString::number((int) EFFECT_AUDIO)); + //insertTopLevelItems(0, folders); + + + int ct = MainWindow::videoEffects.count(); + for (int ix = 0; ix < ct; ix ++) { + effectInfo = MainWindow::videoEffects.effectIdInfo(ix); + parentItem = NULL; + for (int i = 0; i < folders.count(); i++) { + QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts); + if (l.contains(effectInfo.at(2))) { + parentItem = folders.at(i); + break; + } + } + if (parentItem == NULL) parentItem = misc; + if (!effectInfo.isEmpty()) { + item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst())); + item->setIcon(0, videoIcon); + item->setData(0, TypeRole, QString::number((int) EFFECT_VIDEO)); + item->setData(0, IdRole, effectInfo); + if (item->text(0) == current) { + setCurrentItem(item); + found = true; + } + } + } + + ct = MainWindow::audioEffects.count(); + for (int ix = 0; ix < ct; ix ++) { + effectInfo = MainWindow::audioEffects.effectIdInfo(ix); + parentItem = NULL; + for (int i = 0; i < folders.count(); i++) { + QStringList l = folders.at(i)->data(0, IdRole).toString().split(',', QString::SkipEmptyParts); + if (l.contains(effectInfo.at(2))) { + parentItem = folders.at(i); + break; + } + } + if (parentItem == NULL) parentItem = audio; + if (!effectInfo.isEmpty()) { + item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst())); + item->setIcon(0, audioIcon); + item->setData(0, TypeRole, QString::number((int) EFFECT_AUDIO)); + item->setData(0, IdRole, effectInfo); + if (item->text(0) == current) { + setCurrentItem(item); + found = true; + } + } } - names = MainWindow::customEffects.effectNames(); - foreach(const QString &str, names) { - item = new QListWidgetItem(str, this); - item->setData(Qt::UserRole, QString::number((int) EFFECT_CUSTOM)); + ct = MainWindow::customEffects.count(); + kDebug() << "--- REBUILDING;: " << ct; + for (int ix = 0; ix < ct; ix ++) { + effectInfo = MainWindow::customEffects.effectIdInfo(ix); + if (!effectInfo.isEmpty()) { + item = new QTreeWidgetItem(custom, QStringList(effectInfo.takeFirst())); + item->setIcon(0, customIcon); + item->setData(0, TypeRole, QString::number((int) EFFECT_CUSTOM)); + item->setData(0, IdRole, effectInfo); + if (item->text(0) == current) { + setCurrentItem(item); + found = true; + } + } + } + if (!found && !currentFolder.isEmpty()) { + // previously selected effect was removed, focus on its parent folder + for (int i = 0; i < topLevelItemCount(); i++) { + if (topLevelItem(i)->text(0) == currentFolder) { + setCurrentItem(topLevelItem(i)); + break; + } + } + + } + setSortingEnabled(true); + sortByColumn(0, Qt::AscendingOrder); +} + +QTreeWidgetItem *EffectsListWidget::findFolder(const QString name) +{ + QTreeWidgetItem *item = NULL; + QList result = findItems(name, Qt::MatchExactly); + if (!result.isEmpty()) { + for (int j = 0; j < result.count(); j++) { + if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) { + item = result.at(j); + break; + } + } } + return item; } -QDomElement EffectsListWidget::currentEffect() { +const QDomElement EffectsListWidget::currentEffect() const +{ return itemEffect(currentItem()); } -QDomElement EffectsListWidget::itemEffect(QListWidgetItem *item) { +const QDomElement EffectsListWidget::itemEffect(QTreeWidgetItem *item) const +{ QDomElement effect; - if (!item) return effect; - switch (item->data(Qt::UserRole).toInt()) { + if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return effect; + QStringList effectInfo = item->data(0, IdRole).toStringList(); + kDebug() << "// EFFECT SELECTED: " << effectInfo; + switch (item->data(0, TypeRole).toInt()) { case 1: - effect = MainWindow::videoEffects.getEffectByName(item->text()); + effect = MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement(); break; case 2: - effect = MainWindow::audioEffects.getEffectByName(item->text()); + effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement(); break; default: - effect = MainWindow::customEffects.getEffectByName(item->text()); + effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement(); break; } return effect; } -QString EffectsListWidget::currentInfo() { - QListWidgetItem *item = currentItem(); - if (!item) return QString(); +QString EffectsListWidget::currentInfo() +{ + QTreeWidgetItem *item = currentItem(); + if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString(); QString info; - switch (item->data(Qt::UserRole).toInt()) { + QStringList effectInfo = item->data(0, IdRole).toStringList(); + switch (item->data(0, TypeRole).toInt()) { case 1: - info = MainWindow::videoEffects.getInfo(item->text()); + info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1)); break; case 2: - info = MainWindow::audioEffects.getInfo(item->text()); + info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1)); break; default: - info = MainWindow::customEffects.getInfo(item->text()); + info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1)); break; } return info; } // virtual -void EffectsListWidget::mousePressEvent(QMouseEvent *event) { +void EffectsListWidget::mousePressEvent(QMouseEvent *event) +{ if (event->button() == Qt::LeftButton) { - this->m_DragStartPosition = event->pos(); + m_DragStartPosition = event->pos(); m_dragStarted = true; } - KListWidget::mousePressEvent(event); + QTreeWidget::mousePressEvent(event); } // virtual -void EffectsListWidget::mouseMoveEvent(QMouseEvent *event) { +void EffectsListWidget::mouseMoveEvent(QMouseEvent *event) +{ if (!m_dragStarted) return; if ((event->pos() - m_DragStartPosition).manhattanLength() < QApplication::startDragDistance()) return; { - QListWidgetItem *clickItem = itemAt(event->pos()); - if (clickItem) { + QTreeWidgetItem *clickItem = itemAt(event->pos()); + if (clickItem && clickItem->data(0, TypeRole).toInt() != (int)EFFECT_FOLDER) { QDrag *drag = new QDrag(this); QMimeData *mimeData = new QMimeData; - const QList list = selectedItems(); + const QList list = selectedItems(); QDomDocument doc; - foreach(QListWidgetItem *item, list) { - doc.appendChild(doc.importNode(itemEffect(item), true)); + foreach(QTreeWidgetItem *item, list) { + const QDomElement e = itemEffect(item); + if (!e.isNull()) doc.appendChild(doc.importNode(e, true)); } QByteArray data; data.append(doc.toString().toUtf8()); @@ -141,26 +326,25 @@ void EffectsListWidget::mouseMoveEvent(QMouseEvent *event) { //QPixmap pix = qVariantValue(clickItem->data(Qt::DecorationRole)); //drag->setPixmap(pix); //drag->setHotSpot(QPoint(0, 50)); - drag->start(Qt::MoveAction); + drag->start(Qt::CopyAction); } - //event->accept(); + event->accept(); } } -void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event) { - event->setDropAction(Qt::IgnoreAction); - //if (item) { - event->setDropAction(Qt::MoveAction); +void EffectsListWidget::dragMoveEvent(QDragMoveEvent * event) +{ + event->setDropAction(Qt::CopyAction); if (event->mimeData()->hasText()) { event->acceptProposedAction(); } - //} } //virtual -void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event) { - QListWidgetItem *item = itemAt(event->pos()); - if (item && item->data(Qt::UserRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos()); +void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event) +{ + QTreeWidgetItem *item = itemAt(event->pos()); + if (item && item->data(0, TypeRole).toInt() == EFFECT_CUSTOM) m_menu->popup(event->globalPos()); } #include "effectslistwidget.moc"