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>
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
53 QTreeWidgetItem
*header
= headerItem();
55 for( int i
= 0; i
< header
->columnCount(); ++i
) {
56 varList
.data
<< header
->text(i
);
62 void QcTreeWidget::setColumns( const VariantList
& varList
)
64 int count
= varList
.data
.size();
65 setColumnCount( count
);
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
)
81 QTreeWidget::setCurrentItem( item
);
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();
104 return parent
->indexOfChild( item
);
106 return indexOfTopLevelItem( item
);
109 QcTreeWidget::ItemPtr
QcTreeWidget::addItem (
110 const QcTreeWidget::ItemPtr
& parent
, const VariantList
& varList
)
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();
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() ) {
142 return item
->safePtr();
145 void QcTreeWidget::removeItem( const QcTreeWidget::ItemPtr
& item
)
150 VariantList
QcTreeWidget::strings( const QcTreeWidget::ItemPtr
& item
)
153 if( !item
) return varList
;
154 for( int i
= 0; i
< item
->columnCount(); ++i
) {
155 varList
.data
<< item
->text(i
);
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
)
184 QWidget
*w
= qobject_cast
<QWidget
*>(o
->object());
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
);
211 case Qt::Key_PageDown
:
214 // Prevent propagating to parent when scroller reaches minimum or maximum:
220 //////////////////////////////// Item //////////////////////////////////////
222 QcTreeWidget::ItemPtr
QcTreeWidget::Item::safePtr( QTreeWidgetItem
* item
)
224 if( item
&& item
->type() == Item::Type
)
225 return static_cast<Item
*>(item
)->safePtr();
230 void QcTreeWidget::Item::initialize (
231 VMGlobals
*g
, PyrObject
*obj
, const QcTreeWidget::ItemPtr
&ptr
)
233 Q_ASSERT( isKindOf( obj
, SC_CLASS(QTreeViewItem
) ) );
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() );
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) );
259 bool QcTreeWidget::Item::operator< (const QTreeWidgetItem
&other
) const
261 int column
= treeWidget()->sortColumn();
262 return text(column
).toLower() < other
.text(column
).toLower();