BPLightContraption
 All Classes Namespaces Files Functions Variables Macros Pages
powerunit_gui.cpp
Go to the documentation of this file.
1 /*! \file
2  * \brief A class for the GUI controlling of a power unit
3  * \author Benjamin Pritchard (ben@bennyp.org)
4  * \copyright 2013 Benjamin Pritchard. Released under the MIT License
5  */
6 
7 #include "powerunit_gui.h"
8 #include "commands-text.h"
9 
10 #include <QObject>
11 #include <QTextStream>
12 #include <QSharedPointer>
13 
14 PUInterfaceGUI::PUInterfaceGUI(quint8 id, const QString & desc, QSharedPointer<MCInterface> mc, QWidget * parent)
15  : PUInterface(id, desc, mc)
16 {
17  _label = NULL;
20  _parent = parent;
21 }
22 
24 {
25 
26 }
27 
28 void PUInterfaceGUI::AttachToGui(QLabel * label, QPushButton * onbutton, QPushButton * offbutton,
29  QSlider * levelslider)
30 {
31  _label = label;
32  _onbutton = onbutton;
33  _offbutton = offbutton;
34  _levelslider = levelslider;
35 
36  label->setText(QString(GetDescription()));
37  connect(onbutton, SIGNAL(clicked()), this, SLOT(TurnOn()));
38  connect(offbutton, SIGNAL(clicked()), this, SLOT(TurnOff()));
39  connect(levelslider, SIGNAL(valueChanged(int)), this, SLOT(LevelSliderChange(int)));
40 
41  SyncGUI();
42 }
43 
45 {
46  try {
48  }
49  catch (const MCInterfaceException & ex)
50  {
51  ExceptionBox(ex);
52  }
53 
54  SyncGUI();
55 }
56 
58 {
59  try {
61  }
62  catch (const MCInterfaceException & ex)
63  {
64  ExceptionBox(ex);
65  }
66 
67  SyncGUI();
68 }
69 
71 {
72  try {
73  SetLevel(val);
74  }
75  catch (const MCInterfaceException & ex)
76  {
77  // revert the slider to the old value
78  // which should still be in _level
79  // This would cause it to emit a changed signal
80  //_levelslider->setValue(GetLevel());
81  ExceptionBox(ex);
82  }
83 
84  SyncGUI();
85 }
86 
87 
89 {
90  QString label = QString("%1\n%2").arg(GetDescription(),ConvertPUState(GetState()));
91 
92  if(GetState() == PUSTATE_DIM)
93  label.append(QString(" (%1%)").arg(GetLevel()));
94 
95  _label->setText(label);
96 
97  _levelslider->setValue(GetLevel());
98  _levelslider->setEnabled(MCIsOpen());
99  _onbutton->setEnabled(MCIsOpen());
100  _offbutton->setEnabled(MCIsOpen());
101 }
102 
104 {
105  // Avoid emitting a 'changed' signal
106  disconnect(_levelslider, SIGNAL(valueChanged(int)), this, SLOT(LevelSliderChange(int)));
107 
109  SyncGUI();
110 
111  connect(_levelslider, SIGNAL(valueChanged(int)), this, SLOT(LevelSliderChange(int)));
112 }
113 
114 void PUInterfaceGUI::SyncState(char state, quint8 level)
115 {
116  PUInterface::SyncState(state, level);
117  SyncGUI();
118 }
119 
121 {
122  QString errstr;
123  QTextStream err(&errstr);
124  err << e.what() << "\n";
125  err << "\nuC Error: " << e.GetMCError() << " (" << ConvertMCError(e.GetMCError()) << ")";
126  err << "\nuC Error Cmd: " << e.GetMCErrorCMD() << " (" << ConvertCommandID(e.GetMCErrorCMD()) << ")";
127  err << "\nuC Error ID: " << e.GetMCErrorID() << " (" << ConvertPUID(e.GetMCErrorID()) << ")";
128  err << "\nSP Error: " << e.GetSPError() << " (" << e.GetSPErrorText() << ")";
129 
130  QMessageBox::information(_parent, "PUInterface Error", errstr);
131 }
132