summaryrefslogtreecommitdiffstats
path: root/src/knutpanelmeter.cpp
blob: 981d3e97b412efb5776bc193db8eb96f76e1ca5f (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
                          knutpanelmeter.cpp  -  description
                             -------------------
    begin                : �t b?e 16 2004
    copyright            : (C) 2004 by Daniel Prynych
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "knutpanelmeter.h"

#include <kdebug.h>

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqfont.h>


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

//#include <iostream>


extern const char *nameUpsVars1[];
extern const char *nameUpsVars2[];


KNutPanelMeter::KNutPanelMeter(TQPalette aPanelPalette, KNutVarData::upsInfoDef* activeUpsInfo, KNutVarData::SettingDataDef* settingData, KNutVarData* analogRecords, TQWidget *parent, const char *name ) : TQScrollView(parent,name,TQt::WResizeNoErase|TQt::WStaticContents) {

  m_settingData = settingData;
  m_activeUpsInfo = activeUpsInfo;
  m_analogRecords = analogRecords;
  m_aPanelPalette = aPanelPalette;

  m_analogArea = new TQLabel(this->viewport());
  addChild(m_analogArea);
  setResizePolicy ( TQScrollView::AutoOneFit ) ;


////NUTNO PAK UPRAVIT ACTIVATE A DEAKTIVATE ANALOGS
 m_analogPanels = new analogPanelRecordsDef;
    for (int i=0; i< knc::NumberOfVars; i++)
      m_analogPanels->upsAnalogs[i]=0L;

  m_widthArea =0;
  m_heightArea =0;
 }

KNutPanelMeter::~KNutPanelMeter(){

  deActivateAnalogs();
  delete m_analogPanels;
  m_analogPanels=0L;
  }



void KNutPanelMeter::deActivateAnalogs (void) {

//std::cout << "KNutPanelMeter::deActivateAnalogs" << std::endl;

  if (m_analogPanels) {
    for (int i=0; i< knc::NumberOfVars; i++) {
      if (m_analogPanels->upsAnalogs[i] != 0) {
        delete m_analogPanels->upsAnalogs[i]; // uvolnime meraky
        }
      m_analogPanels->upsAnalogs[i]=0L;
      }
    }
  m_widthArea =0;
  m_heightArea =0;
  }

void KNutPanelMeter::activateAnalogs (void) {
//tqDebug ("KNutPanelMeter::activateAnalogs start");


  KNutVarData::VarStrucDef analogMemberData;
  int ii =0;
  // deletes vatiables which ups doeasn't support 
  // vyjmeme promene ktere ups nepodporuje

  //// nasledujici radka neni nutna
  for (int i=0; i< knc::NumberOfVars; i++) m_analogPanels->upsAnalogs[i]=0L;
  for (int i=0; i< knc::NumberOfVars; i++) {
    // otestujeme zda dane UPS podporuje tuto promenou
    // pokud existuje vrati 1
    switch (m_activeUpsInfo->nutVariables) {
      case 1:
        if (!m_activeUpsInfo->upsNet->existName(nameUpsVars1[m_activeUpsInfo->record.upsVarCode[i]]))
          m_activeUpsInfo->record.upsVarCode[ii++] = m_activeUpsInfo->record.upsVarCode[i];
      break;
      case 2:
        if (!m_activeUpsInfo->upsNet->existName(nameUpsVars2[m_activeUpsInfo->record.upsVarCode[i]]))
          m_activeUpsInfo->record.upsVarCode[ii++] = m_activeUpsInfo->record.upsVarCode[i];
      break;
      }
    }
  for (int i=ii; i< knc::NumberOfVars; i++) m_activeUpsInfo->record.upsVarCode[i]=0;// doplnime zbytek nulami to znamena zadna promena

  for (int i=0; i< knc::NumberOfVars; i++) {
    if (m_activeUpsInfo->record.upsVarCode[i] != 0) {
      analogMemberData=m_analogRecords->getData (m_activeUpsInfo->record.upsVarCode[i]);

      m_analogPanels->scaleEnd[i]=analogMemberData.end;
      m_analogPanels->scaleStart[i]=analogMemberData.start;
      TQColor analogFingerColor, analogScaleColor, analogFontColor;
      if (m_settingData->customOAnalogColor) {
        analogFingerColor = m_settingData->analogFingerColor;
        analogScaleColor = m_settingData->analogScaleColor;
        analogFontColor = m_settingData->analogFontColor;
        }
      else {
        analogFingerColor = DEFAULT_FINGER_COLOR;
        analogScaleColor = DEFAULT_SCALE_COLOR;
        analogFontColor = DEFAULT_FONT_COLOR;
        }

      if (m_settingData->customFont)
        // doesn't make background
        m_analogPanels->upsAnalogs[i] = new KNutAnalog( m_analogArea,0L,analogMemberData.start,analogMemberData.end,analogMemberData.typeAnalog,analogMemberData.numberItem,analogMemberData.numberInItem,analogFingerColor,analogScaleColor,analogFontColor,&(m_settingData->aPanelFont),m_settingData->typeDigitalProcessing);
      else
        m_analogPanels->upsAnalogs[i] = new KNutAnalog( m_analogArea,0L,analogMemberData.start,analogMemberData.end,analogMemberData.typeAnalog,analogMemberData.numberItem,analogMemberData.numberInItem,analogFingerColor,analogScaleColor,analogFontColor,&(m_settingData->systemFont),m_settingData->typeDigitalProcessing);
      //nastavime barvu mezikruzi


      if (m_settingData->customOAnalogColor) {
        m_analogPanels->upsAnalogs[i]->setArcColor(1,m_settingData->analogOKColor); // doesn't make background
        m_analogPanels->upsAnalogs[i]->setArcColor(2,m_settingData->analogWarnningColor); // doesn't make background
        m_analogPanels->upsAnalogs[i]->setArcColor(3,m_settingData->analogErrorColor); // doesn't make background
        }
      // po vygenerovani panelu jsou standartni barvy nastaveny automaticky
      //nastavime mezikruzi
      for (int j = 0; j < 5; j++ ) {
        if (!((analogMemberData.scaleStruct[j].start == 0) && (analogMemberData.scaleStruct[j].end ==0 ))) {
          m_analogPanels->upsAnalogs[i]->addArc (analogMemberData.scaleStruct[j].start,analogMemberData.scaleStruct[j].end,analogMemberData.scaleStruct[j].scaleColor, false); // doesn't make background
        }
      }
      m_analogPanels->upsAnalogs[i]->setText(analogMemberData.nameAnalog,analogMemberData.nameUnit);
      m_analogPanels->upsAnalogs[i]->setPalette(m_aPanelPalette);
      m_analogPanels->upsAnalogs[i]->makeBackground(false); // makes background of analog panel

      }
    }

//tqDebug ( "KNutPanelMeter::activateAnalogs 1" );
  m_analogArea->repaint(); //repaint all analog panels, background was made already
//tqDebug ("KNutPanelMeter::activateAnalogs - end");
//  analogSV->resizeContents(sizeHint().width(),sizeHint().height());
  }



void KNutPanelMeter::setSize (bool showPanels) {
    int row =0;
    int col=0;
    int maxCols =0;
    int maxRows =0;

    if (!m_activeUpsInfo->netError) {
      for (int i=0; i< knc::NumberOfVars; i++) {
        if (m_activeUpsInfo->record.upsVarCode[i] != 0) {
          if (maxCols < m_settingData->countCols) maxCols++;
          maxRows = row+1;
          // jestlize upsAnalogs =0 nejsou aktivovany
          if (m_analogPanels->upsAnalogs[i]) {
            m_analogPanels->upsAnalogs[i]->move (SpacePanels + (SpacePanels)+(col*(KNutAnalog::AnalogWidth+SpacePanels)),SpacePanels+(row*(KNutAnalog::AnalogWidth+SpacePanels)));
            if (showPanels) m_analogPanels->upsAnalogs[i]->show();
            }
          col++;
          if (col > (m_settingData->countCols-1) ) {col=0; row++;}; // prechod na dalsi radku
          }
        }
      }
    // musime nastavit velikost
    m_widthArea = (KNutAnalog::AnalogWidth + SpacePanels)*maxCols +2*SpacePanels; // sirka
    m_heightArea = (KNutAnalog::AnalogWidth + SpacePanels)*(maxRows) + SpacePanels; // minimalni velikost podle mericich panelu

    m_analogArea->setMinimumSize (m_widthArea,m_heightArea);
    adjustSize();
    resizeContents(m_widthArea,m_heightArea);
    repaint();
  }

void KNutPanelMeter::repaintScaleAnalogPanel (int panelNumber, float value) {

  if (m_analogPanels->upsAnalogs[panelNumber] != 0L){
    switch (m_activeUpsInfo->record.upsVarCode[panelNumber]) {
      case knc::VarCurrent : // output.current
        if (m_analogPanels->scaleEnd[panelNumber] < MaxCurrent) {
          if (m_analogPanels->scaleEnd[panelNumber] < value) {
            m_analogPanels->scaleEnd[panelNumber]=(static_cast<int>(floor((value+1)/5)))*5 +5;
            if (m_analogPanels->scaleEnd[panelNumber] > MaxCurrent) m_analogPanels->scaleEnd[panelNumber]=MaxCurrent;
              m_analogPanels->upsAnalogs[panelNumber]->setRange(m_analogPanels->scaleStart[panelNumber],m_analogPanels->scaleEnd[panelNumber],true);
            }
          }
        break;
      case knc::VarBattVolt : // battery.voltage
        if (m_analogPanels->scaleEnd[panelNumber] < MaxBattVolt) {
          if (m_analogPanels->scaleEnd[panelNumber] < value) {
            m_analogPanels->scaleEnd[panelNumber]=(static_cast<int>(floor((value+1)/10)))*10 +10;
            if (m_analogPanels->scaleEnd[panelNumber] > MaxBattVolt) m_analogPanels->scaleEnd[panelNumber]=MaxBattVolt;
              m_analogPanels->upsAnalogs[panelNumber]->setRange(m_analogPanels->scaleStart[panelNumber],m_analogPanels->scaleEnd[panelNumber],true);
            }
          }
        break;
      case knc::VarBattCurrent : // battery.current
        if (m_analogPanels->scaleEnd[panelNumber] < MaxBattCurrent) {
          if ((m_analogPanels->scaleEnd[panelNumber] < value) || (m_analogPanels->scaleStart[panelNumber] > value)) {
            if ((value >10) || (value <- 15)) {
              if ((value >20) || (value < -30)) {
                m_analogPanels->scaleEnd[panelNumber]=30;
                m_analogPanels->scaleStart[panelNumber]=-45;
                 }
              else {
                m_analogPanels->scaleEnd[panelNumber]=20;
                m_analogPanels->scaleStart[panelNumber]=-30;
                }
              m_analogPanels->upsAnalogs[panelNumber]->setPointer(value); // jistota pokud by nam to nekde propadlo
              m_analogPanels->upsAnalogs[panelNumber]->hide();// schovame analogovy panel   hides analog panel
              m_analogPanels->upsAnalogs[panelNumber]->setRange (m_analogPanels->scaleStart[panelNumber],m_analogPanels->scaleEnd[panelNumber]);
              m_analogPanels->upsAnalogs[panelNumber]->repaint(); // prekreslime , repaints panel
              m_analogPanels->upsAnalogs[panelNumber]->show(); //zobrazime merak  shows analog panel
              }
            }
         }
       break;
      } // end switch
    }
  }



void KNutPanelMeter::repaintAnalogPointer (int panelNumber, float value, bool run) {
  if (m_analogPanels->upsAnalogs[panelNumber] != 0L)
     m_analogPanels->upsAnalogs[panelNumber]->setPointer (value,run); // nastavi hodnotu rucicky
  }



void KNutPanelMeter::setAnalogBG(TQPalette aPanelPalette) {

  m_aPanelPalette = aPanelPalette;
  for (int i=0; i< knc::NumberOfVars; i++) {
    if ((m_activeUpsInfo->record.upsVarCode[i]) && (m_analogPanels->upsAnalogs[i]))
      m_analogPanels->upsAnalogs[i]->setPalette(m_aPanelPalette);
    }
 }


void KNutPanelMeter::changeAnalogPanelsDigProcessing (void) {

  if (m_analogPanels == 0) return;

  for (int i=0; i< knc::NumberOfVars; i++) {
    if (m_analogPanels->upsAnalogs[i] != 0L) m_analogPanels->upsAnalogs[i]->setDigitalProcesing(m_settingData->typeDigitalProcessing,true);
    }

  }


void KNutPanelMeter::repaintAnalogPanels (bool repaintFrequencyPanels, bool repaintVoltagePanels, bool repaintAllPanels) {

KNutVarData::VarStrucDef analogMemberData;

  if (m_analogPanels == 0) return;

  // pregenerovani panelu slouzi pro settingMain a setting Analog
  for (int i=0; i< knc::NumberOfVars; i++) {
    if ((m_activeUpsInfo->record.upsVarCode[i]) && (m_analogPanels->upsAnalogs[i])) {  
           //if upsAnalogs[x] is set to zero, this upsAnalog is not used
           // record.upsVarCode contain list of code of analog meter 
           // 0 is code for none analog meter, 1 for Input Voltage, 2 for Battery Charge,
      if (((m_activeUpsInfo->record.upsVarCode[i] == knc::VarAcfreq) && (repaintFrequencyPanels)) ||
           ((m_activeUpsInfo->record.upsVarCode[i] == knc::VarOutFreq) && (repaintFrequencyPanels)) ||
           (((m_activeUpsInfo->record.upsVarCode[i] == knc::VarUtility) ||
             (m_activeUpsInfo->record.upsVarCode[i] == knc::VarOutVolt)) && (repaintVoltagePanels))) {

        analogMemberData=m_analogRecords->getData (m_activeUpsInfo->record.upsVarCode[i]); // nacteme data

        m_analogPanels->upsAnalogs[i]->hide();// schovame analogovy panel
        m_analogPanels->upsAnalogs[i]->delArc(); //zrusime segmenty
        m_analogPanels->upsAnalogs[i]->setRange (analogMemberData.start,analogMemberData.end);
        for (int j = 0; j < 5; j++ ) {
          if (!((analogMemberData.scaleStruct[j].start == 0) && (analogMemberData.scaleStruct[j].end ==0 ))) {
            m_analogPanels->upsAnalogs[i]->addArc (analogMemberData.scaleStruct[j].start,analogMemberData.scaleStruct[j].end,analogMemberData.scaleStruct[j].scaleColor, false);
            } // end if (!
          }// end for (int i=0

        m_analogPanels->upsAnalogs[i]->makeBackground(false); // repaints analog meter
        m_analogPanels->upsAnalogs[i]->repaintPointer(false); // repaints analog meter
        m_analogPanels->upsAnalogs[i]->repaint(); // repaints analog meter
        m_analogPanels->upsAnalogs[i]->show(); // shows analog meter
        } // end if (((activeUpsInfo
      if (repaintAllPanels) {
        if (m_settingData->customOAnalogColor) {
          m_analogPanels->upsAnalogs[i]->setPointerColor(m_settingData->analogFingerColor);
          m_analogPanels->upsAnalogs[i]->setArcColor(1,m_settingData->analogOKColor);
          m_analogPanels->upsAnalogs[i]->setArcColor(2,m_settingData->analogWarnningColor);
          m_analogPanels->upsAnalogs[i]->setArcColor(3,m_settingData->analogErrorColor);
          m_analogPanels->upsAnalogs[i]->setScaleColor(m_settingData->analogScaleColor);
          m_analogPanels->upsAnalogs[i]->setFontColor(m_settingData->analogFontColor);
          }
        else {
          m_analogPanels->upsAnalogs[i]->setPointerColor (DEFAULT_FINGER_COLOR);
          m_analogPanels->upsAnalogs[i]->setDefaultArcColor();
          m_analogPanels->upsAnalogs[i]->setScaleColor (DEFAULT_SCALE_COLOR);
          m_analogPanels->upsAnalogs[i]->setFontColor (DEFAULT_FONT_COLOR);
          }
        m_analogPanels->upsAnalogs[i]->repaintAll();
        }
      }
    } // end for
  }

void KNutPanelMeter::zeroingPanels (void) {
  if (m_analogPanels) {
    for (int i=0; i< knc::NumberOfVars; i++) {
      if ((m_analogPanels->upsAnalogs[i] != 0L) && (m_activeUpsInfo->record.upsVarCode[i] != 0)) // jistota pokud by nam to nekde propadlo
        m_analogPanels->upsAnalogs[i]->setPointer (0); // nastavi hodnotu rucicky
      }
    }
  }



TQSize KNutPanelMeter::sizeHint (void){

  return TQSize(m_widthArea,m_heightArea);
  }


void KNutPanelMeter::slotSetPanelsFont(TQFont newFont) {

  for (int i=0; i< knc::NumberOfVars; i++) {
    if ((m_activeUpsInfo->record.upsVarCode[i]) && (m_analogPanels->upsAnalogs[i])) {
      m_analogPanels->upsAnalogs[i]->slotSetAllFont(newFont);
      // je nutno prekresli i rucicku
      }
    }
  }

#include "knutpanelmeter.moc"