From e1e8f134e0b836925537e63f0504bdf1f091fa4a Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 17 Jan 2014 19:10:10 +0100 Subject: [PATCH] Some work on deleting, fix for external browser. --- src/core/feedsmodel.cpp | 46 +++++++++++---------------------- src/core/feedsmodel.h | 2 +- src/core/feedsmodelrootitem.cpp | 4 +++ src/core/feedsmodelrootitem.h | 32 +++++++++++++++++++++++ src/gui/feedmessageviewer.cpp | 6 ++--- src/gui/feedsview.cpp | 21 +++------------ src/gui/feedsview.h | 2 +- src/gui/formmain.cpp | 4 +-- src/gui/formmain.ui | 21 +++++---------- src/gui/messagesview.cpp | 8 +++++- 10 files changed, 74 insertions(+), 72 deletions(-) diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index f01cad2b7..100745228 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -133,42 +133,26 @@ int FeedsModel::rowCount(const QModelIndex &parent) const { return parent_item->childCount(); } -// TODO: přepsat tudle metodu, -// vim ze to zhruba funguje ale je potreba pridat taky -// vymazani feedu/kategorie z SQL (pridat metodu do FeedsModelRootItem -// kterou si podedi) -bool FeedsModel::removeItems(const QModelIndexList &indexes) { - QList items; - QList items_for_deletion; +bool FeedsModel::removeItem(const QModelIndex &index) { + if (index.isValid()) { + QModelIndex parent_index = index.parent(); + FeedsModelRootItem *deleting_item = itemForIndex(index); + FeedsModelRootItem *parent_item = itemForIndex(parent_index); - // Collect all items lying on given indexes. - foreach (const QModelIndex &index, indexes) { - FeedsModelRootItem *item = itemForIndex(index); + beginRemoveRows(parent_index, index.row(), index.row()); - if (item->kind() != FeedsModelRootItem::RootItem) { - items << item; + if (deleting_item->removeItself() && + parent_item->removeChild(deleting_item)) { + // Free deleted item from the memory + delete deleting_item; } + + endRemoveRows(); + + return true; } - // Remove given items from the model. - foreach (FeedsModelRootItem *item, items) { - QModelIndex index = indexForItem(item); - - if (index.isValid()) { - QModelIndex parent_index = index.parent(); - FeedsModelRootItem *parent_item = itemForIndex(parent_index); - - beginRemoveRows(parent_index, index.row(), index.row()); - if (item->removeItself() || parent_item->removeChild(index.row())) { - items_for_deletion << item; - } - endRemoveRows(); - } - } - - qDeleteAll(items_for_deletion); - - return true; + return false; } QList FeedsModel::messagesForFeeds(const QList &feeds) { diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index e6c6cca2f..d9dd98da8 100644 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -46,7 +46,7 @@ class FeedsModel : public QAbstractItemModel { } // Feed/category manipulators. - bool removeItems(const QModelIndexList &indexes); + bool removeItem(const QModelIndex &index); // Returns (undeleted) messages for given feeds. QList messagesForFeeds(const QList &feeds); diff --git a/src/core/feedsmodelrootitem.cpp b/src/core/feedsmodelrootitem.cpp index 60b80b562..c634d60ac 100755 --- a/src/core/feedsmodelrootitem.cpp +++ b/src/core/feedsmodelrootitem.cpp @@ -46,6 +46,10 @@ int FeedsModelRootItem::countOfAllMessages() const { return total_count; } +bool FeedsModelRootItem::removeChild(FeedsModelRootItem *child) { + return m_childItems.removeOne(child); +} + int FeedsModelRootItem::countOfUnreadMessages() const { int total_count = 0; diff --git a/src/core/feedsmodelrootitem.h b/src/core/feedsmodelrootitem.h index 315199a4f..0fc2f920a 100755 --- a/src/core/feedsmodelrootitem.h +++ b/src/core/feedsmodelrootitem.h @@ -72,6 +72,37 @@ class FeedsModelRootItem { return m_childItems; } + // Checks whether this instance is child (can be nested) + // if given root item. + bool isChild(FeedsModelRootItem *root) { + + while (root->kind() != FeedsModelRootItem::RootItem) { + if (root->childItems().contains(this)) { + return true; + } + else { + root = root->parent(); + } + } + + return false; +/* + parents << root; + + while (!parents.isEmpty()) { + foreach (FeedsModelRootItem *root_child, parents.takeFirst()->childItems()) { + if (root_child == this) { + return true; + } + else if (root_child->kind() == FeedsModelRootItem::Category) { + parents << root_child; + } + } + } + + return false;*/ + } + // Removes all children from this item. // NOTE: Children are NOT freed from the memory. inline void clearChildren() { @@ -81,6 +112,7 @@ class FeedsModelRootItem { // Removes particular child at given index. // NOTE: Child is NOT freed from the memory. bool removeChild(int index); + bool removeChild(FeedsModelRootItem *child); inline Kind kind() const { return m_kind; diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index a6c53782b..ebfba9eae 100644 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -223,8 +223,8 @@ void FeedMessageViewer::createConnections() { SIGNAL(triggered()), m_feedsView, SLOT(editSelectedItem())); connect(FormMain::instance()->m_ui->m_actionViewSelectedItemsNewspaperMode, SIGNAL(triggered()), m_feedsView, SLOT(openSelectedFeedsInNewspaperMode())); - connect(FormMain::instance()->m_ui->m_actionDeleteSelectedFeedsCategories, - SIGNAL(triggered()), m_feedsView, SLOT(deleteSelectedItems())); + connect(FormMain::instance()->m_ui->m_actionDeleteSelectedFeedCategory, + SIGNAL(triggered()), m_feedsView, SLOT(deleteSelectedItem())); } void FeedMessageViewer::initialize() { @@ -241,7 +241,7 @@ void FeedMessageViewer::initialize() { m_toolBar->addAction(FormMain::instance()->m_ui->m_actionUpdateSelectedFeedsCategories); m_toolBar->addAction(FormMain::instance()->m_ui->m_actionAddNewFeed); m_toolBar->addAction(FormMain::instance()->m_ui->m_actionEditSelectedFeedCategory); - m_toolBar->addAction(FormMain::instance()->m_ui->m_actionDeleteSelectedFeedsCategories); + m_toolBar->addAction(FormMain::instance()->m_ui->m_actionDeleteSelectedFeedCategory); m_toolBar->addAction(FormMain::instance()->m_ui->m_actionMarkFeedsAsRead); m_toolBar->addAction(FormMain::instance()->m_ui->m_actionMarkFeedsAsUnread); m_toolBar->addAction(FormMain::instance()->m_ui->m_actionClearFeeds); diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index e85153329..724dce7cf 100644 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -103,23 +103,8 @@ void FeedsView::editSelectedItem() { } -void FeedsView::deleteSelectedItems() { - QModelIndexList selection = selectionModel()->selectedRows(); - QModelIndexList mapped_selection = m_proxyModel->mapListToSource(selection); - - //FeedsModelRootItem *parent = m_sourceModel->itemForIndex(mapped_selection.at(0).parent()); - - m_sourceModel->removeItems(mapped_selection); - - /* - QModelIndex id = m_sourceModel->indexForItem(parent); - - if (id.isValid()) { - selectionModel()->clearSelection(); - selectionModel()->select(m_proxyModel->mapFromSource(id), - QItemSelectionModel::Rows | - QItemSelectionModel::Select); - }*/ +void FeedsView::deleteSelectedItem() { + m_sourceModel->removeItem(m_proxyModel->mapToSource(currentIndex())); } void FeedsView::markSelectedFeedsReadStatus(int read) { @@ -233,7 +218,7 @@ void FeedsView::setupAppearance() { setIndentation(10); setDragDropMode(QAbstractItemView::NoDragDrop); setAllColumnsShowFocus(true); - setSelectionMode(QAbstractItemView::ExtendedSelection); + setSelectionMode(QAbstractItemView::SingleSelection); setRootIsDecorated(false); // Sort in ascending order, that is categories are diff --git a/src/gui/feedsview.h b/src/gui/feedsview.h index 8b15289bc..6c9de5e38 100644 --- a/src/gui/feedsview.h +++ b/src/gui/feedsview.h @@ -71,7 +71,7 @@ class FeedsView : public QTreeView { // Category operators. void addNewCategory(); void editSelectedItem(); - void deleteSelectedItems(); + void deleteSelectedItem(); // Reloads counts for selected feeds. void updateCountsOfSelectedFeeds(bool update_total_too = true); diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index 679b70031..11b882310 100755 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -86,7 +86,7 @@ QList FormMain::allActions() { m_ui->m_actionUpdateAllFeeds << m_ui->m_actionUpdateSelectedFeedsCategories << m_ui->m_actionEditSelectedFeedCategory << - m_ui->m_actionDeleteSelectedFeedsCategories; + m_ui->m_actionDeleteSelectedFeedCategory; return actions; } @@ -222,7 +222,7 @@ void FormMain::setupIcons() { m_ui->m_actionUpdateAllFeeds->setIcon(IconThemeFactory::instance()->fromTheme("document-save-as")); m_ui->m_actionUpdateSelectedFeedsCategories->setIcon(IconThemeFactory::instance()->fromTheme("document-save")); m_ui->m_actionClearFeeds->setIcon(IconThemeFactory::instance()->fromTheme("mail-mark-junk")); - m_ui->m_actionDeleteSelectedFeedsCategories->setIcon(IconThemeFactory::instance()->fromTheme("edit-delete")); + m_ui->m_actionDeleteSelectedFeedCategory->setIcon(IconThemeFactory::instance()->fromTheme("edit-delete")); m_ui->m_actionDeleteSelectedMessages->setIcon(IconThemeFactory::instance()->fromTheme("mail-mark-junk")); m_ui->m_actionAddNewCategory->setIcon(IconThemeFactory::instance()->fromTheme("document-new")); m_ui->m_actionAddNewFeed->setIcon(IconThemeFactory::instance()->fromTheme("document-new")); diff --git a/src/gui/formmain.ui b/src/gui/formmain.ui index 618c2de77..b117b2cc1 100644 --- a/src/gui/formmain.ui +++ b/src/gui/formmain.ui @@ -15,16 +15,7 @@ - - 0 - - - 0 - - - 0 - - + 0 @@ -48,7 +39,7 @@ 0 0 979 - 21 + 20 @@ -100,7 +91,7 @@ - + @@ -251,12 +242,12 @@ Edit selected feed/category. - + - &Delete selected feeds/categories + &Delete selected feed/category - Delete selected feeds/categories. + Delete selected feed/category. diff --git a/src/gui/messagesview.cpp b/src/gui/messagesview.cpp index 2deb29982..0981c49b0 100644 --- a/src/gui/messagesview.cpp +++ b/src/gui/messagesview.cpp @@ -229,7 +229,13 @@ void MessagesView::openSelectedSourceArticlesExternally() { foreach (const QModelIndex &index, selectionModel()->selectedRows()) { QString link = m_sourceModel->messageAt(m_proxyModel->mapToSource(index).row()).m_url; - QProcess::execute(browser, QStringList() << arguments.arg(link)); + + if (!QProcess::startDetached(browser, QStringList() << arguments.arg(link))) { + QMessageBox::critical(this, + tr("Problem with starting external web browser"), + tr("External web browser could not be started."), + QMessageBox::Ok); + } } }