BtTreeView::BtTreeView(QWidget *parent, BtTreeModel::TypeMasks type) : QTreeView(parent) { // Set some global properties that all the kids will use. setAllColumnsShowFocus(true); setContextMenuPolicy(Qt::CustomContextMenu); setRootIsDecorated(false); setDragEnabled(true); setAcceptDrops(true); setDropIndicatorShown(true); setSelectionMode(QAbstractItemView::ExtendedSelection); _type = type; _model = new BtTreeModel(this, _type); filter = new BtTreeFilterProxyModel(this, _type); filter->setSourceModel(_model); setModel(filter); filter->setDynamicSortFilter(true); setExpanded(findElement(0), true); setSortingEnabled(true); sortByColumn(0,Qt::AscendingOrder); resizeColumnToContents(0); // and one wee connection connect( _model, SIGNAL(expandFolder(BtTreeModel::TypeMasks, QModelIndex)), this, SLOT(expandFolder(BtTreeModel::TypeMasks, QModelIndex))); }
// The actual magic shouldn't be hard. Once we trap the signal, find the // recipe, remove it from the parent and add it to the target folder. // It is not easy. Indexes are ephemeral things. We MUST calculate the insert // index after we have removed the recipe. BAD THINGS happen otherwise. // void BtTreeModel::folderChanged(QString name) { BeerXMLElement* test = qobject_cast<BeerXMLElement*>(sender()); QModelIndex ndx, pIndex; bool expand = true; if ( ! test ) return; // Find it. ndx = findElement(test); if ( ! ndx.isValid() ) { Brewtarget::logW("folderChanged:: could not find element"); return; } pIndex = parent(ndx); // Get the parent // If the parent isn't valid, its the root if ( ! pIndex.isValid() ) pIndex = createIndex(0,0,rootItem->child(0)); int i = item(ndx)->childNumber(); // Remove it if ( ! removeRows(i, 1, pIndex) ) { Brewtarget::logW("folderChanged:: could not remove row"); return; } // Find the new parent // That's awkward, but dropping a folder prolly does need a the folder // created. QModelIndex newNdx = findFolder(test->folder(), rootItem->child(0), true); if ( ! newNdx.isValid() ) { newNdx = createIndex(0,0,rootItem->child(0)); expand = false; } BtTreeItem* local = item(newNdx); int j = local->childCount(); if ( ! insertRow(j,newNdx,test,_type) ) { Brewtarget::logW("folderChanged:: could not insert row"); return; } // If we have brewnotes, set them up here. if ( treeMask & RECIPEMASK ) addBrewNoteSubTree(qobject_cast<Recipe*>(test),j,local); if ( expand ) emit expandFolder(treeMask,newNdx); return; }