Add icon and menu entry
[dashstudio.git] / src / components / timeline / layermanager.cpp
blob237c12d80fa97d285f16443c98d9adfd0d1d5634
1 /***************************************************************************
2 * Copyright (C) 2005 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "layermanager.h"
23 #include <QLabel>
24 #include <QLayout>
26 #include <QPixmap>
27 #include <QHeaderView>
28 #include <QPainter>
29 #include <QItemDelegate>
30 #include <QLineEdit>
32 #include <dgui/separator.h>
33 #include <dgui/application.h>
34 #include <dgui/iconloader.h>
35 #include <dcore/debug.h>
37 namespace Dash {
38 namespace Component {
40 const int LAYER_COLUMN = 0;
41 const int LOCK_COLUMN = 1;
42 const int VIEW_COLUMN = 2;
44 // Header
46 class LayerManagerHeader : public QHeaderView
48 public:
49 LayerManagerHeader(QWidget * parent = 0);
50 ~LayerManagerHeader();
52 void paintSection(QPainter *painter, const QRect & rect, int logicalIndex ) const;
54 private:
55 QIcon m_lockIcon;
56 QIcon m_viewIcon;
59 LayerManagerHeader::LayerManagerHeader(QWidget * parent ) : QHeaderView(Qt::Horizontal , parent)
61 setClickable(true);
62 setCascadingSectionResizes(true);
64 m_lockIcon = DGui::IconLoader::self()->load("lock.png");
65 m_viewIcon = DGui::IconLoader::self()->load("show_hide_all_layers.png");
71 LayerManagerHeader::~LayerManagerHeader()
75 void LayerManagerHeader::paintSection ( QPainter * painter, const QRect & rect, int logicalIndex ) const
77 if ( !rect.isValid() ) return;
79 QStyleOptionHeader headerOption;
80 headerOption.rect = rect;
81 headerOption.orientation = Qt::Horizontal;
82 headerOption.position = QStyleOptionHeader::Middle;
84 QStyle::State state = QStyle::State_None;
85 if (isEnabled())
86 state |= QStyle::State_Enabled;
87 if (window()->isActiveWindow())
88 state |= QStyle::State_Active;
90 style()->drawControl ( QStyle::CE_HeaderSection, &headerOption, painter );
92 painter->drawRect( rect.normalized().adjusted(0, 1, 0, -1) );
94 QString text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();;
96 QFontMetrics fm( painter->font() );
98 int x = rect.x() + (sectionSize(logicalIndex) - fm.width( text ))/2;
99 int y = fm.height() + ( rect.y() / 2);
101 painter->drawText( x, y, text);
103 QPen originalPen = painter->pen();
105 painter->setPen(QPen(palette().text().color(), 2) );
106 // painter->drawRect(rect.normalized().adjusted(1, 1, -2, -2));
109 painter->setPen(originalPen);
111 switch(logicalIndex)
113 case LAYER_COLUMN:
115 // nothing...
117 break;
118 case LOCK_COLUMN:
120 m_lockIcon.paint( painter, rect.normalized().adjusted(3,3, -3, -3));
122 break;
123 case VIEW_COLUMN:
125 m_viewIcon.paint( painter, rect.normalized().adjusted(3,3, -3, -3));
127 break;
132 // DELEGATE
135 class LayerManagerItemDelegate : public QItemDelegate
137 public:
138 LayerManagerItemDelegate(QObject * parent = 0 );
139 ~LayerManagerItemDelegate();
140 virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
143 LayerManagerItemDelegate::LayerManagerItemDelegate(QObject * parent) : QItemDelegate(parent)
147 LayerManagerItemDelegate::~LayerManagerItemDelegate()
151 void LayerManagerItemDelegate::paint ( QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
154 QItemDelegate::paint(painter, option, index);
156 LayerManager *table = qobject_cast<LayerManager *>(index.model()->parent());
158 QTableWidgetItem *item = table->itemFromIndex(index);
160 if ( item )
162 switch( index.column() )
164 case LAYER_COLUMN:
166 if ( item->isSelected() )
168 painter->drawRect(option.rect.normalized().adjusted(1,1,-2, -2 ));
171 break;
172 case LOCK_COLUMN:
173 case VIEW_COLUMN:
175 QStyleOptionButton checkOption;
177 checkOption.state = option.state;
179 if ( item->checkState() == Qt::Checked )
181 checkOption.state |= QStyle::State_On;
184 checkOption.rect = option.rect.normalized().adjusted(2,2,-2,-2);
186 table->style()->drawPrimitive ( QStyle::PE_IndicatorCheckBox, &checkOption, painter);
188 break;
195 ////////////////////////////////
198 struct LayerManager::Private
200 Private() : allSelected(false), allVisible(true), allLock(false), rowHeight(20) {}
202 bool allSelected, allVisible, allLock;
203 int rowHeight;
207 LayerManager::LayerManager(QWidget *parent) : QTableWidget(0, 3, parent), d(new Private)
209 // DINIT;
211 QTableWidgetItem *prototype = new QTableWidgetItem;
212 prototype->setTextAlignment(Qt::AlignCenter);
213 // prototype->setBackgroundColor( palette().text().color() );
214 // prototype->setTextColor(palette().background().color() );
216 setItemPrototype(prototype);
218 setHorizontalHeaderLabels(QStringList() << tr("Layer") << tr("L") << tr("V") );
219 verticalHeader()->hide();
221 // setHorizontalHeader(new LayerManagerHeader(this));
222 // setItemDelegate(new LayerManagerItemDelegate(this));
224 connect(this, SIGNAL(itemChanged ( QTableWidgetItem * )), this, SLOT(onItemChanged( QTableWidgetItem * )));
229 LayerManager::~LayerManager()
231 // DEND;
232 delete d;
236 void LayerManager::insertLayer(int position, const QString &name)
238 blockSignals(true);
239 if ( position >= 0 && position <= rowCount())
242 QTableWidgetItem *newLayer = new QTableWidgetItem(name);
243 newLayer->setIcon(DGui::IconLoader::self()->load("applications-graphics.svg"));
245 newLayer->setTextAlignment(Qt::AlignCenter);
247 newLayer->setBackgroundColor( palette().background().color() );
248 newLayer->setTextColor(palette().foreground().color() );
250 insertRow(position);
251 setItem(position, 0, newLayer);
252 fixSize();
254 QTableWidgetItem *lockItem = new QTableWidgetItem;
255 lockItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
256 lockItem->setCheckState( Qt::Unchecked);
257 lockItem->setTextAlignment(Qt::AlignCenter);
258 lockItem->setText(" ");
259 setItem(position, 1, lockItem);
261 QTableWidgetItem *viewItem = new QTableWidgetItem;
262 viewItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
263 viewItem->setCheckState( Qt::Checked);
264 viewItem->setTextAlignment(Qt::AlignCenter);
265 viewItem->setText(" ");
266 setItem(position, 2, viewItem);
268 blockSignals(false);
271 void LayerManager::insertSoundLayer(int position, const QString &name)
273 blockSignals(true);
274 if ( position >= 0 && position <= rowCount() )
276 QTableWidgetItem *newLayer = new QTableWidgetItem(name);
277 newLayer->setTextAlignment(Qt::AlignCenter);
279 newLayer->setBackgroundColor( palette().background().color() );
280 newLayer->setTextColor(palette().foreground().color() );
282 insertRow(position);
283 setItem(position, 0, newLayer);
284 fixSize();
286 newLayer->setIcon( DGui::IconLoader::self()->load("audio-volume-high.svg") );
288 QTableWidgetItem *lockItem = new QTableWidgetItem;
289 lockItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
290 lockItem->setCheckState( Qt::Unchecked);
292 setItem(position, 1, lockItem);
294 QTableWidgetItem *viewItem = new QTableWidgetItem;
295 viewItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
296 viewItem->setCheckState( Qt::Checked);
298 setItem(position, 2, viewItem);
300 blockSignals(false);
303 void LayerManager::removeLayer(int position)
305 removeRow( verticalHeader()->logicalIndex(position) );
308 void LayerManager::renameLayer(int position, const QString &name)
310 QTableWidgetItem *item = this->item(verticalHeader()->logicalIndex(position), 0);
312 if ( item )
314 item->setText(name);
318 void LayerManager::resizeEvent( QResizeEvent *)
320 fixSize();
323 void LayerManager::fixSize()
325 int offset = 0;
326 if ( verticalScrollBar()->isVisible() )
328 offset = verticalScrollBar()->width()-2;
330 else
332 offset = -2;
335 int width = this->width() - offset;
337 horizontalHeader()->resizeSection(0, width-(d->rowHeight*2)-8 );
338 horizontalHeader()->resizeSection(1, d->rowHeight );
339 horizontalHeader()->resizeSection(2, d->rowHeight );
341 for(int row = 0; row < rowCount(); row++)
343 verticalHeader()->resizeSection(row, d->rowHeight);
348 void LayerManager::setRowHeight(int rowHeight)
350 d->rowHeight = rowHeight;
354 void LayerManager::commitData( QWidget *editor )
356 QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
358 QTableWidget::commitData(0); // Don't rename
360 if ( lineEdit )
362 emit layerRenamed( verticalHeader()->visualIndex(currentRow()), lineEdit->text() );
367 void LayerManager::onItemChanged( QTableWidgetItem * item)
369 if( item->column() == LOCK_COLUMN)
371 emit layerLocked(visualRow(item->row()), item->checkState() == Qt::Checked );
373 else if(item->column() == VIEW_COLUMN)
375 emit layerVisibilityChanged(visualRow(item->row()), item->checkState() == Qt::Checked );
381 void LayerManager::moveLayer(int position, int newPosition)
383 if ( position < 0 || position >= rowCount() || newPosition < 0 || newPosition >= rowCount() ) return;
385 verticalHeader()->moveSection( (position), (newPosition) );
387 // QTableWidgetItem *item1 = takeItem(position, 0);
389 // bool up = true;
390 // if ( position > newPosition )
391 // {
392 // up = false; // down
393 // }
395 // if ( up )
396 // {
397 // for(int i = position+1; i <= newPosition; i++)
398 // {
399 // setItem(i-1, 0, takeItem(i, 0));
400 // }
401 // }
402 // else
403 // {
404 // for(int i = position-1; i >= newPosition; i-- )
405 // {
406 // setItem(i+1, 0, takeItem(i, 0));
407 // }
408 // }
410 // setItem(newPosition, 0, item1);
412 // setCurrentItem(item1);
416 void LayerManager::lockLayer(int position, bool locked)
418 if ( position < 0 || position >= rowCount() ) return;
420 QTableWidgetItem *item = this->item(verticalHeader()->logicalIndex(position), LOCK_COLUMN);
422 if ( item )
424 if ( locked )
426 item->setCheckState (Qt::Checked);
428 else
430 item->setCheckState (Qt::Unchecked);