Logo Search packages:      
Sourcecode: edfbrowser version File versions  Download package

load_montage_dialog.cpp

/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2007, 2008, 2009 Teunis van Beelen
*
* teuniz@gmail.com
*
***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
***************************************************************************
*
* This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*
***************************************************************************
*/



#include "load_montage_dialog.h"



UI_LoadMontagewindow::UI_LoadMontagewindow(QWidget *parent, char *path)
{
  int i;

  mainwindow = (UI_Mainwindow *)parent;

  mtg_path[0] = 0;

  if(path!=NULL)
  {
    strcpy(mtg_path, path);
  }

  if(mainwindow->files_open==1)
  {
    LoadMontageDialog = NULL;
    LoadButtonClicked();
    return;
  }

  LoadMontageDialog = new QDialog;

  LoadMontageDialog->setMinimumSize(QSize(800, 180));
  LoadMontageDialog->setMaximumSize(QSize(800, 180));
  LoadMontageDialog->setWindowTitle("Load montage");
  LoadMontageDialog->setModal(TRUE);
  LoadMontageDialog->setAttribute(Qt::WA_DeleteOnClose, TRUE);

  label1 = new QLabel(LoadMontageDialog);
  label1->setGeometry(QRect(10, 10, 780, 25));
  label1->setText("Choose to which file you want to apply the montage:");

  filelist = new QListWidget(LoadMontageDialog);
  filelist->setGeometry(QRect(10, 35, 780, 75));
  filelist->setSelectionBehavior(QAbstractItemView::SelectRows);
  filelist->setSelectionMode(QAbstractItemView::SingleSelection);
  for(i=0; i<mainwindow->files_open; i++)
  {
    new QListWidgetItem(mainwindow->edfheaderlist[i]->filename, filelist);
  }

  LoadButton = new QPushButton(LoadMontageDialog);
  LoadButton->setGeometry(QRect(10, 140, 100, 25));
  LoadButton->setText("Load");

  CloseButton = new QPushButton(LoadMontageDialog);
  CloseButton->setGeometry(QRect(690, 140, 100, 25));
  CloseButton->setText("Close");

  QObject::connect(CloseButton,  SIGNAL(clicked()), LoadMontageDialog, SLOT(close()));
  QObject::connect(LoadButton,   SIGNAL(clicked()), this,              SLOT(LoadButtonClicked()));

  filelist->setCurrentRow(mainwindow->files_open - 1);

  LoadMontageDialog->exec();
}




