#include "searchinblobs.h"
#include "constants.h"
#include "delegate.h"
#include "tools.h"
#include "result.h"

#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextCodec>
#include <QFile>
#include <QTextStream>
#include <QDate>
#include <QMessageBox>

using namespace Constants;
using namespace ToolsSpace;

Searchinblobs::Searchinblobs(QObject * parent)
{
    Q_UNUSED(parent);
    setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    initialise();
    connect(quitButton,SIGNAL(pressed()),this,SLOT(close()));
    connect(searchButton,SIGNAL(pressed()),this,SLOT(parse()));
    connect(addButton,SIGNAL(pressed()),this,SLOT(addarequest()));
    connect(deleteButton,SIGNAL(pressed()),this,SLOT(deleterequest()));
    connect(this,SIGNAL(destroyed()),this,SLOT(deleteSearchinBlobs()));
}

Searchinblobs::~Searchinblobs()
{
    qWarning() << __FILE__ << QString::number(__LINE__) << "destructeur Searchinblobs" ;
    QFile file(qApp->applicationDirPath()+"/../resources/fichieroff");
    if (!sauveBox->isChecked())
    {
          if (!file.remove())
          {
                qWarning() << __FILE__ << QString::number(__LINE__) << "unable to remove fileoff" ;
              }
        }
}

void Searchinblobs::deleteSearchinBlobs()
{
    qWarning() << __FILE__ << QString::number(__LINE__) << "in deleteSearchinBlobs" ;
    QFile file(qApp->applicationDirPath()+"/../resources/fichieroff");
    if (!sauveBox->isChecked())
    {
          if (!file.remove())
          {
                qWarning() << __FILE__ << QString::number(__LINE__) << "unable to remove fileoff" ;
              }
        }
}

void Searchinblobs::initialise()
{
    m_db = QSqlDatabase::database("DrTuxTest");
    Tools t(this);
    m_hashConditions = t.hashOfConditionDelegateCombo();
    m_analyseModel = new QStandardItemModel(1,ModelResearchFieldsMaxParam,this);
    m_analyseModel->setHeaderData(CONJONCTION,Qt::Horizontal,trUtf8("Conjonction"));
    m_analyseModel->setHeaderData(VARIABLE,Qt::Horizontal,trUtf8("Variable"));
    m_analyseModel->setHeaderData(CONDITIONOFRESEARCH,Qt::Horizontal,trUtf8("Condition"));
    m_analyseModel->setHeaderData(COMPAREVARIABLE,Qt::Horizontal,trUtf8("Autre variable"));
    m_analyseModel->setHeaderData(DATEBEGIN,Qt::Horizontal,trUtf8("Date de début"));
    m_analyseModel->setHeaderData(DATEEND,Qt::Horizontal,trUtf8("Date de fin"));
    m_analyseModel->setHeaderData(TYPEOFDOC,Qt::Horizontal,trUtf8("Dans type"));
    m_analyseModel->setHeaderData(INDEXOFVALUE,Qt::Horizontal,trUtf8("Index de la valeur"));
    m_analyseModel->setHeaderData(TOKENEDIT,Qt::Horizontal,trUtf8("Etiquette dans le terrain"));
    m_analyseModel->setData(m_analyseModel->index(0,VARIABLE),trUtf8("Patient"),Qt::EditRole);
    tableView->setModel(m_analyseModel); 
    tableView->setItemDelegateForColumn(CONJONCTION,new ComboDelegate(this));
    tableView->setItemDelegateForColumn(CONDITIONOFRESEARCH,new ComboDelegate(this));
    tableView->setItemDelegateForColumn(DATEBEGIN,new DateDelegate(this));
    tableView->setItemDelegateForColumn(DATEEND,new DateDelegate(this));
    tableView->setItemDelegateForColumn(TYPEOFDOC,new ComboDelegate(this));
    
    m_resultsModel = new QStandardItemModel;//0,FieldsModelResults_MaxParam,this);
}

