]> git.sesse.net Git - kdenlive/blob - src/effectslistview.cpp
includemocs
[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
34 EffectsListView::EffectsListView(QWidget *parent) :
35         QWidget(parent)
36 {
37     setupUi(this);
38     QString styleSheet = "QTreeView::branch:has-siblings:!adjoins-item{border-image:none;border:0px} \
39     QTreeView::branch:has-siblings:adjoins-item {border-image: none;border:0px}      \
40     QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image: none;border:0px} \
41     QTreeView::branch:has-children:!has-siblings:closed,QTreeView::branch:closed:has-children:has-siblings {   \
42          border-image: none;image: url(:/images/stylesheet-branch-closed.png);}      \
43     QTreeView::branch:open:has-children:!has-siblings,QTreeView::branch:open:has-children:has-siblings  {    \
44          border-image: none;image: url(:/images/stylesheet-branch-open.png);}";
45
46     QMenu *contextMenu = new QMenu(this);
47     m_effectsList = new EffectsListWidget(contextMenu);
48     m_effectsList->setStyleSheet(styleSheet);
49     QVBoxLayout *lyr = new QVBoxLayout(effectlistframe);
50     lyr->addWidget(m_effectsList);
51     lyr->setContentsMargins(0, 0, 0, 0);
52     search_effect->setTreeWidget(m_effectsList);
53     search_effect->setToolTip(i18n("Search in the effect list"));
54     
55     int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
56     QSize iconSize(size, size);
57     buttonInfo->setIcon(KIcon("help-about"));
58     buttonInfo->setToolTip(i18n("Show/Hide the effect description"));
59     buttonInfo->setIconSize(iconSize);
60     setFocusPolicy(Qt::StrongFocus);
61     setFocusProxy(search_effect);
62     m_effectsList->setFocusProxy(search_effect);
63
64     if (KdenliveSettings::showeffectinfo())
65         buttonInfo->setDown(true);
66     else
67         infopanel->hide();
68
69     contextMenu->addAction(KIcon("edit-delete"), i18n("Delete effect"), this, SLOT(slotRemoveEffect()));
70
71     connect(type_combo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterList(int)));
72     connect(buttonInfo, SIGNAL(clicked()), this, SLOT(showInfoPanel()));
73     connect(m_effectsList, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateInfo()));
74     connect(m_effectsList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotEffectSelected()));
75     connect(search_effect, SIGNAL(hiddenChanged(QTreeWidgetItem*,bool)), this, SLOT(slotUpdateSearch(QTreeWidgetItem*,bool)));
76     connect(m_effectsList, SIGNAL(applyEffect(QDomElement)), this, SIGNAL(addEffect(QDomElement)));
77     connect(search_effect, SIGNAL(textChanged(QString)), this, SLOT(slotAutoExpand(QString)));
78     //m_effectsList->setCurrentRow(0);
79 }
80
81 void EffectsListView::filterList(int pos)
82 {
83     for (int i = 0; i < m_effectsList->topLevelItemCount(); ++i) {
84         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
85         bool hideFolder = true;
86         for (int j = 0; j < folder->childCount(); j++) {
87             QTreeWidgetItem *item = folder->child(j);
88             if (pos == 0 || pos == item->data(0, Qt::UserRole).toInt()) {
89                 item->setHidden(false);
90                 hideFolder = false;
91             } else {
92                 item->setHidden(true);
93             }
94         }
95         // do not hide the folder if it's empty but "All" is selected
96         if (pos == 0)
97             hideFolder = false;
98         folder->setHidden(hideFolder);
99     }
100     // make sure we don't show anything not matching the search expression
101     search_effect->updateSearch();
102
103
104     /*item = m_effectsList->currentItem();
105     if (item) {
106         if (item->isHidden()) {
107             int i;
108             for (i = 0; i < m_effectsList->count() && m_effectsList->item(i)->isHidden(); ++i); //do nothing
109             m_effectsList->setCurrentRow(i);
110         } else m_effectsList->scrollToItem(item);
111     }*/
112 }
113
114 void EffectsListView::showInfoPanel()
115 {
116     bool show = !infopanel->isVisible();
117     infopanel->setVisible(show);
118     buttonInfo->setDown(show);
119     KdenliveSettings::setShoweffectinfo(show);
120 }
121
122 void EffectsListView::slotEffectSelected()
123 {
124     QDomElement effect = m_effectsList->currentEffect();
125     QTreeWidgetItem* item=m_effectsList->currentItem();
126     if (item &&  m_effectsList->indexOfTopLevelItem(item)!=-1){
127         item->setExpanded(!item->isExpanded());         
128     }
129     if (!effect.isNull())
130         emit addEffect(effect);
131 }
132
133 void EffectsListView::slotUpdateInfo()
134 {
135     infopanel->setText(m_effectsList->currentInfo());
136 }
137
138 void EffectsListView::reloadEffectList(QMenu *effectsMenu, KActionCategory *effectActions)
139 {
140     m_effectsList->initList(effectsMenu, effectActions);
141 }
142
143 void EffectsListView::slotRemoveEffect()
144 {
145     QTreeWidgetItem *item = m_effectsList->currentItem();
146     QString effectId = item->text(0);
147     QString path = KStandardDirs::locateLocal("appdata", "effects/", true);
148
149     QDir directory = QDir(path);
150     QStringList filter;
151     filter << "*.xml";
152     const QStringList fileList = directory.entryList(filter, QDir::Files);
153     QString itemName;
154     foreach(const QString &filename, fileList) {
155         itemName = KUrl(path + filename).path();
156         QDomDocument doc;
157         QFile file(itemName);
158         doc.setContent(&file, false);
159         file.close();
160         QDomNodeList effects = doc.elementsByTagName("effect");
161         if (effects.count() != 1) {
162             kDebug() << "More than one effect in file " << itemName << ", NOT SUPPORTED YET";
163         } else {
164             QDomElement e = effects.item(0).toElement();
165             if (e.attribute("id") == effectId) {
166                 QFile::remove(itemName);
167                 break;
168             }
169         }
170     }
171     emit reloadEffects();
172 }
173
174 void EffectsListView::slotUpdateSearch(QTreeWidgetItem *item, bool hidden)
175 {
176     if (!hidden) {
177         if (item->data(0, Qt::UserRole).toInt() == type_combo->currentIndex()) {
178             if (item->parent())
179                 item->parent()->setHidden(false);
180         } else {
181             if (type_combo->currentIndex() != 0)
182                 item->setHidden(true);
183         }
184     }
185 }
186
187 void EffectsListView::slotAutoExpand(const QString &text)
188 {
189     search_effect->updateSearch();
190     bool selected = false;
191     for (int i = 0; i < m_effectsList->topLevelItemCount(); ++i) {
192         QTreeWidgetItem *folder = m_effectsList->topLevelItem(i);
193         bool expandFolder = false;
194         /*if (folder->isHidden())
195             continue;*/
196         if (!text.isEmpty()) {
197             for (int j = 0; j < folder->childCount(); j++) {
198                 QTreeWidgetItem *item = folder->child(j);
199                 if (!item->isHidden()) {
200                     expandFolder = true;
201                     if (!selected) {
202                         m_effectsList->setCurrentItem(item);
203                         selected = true;
204                     }
205                 }
206             }
207         }
208         folder->setExpanded(expandFolder);
209     }
210     if (!selected) m_effectsList->setCurrentItem(NULL);
211 }
212
213 void EffectsListView::updatePalette()
214 {
215     m_effectsList->setStyleSheet(m_effectsList->styleSheet());
216 }
217
218 #include "effectslistview.moc"