SignalConfig.cpp 7 KB
Newer Older
Hurstel's avatar
Hurstel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#include "SignalConfig.h"

#include <QDir>
#include <QFile>
#include <QFileDialog>

#include <QtXml>

SignalConfig::States SignalConfig::m_states;

SignalConfig::SignalConfig(QWidget *parent): QDialog(parent),
    b_autoUpdate(true)
{
    this->setupUi(this);

    UIupdate();

    connect(this->lineEdit, SIGNAL(returnPressed()), this, SLOT(updateFromUI()));
    connect(this->spinBox_R, SIGNAL(valueChanged(int)), this, SLOT(updateFromUI()));
    connect(this->spinBox_G, SIGNAL(valueChanged(int)), this, SLOT(updateFromUI()));
    connect(this->spinBox_B, SIGNAL(valueChanged(int)), this, SLOT(updateFromUI()));
    connect(this->spinBox_W, SIGNAL(valueChanged(int)), this, SLOT(updateFromUI()));
    connect(this->spinBox_H, SIGNAL(valueChanged(int)), this, SLOT(updateFromUI()));

    connect(this->pushButton, SIGNAL(pressed()), this, SLOT(browseForImg()));
    connect(this->pushButton_2, SIGNAL(pressed()), this, SLOT(resetImg()));
    connect(this->toolButton, SIGNAL(pressed()), this, SLOT(cancelImgPath()));

    connect(this->lineEdit_2, SIGNAL(returnPressed()), this, SLOT(updateFromUI()));

    connect(this->pushButton_3, SIGNAL(pressed()), this, SLOT(browseForSnd()));
    connect(this->toolButton_2, SIGNAL(pressed()), this, SLOT(cancelSndPath()));
}

bool SignalConfig::fileOK(QString path){
    return QDir::isAbsolutePath(path) && QFile::exists(path);
}

bool SignalConfig::checkErrors(){
    return ( (m_states.isNoImgPath || fileOK(m_states.imgPath))
             && (m_states.isNoSndPath || fileOK(m_states.sndPath)) );
}


void SignalConfig::UIupdate(){
        lineEdit->setText((m_states.isNoImgPath)?QString("[default display]"):m_states.imgPath);

        spinBox_R->setValue(m_states.color[0]);
        spinBox_G->setValue(m_states.color[1]);
        spinBox_B->setValue(m_states.color[2]);

        spinBox_W->setValue(m_states.size[0]);
        spinBox_H->setValue(m_states.size[1]);

        groupBox_2->setVisible(m_states.isNoImgPath);

        lineEdit_2->setText((m_states.isNoSndPath)?QString("[default sound]"):m_states.sndPath);

        buttonBox->button(QDialogButtonBox::Ok)->setEnabled(checkErrors());
}

void SignalConfig::updateFromUI(){
    if(b_autoUpdate){
        m_states.color[0]= spinBox_R->value();
        m_states.color[1]= spinBox_G->value();
        m_states.color[2]= spinBox_B->value();

        m_states.size[0]= spinBox_W->value();
        m_states.size[1]= spinBox_H->value();

        m_states.isNoImgPath= (!fileOK(lineEdit->text()));

        m_states.imgPath= (m_states.isNoImgPath) ?
                            QString("[default display]")
                        :   lineEdit->text() ;

        groupBox_2->setVisible(m_states.isNoImgPath);


        m_states.isNoSndPath= (!fileOK(lineEdit_2->text()));

        m_states.sndPath= (m_states.isNoSndPath)?
                                QString("[default sound]")
                             : lineEdit_2->text();

        buttonBox->button(QDialogButtonBox::Ok)->setEnabled(checkErrors());
    }
}

void SignalConfig::browseForImg(){
    b_autoUpdate= false;
    lineEdit->setText(
                QFileDialog::getOpenFileName(this,
                        tr("Open Image"), "", tr("Images (*.png *.jpg *.jpeg)"))
            );
    QSize s= QPixmap(lineEdit->text()).size();
    spinBox_W->setValue(s.width());
    spinBox_H->setValue(s.height());
    b_autoUpdate= true;

    updateFromUI();
}