void Searchinblobs::parse()
{
    for (int row = 0; row < m_analyseModel->rowCount(); ++row)
    {
        Result rt(this);
        Tools t(this);
        QString choiceOfCondition = m_analyseModel->data(m_analyseModel->index(row,CONDITIONOFRESEARCH),Qt::DisplayRole).toString();
        int condition = m_hashConditions.key(choiceOfCondition);
        QString variable = m_analyseModel->data(m_analyseModel->index(row,VARIABLE),Qt::DisplayRole).toString();
        QString comparevariable = m_analyseModel->data(m_analyseModel->index(row,COMPAREVARIABLE),Qt::DisplayRole).toString();
        QString conjunction = m_analyseModel->data(m_analyseModel->index(row,CONJONCTION),Qt::DisplayRole).toString();
        int rowOfConjunction = t.hashOfConjonctions().key(conjunction);
        QString tableblobs = "RubriquesBlobs";
        QString idprimkey = "RbDate_PrimKey";
        QString blob = "RbDate_DataRub";
        QString refDos = "RbDate_IDDos";
        if (!m_db.tables().contains(tableblobs))
        {
              tableblobs = "rubriquesblobs";
            }
        QStringList resultsList;
        switch(condition){
            case CONTAINS :
                resultsList = allIdWitchContainsVariable(comparevariable,row);
                break;
            case INF :
                resultsList = allIdWhoseVariableIsInfTo(variable,comparevariable,row);
                break;
            case SUP :
                QMessageBox::information(0,trUtf8("Info"),trUtf8("Non implementé."),QMessageBox::Ok);
                break;
            default :
                break;
            }
            rt.setResults(setHashIdAndNamesOfResults(resultsList));
            if (rt.exec() == QDialog::Accepted)
            {
                 switch(rowOfConjunction){
                     case AND :
                         m_listOfReturnedIdWithConjunction = rt.getListOfId();
                         break;
                     case OR :
                         m_listOfReturnedIdWithConjunction << rt.getListOfId();
                         m_listOfReturnedIdWithConjunction.removeDuplicates();
                         break;
                     default :
                         m_listOfReturnedIdWithConjunction = rt.getListOfId();
                         break;
                     }
                }
        }//for        
}

QStringList Searchinblobs::allIdWitchContainsVariable(QString comparevariable,int row)
{
    enum reqBlob
    {
        BLOB=0,
        REFDOS
        };
    Tools t;
    QDate beginDate = m_analyseModel->data(m_analyseModel->index(row,DATEBEGIN),Qt::DisplayRole).toDate();
    QDate endDate = m_analyseModel->data(m_analyseModel->index(row,DATEEND),Qt::DisplayRole).toDate();
    QString begin = beginDate.toString("yyyy-MM-dd");
    QString end = endDate.toString("yyyy-MM-dd");
    QString typeblob = m_analyseModel->data(m_analyseModel-> index(row,TYPEOFDOC),Qt::DisplayRole).toString();
    QString numbertype = t.hashOfTypesOfHeads().key(typeblob);
    QStringList list;
    QStringList listOfConcernedBlobs;
    QString tablehead = "RubriquesHead";
    if (!m_db.tables().contains(tablehead))
        {
              tablehead = "rubriqueshead";
            }
    QString tableblobs = "RubriquesBlobs";
    QString idprimkeyblob = "RbDate_PrimKey";
    QString blob = "RbDate_DataRub";
    QString refDos = "RbDate_IDDos";
    if (!m_db.tables().contains(tableblobs))
    {
        tableblobs = "rubriquesblobs";
            }
    QSqlQuery qHead(m_db);
    QString reqHead = QString("select %1 from %2")
                             .arg("RbDate_RefBlobs_Primkey",tablehead);

    reqHead += QString(" where %1 = '%2' ").arg("RbDate_TypeRub",numbertype);
    reqHead += QString(" and %1 between '%2' and '%3'").arg("RbDate_Date",begin,end);
    if (!qHead.exec(reqHead))
    {
          qWarning() << __FILE__ << QString::number(__LINE__) << qHead.lastError().text() ;
          }
    while (qHead.next())
    {
        listOfConcernedBlobs << qHead.value(0).toString();
        }
    foreach(QString idblob,listOfConcernedBlobs){
        QSqlQuery qy(m_db);
        QString reqBlob = QString("select %1,%2 from %3 where %4 = '%5'").arg(blob,refDos,tableblobs,idprimkeyblob,idblob);
        //reqBlob += " and RbDate_IDDos='1121'";
        if (!qy.exec(reqBlob))
        {
              qWarning() << __FILE__ << QString::number(__LINE__) << qy.lastError().text() ;
              return QStringList();
            }
        while (qy.next())
        {
            QVariant blob = qy.value(BLOB);
            QString refdos = qy.value(REFDOS).toString();
            QTextCodec* codec = QTextCodec::codecForName("utf-8");
            QString blobstring = codec->toUnicode(blob.toByteArray ()) ;
            if (blobstring.contains(comparevariable))
            {
                list << refdos;
                }
            }
        }//foreach
    return list;
}



