EdgeTableWidget.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  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "EdgeTableWidget.h"
23 
24 #include <QAction>
25 #include <QHeaderView>
26 #include <QMenu>
27 
28 #include "utils.h"
29 
30 
32 {
33 
35  {
36  QStringList columns{"Source", "Target"};
37  setColumnCount(columns.size());
38  setHorizontalHeaderLabels(columns);
39  horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
40  horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
41  horizontalHeader()->setVisible(true);
42 
43  setEditTriggers(QAbstractItemView::NoEditTriggers);
44  setSortingEnabled(true);
45 
46  setAlternatingRowColors(true);
47 
48  QString styleSheet = this->styleSheet();
49  styleSheet = styleSheet + "\n" + "selection-background-color: #FF8000;";
50  setStyleSheet(styleSheet);
51 
52  setContextMenuPolicy(Qt::CustomContextMenu);
53  connect(this, &This::customContextMenuRequested, this, &This::makeContextMenu);
54  }
55 
56 
57  QTableWidgetItem*
59  const core::VertexAttribs& targetAttrib)
60  {
61  QTableWidgetItem* result = nullptr;
62 
63  setSortingEnabled(false);
64  {
65  int row = rowCount();
66  setRowCount(row + 1);
67 
68  setItem(row, 0, new QTableWidgetItem{QString::fromStdString(sourceAttrib.getName())});
69  setItem(row, 1, new QTableWidgetItem{QString::fromStdString(targetAttrib.getName())});
70 
71  result = item(row, 0);
72  }
73  setSortingEnabled(true);
74 
75  return result;
76  }
77 
78 
79  void
80  EdgeTableWidget::updateEdge(GuiGraph::Edge edge)
81  {
82  QColor bgColor = edge.attrib().highlighted ? bgColorSelected : bgColorDefault;
83  QFont font;
84  font.setBold(edge.attrib().highlighted);
85 
86  setSortingEnabled(false);
87  {
88  int row = this->row(edge.attrib().tableWidgetItem);
89  for (int col = 0; col < 2; ++col)
90  {
91  auto* item = this->item(row, col);
93 
94  item->setData(Qt::BackgroundRole, bgColor);
95  item->setFont(font);
96  }
97  }
98  setSortingEnabled(true);
99  }
100 
101 
102  void
103  EdgeTableWidget::removeEdge(GuiGraph::Edge& edge)
104  {
105  if (currentItem() == edge.attrib().tableWidgetItem)
106  {
107  setCurrentItem(nullptr);
108  }
109 
110  removeRow(row(edge.attrib().tableWidgetItem));
111  edge.attrib().tableWidgetItem = nullptr;
112  }
113 
114 
115  QList<QTableWidgetItem*>
117  {
118  return utils::getSelectedItemsOfColumn(this, 0);
119  }
120 
121 
122  void
124  {
125  QList<QTableWidgetItem*> items = selectedEdgeItems();
126 
127  QMenu menu;
128  if (items.size() == 0)
129  {
130  QAction* action = menu.addAction("No edges selected");
131  action->setEnabled(false);
132  }
133  else
134  {
135  QString desc;
136  if (items.size() == 1)
137  {
138  desc = "edge '" + items[0]->text() + "' " + utils::arrowRight + " '" +
139  item(row(items[0]), 1)->text() + "'";
140  }
141  else
142  {
143  desc = QString::number(items.size()) + " edges";
144  }
145 
146  menu.addSection("Selected " + desc);
147  connect(menu.addAction("Remove " + desc),
148  &QAction::triggered,
149  [this, &items]() { emit edgeRemovalRequested(items); });
150  }
151 
152  menu.exec(mapToGlobal(pos));
153  }
154 
155 } // namespace armarx::navigation::qt_plugins::location_graph_editor
armarx::navigation::qt_plugins::location_graph_editor::utils::arrowRight
const QString arrowRight
->
Definition: utils.cpp:34
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::addEdge
QTableWidgetItem * addEdge(const EdgeT &edge)
Definition: EdgeTableWidget.h:47
EdgeTableWidget.h
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::removeEdge
void removeEdge(GuiGraph::Edge &edge)
Definition: EdgeTableWidget.cpp:103
armarx::navigation::core::VertexAttribs::getName
std::string getName() const
Definition: Graph.cpp:41
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::updateEdge
void updateEdge(GuiGraph::Edge edge)
Definition: EdgeTableWidget.cpp:80
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::makeContextMenu
void makeContextMenu(QPoint pos)
Definition: EdgeTableWidget.cpp:123
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::EdgeTableWidget
EdgeTableWidget()
Definition: EdgeTableWidget.cpp:34
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::bgColorDefault
QColor bgColorDefault
Definition: EdgeTableWidget.h:77
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::bgColorSelected
QColor bgColorSelected
Definition: EdgeTableWidget.h:78
armarx::navigation::qt_plugins::location_graph_editor
Definition: GuiGraph.cpp:29
utils.h
armarx::navigation::core::VertexAttribs
Definition: Graph.h:43
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::selectedEdgeItems
QList< QTableWidgetItem * > selectedEdgeItems()
Definition: EdgeTableWidget.cpp:116
armarx::navigation::qt_plugins::location_graph_editor::utils::getSelectedItemsOfColumn
QList< QTableWidgetItem * > getSelectedItemsOfColumn(QTableWidget *widget, int column)
Definition: utils.cpp:40