Remove item from Favorites if one already exist while click on star button in main window

This commit is contained in:
Abs62 2017-12-07 18:47:53 +03:00
parent f0e92940d4
commit a2dc6d96b6
4 changed files with 96 additions and 4 deletions

View file

@ -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();

View file

@ -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 );

View file

@ -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 )

View file

@ -283,6 +283,8 @@ private slots:
void showFTSIndexingName( QString const & name );
void handleAddToFavoritesButton();
void addCurrentTabToFavorites();
void addAllTabsToFavorites();