QStringList Searchinblobs::allIdWhoseVariableIsInfTo(QString variable,QString comparevariable,int row)
{
    qDebug() << __FILE__ << QString::number(__LINE__) << " in allIdWhoseVariableIsInfTo " ;
    QApplication::setOverrideCursor(Qt::WaitCursor);
    enum reqBlob
    {
        BLOB=0,
        REFDOS
        };
    Tools t(this);
    QString conjunction = m_analyseModel->data(m_analyseModel->index(row,CONJONCTION),Qt::DisplayRole).toString();
    int rowOfConjunction = t.hashOfConjonctions().key(conjunction);
    QDate beginDate = m_analyseModel->data(m_analyseModel->index(row,DATEBEGIN),Qt::DisplayRole).toDate();
    QDate endDate = m_analyseModel->data(m_analyseModel->index(row,DATEEND),Qt::DisplayRole).toDate();
    QString typeblob = m_analyseModel->data(m_analyseModel-> index(row,TYPEOFDOC),Qt::DisplayRole).toString();
    int indexOfNumberToCompare = m_analyseModel->data(m_analyseModel-> index(row,INDEXOFVALUE),Qt::DisplayRole).toInt();
    QString numbertype = t.hashOfTypesOfHeads().key(typeblob);
    int type = t.hashIntAndTypeStringsOfHeads().key(typeblob);
    QString begin = beginDate.toString("yyyy-MM-dd");
    QString end = endDate.toString("yyyy-MM-dd");
    QStringList listOfStudiedIds;
    switch(rowOfConjunction){
        case AND :
            qDebug() << __FILE__ << QString::number(__LINE__) << " in AND ";
            listOfStudiedIds = m_listOfReturnedIdWithConjunction;
            break;
        case OR :
            qDebug() << __FILE__ << QString::number(__LINE__) << " in OR ";
            listOfStudiedIds = m_listOfReturnedIdWithConjunction;
            break;
        default :
            qDebug() << __FILE__ << QString::number(__LINE__) << " in default ";
            listOfStudiedIds = m_listOfReturnedIdWithConjunction;
            break;    
        }
    QFile fileForTest(qApp->applicationDirPath()+"/lines.txt");
    if (!fileForTest.open(QIODevice::WriteOnly))
    {
        qWarning() << __FILE__ << QString::number(__LINE__) << "unable to write on lines.txt" ;
        }
    QTextStream streamForTest(&fileForTest);
    QFile fileNumbers(qApp->applicationDirPath()+"/numbers.txt");
    if (!fileNumbers.open(QIODevice::WriteOnly))
    {
          qWarning() << __FILE__ << QString::number(__LINE__) << "unable to write on numbers.txt" ;
        }
    QTextStream streamNumbers(&fileNumbers);

    QString tableblobs = "RubriquesBlobs";
    QString idprimkeyblob = "RbDate_PrimKey";
    QString blob = "RbDate_DataRub";
    QString refDos = "RbDate_IDDos";
    if (!m_db.tables().contains(tableblobs))
    {
        tableblobs = "rubriquesblobs";
            }
    QStringList listOfId;
    QString tablehead = "RubriquesHead";
    if (!m_db.tables().contains(tablehead))
    {
        tablehead = "rubriqueshead";
        }
    QStringList listOfConcernedBlobs;
    QSqlQuery qHeads(m_db);

    if (listOfStudiedIds.size()<1)
    {
        QString requeteHead = QString("select %1 from %2 where %3 = '%4'")
                             .arg("RbDate_RefBlobs_Primkey",tablehead,"RbDate_TypeRub",numbertype);
                             qDebug() << __FILE__ << QString::number(__LINE__) << " requete =" << requeteHead ;
                             requeteHead += QString(" and %1 between '%2' and '%3'").arg("RbDate_Date",begin,end);
        if (!qHeads.exec(requeteHead))
        {
            qWarning() << __FILE__ << QString::number(__LINE__) << qHeads.lastError().text() ;
            }
        while (qHeads.next())
        {
            QString idBlog = qHeads.value(0).toString();
            listOfConcernedBlobs << idBlog;
            }
        }
    else
    {
        for (int i = 0; i < listOfStudiedIds.size(); ++i)
        {
              QString requeteHead = QString("select %1 from %2 where %3 = '%4'")
                             .arg("RbDate_RefBlobs_Primkey",tablehead,"RbDate_TypeRub",numbertype);
                             qDebug() << __FILE__ << QString::number(__LINE__) << " requete =" << requeteHead ;
              requeteHead += QString(" and %1 between '%2' and '%3'").arg("RbDate_Date",begin,end);
              QString guid = listOfStudiedIds[i];
              requeteHead += QString(" and %1 = '%2'").arg("RbDate_IDDos",guid);
              //qDebug() << __FILE__ << QString::number(__LINE__) << " requeteHead =" << requeteHead ;
              if (!qHeads.exec(requeteHead))
              {
                  qWarning() << __FILE__ << QString::number(__LINE__) << qHeads.lastError().text() ;
                  }
              while (qHeads.next())
              {
                  QString idBlog = qHeads.value(0).toString();
                  listOfConcernedBlobs << idBlog;
                  }
            }
        }

    switch(type){
        case DOCUMENTS :
            foreach(QString idblob,listOfConcernedBlobs){
                    listOfId << searchInfInDocuments(blob,refDos,tableblobs,idprimkeyblob,idblob,variable,comparevariable,indexOfNumberToCompare);
                    }
            break;
        case TERRAIN :
            foreach(QString idblob,listOfConcernedBlobs){
                listOfId << searchInTerrain(blob,refDos,tableblobs,idprimkeyblob,idblob,variable,comparevariable,row);
                }
            break;
        case OBSERVATION :
            foreach(QString idblob,listOfConcernedBlobs){
                listOfId << searchInObservation(blob,refDos,tableblobs,idprimkeyblob,idblob,variable,comparevariable,row);
                }
            break;
        default :
            break;    
        }

    QApplication::restoreOverrideCursor();
    return listOfId;
}

