Added service root node classes and refactored editing of feeds/cats.

This commit is contained in:
Martin Rotter 2015-10-30 11:41:02 +01:00
parent d390ea2df6
commit fa373df15c
14 changed files with 147 additions and 61 deletions

View file

@ -252,33 +252,6 @@ bool FeedsModel::addCategory(StandardCategory *category, RootItem *parent) {
return result; return result;
} }
bool FeedsModel::editCategory(StandardCategory *original_category, StandardCategory *new_category_data) {
RootItem *original_parent = original_category->parent();
RootItem *new_parent = new_category_data->parent();
bool result = original_category->editItself(new_category_data);
if (result && original_parent != new_parent) {
// User edited category and set it new parent item,
// se we need to move the item in the model too.
int original_index_of_category = original_parent->childItems().indexOf(original_category);
int new_index_of_category = new_parent->childCount();
// Remove the original item from the model...
beginRemoveRows(indexForItem(original_parent), original_index_of_category, original_index_of_category);
original_parent->removeChild(original_category);
endRemoveRows();
// ...and insert it under the new parent.
beginInsertRows(indexForItem(new_parent), new_index_of_category, new_index_of_category);
new_parent->appendChild(original_category);
endInsertRows();
}
// Cleanup temporary new category data.
delete new_category_data;
return result;
}
bool FeedsModel::addFeed(StandardFeed *feed, RootItem *parent) { bool FeedsModel::addFeed(StandardFeed *feed, RootItem *parent) {
// Get index of parent item (parent standard category or root item). // Get index of parent item (parent standard category or root item).
QModelIndex parent_index = indexForItem(parent); QModelIndex parent_index = indexForItem(parent);
@ -297,30 +270,25 @@ bool FeedsModel::addFeed(StandardFeed *feed, RootItem *parent) {
return result; return result;
} }
bool FeedsModel::editFeed(StandardFeed *original_feed, StandardFeed *new_feed_data) { void FeedsModel::reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent) {
RootItem *original_parent = original_feed->parent(); RootItem *original_parent = original_node->parent();
RootItem *new_parent = new_feed_data->parent();
bool result = original_feed->editItself(new_feed_data);
if (result && original_parent != new_parent) { if (original_parent != new_parent) {
// User edited category and set it new parent item, // User edited category and set it new parent item,
// se we need to move the item in the model too. // se we need to move the item in the model too.
int original_index_of_feed = original_parent->childItems().indexOf(original_feed); int original_index_of_feed = original_parent->childItems().indexOf(original_node);
int new_index_of_feed = new_parent->childCount(); int new_index_of_feed = new_parent->childCount();
// Remove the original item from the model... // Remove the original item from the model...
beginRemoveRows(indexForItem(original_parent), original_index_of_feed, original_index_of_feed); beginRemoveRows(indexForItem(original_parent), original_index_of_feed, original_index_of_feed);
original_parent->removeChild(original_feed); original_parent->removeChild(original_node);
endRemoveRows(); endRemoveRows();
// ... and insert it under the new parent. // ... and insert it under the new parent.
beginInsertRows(indexForItem(new_parent), new_index_of_feed, new_index_of_feed); beginInsertRows(indexForItem(new_parent), new_index_of_feed, new_index_of_feed);
new_parent->appendChild(original_feed); new_parent->appendChild(original_node);
endInsertRows(); endInsertRows();
} }
delete new_feed_data;
return result;
} }
QList<Feed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) { QList<Feed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {

View file

@ -41,9 +41,6 @@ typedef QPair<int, StandardFeed*> FeedAssignmentItem;
class FeedsModel : public QAbstractItemModel { class FeedsModel : public QAbstractItemModel {
Q_OBJECT Q_OBJECT
friend class StandardFeed;
friend class StandardCategory;
public: public:
// Constructors and destructors. // Constructors and destructors.
explicit FeedsModel(QObject *parent = 0); explicit FeedsModel(QObject *parent = 0);
@ -75,15 +72,13 @@ class FeedsModel : public QAbstractItemModel {
// Standard category manipulators. // Standard category manipulators.
bool addCategory(StandardCategory *category, RootItem *parent); bool addCategory(StandardCategory *category, RootItem *parent);
bool editCategory(StandardCategory *original_category, StandardCategory *new_category_data);
// Standard feed manipulators. // Standard feed manipulators.
bool addFeed(StandardFeed *feed, RootItem *parent); bool addFeed(StandardFeed *feed, RootItem *parent);
// New feed is just temporary feed, it is not added to the model. // Checks if new parent node is different from one used by original node.
// It is used to fetch its data to the original feed // If it is, then it reassigns original_node to new parent.
// and the original feed is moved if needed. void reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent);
bool editFeed(StandardFeed *original_feed, StandardFeed *new_feed_data);
// Returns the list of feeds which should be updated // Returns the list of feeds which should be updated
// according to auto-update schedule. // according to auto-update schedule.

View file

@ -75,7 +75,7 @@ class RootItem {
return false; return false;
} }
virtual void edit() { virtual void editViaDialog() {
} }
virtual int row() const; virtual int row() const;

View file

@ -285,7 +285,7 @@ void FeedsView::editSelectedItem() {
} }
if (selectedItem()->canBeEdited()) { if (selectedItem()->canBeEdited()) {
selectedItem()->edit(); selectedItem()->editViaDialog();
} }
else { else {
qApp->showGuiMessage(tr("Cannot edit item"), qApp->showGuiMessage(tr("Cannot edit item"),

View file

@ -133,14 +133,19 @@ void FormStandardCategoryDetails::apply() {
} }
} }
else { else {
if (m_feedsModel->editCategory(m_editableCategory, new_category)) { bool edited = m_editableCategory->editItself(new_category);
if (edited) {
m_feedsModel->reassignNodeToNewParent(m_editableCategory, new_category->parent());
// Remove new temporary feed data holder object.
delete new_category;
accept(); accept();
} }
else { else {
qApp->showGuiMessage(tr("Cannot edit category"), qApp->showGuiMessage(tr("Cannot edit category"),
tr("Category was not edited due to error."), tr("Category was not edited due to error."),
QSystemTrayIcon::Critical, QSystemTrayIcon::Critical, this, true);
qApp->mainForm(), true);
} }
} }
} }

View file

@ -251,7 +251,13 @@ void FormStandardFeedDetails::apply() {
} }
else { else {
// Edit the feed. // Edit the feed.
if (m_feedsModel->editFeed(m_editableFeed, new_feed)) { bool edited = m_editableFeed->editItself(new_feed);
if (edited) {
m_feedsModel->reassignNodeToNewParent(m_editableFeed, new_feed->parent());
// Remove new temporary feed data holder object.
delete new_feed;
accept(); accept();
} }
else { else {
@ -264,8 +270,8 @@ void FormStandardFeedDetails::apply() {
void FormStandardFeedDetails::guessFeed() { void FormStandardFeedDetails::guessFeed() {
QPair<StandardFeed*, QNetworkReply::NetworkError> result = StandardFeed::guessFeed(m_ui->m_txtUrl->lineEdit()->text(), QPair<StandardFeed*, QNetworkReply::NetworkError> result = StandardFeed::guessFeed(m_ui->m_txtUrl->lineEdit()->text(),
m_ui->m_txtUsername->lineEdit()->text(), m_ui->m_txtUsername->lineEdit()->text(),
m_ui->m_txtPassword->lineEdit()->text()); m_ui->m_txtPassword->lineEdit()->text());
if (result.first != NULL) { if (result.first != NULL) {
// Icon or whole feed was guessed. // Icon or whole feed was guessed.
@ -308,8 +314,8 @@ void FormStandardFeedDetails::guessFeed() {
void FormStandardFeedDetails::guessIconOnly() { void FormStandardFeedDetails::guessIconOnly() {
QPair<StandardFeed*, QNetworkReply::NetworkError> result = StandardFeed::guessFeed(m_ui->m_txtUrl->lineEdit()->text(), QPair<StandardFeed*, QNetworkReply::NetworkError> result = StandardFeed::guessFeed(m_ui->m_txtUrl->lineEdit()->text(),
m_ui->m_txtUsername->lineEdit()->text(), m_ui->m_txtUsername->lineEdit()->text(),
m_ui->m_txtPassword->lineEdit()->text()); m_ui->m_txtPassword->lineEdit()->text());
if (result.first != NULL) { if (result.first != NULL) {
// Icon or whole feed was guessed. // Icon or whole feed was guessed.
@ -476,7 +482,7 @@ void FormStandardFeedDetails::initialize() {
} }
void FormStandardFeedDetails::loadCategories(const QList<StandardCategory*> categories, void FormStandardFeedDetails::loadCategories(const QList<StandardCategory*> categories,
RootItem *root_item) { RootItem *root_item) {
m_ui->m_cmbParentCategory->addItem(root_item->icon(), m_ui->m_cmbParentCategory->addItem(root_item->icon(),
root_item->title(), root_item->title(),
QVariant::fromValue((void*) root_item)); QVariant::fromValue((void*) root_item));

View file

@ -126,7 +126,7 @@ QVariant StandardCategory::data(int column, int role) const {
} }
} }
void StandardCategory::edit() { void StandardCategory::editViaDialog() {
// TODO: fix passing of the model // TODO: fix passing of the model
QPointer<FormStandardCategoryDetails> form_pointer = new FormStandardCategoryDetails(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel(), QPointer<FormStandardCategoryDetails> form_pointer = new FormStandardCategoryDetails(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel(),
qApp->mainForm()); qApp->mainForm());

View file

@ -50,7 +50,7 @@ class StandardCategory : public RootItem {
return true; return true;
} }
void edit(); void editViaDialog();
// Removes category and all its children from persistent // Removes category and all its children from persistent
// database. // database.

View file

@ -98,7 +98,7 @@ int StandardFeed::countOfUnreadMessages() const {
return m_unreadCount; return m_unreadCount;
} }
void StandardFeed::edit() { void StandardFeed::editViaDialog() {
// TODO: fix passing of the model // TODO: fix passing of the model
QPointer<FormStandardFeedDetails> form_pointer = new FormStandardFeedDetails(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel(), QPointer<FormStandardFeedDetails> form_pointer = new FormStandardFeedDetails(qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->sourceModel(),
qApp->mainForm()); qApp->mainForm());

View file

@ -66,7 +66,7 @@ class StandardFeed : public Feed {
return true; return true;
} }
void edit(); void editViaDialog();
// Obtains data related to this feed. // Obtains data related to this feed.
QVariant data(int column, int role) const; QVariant data(int column, int role) const;

View file

@ -0,0 +1,26 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "services/standard/standardserviceroot.h"
StandardServiceRoot::StandardServiceRoot(RootItem *parent) : ServiceRoot(parent) {
}
StandardServiceRoot::~StandardServiceRoot() {
}

View file

@ -0,0 +1,30 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef STANDARDSERVICEROOT_H
#define STANDARDSERVICEROOT_H
#include "services/abstract/serviceroot.h"
class StandardServiceRoot : public ServiceRoot {
public:
explicit StandardServiceRoot(RootItem *parent = NULL);
virtual ~StandardServiceRoot();
};
#endif // STANDARDSERVICEROOT_H

View file

@ -0,0 +1,26 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "services/tt-rss/ttrssserviceroot.h"
TtRssServiceRoot::TtRssServiceRoot(RootItem *parent) : ServiceRoot(parent) {
}
TtRssServiceRoot::~TtRssServiceRoot() {
}

View file

@ -0,0 +1,30 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef TTRSSSERVICEROOT_H
#define TTRSSSERVICEROOT_H
#include "services/abstract/serviceroot.h"
class TtRssServiceRoot : public ServiceRoot {
public:
explicit TtRssServiceRoot(RootItem *parent = NULL);
virtual ~TtRssServiceRoot();
};
#endif // TTRSSSERVICEROOT_H