curvelist_view.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 "curvelist_view.h"
8 #include <QApplication>
9 #include <QDrag>
10 #include <QMessageBox>
11 #include <QMimeData>
12 #include <QDebug>
13 #include <QScrollBar>
14 #include "curvelist_panel.h"
15 
17  : QTableWidget(parent), CurvesView(parent)
18 {
19  setColumnCount(2);
20  setEditTriggers(NoEditTriggers);
21  setDragEnabled(false);
22  setDefaultDropAction(Qt::IgnoreAction);
23  setDragDropOverwriteMode(false);
24  setDragDropMode(NoDragDrop);
25  viewport()->installEventFilter(this);
26 
27  setSelectionBehavior(QAbstractItemView::SelectRows);
28  setSelectionMode(ExtendedSelection);
29  setFocusPolicy(Qt::ClickFocus);
30 
31  verticalHeader()->setVisible(false);
32  horizontalHeader()->setVisible(false);
33 
34  horizontalHeader()->setStretchLastSection(true);
35 
36  setColumnWidth(1, 100);
37 
38  setHorizontalHeaderItem(0, new QTableWidgetItem("Time series"));
39  setHorizontalHeaderItem(1, new QTableWidgetItem("Current value"));
40 
41  setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
42  setShowGrid(false);
43 }
44 
45 void CurveTableView::addItem(const QString& prefix, const QString& plot_name,
46  const QString& plot_ID)
47 {
48  if (_inserted_curves.contains(plot_ID))
49  {
50  return;
51  }
52 
53  QString row_name = prefix;
54  if (!prefix.isEmpty() && !prefix.endsWith('/'))
55  {
56  row_name += "/";
57  }
58  row_name += plot_name;
59 
60  auto item = new SortedTableItem<QTableWidgetItem>(plot_name);
61  QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
62  font.setPointSize(_point_size);
63  item->setFont(font);
64  item->setData(Qt::UserRole, plot_ID);
65  const int row = rowCount();
66  QTableWidget::setRowCount(row + 1);
67  QTableWidget::setItem(row, 0, item);
68 
69  auto val_cell = new QTableWidgetItem("-");
70  val_cell->setTextAlignment(Qt::AlignRight);
71  val_cell->setFlags(Qt::NoItemFlags | Qt::ItemIsEnabled);
72  font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
73  font.setPointSize(_point_size - 2);
74  val_cell->setFont(font);
75 
76  QTableWidget::setItem(row, 1, val_cell);
77 
78  _inserted_curves.insert({ plot_ID });
79 }
80 
82 {
83  sortByColumn(0, Qt::AscendingOrder);
85 }
86 
87 std::vector<std::string> CurveTableView::getSelectedNames()
88 {
89  std::vector<std::string> non_hidden_list;
90 
91  for (const QTableWidgetItem* cell : selectedItems())
92  {
93  non_hidden_list.push_back(cell->text().toStdString());
94  }
95  return non_hidden_list;
96 }
97 
99 {
100  if (rowCount() == 0)
101  {
102  return;
103  }
104  setViewResizeEnabled(false);
105 
106  for (int row = 0; row < rowCount(); row++)
107  {
108  auto cell = item(row, 0);
109  QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
110  font.setPointSize(_point_size);
111  cell->setFont(font);
112 
113  cell = item(row, 1);
114  font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
115  font.setPointSize(_point_size - 2);
116  cell->setFont(font);
117  }
118  setViewResizeEnabled(true);
119 }
120 
121 void CurveTableView::removeCurve(const QString& name)
122 {
123  for (int row = 0; row < model()->rowCount(); row++)
124  {
125  if (item(row, 0)->text() == name)
126  {
127  removeRow(row);
128  break;
129  }
130  }
131  _inserted_curves.remove(name);
132 }
133 
134 bool CurveTableView::applyVisibilityFilter(const QString& search_string)
135 {
136  setViewResizeEnabled(false);
137 
138  bool updated = false;
139  _hidden_count = 0;
140 
141  QStringList spaced_items = search_string.split(' ');
142 
143  for (int row = 0; row < model()->rowCount(); row++)
144  {
145  auto cell = item(row, 0);
146  QString name = cell->text();
147  bool toHide = false;
148 
149  if (search_string.isEmpty() == false)
150  {
151  for (const auto& item : spaced_items)
152  {
153  if (name.contains(item, Qt::CaseInsensitive) == false)
154  {
155  toHide = true;
156  break;
157  }
158  }
159  }
160  if (toHide)
161  {
162  _hidden_count++;
163  }
164 
165  if (toHide != isRowHidden(row))
166  {
167  updated = true;
168  }
169 
170  setRowHidden(row, toHide);
171  }
172 
173  setViewResizeEnabled(true);
174  return updated;
175 }
176 
178 {
179  verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
180  if (enable)
181  {
182  horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
183  horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
184  }
185  else
186  {
187  horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
188  horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
189  }
190 }
191 
193 {
194  setViewResizeEnabled(true);
195  if (hide)
196  {
197  hideColumn(1);
198  }
199  else
200  {
201  showColumn(1);
202  }
203 }
204 
205 CurvesView::CurvesView(CurveListPanel* parent) : _parent_panel(parent)
206 {
207 }
208 
209 bool CurvesView::eventFilterBase(QObject* object, QEvent* event)
210 {
211  QAbstractItemView* table_widget = qobject_cast<QAbstractItemView*>(object);
212 
213  if (qobject_cast<QScrollBar*>(object))
214  {
215  return false;
216  }
217 
218  auto obj = object;
219  while (obj && !table_widget)
220  {
221  obj = obj->parent();
222  table_widget = qobject_cast<QAbstractItemView*>(obj);
223  }
224 
225  bool ctrl_modifier_pressed =
226  (QGuiApplication::keyboardModifiers() == Qt::ControlModifier);
227 
228  if (event->type() == QEvent::MouseButtonPress)
229  {
230  QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
231 
232  _dragging = false;
233  _drag_start_pos = mouse_event->pos();
234 
235  if (mouse_event->button() == Qt::LeftButton)
236  {
237  _newX_modifier = false;
238  }
239  else if (mouse_event->button() == Qt::RightButton)
240  {
241  _newX_modifier = true;
242  }
243  else
244  {
245  return true;
246  }
247  return false;
248  }
249  else if (event->type() == QEvent::MouseMove)
250  {
251  QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
252  double distance_from_click = (mouse_event->pos() - _drag_start_pos).manhattanLength();
253 
254  if ((mouse_event->buttons() == Qt::LeftButton ||
255  mouse_event->buttons() == Qt::RightButton) &&
256  distance_from_click >= QApplication::startDragDistance() && !_dragging)
257  {
258  _dragging = true;
259  QDrag* drag = new QDrag(table_widget);
260  QMimeData* mimeData = new QMimeData;
261 
262  QByteArray mdata;
263  QDataStream stream(&mdata, QIODevice::WriteOnly);
264 
265  auto selected_names = _parent_panel->getSelectedNames();
266 
267  std::sort(selected_names.begin(), selected_names.end());
268 
269  for (const auto& curve_name : selected_names)
270  {
271  stream << QString::fromStdString(curve_name);
272  }
273 
274  if (!_newX_modifier)
275  {
276  mimeData->setData("curveslist/add_curve", mdata);
277  }
278  else
279  {
280  if (selected_names.size() != 2)
281  {
282  if (selected_names.size() >= 1)
283  {
284  QMessageBox::warning(table_widget, "New in version 2.3+",
285  "To create a new XY curve, you must select two "
286  "timeseries and "
287  "drag&drop them using the RIGHT mouse button.",
288  QMessageBox::Ok);
289  }
290  return true;
291  }
292  mimeData->setData("curveslist/new_XY_axis", mdata);
293 
294  QPixmap cursor(QSize(80, 30));
295  cursor.fill(Qt::transparent);
296 
297  QPainter painter;
298  painter.begin(&cursor);
299 
300  QString text("XY");
301  painter.setFont(QFont("Arial", 14));
302 
303  painter.setBackground(Qt::transparent);
304  painter.setPen(table_widget->palette().foreground().color());
305  painter.drawText(QRect(0, 0, 80, 30), Qt::AlignCenter, text);
306  painter.end();
307 
308  drag->setDragCursor(cursor, Qt::MoveAction);
309  }
310 
311  drag->setMimeData(mimeData);
312  drag->exec(Qt::CopyAction | Qt::MoveAction);
313  }
314  return true;
315  }
316  else if (event->type() == QEvent::Wheel)
317  {
318  QWheelEvent* wheel_event = dynamic_cast<QWheelEvent*>(event);
319 
320  if (ctrl_modifier_pressed)
321  {
322  int prev_size = _point_size;
323  if (wheel_event->delta() < 0)
324  {
325  _point_size = std::max(8, prev_size - 1);
326  }
327  else if (wheel_event->delta() > 0)
328  {
329  _point_size = std::min(14, prev_size + 1);
330  }
331  if (_point_size != prev_size)
332  {
334  }
335  return true;
336  }
337  }
338  return false;
339 }
CurveTableView::_inserted_curves
QSet< QString > _inserted_curves
Definition: curvelist_view.h:141
sol::object
basic_object< reference > object
Definition: forward.hpp:1209
CurvesView::_drag_start_pos
QPoint _drag_start_pos
Definition: curvelist_view.h:87
CurveTableView::hideValuesColumn
virtual void hideValuesColumn(bool hide) override
Definition: curvelist_view.cpp:192
CurveTableView::_hidden_count
int _hidden_count
Definition: curvelist_view.h:140
CurvesView::eventFilterBase
bool eventFilterBase(QObject *object, QEvent *event)
Definition: curvelist_view.cpp:209
CurvesView::_dragging
bool _dragging
Definition: curvelist_view.h:89
CurvesView
Definition: curvelist_view.h:51
CurveTableView::addItem
void addItem(const QString &prefix, const QString &tree_name, const QString &plot_ID) override
Definition: curvelist_view.cpp:45
CurveListPanel
Definition: curvelist_panel.h:29
CurveTableView::refreshFontSize
void refreshFontSize() override
Definition: curvelist_view.cpp:98
curvelist_panel.h
CurveListPanel::getSelectedNames
std::vector< std::string > getSelectedNames() const
Definition: curvelist_panel.cpp:507
CurveTableView::getSelectedNames
std::vector< std::string > getSelectedNames() override
Definition: curvelist_view.cpp:87
CurveTableView::applyVisibilityFilter
bool applyVisibilityFilter(const QString &filter_string) override
Definition: curvelist_view.cpp:134
sol::meta::enable
std::enable_if_t< all< Args... >::value, enable_t > enable
Definition: sol.hpp:2244
SortedTableItem
Definition: curvelist_view.h:28
CurvesView::_parent_panel
CurveListPanel * _parent_panel
Definition: curvelist_view.h:90
sort
static int sort(lua_State *L)
Definition: ltablib.c:398
CurvesView::_point_size
int _point_size
Definition: curvelist_view.h:86
CurveTableView::removeCurve
void removeCurve(const QString &name) override
Definition: curvelist_view.cpp:121
CurveTableView::setViewResizeEnabled
void setViewResizeEnabled(bool enable) override
Definition: curvelist_view.cpp:177
CurveListPanel::changeFontSize
void changeFontSize(int point_size)
Definition: curvelist_panel.cpp:263
curvelist_view.h
CurvesView::CurvesView
CurvesView(CurveListPanel *parent)
Definition: curvelist_view.cpp:205
CurveTableView::refreshColumns
void refreshColumns() override
Definition: curvelist_view.cpp:81
CurveTableView::CurveTableView
CurveTableView(CurveListPanel *parent)
Definition: curvelist_view.cpp:16
CurvesView::_newX_modifier
bool _newX_modifier
Definition: curvelist_view.h:88


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