protobuf_factory.cpp
Go to the documentation of this file.
1 #include "protobuf_factory.h"
2 
3 #include <QSettings>
4 #include <QMessageBox>
5 
7 #include "PlotJuggler/svg_util.h"
8 
9 namespace gp = google::protobuf;
10 
12 {
13  _widget = new QWidget(nullptr);
14  ui = new Ui::ProtobufLoader;
15  ui->setupUi(_widget);
16 
17  _source_tree.MapPath("", "");
18  _source_tree.MapPath("/", "/");
19 
20  loadSettings();
21 
22  QSettings settings;
23  QString theme = settings.value("Preferences::theme", "light").toString();
24  ui->pushButtonRemove->setIcon(LoadSvg(":/resources/svg/trash.svg", theme));
25 
26  connect(ui->pushButtonInclude, &QPushButton::clicked, this,
28  connect(ui->pushButtonLoad, &QPushButton::clicked, this,
30  connect(ui->pushButtonRemove, &QPushButton::clicked, this,
32 
33  QString last_type = settings.value("ProtobufParserCreator.lastType").toString();
34  int combo_index = ui->comboBox->findText(last_type, Qt::MatchExactly);
35  if (!last_type.isEmpty() && combo_index != -1)
36  {
37  ui->comboBox->setCurrentIndex(combo_index);
38  onComboChanged(last_type);
39  }
40 
41  connect(ui->comboBox, qOverload<const QString&>(&QComboBox::currentIndexChanged), this,
43 }
44 
45 void ParserFactoryProtobuf::importFile(QString filename)
46 {
47  QFile file(filename);
48  if (!file.exists())
49  {
50  QMessageBox::warning(nullptr, tr("Error loading file"),
51  tr("File %1 does not exist").arg(filename), QMessageBox::Cancel);
52  return;
53  }
54  file.open(QIODevice::ReadOnly);
55  FileInfo info;
56  QFileInfo fileinfo(filename);
57  QString file_basename = fileinfo.fileName();
58  info.file_path = filename;
59  info.proto_text = file.readAll();
60 
61  _source_tree.MapPath("", filename.toStdString());
62  _source_tree.MapPath("", file_basename.toStdString());
63  _source_tree.MapPath("", fileinfo.absolutePath().toStdString());
64 
65  FileErrorCollector error_collector;
66 
67  _importer.reset(new gp::compiler::Importer(&_source_tree, &error_collector));
68  info.file_descriptor = _importer->Import(file_basename.toStdString());
69 
70  if (!info.file_descriptor)
71  {
72  if (error_collector.errors().size() > 0)
73  {
74  QMessageBox::warning(nullptr, "Error parsing Proto file",
75  error_collector.errors().front(), QMessageBox::Cancel);
76  }
77  return;
78  }
79 
80  ui->loadedProto->setText(file_basename);
81  ui->protoPreview->setText(info.proto_text);
82  ui->comboBox->clear();
83 
84  for (int i = 0; i < info.file_descriptor->message_type_count(); i++)
85  {
86  const std::string& type_name = info.file_descriptor->message_type(i)->name();
87  auto descriptor = info.file_descriptor->FindMessageTypeByName(type_name);
88  QString type_qname = QString::fromStdString(type_name);
89  info.descriptors.insert({ type_qname, descriptor });
90  ui->comboBox->addItem(type_qname);
91  }
92  _loaded_file = std::move(info);
93 }
94 
96 {
97  ui->listWidget->clear();
98  ui->protoPreview->clear();
99 
100  QSettings settings;
101 
102  auto include_list = settings.value("ProtobufParserCreator.include_dirs").toStringList();
103  for (const auto& include_dir : include_list)
104  {
105  QDir path_dir(include_dir);
106  if (path_dir.exists())
107  {
108  ui->listWidget->addItem(include_dir);
109  _source_tree.MapPath("", include_dir.toStdString());
110  }
111  }
112  ui->listWidget->sortItems();
113 
114  auto filename = settings.value("ProtobufParserCreator.protofile").toString();
115  if (!filename.isEmpty())
116  {
117  importFile(filename);
118  }
119 }
120 
122 {
123  QSettings settings;
124  QStringList include_list;
125  for (int row = 0; row < ui->listWidget->count(); row++)
126  {
127  include_list += ui->listWidget->item(row)->text();
128  }
129  settings.setValue("ProtobufParserCreator.include_dirs", include_list);
130  settings.setValue("ProtobufParserCreator.protofile", _loaded_file.file_path);
131 }
132 
134 {
135  delete ui;
136 }
137 
139  const std::string& type_name,
140  const std::string& schema,
142 {
143  QString descriptor_name =
144  type_name.empty() ? ui->comboBox->currentText() : QString::fromStdString(type_name);
145  if (schema.empty())
146  {
147  auto descr_it = _loaded_file.descriptors.find(descriptor_name);
148  if (descr_it == _loaded_file.descriptors.end())
149  {
150  throw std::runtime_error("ParserFactoryProtobuf: can't find the descriptor");
151  }
152  auto selected_descriptor = descr_it->second;
153  return std::make_shared<ProtobufParser>(topic_name, selected_descriptor, data);
154  }
155  else
156  {
157  gp::FileDescriptorSet field_set;
158  if (!field_set.ParseFromArray(schema.data(), schema.size()))
159  {
160  throw std::runtime_error("failed to parse schema data");
161  }
162 
163  return std::make_shared<ProtobufParser>(topic_name, type_name, field_set, data);
164  }
165 }
166 
168 {
169  QSettings settings;
170  QString directory_path =
171  settings.value("ProtobufParserCreator.loadDirectory", QDir::currentPath())
172  .toString();
173 
174  QString dirname =
175  QFileDialog::getExistingDirectory(_widget, tr("Load StyleSheet"), directory_path);
176  if (dirname.isEmpty())
177  {
178  return;
179  }
180  settings.setValue("ProtobufParserCreator.loadDirectory", dirname);
181  if (ui->listWidget->findItems(dirname, Qt::MatchExactly).empty())
182  {
183  ui->listWidget->addItem(dirname);
184  ui->listWidget->sortItems();
185  }
186 }
187 
189 {
190  QSettings settings;
191 
192  QString directory_path =
193  settings.value("ProtobufParserCreator.loadDirectory", QDir::currentPath())
194  .toString();
195 
196  QString filename = QFileDialog::getOpenFileName(_widget, tr("Load StyleSheet"),
197  directory_path, tr("(*.proto)"));
198  if (filename.isEmpty())
199  {
200  return;
201  }
202 
203  importFile(filename);
204 
205  directory_path = QFileInfo(filename).absolutePath();
206  settings.setValue("ProtobufParserCreator.loadDirectory", directory_path);
207 
208  saveSettings();
209 }
210 
212 {
213  auto selected = ui->listWidget->selectedItems();
214 
215  while (!selected.isEmpty())
216  {
217  auto item = selected.front();
218  delete ui->listWidget->takeItem(ui->listWidget->row(item));
219  selected.pop_front();
220  }
221  saveSettings();
222  loadSettings();
223 }
224 
225 void ParserFactoryProtobuf::onComboChanged(const QString& text)
226 {
227  auto descr_it = _loaded_file.descriptors.find(text);
228  if (descr_it != _loaded_file.descriptors.end())
229  {
230  QSettings settings;
231  settings.setValue("ProtobufParserCreator.lastType", text);
232  }
233 }
ParserFactoryProtobuf::loadSettings
void loadSettings()
Definition: protobuf_factory.cpp:95
ParserFactoryProtobuf::FileInfo
Definition: protobuf_factory.h:45
ParserFactoryProtobuf::saveSettings
void saveSettings()
Definition: protobuf_factory.cpp:121
ParserFactoryProtobuf::_widget
QWidget * _widget
Definition: protobuf_factory.h:40
PJ::MessageParserPtr
std::shared_ptr< MessageParser > MessageParserPtr
Definition: messageparser_base.h:136
ParserFactoryProtobuf::_importer
std::unique_ptr< google::protobuf::compiler::Importer > _importer
Definition: protobuf_factory.h:43
ParserFactoryProtobuf::FileInfo::file_descriptor
const google::protobuf::FileDescriptor * file_descriptor
Definition: protobuf_factory.h:49
LoadSvg
const QPixmap & LoadSvg(QString filename, QString style_name="light")
Definition: svg_util.h:26
ParserFactoryProtobuf::importFile
void importFile(QString filename)
Definition: protobuf_factory.cpp:45
protobuf_factory.h
format.h
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
sol::type_name
std::string type_name(lua_State *L, type t)
Definition: sol.hpp:8079
ParserFactoryProtobuf::~ParserFactoryProtobuf
~ParserFactoryProtobuf() override
Definition: protobuf_factory.cpp:133
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
ParserFactoryProtobuf::FileInfo::proto_text
QByteArray proto_text
Definition: protobuf_factory.h:48
ParserFactoryProtobuf::FileInfo::file_path
QString file_path
Definition: protobuf_factory.h:47
ParserFactoryProtobuf::onRemoveInclude
void onRemoveInclude()
Definition: protobuf_factory.cpp:211
ParserFactoryProtobuf::onComboChanged
void onComboChanged(const QString &text)
Definition: protobuf_factory.cpp:225
ParserFactoryProtobuf::onIncludeDirectory
void onIncludeDirectory()
Definition: protobuf_factory.cpp:167
FileErrorCollector::errors
const QStringList & errors()
Definition: error_collectors.h:36
ParserFactoryProtobuf::_source_tree
google::protobuf::compiler::DiskSourceTree _source_tree
Definition: protobuf_factory.h:42
ParserFactoryProtobuf::FileInfo::descriptors
std::map< QString, const google::protobuf::Descriptor * > descriptors
Definition: protobuf_factory.h:50
ParserFactoryProtobuf::createParser
MessageParserPtr createParser(const std::string &topic_name, const std::string &type_name, const std::string &schema, PlotDataMapRef &data) override
Definition: protobuf_factory.cpp:138
FileErrorCollector
Definition: error_collectors.h:27
ParserFactoryProtobuf::onLoadFile
void onLoadFile()
Definition: protobuf_factory.cpp:188
ParserFactoryProtobuf::_loaded_file
FileInfo _loaded_file
Definition: protobuf_factory.h:52
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::PlotDataMapRef
Definition: plotdata.h:34
ParserFactoryProtobuf::ParserFactoryProtobuf
ParserFactoryProtobuf()
Definition: protobuf_factory.cpp:11
ParserFactoryProtobuf::ui
Ui::ProtobufLoader * ui
Definition: protobuf_factory.h:39
svg_util.h


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45