QHash<QString,QString> Searchinblobs::setHashIdAndNamesOfResults(QStringList resultsList)
{
    QHash<QString,QString> hash;
    QString nomDoss = "FchGnrl_NomDos";
    QString prenomDoss = "FchGnrl_Prenom";
    QString idDoss = "FchGnrl_IDDos";
    QString table = "IndexNomPrenom";
    if (!m_db.tables().contains(table))
        {
              table = "indexnomprenom";
            }
    foreach(QString idstring,resultsList){
        QSqlQuery query(m_db);
        QString requete = QString("select %1 , %2 from %3 where %4 = '%5'").arg(nomDoss,prenomDoss,table,idDoss,idstring);
        //qDebug() << __FILE__ << QString::number(__LINE__) << " requete =" << requete ;
        if (!query.exec(requete))
        {
              qWarning() << __FILE__ << QString::number(__LINE__) << query.lastError().text() ;
              return QHash<QString,QString>();
            }
        while (query.next())
        {
            QString name = query.value(NAME).toString();
            QString surname = query.value(SURNAME).toString();
            hash.insert(idstring,name+";"+surname);
            }
        }
    return hash;
}

QStringList Searchinblobs::searchInObservation(QString blob,QString refDos,QString tableblobs,QString idprimkeyblob,QString idblob,QString variable,QString comparevariable,int row)
{
    enum reqBlob
    {
        BLOB=0,
        REFDOS
        };
    enum inLine
    {
        TEXT_INLINE=0,
        RESULT_INLINE
        };
    enum TypeOfValueToCompare
    {
        SIMPLEVALUE=0,
        VALUESEPARATEDBYSLASH
        };
        int typeofvaluetocompare = 0;
        if (comparevariable.contains(QRegExp("[0-9]/[0-9]")))
        {
              typeofvaluetocompare = int(VALUESEPARATEDBYSLASH);
            }
        //int a = indexOfNumberToCompare;
        QRegExp span = QRegExp("</span>");
        QRegExp spanstylebefore = QRegExp("<span style=\"font-family:Arial;font-size:.*pt;font-weight:600;color:#0000ff\">");
        QRegExp spanstyleafter = QRegExp("<span style=\"font-family:Arial;font-size:.*pt\">");
        QString result;
        QSqlQuery qy(m_db);
        QStringList listOfId;
        QString reqBlob = QString("select %1,%2 from %3 where %4 = '%5'").arg(blob,refDos,tableblobs,idprimkeyblob,idblob);
        //reqBlob += " and RbDate_IDDos='1121'";
        if (!qy.exec(reqBlob))
        {
              qWarning() << __FILE__ << QString::number(__LINE__) << qy.lastError().text() ;
              return QStringList();
            }
        while (qy.next())
        {
            QString blob = qy.value(BLOB).toString();
            QString refdos = qy.value(REFDOS).toString();
            QStringList bloblist;
            if (blob.contains("<br />"))
            {
                  bloblist = blob.split("<br />");
                  }
            else if (blob.contains("<br/>"))
            {
                  bloblist = blob.split("<br/>");
                }
            else if (blob.contains("</p>"))
            {
                  bloblist = blob.split("</p>");
                }
            
            else
            {
                qWarning() << __FILE__ << QString::number(__LINE__) << "pas de separateur retrouve" ;
                return QStringList();
                }
            foreach(QString line,bloblist){
                if (line.contains(variable))
                {
                      line.remove(span);
                      line.remove(spanstylebefore);
                      line.remove(spanstyleafter);
                      //qDebug() << __FILE__ << QString::number(__LINE__) << " line  =" << line ;
                      if (line.contains(":"))
                      {
                            QStringList listinline;
                            listinline = line.split(":");
                            if (listinline.size()>1)
                            {
                                  result = listinline[RESULT_INLINE];
                                }                            
                          }
                    }
                }
            
            
            if (result.contains(QRegExp("[a-z]",Qt::CaseInsensitive)))
            {
                  QStringList list;
                  list = result.split(" ",QString::SkipEmptyParts);
                  foreach(QString item,list){
                      if (item.contains(QRegExp("[0-9]/[0-9]"))&& typeofvaluetocompare == VALUESEPARATEDBYSLASH)
                      {
                            QStringList l;
                            l = item.split("/");
                            result = l.join("+");
                            QStringList ltocompare;
                            ltocompare = comparevariable.split("/");
                            if (l[0].toInt() <= ltocompare[0].toInt() && l[1].toInt() <= ltocompare[1].toInt())
                            {
                                  listOfId << refdos;
                                }
                            else // pour ne garder que la derniere valeur de TA
                            {
                                if (listOfId.contains(refdos))
                                {
                                      listOfId.removeAll(refdos);
                                    }
                                }
                          }
                      else if (item.contains(QRegExp("[0-9]"))&& typeofvaluetocompare == SIMPLEVALUE)
                      {
                            result = item;
                            if (result.toInt() < comparevariable.toInt())
                            {
                                  listOfId << refdos;
                                }
                          }
                      else
                      {
                          qWarning() << __FILE__ << QString::number(__LINE__) << "no numbers found , result = " << result;
                          }
                }
            }
            else
            {
                if (result.contains(QRegExp("[0-9]/[0-9]"))&& typeofvaluetocompare == VALUESEPARATEDBYSLASH)
                {
                            QStringList l;
                            l = result.split("/");
                            result = l.join("+");
                            QStringList ltocompare;
                            ltocompare = comparevariable.split("/");
                            if (l[0].toInt() <= ltocompare[0].toInt() && l[1].toInt() <= ltocompare[1].toInt())
                            {
                                  listOfId << refdos;
                                  QString exline = textEdit->toHtml();
                                  exline += result+":"+refdos+"<br/>";
                                  textEdit->setHtml(exline);
                                }
                          }
                else if (result.contains(QRegExp("[0-9]"))&& typeofvaluetocompare == SIMPLEVALUE)
                {
                      if (result.toInt() < comparevariable.toInt())
                      {
                            listOfId << refdos;
                          }
                    }
                else
                {
                    qWarning() << __FILE__ << QString::number(__LINE__) << "no numbers found , result = " << result;
                    }
                }
                }//while q.next()
    return listOfId;
}

