home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / i18n / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  4.1 KB  |  171 lines

  1. /****************************************************************************
  2. ** $Id:  qt/main.cpp   3.0.0   edited Jun 22 13:24 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include <qapplication.h>
  12. #include <qtranslator.h>
  13. #include <qfileinfo.h>
  14. #include <qmessagebox.h>
  15. #include <qcheckbox.h>
  16. #include <qvbox.h>
  17. #include <qlayout.h>
  18. #include <qbuttongroup.h>
  19. #include <qpushbutton.h>
  20. #include <qsignalmapper.h>
  21. #include <stdlib.h>
  22.  
  23. #if defined(Q_OS_UNIX)
  24. #include <unistd.h>
  25. #endif
  26.  
  27. #include "mywidget.h"
  28.  
  29. //#define USE_I18N_FONT
  30.  
  31. class QVDialog : public QDialog {
  32. public:
  33.     QVDialog(QWidget *parent=0, const char *name=0, bool modal=FALSE,
  34.              WFlags f=0) : QDialog(parent,name,modal,f)
  35.     {
  36.     QVBoxLayout* vb = new QVBoxLayout(this,8);
  37.     vb->setAutoAdd(TRUE);
  38.     hb = 0;
  39.     sm = new QSignalMapper(this);
  40.     connect(sm,SIGNAL(mapped(int)),this,SLOT(done(int)));
  41.     }
  42.     void addButtons( const QString& cancel=QString::null,
  43.             const QString& ok=QString::null,
  44.             const QString& mid1=QString::null,
  45.             const QString& mid2=QString::null,
  46.             const QString& mid3=QString::null)
  47.     {
  48.     addButton(ok.isNull() ? tr("OK") : ok, 1);
  49.     if ( !mid1.isNull() ) addButton(mid1,2);
  50.     if ( !mid2.isNull() ) addButton(mid2,3);
  51.     if ( !mid3.isNull() ) addButton(mid3,4);
  52.     addButton(cancel.isNull() ? tr("Cancel") : cancel, 0);
  53.     }
  54.  
  55.     void addButton( const QString& text, int result )
  56.     {
  57.     if ( !hb )
  58.         hb = new QHBox(this);
  59.     QPushButton *c = new QPushButton(text, hb);
  60.     sm->setMapping(c,result);
  61.     connect(c,SIGNAL(clicked()),sm,SLOT(map()));
  62.     }
  63.  
  64. private:
  65.     QSignalMapper *sm;
  66.     QHBox *hb;
  67. };
  68.  
  69. MyWidget* showLang(QString lang)
  70. {
  71.  
  72.     static QTranslator *translator = 0;
  73.  
  74.     qApp->setPalette(QPalette(QColor(220-rand()%64,220-rand()%64,220-rand()%64)));
  75.  
  76.     lang = "mywidget_" + lang + ".qm";
  77.     QFileInfo fi( lang );
  78.  
  79.     if ( !fi.exists() ) {
  80.     QMessageBox::warning( 0, "File error",
  81.                   QString("Cannot find translation for language: "+lang+
  82.                       "\n(try eg. 'de', 'ko' or 'no')") );
  83.     return 0;
  84.     }
  85.     if ( translator ) {
  86.     qApp->removeTranslator( translator );
  87.     delete translator;
  88.     }
  89.     translator = new QTranslator( 0 );
  90.     translator->load( lang, "." );
  91.     qApp->installTranslator( translator );
  92.     MyWidget *m = new MyWidget;
  93.     m->setCaption("Qt Example - i18n - " + m->caption() );
  94.     return m;
  95. }
  96.  
  97. int main( int argc, char** argv )
  98. {
  99.     QApplication app( argc, argv );
  100.  
  101.     const char* qm[]=
  102.     { "cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh", 0 };
  103.  
  104. #if defined(Q_OS_UNIX)
  105.     srand(getpid()<<2);
  106. #endif
  107.  
  108.     QFont font("unifont",16,50,FALSE);
  109.     qApp->setFont(font);
  110.  
  111.     QString lang;
  112.     if ( argc == 2 )
  113.         lang = argv[1];
  114.  
  115.     if ( argc != 2 || lang == "all" ) {
  116.     QVDialog dlg(0,0,TRUE);
  117.     QCheckBox* qmb[sizeof(qm)/sizeof(qm[0])];
  118.     int r;
  119.     if ( lang == "all" ) {
  120.         r = 2;
  121.     } else {
  122.         QButtonGroup *bg = new QButtonGroup(4,Qt::Vertical,"Choose Locales",&dlg);
  123.         for ( int i=0; qm[i]; i++ )
  124.         qmb[i] = new QCheckBox((const char*)qm[i],bg);
  125.         dlg.addButtons("Cancel","OK","All");
  126.         r = dlg.exec();
  127.     }
  128.     if ( r ) {
  129.         bool tight = qApp->desktop()->width() < 1024;
  130.         int x=5;
  131.         int y=25;
  132.         for ( int i=0; qm[i]; i++ ) {
  133.         if ( r == 2 || qmb[i]->isChecked() ) {
  134.             MyWidget* w = showLang((const char*)qm[i]);
  135.  
  136.             if( w == 0 ) exit( 0 );
  137.             QObject::connect(w, SIGNAL(closed()), qApp, SLOT(quit()));
  138.             w->setGeometry(x,y,197,356);
  139.             w->show();
  140.             if ( tight ) {
  141.             x += 8;
  142.             y += 8;
  143.             } else {
  144.             x += 205;
  145.             if ( x > 1000 ) {
  146.                 x = 5;
  147.                 y += 384;
  148.             }
  149.             }
  150.         }
  151.         }
  152.     } else {
  153.             exit( 0 );
  154.         }
  155.     } else {
  156.     QString lang = argv[1];
  157.     QWidget* m = showLang(lang);
  158.     app.setMainWidget( m );
  159.     m->setCaption("Qt Example - i18n");
  160.     m->show();
  161.     }
  162.  
  163. #ifdef USE_I18N_FONT
  164.     memorymanager->savePrerenderedFont(font.handle(),FALSE);
  165. #endif
  166.  
  167.     // While we run "all", kill them all
  168.     return app.exec();
  169.  
  170. }
  171.