// Delete an item from the tree. We really just hide it. void NTagView::deleteRequested() { QList<QTreeWidgetItem*> items = selectedItems(); qint32 lid = items[0]->data(NAME_POSITION, Qt::UserRole).toInt(); if (global.confirmDeletes()) { QMessageBox msgBox; msgBox.setIcon(QMessageBox::Question); msgBox.setText(tr("Are you sure you want to delete this tag?")); msgBox.setWindowTitle(tr("Verify Delete")); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); int ret = msgBox.exec(); if (ret == QMessageBox::No) return; } TagTable table(global.db); table.deleteTag(lid); // NTagViewItem *ptr = (NTagViewItem*)items[0]; // ptr->setHidden(true); // Now remove it in the datastore NTagViewItem *ptr = dataStore.take(items[0]->data(NAME_POSITION, Qt::UserRole).toInt()); emit(tagDeleted(lid, ptr->data(NAME_POSITION, Qt::DisplayRole).toString())); delete ptr; }
// 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)); }
// 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(); }
// 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)); }
// 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(); }
// Delete an item from the tree. We really just hide it. void NTagView::mergeRequested() { QList<QTreeWidgetItem*> items = selectedItems(); QMessageBox msgBox; msgBox.setIcon(QMessageBox::Question); msgBox.setText(tr("Are you sure you want to merge these tags?")); msgBox.setWindowTitle(tr("Verify Merge")); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); int ret = msgBox.exec(); if (ret == QMessageBox::No) return; qint32 lid = items[0]->data(NAME_POSITION, Qt::UserRole).toInt(); NoteTable ntable(global.db); QList<qint32> notes; for (int j=1; j<items.size(); j++) { ntable.findNotesByTag(notes, items[j]->data(NAME_POSITION, Qt::UserRole).toInt()); for (int i=0; i<notes.size(); i++) { if (!ntable.hasTag(notes[i], lid)) { ntable.addTag(notes[i], lid, true); QString tagString = ntable.getNoteListTags(notes[i]); emit(updateNoteList(notes[i], NOTE_TABLE_TAGS_POSITION, tagString)); // qint64 dt = QDateTime::currentMSecsSinceEpoch(); // ntable.updateDate(notes[i], dt, NOTE_UPDATED_DATE, true); // emit(updateNoteList(notes[i], NOTE_TABLE_DATE_UPDATED_POSITION, dt)); } } } // Now delete the old tags. for (int i=1; i<items.size(); i++) { qint32 lid = items[i]->data(NAME_POSITION, Qt::UserRole).toInt(); TagTable table(global.db); table.deleteTag(lid); // Now remove it in the datastore NTagViewItem *ptr = dataStore.take(items[i]->data(NAME_POSITION, Qt::UserRole).toInt()); 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(); }
// 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); }