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