class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / QcTreeWidget.cpp
blob0280802d45af926b8f1b633ca0584f5d3f19074b
1 /************************************************************************
3 * Copyright 2011 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "QcTreeWidget.h"
23 #include "../QcWidgetFactory.h"
25 #include <PyrKernel.h>
27 class QcTreeWidgetFactory : public QcWidgetFactory<QcTreeWidget>
29 void initialize( QWidgetProxy *p, QcTreeWidget *w ) {
30 p->setMouseEventWidget( w->viewport() );
34 static QcTreeWidgetFactory treeWidgetFactory;
36 QcTreeWidget::QcTreeWidget()
38 // Forward signals to argument-less versions connectable from SC.
39 connect( this, SIGNAL( itemActivated(QTreeWidgetItem*, int) ),
40 this, SIGNAL( action() ) );
41 connect( this, SIGNAL( itemPressed(QTreeWidgetItem*, int) ),
42 this, SIGNAL( itemPressedAction() ) );
43 connect( this, SIGNAL( currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*) ),
44 this, SIGNAL( currentItemChanged() ) );
48 VariantList QcTreeWidget::columns() const
50 VariantList varList;
51 QTreeWidgetItem *header = headerItem();
52 if( header ) {
53 for( int i = 0; i < header->columnCount(); ++i ) {
54 varList.data << header->text(i);
57 return varList;
60 void QcTreeWidget::setColumns( const VariantList & varList )
62 int count = varList.data.size();
63 setColumnCount( count );
64 QStringList labels;
65 Q_FOREACH( QVariant var, varList.data )
66 labels << var.toString();
67 setHeaderLabels( labels );
70 QcTreeWidget::ItemPtr QcTreeWidget::currentItem() const
72 QTreeWidgetItem *item = QTreeWidget::currentItem();
73 return QcTreeWidget::Item::safePtr( item );
76 void QcTreeWidget::setCurrentItem( const ItemPtr &item )
78 _emitAction = false;
79 QTreeWidget::setCurrentItem( item );
80 _emitAction = true;
83 QcTreeWidget::ItemPtr QcTreeWidget::item( const QcTreeWidget::ItemPtr & parent, int index )
85 QTreeWidgetItem *item =
86 parent ? parent->child(index) : QTreeWidget::topLevelItem(index);
88 return QcTreeWidget::Item::safePtr( item );
91 QcTreeWidget::ItemPtr QcTreeWidget::parentItem( const QcTreeWidget::ItemPtr & item )
93 if( !item ) return QcTreeWidget::ItemPtr();
94 return QcTreeWidget::Item::safePtr( item->parent() );
97 int QcTreeWidget::indexOfItem( const QcTreeWidget::ItemPtr & item )
99 if( !item ) return -1;
100 QTreeWidgetItem *parent = item->parent();
101 if( parent )
102 return parent->indexOfChild( item );
103 else
104 return indexOfTopLevelItem( item );
107 QcTreeWidget::ItemPtr QcTreeWidget::addItem (
108 const QcTreeWidget::ItemPtr & parent, const VariantList & varList )
110 QStringList strings;
111 for( int i = 0; i < varList.data.count(); ++i )
112 strings << varList.data[i].toString();
114 Item *item = new Item( strings );
115 if( !parent ) addTopLevelItem( item );
116 else parent->addChild( item );
118 return item->safePtr();
121 QcTreeWidget::ItemPtr QcTreeWidget::insertItem (
122 const QcTreeWidget::ItemPtr & parent, int index, const VariantList & varList )
124 int itemCount = topLevelItemCount();
125 if( index < 0 || index > itemCount ) return ItemPtr();
127 QStringList strings;
128 for( int i = 0; i < varList.data.count(); ++i )
129 strings << varList.data[i].toString();
131 Item *item = new Item( strings );
132 if( !parent ) insertTopLevelItem( index, item );
133 else parent->insertChild( index, item );
135 if( !item->treeWidget() ) {
136 delete item;
137 return ItemPtr();
140 return item->safePtr();
143 void QcTreeWidget::removeItem( const QcTreeWidget::ItemPtr & item )
145 delete item.ptr();
148 VariantList QcTreeWidget::strings( const QcTreeWidget::ItemPtr & item )
150 VariantList varList;
151 if( !item ) return varList;
152 for( int i = 0; i < item->columnCount(); ++i ) {
153 varList.data << item->text(i);
155 return varList;
158 void QcTreeWidget::setText( const QcTreeWidget::ItemPtr &item, int column, const QString & text )
160 if( item ) item->setText( column, text );
163 void QcTreeWidget::setColor( const QcTreeWidget::ItemPtr &item, int column, const QColor & color )
165 if( item ) item->setBackground( column, color );
168 void QcTreeWidget::setTextColor( const QcTreeWidget::ItemPtr & item, int column, const QColor & color )
170 if( item ) item->setData( column, Qt::ForegroundRole, color );
173 QWidget * QcTreeWidget::itemWidget( const QcTreeWidget::ItemPtr &item, int column )
175 return item ? QTreeWidget::itemWidget( item, column ) : 0;
178 void QcTreeWidget::setItemWidget( const QcTreeWidget::ItemPtr &item, int column, QObjectProxy *o )
180 if( !item ) return;
182 QWidget *w = qobject_cast<QWidget*>(o->object());
183 if( !w ) return;
185 QTreeWidget::setItemWidget( item, column, w );
188 void QcTreeWidget::removeItemWidget( const QcTreeWidget::ItemPtr &item, int column )
190 if( item ) QTreeWidget::removeItemWidget( item, column );
193 void QcTreeWidget::sort( int column, bool descending )
195 sortItems( column, descending ? Qt::DescendingOrder : Qt::AscendingOrder );
198 QcTreeWidget::ItemPtr QcTreeWidget::Item::safePtr( QTreeWidgetItem * item )
200 if( item && item->type() == Item::Type )
201 return static_cast<Item*>(item)->safePtr();
202 else
203 return ItemPtr();
206 void QcTreeWidget::Item::initialize (
207 VMGlobals *g, PyrObject *obj, const QcTreeWidget::ItemPtr &ptr )
209 Q_ASSERT( isKindOf( obj, class_QTreeViewItem ) );
210 if( ptr.id() ) {
211 // store the SafePtr
212 QcTreeWidget::ItemPtr *newPtr = new QcTreeWidget::ItemPtr( ptr );
213 SetPtr( obj->slots+0, newPtr );
214 // store the id for equality comparison
215 SetPtr( obj->slots+1, ptr.id() );
216 // install finalizer
218 else {
219 SetNil( obj->slots+0 );
220 SetNil( obj->slots+1 );
222 InstallFinalizer( g, obj, 2, &QcTreeWidget::Item::finalize );
225 int QcTreeWidget::Item::finalize( VMGlobals *g, PyrObject *obj )
227 qcDebugMsg(1,"finalizing QTreeViewItem!");
228 if( IsPtr( obj->slots+0 ) ) {
229 QcTreeWidget::ItemPtr *ptr = static_cast<QcTreeWidget::ItemPtr*>( slotRawPtr(obj->slots+0) );
230 delete ptr;
232 return errNone;