From abc6d30d7c9beebc292902f9a39f1914eb0f2c72 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Fri, 6 Feb 2009 17:04:11 +0000 Subject: [PATCH] + Implement saving and restoring of the last chosen groups. --- src/config.cc | 13 +++++++++++++ src/config.hh | 3 +++ src/groupcombobox.cc | 12 ++++++++++++ src/groupcombobox.hh | 4 ++++ src/mainwindow.cc | 36 ++++++++++++++++++++++++++++++++++++ src/mainwindow.hh | 2 ++ src/scanpopup.cc | 8 ++++++-- src/scanpopup.hh | 4 ++-- 8 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/config.cc b/src/config.cc index 6c82ad07..9f61ea25 100644 --- a/src/config.cc +++ b/src/config.cc @@ -135,6 +135,9 @@ Class load() throw( exError ) c.preferences.scanPopupModifiers = ( preferences.namedItem( "scanPopupModifiers" ).toElement().text().toULong() ); } + c.lastMainGroup = root.namedItem( "lastMainGroup" ).toElement().text(); + c.lastPopupGroup = root.namedItem( "lastPopupGroup" ).toElement().text(); + return c; } @@ -231,6 +234,16 @@ void save( Class const & c ) throw( exError ) preferences.appendChild( opt ); } + { + QDomElement opt = dd.createElement( "lastMainGroup" ); + opt.appendChild( dd.createTextNode( c.lastMainGroup ) ); + root.appendChild( opt ); + + opt = dd.createElement( "lastPopupGroup" ); + opt.appendChild( dd.createTextNode( c.lastPopupGroup ) ); + root.appendChild( opt ); + } + configFile.write( dd.toByteArray() ); } diff --git a/src/config.hh b/src/config.hh index cf23cfe4..14723411 100644 --- a/src/config.hh +++ b/src/config.hh @@ -44,6 +44,9 @@ struct Class Paths paths; Groups groups; Preferences preferences; + + QString lastMainGroup; // Last used group in main window + QString lastPopupGroup; // Last used group in popup window }; DEF_EX( exError, "Error with the program's configuration", std::exception ) diff --git a/src/groupcombobox.cc b/src/groupcombobox.cc index 7d883e73..44cb166d 100644 --- a/src/groupcombobox.cc +++ b/src/groupcombobox.cc @@ -26,3 +26,15 @@ void GroupComboBox::fill( Instances::Groups const & groups ) } } +void GroupComboBox::setCurrentGroup( QString const & gr ) +{ + for( int x = 0; x < count(); ++x ) + { + if ( itemText( x ) == gr ) + { + setCurrentIndex( x ); + break; + } + } +} + diff --git a/src/groupcombobox.hh b/src/groupcombobox.hh index b10e26ca..6faa989c 100644 --- a/src/groupcombobox.hh +++ b/src/groupcombobox.hh @@ -18,6 +18,10 @@ public: /// Fills combo-box with the given groups void fill( Instances::Groups const & ); + + /// Chooses the given group in the combobox. If there's no such group, + /// does nothing. + void setCurrentGroup( QString const & ); }; #endif diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 4eb9d896..e3907350 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -81,6 +81,9 @@ MainWindow::MainWindow(): connect( ui.preferences, SIGNAL( activated() ), this, SLOT( editPreferences() ) ); + connect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ), + this, SLOT( currentGroupChanged( QString const & ) ) ); + connect( ui.translateLine, SIGNAL( textChanged( QString const & ) ), this, SLOT( translateInputChanged( QString const & ) ) ); @@ -103,6 +106,12 @@ MainWindow::MainWindow(): show(); } +MainWindow::~MainWindow() +{ + // Save any changes in last chosen groups etc + Config::save( cfg ); +} + LoadDictionaries::LoadDictionaries( vector< string > const & allFiles_ ): allFiles( allFiles_ ) { @@ -295,6 +304,11 @@ void MainWindow::updateGroupList() ui.groupLabel->setText( haveGroups ? tr( "Look up in:" ) : tr( "Look up:" ) ); + // currentIndexChanged() signal is very trigger-happy. To avoid triggering + // it, we disconnect it while we're clearing and filling back groups. + disconnect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ), + this, SLOT( currentGroupChanged( QString const & ) ) ); + { DictLock _; @@ -305,6 +319,10 @@ void MainWindow::updateGroupList() } ui.groupList->fill( groupInstances ); + ui.groupList->setCurrentGroup( cfg.lastMainGroup ); + + connect( ui.groupList, SIGNAL( currentIndexChanged( QString const & ) ), + this, SLOT( currentGroupChanged( QString const & ) ) ); } void MainWindow::makeScanPopup() @@ -449,6 +467,15 @@ void MainWindow::editPreferences() } } +void MainWindow::currentGroupChanged( QString const & gr ) +{ + cfg.lastMainGroup = gr; + + // Update word search results + + translateInputChanged( ui.translateLine->text() ); +} + void MainWindow::translateInputChanged( QString const & newValue ) { QString req = newValue.trimmed(); @@ -500,7 +527,10 @@ void MainWindow::prefixMatchComplete( WordFinderResults r ) } if ( ui.wordList->count() ) + { ui.wordList->scrollToItem( ui.wordList->item( 0 ), QAbstractItemView::PositionAtTop ); + ui.wordList->setCurrentItem( 0, QItemSelectionModel::Clear ); + } ui.wordList->setUpdatesEnabled( true ); ui.wordList->unsetCursor(); @@ -614,6 +644,12 @@ void MainWindow::trayIconActivated( QSystemTrayIcon::ActivationReason ) { if ( !isVisible() ) show(); + else + if ( isMinimized() ) + { + showNormal(); + activateWindow(); + } else hide(); } diff --git a/src/mainwindow.hh b/src/mainwindow.hh index 892ffafc..3d28b10b 100644 --- a/src/mainwindow.hh +++ b/src/mainwindow.hh @@ -56,6 +56,7 @@ class MainWindow: public QMainWindow public: MainWindow(); + ~MainWindow(); private: @@ -113,6 +114,7 @@ private slots: void editPreferences(); void indexingDictionary( QString dictionaryName ); + void currentGroupChanged( QString const & ); void translateInputChanged( QString const & ); void prefixMatchComplete( WordFinderResults ); void wordListItemActivated( QListWidgetItem * ); diff --git a/src/scanpopup.cc b/src/scanpopup.cc index bcfb3260..03a1e137 100644 --- a/src/scanpopup.cc +++ b/src/scanpopup.cc @@ -14,7 +14,7 @@ using std::wstring; ScanPopup::ScanPopup( QWidget * parent, - Config::Class const & cfg_, + Config::Class & cfg_, ArticleNetworkAccessManager & articleNetMgr, std::vector< sptr< Dictionary::Class > > const & allDictionaries_, Instances::Groups const & groups_ ): @@ -33,6 +33,8 @@ ScanPopup::ScanPopup( QWidget * parent, ui.prefixButton->hide(); ui.groupList->fill( groups ); + ui.groupList->setCurrentGroup( cfg.lastPopupGroup ); + setWindowFlags( Qt::Popup ); #ifdef Q_OS_WIN32 @@ -131,10 +133,12 @@ QString ScanPopup::elideInputWord() } -void ScanPopup::currentGroupChanged( QString const & ) +void ScanPopup::currentGroupChanged( QString const & gr ) { if ( isVisible() ) initiateTranslation(); + + cfg.lastPopupGroup = gr; } void ScanPopup::initiateTranslation() diff --git a/src/scanpopup.hh b/src/scanpopup.hh index ddb4ee3c..e4ec1855 100644 --- a/src/scanpopup.hh +++ b/src/scanpopup.hh @@ -22,14 +22,14 @@ class ScanPopup: public QDialog, KeyboardState public: ScanPopup( QWidget * parent, - Config::Class const & cfg, + Config::Class & cfg, ArticleNetworkAccessManager &, std::vector< sptr< Dictionary::Class > > const & allDictionaries, Instances::Groups const & ); private: - Config::Class const & cfg; + Config::Class & cfg; std::vector< sptr< Dictionary::Class > > const & allDictionaries; Instances::Groups const & groups; Ui::ScanPopup ui;