TermDialog.cpp

Go to the documentation of this file.
00001 #include "TermDialog.h"
00002 #include "icons/editcut.xpm"
00003 #include "icons/editcopy.xpm"
00004 #include "icons/editpaste.xpm"
00005 #include "icons/void.xpm"
00006 
00007 TermDialog::TermDialog( Vocabulary& vocab, Controller* controller, QWidget* parent ) 
00008     : QDialog( parent, 0, true ), vocab( vocab ), controller( controller ), editedTerm( new Term( vocab.getMaxTermId() + 1, vocab.getId() ) ) {
00009     init();
00010 }
00011 
00012 TermDialog::TermDialog( Vocabulary& vocab, Controller* controller, QWidget* parent, const Term& term ) 
00013     : QDialog( parent, 0, true ), vocab( vocab ), controller( controller ), editedTerm( new Term( term ) ) {
00014     init();
00015 }
00016 
00017 void TermDialog::init() {
00018     Preferences& prefs = controller->getPreferences();
00019 
00020     QString firstLang( prefs.getFirstLanguage() );
00021     QString testLang( prefs.getTestLanguage() );
00022     bool isDigraphEnabled( prefs.isDigraphEnabled() );
00023 
00024     menuBar = new QMenuBar( this );
00025 
00026     QPopupMenu* edition = new QPopupMenu( this );
00027     menuBar->insertItem( QObject::tr( "Edition" ), edition );
00028 
00029     cutAction = new QAction( QObject::tr( "Cut" ), ZPIXMAP( editcut_xpm ), QString::null, 0, this, 0 );
00030     connect( cutAction, SIGNAL( activated() ), this, SLOT( cut() ) );
00031     cutAction->addTo( edition );
00032 
00033     copyAction = new QAction( QObject::tr( "Copy" ), ZPIXMAP( editcopy_xpm ), QString::null, 0, this, 0 );
00034     connect( copyAction, SIGNAL( activated() ), this, SLOT( copy() ) );
00035     copyAction->addTo( edition );
00036 
00037     pasteAction = new QAction( QObject::tr( "Paste" ), ZPIXMAP( editpaste_xpm ), QString::null, 0, this, 0 );
00038     connect( pasteAction, SIGNAL( activated() ), this, SLOT( paste() ) );
00039     pasteAction->addTo( edition );
00040 
00041     topPanel = new QHBox( this, "TopPanel" ); 
00042     topLeftPanel = new QVBox( topPanel, "TopLeftPanel" );
00043 
00044     firstLangPanel = new QHGroupBox( QObject::tr( firstLang ), topLeftPanel, "FirstLangPanel" );
00045     testLangPanel = new QVGroupBox( QObject::tr( testLang ), topLeftPanel, "TestLangPanel" );
00046 
00047     firstLangTermPanel = new QHBox( firstLangPanel, "FirstLangTermPanel" ); 
00048     firstLangTermLabel = new QLabel( tr( "Word/Expr." ), firstLangTermPanel, "FirstLangTermLabel" );
00049     firstLangTermLineEdit = new DigraphLineEdit( firstLangTermPanel, "FirstLangTermLineEdit" );
00050     firstLangTermLineEdit->setFont( prefs.getMediumFont( firstLang ) );
00051     firstLangTermLineEdit->setDigraphEnabled( isDigraphEnabled );
00052     testLangTopPanel = new QWidget( testLangPanel, "TestLangTopPanel" );
00053     testLangTopPanelLayout = new QHBoxLayout( testLangTopPanel );
00054 
00055     testLangLabelsPanel = new QVBox( testLangTopPanel, "TestLangLabelsPanel" );
00056     testLangTermAltLabel = new QLabel( tr( "Alt./Phon." ), testLangLabelsPanel, "TestLangTermAltLabel" );
00057     testLangTermLabel = new QLabel( tr( "Word/Expr." ), testLangLabelsPanel, "TestLangTermLabel" );
00058 
00059     testLangFieldsPanel = new QVBox( testLangTopPanel, "TestLangFieldsPanel" );
00060     testLangTermAltLineEdit = new DigraphLineEdit( testLangFieldsPanel, "TestLangTermAltLineEdit" );
00061     testLangTermAltLineEdit->setFont( prefs.getMediumFont( testLang ) );
00062     testLangTermAltLineEdit->setDigraphEnabled( isDigraphEnabled );
00063     testLangTermLineEdit = new DigraphLineEdit( testLangFieldsPanel, "TestLangTermLineEdit" );
00064     testLangTermLineEdit->setFont( prefs.getLargeFont( testLang ) );
00065     testLangTermLineEdit->setDigraphEnabled( isDigraphEnabled );
00066    
00067     testLangTopPanelLayout->addWidget( testLangLabelsPanel );
00068     testLangTopPanelLayout->addWidget( testLangFieldsPanel, 1 );
00069 
00070     commentLabel = new QLabel( tr( "Examples/Comments" ), this, "TestLangCommentLabel" );
00071     commentMultiLineEdit = new DigraphMultiLineEdit( this, "TestLangCommentMultiLineEdit" );
00072     commentMultiLineEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00073     commentMultiLineEdit->setWrapPolicy( QMultiLineEdit::Anywhere );
00074     commentMultiLineEdit->setWordWrap( QMultiLineEdit::WidgetWidth );
00075     commentMultiLineEdit->setFont( prefs.getBestFont( firstLang, testLang ) );
00076     commentMultiLineEdit->setDigraphEnabled( isDigraphEnabled );
00077 
00078     imageBox = new QVGroupBox( tr( "Image" ), topPanel, "ImageBox" );
00079 
00080     imagePanel = new QWidget( imageBox, "ImagePanel" );
00081     imagePanelLayout = new QVBoxLayout( imagePanel );
00082     imagePanelLayout->setSpacing( 2 );
00083     imageWrapper = new QVBox( imagePanel, "ImageWrapper" );
00084     image = new QLabel( imageWrapper, "Image" );
00085     image->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding ) );
00086     image->setAlignment( AlignHCenter | AlignVCenter );
00087     image->setScaledContents( true );
00088 
00089     imageBox->setMaximumHeight( topLeftPanel->sizeHint().height() );
00090 
00091     imageButtonsPanel = new QHBox( imagePanel, "ImageButtonsPanel" );
00092     setImageButton = new QPushButton( tr( "setImage" ), imageButtonsPanel, "SetImageButton" );
00093     QToolTip::add( setImageButton, tr( "setImageTooltip" ) );
00094     connect( setImageButton, SIGNAL( clicked() ), this, SLOT( setImage() ) );
00095     clearImageButton = new QPushButton( tr( "clearImage" ), imageButtonsPanel, "clearImageButton" );
00096     QToolTip::add( clearImageButton, tr( "clearImageTooltip" ) );
00097     connect( clearImageButton, SIGNAL( clicked() ), this, SLOT( clearImage() ) );
00098     imagePanelLayout->addWidget( imageWrapper, 1 );
00099     imagePanelLayout->addWidget( imageButtonsPanel );
00100 
00101     mainLayout = new QVBoxLayout( this );
00102     mainLayout->setSpacing( 2 );
00103     mainLayout->setMenuBar( menuBar );
00104     mainLayout->addWidget( topPanel );
00105     mainLayout->addWidget( commentLabel );
00106     mainLayout->addWidget( commentMultiLineEdit, 1 );
00107     mainLayout->activate();
00108 
00109     setCaption( tr( "EditTerm" ) );
00110 
00111     updateUi();
00112 }
00113 
00114 TermDialog::~TermDialog() {
00115 }
00116 
00117 void TermDialog::show() {
00118     QDialog::show();
00119     resizeImage();
00120 }
00121 
00122 void TermDialog::updateModel() {
00123     if( !editedTerm->isTranslationExists( controller->getPreferences().getFirstLanguage() ) )
00124         editedTerm->addTranslation( controller->getPreferences().getFirstLanguage() );
00125 
00126     Translation& firstLangTranslation = editedTerm->getTranslation( controller->getPreferences().getFirstLanguage() );
00127     firstLangTranslation.setWord( firstLangTermLineEdit->text() );
00128 
00129     if( !editedTerm->isTranslationExists( controller->getPreferences().getTestLanguage() ) )
00130         editedTerm->addTranslation( controller->getPreferences().getTestLanguage() );
00131 
00132     Translation& testLangTranslation = editedTerm->getTranslation( controller->getPreferences().getTestLanguage() );
00133     testLangTranslation.setWord( testLangTermLineEdit->text() );
00134     testLangTranslation.setAlt( testLangTermAltLineEdit->text() );
00135 
00136     BilingualKey commentKey( controller->getPreferences().getFirstLanguage(), controller->getPreferences().getTestLanguage() );
00137     editedTerm->addComment( commentKey, commentMultiLineEdit->text() );
00138 
00139     // If the path refers to an image in toMOTko's vocabulary, we use a relative path instead.
00140     QString vocabLocation = controller->getApplicationDirName() + "/" + vocab.getParent()->getPath() +
00141         "/v-" + QString::number( vocab.getId() ) + "/";
00142     QString imagePath = tempImagePath.left( vocabLocation.length() ) == vocabLocation ? 
00143             tempImagePath.right( tempImagePath.length() - vocabLocation.length() ) : tempImagePath;
00144     editedTerm->setImagePath( imagePath );
00145 }
00146 
00147 void TermDialog::cut() {
00148     QWidget* widget = qApp->focusWidget();
00149     if( widget != NULL ) {
00150         const char* className = widget->className();
00151         if( strcmp( className, "DigraphLineEdit" ) == 0 )
00152             ((DigraphLineEdit*)widget)->cut();
00153         else if( strcmp( className, "DigraphMultiLineEdit" ) == 0 )
00154             ((DigraphMultiLineEdit*)widget)->cut();
00155     }
00156 }
00157 
00158 void TermDialog::copy() {
00159     QWidget* widget = qApp->focusWidget();
00160     if( widget != NULL ) {
00161         const char* className = widget->className();
00162         if( strcmp( className, "DigraphLineEdit" ) == 0 )
00163             ((DigraphLineEdit*)widget)->copy();
00164         else if( strcmp( className, "DigraphMultiLineEdit" ) == 0 )
00165             ((DigraphMultiLineEdit*)widget)->copy();
00166     }
00167 }
00168 
00169 void TermDialog::paste() {
00170     QWidget* widget = qApp->focusWidget();
00171     if( widget != NULL ) {
00172         const char* className = widget->className();
00173         if( strcmp( className, "DigraphLineEdit" ) == 0 )
00174             ((DigraphLineEdit*)widget)->paste();
00175         else if( strcmp( className, "DigraphMultiLineEdit" ) == 0 )
00176             ((DigraphMultiLineEdit*)widget)->paste();
00177     }
00178 }
00179 
00180 void TermDialog::setImage() {
00181     QDir dir = QPEApplication::documentDir();
00182     if( tempImagePath )
00183         dir = QFileInfo( tempImagePath ).dir();
00184 
00185     ZFileDialog dialog( tr( "SetImage..." ), dir.path(), ZFileDialog::ExistingFile, this );
00186     QStringList allowedExtensions = QStringList::split( QString( "," ), QString( ".gif,.png" ) );
00187     dialog.setFilters( allowedExtensions );
00188 
00189     int result = dialog.exec();
00190     if( result && !dialog.selectedFile().isEmpty() ) {
00191         QString fileExtension = dialog.selectedFile().right( 4 );
00192         if( allowedExtensions.contains( fileExtension ) )
00193             initImage( dialog.selectedFile() );
00194     }
00195 }
00196 
00197 void TermDialog::clearImage() {
00198     image->setPixmap( ZPIXMAP( void_xpm ) );
00199     tempImagePath = QString::null;
00200     imageFormat = QString::null;
00201 }
00202 
00203 void TermDialog::initImage( const QString& imagePath ) {
00204     clearImage();
00205     if( !imagePath.isNull() ) {
00206         QFileInfo info( imagePath );
00207         if( info.exists() ) {
00208             imageFormat = QPixmap::imageFormat( imagePath );
00209             if( imageFormat == "GIF" || imageFormat == "PNG" ) {
00210                 tempImagePath = imagePath;
00211                 if( imageFormat == "GIF" ) {
00212                     const QMovie& movie( imagePath );
00213                     image->setMovie( movie );
00214                 }
00215                 else if( imageFormat == "PNG" ) {
00216                     QPixmap pixmap( imagePath );
00217                     image->setPixmap( pixmap );
00218                     resizeImage();
00219                 }
00220             }
00221         }
00222     }
00223 }
00224 
00225 void TermDialog::resizeImage() const {
00226     if( !tempImagePath.isNull() ) {
00227         if( imageFormat == "GIF" ) {
00228             // Nothing to do for this format.  The default behavior works fine.
00229         }
00230         else if( imageFormat == "PNG" ) {
00231             int proportionalWidth = image->height() * image->pixmap()->width() / image->pixmap()->height();
00232             image->setMaximumWidth( proportionalWidth );
00233         }
00234     }
00235 }
00236 
00237 const Term& TermDialog::getTerm() {
00238     updateModel();
00239     return( *editedTerm );
00240 }
00241 
00242 void TermDialog::updateUi() {
00243     if( editedTerm ) {
00244         if( editedTerm->isTranslationExists( controller->getPreferences().getFirstLanguage() ) ) {
00245             Translation& firstLangTranslation = editedTerm->getTranslation( controller->getPreferences().getFirstLanguage() );
00246             firstLangTermLineEdit->setText( firstLangTranslation.getWord() );
00247             firstLangTermLineEdit->setCursorPosition( 0 );
00248         }
00249 
00250         if( editedTerm->isTranslationExists( controller->getPreferences().getTestLanguage() ) ) {
00251             Translation& testLangTranslation = editedTerm->getTranslation( controller->getPreferences().getTestLanguage() );
00252             testLangTermLineEdit->setText( testLangTranslation.getWord() );
00253             testLangTermLineEdit->setCursorPosition( 0 );
00254             testLangTermAltLineEdit->setText( testLangTranslation.getAlt() );
00255             testLangTermAltLineEdit->setCursorPosition( 0 );
00256             BilingualKey commentKey( controller->getPreferences().getFirstLanguage(), controller->getPreferences().getTestLanguage() );
00257             if( editedTerm->isCommentExists( commentKey ) ) {
00258                 commentMultiLineEdit->setText( editedTerm->getComment( commentKey ) );
00259                 commentMultiLineEdit->setCursorPosition( 0, 0 );
00260             }
00261         }
00262 
00263         QString absPath = controller->getResolvedImagePath( editedTerm->getImagePath(), vocab );
00264         initImage( absPath );
00265     }
00266 }

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