// Add a new tag to the tree void NTagView::addRequested() { TagProperties dialog; QList<QTreeWidgetItem*> items = selectedItems(); dialog.setLid(0); dialog.exec(); if (!dialog.okPressed) return; TagTable table(global.db); NTagViewItem *newWidget = new NTagViewItem(); QString name = dialog.name.text().trimmed(); qint32 lid = table.findByName(name, accountFilter); newWidget->setData(NAME_POSITION, Qt::DisplayRole, name); newWidget->setData(NAME_POSITION, Qt::UserRole, lid); root->addChild(newWidget); this->sortItems(NAME_POSITION, Qt::AscendingOrder); resetSize(); this->sortByColumn(NAME_POSITION); // Now add it to the datastore dataStore.insert(lid, newWidget); emit(tagAdded(lid)); }
// 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(); }
// An edit is complete. Validate it is an acceptable tag void NTagView::editComplete() { QString text = editor->text().trimmed(); qint32 lid = editor->lid; TagTable table(global.db); Tag tag; table.get(tag, lid); QString oldName = ""; if (tag.name.isSet()) oldName = tag.name; // Check that this tag doesn't already exist // if it exists, we go back to the original name qint32 check = table.findByName(text, accountFilter); if (check != 0) { NTagViewItem *item = dataStore[lid]; QString tagname = ""; if (tag.name.isSet()) tagname = tag.name; item->setData(NAME_POSITION, Qt::DisplayRole, tagname); } else { tag.name = text; table.update(tag, true); } this->sortItems(NAME_POSITION, Qt::AscendingOrder); resetSize(); this->sortByColumn(NAME_POSITION); emit(tagRenamed(lid, oldName, text)); }
// 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); }