QTfrontend/ui/page/pagemain.cpp
branchwebgl
changeset 9950 2759212a27de
parent 9236 ddd675825672
parent 9852 0a5ec79af0a2
child 10015 4feced261c68
equal deleted inserted replaced
9521:8054d9d775fd 9950:2759212a27de
    19 #include <QGridLayout>
    19 #include <QGridLayout>
    20 #include <QHBoxLayout>
    20 #include <QHBoxLayout>
    21 #include <QPushButton>
    21 #include <QPushButton>
    22 #include <QLabel>
    22 #include <QLabel>
    23 #include <QTime>
    23 #include <QTime>
       
    24 #include <QSettings>
    24 
    25 
    25 #include "pagemain.h"
    26 #include "pagemain.h"
    26 #include "hwconsts.h"
    27 #include "hwconsts.h"
    27 #include "hwform.h"
    28 #include "hwform.h"
       
    29 #include "DataManager.h"
    28 
    30 
    29 QLayout * PageMain::bodyLayoutDefinition()
    31 QLayout * PageMain::bodyLayoutDefinition()
    30 {
    32 {
    31     QGridLayout * pageLayout = new QGridLayout();
    33     QGridLayout * pageLayout = new QGridLayout();
    32     //pageLayout->setColumnStretch(0, 1);
    34     //pageLayout->setColumnStretch(0, 1);
   117     return bottomLayout;
   119     return bottomLayout;
   118 }
   120 }
   119 
   121 
   120 void PageMain::connectSignals()
   122 void PageMain::connectSignals()
   121 {
   123 {
       
   124 #ifndef QT_DEBUG
       
   125     connect(this, SIGNAL(pageEnter()), this, SLOT(updateTip()));
       
   126 #endif
   122     connect(BtnNet, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   127     connect(BtnNet, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   123     //connect(BtnNetLocal, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   128     //connect(BtnNetLocal, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   124     //connect(BtnNetOfficial, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   129     //connect(BtnNetOfficial, SIGNAL(clicked()), this, SLOT(toggleNetworkChoice()));
   125     // TODO: add signal-forwarding required by (currently missing) encapsulation
   130     // TODO: add signal-forwarding required by (currently missing) encapsulation
   126 }
   131 }
   130     initPage();
   135     initPage();
   131 
   136 
   132     if(frontendEffects)
   137     if(frontendEffects)
   133         setAttribute(Qt::WA_NoSystemBackground, true);
   138         setAttribute(Qt::WA_NoSystemBackground, true);
   134     mainNote->setOpenExternalLinks(true);
   139     mainNote->setOpenExternalLinks(true);
   135 
       
   136 #ifdef QT_DEBUG
   140 #ifdef QT_DEBUG
   137     setDefaultDescription(QLabel::tr("This development build is 'work in progress' and may not be compatible with other versions of the game, while some features might be broken or incomplete!"));
   141     setDefaultDescription(QLabel::tr("This development build is 'work in progress' and may not be compatible with other versions of the game, while some features might be broken or incomplete!"));
   138 #else
   142 #else
   139     setDefaultDescription(QLabel::tr("Tip: %1").arg(randomTip()));
   143     setDefaultDescription(QLabel::tr("Tip: %1").arg(randomTip()));
   140 #endif
   144 #endif
   141 
   145 }
   142 }
   146 
   143 
   147 void PageMain::updateTip()
   144 QString PageMain::randomTip() const
   148 {
       
   149     setDefaultDescription(QLabel::tr("Tip: %1").arg(randomTip()));
       
   150 }
       
   151 
       
   152 QString PageMain::randomTip()
   145 {
   153 {
   146 #ifdef _WIN32
   154 #ifdef _WIN32
   147     int platform = 1;
   155     int platform = 1;
   148 #elif defined __APPLE__
   156 #elif defined __APPLE__
   149     int platform = 2;
   157     int platform = 2;
   150 #else
   158 #else
   151     int platform = 3;
   159     int platform = 3;
   152 #endif
   160 #endif
   153     QStringList Tips;
   161     if(!Tips.length())
   154     QFile file(":/res/xml/tips.xml");
   162     {
   155     file.open(QIODevice::ReadOnly);
   163         DataManager & dataMgr = DataManager::instance();
   156     QTextStream in(&file);
   164 
   157     QString line = in.readLine();
   165         // get locale
   158     int tip_platform = 0;
   166         QSettings settings(dataMgr.settingsFileName(),
   159     while (!line.isNull()) {
   167                            QSettings::IniFormat);
   160         if(line.contains("<windows-only>", Qt::CaseSensitive))
   168 
   161             tip_platform = 1;
   169         QString loc = settings.value("misc/locale", "").toString();
   162         if(line.contains("<mac-only>", Qt::CaseSensitive))
   170         if (loc.isEmpty())
   163             tip_platform = 2;
   171             loc = QLocale::system().name();
   164         if(line.contains("<linux-only>", Qt::CaseSensitive))
   172 
   165             tip_platform = 3;
   173         QString tipFile = QString("physfs://Locale/tips_" + loc + ".xml");
   166         if(line.contains("</windows-only>", Qt::CaseSensitive) ||
   174 
   167                 line.contains("</mac-only>", Qt::CaseSensitive) ||
   175         // if file is non-existant try with language only
   168                 line.contains("</linux-only>", Qt::CaseSensitive)) {
   176         if (!QFile::exists(tipFile))
   169             tip_platform = 0;
   177             tipFile = QString("physfs://Locale/tips_" + loc.remove(QRegExp("_.*$")) + ".xml");
       
   178 
       
   179         // fallback if file for current locale is non-existant
       
   180         if (!QFile::exists(tipFile))
       
   181             tipFile = QString("physfs://Locale/tips_en.xml");
       
   182 
       
   183         QFile file(tipFile);
       
   184         file.open(QIODevice::ReadOnly);
       
   185         QTextStream in(&file);
       
   186         in.setCodec("UTF-8");
       
   187         QString line = in.readLine();
       
   188         int tip_platform = 0;
       
   189         while (!line.isNull()) {
       
   190             if(line.contains("<windows-only>", Qt::CaseSensitive))
       
   191                 tip_platform = 1;
       
   192             if(line.contains("<mac-only>", Qt::CaseSensitive))
       
   193                 tip_platform = 2;
       
   194             if(line.contains("<linux-only>", Qt::CaseSensitive))
       
   195                 tip_platform = 3;
       
   196             if(line.contains("</windows-only>", Qt::CaseSensitive) ||
       
   197                     line.contains("</mac-only>", Qt::CaseSensitive) ||
       
   198                     line.contains("</linux-only>", Qt::CaseSensitive)) {
       
   199                 tip_platform = 0;
       
   200             }
       
   201             QStringList split_string = line.split(QRegExp("</?tip>"));
       
   202             if((tip_platform == platform || tip_platform == 0) && split_string.size() != 1)
       
   203                 Tips << split_string[1];
       
   204             line = in.readLine();
   170         }
   205         }
   171         QStringList split_string = line.split(QRegExp("</?tip>"));
   206         // The following tip will require links to app store entries first.
   172         if((tip_platform == platform || tip_platform == 0) && split_string.size() != 1)
   207         //Tips << tr("Want to play Hedgewars any time? Grab the Mobile version for %1 and %2.", "Tips").arg("").arg("");
   173             Tips << tr(split_string[1].toLatin1().data(), "Tips");
   208         // the ios version is located here: http://itunes.apple.com/us/app/hedgewars/id391234866
   174         line = in.readLine();
   209 
       
   210         file.close();
   175     }
   211     }
   176     // The following tip will require links to app store entries first.
   212     
   177     //Tips << tr("Want to play Hedgewars any time? Grab the Mobile version for %1 and %2.", "Tips").arg("").arg("");
   213     if(Tips.length())
   178     // the ios version is located here: http://itunes.apple.com/us/app/hedgewars/id391234866
   214         return Tips[QTime(0, 0, 0).secsTo(QTime::currentTime()) % Tips.length()];
   179 
   215     else
   180     file.close();
   216         return QString();
   181     return Tips[QTime(0, 0, 0).secsTo(QTime::currentTime()) % Tips.length()];
       
   182 }
   217 }
   183 
   218 
   184 void PageMain::toggleNetworkChoice()
   219 void PageMain::toggleNetworkChoice()
   185 {
   220 {
   186     bool visible = BtnNetLocal->isVisible();
   221     bool visible = BtnNetLocal->isVisible();