]> git.sesse.net Git - kdenlive/blob - src/effectslistview.cpp
expand folders in effectlist on doubleclick
[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     search_effect->setToolTip(i18n("Search in the effect list"));
45     buttonInfo->setIcon(KIcon("help-about"));
46     buttonInfo->setToolTip(i18n("Show/Hide the effect description"));
47     setFocusPolicy(Qt::StrongFocus);
48     setFocusProxy(search_effect);
49     m_effectsList->setFocusProxy(search_effect);
50
51     if (KdenliveSettings::showeffectinfo())
52         buttonInfo->setDown(true);
53     else
54         infopanel->hide();
55
56     menu->addAction(KIcon("edit-delete"), i18n("Delete effect"), this, SLOT(slotRemoveEffect()));
57
58     connect(type_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterList(int)));
59     connect(buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
60     connect(m_effectsList, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateInfo()));
61     connect(m_effectsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotEffectSelected()));
62     connect(search_effect, SIGNAL(hiddenChanged(QTreeWidgetItem *, bool)), this, SLOT(slotUpdateSearch(QTreeWidgetItem *, bool)));
63     connect(search_effect, SIGNAL(textChanged(QString)), this, SLOT(slotAutoExpand(QString)));
64     //m_effectsList->setCurrentRow(0);
65 }
66
67 void EffectsListView::filterList(int pos)
68 {
69     for (int i = 0; i < m_effectsList->topLevelItemCount(); i++) {
70         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
71         bool hideFolder = true;
72         for (int j = 0; j < folder->childCount(); j++) {
73             QTreeWidgetItem *item = folder->child(j);
74             if (pos == 0 || pos == item->data(0, Qt::UserRole).toInt()) {
75                 item->setHidden(false);
76                 hideFolder = false;
77             } else {
78                 item->setHidden(true);
79             }
80         }
81         // do not hide the folder if it's empty but "All" is selected
82         if (pos == 0)
83             hideFolder = false;
84         folder->setHidden(hideFolder);
85     }
86     // make sure we don't show anything not matching the search expression
87     search_effect->updateSearch();
88
89
90     /*item = m_effectsList->currentItem();
91     if (item) {
92         if (item->isHidden()) {
93             int i;
94             for (i = 0; i < m_effectsList->count() && m_effectsList->item(i)->isHidden(); i++); //do nothing
95             m_effectsList->setCurrentRow(i);
96         } else m_effectsList->scrollToItem(item);
97     }*/
98 }
99
100 void EffectsListView::showInfoPanel()
101 {
102     bool show = !infopanel->isVisible();
103     infopanel->setVisible(show);
104     buttonInfo->setDown(show);
105     KdenliveSettings::setShoweffectinfo(show);
106 }
107
108 void EffectsListView::slotEffectSelected()
109 {
110     QDomElement effect = m_effectsList->currentEffect();
111         QTreeWidgetItem* item=m_effectsList->currentItem();
112         if (item &&  item->data(0, Qt::UserRole).toInt() == 4){
113                 item->setExpanded(!item->isExpanded());         
114         }
115     if (!effect.isNull())
116         emit addEffect(effect);
117 }
118
119 void EffectsListView::slotUpdateInfo()
120 {
121     infopanel->setText(m_effectsList->currentInfo());
122 }
123
124 void EffectsListView::reloadEffectList()
125 {
126     m_effectsList->initList();
127 }
128
129 void EffectsListView::slotRemoveEffect()
130 {
131     QTreeWidgetItem *item = m_effectsList->currentItem();
132     QString effectId = item->text(0);
133     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
134
135     QDir directory = QDir(path);
136     QStringList filter;
137     filter << "*.xml";
138     const QStringList fileList = directory.entryList(filter, QDir::Files);
139     QString itemName;
140     foreach(const QString &filename, fileList) {
141         itemName = KUrl(path + filename).path();
142         QDomDocument doc;
143         QFile file(itemName);
144         doc.setContent(&file, false);
145         file.close();
146         QDomNodeList effects = doc.elementsByTagName("effect");
147         if (effects.count() != 1) {
148             kDebug() << "More than one effect in file " << itemName << ", NOT SUPPORTED YET";
149         } else {
150             QDomElement e = effects.item(0).toElement();
151             if (e.attribute("id") == effectId) {
152                 QFile::remove(itemName);
153                 break;
154             }
155         }
156     }
157     emit reloadEffects();
158 }
159
160 void EffectsListView::slotUpdateSearch(QTreeWidgetItem *item, bool hidden)
161 {
162     if (!hidden) {
163         if (item->data(0, Qt::UserRole).toInt() == type_combo->currentIndex()) {
164             if (item->parent())
165                 item->parent()->setHidden(false);
166         } else {
167             if (type_combo->currentIndex() != 0)
168                 item->setHidden(true);
169         }
170     }
171 }
172
173 void EffectsListView::slotAutoExpand(QString text)
174 {
175     search_effect->updateSearch();
176
177     for (int i = 0; i < m_effectsList->topLevelItemCount(); ++i) {
178         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
179         bool expandFolder = false;
180         /*if (folder->isHidden())
181             continue;*/
182         if (!text.isEmpty()) {
183             for (int j = 0; j < folder->childCount(); j++) {
184                 QTreeWidgetItem *item = folder->child(j);
185                 if (!item->isHidden())
186                     expandFolder = true;
187             }
188         }
189         folder->setExpanded(expandFolder);
190     }
191 }
192
193 #include "effectslistview.moc"