//////////////////////////////////////////////////////////////////////////
{
public :
{
m_eDropAction = Qt::MoveAction;
}
virtual ~imQMimeData () {}
imNode* GetNode(void) const { return m_pNode; }
void SetNode(imNode* _pNode) { m_pNode = _pNode; }
imU32 GetOldIndex() const { return m_nOldIndex; }
void SetOldIndex(imU32 _nIndex) { m_nOldIndex = _nIndex; }
public :
imNode* m_pNode;
imU32 m_nOldIndex;
Qt::DropAction m_eDropAction;
};
//////////////////////////////////////////////////////////////////////////
imGuiSceneExplorer
::imGuiSceneExplorer(QWidget * parent
) : QTreeWidget(parent
), imIStreamingCallback
(){
m_pScene = imNULL;
m_bBuildingTree = imFALSE;
m_pUI = imNULL;
}
//////////////////////////////////////////////////////////////////////////
imGuiSceneExplorer::~imGuiSceneExplorer()
{
clear();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::SetUI(Ui::imGuiMainWindowClass* _pUI)
{
m_pUI = _pUI;
setColumnCount(2);
headerItem()->setText(0, tr("Name"));
headerItem()->setText(1, tr("Type"));
header
()->setResizeMode
(0,
QHeaderView::ResizeToContents);
header
()->setResizeMode
(1,
QHeaderView::ResizeToContents);
// drag & drop
setAcceptDrops(imTRUE);
setDragEnabled(true);
setDragDropMode(DragDrop);
setDragDropOverwriteMode(imFALSE);
setDropIndicatorShown(imTRUE);
// context menu
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowTreeContextMenu(const QPoint&)));
}
//////////////////////////////////////////////////////////////////////////
QSize imGuiSceneExplorer
::sizeHint() const {
return theSize;
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::SelectNode(imNode* _pNode)
{
while (*it)
{
imNode* pNode = GetNodeFromItem(*it);
if (pNode == _pNode)
{
setCurrentItem(*it);
if (m_pScene)
m_pScene->SetSelectedNode(_pNode);
return;
}
++it;
}
}
//////////////////////////////////////////////////////////////////////////
{
if (!_pItem)
return imNULL;
return *(imNode**)_pItem->data(0, Qt::UserRole).toByteArray().constData();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::BuildContent( imScene* _pScene )
{
clear();
m_pScene = _pScene;
if (!_pScene)
return;
// create top level item
m_bBuildingTree = imTRUE;
imNode* pRoot = _pScene->GetRootNode();
AddNodeItem(pRoot, imNULL, -1);
m_bBuildingTree = imFALSE;
}
//////////////////////////////////////////////////////////////////////////
{
imAssert(_pNode);
if (_pNode)
{
Qt::ItemFlags flag = pItem->flags();
flag |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
pItem->setFlags(flag);
// node name
imStringBase sName(_pNode->GetName().GetLength() ? _pNode->GetName() : _pNode->RTTIGetClass()->getName());
pItem->setText(0, sQName);
// node type
imStringBase sType(_pNode->RTTIGetClass()->getName());
pItem->setText(1, sQType);
// set data
QByteArray data
((const char *)&_pNode,
sizeof(_pNode
));
pItem->setData( 0, Qt::UserRole, oVariant);
return pItem;
}
return imNULL;
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer
::rowsInserted ( const QModelIndex & parent,
int start,
int end
) {
if (!m_bBuildingTree)
{
if (pItemParent)
{
imNode* pParent = GetNodeFromItem(pItemParent);
if (pParent)
{
for (imInt i = start; i <= end; ++i)
{
imNode* pChild = GetNodeFromItem(pItemChild);
pParent->InsertChild(pChild, i);
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer
::rowsAboutToBeRemoved ( const QModelIndex & parent,
int start,
int end
) {
if (!m_bBuildingTree)
{
imNode* pParent = GetNodeFromItem(pItemParent);
for (imInt i = start; i <= end; ++i)
{
imNode* pChild = GetNodeFromItem(pItemChild);
pParent->RemoveChild(i, imFALSE);
}
}
}
//////////////////////////////////////////////////////////////////////////
QMimeData *imGuiSceneExplorer
::mimeData(const QList<QTreeWidgetItem
*> items
) const {
imAssert(items.size());
if (items.size())
{
imQMimeData *mimeData = imNew imQMimeData();
imNode* pNode = *(imNode**)items[0]->data(0, Qt::UserRole).toByteArray().constData();
mimeData->SetNode(pNode);
mimeData->SetItem(items[0]);
mimeData->SetOldIndex(items[0]->parent() ? items[0]->parent()->indexOfChild(items[0]) : 0);
return mimeData;
}
return imNULL;
}
//////////////////////////////////////////////////////////////////////////
{
if (action == Qt::IgnoreAction)
return true;
if (parent)
{
const imQMimeData* myData = static_cast<const imQMimeData *>(data);
if (myData && parent != myData->GetItem())
{
imNode* pNode = myData->GetNode();
AddNodeItem(pNode, parent, index);
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////
{
event->accept();
}
//////////////////////////////////////////////////////////////////////////
{
event->accept();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer
::paintEvent( QPaintEvent* event
) {
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::OnItemSelected()
{
// do something
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class imQMimeData : public QMimeData
{
public :
imQMimeData () : QMimeData ()
{
m_eDropAction = Qt::MoveAction;
}
virtual ~imQMimeData () {}
imNode* GetNode(void) const { return m_pNode; }
void SetNode(imNode* _pNode) { m_pNode = _pNode; }
QTreeWidgetItem* GetItem(void) const { return m_pItem; }
void SetItem(QTreeWidgetItem* _pItem) { m_pItem = _pItem; }
imU32 GetOldIndex() const { return m_nOldIndex; }
void SetOldIndex(imU32 _nIndex) { m_nOldIndex = _nIndex; }
public :
imNode* m_pNode;
QTreeWidgetItem* m_pItem;
imU32 m_nOldIndex;
Qt::DropAction m_eDropAction;
};
//////////////////////////////////////////////////////////////////////////
imGuiSceneExplorer::imGuiSceneExplorer(QWidget * parent) : QTreeWidget(parent), imIStreamingCallback()
{
m_pScene = imNULL;
m_bBuildingTree = imFALSE;
m_pUI = imNULL;
}
//////////////////////////////////////////////////////////////////////////
imGuiSceneExplorer::~imGuiSceneExplorer()
{
clear();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::SetUI(Ui::imGuiMainWindowClass* _pUI)
{
m_pUI = _pUI;
setColumnCount(2);
headerItem()->setText(0, tr("Name"));
headerItem()->setText(1, tr("Type"));
header()->setResizeMode(0, QHeaderView::ResizeToContents);
header()->setResizeMode(1, QHeaderView::ResizeToContents);
// drag & drop
setAcceptDrops(imTRUE);
setDragEnabled(true);
setDragDropMode(DragDrop);
setDragDropOverwriteMode(imFALSE);
setDropIndicatorShown(imTRUE);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
// context menu
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowTreeContextMenu(const QPoint&)));
}
//////////////////////////////////////////////////////////////////////////
QSize imGuiSceneExplorer::sizeHint() const
{
QSize theSize(256, 768);
return theSize;
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::SelectNode(imNode* _pNode)
{
QTreeWidgetItemIterator it(this);
while (*it)
{
imNode* pNode = GetNodeFromItem(*it);
if (pNode == _pNode)
{
setCurrentItem(*it);
if (m_pScene)
m_pScene->SetSelectedNode(_pNode);
return;
}
++it;
}
}
//////////////////////////////////////////////////////////////////////////
imNode* imGuiSceneExplorer::GetNodeFromItem(QTreeWidgetItem* _pItem)
{
if (!_pItem)
return imNULL;
return *(imNode**)_pItem->data(0, Qt::UserRole).toByteArray().constData();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::BuildContent( imScene* _pScene )
{
clear();
m_pScene = _pScene;
if (!_pScene)
return;
// create top level item
m_bBuildingTree = imTRUE;
imNode* pRoot = _pScene->GetRootNode();
AddNodeItem(pRoot, imNULL, -1);
m_bBuildingTree = imFALSE;
}
//////////////////////////////////////////////////////////////////////////
QTreeWidgetItem* imGuiSceneExplorer::CreateItemFromNode(imNode* _pNode)
{
imAssert(_pNode);
if (_pNode)
{
QTreeWidgetItem* pItem = imNew QTreeWidgetItem();
Qt::ItemFlags flag = pItem->flags();
flag |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
pItem->setFlags(flag);
// node name
imStringBase sName(_pNode->GetName().GetLength() ? _pNode->GetName() : _pNode->RTTIGetClass()->getName());
QString sQName = QString::fromWCharArray((const wchar_t*)sName);
pItem->setText(0, sQName);
// node type
imStringBase sType(_pNode->RTTIGetClass()->getName());
QString sQType = QString::fromWCharArray((const wchar_t*)sType);
pItem->setText(1, sQType);
// set data
QByteArray data((const char *)&_pNode, sizeof(_pNode));
QVariant oVariant(data);
pItem->setData( 0, Qt::UserRole, oVariant);
return pItem;
}
return imNULL;
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::rowsInserted ( const QModelIndex & parent, int start, int end )
{
if (!m_bBuildingTree)
{
QTreeWidgetItem* pItemParent = itemFromIndex(parent);
if (pItemParent)
{
imNode* pParent = GetNodeFromItem(pItemParent);
if (pParent)
{
for (imInt i = start; i <= end; ++i)
{
QTreeWidgetItem* pItemChild = pItemParent->child(i);
imNode* pChild = GetNodeFromItem(pItemChild);
pParent->InsertChild(pChild, i);
}
}
}
}
QTreeWidget::rowsInserted(parent,start,end);
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end )
{
if (!m_bBuildingTree)
{
QTreeWidgetItem* pItemParent = itemFromIndex(parent);
imNode* pParent = GetNodeFromItem(pItemParent);
for (imInt i = start; i <= end; ++i)
{
QTreeWidgetItem* pItemChild = pItemParent->child(i);
imNode* pChild = GetNodeFromItem(pItemChild);
pParent->RemoveChild(i, imFALSE);
}
}
QTreeWidget::rowsAboutToBeRemoved(parent,start,end);
}
//////////////////////////////////////////////////////////////////////////
QMimeData *imGuiSceneExplorer::mimeData(const QList<QTreeWidgetItem *> items) const
{
imAssert(items.size());
if (items.size())
{
imQMimeData *mimeData = imNew imQMimeData();
imNode* pNode = *(imNode**)items[0]->data(0, Qt::UserRole).toByteArray().constData();
mimeData->SetNode(pNode);
mimeData->SetItem(items[0]);
mimeData->SetOldIndex(items[0]->parent() ? items[0]->parent()->indexOfChild(items[0]) : 0);
return mimeData;
}
return imNULL;
}
//////////////////////////////////////////////////////////////////////////
bool imGuiSceneExplorer::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
if (action == Qt::IgnoreAction)
return true;
if (parent)
{
const imQMimeData* myData = static_cast<const imQMimeData *>(data);
if (myData && parent != myData->GetItem())
{
imNode* pNode = myData->GetNode();
AddNodeItem(pNode, parent, index);
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::dragEnterEvent ( QDragEnterEvent * event )
{
QTreeWidget::dragEnterEvent(event);
event->accept();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::dragMoveEvent ( QDragMoveEvent * event )
{
QTreeWidget::dragMoveEvent(event);
event->accept();
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::paintEvent( QPaintEvent* event )
{
QTreeWidget::paintEvent (event);
}
//////////////////////////////////////////////////////////////////////////
void imGuiSceneExplorer::OnItemSelected()
{
// do something
}
//////////////////////////////////////////////////////////////////////////
To copy to clipboard, switch view to plain text mode
Bookmarks