QStringList Searchinblobs::searchInfInDocuments(QString blob,QString refDos,QString tableblobs,QString idprimkeyblob,QString idblob,QString variable,QString comparevariable, int indexOfNumberToCompare)
{
    enum reqBlob
    {
        BLOB=0,
        REFDOS
        };
        int a = indexOfNumberToCompare;
        QSqlQuery qy(m_db);
        QStringList listOfId;
        QString reqBlob = QString("select %1,%2 from %3 where %4 = '%5'").arg(blob,refDos,tableblobs,idprimkeyblob,idblob);
        //reqBlob += " and RbDate_IDDos='1121'";
        if (!qy.exec(reqBlob))
        {
              qWarning() << __FILE__ << QString::number(__LINE__) << qy.lastError().text() ;
              return QStringList();
            }
        while (qy.next())
        {
            QString blob = qy.value(BLOB).toString();
            QString refdos = qy.value(REFDOS).toString();
            QStringList bloblist;
            //qDebug() << __FILE__ << QString::number(__LINE__) << " blob =" << blob ;
            if (blob.contains("<br />"))
            {
                  bloblist = blob.split("<br />");
                  }
            else if (blob.contains("<br/>"))
            {
                  bloblist = blob.split("<br/>");
                }
            else if (blob.contains("</p>"))
            {
                  bloblist = blob.split("</p>");
                }
            
            else
            {
                //QMessageBox::warning(0,trUtf8("Warning"),trUtf8("blob ne contient pas de séparateur connu"),QMessageBox::Ok);
                return QStringList();
                }
                
            //qDebug() << __FILE__ << QString::number(__LINE__) << " variable =" << variable ;
            if (blob.contains(variable,Qt::CaseInsensitive))
            {
                  foreach(QString line,bloblist){
                      if (line.contains(variable,Qt::CaseInsensitive)&& !line.contains("TEX") && !line.contains("RES"))
                      {
                            QString exline = textEdit->toHtml();
                            exline += line+":"+refdos+"<br/>";
                            textEdit->setHtml(exline);
                            QStringList listInLine;
                            line.simplified();
                            listInLine = line.split(" ",QString::SkipEmptyParts);
                            listInLine.removeAll(".");
                            qDebug() << __FILE__ << QString::number(__LINE__) << " listInLine =" << listInLine.join("_") ;
                            if (listInLine.size()< a)
                            {
                                  qWarning() << __FILE__ << QString::number(__LINE__) << "index to far" ;
                                  return QStringList();
                                }
                            
                            foreach(QString w,listInLine){
                                qDebug() << __FILE__ << QString::number(__LINE__) << "w before = " << w;
                                if (a <1 )
                                {
                                      continue;
                                    }
                                if (w.contains(QRegExp("[0-9]")) && !w.contains(QRegExp("[a-z]",Qt::CaseInsensitive)) && w == listInLine[a-1])
                                {
                                      if (w.contains(","))
                                      {
                                            w.replace(",",".");
                                          }
                                    if (w.toDouble()<comparevariable.toDouble())
                                    {
                                          qDebug() << __FILE__ << QString::number(__LINE__) << "w = " << w;
                                          listOfId << refdos;
                                        }
                                    }
                                }
                              }
                      }
                   }//if blob contains
                
            }//while q.next()
    return listOfId;
}

