1 /***************************************************************************
2 * Copyright (C) 2005 by David Cuadrado *
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. *
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. *
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 "framestable.h"
24 #include <QPaintEvent>
25 #include <QItemSelectionModel>
26 #include <QPainterPath>
28 #include <QHeaderView>
31 // #include "ktprojectrequest.h"
34 namespace Components
{
36 ////////// FramesTableItemDelegate ///////////
38 class FramesTableItemDelegate
: public QAbstractItemDelegate
41 FramesTableItemDelegate(QObject
* parent
= 0 );
42 ~FramesTableItemDelegate();
43 virtual void paint ( QPainter
* painter
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const;
44 virtual QSize
sizeHint ( const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const;
47 FramesTableItemDelegate::FramesTableItemDelegate(QObject
* parent
) : QAbstractItemDelegate(parent
)
51 FramesTableItemDelegate::~FramesTableItemDelegate()
55 void FramesTableItemDelegate::paint ( QPainter
* painter
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const
57 Q_ASSERT(index
.isValid());
59 FramesTable
*table
= qobject_cast
<FramesTable
*>(index
.model()->parent());
60 FramesTableItem
*item
= dynamic_cast<FramesTableItem
*>(table
->itemFromIndex(index
));
63 QStyleOptionViewItem opt
= option
;
65 // draw the background color
66 value
= index
.data( Qt::BackgroundColorRole
);
72 bool sound
= table
->isSoundLayer(index
.row());
76 painter
->fillRect(option
.rect
, value
.value
<QColor
>() );
88 bool sound
= table
->isSoundLayer(index
.row());
92 if ( index
.column() % 5 == 0 )
94 painter
->fillRect(option
.rect
, Qt::lightGray
);
98 painter
->fillRect(option
.rect
, Qt::white
);
109 if (option
.showDecorationSelected
&& (option
.state
& QStyle::State_Selected
))
111 QPalette::ColorGroup cg
= option
.state
& QStyle::State_Enabled
? QPalette::Normal
: QPalette::Disabled
;
114 painter
->setPen(QPen(option
.palette
.brush(cg
, QPalette::Highlight
), 3));
115 painter
->drawRect(option
.rect
.adjusted(1,1,-2,-2));
121 int offset
= option
.rect
.width() - 2;
123 if ( item
&& index
.isValid() )
128 painter
->setBrush(Qt::black
);
130 if( !item
->isSound() )
132 painter
->drawEllipse( option
.rect
.left(), option
.rect
.bottom() - offset
, offset
, offset
);
136 painter
->setBrush(Qt::blue
);
137 painter
->drawRect( option
.rect
.left(), option
.rect
.bottom() - offset
, offset
, offset
);
143 if ( item
->isLocked() )
146 painter
->setBrush(Qt::red
);
148 painter
->drawEllipse( option
.rect
.left(), option
.rect
.bottom() - offset
, offset
, offset
);
155 QSize
FramesTableItemDelegate::sizeHint ( const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const
157 Q_ASSERT(index
.isValid());
158 const QAbstractItemModel
*model
= index
.model();
161 QVariant value
= model
->data(index
, Qt::FontRole
);
162 QFont fnt
= value
.isValid() ? qvariant_cast
<QFont
>(value
) : option
.font
;
163 QString text
= model
->data(index
, Qt::DisplayRole
).toString();
165 if (model
->data(index
, Qt::DecorationRole
).isValid())
166 pixmapRect
= QRect(0, 0, option
.decorationSize
.width(),
167 option
.decorationSize
.height());
169 QFontMetrics
fontMetrics(fnt
);
171 return (pixmapRect
).size();
175 ////////// FramesTableItem ////////
177 FramesTableItem::FramesTableItem()
181 FramesTableItem::~FramesTableItem()
186 bool FramesTableItem::isUsed()
188 return data(IsUsed
).toBool();
191 bool FramesTableItem::isLocked()
193 return data(IsLocked
).toBool();
196 bool FramesTableItem::isSound()
198 QVariant data
= this->data(IsSound
);
200 if( data
.canConvert
<bool>() )
202 return data
.toBool();
209 struct FramesTable::Private
213 LayerItem() : lastItem(-1), sound(false) {};
218 int rectWidth
, rectHeight
;
219 QList
<LayerItem
> layers
;
223 FramesTable::FramesTable(QWidget
*parent
) : QTableWidget(0, 100, parent
), d(new Private
)
225 d
->ruler
= new TLRuler
;
229 FramesTable::~FramesTable()
234 void FramesTable::setup()
236 setItemDelegate( new FramesTableItemDelegate(this) );
238 setSelectionBehavior(QAbstractItemView::SelectItems
);
239 setSelectionMode (QAbstractItemView::SingleSelection
);
241 setHorizontalHeader(d
->ruler
);
242 connect(d
->ruler
, SIGNAL(logicalSectionSelected( int )), this, SLOT(emitFrameSelected( int )));
243 connect(this, SIGNAL(currentItemChanged( QTableWidgetItem
*, QTableWidgetItem
*)), this, SLOT(emitFrameSelected(QTableWidgetItem
*, QTableWidgetItem
*)));
244 verticalHeader()->hide();
246 setItemSize( 10, 25 );
248 horizontalHeader()->setResizeMode(QHeaderView::Custom
);
249 verticalHeader()->setResizeMode(QHeaderView::Custom
);
254 void FramesTable::emitFrameSelected(int col
)
258 FramesTableItem
*item
= dynamic_cast<FramesTableItem
*>(this->item(currentRow(), col
));
264 // emit frameRequest(KTProjectRequest::Select, this->column(item), verticalHeader()->visualIndex(this->row(item)), -1);
269 void FramesTable::emitFrameSelected(QTableWidgetItem
*curr
, QTableWidgetItem
*prev
)
271 FramesTableItem
*item
= dynamic_cast<FramesTableItem
*>(curr
);
277 // emit frameRequest(KTProjectRequest::Select, this->column(item), verticalHeader()->visualIndex(this->row(item)), -1);
282 void FramesTable::setItemSize(int w
, int h
)
290 bool FramesTable::isSoundLayer(int row
)
292 if( row
< 0 && row
>= d
->layers
.count() )
295 return d
->layers
[row
].sound
;
298 void FramesTable::insertLayer(int pos
, const QString
&name
)
302 Private::LayerItem layer
;
304 d
->layers
.insert(pos
, layer
);
306 // selectCell( pos, 0 );
311 void FramesTable::insertSoundLayer(int layerPos
, const QString
&name
)
315 Private::LayerItem layer
;
317 d
->layers
.insert(layerPos
, layer
);
322 void FramesTable::removeCurrentLayer()
324 int pos
= verticalHeader()->logicalIndex(currentRow());
328 void FramesTable::removeLayer(int pos
)
330 pos
= verticalHeader()->logicalIndex(pos
);
332 d
->layers
.removeAt(pos
);
335 void FramesTable::moveLayer(int position
, int newPosition
)
337 if ( position
< 0 || position
>= rowCount() || newPosition
< 0 || newPosition
>= rowCount() ) return;
341 verticalHeader()->moveSection(position
, newPosition
);
345 // FramesTableItem *item1 = takeItem(position, 0);
348 // if ( position > newPosition )
350 // up = false; // down
355 // for(int i = position+1; i <= newPosition; i++)
357 // setItem(i-1, 0, takeItem(i, 0));
362 // for(int i = position-1; i >= newPosition; i-- )
364 // setItem(i+1, 0, takeItem(i, 0));
368 // setItem(newPosition, 0, item1);
370 // setCurrentItem(item1);
373 int FramesTable::lastFrameByLayer(int layerPos
)
375 int pos
= verticalHeader()->logicalIndex(layerPos
);
376 if ( pos
< 0 || pos
> d
->layers
.count() )
380 return d
->layers
[pos
].lastItem
;
386 void FramesTable::insertFrame(int layerPos
, const QString
&name
)
388 if ( layerPos
< 0 || layerPos
>= d
->layers
.count() ) return;
390 layerPos
= verticalHeader()->logicalIndex(layerPos
);
392 d
->layers
[layerPos
].lastItem
++;
394 if ( d
->layers
[layerPos
].lastItem
>= columnCount() )
396 insertColumn( d
->layers
[layerPos
].lastItem
);
399 setAttribute( layerPos
, d
->layers
[layerPos
].lastItem
, FramesTableItem::IsUsed
, true);
400 setAttribute( layerPos
, d
->layers
[layerPos
].lastItem
, FramesTableItem::IsSound
, d
->layers
[layerPos
].sound
);
402 viewport()->update();
405 void FramesTable::setCurrentFrame(FramesTableItem
*item
)
407 setCurrentItem(item
);
410 void FramesTable::setCurrentLayer(int layerPos
)
412 setCurrentItem(item(verticalHeader()->logicalIndex(layerPos
), 0));
415 void FramesTable::selectFrame(int index
)
417 setCurrentItem( item( currentRow(), index
) );
420 void FramesTable::removeFrame(int layerPos
, int position
)
422 // for(int frameIndex = position; frameIndex < columnCount(); frameIndex++ )
424 // setAttribute( layerPos, position, FramesTableItem::IsUsed, false);
427 if ( layerPos
< 0 || layerPos
>= d
->layers
.count() )
432 layerPos
= verticalHeader()->logicalIndex(layerPos
);
434 setAttribute( layerPos
, d
->layers
[layerPos
].lastItem
, FramesTableItem::IsUsed
, false );
436 d
->layers
[layerPos
].lastItem
--;
438 // viewport()->update( visualRect(indexFromItem( item(layerPos, position) )) );
439 viewport()->update();
442 void FramesTable::lockFrame(int layerPos
, int position
, bool lock
)
444 if ( layerPos
< 0 || layerPos
>= d
->layers
.count() )
449 layerPos
= verticalHeader()->logicalIndex(layerPos
);
451 setAttribute( layerPos
, position
, FramesTableItem::IsLocked
, lock
);
453 viewport()->update();
457 void FramesTable::setAttribute(int row
, int col
, FramesTableItem::Attributes att
, bool value
)
459 QTableWidgetItem
*item
= this->item(row
, col
);
463 item
= new FramesTableItem
;
464 setItem(row
, col
, item
);
467 item
->setData(att
, value
);
470 void FramesTable::fixSize()
472 for(int column
= 0; column
< columnCount(); column
++)
474 horizontalHeader()->resizeSection(column
, d
->rectWidth
);
476 for( int row
= 0; row
< rowCount(); row
++)
478 verticalHeader()->resizeSection(row
, d
->rectHeight
);
482 // void FramesTable::fixSectionMoved(int logical, int visual, int newVisual)
484 // verticalHeader()->moveSection(newVisual, visual);