void SignalConfig::cancelImgPath(){
    lineEdit->setText(QString("[default display]"));
    m_states.isNoImgPath= true;

    updateFromUI();
}

void SignalConfig::resetImg(){
    b_autoUpdate= false;

    m_states= SignalConfig::States();

    UIupdate();

    b_autoUpdate= true;
}

void SignalConfig::browseForSnd(){
    b_autoUpdate= false;
    lineEdit_2->setText(
                QFileDialog::getOpenFileName(this,
                         tr("Open sound file"), "", tr("Wav (*.wav)"))
             );

    b_autoUpdate= true;

    updateFromUI();
}

void SignalConfig::cancelSndPath(){
    lineEdit_2->setText(QString("[default sound]"));
    m_states.isNoSndPath= true;

    updateFromUI();
}

SignalConfig::FILE_RW_STATUS SignalConfig::loadFromXML(){
    QDomDocument doc;
    QFile xml_file(".SignalConfig.xml");


    m_states= SignalConfig::States();


    if(!xml_file.open(QIODevice::ReadOnly)){
        return CANT_OPEN_FORCE_DEFAULT;
    }

    if(!doc.setContent(&xml_file)){
        return INVALID_CONTENT_FORCE_DEFAULT;
    }

    QDomElement root= doc.documentElement();

    QDomElement elem= root.firstChildElement("img_path");

    m_states.isNoImgPath= (elem.isNull() || (!fileOK(elem.text())));
    m_states.imgPath= (m_states.isNoImgPath)?QString("[default display]")
                              :   elem.text() ;
    if(!m_states.isNoImgPath){
        m_states.imgPath= elem.text();
    }



    elem= root.firstChildElement("default_color");

    if(!elem.isNull()){
        bool ok; int v;
        QString col_str[]= {"red", "green", "blue"};

        for(int i=0; i<3; ++i){
            v= elem.attribute(col_str[i],"0").toInt(&ok);
            if(!ok)
                v= 0;
            m_states.color[i]= (unsigned int)( (v<0)? 0 : (v>255)? 255 : v );
        }
    }


    elem= root.firstChildElement("size");

    if(!elem.isNull()){
        bool ok; int v;

        v= elem.attribute("width","100").toInt(&ok);
        if(!ok)
            v= 100;
        m_states.size[0]= (unsigned int)( (v<0)?0:v );

        v= elem.attribute("height","100").toInt(&ok);
        if(!ok)
            v= 100;
        m_states.size[1]= (unsigned int)( (v<0)?0:v );
    }

    elem= root.firstChildElement("snd_path");

    m_states.isNoSndPath= (elem.isNull() || (!fileOK(elem.text())));
    m_states.sndPath= (m_states.isNoSndPath)? QString("[default sound]")
                                           : elem.text();
    if(!m_states.isNoSndPath){
        m_states.sndPath= elem.text();
    }

    return LOADED;
}

SignalConfig::FILE_RW_STATUS SignalConfig::saveToXML(){
    QDomDocument doc;

    QDomElement main= doc.createElement("signal_config");
    doc.appendChild(main);

    QDomElement elem_param= doc.createElement("img_path");
    QDomText text= doc.createTextNode(m_states.imgPath);
    elem_param.appendChild(text);
    main.appendChild(elem_param);

    elem_param= doc.createElement("default_color");
    QString col_str[]= {"red", "green", "blue"};
    for(unsigned int i=0; i<3; ++i){
        elem_param.setAttribute(col_str[i], m_states.color[i]);
    }
    main.appendChild(elem_param);

    elem_param= doc.createElement("size");
    elem_param.setAttribute("width", m_states.size[0]);
    elem_param.setAttribute("height", m_states.size[1]);
    main.appendChild(elem_param);

    elem_param= doc.createElement("snd_path");
    text= doc.createTextNode(m_states.sndPath);
    elem_param.appendChild(text);
    main.appendChild(elem_param);

    QFile file(".SignalConfig.xml");
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
        file.close();

        return CANT_OPEN_FORCE_DEFAULT;
    }

    QTextStream ts(&file);

    ts << doc.toString();

    file.close();

    return SAVED;
}