]> git.sesse.net Git - kdenlive/blob - src/tracksconfigdialog.cpp
Cleaning code style of Definitions.
[kdenlive] / src / tracksconfigdialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
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 #include "tracksconfigdialog.h"
21 #include "kdenlivedoc.h"
22
23 #include <QTableWidget>
24 #include <QComboBox>
25 #include <KDebug>
26 #include <KIcon>
27
28 TracksDelegate::TracksDelegate(QObject *parent) :
29         QItemDelegate(parent)
30 {
31 }
32
33 QWidget *TracksDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex & /*index*/) const
34 {
35     QComboBox *comboBox = new QComboBox(parent);
36     comboBox->addItem(i18n("Video"));
37     comboBox->addItem(i18n("Audio"));
38     connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
39     return comboBox;
40 }
41
42 void TracksDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
43 {
44     QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
45     if (!comboBox)
46         return;
47     const int pos = comboBox->findText(index.model()->data(index).toString(), Qt::MatchExactly);
48     comboBox->setCurrentIndex(pos);
49 }
50
51 void TracksDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
52 {
53     QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
54     if (!comboBox)
55         return;
56     model->setData(index, comboBox->currentText());
57 }
58
59 void TracksDelegate::emitCommitData()
60 {
61     emit commitData(qobject_cast<QWidget *>(sender()));
62 }
63
64
65 TracksConfigDialog::TracksConfigDialog(KdenliveDoc * doc, int selected, QWidget* parent) :
66         QDialog(parent),
67         m_doc(doc)
68 {
69     setupUi(this);
70
71     table->setColumnCount(5);
72     table->setHorizontalHeaderLabels(QStringList() << i18n("Name") << i18n("Type") << i18n("Hidden") << i18n("Muted") << i18n("Locked"));
73     table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
74     table->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
75     table->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
76     table->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);
77     table->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);
78
79     table->setItemDelegateForColumn(1, new TracksDelegate(this));
80
81     buttonReset->setIcon(KIcon("document-revert"));
82     buttonReset->setToolTip(i18n("Reset"));
83     connect(buttonReset, SIGNAL(clicked()), this, SLOT(setupOriginal()));
84
85     buttonAdd->setIcon(KIcon("list-add"));
86     buttonAdd->setToolTip(i18n("Add Track"));
87     buttonAdd->setEnabled(false);
88
89     buttonDelete->setIcon(KIcon("list-remove"));
90     buttonDelete->setToolTip(i18n("Delete Track"));
91     connect(buttonDelete, SIGNAL(clicked()), this, SLOT(slotDelete()));
92
93     buttonUp->setIcon(KIcon("arrow-up"));
94     buttonUp->setToolTip(i18n("Move Track upwards"));
95     buttonUp->setEnabled(false);
96
97     buttonDown->setIcon(KIcon("arrow-down"));
98     buttonDown->setToolTip(i18n("Move Track downwards"));
99     buttonDown->setEnabled(false);
100
101     setupOriginal(selected);
102     connect(table, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(slotUpdateRow(QTableWidgetItem*)));
103 }
104
105 const QList <TrackInfo> TracksConfigDialog::tracksList()
106 {
107     QList <TrackInfo> tracks;
108     TrackInfo info;
109     for (int i = table->rowCount() - 1; i >= 0; --i) {
110         info.trackName = table->item(i, 0)->text();
111         QTableWidgetItem *item = table->item(i, 1);
112         if (item->text() == i18n("Audio")) {
113             info.type = AudioTrack;
114             info.isBlind = true;
115         } else {
116             info.type = VideoTrack;
117             info.isBlind = (table->item(i, 2)->checkState() == Qt::Checked);
118         }
119         info.isMute = (table->item(i, 3)->checkState() == Qt::Checked);
120         info.isLocked = (table->item(i, 4)->checkState() == Qt::Checked);
121         tracks << info;
122     }
123     return tracks;
124 }
125
126 QList <int> TracksConfigDialog::deletedTracks() const
127 {
128     return m_deletedRows;
129 }
130
131 void TracksConfigDialog::setupOriginal(int selected)
132 {
133     table->setRowCount(m_doc->tracksCount());
134
135     QStringList numbers;
136     TrackInfo info;
137     for (int i = m_doc->tracksCount() - 1; i >= 0; --i) {
138         numbers << QString::number(i);
139         info = m_doc->trackInfoAt(m_doc->tracksCount() - i - 1);
140         table->setItem(i, 0, new QTableWidgetItem(info.trackName));
141
142         QTableWidgetItem *item1 = new QTableWidgetItem(i18n("Video"));
143         if (info.type == AudioTrack)
144             item1->setText(i18n("Audio"));
145         table->setItem(i, 1, item1);
146         table->openPersistentEditor(item1);
147
148         QTableWidgetItem *item2 = new QTableWidgetItem("");
149         item2->setFlags(item2->flags() & ~Qt::ItemIsEditable);
150         item2->setCheckState(info.isBlind ? Qt::Checked : Qt::Unchecked);
151         if (info.type == AudioTrack)
152             item2->setFlags(item2->flags() & ~Qt::ItemIsEnabled);
153         table->setItem(i, 2, item2);
154
155         QTableWidgetItem *item3 = new QTableWidgetItem("");
156         item3->setFlags(item3->flags() & ~Qt::ItemIsEditable);
157         item3->setCheckState(info.isMute ? Qt::Checked : Qt::Unchecked);
158         table->setItem(i, 3, item3);
159
160         QTableWidgetItem *item4 = new QTableWidgetItem("");
161         item4->setFlags(item4->flags() & ~Qt::ItemIsEditable);
162         item4->setCheckState(info.isLocked ? Qt::Checked : Qt::Unchecked);
163         table->setItem(i, 4, item4);
164     }
165     table->setVerticalHeaderLabels(numbers);
166
167     table->resizeColumnsToContents();
168     if (selected != -1)
169         table->selectRow(selected);
170
171     m_deletedRows = QList<int>();
172 }
173
174 void TracksConfigDialog::slotUpdateRow(QTableWidgetItem* item)
175 {
176     if (table->column(item) == 1) {
177         QTableWidgetItem *item2 = table->item(table->row(item), 2);
178         if (item->text() == i18n("Audio")) {
179             item2->setFlags(item2->flags() & ~Qt::ItemIsEnabled);
180             item2->setCheckState(Qt::Checked);
181         } else {
182             item2->setFlags(item2->flags() | Qt::ItemIsEnabled);
183             item2->setCheckState(Qt::Unchecked);
184         }
185     }
186 }
187
188 void TracksConfigDialog::slotDelete()
189 {
190     int row = table->currentRow();
191     int i = 0;
192     while (i < m_deletedRows.count()) {
193         if (m_deletedRows.at(i) == row)
194             return;
195         if (m_deletedRows.at(i) > row)
196             break;
197         ++i;
198     }
199     m_deletedRows.insert(i, row);
200     for (i = 0; i < table->columnCount(); ++i) {
201         QTableWidgetItem *item = table->item(row, i);
202         item->setFlags(Qt::NoItemFlags);
203         item->setBackground(palette().dark());
204     }
205 }
206
207 #include "tracksconfigdialog.moc"