From 1b99356a9b426f87194d16a0493bcbb8387025c1 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Sat, 18 Jan 2014 07:39:43 +0100 Subject: [PATCH] Initial impl. of adding. --- src/core/feedsmodel.cpp | 18 ++++++++++- src/core/feedsmodel.h | 2 ++ src/core/feedsmodelcategory.h | 3 +- src/core/feedsmodelrootitem.h | 4 +++ src/core/feedsmodelstandardcategory.cpp | 33 ++++++++++++++++++++ src/core/feedsmodelstandardcategory.h | 3 ++ src/core/feedsproxymodel.cpp | 3 +- src/gui/feedsview.cpp | 22 ++++++++----- src/gui/formcategorydetails.cpp | 41 ++++++++++++++++++++++--- src/gui/formcategorydetails.h | 7 +++-- src/gui/formcategorydetails.ui | 16 ---------- 11 files changed, 118 insertions(+), 34 deletions(-) diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 100745228..1c3d52864 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -133,6 +133,23 @@ int FeedsModel::rowCount(const QModelIndex &parent) const { return parent_item->childCount(); } +bool FeedsModel::addItem(FeedsModelRootItem *item, + FeedsModelRootItem *parent) { + QModelIndex parent_index = indexForItem(parent); + + beginInsertRows(parent_index, parent->childCount(), parent->childCount()); + + // Add item to hierarchy. + parent->appendChild(item); + + // Add item to persistent storage. + item->addItself(); + + endInsertRows(); + + return true; +} + bool FeedsModel::removeItem(const QModelIndex &index) { if (index.isValid()) { QModelIndex parent_index = index.parent(); @@ -329,7 +346,6 @@ void FeedsModel::loadFromDatabase() { } case FeedsModelCategory::Feedly: - case FeedsModelCategory::TinyTinyRss: default: // NOTE: Not yet implemented. break; diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index d9dd98da8..f2dfe9d45 100644 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -46,6 +46,8 @@ class FeedsModel : public QAbstractItemModel { } // Feed/category manipulators. + bool addItem(FeedsModelRootItem *item, + FeedsModelRootItem *parent); bool removeItem(const QModelIndex &index); // Returns (undeleted) messages for given feeds. diff --git a/src/core/feedsmodelcategory.h b/src/core/feedsmodelcategory.h index 74f145d81..60c0f3eb2 100755 --- a/src/core/feedsmodelcategory.h +++ b/src/core/feedsmodelcategory.h @@ -15,8 +15,7 @@ class FeedsModelCategory : public FeedsModelRootItem { // NOTE: This is equivavelnt to Categories(type). enum Type { Standard = 0, - Feedly = 1, - TinyTinyRss = 2 + Feedly = 1 }; // Constructors and destructors diff --git a/src/core/feedsmodelrootitem.h b/src/core/feedsmodelrootitem.h index 0fc2f920a..e736fef5d 100755 --- a/src/core/feedsmodelrootitem.h +++ b/src/core/feedsmodelrootitem.h @@ -57,6 +57,10 @@ class FeedsModelRootItem { virtual int countOfUnreadMessages() const; virtual int countOfAllMessages() const; + virtual bool addItself() { + return false; + } + // This method is used to permanently // "remove" (or "unregister") this item. // This typically removes item and its diff --git a/src/core/feedsmodelstandardcategory.cpp b/src/core/feedsmodelstandardcategory.cpp index 8eb1ada82..58faff7d2 100755 --- a/src/core/feedsmodelstandardcategory.cpp +++ b/src/core/feedsmodelstandardcategory.cpp @@ -91,6 +91,39 @@ QVariant FeedsModelStandardCategory::data(int column, int role) const { } } +bool FeedsModelStandardCategory::addItself() { + // Children are removed, remove this standard category too. + QSqlDatabase database = DatabaseFactory::instance()->connection(); + QSqlQuery query_add(database); + + query_add.setForwardOnly(true); + + // Remove all messages from this standard feed. + query_add.prepare("INSERT INTO Categories " + "(parent_id, title, description, date_created, icon, type) " + "VALUES (:parent_id, :title, :description, :date_created, :icon, :type);"); + query_add.bindValue(":parent_id", m_parentItem->id()); + query_add.bindValue(":title", m_title); + query_add.bindValue(":description", m_description); + query_add.bindValue(":date_created", m_creationDate.toMSecsSinceEpoch()); + query_add.bindValue(":icon", IconFactory::toByteArray(m_icon)); + query_add.bindValue(":type", (int) m_type); + + if (!query_add.exec()) { + return false; + } + + query_add.prepare("SELECT id FROM Categories WHERE date_created = :date_created;"); + query_add.bindValue(":date_created", m_creationDate.toMSecsSinceEpoch()); + if (query_add.exec() && query_add.next()) { + setId(query_add.value(0).toInt()); + return true; + } + else { + return false; + } +} + bool FeedsModelStandardCategory::removeItself() { bool result = true; diff --git a/src/core/feedsmodelstandardcategory.h b/src/core/feedsmodelstandardcategory.h index 2c0e9b16d..baa5e1898 100644 --- a/src/core/feedsmodelstandardcategory.h +++ b/src/core/feedsmodelstandardcategory.h @@ -21,6 +21,9 @@ class FeedsModelStandardCategory : public FeedsModelCategory { // Returns the actual data representation of standard category. QVariant data(int column, int role) const; + // Add itself to persistent database. + bool addItself(); + // Removes category and all its children from persistent // database. bool removeItself(); diff --git a/src/core/feedsproxymodel.cpp b/src/core/feedsproxymodel.cpp index 46cfba186..b47af4202 100755 --- a/src/core/feedsproxymodel.cpp +++ b/src/core/feedsproxymodel.cpp @@ -49,7 +49,8 @@ bool FeedsProxyModel::lessThan(const QModelIndex &left, else { // In other cases, sort by title. // TODO: mozna pouzit QString::localeAwareCompare tady. - return left_item->title() < right_item->title(); + return QString::compare(left_item->title(), + right_item->title()) < 0; } } else if (left_item->kind() == FeedsModelRootItem::Feed) { diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index 660097d0c..86382fa9a 100644 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -32,10 +32,6 @@ FeedsView::~FeedsView() { qDebug("Destroying FeedsView instance."); } - - - - void FeedsView::setSortingEnabled(bool enable) { QTreeView::setSortingEnabled(enable); header()->setSortIndicatorShown(false); @@ -75,7 +71,7 @@ void FeedsView::clearSelectedFeeds() { void FeedsView::addNewCategory() { QPointer form_pointer = new FormCategoryDetails(m_sourceModel, this); - FormCategoryDetailsAnswer answer = form_pointer.data()->exec(NULL, NULL); + FormCategoryDetailsAnswer answer = form_pointer.data()->exec(NULL); if (answer.m_dialogCode == QDialog::Accepted) { // User submitted some new category and @@ -104,7 +100,19 @@ void FeedsView::editSelectedItem() { } void FeedsView::deleteSelectedItem() { - m_sourceModel->removeItem(m_proxyModel->mapToSource(currentIndex())); + QModelIndex current_index = currentIndex(); + QItemSelectionModel *selection_model = selectionModel(); + + if (!current_index.isValid()) { + return; + } + + if (selection_model->selectedRows().size() > 1) { + selection_model->clearSelection(); + selection_model->select(current_index, QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent); + } + + m_sourceModel->removeItem(m_proxyModel->mapToSource(current_index)); } void FeedsView::markSelectedFeedsReadStatus(int read) { @@ -218,7 +226,7 @@ void FeedsView::setupAppearance() { setIndentation(10); setDragDropMode(QAbstractItemView::NoDragDrop); setAllColumnsShowFocus(true); - setSelectionMode(QAbstractItemView::SingleSelection); + setSelectionMode(QAbstractItemView::ExtendedSelection); setRootIsDecorated(false); // Sort in ascending order, that is categories are diff --git a/src/gui/formcategorydetails.cpp b/src/gui/formcategorydetails.cpp index fac118f78..45636b51a 100644 --- a/src/gui/formcategorydetails.cpp +++ b/src/gui/formcategorydetails.cpp @@ -3,22 +3,28 @@ #include "core/defs.h" #include "core/feedsmodelrootitem.h" #include "core/feedsmodelcategory.h" +#include "core/feedsmodelstandardcategory.h" #include "core/feedsmodel.h" #include "gui/iconthemefactory.h" #include "gui/feedsview.h" #include #include +#include #include +#include "QComboBox" FormCategoryDetails::FormCategoryDetails(FeedsModel *model, QWidget *parent) - : QDialog(parent), m_editableCategory(NULL) { + : QDialog(parent), + m_editableCategory(NULL), + m_feedsModel(model) { initialize(); loadCategories(model->allCategories().values(), model->rootItem()); - setWindowTitle(tr("Add new category")); + connect(m_ui->m_buttonBox, SIGNAL(accepted()), + this, SLOT(apply())); } FormCategoryDetails::~FormCategoryDetails() { @@ -35,15 +41,16 @@ void FormCategoryDetails::setEditableCategory(FeedsModelCategory *editable_categ m_ui->m_btnIcon->setIcon(editable_category->icon()); } -FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_category, - FeedsModelCategory *input_parent_category) { +FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_category) { FormCategoryDetailsAnswer answer; if (input_category == NULL) { // User is adding new category. + setWindowTitle(tr("Add new category")); } else { // User is editing existing category. + setWindowTitle(tr("Edit existing category")); setEditableCategory(input_category); } @@ -52,6 +59,30 @@ FormCategoryDetailsAnswer FormCategoryDetails::exec(FeedsModelCategory *input_ca return answer; } +void FormCategoryDetails::apply() { + if (m_editableCategory == NULL) { + // add category + FeedsModelRootItem *parent = static_cast(m_ui->m_cmbParentCategory->itemData(m_ui->m_cmbParentCategory->currentIndex()).value()); + FeedsModelStandardCategory *cat = new FeedsModelStandardCategory(); + + cat->setTitle(m_ui->m_txtTitle->text()); + cat->setCreationDate(QDateTime::currentDateTime()); + cat->setDescription(m_ui->m_txtDescription->toPlainText()); + cat->setIcon(m_ui->m_btnIcon->icon()); + cat->setType(FeedsModelCategory::Standard); + + if (m_feedsModel->addItem(cat, parent)) { + accept(); + } + else { + // TODO: hlasit chybu + } + } + else { + // edit category + } +} + void FormCategoryDetails::initialize() { m_ui = new Ui::FormCategoryDetails(); m_ui->setupUi(this); @@ -74,6 +105,6 @@ void FormCategoryDetails::loadCategories(const QList categ m_ui->m_cmbParentCategory->addItem(category->data(FDS_MODEL_TITLE_INDEX, Qt::DecorationRole).value(), category->title(), - category->id()); + QVariant::fromValue((void*) category)); } } diff --git a/src/gui/formcategorydetails.h b/src/gui/formcategorydetails.h index 785293d8d..76763dcdb 100644 --- a/src/gui/formcategorydetails.h +++ b/src/gui/formcategorydetails.h @@ -41,8 +41,10 @@ class FormCategoryDetails : public QDialog { // to the database. // NOTE: Newly EDITED category IS COPY of its original. // SO NO ORIGINAL MODEL DATA ARE EDITED OR CHANGED. - FormCategoryDetailsAnswer exec(FeedsModelCategory *input_category, - FeedsModelCategory *input_parent_category); + FormCategoryDetailsAnswer exec(FeedsModelCategory *input_category); + + protected slots: + void apply(); protected: // Sets the category which will be edited. @@ -59,6 +61,7 @@ class FormCategoryDetails : public QDialog { private: Ui::FormCategoryDetails *m_ui; FeedsModelCategory *m_editableCategory; + FeedsModel *m_feedsModel; }; #endif // FORMCATEGORYDETAILS_H diff --git a/src/gui/formcategorydetails.ui b/src/gui/formcategorydetails.ui index 24d3075d0..b7f71dbf0 100644 --- a/src/gui/formcategorydetails.ui +++ b/src/gui/formcategorydetails.ui @@ -108,22 +108,6 @@ - - m_buttonBox - accepted() - FormCategoryDetails - accept() - - - 248 - 254 - - - 157 - 274 - - - m_buttonBox rejected()