BitmapEditor.cpp
Go to the documentation of this file.
1 #include <DatabaseUtils/GUI/BitmapEditor.h>
2 #include <NovaDAQUtilities/HexUtils.h>
3 #include <QtGui/QPushButton>
4 #include <QtGui/QDialogButtonBox>
5 #include <QtGui/QHBoxLayout>
6 #include <QtGui/QMessageBox>
7 #include <QtGui/QLabel>
8 #include <QtGui/QVBoxLayout>
9 #include <boost/lexical_cast.hpp>
10 #include <iomanip>
11 #include <stdint.h>
12 #include <iostream>
13 
14 namespace dbutils {
15 namespace gui {
16 
18 BitmapEditor(const std::string& valueString, int bitCount, QWidget *parent) :
19  QDialog(parent)
20 {
21  setWindowTitle(tr("Bitmap Editor"));
22 
23  // ********************************
24  // first, parse the input value
25  // ********************************
26  int64_t initialValue;
27  if (! novadaq::HexUtils::parse(valueString, initialValue)) {
29  try {
30  initialValue = boost::lexical_cast<int64_t>(valueString);
31  }
32  catch (...) {
33  std::string msgText = "Data Parsing Error: ";
34  msgText.append("Unable to convert value \"");
35  msgText.append(valueString);
36  msgText.append("\" to an integer.");
37  QLabel* label = new QLabel(msgText.c_str());
38 
39  QDialogButtonBox* buttonBox =
40  new QDialogButtonBox(QDialogButtonBox::Cancel);
41  buttonBox->setCenterButtons(true);
42  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
43 
44  QVBoxLayout *mainLayout = new QVBoxLayout;
45  mainLayout->addWidget(label);
46  mainLayout->addWidget(buttonBox);
47  setLayout(mainLayout);
48 
49  return;
50  }
51  }
52  else {
54  }
55 
56  // ********************************
57  // next, create the checkboxes
58  // ********************************
59  int bitsPerGroup = 8;
60  int nGroups = ((int) ((bitCount - 1) / bitsPerGroup)) + 1;
61  //int nGroups = 4;
62  //int bitsPerGroup = ((int) ((bitCount - 1) / nGroups)) + 1;
63 
64  int64_t _64BitOne = 1;
65  QHBoxLayout* fullBitBox = new QHBoxLayout;
66  int totalCount = 0;
67  for (int igrp = 0; igrp < nGroups; ++igrp) {
68  QVBoxLayout* groupBox = new QVBoxLayout;
69  for (int ibit = 0; ibit < bitsPerGroup; ++ibit) {
70  std::string idxString =
71  boost::lexical_cast<std::string>(totalCount);
72  QCheckBox* cbox = new QCheckBox(idxString.c_str());
73 
74  if ((initialValue & (_64BitOne << totalCount)) != 0) {
75  cbox->setChecked(true);
76  }
77 
78  groupBox->addWidget(cbox);
79  _checkboxList.push_back(cbox);
80 
81  ++totalCount;
82  if (totalCount >= bitCount) {break;}
83  }
84  QWidget* tmpWidget = new QWidget();
85  tmpWidget->setLayout(groupBox);
86  fullBitBox->addWidget(tmpWidget);
87  }
88  QWidget* cboxWidget = new QWidget();
89  cboxWidget->setLayout(fullBitBox);
90 
91  // ********************************
92  // create the select/deselect buttons
93  // ********************************
94  QHBoxLayout* selectButtonBox = new QHBoxLayout;
95  selectButtonBox->addStretch();
96  QPushButton* button = new QPushButton(tr("Select All"));
97  connect(button, SIGNAL(clicked()), this, SLOT(selectAll()));
98  selectButtonBox->addWidget(button);
99  button = new QPushButton(tr("Deselect All"));
100  connect(button, SIGNAL(clicked()), this, SLOT(deselectAll()));
101  selectButtonBox->addWidget(button);
102  selectButtonBox->addStretch();
103 
104  QWidget* selButtonWidget = new QWidget();
105  selButtonWidget->setLayout(selectButtonBox);
106 
107  // ********************************
108  // create the accept/reject buttons
109  // ********************************
110  QDialogButtonBox* buttonBox =
111  new QDialogButtonBox(QDialogButtonBox::Ok |
112  QDialogButtonBox::Cancel);
113  buttonBox->setCenterButtons(true);
114 
115  connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
116  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
117 
118  // ********************************
119  // put it all together
120  // ********************************
121  QVBoxLayout *mainLayout = new QVBoxLayout;
122  mainLayout->addWidget(selButtonWidget);
123  mainLayout->addWidget(cboxWidget);
124  mainLayout->addWidget(buttonBox);
125  setLayout(mainLayout);
126 }
127 
129 {
130  int64_t _64BitOne = 1;
131  int64_t workVal = 0;
132  for (int idx = 0; idx < (int) _checkboxList.size(); ++idx) {
133  if (_checkboxList[idx]->isChecked()) {
134  workVal |= (_64BitOne << idx);
135  }
136  }
137 
139  if (_checkboxList.size() > 32) {
140  return novadaq::HexUtils::format(workVal, 16);
141  }
142  else {
143  return novadaq::HexUtils::format(workVal, 8);
144  }
145  }
146  else {
147  return boost::lexical_cast<std::string>(workVal);
148  }
149 }
150 
152 {
153  for (int idx = 0; idx < (int) _checkboxList.size(); ++idx) {
154  _checkboxList[idx]->setChecked(true);
155  }
156 }
157 
159 {
160  for (int idx = 0; idx < (int) _checkboxList.size(); ++idx) {
161  _checkboxList[idx]->setChecked(false);
162  }
163 }
164 
165 } // end of namespace gui
166 } // end of namespace dbutils
def button(text, url)
Definition: HTMLTools.py:104
const char * label
BitmapEditor(const std::string &initialValue, int bitCount, QWidget *parent=0)
def connect(endpoint="tcp://127.0.0.1:5555")
Definition: g4zmq.py:15
std::vector< QCheckBox * > _checkboxList
Definition: BitmapEditor.h:37
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool parse(const std::string &hexString, int32_t &decimalValue)
Definition: HexUtils.cpp:96
std::string getResult() const