]> git.sesse.net Git - kdenlive/blob - src/effectslistview.cpp
- Fix auto expand behaviour when searching the effectlist (1)
[kdenlive] / src / effectslistview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "effectslistview.h"
22 #include "effectslistwidget.h"
23 #include "effectslist.h"
24 #include "kdenlivesettings.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KStandardDirs>
29
30 #include <QMenu>
31 #include <QDir>
32
33 EffectsListView::EffectsListView(QWidget *parent) :
34         QWidget(parent)
35 {
36     setupUi(this);
37
38     QMenu *menu = new QMenu(this);
39     m_effectsList = new EffectsListWidget(menu);
40     QVBoxLayout *lyr = new QVBoxLayout(effectlistframe);
41     lyr->addWidget(m_effectsList);
42     lyr->setContentsMargins(0, 0, 0, 0);
43     search_effect->setTreeWidget(m_effectsList);
44     buttonInfo->setIcon(KIcon("help-about"));
45     setFocusPolicy(Qt::StrongFocus);
46     setFocusProxy(search_effect);
47     m_effectsList->setFocusProxy(search_effect);
48
49     if (KdenliveSettings::showeffectinfo()) {
50         buttonInfo->setDown(true);
51     } else infopanel->hide();
52     menu->addAction(KIcon("edit-delete"), i18n("Delete effect"), this, SLOT(slotRemoveEffect()));
53
54     connect(type_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterList(int)));
55     connect(buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
56     connect(m_effectsList, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateInfo()));
57     connect(m_effectsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEffectSelected()));
58     connect(search_effect, SIGNAL(hiddenChanged(QTreeWidgetItem *, bool)), this, SLOT(slotUpdateSearch(QTreeWidgetItem *, bool)));
59     connect(search_effect, SIGNAL(textChanged(QString)), this, SLOT(slotAutoExpand(QString)));
60     //m_effectsList->setCurrentRow(0);
61 }
62
63 void EffectsListView::filterList(int pos)
64 {
65     for (int i = 0; i < m_effectsList->topLevelItemCount(); i++) {
66         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
67         bool hideFolder = true;
68         for (int j = 0; j < folder->childCount(); j++) {
69             QTreeWidgetItem *item = folder->child(j);
70             if (pos == 0 || pos == item->data(0, Qt::UserRole).toInt()) {
71                 item->setHidden(false);
72                 hideFolder = false;
73             } else {
74                 item->setHidden(true);
75             }
76         }
77         // do not hide the folder if it's empty but "All" is selected
78         if (pos == 0)
79             hideFolder = false;
80         folder->setHidden(hideFolder);
81     }
82     // make sure we don't show anything not matching the search expression
83     search_effect->updateSearch();
84
85
86     /*item = m_effectsList->currentItem();
87     if (item) {
88         if (item->isHidden()) {
89             int i;
90             for (i = 0; i < m_effectsList->count() && m_effectsList->item(i)->isHidden(); i++); //do nothing
91             m_effectsList->setCurrentRow(i);
92         } else m_effectsList->scrollToItem(item);
93     }*/
94 }
95
96 void EffectsListView::showInfoPanel()
97 {
98     bool show = !infopanel->isVisible();
99     infopanel->setVisible(show);
100     buttonInfo->setDown(show);
101     KdenliveSettings::setShoweffectinfo(show);
102 }
103
104 void EffectsListView::slotEffectSelected()
105 {
106     QDomElement effect = m_effectsList->currentEffect();
107     if (!effect.isNull()) emit addEffect(effect);
108 }
109
110 void EffectsListView::slotUpdateInfo()
111 {
112     infopanel->setText(m_effectsList->currentInfo());
113 }
114
115 void EffectsListView::reloadEffectList()
116 {
117     m_effectsList->initList();
118 }
119
120 void EffectsListView::slotRemoveEffect()
121 {
122     QTreeWidgetItem *item = m_effectsList->currentItem();
123     QString effectId = item->text(0);
124     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
125
126     QDir directory = QDir(path);
127     QStringList filter;
128     filter << "*.xml";
129     const QStringList fileList = directory.entryList(filter, QDir::Files);
130     QString itemName;
131     foreach(const QString &filename, fileList) {
132         itemName = KUrl(path + filename).path();
133         QDomDocument doc;
134         QFile file(itemName);
135         doc.setContent(&file, false);
136         file.close();
137         QDomNodeList effects = doc.elementsByTagName("effect");
138         if (effects.count() != 1) {
139             kDebug() << "More than one effect in file " << itemName << ", NOT SUPPORTED YET";
140         } else {
141             QDomElement e = effects.item(0).toElement();
142             if (e.attribute("id") == effectId) {
143                 QFile::remove(itemName);
144                 break;
145             }
146         }
147     }
148     emit reloadEffects();
149 }
150
151 void EffectsListView::slotUpdateSearch(QTreeWidgetItem *item, bool hidden)
152 {
153     if (!hidden) {
154         if (item->data(0, Qt::UserRole).toInt() == type_combo->currentIndex()) {
155             if (item->parent())
156                 item->parent()->setHidden(false);
157         } else {
158             if (type_combo->currentIndex() != 0)
159                 item->setHidden(true);
160         }
161     }
162 }
163
164 void EffectsListView::slotAutoExpand(QString text)
165 {
166     search_effect->updateSearch();
167
168     for (int i = 0; i < m_effectsList->topLevelItemCount(); ++i) {
169         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
170         bool expandFolder = false;
171         /*if (folder->isHidden())
172             continue;*/
173         if (!text.isEmpty()) {
174             for (int j = 0; j < folder->childCount(); j++) {
175                 QTreeWidgetItem *item = folder->child(j);
176                 if (!item->isHidden())
177                     expandFolder = true;
178             }
179         }
180         folder->setExpanded(expandFolder);
181     }
182 }
183
184 #include "effectslistview.moc"