Deleting of items is now moved to interface.

This commit is contained in:
Martin Rotter 2015-11-06 08:25:37 +01:00
parent 39666e7bae
commit 5c0f777189
10 changed files with 90 additions and 51 deletions

View File

@ -208,27 +208,20 @@ int FeedsModel::rowCount(const QModelIndex &parent) const {
} }
} }
bool FeedsModel::removeItem(const QModelIndex &index) { void FeedsModel::removeItem(const QModelIndex &index) {
if (index.isValid()) { if (index.isValid()) {
QModelIndex parent_index = index.parent(); QModelIndex parent_index = index.parent();
RootItem *deleting_item = itemForIndex(index); RootItem *deleting_item = itemForIndex(index);
RootItem *parent_item = deleting_item->parent(); RootItem *parent_item = deleting_item->parent();
// Try to persistently remove the item. // Item was persistently removed.
if (deleting_item->removeItself()) { // Remove it from the model.
// Item was persistently removed. beginRemoveRows(parent_index, index.row(), index.row());
// Remove it from the model. parent_item->removeChild(deleting_item);
beginRemoveRows(parent_index, index.row(), index.row()); endRemoveRows();
parent_item->removeChild(deleting_item);
endRemoveRows();
delete deleting_item; delete deleting_item;
return true;
}
} }
// Item was not removed successfully.
return false;
} }
void FeedsModel::reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent) { void FeedsModel::reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent) {

View File

@ -61,7 +61,7 @@ class FeedsModel : public QAbstractItemModel {
// Removes item with given index. // Removes item with given index.
// NOTE: Also deletes item from memory. // NOTE: Also deletes item from memory.
bool removeItem(const QModelIndex &index); void removeItem(const QModelIndex &index);
// Checks if new parent node is different from one used by original node. // Checks if new parent node is different from one used by original node.
// If it is, then it reassigns original_node to new parent. // If it is, then it reassigns original_node to new parent.

View File

@ -78,7 +78,7 @@ class RootItem {
return false; return false;
} }
virtual bool editViaDialog() { virtual bool editViaGui() {
return false; return false;
} }
@ -90,6 +90,23 @@ class RootItem {
return false; return false;
} }
virtual bool canBeMarkedAsRead() {
return true;
}
virtual bool markAsRead() {
return true;
}
virtual bool canBeMarkedAsUnread() {
return true;
}
virtual bool markAsUnread() {
return true;
}
virtual int row() const; virtual int row() const;
virtual QVariant data(int column, int role) const; virtual QVariant data(int column, int role) const;
@ -98,16 +115,6 @@ class RootItem {
virtual int countOfUnreadMessages() const; virtual int countOfUnreadMessages() const;
virtual int countOfAllMessages() const; virtual int countOfAllMessages() const;
// This method is used to permanently
// "remove" (or "unregister") this item.
// This typically removes item and its
// "children" (for example messages or child feeds)
// from the database.
// Returns true if "I" was removed.
virtual bool removeItself() {
return false;
}
// Access to children. // Access to children.
inline QList<RootItem*> childItems() const { inline QList<RootItem*> childItems() const {
return m_childItems; return m_childItems;

View File

@ -247,7 +247,7 @@ void FeedsView::editSelectedItem() {
} }
if (selectedItem()->canBeEdited()) { if (selectedItem()->canBeEdited()) {
selectedItem()->editViaDialog(); selectedItem()->editViaGui();
} }
else { else {
qApp->showGuiMessage(tr("Cannot edit item"), qApp->showGuiMessage(tr("Cannot edit item"),
@ -282,24 +282,47 @@ void FeedsView::deleteSelectedItem() {
return; return;
} }
if (MessageBox::show(qApp->mainForm(), QMessageBox::Question, tr("Delete feed/category"), RootItem *selected_item = selectedItem();
tr("You are about to delete selected feed or category."), tr("Do you really want to delete selected item?"),
QString(), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No) {
// User changed his mind.
qApp->feedUpdateLock()->unlock();
return;
}
if (m_sourceModel->removeItem(m_proxyModel->mapToSource(current_index))) { if (selected_item != NULL) {
// Item WAS removed, update counts. if (selected_item->canBeDeleted()) {
notifyWithCounts(); // Ask user first.
} if (MessageBox::show(qApp->mainForm(),
else { QMessageBox::Question,
// Item WAS NOT removed, either database-related error occurred tr("Deleting \"%1\"").arg(selected_item->title()),
// or update is undergoing. tr("You are about to completely delete item \"%1\".").arg(selected_item->title()),
qApp->showGuiMessage(tr("Deletion of item failed."), tr("Are you sure?"),
tr("Selected item was not deleted due to error."), QString(), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No) {
QSystemTrayIcon::Warning, qApp->mainForm(), true); // User refused.
qApp->feedUpdateLock()->unlock();
return;
}
// We have deleteable item selected, remove it via GUI.
if (!selected_item->deleteViaGui()) {
qApp->showGuiMessage(tr("Cannot delete \"%1\"").arg(selected_item->title()),
tr("This item cannot be deleted because something critically failed. Submit bug report."),
QSystemTrayIcon::Critical,
qApp->mainForm(),
true);
}
else {
// Item is gone, cleared from database. We can clear it from model now.
// NOTE: Cleared from memory here too.
// TODO: možná toto přesunout taky to metody deleteViaGui
// a delete selected_item jen volat tady, editViaGui taky obstará všechno,
// ale tam je to zas komplexnější.
m_sourceModel->removeItem(m_proxyModel->mapToSource(current_index));
notifyWithCounts();
}
}
else {
qApp->showGuiMessage(tr("Cannot delete \"%1\"").arg(selected_item->title()),
tr("This item cannot be deleted, because it does not support it\nor this functionality is not implemented yet."),
QSystemTrayIcon::Critical,
qApp->mainForm(),
true);
}
} }
// Changes are done, unlock the update master lock. // Changes are done, unlock the update master lock.

View File

@ -28,6 +28,7 @@
#include "gui/feedsview.h" #include "gui/feedsview.h"
#include "services/standard/gui/formstandardcategorydetails.h" #include "services/standard/gui/formstandardcategorydetails.h"
#include "services/standard/standardserviceroot.h" #include "services/standard/standardserviceroot.h"
#include "services/standard/standardfeed.h"
#include <QVariant> #include <QVariant>
#include <QSqlQuery> #include <QSqlQuery>
@ -131,7 +132,7 @@ QVariant StandardCategory::data(int column, int role) const {
} }
} }
bool StandardCategory::editViaDialog() { bool StandardCategory::editViaGui() {
QPointer<FormStandardCategoryDetails> form_pointer = new FormStandardCategoryDetails(serviceRoot(), qApp->mainForm()); QPointer<FormStandardCategoryDetails> form_pointer = new FormStandardCategoryDetails(serviceRoot(), qApp->mainForm());
form_pointer.data()->exec(this, NULL); form_pointer.data()->exec(this, NULL);
@ -139,12 +140,21 @@ bool StandardCategory::editViaDialog() {
return false; return false;
} }
bool StandardCategory::deleteViaGui() {
return removeItself();
}
bool StandardCategory::removeItself() { bool StandardCategory::removeItself() {
bool children_removed = true; bool children_removed = true;
// Remove all child items (feeds, categories.) // Remove all child items (feeds, categories.)
foreach (RootItem *child, m_childItems) { foreach (RootItem *child, m_childItems) {
children_removed &= child->removeItself(); if (child->kind() == RootItemKind::Category) {
children_removed &= static_cast<StandardCategory*>(child)->removeItself();
}
else if (child->kind() == RootItemKind::Feed) {
children_removed &= static_cast<StandardFeed*>(child)->removeItself();
}
} }
if (children_removed) { if (children_removed) {

View File

@ -53,7 +53,8 @@ class StandardCategory : public Category {
return true; return true;
} }
bool editViaDialog(); bool editViaGui();
bool deleteViaGui();
// Removes category and all its children from persistent // Removes category and all its children from persistent
// database. // database.

View File

@ -103,7 +103,7 @@ StandardServiceRoot *StandardFeed::serviceRoot() {
return static_cast<StandardServiceRoot*>(getParentServiceRoot()); return static_cast<StandardServiceRoot*>(getParentServiceRoot());
} }
bool StandardFeed::editViaDialog() { bool StandardFeed::editViaGui() {
QPointer<FormStandardFeedDetails> form_pointer = new FormStandardFeedDetails(serviceRoot(), qApp->mainForm()); QPointer<FormStandardFeedDetails> form_pointer = new FormStandardFeedDetails(serviceRoot(), qApp->mainForm());
form_pointer.data()->exec(this, NULL); form_pointer.data()->exec(this, NULL);
@ -111,6 +111,10 @@ bool StandardFeed::editViaDialog() {
return false; return false;
} }
bool StandardFeed::deleteViaGui() {
return removeItself();
}
QList<Message> StandardFeed::undeletedMessages() const { QList<Message> StandardFeed::undeletedMessages() const {
QList<Message> messages; QList<Message> messages;

View File

@ -69,7 +69,8 @@ class StandardFeed : public Feed {
return true; return true;
} }
bool editViaDialog(); bool editViaGui();
bool deleteViaGui();
QList<Message> undeletedMessages() const; QList<Message> undeletedMessages() const;

View File

@ -33,7 +33,7 @@ TtRssServiceRoot::TtRssServiceRoot(FeedsModel *feeds_model, RootItem *parent) :
TtRssServiceRoot::~TtRssServiceRoot() { TtRssServiceRoot::~TtRssServiceRoot() {
} }
bool TtRssServiceRoot::editViaDialog() { bool TtRssServiceRoot::editViaGui() {
// TODO: zobrazit custom edit dialog pro ttrss // TODO: zobrazit custom edit dialog pro ttrss
return false; return false;
} }

View File

@ -34,7 +34,7 @@ class TtRssServiceRoot : public ServiceRoot {
bool canBeEdited(); bool canBeEdited();
bool canBeDeleted(); bool canBeDeleted();
bool editViaDialog(); bool editViaGui();
QVariant data(int column, int role) const; QVariant data(int column, int role) const;
}; };