/***************************************************************************
                          knutdock.cpp  -  description
                             -------------------
    begin                : �t zari 23 2003
    copyright            : (C) 2003 by Daniel Prynych
    email                : Daniel.Prynych@alo.cz
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "knutdock.h"
#include "knutnet.h"

#include <kiconloader.h>
#include <tdelocale.h>
#include <tdeapplication.h>
#include <tdeaboutapplication.h>
#include <tdemessagebox.h>

#include <tqpainter.h>
#include <tqtooltip.h>
#include <tqpixmap.h>
#include <tqbitmap.h>
#include <tqstring.h>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

//#include <iostream>

//#define ICON_SIZE 24
#define ICON_SIZE 22

KNutDock::KNutDock(KNutVarData::upsInfoDef* activeUpsInfo, KNutVarData::SettingDataDef* settingData, KNutUpsData* upsRecords){
  TQString valueName;

  m_settingData = settingData;
  m_upsRecords = upsRecords;
  m_activeUpsInfo = activeUpsInfo;

//saves backgroundcolor of kicker
  backgroundColor = paletteBackgroundColor();

  m_typeOfIcon = m_settingData->typeOfTDEIcon;

  if (!m_settingData->customTDEIconColor) m_myBGColor = backgroundColor;
  else m_myBGColor = m_settingData->kIconBackgroundColor;

  m_toolTipFlags=m_settingData->toolTipFlags;

  clearValues();


  TDEIconLoader *loader = TDEGlobal::iconLoader();
  m_mainPix = loader->loadIcon ("knutclient",TDEIcon::Small,16);

  // aplications's own icons
  m_errorPix = UserIcon ("knc_error");
  m_connectPix = UserIcon ("knc_conn");
  m_upsPix = UserIcon ("knc_ups");
  m_battPix = UserIcon ("knc_batt");

  createMainMenu();
#define ICON_SIZE 22

  m_statusLayer = 0;
  m_runtimeLayer = 0;

  m_loadLayer = new TQPixmap(ICON_SIZE,ICON_SIZE);
  m_chargeLayer = new TQPixmap(ICON_SIZE,ICON_SIZE);
  m_mainLayer = new TQPixmap(ICON_SIZE,ICON_SIZE);


  initImage();
  changeTDEIcon(); // this function calls function repaintIcon

  }


KNutDock::~KNutDock(){

  if (m_statusLayer) delete m_statusLayer;
  if (m_runtimeLayer) delete m_runtimeLayer;

  delete m_loadLayer;
  delete m_chargeLayer;
  delete m_mainLayer;

  delete m_upsServerMenu;
  delete m_menu;
  }

void KNutDock::resizeEvent ( TQResizeEvent * ){
  initImage();
  changeTDEIcon(); // this function calls function repaintIcon
  }

void KNutDock::changeUps (void) {

  m_upsServerMenu->clear();
  createUpsMenu();
  }


void KNutDock::clearDockValues(void){

  initImage(); // paints standart icon

  clearValues();
  repaintDock(true);
  }


void KNutDock::changeToolTip(void){

  m_toolTipFlags=m_settingData->toolTipFlags;
  toolTip(m_runtimeValue, m_statusValue, m_batteryChargeValue, m_upsLoadValue);
  }


void KNutDock::changeKIBColor(void) {

  if (!m_settingData->customTDEIconColor) m_myBGColor = backgroundColor;
  else m_myBGColor = m_settingData->kIconBackgroundColor; // uses defined color

  repaintDock(true);
  }

void KNutDock::changeTDEIcon (void) {

  m_typeOfIcon = m_settingData->typeOfTDEIcon;
  if ( m_typeOfIcon == generalKI) {

    if (m_mainLayer) {
    delete m_mainLayer;
    m_mainLayer=new TQPixmap(ICON_SIZE,ICON_SIZE);
    }

    if (!m_statusLayer) {
      m_statusLayer = new TQPixmap(ICON_SIZE,ICON_SIZE);
      }
    if (!m_runtimeLayer) {
      m_runtimeLayer = new TQPixmap(ICON_SIZE,ICON_SIZE);
      }
    paintStatus(m_statusValue);
    paintRuntime(m_runtimeValue, m_statusValue);
    }
  else {  // picture Icon
    delete m_statusLayer;
    m_statusLayer = 0;
    delete m_runtimeLayer;
    m_runtimeLayer=0;
    }
  paintLoad(m_upsLoadValue);
  paintCharge(m_batteryChargeValue);

  m_statusWarningValue = m_statusValue & (KNutNet::CAL | KNutNet::TRIM | KNutNet::BOOST
                     | KNutNet::OVER | KNutNet::RB);
  repaintDock(true);
  }

void KNutDock::initImage(void) {
  setPixmap(m_mainPix);
  m_dataOk = idle;
  m_dataOkOld = m_dataOk;
  }

void KNutDock::setNewUPS(void) {
  m_upsServerMenu->clear();
  createUpsMenu();
  }


void KNutDock::repaintDock (const bool always) {
  TQString nutStringVar;
  int myRuntimeValue;
  int myUpsLoadValue;
  int myStatusValue;
  int myStatusBatValue;
  int myBatteryChargeValue;
  bool repaintNow = false;
  // value repaintNow is seted when some ups values are changed
  bool okFloat;
  TQString valueName;
  TQPainter paint;


  if (m_activeUpsInfo->upsNet == 0) { repaint(); return;}

  m_dataOkOld=m_dataOk;

  if ((m_activeUpsInfo->upsNet->getState() == KNutNet::Connected) // paints icons with information values
       && (!m_activeUpsInfo->netError))  {
    // ups is connected dataOK is ok (program shows icon of connecting UPS)
    m_dataOk = ok; // pro paint event udava ze mam pouzit nase ikony

    if (!m_activeUpsInfo->upsNet->existName(m_activeUpsInfo->runtimeVar)) {
      nutStringVar = m_activeUpsInfo->upsNet->readStringVar(m_activeUpsInfo->runtimeVar);
//std::cout << "KNutDock::repaintDock RUNTIME " << nutStringVar << std::endl;
      if (nutStringVar.isEmpty()) myRuntimeValue=knc::numberVarError;
      else {
        myRuntimeValue = (int)nutStringVar.toFloat(&okFloat);
        if (!okFloat) myRuntimeValue = knc::numberVarError;
        }
      }
    else myRuntimeValue = knc::numberVarError;

    myStatusValue = m_activeUpsInfo->upsNet->readStatus();


    switch (m_typeOfIcon) {
      case generalKI:
//    *********STATUS****************
      if (myStatusValue !=m_statusValue  ) {
        m_statusValue = myStatusValue;
        paintStatus(m_statusValue);
        repaintNow = true;
        }

//   **********RUNTIME***********
      myStatusBatValue = myStatusValue & KNutNet::OB; // OB = UPS runs on battery
      if ((myRuntimeValue != m_runtimeValue) || (myStatusBatValue != m_statusBatValue)) {
        // when runtime is changed or flags OB is changed we must repaint icons
        // when OB is changed same items in icon will change color
        m_runtimeValue = myRuntimeValue; // runtime is old value of runtime, one is defined like global value of class
        m_statusBatValue = myStatusBatValue;
        paintRuntime(m_runtimeValue, m_statusValue);
        repaintNow = true;
        }
      break;

      case pictureKI:
//    *********STATUS****************
      if (myStatusValue !=m_statusValue  ) {
        m_statusValue = myStatusValue;
        m_statusWarningValue = myStatusValue & (KNutNet::CAL | KNutNet::TRIM | KNutNet::BOOST
                         | KNutNet::OVER | KNutNet::RB);
        repaintNow = true;
        }

//   **********RUNTIME***********
      if (myRuntimeValue != m_runtimeValue) {
        m_runtimeValue = myRuntimeValue;
        repaintNow = true;
        }
      break;
      } /////////////////////  end of models

    // Other values are for all modes

//  **********UPS LOAD***************
    if (m_activeUpsInfo->nutVariables == 1) valueName = "LOADPCT";
    else valueName = "ups.load";
    if (!m_activeUpsInfo->upsNet->existName(valueName)) {
      nutStringVar = m_activeUpsInfo->upsNet->readStringVar(valueName);
      if (nutStringVar.isEmpty()) myUpsLoadValue=knc::numberVarError;
      else {
        myUpsLoadValue = (int)nutStringVar.toFloat(&okFloat);
        if (!okFloat) myUpsLoadValue=knc::numberVarError;
        }
      }
    else myUpsLoadValue=knc::numberVarError;

    if (myUpsLoadValue != m_upsLoadValue) {
      m_upsLoadValue = myUpsLoadValue;
      if (m_typeOfIcon == generalKI) paintLoad(m_upsLoadValue);
      repaintNow = true;
      }

//  *********BATTERY CHARGE****************
    if (m_activeUpsInfo->nutVariables == 1) valueName="BATTPCT";
    else valueName="battery.charge";
    if (!m_activeUpsInfo->upsNet->existName(valueName)) {
      nutStringVar = m_activeUpsInfo->upsNet->readStringVar(valueName);
      if (nutStringVar.isEmpty()) myBatteryChargeValue=knc::numberVarError;
      else {
        myBatteryChargeValue = (int)nutStringVar.toFloat(&okFloat);
        if (!okFloat) myBatteryChargeValue = knc::numberVarError;
        }
      }
    else myBatteryChargeValue=knc::numberVarError;

    if (myBatteryChargeValue != m_batteryChargeValue) {
      m_batteryChargeValue = myBatteryChargeValue;
      if (m_typeOfIcon == generalKI)paintCharge(m_batteryChargeValue);
      repaintNow = true;
      }
//  ************* End of values********************

    if (repaintNow || always  || (m_dataOk != m_dataOkOld)) {


      switch (m_typeOfIcon) {
        case generalKI:
          m_mainLayer->fill(m_myBGColor);
//          copyBlt (m_mainLayer,0,0,m_runtimeLayer,0,0,ICON_SIZE,ICON_SIZE);
          bitBlt (m_mainLayer,0,0,m_runtimeLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP);
          bitBlt (m_mainLayer,0,0,m_loadLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP);
          bitBlt (m_mainLayer,0,0,m_chargeLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP);
          bitBlt (m_mainLayer,0,0,m_statusLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP);
        break;
        case pictureKI:
//          if (m_statusValue & KNutNet::OL) bitBlt (m_mainLayer,0,0,m_loadLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP); // icon of UPS
          if (m_statusValue & KNutNet::OL) {  // ONLINE - icon of UPS
            int myLoad;
            TQPainter paint;
            copyBlt (m_mainLayer,0,0,&m_upsPix,0,0,ICON_SIZE,ICON_SIZE); // icon of UPS

            paint.begin(m_mainLayer);
            if (m_upsLoadValue == knc::numberVarError) myLoad=0;
            else myLoad = abs(m_upsLoadValue/10);

            if (m_upsLoadValue != knc::numberVarError) {
            if (myLoad > 12) myLoad=12;
            if (myLoad > 10) myLoad++; // preskocime rysku pro 100 %
            if (myLoad < 9) paint.setPen(TQColor(0,192,0)); //green
            else {
              if (myLoad < 11) paint.setPen(TQColor(255,255,0)); //yellow
              else paint.setPen(TQColor(255,0,0));//red
              }
            for (int i = 19; i < 22; i++) paint.drawLine(i,13,i,13-myLoad +1);

            if (myLoad < 13) {
              paint.setPen(TQColor(168,220,255));
                for (int i = 20; i < 23; i++ ) paint.drawLine(i,1,i,13-myLoad);
                }
              }
            paint.setPen(TQColor(TQt::black)); // line for 100 % load
            paint.drawLine(19,3,22,3);
            if (!m_statusWarningValue) paintOKPoint(paint);
            else paintErrorPoint(paint);
            paint.end();
            }
          else {
            TQPainter paint;
            copyBlt (m_mainLayer,0,0,&m_battPix,0,0,ICON_SIZE,ICON_SIZE); // icon of Battery
            paint.begin(m_mainLayer);
            if (m_batteryChargeValue != knc::numberVarError) {
              if (m_batteryChargeValue < 40) paint.setPen(TQt::red);
              else paint.setPen(TQt::black);
              if (m_batteryChargeValue < 40) paint.setPen(TQt::red);
              else {
                if (m_batteryChargeValue < 50) paint.setPen(TQt::yellow);
                else paint.setPen(TQt::green);
                }
              int delka = abs((16*m_batteryChargeValue)/100);
              delka += abs((delka-1)/4) -1;
              paint.drawLine (2,7,2+delka,7);
              paint.drawLine (2,8,2+delka,8);
              paint.drawLine (2,9,2+delka,9);
              }
            paint.end();
            }
        break;
        }
      toolTip(m_runtimeValue, m_statusValue, m_batteryChargeValue, m_upsLoadValue);

      setPixmap(*m_mainLayer);
      }
    }
  else { // KNutNet->state isn't  connected
     // we must test activeUpsInfo->netError because error can rise higher level
    if (m_activeUpsInfo->netError)
      m_dataOk = error; // error is value not variable
    else {
      if ((m_activeUpsInfo->upsNet->getState() == KNutNet::Connecting) ||
        (m_activeUpsInfo->upsNet->getState() == KNutNet::HostLookup))
        m_dataOk = connecting;
      else m_dataOk = idle;
      }
    if ((m_dataOk != m_dataOkOld) or always) {
      toolTip(0, -1, 0, 0);
      m_dataOkOld=m_dataOk;
      if (m_dataOk == connecting) setPixmap(m_connectPix);
      else {
        if (m_dataOk == error) setPixmap(m_errorPix);
        else  {
          setPixmap(m_mainPix);
          }
        }
      }
    }
  repaint();
  }



void KNutDock::toolTip (int runtime, int status, int batteryCharge, int upsLoad ) {

  TQString text;
  TQString manufacturer = "";
  TQString upsModel = "";
  TQString serial = "";
  TQString firmware = "";
  int myNetErr = 0;

  int min = abs(runtime/60);
  int sec = runtime - (min*60);

  if (m_activeUpsInfo->upsNet != 0) {
    myNetErr= m_activeUpsInfo->upsNet->getError();
    switch (m_activeUpsInfo->nutVariables) {
      case 1:
        manufacturer = m_activeUpsInfo->upsNet->readStringVar("MFR");
        upsModel = m_activeUpsInfo->upsNet->readStringVar("MODEL");
        serial = m_activeUpsInfo->upsNet->readStringVar("SERIAL");
        firmware = m_activeUpsInfo->upsNet->readStringVar("FIRMREV");
      break;
      case 2:
        manufacturer = m_activeUpsInfo->upsNet->readStringVar("ups.mfr");
        upsModel = m_activeUpsInfo->upsNet->readStringVar("ups.model");
        serial = m_activeUpsInfo->upsNet->readStringVar("ups.serial");
        firmware = m_activeUpsInfo->upsNet->readStringVar("ups.firmware");
      break;
      }
    }

  text = i18n("Name")+" : "+m_activeUpsInfo->record.name + "\n"
  +i18n("UPS name")+" : "+m_activeUpsInfo->record.upsName + "\n"
  +i18n("UPS address")+" : "+m_activeUpsInfo->record.upsAddress +"\n"
  +i18n("Status : ");

  if (status == -1) {
    text += i18n("Connection doesn't exist.");
    }
  else {
    if (status & KNutNet::OL) {
      text += i18n("UPS On line");
      }
    else {
      if (status & KNutNet::OB) {
        text += i18n("UPS On battery");
        }
      else {
        if (status & KNutNet::OFF) {
          text += i18n("UPS Of line");
          }
        }
      }

    if (status & KNutNet::LB) {
      text += "\n" + i18n("Status : ") + i18n("UPS Battery is low");
      }
    if (status & KNutNet::RB) {
      text += "\n" + i18n("Status : ") + i18n("Replace battery");
      }
    if (status & KNutNet::CAL) {
      text += "\n" + i18n("Status : ") + i18n("UPS is performing calibration");
      }
    if (status & KNutNet::OVER) {
      text += "\n" + i18n("Status : ") + i18n("UPS is Overload");
      }
    if (status & KNutNet::TRIM) {
      text += "\n" + i18n("Status : ") + i18n("UPS is trimming voltage");
      }
    if (status & KNutNet::BOOST) {
      text += "\n" + i18n("Status : ")+ i18n("UPS is boosting voltage");
      }


    }
  if ((status == -1) && (myNetErr != KNutNet::NoError)) {
    text += "\n" + i18n("Error : ")+ KNutVarData::errorToText(myNetErr);
    }

  if (m_toolTipFlags & TTMFR) text += "\n" + i18n("Manufac. : ")+ manufacturer;

  if (m_toolTipFlags & TTModel) text += "\n" + i18n("Name")+" : "+ upsModel;

  if (m_toolTipFlags & TTSerial) text += "\n" + i18n("Serial")+" : "+ serial;

  if (m_toolTipFlags & TTFirm) text += "\n" + i18n("Firmware")+" : "+ firmware;

  if (m_toolTipFlags & TTRun) {
    if (runtime != knc::numberVarError)
      text += "\n" + i18n("Runtime") + i18n(" : %1:%2 min").arg(min).arg(sec);
    }

  if (m_toolTipFlags & TTCharge) {
    if (batteryCharge != knc::numberVarError)
      text += "\n" + i18n("Battery Charge") + TQString(" : %1 %").arg(batteryCharge);
    }

  if (m_toolTipFlags & TTLoad) {
    if (upsLoad != knc::numberVarError)
      text += "\n" + i18n("UPS Load") + TQString(" : %1 %").arg(upsLoad);
    }

  TQToolTip::remove(this);
  TQToolTip::add(this,text);
 }



/*********************************************************************************************/
/*                                                                                           */
/*                         PRIVATE SLOTS                                                     */
/*                                                                                           */
/*********************************************************************************************/

 void KNutDock::slotShowMyAbout() {
  TDEAboutApplication *dialog;

  dialog= new TDEAboutApplication;
  dialog->exec();
  delete dialog;
}

