#include <QtGui>
#include <QtCore>
#include "lecturedialog.h"
#include "lectlabel.h"
#include "string.h"
extern QString g_pathabsolu;
extern int g_increment;
//class Lecturalarme;
mylabel::mylabel(QString& nom):QLabel(0,Qt::Window|Qt::WindowStaysOnTopHint)
{
  setAttribute(Qt::WA_DeleteOnClose);
  QString name = nom;//récupération du nom du prénom patient et de la cause de l'alerte.
  ////////////////////////création du widget//////////////////////////////////////
  m_gridlayout = new QGridLayout;
  m_i=0;
  m_label = new QLabel;
  resize(300,300);
  //////////////////récupérer l'image////////////////////////////////////////////
  QStringList listeimage;
  QString m_image;
  QFile fileimage(""+g_pathabsolu+"/image.ini");
  if(!fileimage.open(QIODevice::ReadOnly|QIODevice::Text)){
      QMessageBox::warning(0,tr("erreur"),trUtf8("Le fichier image.ini ne peut s'ouvrir."),QMessageBox::Ok,0,Qt::Dialog | Qt::WindowStaysOnTopHint);
  }
  QTextStream streamimage(&fileimage);
      while(!streamimage.atEnd()){
          QString ligne = streamimage.readLine();
          listeimage << ""+ligne+"";
      }
  m_image = listeimage[0];
  QPixmap pixmap(""+g_pathabsolu+"/"+m_image+"");
  QBrush brush(pixmap);
  m_palette.setBrush(QPalette::Window,brush);
  QString texteavant = "<html><body><font size = 4 color = ""blue""><bold>""une alerte<br>concernant "+name+"<br>est arrivée""</bold></font></body></html>";
  std::string texteavantstr = texteavant.toStdString();
  const char *textechar = texteavantstr.c_str();
  QString textealerte = trUtf8(textechar);

//////// construire label et positionner le layout///////////////////////////////

    QFont font;
    font.setPointSize(12);
    m_label->setFont(font);
    m_label->setFrameShape(QFrame::Box);
    m_label->setLineWidth(3);
    m_label->setMidLineWidth(0);
    m_label->setPalette(m_palette);
    m_label->setAutoFillBackground(true);
    m_label->setText(""+textealerte+"");
    m_gridlayout->addWidget(m_label, 0, 0, 1, 1);
  setLayout(m_gridlayout);

  setMinimumSize(300,1);//démarrer petit
  m_timer = new QTimer;//créer le timer qui va faire croitre le widget.
  connect(m_timer,SIGNAL(timeout()),this,SLOT(croitre()));
  m_timer->start(10);
}

mylabel::~mylabel(){
    delete m_gridlayout;
    delete m_label;
    delete m_timer;
    }

/*void mylabel::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
	{
emit close();
	}
}*/
void mylabel::closeEvent(QCloseEvent *event){
  g_increment--;
  event->accept();
}

void mylabel::croitre(){
  int i                         = m_i - 20*g_increment;
  QDesktopWidget *desktopwidget = new QDesktopWidget;
  QRect rect                    = desktopwidget->availableGeometry(this);
  //int bottomleftx = rect.bottomLeft().x();
  //int bottomlefty = rect.bottomLeft().y();
  int bottomrightx              = rect.bottomRight().x();
  int bottomrighty              = rect.bottomRight().y();
  //int topleftx = rect.topLeft().x();
  //int toplefty = rect.topLeft().y();
  //int toprightx = rect.topRight().x();
  //int toprighty = rect.topRight().y();
  setGeometry(QRect(bottomrightx-300, bottomrighty-i, 300, i));
  show();
  m_i++;
  if(m_i>299){
      m_timer->stop();//sinon le widget continue à monter.
  }
}
