Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Kite 521c84
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/treemodel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qstringlist></qstringlist>
Toshihiro Shimizu 890ddd
#include <qtreeview></qtreeview>
Toshihiro Shimizu 890ddd
#include <qheaderview></qheaderview>
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qvariant.h></qvariant.h>
Toshihiro Shimizu 890ddd
#include <qicon.h></qicon.h>
Toshihiro Shimizu 890ddd
#include <qtextedit.h></qtextedit.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfx.h"
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//====================================================================================================
Toshihiro Shimizu 890ddd
// Item
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TreeModel::Item::Item()
Shinya Kitaoka 120a6e
    : m_model(0), m_parent(0), m_depth(0), m_row(0), m_opened(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TreeModel::Item::~Item() {
Shinya Kitaoka 120a6e
  qDeleteAll(m_childItems);
Shinya Kitaoka 120a6e
  m_childItems.clear();
Shinya Kitaoka 120a6e
  m_model  = 0;
Shinya Kitaoka 120a6e
  m_row    = 0;
Shinya Kitaoka 120a6e
  m_depth  = 0;
Shinya Kitaoka 120a6e
  m_parent = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::Item::updateChild(Item *child, int row) {
Shinya Kitaoka 120a6e
  assert(m_model);
Shinya Kitaoka 120a6e
  child->m_model  = m_model;
Shinya Kitaoka 120a6e
  child->m_depth  = m_depth + 1;
Shinya Kitaoka 120a6e
  child->m_parent = this;
Shinya Kitaoka 120a6e
  child->m_row    = row;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::Item::updateChildren() {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < m_childItems.size(); i++) updateChild(m_childItems[i], i);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TreeModel::Item *TreeModel::Item::appendChild(TreeModel::Item *child) {
Shinya Kitaoka 120a6e
  assert(child);
Shinya Kitaoka 120a6e
  assert(!m_childItems.contains(child));
Shinya Kitaoka 120a6e
  updateChild(child, m_childItems.size());
Shinya Kitaoka 120a6e
  m_childItems.append(child);
Shinya Kitaoka 120a6e
  return child;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void TreeModel::Item::deleteChild(Item *child)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  int index = m_childItems.indexOf(child);
Toshihiro Shimizu 890ddd
  if(index != -1)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    m_childItems.takeAt(index);
Shinya Kitaoka 120a6e
    assert(!m_childItems.contains(child));
Toshihiro Shimizu 890ddd
    // m_childItems is not supposed to contain duplicated entries
Toshihiro Shimizu 890ddd
    delete child;
Toshihiro Shimizu 890ddd
    updateChildren();
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
Item* matchItem(Item*item, QList<item*> &items)</item*>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  void *itemData = item->getInternalPointer();
Toshihiro Shimizu 890ddd
  if(!itemData) return 0;
Toshihiro Shimizu 890ddd
  int i;
Toshihiro Shimizu 890ddd
  for(i=0;i
Toshihiro Shimizu 890ddd
    if(items.at(i)->getInternalPointer()==itemData)
Toshihiro Shimizu 890ddd
      return items.at(i);
Toshihiro Shimizu 890ddd
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::Item::setChildren(QList<item *=""> &newChildren) {</item>
Shinya Kitaoka 120a6e
  assert(m_model);
Shinya Kitaoka 120a6e
  QModelIndex itemIndex = createIndex();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // save old children and clear 'm_childItems'
Shinya Kitaoka 120a6e
  QList<item *=""> oldChildren(m_childItems);</item>
Shinya Kitaoka 120a6e
  m_childItems.clear();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // for each child to add
Shinya Kitaoka 120a6e
  for (i = 0; i < newChildren.size(); i++) {
Shinya Kitaoka 120a6e
    Item *newChild           = newChildren.at(i);
Shinya Kitaoka 120a6e
    void *newInternalPointer = newChild->getInternalPointer();
Shinya Kitaoka 120a6e
    if (newInternalPointer != 0) {
Shinya Kitaoka 120a6e
      // search among old children
Shinya Kitaoka 120a6e
      int j;
Shinya Kitaoka 120a6e
      for (j = 0; j < oldChildren.size(); j++)
Shinya Kitaoka 120a6e
        if (oldChildren.at(j)->getInternalPointer() == newInternalPointer)
Shinya Kitaoka 120a6e
          break;
Shinya Kitaoka 120a6e
      if (j < oldChildren.size()) {
Shinya Kitaoka 120a6e
        Item *oldChild = oldChildren.takeAt(j);
Shinya Kitaoka 120a6e
        if (oldChild != newChild) {
Shinya Kitaoka 120a6e
          // Found! Delete newChild, remove it from 'newChildren' and
Shinya Kitaoka 120a6e
          // update consequently the index
Shinya Kitaoka 120a6e
          delete newChild;
Shinya Kitaoka 120a6e
          newChildren.takeAt(i);
Shinya Kitaoka 120a6e
          i--;
Shinya Kitaoka 120a6e
          // use the found child instead of the new one.
Shinya Kitaoka 120a6e
          newChild = oldChild;
Shinya Kitaoka 120a6e
          oldChild->refresh();
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          // should never happen; (if it happens this is not a big problem)
Shinya Kitaoka 120a6e
          assert(0);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // add the new child to 'm_childItems'
Shinya Kitaoka 120a6e
    m_childItems.push_back(newChild);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // update children model, row, parent, etc.
Shinya Kitaoka 120a6e
  updateChildren();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // postpone item deletion to avoid the (possible) reuse of the
Shinya Kitaoka 120a6e
  // same pointer for the newly created items. (Pointers match is
Shinya Kitaoka 120a6e
  // used updating persistent indices. see: TreeModel::endRefresh())
Shinya Kitaoka 120a6e
  for (i = 0; i < oldChildren.size(); i++) {
Shinya Kitaoka 120a6e
    Item *itemToDelete = oldChildren[i];
Shinya Kitaoka 120a6e
    if (!m_model->m_itemsToDelete.contains(itemToDelete))
Shinya Kitaoka 120a6e
      m_model->m_itemsToDelete.push_back(itemToDelete);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QVariant TreeModel::Item::data(int role) const {
Shinya Kitaoka 120a6e
  if (role == Qt::DecorationRole)
Kite 521c84
    return createQIcon("folder", true);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return QVariant();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QModelIndex TreeModel::Item::createIndex() {
Shinya Kitaoka 120a6e
  return m_parent ? m_model->createIndex(m_row, 0, this) : QModelIndex();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//====================================================================================================
Toshihiro Shimizu 890ddd
// TreeModel
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TreeModel::TreeModel(TreeView *parent)
Shinya Kitaoka 120a6e
    : QAbstractItemModel(parent), m_rootItem(0), m_view(parent) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TreeModel::~TreeModel() { delete m_rootItem; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::setExpandedItem(const QModelIndex &index, bool expanded) {
shun-iwasawa 4a3868
  if (m_view) m_view->setExpanded(index, expanded);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::beginRefresh() { emit layoutAboutToBeChanged(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::endRefresh() {
Shinya Kitaoka 120a6e
  QList<qmodelindex> oldIndices, newIndices;</qmodelindex>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  QList<item *="">::iterator it;</item>
Shinya Kitaoka 120a6e
shun-iwasawa 0d01d8
  // comment out as no subclass of TreeModel reimplement removeRows() for now
shun-iwasawa 0d01d8
  // and it causes assertion failure on calling beginRemoveRows() when deleting
shun-iwasawa 0d01d8
  // the last column in the xsheet
shun-iwasawa 0d01d8
  /*
Shinya Kitaoka 120a6e
  for (i = m_itemsToDelete.size() - 1; i >= 0; i--) {
Shinya Kitaoka 120a6e
    int row          = m_itemsToDelete[i]->getRow();
Shinya Kitaoka 120a6e
    Item *parentItem = m_itemsToDelete[i]->getParent();
Shinya Kitaoka 120a6e
    QModelIndex parentIndex =
Shinya Kitaoka 120a6e
        parentItem ? parentItem->createIndex() : QModelIndex();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    beginRemoveRows(parentIndex, row, row);
shun-iwasawa 0d01d8
    removeRows(row, 1, parentIndex);  // NOTE: This is currently doing NOTHING?
shun-iwasawa 0d01d8
  (see
Shinya Kitaoka 120a6e
                                  // Qt's manual)
Shinya Kitaoka 120a6e
    endRemoveRows();
shun-iwasawa 0d01d8
  }*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  qDeleteAll(m_itemsToDelete);
Shinya Kitaoka 120a6e
  m_itemsToDelete.clear();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!persistentIndexList().empty()) {
Shinya Kitaoka 120a6e
    for (i = 0; i < persistentIndexList().size(); i++) {
Shinya Kitaoka 120a6e
      QModelIndex oldIndex = persistentIndexList()[i];
Shinya Kitaoka 120a6e
      Item *item           = static_cast<item *="">(oldIndex.internalPointer());</item>
Shinya Kitaoka 120a6e
      if (item) {
Shinya Kitaoka 120a6e
        QModelIndex newIndex = item->createIndex();
Shinya Kitaoka 120a6e
        if (oldIndex != newIndex) {
Shinya Kitaoka 120a6e
          oldIndices.push_back(oldIndex);
Shinya Kitaoka 120a6e
          newIndices.push_back(newIndex);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    changePersistentIndexList(oldIndices, newIndices);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  emit layoutChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TreeModel::columnCount(const QModelIndex &parent) const { return 1; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
Shinya Kitaoka 120a6e
  if (!index.isValid()) return 0;
Shinya Kitaoka 120a6e
  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QModelIndex TreeModel::index(int row, int column,
Shinya Kitaoka 120a6e
                             const QModelIndex &parent) const {
Shinya Kitaoka 120a6e
  // column=!0 are not supported
Shinya Kitaoka 120a6e
  if (column != 0) return QModelIndex();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Item *parentItem =
Shinya Kitaoka 120a6e
      parent.isValid() ? (Item *)(parent.internalPointer()) : m_rootItem;
Shinya Kitaoka 120a6e
  // if m_rootItem has not been defined yet. (It should not happen, but just in
Shinya Kitaoka 120a6e
  // case)
Shinya Kitaoka 120a6e
  if (!parentItem) return QModelIndex();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int childCount = parentItem->getChildCount();
Shinya Kitaoka 120a6e
  if (row < 0 || row >= childCount) return QModelIndex();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Item *item = parentItem->getChild(row);
Shinya Kitaoka 120a6e
  if (!item) return QModelIndex();  // it should never happen
Shinya Kitaoka 120a6e
  return item->createIndex();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QModelIndex TreeModel::parent(const QModelIndex &index) const {
Shinya Kitaoka 120a6e
  if (!index.isValid()) return QModelIndex();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Item *item = (Item *)index.internalPointer();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TreeModel::Item *parentItem = item->getParent();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!parentItem || parentItem == m_rootItem) return QModelIndex();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return parentItem->createIndex();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TreeModel::rowCount(const QModelIndex &parent) const {
Shinya Kitaoka 120a6e
  if (parent.column() > 0) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!parent.isValid())
Shinya Kitaoka 120a6e
    return m_rootItem ? m_rootItem->getChildCount() : 0;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return ((Item *)(parent.internalPointer()))->getChildCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::onExpanded(const QModelIndex &index) {
Shinya Kitaoka 120a6e
  if (!index.isValid()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Item *item = (Item *)(index.internalPointer());
Shinya Kitaoka 120a6e
  item->setIsOpen(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::onCollapsed(const QModelIndex &index) {
Shinya Kitaoka 120a6e
  if (!index.isValid()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Item *item = (Item *)(index.internalPointer());
Shinya Kitaoka 120a6e
  item->setIsOpen(false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QVariant TreeModel::data(const QModelIndex &index, int role) const {
Shinya Kitaoka 120a6e
  if (!index.isValid()) return QVariant();
Shinya Kitaoka 120a6e
  Item *item = static_cast<item *="">(index.internalPointer());</item>
Shinya Kitaoka 120a6e
  return item->data(role);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::setRootItem(Item *rootItem) {
Shinya Kitaoka 120a6e
  if (rootItem == m_rootItem) return;
Shinya Kitaoka 120a6e
  delete m_rootItem;
Shinya Kitaoka 120a6e
  m_rootItem = rootItem;
Shinya Kitaoka 120a6e
  if (m_rootItem) m_rootItem->setModel(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton 6e785f
// postpone freeing, so existing items can be referenced while refreshing.
Campbell Barton 6e785f
void TreeModel::setRootItem_NoFree(Item *rootItem) {
Campbell Barton 6e785f
  if (rootItem == m_rootItem) return;
Campbell Barton 6e785f
  m_rootItem = rootItem;
Campbell Barton 6e785f
  if (m_rootItem) m_rootItem->setModel(this);
Campbell Barton 6e785f
}
Campbell Barton 6e785f
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeModel::setRowHidden(int row, const QModelIndex &parent, bool hide) {
shun-iwasawa 4a3868
  if (m_view) m_view->setRowHidden(row, parent, hide);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//====================================================================================================
Toshihiro Shimizu 890ddd
// TreeView
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TreeView::TreeView(QWidget *parent) : QTreeView(parent), m_dragging(false) {
Shinya Kitaoka 120a6e
  header()->hide();
Shinya Kitaoka 120a6e
  setUniformRowHeights(true);
Shinya Kitaoka 120a6e
  setIconSize(QSize(32, 32));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
//! Resizes viewport to contents
Shinya Kitaoka 120a6e
void TreeView::resizeToConts(void) { resizeColumnToContents(0); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeView::resizeEvent(QResizeEvent *event) {
Shinya Kitaoka 120a6e
  resizeColumnToContents(0);
Shinya Kitaoka 120a6e
  QTreeView::resizeEvent(event);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeView::setModel(TreeModel *model) {
Shinya Kitaoka 120a6e
  QTreeView::setModel(model);
Shinya Kitaoka 120a6e
  disconnect();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(expanded(const QModelIndex &)), model,
Shinya Kitaoka 120a6e
          SLOT(onExpanded(const QModelIndex &)));
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(collapsed(const QModelIndex &)), model,
Shinya Kitaoka 120a6e
          SLOT(onCollapsed(const QModelIndex &)));
Shinya Kitaoka 120a6e
  // setItemDelegate(new Delegate(this));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Connect all possible changes that can alter the
Shinya Kitaoka 120a6e
  // bottom horizontal scrollbar to resize contents...
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(expanded(const QModelIndex &)), this,
Shinya Kitaoka 120a6e
          SLOT(resizeToConts()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(collapsed(const QModelIndex &)), this,
Shinya Kitaoka 120a6e
          SLOT(resizeToConts()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  connect(this->model(), SIGNAL(layoutChanged()), this, SLOT(resizeToConts()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
//----------------------------------------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void TreeView::mouseDoubleClickEvent(QMouseEvent *) {
Shinya Kitaoka 120a6e
  // ignore double click!
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void TreeView::mousePressEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  if (e->button() != Qt::RightButton) QTreeView::mousePressEvent(e);
Shinya Kitaoka 120a6e
  QModelIndex index = indexAt(e->pos());
Shinya Kitaoka 120a6e
  if (index.isValid()) {
Shinya Kitaoka 120a6e
    TreeModel::Item *item =
Shinya Kitaoka 120a6e
        static_cast<treemodel::item *="">(index.internalPointer());</treemodel::item>
Shinya Kitaoka 120a6e
    QRect itemRect = visualRect(index);
Shinya Kitaoka 120a6e
    QPoint itemPos = e->pos() - itemRect.topLeft();
Shinya Kitaoka 120a6e
    if (e->button() == Qt::RightButton) {
Shinya Kitaoka 120a6e
      if (selectionMode() != QAbstractItemView::ExtendedSelection)
Shinya Kitaoka 120a6e
        setCurrentIndex(item->createIndex());
Shinya Kitaoka 120a6e
      onClick(item, itemPos, e);
Shinya Kitaoka 120a6e
      openContextMenu(item, e->globalPos());
Shinya Kitaoka 120a6e
    } else if (e->button() == Qt::LeftButton) {
Shinya Kitaoka 120a6e
      m_dragging = true;
Shinya Kitaoka 120a6e
      setMouseTracking(true);
Shinya Kitaoka 120a6e
      onClick(item, itemPos, e);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // for drag & drop
Shinya Kitaoka 120a6e
    else if (e->button() == Qt::MidButton) {
Shinya Kitaoka 120a6e
      m_dragging = true;
Shinya Kitaoka 120a6e
      setMouseTracking(true);
Shinya Kitaoka 120a6e
      onMidClick(item, itemPos, e);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeView::mouseMoveEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  QTreeView::mouseMoveEvent(e);
Shinya Kitaoka 120a6e
  if (m_dragging) {
Shinya Kitaoka 120a6e
    QModelIndex index = indexAt(e->pos());
Shinya Kitaoka 120a6e
    if (index.isValid()) {
Shinya Kitaoka 120a6e
      TreeModel::Item *item =
Shinya Kitaoka 120a6e
          static_cast<treemodel::item *="">(index.internalPointer());</treemodel::item>
Shinya Kitaoka 120a6e
      QRect itemRect = visualRect(index);
Shinya Kitaoka 120a6e
      QPoint itemPos = e->pos() - itemRect.topLeft();
Shinya Kitaoka 120a6e
      onDrag(item, itemPos, e);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TreeView::mouseReleaseEvent(QMouseEvent *e) {
Shinya Kitaoka 120a6e
  QTreeView::mouseReleaseEvent(e);
Shinya Kitaoka 120a6e
  if (m_dragging) {
Shinya Kitaoka 120a6e
    m_dragging = false;
Shinya Kitaoka 120a6e
    setMouseTracking(false);
Shinya Kitaoka 120a6e
    onRelease();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
bool TreeView::Delegate::editorEvent(QEvent *e, QAbstractItemModel
Shinya Kitaoka 120a6e
*abstractModel, const QStyleOptionViewItem &option, const QModelIndex &index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  if(e->type() != QEvent::MouseButtonPress) return false;
Toshihiro Shimizu 890ddd
  TreeModel *model = dynamic_cast<treemodel *="">(abstractModel);</treemodel>
Toshihiro Shimizu 890ddd
  if(!model || !index.isValid()) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TreeModel::Item *item = static_cast
Shinya Kitaoka 120a6e
*>(index.internalPointer());
Toshihiro Shimizu 890ddd
  QMouseEvent* mouseEvent = dynamic_cast<qmouseevent*>(e);</qmouseevent*>
Toshihiro Shimizu 890ddd
  QPoint pos = mouseEvent->pos();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_treeView->onClick(item, pos, option);
Toshihiro Shimizu 890ddd
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/