void KNutDock::slotExitApplication (void) {

  if (m_settingData->areYouSure) {
    if (KMessageBox::warningYesNo (0,i18n("Are you sure ?")) == 3)
      emit endApplication();
    }
  else emit endApplication();
  }

void KNutDock::slotUpsName(int i) {

  emit UPSChanged(m_upsRecords->getName(i-101));
  }


void KNutDock::slotMinimize(void) {

  emit minimize ();
  }

void KNutDock::slotPreferences(void) {

  emit preferences();
  }

void KNutDock::slotPrintUPSVars(void) {

  emit printUPSVars();
  }

void KNutDock::slotInstComms(void) {

  emit instComms();
  }

void KNutDock::slotRWVars(void) {

  emit RWVars();
  }

void KNutDock::slotRefreshConnection(void) {

  emit makeRefreshConnection();
  }


//------------------------------------------

void KNutDock::mousePressEvent(TQMouseEvent *e) {

//tqDebug("KNutDock::mousePressEvent");
  if( e->button()==TQt::LeftButton  ) emit activeMainWindow();
  if( e->button()==TQt::RightButton ) m_menu->popup(e->globalPos());
  }




void KNutDock::paintEvent (TQPaintEvent *e) {

  KSystemTray::paintEvent(e);
  if ((m_activeUpsInfo->upsNet->getState() == KNutNet::Connected) and
     (m_statusValue != KNutNet::NS)) { // When driver isn't connected, getState is connected and m_statusValue is NS // NONE STATUS
    if (m_typeOfIcon == pictureKI) {
      TQPainter painter(this);
//      painter.begin();
      if (m_statusValue & KNutNet::OL) {  // ONLINE - icon of UPS
        painter.setPen(TQt::black);
        paintNumbers (m_upsLoadValue, (width()/-11), ((float) width()/(float) 1.46666666667), &painter, ((float) width()/(float) 22) );
        }
      else {  // OFFLINE icon of battery
        if (m_batteryChargeValue != knc::numberVarError) {
          if (m_batteryChargeValue < 40) painter.setPen(TQt::red);
          else painter.setPen(TQt::black);
          paintNumbers(m_batteryChargeValue,(width()/-11),((float) width()/(float) 1.375),&painter, ((float) width()/(float) 22));
          }
        else {
          painter.setPen(TQt::black);
          paintDash (((float) width()/(float) 7.33333333333),((float) width()/(float) 1.375),&painter, ((float) width()/(float) 22));
          paintDash (((float) width()/(float) 2.75),((float) width()/(float) 1.375),&painter, ((float) width()/(float) 22));
          }
        }
 //     painter.end();
      }
    }
  }




