"Post Window" -> "Post window" prevents it being seen as two separate
[supercollider.git] / QtCollider / widgets / QcTreeWidget.cpp
blob9cf8f099923452b2ddea12cf1dbfae15b8ebe596
1 /************************************************************************
3 * Copyright 2011-2012 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 #include <QKeyEvent>
29 class QcTreeWidgetFactory : public QcWidgetFactory<QcTreeWidget>
31 void initialize( QWidgetProxy *p, QcTreeWidget *w ) {
32 p->setMouseEventWidget( w->viewport() );
36 QC_DECLARE_FACTORY( QcTreeWidget, QcTreeWidgetFactory );
38 QcTreeWidget::QcTreeWidget()
40 // Forward signals to argument-less versions connectable from SC.
41 connect( this, SIGNAL( itemActivated(QTreeWidgetItem*, int) ),
42 this, SIGNAL( action() ) );
43 connect( this, SIGNAL( itemPressed(QTreeWidgetItem*, int) ),
44 this, SIGNAL( itemPressedAction() ) );
45 connect( this, SIGNAL( currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*) ),
46 this, SIGNAL( currentItemChanged() ) );
50 VariantList QcTreeWidget::columns() const
52 VariantList varList;
53 QTreeWidgetItem *header = headerItem();
54 if( header ) {
55 for( int i = 0; i < header->columnCount(); ++i ) {
56 varList.data << header->text(i);
59 return varList;
62 void QcTreeWidget::setColumns( const VariantList & varList )
64 int count = varList.data.size();
65 setColumnCount( count );
66 QStringList labels;
67 Q_FOREACH( QVariant var, varList.data )
68 labels << var.toString();
69 setHeaderLabels( labels );
72 QcTreeWidget::ItemPtr QcTreeWidget::currentItem() const
74 QTreeWidgetItem *item = QTreeWidget::currentItem();
75 return QcTreeWidget::Item::safePtr( item );
78 void QcTreeWidget::setCurrentItem( const ItemPtr &item )
80 _emitAction = false;
81 QTreeWidget::setCurrentItem( item );
82 _emitAction = true;
85 QcTreeWidget::ItemPtr QcTreeWidget::item( const QcTreeWidget::ItemPtr & parent, int index )
87 QTreeWidgetItem *item =
88 parent ? parent->child(index) : QTreeWidget::topLevelItem(index);
90 return QcTreeWidget::Item::safePtr( item );
93 QcTreeWidget::ItemPtr QcTreeWidget::parentItem( const QcTreeWidget::ItemPtr & item )
95 if( !item ) return QcTreeWidget::ItemPtr();
96 return QcTreeWidget::Item::safePtr( item->parent() );
99 int QcTreeWidget::indexOfItem( const QcTreeWidget::ItemPtr & item )
101 if( !item ) return -1;
102 QTreeWidgetItem *parent = item->parent();
103 if( parent )
104 return parent->indexOfChild( item );
105 else
106 return indexOfTopLevelItem( item );
109 QcTreeWidget::ItemPtr QcTreeWidget::addItem (
110 const QcTreeWidget::ItemPtr & parent, const VariantList & varList )
112 QStringList strings;
113 for( int i = 0; i < varList.data.count(); ++i )
114 strings << varList.data[i].toString();
116 Item *item = new Item( strings );
117 if( !parent ) addTopLevelItem( item );
118 else parent->addChild( item );
120 return item->safePtr();
123 QcTreeWidget::ItemPtr QcTreeWidget::insertItem (
124 const QcTreeWidget::ItemPtr & parent, int index, const VariantList & varList )
126 int itemCount = topLevelItemCount();
127 if( index < 0 || index > itemCount ) return ItemPtr();
129 QStringList strings;
130 for( int i = 0; i < varList.data.count(); ++i )
131 strings << varList.data[i].toString();
133 Item *item = new Item( strings );
134 if( !parent ) insertTopLevelItem( index, item );
135 else parent->insertChild( index, item );
137 if( !item->treeWidget() ) {
138 delete item;
139 return ItemPtr();
142 return item->safePtr();
145 void QcTreeWidget::removeItem( const QcTreeWidget::ItemPtr & item )
147 delete item.ptr();
150 VariantList QcTreeWidget::strings( const QcTreeWidget::ItemPtr & item )
152 VariantList varList;
153 if( !item ) return varList;
154 for( int i = 0; i < item->columnCount(); ++i ) {
155 varList.data << item->text(i);
157 return varList;
160 void QcTreeWidget::setText( const QcTreeWidget::ItemPtr &item, int column, const QString & text )
162 if( item ) item->setText( column, text );
165 void QcTreeWidget::setColor( const QcTreeWidget::ItemPtr &item, int column, const QColor & color )
167 if( item ) item->setBackground( column, color );
170 void QcTreeWidget::setTextColor( const QcTreeWidget::ItemPtr & item, int column, const QColor & color )
172 if( item ) item->setData( column, Qt::ForegroundRole, color );
175 QWidget * QcTreeWidget::itemWidget( const QcTreeWidget::ItemPtr &item, int column )
177 return item ? QTreeWidget::itemWidget( item, column ) : 0;
180 void QcTreeWidget::setItemWidget( const QcTreeWidget::ItemPtr &item, int column, QObjectProxy *o )
182 if( !item ) return;
184 QWidget *w = qobject_cast<QWidget*>(o->object());
185 if( !w ) return;
187 QTreeWidget::setItemWidget( item, column, w );
190 void QcTreeWidget::removeItemWidget( const QcTreeWidget::ItemPtr &item, int column )
192 if( item ) QTreeWidget::removeItemWidget( item, column );
195 void QcTreeWidget::sort( int column, bool descending )
197 sortItems( column, descending ? Qt::DescendingOrder : Qt::AscendingOrder );
200 void QcTreeWidget::keyPressEvent( QKeyEvent *e )
202 QTreeWidget::keyPressEvent( e );
204 switch (e->key())
206 case Qt::Key_Up:
207 case Qt::Key_Down:
208 case Qt::Key_Left:
209 case Qt::Key_Right:
210 case Qt::Key_PageUp:
211 case Qt::Key_PageDown:
212 case Qt::Key_Home:
213 case Qt::Key_End:
214 // Prevent propagating to parent when scroller reaches minimum or maximum:
215 e->accept();
216 default: break;
220 //////////////////////////////// Item //////////////////////////////////////
222 QcTreeWidget::ItemPtr QcTreeWidget::Item::safePtr( QTreeWidgetItem * item )
224 if( item && item->type() == Item::Type )
225 return static_cast<Item*>(item)->safePtr();
226 else
227 return ItemPtr();
230 void QcTreeWidget::Item::initialize (
231 VMGlobals *g, PyrObject *obj, const QcTreeWidget::ItemPtr &ptr )
233 Q_ASSERT( isKindOf( obj, SC_CLASS(QTreeViewItem) ) );
234 if( ptr.id() ) {
235 // store the SafePtr
236 QcTreeWidget::ItemPtr *newPtr = new QcTreeWidget::ItemPtr( ptr );
237 SetPtr( obj->slots+0, newPtr );
238 // store the id for equality comparison
239 SetPtr( obj->slots+1, ptr.id() );
240 // install finalizer
242 else {
243 SetNil( obj->slots+0 );
244 SetNil( obj->slots+1 );
246 InstallFinalizer( g, obj, 2, &QcTreeWidget::Item::finalize );
249 int QcTreeWidget::Item::finalize( VMGlobals *g, PyrObject *obj )
251 qcDebugMsg(1,"finalizing QTreeViewItem!");
252 if( IsPtr( obj->slots+0 ) ) {
253 QcTreeWidget::ItemPtr *ptr = static_cast<QcTreeWidget::ItemPtr*>( slotRawPtr(obj->slots+0) );
254 delete ptr;
256 return errNone;
259 bool QcTreeWidget::Item::operator< (const QTreeWidgetItem &other) const
261 int column = treeWidget()->sortColumn();
262 return text(column).toLower() < other.text(column).toLower();