// Delete an item from the tree. We really just hide it. void NTagView::deleteRequested() { QList<QTreeWidgetItem*> items = selectedItems(); if (global.confirmDeletes()) { QMessageBox msgBox; msgBox.setIcon(QMessageBox::Question); if (items.size() == 1) msgBox.setText(tr("Are you sure you want to delete this tag?")); else msgBox.setText(tr("Are you sure you want to delete all selected tags?")); msgBox.setWindowTitle(tr("Verify Delete")); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); int ret = msgBox.exec(); if (ret == QMessageBox::No) return; } for (int i=0; i<items.size(); i++) { // Delete from the DB qint32 lid = items[i]->data(NAME_POSITION, Qt::UserRole).toInt(); TagTable table(global.db); table.deleteTag(lid); // Now remove selected tags NTagViewItem *ptr = dataStore.take(items[i]->data(NAME_POSITION, Qt::UserRole).toInt()); QList<QTreeWidgetItem*> children = ptr->takeChildren(); ptr->parent()->addChildren(children); ptr->parent()->removeChild(ptr); emit(tagDeleted(lid, ptr->data(NAME_POSITION, Qt::DisplayRole).toString())); delete ptr; } }
// If a tag has a zero count and if we should hide the tags, hide it. // Make sure the tag's parents are also visible if the child has a non-zero // count. void NTagView::hideUnassignedTags() { NTagViewItem *item; if (hideUnassignedAction->isChecked()) hideUnassigned = true; else hideUnassigned = false; // Save this option global.settings->beginGroup("SaveState"); global.settings->setValue("hideUnassigned", hideUnassigned); global.settings->endGroup(); QList<qint32> keys = dataStore.keys(); // Unhide everything if they don't want items hidden. if (hideUnassigned != true) { for (int i=0; i<keys.size(); i++) { item = dataStore[keys[i]]; if (item != NULL) { if (item->account == accountFilter) item->setHidden(false); else item->setHidden(true); } } resetSize(); return; } // Start hiding unassigned tags for (int i=0; i<keys.size(); i++) { item = dataStore[keys[i]]; if (item != NULL) { if (item->subTotal == 0 || item->account != accountFilter) item->setHidden(true); else item->setHidden(false); } } for (int i=0; i<keys.size(); i++) { item = dataStore[keys[i]]; if (item != NULL && !item->isHidden()) { while(item->parentLid > 0) { item->parent()->setHidden(false); item = (NTagViewItem*)item->parent(); } } } resetSize(); }
// Rebuild the GUI tree. void NTagView::rebuildTree() { if (!this->rebuildTagTreeNeeded) return; QHashIterator<qint32, NTagViewItem *> i(dataStore); TagTable tagTable(global.db); while (i.hasNext()) { i.next(); NTagViewItem *widget = i.value(); if (widget != NULL && widget->parentGuid != "") { if (widget->parentLid == 0) { widget->parentLid = tagTable.getLid(widget->parentGuid); } NTagViewItem *parent = dataStore[widget->parentLid]; widget->parent()->removeChild(widget); if (parent != NULL) { parent->childrenLids.append(i.key()); parent->addChild(widget); } } } this->sortByColumn(NAME_POSITION, Qt::AscendingOrder); this->rebuildTagTreeNeeded = false; this->resetSize(); }
// A tag is purged. void NTagView::tagExpunged(qint32 lid) { // Check if it already exists if (this->dataStore.contains(lid)) { NTagViewItem *item = this->dataStore.value(lid); item->parent()->removeChild(item); this->dataStore.remove(lid); } this->resetSize(); }
// Load up the data from the database void NTagView::loadData() { // Empty out the old data store QList<qint32> keys = dataStore.keys(); for (int i=0; i<keys.size(); i++) { if (dataStore.contains(keys[i])) { NTagViewItem *ptr = dataStore.take(keys[i]); dataStore.remove(keys[i]); if (ptr->parent() != NULL) ptr->parent()->removeChild(ptr); QLOG_DEBUG() << ptr; ptr->setHidden(true); // delete ptr; << We can leak memory, but otherwise it sometimes gets confused and causes crashes } } NSqlQuery query(global.db); TagTable tagTable(global.db); query.exec("Select lid, name, parent_gid, account from TagModel order by name"); while (query.next()) { qint32 lid = query.value(0).toInt(); QString name = query.value(1).toString(); QString parentGid = query.value(2).toString(); qint32 account = query.value(3).toInt(); NTagViewItem *newWidget = new NTagViewItem(); newWidget->setData(NAME_POSITION, Qt::DisplayRole, name); newWidget->setData(NAME_POSITION, Qt::UserRole, lid); newWidget->account = account; if (account != accountFilter) newWidget->setHidden(true); else newWidget->setHidden(false); this->dataStore.insert(lid, newWidget); newWidget->parentGuid = parentGid; newWidget->parentLid = tagTable.getLid(parentGid); root->addChild(newWidget); } query.finish(); this->rebuildTree(); }
// A tag has been updated. Things like a sync can cause this to be called // because a tag's name may have changed. void NTagView::tagUpdated(qint32 lid, QString name, QString parentGuid, qint32 account) { this->rebuildTagTreeNeeded = true; qint32 parentLid = 0; NTagViewItem *parentWidget = root; TagTable tagTable(global.db); // Check if it already exists and if its parent exists NTagViewItem *newWidget = NULL; if (this->dataStore.contains(lid) && dataStore[lid] != NULL) { newWidget = dataStore[lid]; if (newWidget->parent() != NULL) newWidget->parent()->removeChild(newWidget); } else { newWidget = new NTagViewItem(); newWidget->account = account; dataStore.remove(lid); dataStore.insert(lid, newWidget); } parentLid = tagTable.getLid(parentGuid); if (parentGuid != "") { if (parentLid > 0 && dataStore.contains(parentLid)) { parentWidget = dataStore[parentLid]; if (parentWidget == NULL) { parentWidget = new NTagViewItem(); parentWidget->account = account; if (account != this->accountFilter) parentWidget->setHidden(true); dataStore.remove(parentLid); dataStore.insert(parentLid, parentWidget); } } else { if (parentLid == 0) { Tag parentTag; parentTag.guid = parentGuid; parentTag.updateSequenceNum = 0; parentTag.name = parentGuid; parentLid = tagTable.add(0, parentTag, false, account); } parentWidget = new NTagViewItem(); root->addChild(parentWidget); parentWidget->setData(NAME_POSITION, Qt::UserRole, parentLid); parentWidget->setData(NAME_POSITION, Qt::DisplayRole, tr("-<Missing Tag>-")); dataStore.insert(parentLid, parentWidget); } } if (account != accountFilter) newWidget->setHidden(true); else newWidget->setHidden(false); parentWidget->addChild(newWidget); newWidget->setData(NAME_POSITION, Qt::DisplayRole, name); newWidget->setData(NAME_POSITION, Qt::UserRole, lid); newWidget->parentGuid = parentGuid; newWidget->parentLid = parentLid; newWidget->account = account; if (this->dataStore.count() == 1) { this->expandAll(); } resetSize(); this->sortByColumn(NAME_POSITION); }