EditDefaultValueDialog.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Valerij Wittenbeck (valerij.wittenbeck at student dot kit dot edu
18 * @date 2015
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "EditDefaultValueDialog.h"
24 
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QtGui>
28 
30 
33 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_EditDefaultValueDialog.h>
34 
35 namespace armarx
36 {
38  QString typeString,
39  const Ice::CommunicatorPtr& communicator,
40  QWidget* parent) :
41  QDialog(parent),
42  ui(new Ui::EditDefaultValueDialog),
43  typeString(typeString),
44  communicator(communicator)
45  {
46  ui->setupUi(this);
47 
48  std::string verboseJson = json.toUtf8().data();
49  std::string compressedJson;
50  try
51  {
52  compressedJson =
53  VariantJsonCompressor::Compress(verboseJson, typeString.toUtf8().data());
54  }
55  catch (VariantJsonException& ex)
56  {
57  compressedJson = verboseJson;
58  ui->labelParseResult->setStyleSheet("QLabel { color : red; }");
59  ui->labelParseResult->setText(QString::fromUtf8(ex.what()));
60  }
61 
62  connect(ui->textEditJson, SIGNAL(textChanged()), SLOT(onJsonTextChanged()));
63  connect(ui->pushButtonFormat, SIGNAL(clicked()), SLOT(onFormat()));
64  connect(ui->pushButtonShowInternalFormat, SIGNAL(clicked()), SLOT(onShowInternalJson()));
65  connect(ui->pushButtonImport, SIGNAL(clicked(bool)), SLOT(onImportInternalJson()));
66  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
67  }
68 
70  {
71  }
72 
73  QString
75  {
76  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
77  std::string verboseJson =
78  VariantJsonCompressor::Decompress(compressedJson, typeString.toUtf8().data());
79 
80  return QString::fromUtf8(verboseJson.c_str());
81  }
82 
83  void
84  EditDefaultValueDialog::onJsonTextChanged()
85  {
86  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
88  compressedJson, typeString.toUtf8().data(), communicator);
89  bool iserr = r.iserr();
90  if (iserr)
91  {
92  ui->labelParseResult->setStyleSheet("QLabel { color : red; }");
93  ui->labelParseResult->setText(QString::fromUtf8(r.geterr().c_str()));
94  }
95  else
96  {
97  ui->labelParseResult->setStyleSheet("QLabel { color : green }");
98  ui->labelParseResult->setText("Input is valid.");
99  }
100  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!iserr);
101  ui->pushButtonShowInternalFormat->setEnabled(!iserr);
102  }
103 
104  void
105  EditDefaultValueDialog::onFormat()
106  {
107  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
108  ParseResult r = VariantJsonCompressor::FormatUserInput(compressedJson);
109  if (r.iserr())
110  {
111  QMessageBox msgBox;
112  msgBox.setText(QString::fromStdString(r.geterr()));
113  msgBox.exec();
114  }
115  else
116  {
117  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
118  }
119  }
120 
121  void
122  EditDefaultValueDialog::onShowInternalJson()
123  {
124  QMessageBox msgBox;
125  QString text;
126  try
127  {
128  text = getJson();
129  }
130  catch (VariantJsonException& ex)
131  {
132  text = QString("Error: ") + QString::fromStdString(ex.what());
133  }
134 
135  msgBox.setText(text);
136  msgBox.exec();
137  }
138 
139  void
140  EditDefaultValueDialog::onImportInternalJson()
141  {
142  QDialog editDefaultDialog;
143  editDefaultDialog.setWindowTitle("JSON Import");
144  editDefaultDialog.resize(QSize(600, 400));
145  QTextEdit* dialogTextEdit = new QTextEdit();
146  dialogTextEdit->setAcceptRichText(false);
147  dialogTextEdit->setPlainText("");
148 
149  QVBoxLayout* layout = new QVBoxLayout;
150  layout->addWidget(dialogTextEdit);
151  QDialogButtonBox* buttonBox = new QDialogButtonBox(dialogTextEdit);
152  buttonBox->setOrientation(Qt::Horizontal);
153  buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
154  layout->addWidget(buttonBox);
155  editDefaultDialog.setLayout(layout);
156 
157  connect(buttonBox, SIGNAL(accepted()), &editDefaultDialog, SLOT(accept()));
158  connect(buttonBox, SIGNAL(rejected()), &editDefaultDialog, SLOT(reject()));
159 
160  if (editDefaultDialog.exec() == QDialog::Accepted)
161  {
162  std::string json(dialogTextEdit->toPlainText().toUtf8().data());
163  std::string compressedJson;
164  try
165  {
166  compressedJson = VariantJsonCompressor::Compress(json, typeString.toUtf8().data());
167  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
168  }
169  catch (VariantJsonException& ex)
170  {
171  QMessageBox msgBox;
172  msgBox.setText(QString("Error: ") + QString::fromStdString(ex.getReason()));
173  msgBox.exec();
174  }
175  catch (LocalException& ex)
176  {
177  QMessageBox msgBox;
178 
179  msgBox.setText(QString("Error: ") + QString::fromStdString(ex.getReason()));
180  msgBox.exec();
181  }
182  }
183  }
184 } // namespace armarx
EditDefaultValueDialog.h
armarx::ParseResult
Definition: ParseResult.h:33
armarx::ParseResult::iserr
bool iserr()
Definition: ParseResult.cpp:34
armarx::VariantJsonCompressor::FormatUserInput
static ParseResult FormatUserInput(std::string &json, int indenting=2)
Definition: VariantJsonCompressor.cpp:77
IceInternal::Handle<::Ice::Communicator >
armarx::VariantJsonException
Definition: VariantJsonException.h:30
armarx::EditDefaultValueDialog
Definition: EditDefaultValueDialog.h:38
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
armarx::VariantJsonCompressor::CheckUserInput
static ParseResult CheckUserInput(const std::string &json, const std::string &variantBaseTypeName, const Ice::CommunicatorPtr &communicator)
Definition: VariantJsonCompressor.cpp:42
armarx::EditDefaultValueDialog::~EditDefaultValueDialog
~EditDefaultValueDialog() override
Definition: EditDefaultValueDialog.cpp:69
armarx::VariantJsonCompressor::Decompress
static std::string Decompress(const std::string &json, const std::string &variantBaseTypeName)
Definition: VariantJsonCompressor.cpp:255
armarx::EditDefaultValueDialog::EditDefaultValueDialog
EditDefaultValueDialog(QString json, QString typeString, const Ice::CommunicatorPtr &communicator, QWidget *parent=0)
Definition: EditDefaultValueDialog.cpp:37
armarx::EditDefaultValueDialog::getJson
QString getJson()
Definition: EditDefaultValueDialog.cpp:74
VariantJsonCompressor.h
armarx::ParseResult::geterr
std::string geterr()
Definition: ParseResult.cpp:40
Logging.h
armarx::VariantJsonCompressor::Compress
static std::string Compress(const std::string &json, const std::string &variantBaseTypeName, int indenting=2)
Definition: VariantJsonCompressor.cpp:230
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
VariantJsonException.h