/*********************************************************************************************/
/*                                                                                           */
/*                         PRIVATE FUNCTIONS                                                 */
/*                                                                                           */
/*********************************************************************************************/

void KNutDock::clearValues(void) {

  m_batteryChargeValue = 0;
  m_runtimeValue = 0;
  m_upsLoadValue = 0;
  m_statusValue = 0;
  }


void KNutDock::createUpsMenu(void) {

  int n = m_upsRecords->getCount();
  for (int i =0; i < n; i++) {
    if (m_activeUpsInfo->record.name == m_upsRecords->getName(i))
      m_upsServerMenu->insertItem(kapp->miniIcon(),m_upsRecords->getName(i), this, TQ_SLOT(slotUpsName(int)),0,101+i);
    else
      m_upsServerMenu->insertItem(m_upsRecords->getName(i), this, TQ_SLOT(slotUpsName(int)),0,101+i);
    }
  }

void KNutDock::createMainMenu(void) {

  m_menu = new TDEPopupMenu;
  m_upsServerMenu = new TDEPopupMenu;
  createUpsMenu();

  m_menu->insertTitle (kapp->miniIcon(),"knutclient");
  m_menu->insertSeparator();
  m_menu->insertItem("UPS",m_upsServerMenu);
  m_menu->insertSeparator();
  m_menu->insertItem(i18n("&Showing UPS variables and commands"), this, TQ_SLOT(slotPrintUPSVars()));
  m_menu->insertItem(i18n("&Running instant commands"), this, TQ_SLOT(slotInstComms()));
  m_menu->insertItem(i18n("Setting R&W variables"), this, TQ_SLOT(slotRWVars()));
  m_menu->insertSeparator();
  m_menu->insertItem(SmallIcon("reload"),i18n("Reconnect"), this, TQ_SLOT(slotRefreshConnection()));
  m_menu->insertSeparator();
  m_menu->insertItem(SmallIcon("configure"),i18n("&Preferences"), this, TQ_SLOT(slotPreferences()));
  m_menu->insertSeparator();
  m_menu->insertItem(kapp->miniIcon(),i18n("&About KNutClient"), this, TQ_SLOT(slotShowMyAbout()));
  m_menu->insertSeparator();
  m_menu->insertItem(i18n("&Minimize"), this, TQ_SLOT(slotMinimize()));
  m_menu->insertSeparator();
  m_menu->insertItem (SmallIcon("system-log-out"),i18n("&Exit"),this ,TQ_SLOT(slotExitApplication()));
  }


