MainWindow.cpp

Go to the documentation of this file.
00001 #include "MainWindow.h"
00002 #include "icons/about.xpm"
00003 #include "icons/startQuiz.xpm"
00004 #include "icons/glossaryManager.xpm"
00005 #include "icons/import.xpm"
00006 #include "icons/export.xpm"
00007 #include "icons/preferences.xpm"
00008 #include "icons/quit.xpm"
00009 #include "icons/editcut.xpm"
00010 #include "icons/editcopy.xpm"
00011 #include "icons/editpaste.xpm"
00012 #include "icons/blueArrow.xpm"
00013 #include "icons/eye.xpm"
00014 #include "icons/goodAns.xpm"
00015 #include "icons/badAns.xpm"
00016 #include "icons/addTerm.xpm"
00017 #include "icons/editTerm.xpm"
00018 #include "icons/maximize.xpm"
00019 #include "icons/search.xpm"
00020 #include "icons/removeTerm.xpm"
00021 #include "icons/addFolder.xpm"
00022 #include "icons/addVocab.xpm"
00023 #include "icons/removeItem.xpm"
00024 #include "icons/checkAllTerms.xpm"
00025 #include "icons/inverseCheckedTerms.xpm"
00026 #include "icons/flag_en.xpm"
00027 #include "icons/flag_es.xpm"
00028 #include "icons/flag_fr.xpm"
00029 #include "icons/flag_ja.xpm"
00030 #include "icons/flag_zh.xpm"
00031 #include "icons/flag_de.xpm"
00032 
00033 MainWindow::MainWindow( Controller* controller )
00034     : QMainWindow( 0, "example application main window", WDestructiveClose ), control( controller ) {
00035     Preferences& prefs( controller->getPreferences() );
00036 
00037     translator = new QTranslator( this );
00038     translator->load( "toMOTko", QString( "/opt/Qtopia/i18n/" ) + prefs.getInterfaceLanguage() );
00039     qApp->installTranslator( translator );
00040 
00041     setToolBarsMovable( false );
00042 
00043     QPEToolBar* topBar = new QPEToolBar( this );
00044     topBar->setHorizontalStretchable( true );
00045 
00046     menuBar = new QPEMenuBar( topBar );
00047     menuBar->setMargin( 0 );
00048 
00049     toolBar = new QPEToolBar( this );
00050 
00051     languageSelectorPanel = new QHBox( toolBar, "LanguageSelectorPanel" );
00052     firstLanguageComboBox = new QComboBox( languageSelectorPanel, "FirstLanguageComboBox" );
00053     languageSelectorLabel = new QLabel( languageSelectorPanel, "LanguageSelectorLabel" );
00054     languageSelectorLabel->setPixmap( ZPIXMAP( blueArrow_xpm ) );
00055     testLanguageComboBox = new QComboBox( languageSelectorPanel, "TestLanguageComboBox" );
00056     updateFirstLanguageValues();
00057     updateTestLanguageValues();
00058     connect( firstLanguageComboBox, SIGNAL( activated( const QString& ) ), this, SLOT( setFirstLanguage( const QString& ) ) );
00059     connect( testLanguageComboBox, SIGNAL( activated( const QString& ) ), this, SLOT( setTestLanguage( const QString& ) ) );
00060 
00061     copyAction = Util::createAction( QObject::tr( "Copy" ), editcopy_xpm, this, SLOT( copy() ), CTRL + Key_C );
00062     cutAction = Util::createAction( QObject::tr( "Cut" ), editcut_xpm, this, SLOT( cut() ), CTRL + Key_X );
00063     pasteAction = Util::createAction( QObject::tr( "Paste" ), editpaste_xpm, this, SLOT( paste() ), CTRL + Key_V );
00064 
00065     progressBar = new QProgressBar( toolBar, "ProgressBar" );
00066     connect( controller, SIGNAL( progressChanged( int ) ), progressBar, SLOT( setProgress( int ) ) );
00067     progressBar->hide();
00068     progressBar->setCenterIndicator( true );
00069 
00070     setIcon( Resource::loadPixmap( QString( "toMOTko" ) ) ); 
00071 
00072     mainPanel = new QWidgetStack( this, "MainPanel" );
00073 
00074     quizFrame = new QuizFrame( control, mainPanel, "QuizFrame" );
00075 
00076     vocabManagerFrame = new VocabularyManagerFrame( control, mainPanel, "VocabularyManagerFrame" );
00077     vocabManagerFrame->setDigraphEnabled( prefs.isDigraphEnabled() ); 
00078 
00079     action[ ACTION_REVEAL ] = Util::createAction( tr( "Reveal" ), eye_xpm, 
00080         quizFrame, SLOT( reveal() ), prefs.getAccelerator( ACTION_REVEAL ) );
00081     action[ ACTION_RIGHT_ANSWER ] = Util::createAction( tr( "RightAnswer" ), goodAns_xpm, 
00082         quizFrame, SLOT( rightAnswer() ), prefs.getAccelerator( ACTION_RIGHT_ANSWER ) );
00083     action[ ACTION_WRONG_ANSWER ] = Util::createAction( tr( "WrongAnswer" ), badAns_xpm, 
00084         quizFrame, SLOT( wrongAnswer() ), prefs.getAccelerator( ACTION_WRONG_ANSWER ) );
00085     action[ ACTION_EDIT_QUIZ_TERM ] = Util::createAction( tr( "EditQuizTerm" ), editTerm_xpm, 
00086         quizFrame, SLOT( editCurrentTerm() ), prefs.getAccelerator( ACTION_EDIT_QUIZ_TERM ) );
00087     action[ ACTION_START_QUIZ ] = Util::createAction( tr( "StartQuiz" ), startQuiz_xpm, 
00088         this, SLOT( startQuiz() ), prefs.getAccelerator( ACTION_START_QUIZ ) );
00089     action[ ACTION_MANAGE_GLOSSARIES ] = Util::createAction( tr( "ManageGlossaries" ), glossaryManager_xpm, 
00090         this, SLOT( invokeVocabularyManager() ), prefs.getAccelerator( ACTION_MANAGE_GLOSSARIES ) );
00091     action[ ACTION_IMPORT ] = Util::createAction( tr( "Import..." ), import_xpm, 
00092         this, SLOT( importData() ), prefs.getAccelerator( ACTION_IMPORT ) );
00093     action[ ACTION_EXPORT ] = Util::createAction( tr( "Export..." ), export_xpm, 
00094         this, SLOT( exportData() ), prefs.getAccelerator( ACTION_EXPORT ) );
00095     action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ] = Util::createAction( tr( "ShowAllGlossariesAndTerms" ), NULL, 
00096         this, SLOT( toggleLanguageFilter() ), prefs.getAccelerator( ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ), true );
00097     action[ ACTION_PREFERENCES ] = Util::createAction( tr( "Preferences..." ), preferences_xpm, 
00098         this, SLOT( preferences() ), prefs.getAccelerator( ACTION_PREFERENCES ) );
00099     action[ ACTION_QUIT ] = Util::createAction( tr( "Quit" ), quit_xpm, 
00100         this, SLOT( quit() ), prefs.getAccelerator( ACTION_QUIT ) );
00101     action[ ACTION_ADD_FOLDER ] = Util::createAction( tr( "AddFolder" ), addFolder_xpm, 
00102         vocabManagerFrame, SLOT( addFolder() ), prefs.getAccelerator( ACTION_ADD_FOLDER ) );
00103     action[ ACTION_ADD_GLOSSARY ] = Util::createAction( tr( "AddGlossary" ), addVocab_xpm, 
00104         vocabManagerFrame, SLOT( addVocab() ), prefs.getAccelerator( ACTION_ADD_GLOSSARY ) );
00105     action[ ACTION_REMOVE_ITEM ] = Util::createAction( tr( "RemoveItem" ), removeItem_xpm, 
00106         vocabManagerFrame, SLOT( removeItem() ), prefs.getAccelerator( ACTION_REMOVE_ITEM ) );
00107     action[ ACTION_ADD_TERM ] = Util::createAction( tr( "AddTerm" ), addTerm_xpm, 
00108         vocabManagerFrame, SLOT( addTerm() ), prefs.getAccelerator( ACTION_ADD_TERM ) );
00109     action[ ACTION_EDIT_TERM ] = Util::createAction( tr( "EditTerm" ), editTerm_xpm, 
00110         vocabManagerFrame, SLOT( editTerm() ), prefs.getAccelerator( ACTION_EDIT_TERM ) );
00111     action[ ACTION_REMOVE_TERMS ] = Util::createAction( tr( "RemoveTerm" ), removeTerm_xpm, 
00112         vocabManagerFrame, SLOT( removeTerms() ), prefs.getAccelerator( ACTION_REMOVE_TERMS ) );
00113     action[ ACTION_CHECK_ALL_TERMS ] = Util::createAction( tr( "CheckAllTerms" ), checkAllTerms_xpm, 
00114         vocabManagerFrame, SLOT( checkAllTerms() ), prefs.getAccelerator( ACTION_CHECK_ALL_TERMS ) );
00115     action[ ACTION_INVERSE_CHECKED_TERMS ] = Util::createAction( tr( "InverseCheckedTerms" ), 
00116         inverseCheckedTerms_xpm, vocabManagerFrame, SLOT( inverseCheckedTerms() ), prefs.getAccelerator( ACTION_INVERSE_CHECKED_TERMS ) );
00117     action[ ACTION_MAXIMIZE ] = Util::createAction( tr( "Maximize" ), maximize_xpm, 
00118         this, SLOT( toggleMaximize( bool ) ), prefs.getAccelerator( ACTION_MAXIMIZE ), true );
00119     action[ ACTION_SEARCH ] = Util::createAction( tr( "Search..." ), search_xpm, 
00120         this, SLOT( search() ), prefs.getAccelerator( ACTION_SEARCH ) );
00121 
00122     actionsMenu = new QPopupMenu( this );
00123     actionsMenu->setCheckable( true );
00124     menuBar->insertItem( tr( "Actions" ), actionsMenu );
00125    
00126     action[ ACTION_START_QUIZ ]->addTo( actionsMenu );
00127 
00128     action[ ACTION_MANAGE_GLOSSARIES ]->addTo( actionsMenu );
00129 
00130     showAllVocabSeparatorId = actionsMenu->insertSeparator();
00131     action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->addTo( actionsMenu );
00132 
00133     actionsMenu->insertSeparator();
00134     action[ ACTION_IMPORT ]->addTo( actionsMenu );
00135     action[ ACTION_EXPORT ]->addTo( actionsMenu );
00136 
00137     actionsMenu->insertSeparator();
00138 
00139     action[ ACTION_PREFERENCES ]->addTo( actionsMenu );
00140 
00141     actionsMenu->insertSeparator();
00142 
00143     action[ ACTION_QUIT ]->addTo( actionsMenu );
00144 
00145     editionMenu = new QPopupMenu( this );
00146     cutAction->addTo( editionMenu );
00147     copyAction->addTo( editionMenu );
00148     pasteAction->addTo( editionMenu );
00149 
00150     editionMenu->insertSeparator();
00151 
00152     action[ ACTION_SEARCH ]->addTo( editionMenu );
00153 
00154     helpMenu = new QPopupMenu( this );
00155     menuBar->insertItem( tr( "?" ), helpMenu );
00156     helpMenu->insertItem( ZPIXMAP( about_xpm ), tr( "About..." ), this, SLOT( about() ) );
00157     helpMenu->insertSeparator( 1 );
00158     
00159     languageActionGroup = new QActionGroup( this ); 
00160 
00161     QDir i18nDir( QString( "/opt/Qtopia/i18n" ) );
00162     QStringList langDirs = i18nDir.entryList( QString( "*" ) );
00163     QStringList sortedLanguages;
00164     for( uint i = 0; i < langDirs.count(); i++ ) {
00165         if( langDirs[ i ] == QString( "." ) || langDirs[ i ] == QString( ".." ) )
00166             continue;
00167         QString locale = langDirs[ i ];
00168         QString langDirStr = QString( "/opt/Qtopia/i18n/" ) + QString( locale );
00169         QDir langDir( langDirStr );
00170         if( langDir.exists( QString( "toMOTko.qm" ) ) ) {
00171             QTranslator translator( this );
00172             translator.load( QString( "toMOTko.qm" ), langDirStr );
00173             QString language = translator.find( QString( "QObject" ), locale );
00174             sortedLanguages.append( language );
00175             availableLanguages[ language ] = locale;
00176         }
00177     }
00178     sortedLanguages.sort();
00179     for( uint i = 0; i < sortedLanguages.count(); i++ ) {
00180         QAction* langAction = new QAction( this );
00181         langAction->setToggleAction( true );
00182         langAction->setText( sortedLanguages[ i ] );
00183         
00184         // Refactor this if later.  Look at createAction() method for hint (maybe).
00185         QString langCode = availableLanguages[ sortedLanguages[ i ] ];
00186         if( langCode == QString( "en" ) )
00187             langAction->setIconSet( ZPIXMAP( flag_en_xpm ) );
00188         else if( langCode == QString( "es" ) )
00189             langAction->setIconSet( ZPIXMAP( flag_es_xpm ) );
00190         else if( langCode == QString( "fr" ) )
00191             langAction->setIconSet( ZPIXMAP( flag_fr_xpm ) );
00192         else if( langCode == QString( "ja" ) )
00193             langAction->setIconSet( ZPIXMAP( flag_ja_xpm ) );
00194         else if( langCode == QString( "zh" ) )
00195             langAction->setIconSet( ZPIXMAP( flag_zh_xpm ) );
00196         else if( langCode == QString( "de" ) )
00197             langAction->setIconSet( ZPIXMAP( flag_de_xpm ) );
00198 
00199         langAction->addTo( helpMenu );
00200         languageActionGroup->insert( langAction );
00201         if( controller->getPreferences().getInterfaceLanguage() == availableLanguages[ sortedLanguages[ i ] ] )
00202             langAction->setOn( true );
00203     }
00204     
00205     connect( quizFrame, SIGNAL( quizShown() ), progressBar, SLOT( show() ) );
00206     connect( quizFrame, SIGNAL( quizShown() ), languageSelectorPanel, SLOT( hide() ) );
00207     connect( quizFrame, SIGNAL( quizHidden() ), progressBar, SLOT( hide() ) );
00208     connect( quizFrame, SIGNAL( quizHidden() ), languageSelectorPanel, SLOT( show() ) );
00209     connect( quizFrame, SIGNAL( quizHidden() ), control, SLOT( concludeQuiz() ) );
00210 
00211     mainPanel->addWidget( quizFrame, frameQuizIndex );
00212     mainPanel->addWidget( vocabManagerFrame, frameVocabManagerIndex );
00213     connect( vocabManagerFrame, SIGNAL( selectionChanged( QListViewItem* ) ), this, SLOT( updateMenus( QListViewItem* ) ) );
00214     setCentralWidget( mainPanel );
00215     setLanguageFilterEnabled( controller->getPreferences().isLanguageFilterEnabled() );
00216     invokeVocabularyManager();
00217 
00218     setCaption( QString( "toMOTko" ) );
00219 
00220     updateFonts();
00221 
00222     connect( languageActionGroup, SIGNAL( selected( QAction* ) ), this, SLOT( switchLanguage( QAction* ) ) );
00223 }
00224 
00225 MainWindow::~MainWindow() {
00226     delete( control );
00227 }
00228 
00229 Controller* MainWindow::controller() {
00230     return( control );
00231 }
00232 
00233 void MainWindow::updateMenus( QListViewItem* ) {
00234     action[ ACTION_START_QUIZ ]->setText( mainPanel->visibleWidget() == quizFrame ? tr( "RestartQuiz" ) : tr( "StartQuiz" ) );
00235     action[ ACTION_START_QUIZ ]->setMenuText( mainPanel->visibleWidget() == quizFrame ? tr( "RestartQuiz" ) : tr( "StartQuiz" ) );
00236     action[ ACTION_MANAGE_GLOSSARIES ]->setEnabled( mainPanel->visibleWidget() != vocabManagerFrame ); 
00237     if( mainPanel->visibleWidget() == vocabManagerFrame ) {
00238         if( menuBar->indexOf( editionMenuId ) == -1 )
00239             editionMenuId = menuBar->insertItem( QObject::tr( "Edition" ), editionMenu, -1, 1 );
00240         action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->setEnabled( true );
00241         action[ ACTION_IMPORT ]->setEnabled( vocabManagerFrame->isImportAllowed() );
00242         action[ ACTION_EXPORT ]->setEnabled( vocabManagerFrame->isExportAllowed() );
00243     }
00244     else {
00245         if( editionMenu->parent() ) {
00246             if( menuBar->indexOf( editionMenuId ) != -1 )
00247                 menuBar->removeItem( editionMenuId );
00248             if( menuBar->indexOf( viewMenuId ) != -1 )
00249                 menuBar->removeItem( viewMenuId );
00250         }
00251         action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->setEnabled( false );
00252         action[ ACTION_IMPORT ]->setEnabled( false );
00253         action[ ACTION_EXPORT ]->setEnabled( false );
00254     }
00255 }
00256 
00257 void MainWindow::updateFonts() {
00258     QFont labelsFont( control->getPreferences().getLabelsFont() ); 
00259 
00260     qApp->setFont( labelsFont );
00261     firstLanguageComboBox->setFont( labelsFont );
00262     testLanguageComboBox->setFont( labelsFont );
00263     progressBar->setFont( labelsFont );
00264 
00265     quizFrame->updateFonts();
00266     vocabManagerFrame->updateFonts();
00267 }
00268 
00269 bool MainWindow::isDigraphEnabled() const {
00270     return( vocabManagerFrame->isDigraphEnabled() );
00271 }
00272 
00273 void MainWindow::setDigraphEnabled( bool isEnabled ) {
00274     vocabManagerFrame->setDigraphEnabled( isEnabled );
00275 }
00276 
00277 void MainWindow::retranslateUi() {
00278     menuBar->changeItem( menuBar->idAt( 0 ), tr( "Actions" ) );
00279     if( mainPanel->visibleWidget() == vocabManagerFrame )
00280         menuBar->changeItem( menuBar->idAt( 1 ), QObject::tr( "Edition" ) );
00281 
00282     // Could use a loop here.
00283     action[ ACTION_REVEAL ]->setText( tr( "Reveal" ) );
00284     action[ ACTION_REVEAL ]->setMenuText( tr( "Reveal" ) );
00285     action[ ACTION_RIGHT_ANSWER ]->setText( tr( "RightAnswer" ) );
00286     action[ ACTION_RIGHT_ANSWER ]->setMenuText( tr( "RightAnswer" ) );
00287     action[ ACTION_WRONG_ANSWER ]->setText( tr( "WrongAnswer" ) );
00288     action[ ACTION_WRONG_ANSWER ]->setMenuText( tr( "WrongAnswer" ) );
00289     action[ ACTION_EDIT_QUIZ_TERM ]->setText( tr( "EditQuizTerm" ) );
00290     action[ ACTION_EDIT_QUIZ_TERM ]->setMenuText( tr( "EditQuizTerm" ) );
00291     action[ ACTION_START_QUIZ ]->setText( tr( "StartQuiz" ) );
00292     action[ ACTION_START_QUIZ ]->setMenuText( tr( "StartQuiz" ) );
00293     action[ ACTION_MANAGE_GLOSSARIES ]->setText( tr( "ManageGlossaries" ) );
00294     action[ ACTION_MANAGE_GLOSSARIES ]->setMenuText( tr( "ManageGlossaries" ) );
00295     action[ ACTION_IMPORT ]->setText( tr( "Import..." ) );
00296     action[ ACTION_IMPORT ]->setMenuText( tr( "Import..." ) );
00297     action[ ACTION_EXPORT ]->setText( tr( "Export..." ) );
00298     action[ ACTION_EXPORT ]->setMenuText( tr( "Export..." ) );
00299     action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->setText( tr( "ShowAllGlossariesAndTerms" ) );
00300     action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->setMenuText( tr( "ShowAllGlossariesAndTerms" ) );
00301     action[ ACTION_PREFERENCES ]->setText( tr( "Preferences..." ) );
00302     action[ ACTION_PREFERENCES ]->setMenuText( tr( "Preferences..." ) );
00303     action[ ACTION_QUIT ]->setText( tr( "Quit" ) );
00304     action[ ACTION_QUIT ]->setMenuText( tr( "Quit" ) );
00305     action[ ACTION_ADD_FOLDER ]->setText( tr( "AddFolder" ) );
00306     action[ ACTION_ADD_FOLDER ]->setMenuText( tr( "AddFolder" ) );
00307     action[ ACTION_ADD_GLOSSARY ]->setText( tr( "AddGlossary" ) );
00308     action[ ACTION_ADD_GLOSSARY ]->setMenuText( tr( "AddGlossary" ) );
00309     action[ ACTION_REMOVE_ITEM ]->setText( tr( "RemoveItem" ) );
00310     action[ ACTION_REMOVE_ITEM ]->setMenuText( tr( "RemoveItem" ) );
00311     action[ ACTION_ADD_TERM ]->setText( tr( "AddTerm" ) );
00312     action[ ACTION_ADD_TERM ]->setMenuText( tr( "AddTerm" ) );
00313     action[ ACTION_EDIT_TERM ]->setText( tr( "EditTerm" ) );
00314     action[ ACTION_EDIT_TERM ]->setMenuText( tr( "EditTerm" ) );
00315     action[ ACTION_REMOVE_TERMS ]->setText( tr( "RemoveTerm" ) );
00316     action[ ACTION_REMOVE_TERMS ]->setMenuText( tr( "RemoveTerm" ) );
00317     action[ ACTION_CHECK_ALL_TERMS ]->setText( tr( "CheckAllTerms" ) );
00318     action[ ACTION_CHECK_ALL_TERMS ]->setMenuText( tr( "CheckAllTerms" ) );
00319     action[ ACTION_INVERSE_CHECKED_TERMS ]->setText( tr( "InverseCheckedTerms" ) );
00320     action[ ACTION_INVERSE_CHECKED_TERMS ]->setMenuText( tr( "InverseCheckedTerms" ) );
00321     action[ ACTION_MAXIMIZE ]->setText( tr( "Maximize" ) );
00322     action[ ACTION_MAXIMIZE ]->setMenuText( tr( "Maximize" ) );
00323     action[ ACTION_SEARCH ]->setText( tr( "Search..." ) );
00324     action[ ACTION_SEARCH ]->setMenuText( tr( "Search..." ) );
00325 
00326     cutAction->setText( QObject::tr( "Cut" ) );
00327     cutAction->setMenuText( QObject::tr( "Cut" ) );
00328     copyAction->setText( QObject::tr( "Copy" ) );
00329     copyAction->setMenuText( QObject::tr( "Copy" ) );
00330     pasteAction->setText( QObject::tr( "Paste" ) );
00331     pasteAction->setMenuText( QObject::tr( "Paste" ) );
00332 
00333     helpMenu->changeItem( helpMenu->idAt( 0 ), tr( "About..." ) );
00334     
00335     updateFirstLanguageValues();
00336     updateTestLanguageValues();
00337 
00338     quizFrame->retranslateUi();
00339     vocabManagerFrame->retranslateUi();
00340 }
00341 
00342 void MainWindow::switchLanguage( const QString& language ) {
00343     qApp->removeTranslator( translator );
00344     translator->load( "toMOTko", QString( "/opt/Qtopia/i18n/" ) + language );
00345     qApp->installTranslator( translator ); 
00346     retranslateUi();
00347 }
00348 
00349 void MainWindow::toggleMaximize( bool isOn ) {
00350     if( mainPanel->visibleWidget() == quizFrame )
00351         quizFrame->toggleMaximizeComment( isOn );
00352     else if( mainPanel->visibleWidget() == vocabManagerFrame )
00353         vocabManagerFrame->toggleMaximizeDetails( isOn );
00354 }
00355 
00356 void MainWindow::search() {
00357     if( mainPanel->visibleWidget() == vocabManagerFrame )
00358         vocabManagerFrame->search();
00359 }
00360 
00361 void MainWindow::switchLanguage( QAction* langAction ) {
00362     QString language = availableLanguages[ langAction->text() ];
00363     switchLanguage( language );
00364     control->getPreferences().setInterfaceLanguage( language );
00365 }
00366 
00367 void MainWindow::closeEvent( QCloseEvent* ce ) {
00368     bool isOk = vocabManagerFrame->saveData();
00369     if( !isOk )
00370         QMessageBox::warning( this, QObject::tr( "Error" ), QObject::tr( "ErrorSavingData" ) );
00371     ce->accept();
00372 }
00373 
00374 void MainWindow::about() {
00375     QMessageBox::about( this, tr( "AboutToMOTko..." ), tr( "AboutMessage" ) );
00376 }
00377 
00378 void MainWindow::startQuiz() {
00379     if( mainPanel->visibleWidget() == quizFrame ) 
00380         quizFrame->restartQuiz();
00381     else {
00382         bool resumeQuiz = false;
00383         if( control->isResumableQuizAvailable() ) {
00384             QMessageBox msgBox( QObject::tr( "Information" ), tr( "ConfirmResumeQuiz" ),
00385                 QMessageBox::Warning,
00386                 QMessageBox::Yes | QMessageBox::Default | QMessageBox::Escape,
00387                 QMessageBox::No,
00388                 QMessageBox::NoButton,
00389                 this );
00390             msgBox.setButtonText( QMessageBox::Yes, tr( "Yes" ) );
00391             msgBox.setButtonText( QMessageBox::No, tr( "No" ) );
00392         
00393             int response = msgBox.exec();
00394             resumeQuiz = ( response == QMessageBox::Yes );
00395         }
00396         showQuiz();
00397         if( resumeQuiz )
00398             quizFrame->resumeQuiz();
00399         else
00400             quizFrame->startQuiz();
00401     }
00402 }
00403 
00404 void MainWindow::showQuiz() {
00405     mainPanel->raiseWidget( frameQuizIndex );
00406     updateMenus( NULL );
00407     cutAction->removeFrom( toolBar );
00408     copyAction->removeFrom( toolBar );
00409     pasteAction->removeFrom( toolBar );
00410     quizFrame->setFocus();
00411 }
00412 
00413 void MainWindow::invokeVocabularyManager() {
00414     mainPanel->raiseWidget( frameVocabManagerIndex );
00415     updateMenus( NULL );
00416     cutAction->addTo( toolBar );
00417     copyAction->addTo( toolBar );
00418     pasteAction->addTo( toolBar );
00419 }
00420 
00421 void MainWindow::importData() {
00422     vocabManagerFrame->importData();
00423     // Some study languages in the preferences may have been added after the import
00424     // so we need to update the language selectors.
00425     updateFirstLanguageValues();
00426     updateTestLanguageValues();
00427 }
00428 
00429 void MainWindow::exportData() {
00430     vocabManagerFrame->exportData();
00431 }
00432 
00433 void MainWindow::preferences() {
00434     // Would it be possible not to pass the size of the array?
00435     PreferencesDialog dialog( this, &(control->getPreferences()) );
00436     dialog.showMaximized();
00437     int result = dialog.exec();
00438     if( result ) {
00439         // Check if different from previous value before updating.
00440         updateFonts();
00441         switchLanguage( control->getPreferences().getInterfaceLanguage() );
00442         //if( mainPanel->visibleWidget() == quizFrame ) {
00443         //    // Update the quiz.  May be tricky if the current term has no data for the current first/test languages.
00444         //}
00445         //else if( mainPanel->visibleWidget() == vocabManagerFrame )
00446         updateFirstLanguageValues();
00447         updateTestLanguageValues();
00448         setDigraphEnabled( control->getPreferences().isDigraphEnabled() );
00449         quizFrame->setButtonsHidden( control->getPreferences().areQuizButtonsHidden() );
00450         vocabManagerFrame->updateShownItems();
00451     }
00452 }
00453 
00454 void MainWindow::cut() {
00455     QWidget* widget = qApp->focusWidget();
00456     if( widget ) {
00457         if( widget->inherits( "QLineEdit" ) )
00458             ((QLineEdit*)widget)->cut();
00459         else if( widget->inherits( "QMultiLineEdit" ) )
00460             ((QMultiLineEdit*)widget)->cut();
00461         else if( mainPanel->visibleWidget() == vocabManagerFrame )
00462             vocabManagerFrame->cut();
00463     }
00464 }
00465 
00466 void MainWindow::copy() {
00467     QWidget* widget = qApp->focusWidget();
00468     if( widget ) {
00469         if( widget->inherits( "QLineEdit" ) )
00470             ((QLineEdit*)widget)->copy();
00471         else if( widget->inherits( "QMultiLineEdit" ) )
00472             ((QMultiLineEdit*)widget)->copy();
00473         else if( mainPanel->visibleWidget() == vocabManagerFrame )
00474                 vocabManagerFrame->copy();
00475     }
00476 }
00477 
00478 void MainWindow::paste() {
00479     QWidget* widget = qApp->focusWidget();
00480     if( widget ) {
00481         if( widget->inherits( "QLineEdit" ) )
00482             ((QLineEdit*)widget)->paste();
00483         else if( widget->inherits( "QMultiLineEdit" ) )
00484             ((QMultiLineEdit*)widget)->paste();
00485         else if( mainPanel->visibleWidget() == vocabManagerFrame )
00486             vocabManagerFrame->paste();
00487     }
00488 }
00489 
00490 void MainWindow::toggleLanguageFilter() {
00491     setLanguageFilterEnabled( !action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->isOn() );
00492     vocabManagerFrame->updateShownItems();
00493 }
00494 
00495 void MainWindow::quit() {
00496     close( true );
00497 }
00498 
00499 void MainWindow::setLanguageFilterEnabled( bool isEnabled ) {
00500     action[ ACTION_SHOW_ALL_GLOSSARIES_AND_TERMS ]->setOn( !isEnabled );
00501     control->getPreferences().setLanguageFilterEnabled( isEnabled );
00502 }
00503 
00504 void MainWindow::updateFirstLanguageValues() {
00505     updateLanguageSelector( firstLanguageComboBox );
00506     selectLanguage( firstLanguageComboBox, control->getPreferences().getFirstLanguage() );
00507 }
00508 
00509 void MainWindow::updateTestLanguageValues() {
00510     updateLanguageSelector( testLanguageComboBox );
00511     selectLanguage( testLanguageComboBox, control->getPreferences().getTestLanguage() );
00512 }
00513 
00514 void MainWindow::selectLanguage( QComboBox* comboBox, const QString& langCode ) {
00515     int itemCount = comboBox->count();
00516     for( int i = 0; i < itemCount; i++ ) {
00517         if( comboBox->text( i ) == QObject::tr( langCode ) ) {
00518             comboBox->setCurrentItem( i );
00519             return;
00520         }
00521     }
00522 }
00523 
00524 void MainWindow::updateLanguageSelector( QComboBox* comboBox ) {
00525     QValueList<QString> studyLanguages = control->getPreferences().getStudyLanguages();
00526     QStringList sortedLanguages;
00527     for( QValueList<QString>::ConstIterator it = studyLanguages.begin(); it != studyLanguages.end(); it++ ) {
00528         QString lang( *it );
00529         sortedLanguages.append( QObject::tr( lang ) );
00530     }
00531     sortedLanguages.sort(); 
00532 
00533     comboBox->clear();
00534     comboBox->insertItem( QString::null );
00535     for( QStringList::ConstIterator it = sortedLanguages.begin(); it != sortedLanguages.end(); it++ ) {
00536         QString lang( *it );
00537         comboBox->insertItem( lang );
00538     }
00539 }
00540 
00541 void MainWindow::setFirstLanguage( const QString& lang ) {
00542     if( lang == QObject::tr( control->getPreferences().getTestLanguage() ) && control->getPreferences().getTestLanguage() != QString::null )
00543         switchFirstAndTestLanguages();
00544     else
00545         control->getPreferences().setFirstLanguage( Util::getLanguageCode( lang ) );
00546     updateFonts();
00547     quizFrame->updateLanguageLabels();
00548     vocabManagerFrame->updateShownItems();
00549 }
00550 
00551 void MainWindow::setTestLanguage( const QString& lang ) {
00552     if( lang == QObject::tr( control->getPreferences().getFirstLanguage() ) && control->getPreferences().getFirstLanguage() != QString::null )
00553         switchFirstAndTestLanguages();
00554     else
00555         control->getPreferences().setTestLanguage( Util::getLanguageCode( lang ) );
00556     updateFonts();
00557     quizFrame->updateLanguageLabels();
00558     vocabManagerFrame->updateShownItems();
00559 }
00560 
00561 void MainWindow::switchFirstAndTestLanguages() {
00562     QString firstLang( control->getPreferences().getFirstLanguage() );
00563     QString testLang( control->getPreferences().getTestLanguage() );
00564     selectLanguage( firstLanguageComboBox, testLang );
00565     selectLanguage( testLanguageComboBox, firstLang );
00566     control->getPreferences().setFirstLanguage( testLang );
00567     control->getPreferences().setTestLanguage( firstLang );
00568 }

Generated on Sun Mar 1 17:30:47 2009 for toMOTko by  doxygen 1.5.6