QStringList Searchinblobs::searchInTerrain(QString blob,QString refDos,QString tableblobs,QString idprimkeyblob,QString idblob,QString variable,QString comparevariable,int row)
{
    enum reqBlob
    {
        BLOB=0,
        REFDOS
        };
    QStringList listOfId;
    QSqlQuery qy(m_db);
    QString reqBlob = QString("select %1,%2 from %3 where %4 = '%5'").arg(blob,refDos,tableblobs,idprimkeyblob,idblob);
        //reqBlob += " and RbDate_IDDos='1121'";
    if (!qy.exec(reqBlob))
    {
              qWarning() << __FILE__ << QString::number(__LINE__) << qy.lastError().text() ;
              return QStringList();
        }
    while (qy.next())
    {
        QString blob = qy.value(BLOB).toString();
        QString refdos = qy.value(REFDOS).toString();
        QString n;
        double result = 0.00;
        QString token;
        token = m_analyseModel->data(m_analyseModel-> index(row,TOKENEDIT),Qt::DisplayRole).toString();
        if (blob.contains(token))
        {
              QStringList partedBlob = blob.split(token);
              if (partedBlob.size()<2)
              {
                    qWarning() << __FILE__ << QString::number(__LINE__) << "unable to part blob" ;
                    return QStringList();
                  }
              QString secondpart = partedBlob[1];
              QStringList secondpartlist;
              secondpartlist = secondpart.split("\n");
              foreach(QString line,secondpartlist){
                  if (line.contains("["))
                  {
                        break;
                      }
                  if (line.contains("=")&& !line.contains("Mod"))
                  {
                        QStringList l = line.split("=");
                        if (l.size()>1)
                        {
                              n = l[1];
                              if (n.contains(","))
                              {
                                    n.replace(",",".");
                                  }

                            }
                      }
                  }//foreach
                result = n.toDouble();
                if (result < comparevariable.toDouble()&& result !=0.00)
                {
                    listOfId << refdos;
                    QString exline = textEdit->toHtml();
                    exline += n+":"+refdos+"<br/>";
                    textEdit->setHtml(exline);
                    }
            }
        }
        
    return listOfId;
}

void Searchinblobs::addarequest()
{
    if (!m_analyseModel->insertRows(m_analyseModel->rowCount(),1,QModelIndex()))
    {
          qWarning() << __FILE__ << QString::number(__LINE__) << "unable to insert row";
        }
    m_analyseModel->submit();
}

void Searchinblobs::deleterequest()
{
    int row = tableView->currentIndex().row();
    
    if (!m_analyseModel->removeRows(row,1,QModelIndex()))
    {
          qWarning() << __FILE__ << QString::number(__LINE__) << "unable to remove row" ;
        }
    m_analyseModel->submit();
}