void KNutDock::paintCharge (int charge ) {
  TQPainter paint;

  m_chargeLayer->fill(TQt::white);
  paint.begin(m_chargeLayer);
  if (charge > 199) charge = 199;

  if (m_typeOfIcon == generalKI) {
    if (charge != knc::numberVarError) {
      if (charge < 40) paint.setPen(TQt::red);
      else paint.setPen(TQt::black);
      paintNumbers(charge,8,2,&paint, 1);
      }
    else {
      paint.setPen(TQt::black);
      paintDash (13,2,&paint, 1);
      paintDash (18,2,&paint, 1);
      }
    }
  else {
    // picture version
    if (charge != knc::numberVarError) {
      if (charge < 40) paint.setPen(TQt::red);
      else paint.setPen(TQt::black);
      paintNumbers(charge,-2,16,&paint, 1);

      if (charge < 40) paint.setPen(TQt::red);
      else {
        if (charge < 50) paint.setPen(TQt::yellow);
        else paint.setPen(TQt::green);
        }
      int delka = abs((16*charge)/100);
      delka += abs((delka-1)/4) -1;
      paint.drawLine (2,7,2+delka,7);
      paint.drawLine (2,8,2+delka,8);
      paint.drawLine (2,9,2+delka,9);
      }
    else {
      paint.setPen(TQt::black);
      paintDash (3,16,&paint, 1);
      paintDash (8,16,&paint, 1);
      }

    }
  paint.end();
  }


