/* tree.cc */ #include "tree.h" #include #include /* ---------------------------------------------------------------------- */ #ifdef TREE_MODEL TreeModel::TreeModel (Area * root, QObject * parent) : QAbstractItemModel(parent), rootItem (root) { } TreeModel::~TreeModel () { } int TreeModel::columnCount (const QModelIndex & parent) const { return 1; } QVariant TreeModel::data (const QModelIndex & index, int role) const { if (! index.isValid ()) return QVariant (); if (role != Qt::DisplayRole) return QVariant (); Area * item = static_cast (index.internalPointer ()); return item->getTitle (); } QVariant TreeModel::headerData (int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return "area title"; return QVariant (); } QModelIndex TreeModel::index (int row, int column, const QModelIndex & parent) const { if (! hasIndex (row, column, parent)) return QModelIndex (); Area * parentItem; if (! parent.isValid ()) parentItem = rootItem; else parentItem = static_cast (parent.internalPointer ()); Area * childItem = NULL; if (parentItem != NULL) { int pos = 0; childItem = parentItem->getFirstItem (); while (pos < row && childItem != NULL) { childItem = childItem->getNextItem (); pos ++; } } if (childItem != NULL) return createIndex (row, column, childItem); else return QModelIndex (); } QModelIndex TreeModel::parent (const QModelIndex & index) const { if (!index.isValid ()) return QModelIndex (); Area * childItem = static_cast (index.internalPointer ()); Area * parentItem = childItem->getAboveArea (); if (parentItem == NULL || parentItem == rootItem) return QModelIndex (); int pos = 0; Area * item = childItem; while (item != NULL) { item = item->getPrevItem (); pos ++; } if (pos > 0 ) pos --; return createIndex (pos, 0, parentItem); } int TreeModel::rowCount (const QModelIndex & parent) const { Area * parentItem; if (parent.column () > 0) return 0; if (! parent.isValid ()) parentItem = rootItem; else parentItem = static_cast (parent.internalPointer ()); int cnt = 0; if (parentItem != NULL) { Area * item = parentItem->getFirstItem (); while (item != NULL) { cnt ++; item = item->getNextItem (); } } return cnt; } Qt::ItemFlags TreeModel::flags (const QModelIndex & index) const { if (! index.isValid ()) // return 0; return Qt::ItemIsDropEnabled; return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled ; } Qt::DropActions TreeModel::supportedDropActions () const { return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction; } QStringList TreeModel::mimeTypes() const { QStringList types; types << "application/x-color"; return types; } bool TreeModel::dropMimeData (const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) { bool ok = false; if (data->hasColor ()) { QColor color = qVariantValue (data->colorData()); if (parent.isValid ()) { Area * item = static_cast (parent.internalPointer ()); item->setBrush (color); ok = true; } } return ok; } #endif /* ---------------------------------------------------------------------- */ #ifdef TREE_MODEL MyTree::MyTree (QWidget* parent) : QTreeView (parent) { // setSelectionMode (SingleSelection); // setDragEnabled (true); setAcceptDrops (true); setDropIndicatorShown (true); // setDragDropMode (DragDropMode (DragDrop | InternalMove)); // setDragDropMode (InternalMove); } void MyTree::setRoot (Area * root) { TreeModel * model = new TreeModel (root); setModel (model); } #endif /* ---------------------------------------------------------------------- */ #ifndef TREE_MODEL MyTree::MyTree (QWidget* parent) : QTreeWidget (parent) { // setSelectionMode (SingleSelection); // setDragEnabled (true); setAcceptDrops (true); setDropIndicatorShown (true); setDragDropMode (DragDropMode (DragDrop)); // setDragDropMode (DragDropMode (DragDrop | InternalMove)); // setDragDropMode (InternalMove); /* this->setColumnCount (1); QList items; for (int i = 0; i < 10; ++i) items.append (new QTreeWidgetItem ( (QTreeWidget*) 0, QStringList (QString ("item: %1").arg (i)))); this->insertTopLevelItems (0, items); */ } // QMimeData * MyTree::mimeData (const QList items ) const; QStringList MyTree::mimeTypes () const { QStringList types; types << "application/x-color"; return types; } Qt::DropActions MyTree::supportedDropActions () const { return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction; } bool MyTree::dropMimeData (QTreeWidgetItem * item, int index, const QMimeData * data, Qt::DropAction action) { bool ok = false; if (data->hasColor ()) { QColor color = qVariantValue (data->colorData()); MyTreeNode * node = dynamic_cast (item); if (node != NULL) { node->area->setBrush (color); ok = true; } } return ok; } void MyTree::showBranch (MyTreeNode * branch, Area * list) { Area * item = list->getFirstItem (); while (item != NULL) { MyTreeNode * node = new MyTreeNode; node->area = item; node->setText (0, item->getTitle ()); showBranch (node, item); // sub-items if (branch == NULL) addTopLevelItem (node); else branch->addChild (node); item = item->getNextItem (); } } void MyTree::setRoot (Area * root) { clear (); rootItem = root; if (rootItem != NULL) showBranch (NULL, rootItem); } #endif /* ---------------------------------------------------------------------- */