effectslistwidget.cpp 19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/***************************************************************************
 *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/


#include "effectslistwidget.h"
Vincent Pinon's avatar
Vincent Pinon committed
22
#include "effectslist/effectslist.h"
Marco Gittler's avatar
Marco Gittler committed
23
#include "mainwindow.h"
24
#include "kdenlivesettings.h"
25

26
#include "klocalizedstring.h"
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
27
#include <QDebug>
28
#include <QAction>
29
#include <QMenu>
30
#include <QMimeData>
31
#include <QStandardPaths>
32 33


34 35
EffectsListWidget::EffectsListWidget(QWidget *parent) :
    QTreeWidget(parent)
36
{
37
    setColumnCount(1);
38
    setDragEnabled(true);
39
    setAcceptDrops(false);
40
    setHeaderHidden(true);
41 42
    setFrameShape(QFrame::NoFrame);
    setAutoFillBackground(false);
43 44
    setRootIsDecorated(true);
    setIndentation(10);
45 46
    //setSelectionMode(QAbstractItemView::ExtendedSelection);
    setDragDropMode(QAbstractItemView::DragOnly);
47
    updatePalette();
Laurent Montel's avatar
Laurent Montel committed
48
    connect(this, &EffectsListWidget::activated, this, &EffectsListWidget::slotExpandItem);
49 50
}

51 52
EffectsListWidget::~EffectsListWidget()
{
53 54
}

55 56 57
void EffectsListWidget::updatePalette()
{
    QPalette p = qApp->palette();
58
    p.setBrush(QPalette::Base, QBrush(Qt::transparent));
59 60 61
    setPalette(p);
}

62 63 64 65 66
void EffectsListWidget::slotExpandItem(const QModelIndex & index)
{
    setExpanded(index, !isExpanded(index));
}

67
void EffectsListWidget::initList(QMenu *effectsMenu, KActionCategory *effectActions, QString categoryFile, bool transitionMode)
68
{
69 70 71
    QString current;
    QString currentFolder;
    bool found = false;
72
    effectsMenu->clear();
73

74 75
    if (currentItem()) {
        current = currentItem()->text(0);
Till Theato's avatar
Till Theato committed
76 77 78 79
        if (currentItem()->parent())
            currentFolder = currentItem()->parent()->text(0);
        else if (currentItem()->data(0, TypeRole) ==  EFFECT_FOLDER)
            currentFolder = currentItem()->text(0);
80 81
    }

82 83 84
    QTreeWidgetItem *misc = NULL;
    QTreeWidgetItem *audio = NULL;
    QTreeWidgetItem *custom = NULL;
85
    QList <QTreeWidgetItem *> folders;
86 87 88 89 90 91 92

    if (!categoryFile.isEmpty()) {
        QDomDocument doc;
        QFile file(categoryFile);
        doc.setContent(&file, false);
        file.close();
        QStringList folderNames;
93
        QDomNodeList groups = doc.documentElement().elementsByTagName(QStringLiteral("group"));
94 95 96 97 98 99 100 101 102 103
        for (int i = 0; i < groups.count(); ++i) {
            folderNames << i18n(groups.at(i).firstChild().firstChild().nodeValue().toUtf8().constData());
        }
        for (int i = 0; i < topLevelItemCount(); ++i) {
            topLevelItem(i)->takeChildren();
            QString currentName = topLevelItem(i)->text(0);
            if (currentName != i18n("Misc") && currentName != i18n("Audio") && currentName != i18nc("Folder Name", "Custom") && !folderNames.contains(currentName)) {
                takeTopLevelItem(i);
                --i;
            }
104 105
        }

106 107 108
        for (int i = 0; i < groups.count(); ++i) {
            QTreeWidgetItem *item = findFolder(folderNames.at(i));
            if (item) {
109
                item->setData(0, IdRole, groups.at(i).toElement().attribute(QStringLiteral("list")));
110 111 112
            } else {
                item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(folderNames.at(i)));
                item->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
113
                item->setData(0, IdRole, groups.at(i).toElement().attribute(QStringLiteral("list")));
114 115 116 117 118
                item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
                item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
                insertTopLevelItem(0, item);
            }
            folders.append(item);
119
        }
120

121 122 123 124 125 126 127
        misc = findFolder(i18n("Misc"));
        if (misc == NULL) {
            misc = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Misc")));
            misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
            misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
            insertTopLevelItem(0, misc);
        }
128

129 130 131 132 133 134 135
        audio = findFolder(i18n("Audio"));
        if (audio == NULL) {
            audio = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18n("Audio")));
            audio->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
            audio->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
            insertTopLevelItem(0, audio);
        }
136

137 138 139 140 141 142 143
        custom = findFolder(i18nc("Folder Name", "Custom"));
        if (custom == NULL) {
            custom = new QTreeWidgetItem((QTreeWidget*)0, QStringList(i18nc("Folder Name", "Custom")));
            custom->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
            custom->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
            insertTopLevelItem(0, custom);
        }
144
    }
145

146
    //insertTopLevelItems(0, folders);
147
    if (transitionMode) {
Vincent Pinon's avatar
Vincent Pinon committed
148
        loadEffects(&MainWindow::transitions, misc, &folders, TRANSITION_TYPE, current, &found);
149 150
    }
    else {
Vincent Pinon's avatar
Vincent Pinon committed
151 152 153
        loadEffects(&MainWindow::videoEffects, misc, &folders, EFFECT_VIDEO, current, &found);
        loadEffects(&MainWindow::audioEffects, audio, &folders, EFFECT_AUDIO, current, &found);
        loadEffects(&MainWindow::customEffects, custom, static_cast<QList<QTreeWidgetItem *> *>(0), EFFECT_CUSTOM, current, &found);
154 155 156 157 158 159 160
        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;
                }
161 162
            }
        }
163
    }
Till Theato's avatar
Till Theato committed
164 165
    setSortingEnabled(true);
    sortByColumn(0, Qt::AscendingOrder);
166 167

    // populate effects menu
168 169 170 171
    QMenu *sub1 = NULL;
    QMenu *sub2 = NULL;
    QMenu *sub3 = NULL;
    QMenu *sub4 = NULL;
172
    for (int i = 0; i < topLevelItemCount(); ++i) {
173 174 175 176 177 178 179 180 181 182 183 184
        if (topLevelItem(i)->data(0, TypeRole) == TRANSITION_TYPE) {
            QTreeWidgetItem *item = topLevelItem(i);
            QAction *a = new QAction(item->icon(0), item->text(0), effectsMenu);
            QStringList data = item->data(0, IdRole).toStringList();
            QString id = data.at(1);
            if (id.isEmpty()) id = data.at(0);
            a->setData(data);
            a->setIconVisibleInMenu(false);
            effectsMenu->addAction(a);
            effectActions->addAction("transition_" + id, a);
            continue;
        }
185 186
        if (!topLevelItem(i)->childCount())
            continue;
187 188
        QMenu *sub = new QMenu(topLevelItem(i)->text(0), effectsMenu);
        effectsMenu->addMenu(sub);
189 190 191 192 193 194 195 196 197 198 199 200 201 202
        int effectsInCategory = topLevelItem(i)->childCount();
        bool hasSubCategories = false;
        if (effectsInCategory > 60) {
            // create subcategories if there are too many effects
            hasSubCategories = true;
            sub1 = new QMenu(i18nc("menu name for effects names between these 2 letters", "0 - F"), sub);
            sub->addMenu(sub1);
            sub2 = new QMenu(i18nc("menu name for effects names between these 2 letters", "G - L"), sub);
            sub->addMenu(sub2);
            sub3 = new QMenu(i18nc("menu name for effects names between these 2 letters", "M - R"), sub);
            sub->addMenu(sub3);
            sub4 = new QMenu(i18nc("menu name for effects names between these 2 letters", "S - Z"), sub);
            sub->addMenu(sub4);
        }
203
        for (int j = 0; j < effectsInCategory; ++j) {
Laurent Montel's avatar
Laurent Montel committed
204
            QTreeWidgetItem *item = topLevelItem(i)->child(j);
205
            QAction *a = new QAction(item->icon(0), item->text(0), sub);
Laurent Montel's avatar
Laurent Montel committed
206 207 208 209 210 211 212 213 214 215 216
            QStringList data = item->data(0, IdRole).toStringList();
            QString id = data.at(1);
            if (id.isEmpty()) id = data.at(0);
            a->setData(data);
            a->setIconVisibleInMenu(false);
            if (hasSubCategories) {
                // put action in sub category
                QRegExp rx("^[s-z].+");
                if (rx.exactMatch(item->text(0).toLower())) {
                    sub4->addAction(a);
                } else {
217
                    rx.setPattern(QStringLiteral("^[m-r].+"));
218
                    if (rx.exactMatch(item->text(0).toLower())) {
Laurent Montel's avatar
Laurent Montel committed
219 220 221
                        sub3->addAction(a);
                    }
                    else {
222
                        rx.setPattern(QStringLiteral("^[g-l].+"));
223
                        if (rx.exactMatch(item->text(0).toLower())) {
Laurent Montel's avatar
Laurent Montel committed
224
                            sub2->addAction(a);
225
                        }
Laurent Montel's avatar
Laurent Montel committed
226
                        else sub1->addAction(a);
227 228
                    }
                }
Laurent Montel's avatar
Laurent Montel committed
229 230 231
            }
            else sub->addAction(a);
            effectActions->addAction("video_effect_" + id, a);
232 233
        }
    }
Till Theato's avatar
Till Theato committed
234 235
}

Vincent Pinon's avatar
Vincent Pinon committed
236
void EffectsListWidget::loadEffects(const EffectsList *effectlist, QTreeWidgetItem *defaultFolder, const QList<QTreeWidgetItem *> *folders, int type, const QString &current, bool *found)
Till Theato's avatar
Till Theato committed
237 238 239 240
{
    QStringList effectInfo, l;
    QTreeWidgetItem *item;
    int ct = effectlist->count();
241 242
    QFontMetrics f(font());
    int fontSize = f.height();
243

Laurent Montel's avatar
Laurent Montel committed
244
    for (int ix = 0; ix < ct; ++ix) {
245 246
        const QDomElement effect = effectlist->at(ix);
        effectInfo = effectlist->effectInfo(effect);
247
        if (effectInfo.isEmpty()) continue;
248
        QTreeWidgetItem *parentItem = NULL;
Till Theato's avatar
Till Theato committed
249

Alberto Villa's avatar
Alberto Villa committed
250
        if (folders) {
251
            for (int i = 0; i < folders->count(); ++i) {
Laurent Montel's avatar
Laurent Montel committed
252
                l = folders->at(i)->data(0, IdRole).toString().split(QLatin1Char(','), QString::SkipEmptyParts);
Alberto Villa's avatar
Alberto Villa committed
253 254 255 256
                if (l.contains(effectInfo.at(2))) {
                    parentItem = folders->at(i);
                    break;
                }
257 258
            }
        }
Till Theato's avatar
Till Theato committed
259 260
        if (parentItem == NULL)
            parentItem = defaultFolder;
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        QIcon icon2 = generateIcon(fontSize, effectInfo.at(0), effect);
        item = new QTreeWidgetItem(parentItem, QStringList(effectInfo.takeFirst()));
        QString tag = effectInfo.at(0);
        if (type != EFFECT_CUSTOM && tag.startsWith(QLatin1String("movit."))) {
            // GPU effect
            effectInfo.append(QString::number(EFFECT_GPU));
            item->setData(0, TypeRole, EFFECT_GPU);
        } else {
            effectInfo.append(QString::number(type));
            item->setData(0, TypeRole, type);
        }
        if (effectInfo.count() == 4) item->setIcon(0, QIcon::fromTheme(QStringLiteral("folder")));
        else item->setIcon(0, icon2);
        item->setData(0, IdRole, effectInfo);
        item->setToolTip(0, effectlist->getEffectInfo(effect));
        if (parentItem == NULL) {
            addTopLevelItem(item);
        }
        if (item->text(0) == current) {
            setCurrentItem(item);
            *found = true;
283
        }
284
    }
285 286
}

Laurent Montel's avatar
Laurent Montel committed
287
QTreeWidgetItem *EffectsListWidget::findFolder(const QString &name)
288 289 290 291
{
    QTreeWidgetItem *item = NULL;
    QList<QTreeWidgetItem *> result = findItems(name, Qt::MatchExactly);
    if (!result.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
292
        for (int j = 0; j < result.count(); ++j) {
293
            if (result.at(j)->data(0, TypeRole) == EFFECT_FOLDER) {
294 295 296 297 298 299 300 301
                item = result.at(j);
                break;
            }
        }
    }
    return item;
}

302
const QDomElement EffectsListWidget::currentEffect() const
303
{
304 305 306 307 308
    QTreeWidgetItem *item = currentItem();
    if (!item) return QDomElement();
    int type = item->data(0, TypeRole).toInt();
    QStringList info = item->data(0, IdRole).toStringList();
    return itemEffect(type, info);
309 310
}

Vincent Pinon's avatar
Vincent Pinon committed
311
QIcon EffectsListWidget::generateIcon(int size, const QString &name, QDomElement info)
312 313 314 315 316 317 318 319 320 321 322
{
    QPixmap pix(size, size);
    if (name.isEmpty()) {
        pix.fill(Qt::red);
        return QIcon(pix);
    }
    QFont ft = font();
    ft.setBold(true);
    uint hex = qHash(name);
    QString t = "#" + QString::number(hex, 16).toUpper().left(6);
    QColor col(t);
323 324
    info.setAttribute(QStringLiteral("effectcolor"), col.name());
    bool isAudio = info.attribute(QStringLiteral("type")) == QLatin1String("audio");
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    if (isAudio) {
        pix.fill(Qt::transparent);
    }
    else {
        pix.fill(col);
    }
    QPainter p(&pix);
    if (isAudio) {
        p.setPen(Qt::NoPen);
        p.setBrush(col);
        p.drawEllipse(pix.rect());
        p.setPen(QPen());
    }
    p.setFont(ft);
    p.drawText(pix.rect(), Qt::AlignCenter, name.at(0));
    p.end();
    return QIcon(pix);
}

344
const QDomElement EffectsListWidget::itemEffect(int type, QStringList effectInfo)
345
{
346
    QDomElement effect;
347 348
    if (type == (int)EFFECT_FOLDER) return effect;
    switch (type) {
349 350
    case EFFECT_VIDEO:
    case EFFECT_GPU:
351
        effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
352
        break;
353
    case EFFECT_AUDIO:
354
        effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
355
        break;
356 357 358
    case EFFECT_CUSTOM:
        effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
        break;
359 360 361
    case TRANSITION_TYPE:
        effect = MainWindow::transitions.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
        break;
362
    default:
363 364 365 366
        effect =  MainWindow::videoEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
        if (!effect.isNull()) break;
        effect = MainWindow::audioEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
        if (!effect.isNull()) break;
367
        effect = MainWindow::customEffects.getEffectByTag(effectInfo.at(0), effectInfo.at(1)).cloneNode().toElement();
368 369 370
        break;
    }
    return effect;
371 372 373
}


Laurent Montel's avatar
Laurent Montel committed
374
QString EffectsListWidget::currentInfo() const
375
{
376 377
    QTreeWidgetItem *item = currentItem();
    if (!item || item->data(0, TypeRole).toInt() == (int)EFFECT_FOLDER) return QString();
378
    QString info;
379 380
    QStringList effectInfo = item->data(0, IdRole).toStringList();
    switch (item->data(0, TypeRole).toInt()) {
381 382
    case EFFECT_VIDEO:
    case EFFECT_GPU:
383
        info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
384
        break;
385
    case EFFECT_AUDIO:
386
        info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
387
        break;
388 389 390
    case EFFECT_CUSTOM:
        info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
        break;
391 392 393
    case TRANSITION_TYPE:
        info = MainWindow::transitions.getInfo(effectInfo.at(0), effectInfo.at(1));
        break;
394
    default:
395 396 397 398
        info = MainWindow::videoEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
        if (!info.isEmpty()) break;
        info = MainWindow::audioEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
        if (!info.isEmpty()) break;
399
        info = MainWindow::customEffects.getInfo(effectInfo.at(0), effectInfo.at(1));
400 401 402
        break;
    }
    return info;
403 404
}

405 406 407 408
//virtual
void EffectsListWidget::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
Laurent Montel's avatar
Laurent Montel committed
409 410 411
        emit applyEffect(currentEffect());
        e->accept();
        return;
412 413 414 415
    }
    QTreeWidget::keyPressEvent(e);
}

416 417
//virtual
QMimeData * EffectsListWidget::mimeData(const QList<QTreeWidgetItem *> list) const
418
{
419
    QDomDocument doc;
420
    bool transitionMode = false;
421 422
    foreach(QTreeWidgetItem *item, list) {
        if (item->flags() & Qt::ItemIsDragEnabled) {
423
            int type = item->data(0, TypeRole).toInt();
424 425 426
            if (type == TRANSITION_TYPE) {
                transitionMode = true;
            }
427 428
            QStringList info = item->data(0, IdRole).toStringList();
            const QDomElement e = itemEffect(type, info);
Laurent Montel's avatar
Laurent Montel committed
429 430
            if (!e.isNull())
                doc.appendChild(doc.importNode(e, true));
431
        }
432
    }
433 434 435
    QMimeData *mime = new QMimeData;
    QByteArray data;
    data.append(doc.toString().toUtf8());
436
    mime->setData(transitionMode ? "kdenlive/transitionslist" : "kdenlive/effectslist", data);
437
    return mime;
438 439
}

440 441
//virtual
void EffectsListWidget::dragMoveEvent(QDragMoveEvent *event)
442
{
443
    if (event->mimeData()->hasFormat(QStringLiteral("kdenlive/effectslist"))) {
444
        event->acceptProposedAction();
445 446
    } else {
        event->ignore();
447
    }
448 449
}

450

451
//virtual
452 453
void EffectsListWidget::contextMenuEvent(QContextMenuEvent * event)
{
454
    QTreeWidgetItem *item = itemAt(event->pos());
455 456 457
    if (item && item->data(0, TypeRole) !=  EFFECT_FOLDER) {
        emit displayMenu(item, event->globalPos());
    }
458
}
459

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
void EffectsListWidget::setRootOnCustomFolder()
{
    // special case, display only items in "custom" folder"
    QTreeWidgetItem *item = findFolder(i18nc("Folder Name", "Custom"));
    if (!item) {
        // No custom effect, show empty list
        for (int i = 0; i < topLevelItemCount(); ++i) {
            QTreeWidgetItem *folder = topLevelItem(i);
            folder->setHidden(true);
        }
        return;
    }
    setRootIndex(indexFromItem(item));
    for (int j = 0; j < item->childCount(); ++j) {
        QTreeWidgetItem *child = item->child(j);
        child->setHidden(false);
    }
}

void EffectsListWidget::resetRoot()
{
    setRootIndex(indexFromItem(invisibleRootItem()));
}
483
void EffectsListWidget::createTopLevelItems(QList <QTreeWidgetItem *>list, int effectType)
484
{
485
    // Favorites is a pseudo-folder used to store items, not visible to end user, so don't i18n its name
486
    QTreeWidgetItem *misc = findFolder(QStringLiteral("TemporaryFolder"));
487
    if (misc == NULL) {
488
        misc = new QTreeWidgetItem(this, QStringList(QStringLiteral("TemporaryFolder")));
489 490 491 492
        misc->setData(0, TypeRole, QString::number((int) EFFECT_FOLDER));
        misc->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    }
    else qDeleteAll(misc->takeChildren());
493
    setIndentation(0);
494 495 496 497
    misc->addChildren(list);
    for (int j = 0; j < misc->childCount(); ++j) {
        QTreeWidgetItem *child = misc->child(j);
        child->setHidden(false);
498
        child->setData(0, Qt::UserRole, effectType);
499
    }
500
    setRootIndex(indexFromItem(misc));
501 502 503 504
}

void EffectsListWidget::resetFavorites()
{
505
    QTreeWidgetItem *misc = findFolder(QStringLiteral("TemporaryFolder"));
506 507 508 509 510
    if (misc) {
        setRootIndex(indexFromItem(invisibleRootItem()));
        delete misc;
    }
}
511