void KNutDock::paintErrorPoint ( TQPainter& paint ) {

  paint.setPen(TQColor(0,0,255));
  paint.setPen(TQColor(255,0,0));

  paint.drawLine(19,18,21,18);
  paint.drawLine(19,19,21,19);
  paint.drawLine(19,20,21,20);
  }


void KNutDock::paintOKPoint (TQPainter& paint  ) {

  paint.setPen(TQColor(0,255,0));
  paint.drawLine(19,18,21,18);
  paint.drawLine(19,19,21,19);
  paint.drawLine(19,20,21,20);
  }


void KNutDock::paintStatus (int status ) {
  TQPainter paint;
  int lastPos = 0;

  m_statusLayer->fill(TQt::white);
  paint.begin(m_statusLayer);

  if (status & KNutNet::OL) {
    paint.setPen(TQt::black);
    paintChar (1,2,'P',&paint);
    }
  else {
    if (status & KNutNet::OB) {
      paint.setPen(TQt::red);
      paintChar (1,2,'B',&paint);
      }
    }

  if (status & KNutNet::LB) {
    paint.setPen(TQt::red);
    paintChar (6,2,'L',&paint);
    }
  if ((lastPos <2) && (status & KNutNet::RB)) {
    paint.setPen(TQt::red);
    paintChar (6,2,'R',&paint);
    lastPos++;
    }
  if ((lastPos <2) && (status & KNutNet::BOOST)) {
    paint.setPen(TQt::red);
    paintChar (6,2,'B',&paint);
    lastPos++;
    }
  if ((lastPos <2) && (status & KNutNet::TRIM)) {
    paint.setPen(TQt::red);
    paintChar (6,2,'T',&paint);
    lastPos++;
    }
  if ((lastPos <2) && (status & KNutNet::CAL)) {
    paint.setPen(TQt::red);
    paintChar (6,2,'C',&paint);
    lastPos++;
    }
  paint.end();
  }