void UI_LoadMontagewindow::LoadButtonClicked()
{
  int i, j, k, n,
      skip,
      found,
      signalcomps_read=0,
      signals_read,
      signal_cnt,
      filters_read,
      filter_cnt,
      islpf,
      len;

  char *result,
       scratchpad[512],
       str[128];

  double frequency;

  struct date_time_struct date_time;


  struct xml_handle *xml_hdl;

  struct signalcompblock *newsignalcomp;

  if(mainwindow->files_open==1)  n = 0;
  else  n = filelist->currentRow();

  if(mtg_path[0]==0)
  {
    QFileDialog fchooser;

    fchooser.setFileMode(QFileDialog::ExistingFile);
    fchooser.setAcceptMode(QFileDialog::AcceptOpen);
    fchooser.setWindowTitle("Load montage");
    fchooser.setLabelText(QFileDialog::FileName, "Name of montage:");
    fchooser.setDefaultSuffix("mtg");
  #ifdef Q_WS_X11
    fchooser.setDirectory(getenv("HOME"));
  #endif
  #ifdef Q_WS_MAC
    fchooser.setDirectory(getenv("HOME"));
  #endif
    fchooser.setFilter("Montage files (*.mtg *.MTG)");

    if(mainwindow->recent_montagedir[0]!=0)
    {
      fchooser.setDirectory(mainwindow->recent_montagedir);
    }

    if(!(fchooser.exec() == QDialog::Accepted))
    {
      return;
    }

    strcpy(mtg_path, fchooser.selectedFiles().at(0).toLatin1().data());
  }

  strcpy(mainwindow->recent_montagedir, mtg_path);
  len = strlen(mainwindow->recent_montagedir);
  if(len)
  {
    for(i=len-1; i>=0; i--)
    {
      if((mainwindow->recent_montagedir[i] == '/')||(mainwindow->recent_montagedir[i] == '\\'))
      {
        break;
      }
    }
    mainwindow->recent_montagedir[i+1] = 0;
  }

  xml_hdl = xml_get_handle(mtg_path);
  if(xml_hdl==NULL)
  {
    UI_Messagewindow popuperror("Error", "Error, can not open file for reading.");
    return;
  }

  if(strcmp(xml_hdl->elementname, PROGRAM_NAME "_montage"))
  {
    UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
    xml_close(xml_hdl);
    return;
  }

////////////////////////////////////////////

  for(k=0; k<mainwindow->signalcomps; )
  {
    if(mainwindow->signalcomp[k]->filenum==n)
    {
      if(mainwindow->signalcomp[k]->hascursor2)
      {
/*        crosshair_2_active = 0;
        crosshair_2_moving = 0;*/
      }

      if(mainwindow->signalcomp[k]->hascursor1)
      {
//         crosshair_1_active = 0;
//         crosshair_2_active = 0;
//         crosshair_1_moving = 0;
//         crosshair_2_moving = 0;

        for(i=0; i<mainwindow->signalcomps; i++)
        {
          mainwindow->signalcomp[i]->hascursor2 = 0;
        }
      }

      for(i=0; i<mainwindow->signalcomp[k]->filter_cnt; i++)
      {
        free(mainwindow->signalcomp[k]->filter[i]);
      }

      mainwindow->signalcomp[k]->filter_cnt = 0;

      free(mainwindow->signalcomp[k]);

      for(i=k; i<mainwindow->signalcomps - 1; i++)
      {
        mainwindow->signalcomp[i] = mainwindow->signalcomp[i + 1];
      }

      mainwindow->signalcomps--;

      k = 0;
    }
    else
    {
      k++;
    }
  }

////////////////////////////////////////////

  while(1)
  {
    skip = 0;

    xml_goto_root(xml_hdl);

    signals_read = 0;

    if(xml_goto_nth_element_inside(xml_hdl, "signalcomposition", signalcomps_read))
    {
      break;
    }

    newsignalcomp = (struct signalcompblock *)calloc(1, sizeof(struct signalcompblock));
    if(newsignalcomp==NULL)
    {
      UI_Messagewindow popuperrormessage("Error", "Internal error: Memory allocation error:\n\"new signal composition\"");
      xml_close(xml_hdl);
      return;
    }

    if(xml_goto_nth_element_inside(xml_hdl, "num_of_signals", 0))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    result = xml_get_content_of_element(xml_hdl);
    signal_cnt = atoi(result);
    s_free(result);
    if((signal_cnt<1)||(signal_cnt>256))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    newsignalcomp->edfhdr = mainwindow->edfheaderlist[n];
    newsignalcomp->filenum = n;
    newsignalcomp->num_of_signals = signal_cnt;
    newsignalcomp->hascursor1 = 0;
    newsignalcomp->hascursor2 = 0;
    newsignalcomp->hasoffsettracking = 0;
    newsignalcomp->hasgaintracking = 0;
    newsignalcomp->screen_offset = 0;
    newsignalcomp->filter_cnt = 0;
    newsignalcomp->hasruler = 0;

    xml_go_up(xml_hdl);
    if(xml_goto_nth_element_inside(xml_hdl, "voltpercm", 0))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    result = xml_get_content_of_element(xml_hdl);
    newsignalcomp->voltpercm = atof(result);
    if(newsignalcomp->voltpercm==0.0)  newsignalcomp->voltpercm = 0.000000001;
    s_free(result);

    xml_go_up(xml_hdl);
    if(xml_goto_nth_element_inside(xml_hdl, "screen_offset", 0))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    result = xml_get_content_of_element(xml_hdl);
    newsignalcomp->screen_offset = atoi(result);
    s_free(result);

    xml_go_up(xml_hdl);
    if(xml_goto_nth_element_inside(xml_hdl, "color", 0))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    result = xml_get_content_of_element(xml_hdl);
    newsignalcomp->color = atoi(result);
    s_free(result);

    xml_go_up(xml_hdl);
    if(xml_goto_nth_element_inside(xml_hdl, "filter_cnt", 0))
    {
      UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
      free(newsignalcomp);
      xml_close(xml_hdl);
      return;
    }
    result = xml_get_content_of_element(xml_hdl);
    filter_cnt = atoi(result);
    newsignalcomp->filter_cnt = filter_cnt;
    s_free(result);

    xml_go_up(xml_hdl);
    for(signals_read=0; signals_read<signal_cnt; signals_read++)
    {
      if(xml_goto_nth_element_inside(xml_hdl, "signal", signals_read))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }

      if(xml_goto_nth_element_inside(xml_hdl, "factor", 0))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }
      result = xml_get_content_of_element(xml_hdl);
      newsignalcomp->factor[signals_read] = atoi(result);
      s_free(result);

      xml_go_up(xml_hdl);
      if(xml_goto_nth_element_inside(xml_hdl, "label", 0))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }
      result = xml_get_content_of_element(xml_hdl);
      found = 0;
      for(i=0; i<newsignalcomp->edfhdr->edfsignals; i++)
      {
        if(!strcmp(newsignalcomp->edfhdr->edfparam[i].label, result))
        {
          newsignalcomp->edfsignal[signals_read] = i;
          found = 1;
          break;
        }
      }
      s_free(result);

      if(!found)
      {
        free(newsignalcomp);
        skip = 1;
        signalcomps_read++;
        xml_go_up(xml_hdl);
        xml_go_up(xml_hdl);
        break;
      }

      if(signals_read)
      {
        if(newsignalcomp->edfhdr->edfparam[newsignalcomp->edfsignal[signals_read]].smp_per_record
          != newsignalcomp->edfhdr->edfparam[newsignalcomp->edfsignal[0]].smp_per_record)
        {
          free(newsignalcomp);
          skip = 1;
          signalcomps_read++;
          xml_go_up(xml_hdl);
          xml_go_up(xml_hdl);
          break;
        }
      }

      newsignalcomp->sensitivity[signals_read] = newsignalcomp->edfhdr->edfparam[newsignalcomp->edfsignal[signals_read]].bitvalue / (newsignalcomp->voltpercm * mainwindow->pixelsizefactor);

      if(!signals_read)
      {
        newsignalcomp->signallabel[0] = 0;
      }

      if(signal_cnt>1)
      {
        if(newsignalcomp->factor[signals_read]<0)
        {
          strcat(newsignalcomp->signallabel, "- ");
        }
        else
        {
          if(signals_read)
          {
            strcat(newsignalcomp->signallabel, "+ ");
          }
        }
      }
      strcpy(str, newsignalcomp->edfhdr->edfparam[newsignalcomp->edfsignal[signals_read]].label);
      strip_types_from_label(str);
      strcat(newsignalcomp->signallabel, str);
      strcat(newsignalcomp->signallabel, " ");
      len = strlen(newsignalcomp->signallabel);
      for(k=(len-1); k>0; k--)
      {
        if(newsignalcomp->signallabel[k]!=' ')  break;
      }
      newsignalcomp->signallabel[k+2] = 0;

      newsignalcomp->file_duration = newsignalcomp->edfhdr->long_data_record_duration * newsignalcomp->edfhdr->datarecords;

      newsignalcomp->signallabellen = strlen(newsignalcomp->signallabel);

      xml_go_up(xml_hdl);
      xml_go_up(xml_hdl);
    }

    if(skip)  continue;

    strcpy(newsignalcomp->physdimension, newsignalcomp->edfhdr->edfparam[newsignalcomp->edfsignal[0]].physdimension);
    len = strlen(newsignalcomp->physdimension);
    for(k=(len-1); k>0; k--)
    {
      if(newsignalcomp->physdimension[k]!=' ')  break;
    }
    newsignalcomp->physdimension[k+1] = 0;

    for(filters_read=0; filters_read<filter_cnt; filters_read++)
    {
      if(xml_goto_nth_element_inside(xml_hdl, "filter", filters_read))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }

      if(xml_goto_nth_element_inside(xml_hdl, "LPF", 0))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }
      result = xml_get_content_of_element(xml_hdl);
      islpf = atoi(result);
      s_free(result);

      xml_go_up(xml_hdl);
      if(xml_goto_nth_element_inside(xml_hdl, "frequency", 0))
      {
        UI_Messagewindow popuperror("Error", "There seems to be an error in this montage file.");
        free(newsignalcomp);
        xml_close(xml_hdl);
        return;
      }
      result = xml_get_content_of_element(xml_hdl);
      frequency = atof(result);
      s_free(result);

       newsignalcomp->filter[filters_read] = create_filter(islpf,
                                                            frequency,
                                                            (double)(newsignalcomp->edfhdr->edfparam[0].smp_per_record) / newsignalcomp->edfhdr->data_record_duration);

      newsignalcomp->filter_cnt = filters_read + 1;

      xml_go_up(xml_hdl);
      xml_go_up(xml_hdl);
    }

    mainwindow->signalcomp[mainwindow->signalcomps] = newsignalcomp;

    mainwindow->signalcomps++;

    signalcomps_read++;
  }

  xml_close(xml_hdl);

  if(LoadMontageDialog!=NULL) LoadMontageDialog->close();

  utc_to_date_time(mainwindow->edfheaderlist[mainwindow->sel_viewtime]->utc_starttime, &date_time);

  date_time.month_str[0] += 32;
  date_time.month_str[1] += 32;
  date_time.month_str[2] += 32;

  snprintf(scratchpad, 512, PROGRAM_NAME "  %s  %i %s %i",
                mainwindow->edfheaderlist[mainwindow->sel_viewtime]->patient,
                date_time.day,
                date_time.month_str,
                date_time.year);
  len = strlen(scratchpad);
  for(j=0; j<len; j++)
  {
    if(scratchpad[j]=='_')
    {
      scratchpad[j] = ' ';
    }
  }
  mainwindow->setWindowTitle(scratchpad);

  mainwindow->setup_viewbuf();
}


void UI_LoadMontagewindow::strip_types_from_label(char *label)
{
  int i,
      len;


  len = strlen(label);
  if(len<16)
  {
    return;
  }

  if((!(strncmp(label, "EEG ", 4)))
   ||(!(strncmp(label, "ECG ", 4)))
   ||(!(strncmp(label, "EOG ", 4)))
   ||(!(strncmp(label, "ERG ", 4)))
   ||(!(strncmp(label, "EMG ", 4)))
   ||(!(strncmp(label, "MEG ", 4)))
   ||(!(strncmp(label, "MCG ", 4))))
  {
    if(label[4]!=' ')
    {
      for(i=0; i<(len-4); i++)
      {
        label[i] = label[i+4];
      }

      for(; i<len; i++)
      {
        label[i] = ' ';
      }
    }
  }
}









Generated by  Doxygen 1.6.0   Back to index