From a2dc6d96b6d1aa6621c012fa2811275af0859857 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Thu, 7 Dec 2017 18:47:53 +0300 Subject: [PATCH] Remove item from Favorites if one already exist while click on star button in main window --- favoritespanewidget.cc | 37 +++++++++++++++++++++++++++- favoritespanewidget.hh | 6 +++++ mainwindow.cc | 55 +++++++++++++++++++++++++++++++++++++++--- mainwindow.hh | 2 ++ 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/favoritespanewidget.cc b/favoritespanewidget.cc index ded7a2a9..30b67ac3 100644 --- a/favoritespanewidget.cc +++ b/favoritespanewidget.cc @@ -254,6 +254,11 @@ void FavoritesPaneWidget::addHeadword( QString const & path, QString const & hea m_favoritesModel->addNewHeadword( path, headword ); } +bool FavoritesPaneWidget::removeHeadword( QString const & path, QString const & headword ) +{ + return m_favoritesModel->removeHeadword( path, headword ); +} + bool FavoritesPaneWidget::isHeadwordPresent( const QString & path, const QString & headword ) { return m_favoritesModel->isHeadwordPresent( path, headword ); @@ -978,11 +983,41 @@ bool FavoritesModel::addNewHeadword( const QString & path, const QString & headw return addHeadword( headword, parentIdx ); } +bool FavoritesModel::removeHeadword( const QString & path, const QString & headword ) +{ + QModelIndex idx; + + // Find target folder + + QStringList folders = path.split( "/", QString::SkipEmptyParts ); + QStringList::const_iterator it = folders.begin(); + for( ; it != folders.end(); ++it ) + { + idx = findItemInFolder( *it, TreeItem::Folder, idx ); + if( !idx.isValid() ) + break; + } + + if( path.isEmpty() || idx.isValid() ) + { + idx = findItemInFolder( headword, TreeItem::Word, idx ); + if( idx.isValid() ) + { + QModelIndexList list; + list.append( idx ); + removeItemsForIndexes( list ); + return true; + } + } + + return false; +} + bool FavoritesModel::isHeadwordPresent( const QString & path, const QString & headword ) { QModelIndex idx; - // Find or create target folder + // Find target folder QStringList folders = path.split( "/", QString::SkipEmptyParts ); QStringList::const_iterator it = folders.begin(); diff --git a/favoritespanewidget.hh b/favoritespanewidget.hh index e2b00ce3..86e0dc98 100644 --- a/favoritespanewidget.hh +++ b/favoritespanewidget.hh @@ -40,6 +40,8 @@ public: void addHeadword( QString const & path, QString const & headword ); + bool removeHeadword( QString const & path, QString const & headword ); + // Export/import Favorites void getDataInXml( QByteArray & dataStr ); void getDataInPlainText( QString & dataStr ); @@ -195,6 +197,10 @@ public: // return false if it already exists there bool addNewHeadword( QString const & path, QString const & headword ); + // Remove headword from given folder + // return false if failed + bool removeHeadword( QString const & path, QString const & headword ); + // Return true if headwors is already presented in Favorites bool isHeadwordPresent( QString const & path, QString const & headword ); diff --git a/mainwindow.cc b/mainwindow.cc index f61c891b..77584d27 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -246,7 +246,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ): addToFavorites = navToolbar->addAction( starIcon, tr( "Add current tab to Favorites" ) ); navToolbar->widgetForAction( addToFavorites )->setObjectName( "addToFavoritesButton" ); - connect( addToFavorites, SIGNAL( triggered() ), this, SLOT( addCurrentTabToFavorites() ) ); + connect( addToFavorites, SIGNAL( triggered() ), this, SLOT( handleAddToFavoritesButton() ) ); connect( ui.actionAddToFavorites, SIGNAL( triggered() ), this, SLOT( addCurrentTabToFavorites() ) ); beforeOptionsSeparator = navToolbar->addSeparator(); @@ -1725,7 +1725,16 @@ void MainWindow::titleChanged( ArticleView * view, QString const & title ) ui.tabWidget->setTabText( ui.tabWidget->indexOf( view ), escaped ); // Set icon for "Add to Favorites" action - addToFavorites->setIcon( isWordPresentedInFavorites( title, cfg.lastMainGroupId ) ? blueStarIcon : starIcon ); + if( isWordPresentedInFavorites( title, cfg.lastMainGroupId ) ) + { + addToFavorites->setIcon( blueStarIcon ); + addToFavorites->setToolTip( tr( "Remove current tab from Favorites" ) ); + } + else + { + addToFavorites->setIcon( starIcon ); + addToFavorites->setToolTip( tr( "Add current tab to Favorites" ) ); + } updateWindowTitle(); } @@ -1794,7 +1803,16 @@ void MainWindow::tabSwitched( int ) // Set icon for "Add to Favorites" action QString headword = ui.tabWidget->tabText( ui.tabWidget->currentIndex() ); - addToFavorites->setIcon( isWordPresentedInFavorites( unescapeTabHeader( headword ), cfg.lastMainGroupId ) ? blueStarIcon : starIcon ); + if( isWordPresentedInFavorites( unescapeTabHeader( headword ), cfg.lastMainGroupId ) ) + { + addToFavorites->setIcon( blueStarIcon ); + addToFavorites->setToolTip( tr( "Remove current tab from Favorites" ) ); + } + else + { + addToFavorites->setIcon( starIcon ); + addToFavorites->setToolTip( tr( "Add current tab to Favorites" ) ); + } } void MainWindow::tabMenuRequested(QPoint pos) @@ -4497,6 +4515,36 @@ void MainWindow::addCurrentTabToFavorites() ui.favoritesPaneWidget->addHeadword( folder, unescapeTabHeader( headword ) ); addToFavorites->setIcon( blueStarIcon ); + addToFavorites->setToolTip( tr( "Remove current tab from Favorites" ) ); +} + +void MainWindow::handleAddToFavoritesButton() +{ + QString folder; + Instances::Group const * igrp = groupInstances.findGroup( cfg.lastMainGroupId ); + if( igrp ) + folder = igrp->favoritesFolder; + QString headword = unescapeTabHeader( ui.tabWidget->tabText( ui.tabWidget->currentIndex() ) ); + + if( ui.favoritesPaneWidget->isHeadwordPresent( folder, headword ) ) + { + QMessageBox mb( QMessageBox::Question, "GoldenDict", tr( "Remove headword \"%1\" from Favorites?" ).arg( headword ), + QMessageBox::Yes | QMessageBox::No, this ); + if( mb.exec() == QMessageBox::Yes ) + { + if( ui.favoritesPaneWidget->removeHeadword( folder, headword ) ) + { + addToFavorites->setIcon( starIcon ); + addToFavorites->setToolTip( tr( "Add current tab to Favorites" ) ); + } + } + } + else + { + ui.favoritesPaneWidget->addHeadword( folder, headword ); + addToFavorites->setIcon( blueStarIcon ); + addToFavorites->setToolTip( tr( "Remove current tab from Favorites" ) ); + } } void MainWindow::addWordToFavorites( QString const & word, unsigned groupId ) @@ -4522,6 +4570,7 @@ void MainWindow::addAllTabsToFavorites() ui.favoritesPaneWidget->addHeadword( folder, unescapeTabHeader( headword ) ); } addToFavorites->setIcon( blueStarIcon ); + addToFavorites->setToolTip( tr( "Remove current tab from Favorites" ) ); } bool MainWindow::isWordPresentedInFavorites( QString const & word, unsigned groupId ) diff --git a/mainwindow.hh b/mainwindow.hh index d1cd1848..b681f9fa 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -283,6 +283,8 @@ private slots: void showFTSIndexingName( QString const & name ); + void handleAddToFavoritesButton(); + void addCurrentTabToFavorites(); void addAllTabsToFavorites();