void KNutDock::paintRuntime (int runtime, int status ) {
  TQPainter paint;

  m_runtimeLayer->fill(TQt::white);
  paint.begin(m_runtimeLayer);

  if (status & KNutNet::OB)
    paint.setPen(TQt::red);
  else
    paint.setPen(TQt::black);
  if (runtime != knc::numberVarError) {

    int min = abs(runtime/60);
    int sec = runtime - (min*60);

    paintNumber(1,11, abs(min/10) ,&paint, 1);
    paintNumber(6,11,min-((abs(min/10))*10) ,&paint, 1);
    paintNumber(13,11, abs(sec/10) ,&paint, 1);
    paintNumber(18,11,sec-((abs(sec/10))*10) ,&paint, 1);
    }
  else {
    paintDash (1,11,&paint, 1);
    paintDash (6,11,&paint, 1);
    paintDash (13,11,&paint, 1);
    paintDash (18,11,&paint, 1);
    }
  paint.drawLine (11,12,11,13);
  paint.drawLine (11,15,11,16);
  paint.end();
  }

void KNutDock::paintLoad (int load ) {
  TQPainter paint;
  int myLoad;

//  if (m_typeOfIcon != generalKI) bitBlt (m_loadLayer,0,0,m_upsLayer,0,0,ICON_SIZE,ICON_SIZE,AndROP);
  m_loadLayer->fill(TQt::white);
  paint.begin(m_loadLayer);
  if (load == knc::numberVarError) myLoad=0;
  else myLoad = abs(load/10);

  if (m_typeOfIcon == generalKI) {
    if (load != knc::numberVarError) {
      if (myLoad > 11) myLoad=11;
      if (myLoad < 9) paint.setPen(TQt::green);
      else {
      if (myLoad < 11) paint.setPen(TQt::yellow);
      else paint.setPen(TQt::red);
      }
    paint.setPen(TQt::green.light(60));
    for (int i = 0; i < myLoad; i++) {
      paint.drawLine((i*2)+1,19,(i*2)+1,21);
      }
    paint.setPen(TQt::cyan.light(150));
      for (int i = myLoad; i < 11; i++) {
        paint.drawLine((i*2)+1,19,(i*2)+1,21);
      }
    }
  }
  paint.end();

  }




