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 #include <qabstractitemview.h>
28 #include <qaction.h>
29 #include <qcolor.h>
30 #include <qfont.h>
31 #include <qhashfunctions.h>
32 #include <qheaderview.h>
33 #include <qlist.h>
34 #include <qmenu.h>
35 #include <qnamespace.h>
36 #include <qobjectdefs.h>
37 #include <qpoint.h>
38 
40 
43 
44 #include "utils.h"
45 
47 {
48 
50  {
51  QStringList columns{"Source", "Target"};
52  setColumnCount(columns.size());
53  setHorizontalHeaderLabels(columns);
54  horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
55  horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
56  horizontalHeader()->setVisible(true);
57 
58  setEditTriggers(QAbstractItemView::NoEditTriggers);
59  setSortingEnabled(true);
60 
61  setAlternatingRowColors(true);
62 
63  // QString styleSheet = this->styleSheet();
64  // styleSheet = styleSheet + "\n" + "selection-background-color: #FF8000;";
65  // setStyleSheet(styleSheet);
66 
67  setContextMenuPolicy(Qt::CustomContextMenu);
68  connect(this, &This::customContextMenuRequested, this, &This::makeContextMenu);
69  }
70 
71  QTableWidgetItem*
73  const core::VertexAttribs& targetAttrib)
74  {
75  QTableWidgetItem* result = nullptr;
76 
77  setSortingEnabled(false);
78  {
79  int row = rowCount();
80  setRowCount(row + 1);
81 
82  setItem(row, 0, new QTableWidgetItem{QString::fromStdString(sourceAttrib.getName())});
83  setItem(row, 1, new QTableWidgetItem{QString::fromStdString(targetAttrib.getName())});
84 
85  result = item(row, 0);
86  }
87  setSortingEnabled(true);
88 
89  return result;
90  }
91 
92  void
93  EdgeTableWidget::updateEdge(GuiGraph::Edge edge)
94  {
95  QColor bgColor = edge.attrib().highlighted ? bgColorSelected : bgColorDefault;
96  QFont font;
97  font.setBold(edge.attrib().highlighted);
98 
99  setSortingEnabled(false);
100  {
101  int row = this->row(edge.attrib().tableWidgetItem);
102  for (int col = 0; col < 2; ++col)
103  {
104  auto* item = this->item(row, col);
105  ARMARX_CHECK_NOT_NULL(item);
106 
107  item->setData(Qt::BackgroundRole, bgColor);
108  item->setFont(font);
109  }
110  }
111  setSortingEnabled(true);
112  }
113 
114  void
115  EdgeTableWidget::removeEdge(GuiGraph::Edge& edge)
116  {
117  if (currentItem() == edge.attrib().tableWidgetItem)
118  {
119  setCurrentItem(nullptr);
120  }
121 
122  removeRow(row(edge.attrib().tableWidgetItem));
123  edge.attrib().tableWidgetItem = nullptr;
124  }
125 
126  QList<QTableWidgetItem*>
128  {
129  return utils::getSelectedItemsOfColumn(this, 0);
130  }
131 
132  void
134  {
135  QList<QTableWidgetItem*> items = selectedEdgeItems();
136 
137  QMenu menu;
138  if (items.size() == 0)
139  {
140  QAction* action = menu.addAction("No edges selected");
141  action->setEnabled(false);
142  }
143  else
144  {
145  QString desc;
146  if (items.size() == 1)
147  {
148  desc = "edge '" + items[0]->text() + "' " + utils::arrowRight + " '" +
149  item(row(items[0]), 1)->text() + "'";
150  }
151  else
152  {
153  desc = QString::number(items.size()) + " edges";
154  }
155 
156  menu.addSection("Selected " + desc);
157  connect(menu.addAction("Remove " + desc),
158  &QAction::triggered,
159  [this, &items]() { emit edgeRemovalRequested(items); });
160  }
161 
162  menu.exec(mapToGlobal(pos));
163  }
164 
165 } // namespace armarx::navigation::qt_plugins::location_graph_editor
armarx::navigation::qt_plugins::location_graph_editor::utils::arrowRight
const QString arrowRight
->
Definition: utils.cpp:36
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::addEdge
QTableWidgetItem * addEdge(const EdgeT &edge)
Definition: EdgeTableWidget.h:50
EdgeTableWidget.h
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::removeEdge
void removeEdge(GuiGraph::Edge &edge)
Definition: EdgeTableWidget.cpp:115
armarx::navigation::core::VertexAttribs::getName
std::string getName() const
Definition: Graph.cpp:47
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:93
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::makeContextMenu
void makeContextMenu(QPoint pos)
Definition: EdgeTableWidget.cpp:133
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::EdgeTableWidget
EdgeTableWidget()
Definition: EdgeTableWidget.cpp:49
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::bgColorDefault
QColor bgColorDefault
Definition: EdgeTableWidget.h:80
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::bgColorSelected
QColor bgColorSelected
Definition: EdgeTableWidget.h:81
armarx::navigation::qt_plugins::location_graph_editor
Definition: GuiGraph.cpp:34
ExpressionException.h
utils.h
Graph.h
armarx::navigation::core::VertexAttribs
Definition: Graph.h:44
GuiGraph.h
armarx::navigation::qt_plugins::location_graph_editor::EdgeTableWidget::selectedEdgeItems
QList< QTableWidgetItem * > selectedEdgeItems()
Definition: EdgeTableWidget.cpp:127
armarx::navigation::qt_plugins::location_graph_editor::utils::getSelectedItemsOfColumn
QList< QTableWidgetItem * > getSelectedItemsOfColumn(QTableWidget *widget, int column)
Definition: utils.cpp:41