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 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_EditDefaultValueDialog.h>
26 #include <QFileDialog>
27 
28 #include <QtGui>
29 #include <QMessageBox>
30 
33 
34 namespace armarx
35 {
36  EditDefaultValueDialog::EditDefaultValueDialog(QString json, QString typeString, const Ice::CommunicatorPtr& communicator, QWidget* parent) :
37  QDialog(parent),
38  ui(new Ui::EditDefaultValueDialog),
39  typeString(typeString),
40  communicator(communicator)
41  {
42  ui->setupUi(this);
43 
44  std::string verboseJson = json.toUtf8().data();
45  std::string compressedJson;
46  try
47  {
48  compressedJson = VariantJsonCompressor::Compress(verboseJson, typeString.toUtf8().data());
49  }
50  catch (VariantJsonException& ex)
51  {
52  compressedJson = verboseJson;
53  ui->labelParseResult->setStyleSheet("QLabel { color : red; }");
54  ui->labelParseResult->setText(QString::fromUtf8(ex.what()));
55  }
56 
57  connect(ui->textEditJson, SIGNAL(textChanged()), SLOT(onJsonTextChanged()));
58  connect(ui->pushButtonFormat, SIGNAL(clicked()), SLOT(onFormat()));
59  connect(ui->pushButtonShowInternalFormat, SIGNAL(clicked()), SLOT(onShowInternalJson()));
60  connect(ui->pushButtonImport, SIGNAL(clicked(bool)), SLOT(onImportInternalJson()));
61  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
62  }
63 
65  {
66 
67  }
68 
70  {
71  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
72  std::string verboseJson = VariantJsonCompressor::Decompress(compressedJson, typeString.toUtf8().data());
73 
74  return QString::fromUtf8(verboseJson.c_str());
75  }
76 
77  void EditDefaultValueDialog::onJsonTextChanged()
78  {
79  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
80  ParseResult r = VariantJsonCompressor::CheckUserInput(compressedJson, typeString.toUtf8().data(), communicator);
81  bool iserr = r.iserr();
82  if (iserr)
83  {
84  ui->labelParseResult->setStyleSheet("QLabel { color : red; }");
85  ui->labelParseResult->setText(QString::fromUtf8(r.geterr().c_str()));
86  }
87  else
88  {
89  ui->labelParseResult->setStyleSheet("QLabel { color : green }");
90  ui->labelParseResult->setText("Input is valid.");
91  }
92  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!iserr);
93  ui->pushButtonShowInternalFormat->setEnabled(!iserr);
94  }
95 
96  void EditDefaultValueDialog::onFormat()
97  {
98  std::string compressedJson = ui->textEditJson->toPlainText().toUtf8().data();
99  ParseResult r = VariantJsonCompressor::FormatUserInput(compressedJson);
100  if (r.iserr())
101  {
102  QMessageBox msgBox;
103  msgBox.setText(QString::fromStdString(r.geterr()));
104  msgBox.exec();
105  }
106  else
107  {
108  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
109  }
110  }
111 
112  void EditDefaultValueDialog::onShowInternalJson()
113  {
114  QMessageBox msgBox;
115  QString text;
116  try
117  {
118  text = getJson();
119  }
120  catch (VariantJsonException& ex)
121  {
122  text = QString("Error: ") + QString::fromStdString(ex.what());
123  }
124 
125  msgBox.setText(text);
126  msgBox.exec();
127  }
128 
129  void EditDefaultValueDialog::onImportInternalJson()
130  {
131  QDialog editDefaultDialog;
132  editDefaultDialog.setWindowTitle("JSON Import");
133  editDefaultDialog.resize(QSize(600, 400));
134  QTextEdit* dialogTextEdit = new QTextEdit();
135  dialogTextEdit->setAcceptRichText(false);
136  dialogTextEdit->setPlainText("");
137 
138  QVBoxLayout* layout = new QVBoxLayout;
139  layout->addWidget(dialogTextEdit);
140  QDialogButtonBox* buttonBox = new QDialogButtonBox(dialogTextEdit);
141  buttonBox->setOrientation(Qt::Horizontal);
142  buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
143  layout->addWidget(buttonBox);
144  editDefaultDialog.setLayout(layout);
145 
146  connect(buttonBox, SIGNAL(accepted()), &editDefaultDialog, SLOT(accept()));
147  connect(buttonBox, SIGNAL(rejected()), &editDefaultDialog, SLOT(reject()));
148 
149  if (editDefaultDialog.exec() == QDialog::Accepted)
150  {
151  std::string json(dialogTextEdit->toPlainText().toUtf8().data());
152  std::string compressedJson;
153  try
154  {
155  compressedJson = VariantJsonCompressor::Compress(json, typeString.toUtf8().data());
156  ui->textEditJson->setText(QString::fromUtf8(compressedJson.c_str()));
157  }
158  catch (VariantJsonException& ex)
159  {
160  QMessageBox msgBox;
161  msgBox.setText(QString("Error: ") + QString::fromStdString(ex.getReason()));
162  msgBox.exec();
163  }
164  catch (LocalException& ex)
165  {
166  QMessageBox msgBox;
167 
168  msgBox.setText(QString("Error: ") + QString::fromStdString(ex.getReason()));
169  msgBox.exec();
170  }
171  }
172  }
173 }
EditDefaultValueDialog.h
armarx::ParseResult
Definition: ParseResult.h:33
armarx::ParseResult::iserr
bool iserr()
Definition: ParseResult.cpp:33
armarx::VariantJsonCompressor::FormatUserInput
static ParseResult FormatUserInput(std::string &json, int indenting=2)
Definition: VariantJsonCompressor.cpp:67
IceInternal::Handle< ::Ice::Communicator >
armarx::VariantJsonException
Definition: VariantJsonException.h:32
armarx::EditDefaultValueDialog
Definition: EditDefaultValueDialog.h:41
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:40
armarx::EditDefaultValueDialog::~EditDefaultValueDialog
~EditDefaultValueDialog() override
Definition: EditDefaultValueDialog.cpp:64
armarx::VariantJsonCompressor::Decompress
static std::string Decompress(const std::string &json, const std::string &variantBaseTypeName)
Definition: VariantJsonCompressor.cpp:200
armarx::EditDefaultValueDialog::EditDefaultValueDialog
EditDefaultValueDialog(QString json, QString typeString, const Ice::CommunicatorPtr &communicator, QWidget *parent=0)
Definition: EditDefaultValueDialog.cpp:36
armarx::EditDefaultValueDialog::getJson
QString getJson()
Definition: EditDefaultValueDialog.cpp:69
VariantJsonCompressor.h
armarx::ParseResult::geterr
std::string geterr()
Definition: ParseResult.cpp:38
Logging.h
armarx::VariantJsonCompressor::Compress
static std::string Compress(const std::string &json, const std::string &variantBaseTypeName, int indenting=2)
Definition: VariantJsonCompressor.cpp:181
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VariantJsonException.h