summaryrefslogtreecommitdiffstats
path: root/src/knutprintupsvar.cpp
blob: bfd09e416b8741a31e4bdf02c60e9c8055d97765 (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
/***************************************************************************
                          knutprintupsvar.cpp  -  description
                             -------------------
    begin                : St cec 3 2002
    copyright            : (C) 2002 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 "knutprintupsvar.h"
#include "knutvardata.h"

#include <kmessagebox.h>
#include <kglobal.h>
#include <kiconloader.h>

#include <qgroupbox.h>
#include <qscrollview.h>
#include <qtooltip.h>
#include <qlabel.h>
#include <qframe.h>
#include <qpixmap.h>
#include <qstring.h>



//#include <iostream>


// od verze 0.6.0  nebudeme vytvaret vlastni spojeni

KNutPrintUpsVar::KNutPrintUpsVar(KNutNet* const upsNet, const bool useDescription, QWidget* parent,  const char* name, const bool modal )
: KDialogBase(Plain, i18n("Variables"),User1|Cancel,Ok, parent, name, modal, true), myUpsNet(upsNet){

  upsVarDef upsVar;
  upsICommDef upsIComm;
  dataVar pointerVar;
  dataList.clear();

  if (!upsNet) return;

  setButtonText(User1,i18n("Reload Vars"));
  setName( "knutprintupsvar" );
  setCaption( i18n("UPS variables") );

 //int error;  
 if (myUpsNet->getState() != KNutNet::Connected) {
    KNutVarData::showError (KNutNet::NotConnection);
    upsConnectOk = false;
    page=0;
    }
  else {
    upsConnectOk = true;
    countVars=myUpsNet->readNumberVars(KNutNet::AllVars);
    countRWVars=myUpsNet->readNumberVars(KNutNet::RWVars);
    countIComms=myUpsNet->readNumberComms();

    KIconLoader *loader = KGlobal::iconLoader();
    dirPixmap=loader->loadIcon ("folder_open",KIcon::Small,16);

    page = plainPage();
    QHBoxLayout* upsVarsMainLayout = new QHBoxLayout(page);

    table = new KListView(page);
    table->setRootIsDecorated(true);

    table->setAllColumnsShowFocus( TRUE );
    table->setFullWidth ( true );

    table->addColumn( i18n("Name") );
    table->addColumn( i18n("Value") );
    if (useDescription)
      table->addColumn( i18n("Description") );
    table->setSorting (-1);  

    upsVarsMainLayout->addWidget(table);


    KListViewItem* myListItem;
    if (countIComms) {

      ICommList = new KListViewItem( table, i18n("Instant Commands"));
      ICommList->setOpen(true);

      for (int i=0; i<countIComms; i++) {
        myUpsNet->readIComm(i+1,upsIComm);

        myListItem = addListItem( ICommList,upsIComm.upsCommName,"");
        if ((!upsVar.upsDescription.isEmpty()) && useDescription)
          myListItem->setText(2,upsIComm.upsDescription);
       }
     }

    if (countRWVars) {
      RWVarsList = new KListViewItem( table, i18n("Read/Write Variables"));
      RWVarsList->setOpen(true);
      for (int i=0; i<countVars; i++) {
        myUpsNet->readVars(i+1,upsVar);

        if (!upsVar.upsVarType) {
          pointerVar.myName=upsVar.upsVarName;
          pointerVar.myValue = addListItem( RWVarsList,upsVar.upsVarName,upsVar.upsValue);

       if ((!upsVar.upsDescription.isEmpty()) && useDescription)
          pointerVar.myValue->setText(2,upsVar.upsDescription);

          dataList.push_back(pointerVar);
         }
       }
     }


    ROVarsList = new KListViewItem( table, i18n("Read Only Variables"));
    ROVarsList->setOpen(true);

    for (int i=0; i<countVars; i++) {
      myUpsNet->readVars(i+1,upsVar);
      if (upsVar.upsVarType) {

        pointerVar.myName=upsVar.upsVarName;
        pointerVar.myValue = addListItem( ROVarsList,upsVar.upsVarName,upsVar.upsValue);

       if ((!upsVar.upsDescription.isEmpty()) && useDescription)
          pointerVar.myValue->setText(2,upsVar.upsDescription);

       dataList.push_back(pointerVar);
       }
     }

    connect (this,SIGNAL(user1Clicked()),this,SLOT(slotReloadVars()));
    } // end else
  }



KNutPrintUpsVar::~KNutPrintUpsVar(){

  dataList.clear();
  if (page) delete page;
  }


void KNutPrintUpsVar::slotReloadVars (void) {
  upsVarDef upsVar;

  if (myUpsNet->getUpsValues(true) !=0)
    KNutVarData::showError (myUpsNet->getError());
  else {

    QValueVector<dataVar>::const_iterator it;
    for (it = dataList.begin(); it != dataList.end(); it++) {
      myUpsNet->readVars((*it).myName, upsVar);
      (*it).myValue->setText (1, upsVar.upsValue);
      }
    }
  }

  KListViewItem* KNutPrintUpsVar::addListItem (KListViewItem* table, QString varName, QString value) {
  KListViewItem* myListItem;
  KListViewItem* helpListItem;
  QString left;
  int n;

  myListItem = table;
  do {
    n = varName.find(".");
    if (n > 0) {
      left = varName.left(n);
      varName = varName.mid(n+1);
      if (!(helpListItem = existListItem( myListItem,left))) {
        myListItem = new KListViewItem( myListItem, left);
        myListItem->setOpen(true);
        }
      else myListItem = helpListItem;
      if (!myListItem->pixmap(0)) myListItem->setPixmap(0,dirPixmap);
      }
    else { // end part
      helpListItem = new KListViewItem( myListItem, varName, value);
      helpListItem->setOpen(true);
      return helpListItem;
      }
    } while (n > -1);
  return 0; // for compiler only  
  }


    KListViewItem* KNutPrintUpsVar::existListItem (KListViewItem* listItem, QString name) {
    KListViewItem*  myItem;

    if (!(myItem = (KListViewItem *)listItem->firstChild())) return 0l;
    while (myItem) {
      if (myItem->text(0) == name) return myItem;
      myItem = (KListViewItem *)myItem->nextSibling(); 
      }
    return 0l;
    }

bool KNutPrintUpsVar::upsOk (void) { return upsConnectOk; }


#include "knutprintupsvar.moc"