colormap_editor.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "colormap_editor.h"
8 #include "ui_colormap_editor.h"
9 #include "PlotJuggler/svg_util.h"
10 #include <QSettings>
11 #include <QInputDialog>
12 #include <QMessageBox>
13 #include "stylesheet.h"
14 #include "color_map.h"
15 
17  : QDialog(parent), ui(new Ui::colormap_editor)
18 {
19  ui->setupUi(this);
20 
21  QSettings settings;
22 
23  ui->functionText->setHighlighter(new QLuaHighlighter);
24  ui->functionText->setCompleter(new QLuaCompleter);
25 
26  auto theme = settings.value("StyleSheet::theme", "light").toString();
27  on_stylesheetChanged(theme);
28 
29  for (const auto& it : ColorMapLibrary())
30  {
31  ui->listWidget->addItem(it.first);
32  }
33 }
34 
36 {
38  delete ui;
39 }
40 
42 {
43  ui->buttonDelete->setIcon(LoadSvg(":/resources/svg/trash.svg", theme));
44  ui->functionText->setSyntaxStyle(GetLuaSyntaxStyle(theme));
45 }
46 
48 {
49  bool ok;
50 
51  auto colormap = std::make_shared<ColorMap>();
52 
53  auto res = colormap->setScrip(ui->functionText->toPlainText());
54  if (!res.valid())
55  {
56  QMessageBox::warning(this, "Error in the Lua Script", colormap->getError(res),
57  QMessageBox::Cancel);
58  return;
59  }
60 
61  QString default_name;
62  auto selected = ui->listWidget->selectedItems();
63  if (selected.size() == 1)
64  {
65  default_name = selected.front()->text();
66  }
67 
68  QString name =
69  QInputDialog::getText(this, tr("ColorMap Name"), tr("Name of the function:"),
70  QLineEdit::Normal, default_name, &ok);
71  if (!ok || name.isEmpty())
72  {
73  return;
74  }
75  bool overwrite = false;
76 
77  if (!ui->listWidget->findItems(name, Qt::MatchExactly).empty())
78  {
79  auto reply = QMessageBox::question(this, "Confirm overwrite",
80  "A ColorMap with the same name exist already. "
81  "Do you want to overwrite it?",
82  QMessageBox::Yes | QMessageBox::No);
83  if (reply == QMessageBox::No)
84  {
85  return;
86  }
87  overwrite = true;
88  }
89 
90  ColorMapLibrary()[name] = colormap;
91 
92  if (!overwrite)
93  {
94  ui->listWidget->addItem(name);
95  selectRow(ui->listWidget->count() - 1);
96  }
97 }
98 
100 {
101  for (auto item : ui->listWidget->selectedItems())
102  {
103  auto it = ColorMapLibrary().find(item->text());
104  if (it != ColorMapLibrary().end())
105  {
106  if (!it->second->script().isEmpty())
107  {
108  auto reply = QMessageBox::question(this, "Delete ColorMap", "Are you sure?",
109  QMessageBox::Yes | QMessageBox::No);
110  if (reply == QMessageBox::No)
111  {
112  return;
113  }
114  }
115  ColorMapLibrary().erase(it);
116  }
117  delete ui->listWidget->takeItem(ui->listWidget->row(item));
118  }
119 }
120 
122 {
123  ui->listWidget->clearSelection();
124  ui->listWidget->selectionModel()->setCurrentIndex(
125  ui->listWidget->model()->index(row, 0), QItemSelectionModel::SelectionFlag::Select);
126 }
127 
129 {
130  auto it = ColorMapLibrary().find(item->text());
131  if (it != ColorMapLibrary().end())
132  {
133  auto colormap = it->second;
134  ui->functionText->setText(colormap->script());
135  }
136 }
color_map.h
LoadSvg
const QPixmap & LoadSvg(QString filename, QString style_name="light")
Definition: svg_util.h:26
ColorMapEditor::selectRow
void selectRow(int row)
Definition: colormap_editor.cpp:121
GetLuaSyntaxStyle
QSyntaxStyle * GetLuaSyntaxStyle(QString theme)
Definition: stylesheet.h:89
QLuaCompleter
Class, that describes completer with glsl specific types and functions.
Definition: QLuaCompleter.hpp:10
ColorMapEditor::on_stylesheetChanged
void on_stylesheetChanged(QString theme)
Definition: colormap_editor.cpp:41
ok
ROSCPP_DECL bool ok()
ColorMapEditor::on_buttonDelete_clicked
void on_buttonDelete_clicked()
Definition: colormap_editor.cpp:99
colormap_editor.h
ColorMapEditor::ColorMapEditor
ColorMapEditor(QWidget *parent=nullptr)
Definition: colormap_editor.cpp:16
ColorMapEditor::on_buttonSave_clicked
void on_buttonSave_clicked()
Definition: colormap_editor.cpp:47
Ui
Definition: cheatsheet_dialog.h:6
ColorMapEditor::ui
Ui::colormap_editor * ui
Definition: colormap_editor.h:43
SaveColorMapToSettings
void SaveColorMapToSettings()
Definition: color_map.cpp:54
stylesheet.h
ColorMapLibrary
std::map< QString, ColorMap::Ptr > & ColorMapLibrary()
Definition: color_map.cpp:48
QLuaHighlighter
Class, that describes C++ code highlighter.
Definition: QLuaHighlighter.hpp:19
ColorMapEditor::on_listWidget_itemDoubleClicked
void on_listWidget_itemDoubleClicked(QListWidgetItem *item)
Definition: colormap_editor.cpp:128
ColorMapEditor::~ColorMapEditor
~ColorMapEditor()
Definition: colormap_editor.cpp:35
svg_util.h


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