stif/QtUI/StifQtUI/uisetting.cpp
branchRCL_3
changeset 8 cfe32394fcd5
parent 7 8a14024f954a
child 9 7e287c5c61f0
equal deleted inserted replaced
7:8a14024f954a 8:cfe32394fcd5
     1 /*
       
     2  * uisetting.cpp
       
     3  *
       
     4  *  Created on: 2010-2-8
       
     5  *      Author: y183zhan
       
     6  */
       
     7 
       
     8 #include <QFile>
       
     9 #include <QTextStream>
       
    10 #include <QDateTime>
       
    11 #include "uisetting.h"
       
    12 const QString SETTINGFILE = "c:\\TestFramework\\StfQtUISetting.ini";
       
    13 
       
    14 UiSetting::UiSetting()
       
    15     {
       
    16     if(!load())
       
    17         {
       
    18         loadDefault();
       
    19         }
       
    20     }
       
    21 
       
    22 UiSetting::~UiSetting()
       
    23     {
       
    24     }
       
    25 
       
    26 QString UiSetting::ReadSetting(const QString& item)
       
    27     {
       
    28     QString value = "";
       
    29     if(settingList.contains(item))
       
    30         {
       
    31         value = settingList.value(item);
       
    32         }
       
    33     return value;
       
    34     }
       
    35 
       
    36 
       
    37 void UiSetting::SetSetting(const QString& item, const QString& value)
       
    38     {
       
    39     if(settingList.contains(item))
       
    40         {
       
    41         settingList.remove(item);
       
    42         }
       
    43     settingList.insert(item, value);
       
    44     save();
       
    45     }
       
    46 
       
    47 
       
    48 void UiSetting::loadDefault()
       
    49     {
       
    50     settingList.clear();
       
    51     settingList.insert("showoutput", "true");
       
    52     //add mor default setting here.
       
    53     }
       
    54 
       
    55 
       
    56 bool UiSetting::load()
       
    57     {
       
    58     QFile file(SETTINGFILE);
       
    59     if(!file.open(QIODevice::ReadOnly))
       
    60         {
       
    61         return false;
       
    62         }
       
    63     QTextStream in(&file);
       
    64     QString line, item, value;
       
    65     int index;
       
    66     while(!in.atEnd())
       
    67         {
       
    68         line = in.readLine().trimmed().toLower();
       
    69         if(!line.startsWith("//"))
       
    70             {
       
    71             index = line.indexOf("=");
       
    72             if(index > 0 && index < line.length() - 1)
       
    73                 {
       
    74                 item = line.left(index).trimmed();
       
    75                 value = line.right(line.length() - index  -1);
       
    76                 settingList.insert(item, value);
       
    77                 }
       
    78             }
       
    79         //end while.
       
    80         }
       
    81     return true;
       
    82     }
       
    83 
       
    84 bool UiSetting::save()
       
    85     {
       
    86     QFile file(SETTINGFILE);
       
    87     if(!file.open(QIODevice::WriteOnly))
       
    88         {
       
    89         return false;
       
    90         }
       
    91     QTextStream in(&file);
       
    92     in << "//STFQtUI Setting.\r\n";
       
    93     in << "//Created at: " + QDateTime::currentDateTime().toString("yyyy.mm.dd hh:mm::ss");
       
    94     in << "\r\n";
       
    95     for(int i=0;i< settingList.size();i++)
       
    96         {
       
    97         in << settingList.keys()[i];
       
    98         in << "=";
       
    99         in << settingList.value(settingList.keys()[i]);
       
   100         in << "\r\n";
       
   101         }
       
   102     return true;    
       
   103     }
       
   104