void KNutDock::paintNumbers (int numbers, int x, int y, TQPainter* p, float sizeMult ) {
  bool first = false;

  if (numbers > 199) numbers = 199;

  if (numbers != knc::numberVarError) {

    if (numbers > 99) {
      paintNumber (x,y,1,p,sizeMult);
      numbers -= 100;
      first=true;
      }
    int second = abs (numbers/10);
    int third = numbers - (second * 10);

    if ((second != 0) || (first)) paintNumber (((float) x+(5*sizeMult)),y,second,p,sizeMult);
    paintNumber (((float) x+(10*sizeMult)),y,third,p,sizeMult);

    }
  else {
    p->setPen(TQt::black);
    paintDash (((float) x+(5*sizeMult)),y,p,sizeMult);
    paintDash (((float) x+(10*sizeMult)),y,p,sizeMult);
    }
  }


void KNutDock::paintDash (int x, int y, TQPainter *p, float sizeMult) {
  TQPen oldPen = p->pen();
  TQPen newPen;
  newPen.setWidth(((float) (oldPen.width()+1)*sizeMult)-1);
  p->setPen(newPen);
  p->drawLine(x,((float) y+(3*sizeMult)),((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
  p->setPen(oldPen);
  }

void KNutDock::paintNumber (int x, int y, int number, TQPainter *p, float sizeMult) {
  // x and y are left top corner
  TQPen oldPen = p->pen();
  TQPen newPen;
  newPen.setWidth(((float) (oldPen.width()+1)*sizeMult)-1);
  p->setPen(newPen);

  if ((number > -1) && (number < 10)) {
   // painting - kreslim
    switch (number) {
      case 0:
      p->drawRect(x,y,((float) 4*sizeMult),((float) 7*sizeMult));
      break;
      case 1:
        p->drawLine (((float) x+(3*sizeMult)),y,((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
      break;
      case 2:
        p->drawLine(x,y,((float) x+(3*sizeMult)),y);
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
        p->lineTo(x,((float) y+(3*sizeMult)));
        p->lineTo(x,((float) y+(6*sizeMult)));
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
      break;
      case 3:
        p->drawLine(x,y,((float) x+(3*sizeMult)),y);
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
        p->lineTo(x,((float) y+(6*sizeMult)));
        p->drawLine(x,((float) y+(3*sizeMult)),((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
      break;
      case 4:
        p->drawLine(x,y,x,((float) y+(3*sizeMult)));
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
        p->drawLine (((float) x+(3*sizeMult)),y,((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
      break;
      case 5:
        p->drawLine(((float) x+(3*sizeMult)),y,x,y);
        p->lineTo(x,((float) y+(3*sizeMult)));
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
        p->lineTo(x,((float) y+(6*sizeMult)));
      break;
      case 6:
        p->drawRect(x,((float) y+(3*sizeMult)),((float) 4*sizeMult),((float) 4*sizeMult));
        p->drawLine(x,((float) y+(3*sizeMult)),x,y);
        p->lineTo(((float) x+(3*sizeMult)),y);
      break;
      case 7:
        p->drawLine(x,y,((float) x+(3*sizeMult)),y);
        p->lineTo(((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
      break;
      case 8:
        p->drawRect(x,y,((float) 4*sizeMult),((float) 7*sizeMult));
        p->drawLine(x,((float) y+(3*sizeMult)),((float) x+(3*sizeMult)),((float) y+(3*sizeMult)));
      break;
      case 9:
        p->drawRect(x,y,((float) 4*sizeMult),((float) 4*sizeMult));
        p->drawLine(((float) x+(3*sizeMult)),((float) y+(3*sizeMult)),((float) x+(3*sizeMult)),((float) y+(6*sizeMult)));
        p->lineTo(x,((float) y+(6*sizeMult)));
      break;
      }
    }
    p->setPen(oldPen);
  }


void KNutDock::paintChar (int x, int y, char LCDChar, TQPainter *p) {
  // x and y are left up  corner
  switch (LCDChar) {
    case 'L':
      p->drawLine(x,y,x,y+6);
      p->lineTo(x+3,y+6);
    break;
    case 'O':
      p->drawRect(x,y,4,7);
    break;
    case 'B':
      p->drawLine(x,y,x,y+6);
      p->lineTo(x+2,y+6);
      p->drawLine(x,y,x+2,y);
      p->drawLine(x,y+3,x+2,y+3);
      p->drawLine(x+3,y+1,x+3,y+2);
      p->drawLine(x+3,y+4,x+3,y+5);
    break;
    case 'U':
      p->drawLine(x,y,x,y+6);
      p->lineTo(x+3,y+6);
      p->lineTo(x+3,y);
    break;
    case 'C':
      p->drawLine(x+3,y+1,x+3,y);
      p->lineTo(x,y);
      p->lineTo(x,y+6);
      p->lineTo(x+3,y+6);
      p->lineTo(x+3,y+5);
    break;
    case 'R':
      p->drawLine(x,y,x,y+6);
      p->drawLine(x,y,x+2,y);
      p->drawLine(x,y+3,x+2,y+3);
      p->drawLine(x+3,y+1,x+3,y+2);
      p->drawLine(x+3,y+4,x+3,y+6);
    break;
    case 'P':
      p->drawLine(x,y,x,y+6);
      p->drawLine(x,y,x+2,y);
      p->drawLine(x,y+3,x+2,y+3);
      p->drawLine(x+3,y+1,x+3,y+2);
    break;
    case 'T':
      p->drawLine(x+2,y,x+2,y+6);
      p->drawLine(x,y,x+4,y);
    break;
    }
  }




#include "knutdock.moc"
