moveit2
The MoveIt Motion Planning Framework for ROS 2.
navigation_widget.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Dave Coleman */
36 
38 #include <QApplication>
39 #include <QPainter>
40 #include <QScrollBar>
41 #include <QStandardItemModel>
42 #include <iostream>
43 
44 namespace moveit_setup
45 {
46 namespace assistant
47 {
48 // ******************************************************************************************
49 // CLASS
50 // ******************************************************************************************
51 
52 NavigationWidget::NavigationWidget(QWidget* parent) : QListView(parent)
53 {
54  setItemDelegate(new NavDelegate(this));
55  setEditTriggers(QAbstractItemView::NoEditTriggers);
56 
57  // setAttribute(Qt::WA_MacShowFocusRect, false);
58 
59  // Set frame graphics
60  setFrameShape(QFrame::StyledPanel);
61  setFrameShadow(QFrame::Raised);
62  setLineWidth(1);
63  setMidLineWidth(0);
64 
65  // Hard code width and height
66  setMaximumWidth(160);
67  setMinimumWidth(160);
68  setMinimumHeight(300);
69 
70  verticalScrollBar()->setPageStep(3);
71  verticalScrollBar()->setSingleStep(1);
72 
73  model_ = new QStandardItemModel(this);
74  setModel(model_);
75 }
76 
77 void NavigationWidget::setNavs(const QList<QString>& navs)
78 {
79  setModel(nullptr);
80  model_->clear();
81 
82  for (const QString& nav : navs)
83  {
84  QStandardItem* item = new QStandardItem();
85  item->setData(QVariant::fromValue(nav), Qt::DisplayRole);
86  item->setFlags(Qt::NoItemFlags);
87  model_->appendRow(item);
88  }
89 
90  setModel(model_);
91 }
92 
93 void NavigationWidget::setEnabled(const int& index, bool enabled)
94 {
95  if (enabled)
96  model_->item(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
97  Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
98  else
99  model_->item(index)->setFlags(Qt::NoItemFlags);
100 }
101 
102 void NavigationWidget::setSelected(const int& index)
103 {
104  // First make sure item is enabled
105  setEnabled(index, true);
106 
107  // Select one box from column 0, row index
108  QModelIndex top = model_->index(index, 0, QModelIndex());
109  QModelIndex bottom = model_->index(index, 0, QModelIndex());
110 
111  QItemSelection selection(top, bottom);
112  selectionModel()->reset(); // set them all to deselected
113  selectionModel()->select(selection, QItemSelectionModel::Select);
114 }
115 
116 bool NavigationWidget::isEnabled(const int& index) const
117 {
118  return model_->item(index)->flags() > Qt::NoItemFlags;
119 }
120 
121 // ******************************************************************************************
122 // CLASS
123 // ******************************************************************************************
124 
125 NavDelegate::NavDelegate(QObject* parent) : QStyledItemDelegate(parent)
126 {
127 }
128 
129 QSize NavDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
130 {
131  return QSize(option.rect.width(), 45);
132 }
133 
134 void NavDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
135 {
136  const bool is_selected = option.state & QStyle::State_Selected;
137  const QPalette& palette = QApplication::palette();
138 
139  QString nav_name = displayText(index.data(), option.locale);
140 
141  painter->save();
142 
143  // draw background gradient
144  QLinearGradient background_gradient(option.rect.topLeft(), option.rect.bottomLeft());
145  if (is_selected)
146  {
147  background_gradient.setColorAt(0, palette.color(QPalette::Highlight).lighter(125));
148  background_gradient.setColorAt(1, palette.color(QPalette::Highlight));
149  painter->fillRect(option.rect, QBrush(background_gradient));
150  }
151  else
152  {
153  background_gradient.setColorAt(0, palette.color(QPalette::Light));
154  background_gradient.setColorAt(1, palette.color(QPalette::Light).darker(105));
155  painter->fillRect(option.rect, QBrush(background_gradient));
156  }
157 
158  if (!is_selected) // draw shadow
159  {
160  painter->setPen(palette.color(QPalette::Button));
161  painter->drawLine(option.rect.topLeft(), option.rect.topRight());
162  painter->setPen(palette.color(QPalette::Light));
163  const QPoint offset(0, 1);
164  painter->drawLine(option.rect.topLeft() + offset, option.rect.topRight() + offset);
165  }
166 
167  QRect text_rect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 10, option.rect.height());
168  QFont text_font(painter->font());
169  text_font.setPixelSize(14); // Set font size
170  painter->setFont(text_font);
171 
172  // Font color
173  if (is_selected)
174  painter->setPen(palette.color(QPalette::HighlightedText));
175  else if (!option.state.testFlag(QStyle::State_Enabled))
176  painter->setPen(palette.color(QPalette::Dark));
177  else
178  painter->setPen(palette.color(QPalette::ButtonText));
179 
180  painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, nav_name);
181 
182  painter->restore();
183 }
184 } // namespace assistant
185 } // namespace moveit_setup
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setNavs(const QList< QString > &navs)
void setEnabled(const int &index, bool enabled)