// 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++) { NTagViewItem *ptr = dataStore.take(keys[i]); delete ptr; } 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(); }
// 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(); }
// 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); }