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 "layermanager.h"
27 #include <QHeaderView>
29 #include <QItemDelegate>
32 #include <dgui/separator.h>
33 #include <dgui/application.h>
34 #include <dgui/iconloader.h>
35 #include <dcore/debug.h>
40 const int LAYER_COLUMN
= 0;
41 const int LOCK_COLUMN
= 1;
42 const int VIEW_COLUMN
= 2;
46 class LayerManagerHeader
: public QHeaderView
49 LayerManagerHeader(QWidget
* parent
= 0);
50 ~LayerManagerHeader();
52 void paintSection(QPainter
*painter
, const QRect
& rect
, int logicalIndex
) const;
59 LayerManagerHeader::LayerManagerHeader(QWidget
* parent
) : QHeaderView(Qt::Horizontal
, parent
)
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
;
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
);
120 m_lockIcon
.paint( painter
, rect
.normalized().adjusted(3,3, -3, -3));
125 m_viewIcon
.paint( painter
, rect
.normalized().adjusted(3,3, -3, -3));
135 class LayerManagerItemDelegate : public QItemDelegate
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);
162 switch( index.column() )
166 if ( item->isSelected() )
168 painter->drawRect(option.rect.normalized().adjusted(1,1,-2, -2 ));
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);
195 ////////////////////////////////
198 struct LayerManager::Private
200 Private() : allSelected(false), allVisible(true), allLock(false), rowHeight(20) {}
202 bool allSelected
, allVisible
, allLock
;
207 LayerManager::LayerManager(QWidget
*parent
) : QTableWidget(0, 3, parent
), d(new Private
)
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()
236 void LayerManager::insertLayer(int position
, const QString
&name
)
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() );
251 setItem(position
, 0, newLayer
);
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
);
271 void LayerManager::insertSoundLayer(int position
, const QString
&name
)
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() );
283 setItem(position
, 0, newLayer
);
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
);
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);
318 void LayerManager::resizeEvent( QResizeEvent
*)
323 void LayerManager::fixSize()
326 if ( verticalScrollBar()->isVisible() )
328 offset
= verticalScrollBar()->width()-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
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);
390 // if ( position > newPosition )
392 // up = false; // down
397 // for(int i = position+1; i <= newPosition; i++)
399 // setItem(i-1, 0, takeItem(i, 0));
404 // for(int i = position-1; i >= newPosition; i-- )
406 // setItem(i+1, 0, takeItem(i, 0));
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
);
426 item
->setCheckState (Qt::Checked
);
430 item
->setCheckState (Qt::Unchecked
);