moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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
44namespace moveit_setup
45{
46namespace assistant
47{
48// ******************************************************************************************
49// CLASS
50// ******************************************************************************************
51
52NavigationWidget::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
77void 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
93void NavigationWidget::setEnabled(int index, bool enabled)
94{
95 if (enabled)
96 {
97 model_->item(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
98 Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
99 }
100 else
101 {
102 model_->item(index)->setFlags(Qt::NoItemFlags);
103 }
104}
105
107{
108 // First make sure item is enabled
109 setEnabled(index, true);
110
111 // Select one box from column 0, row index
112 QModelIndex top = model_->index(index, 0, QModelIndex());
113 QModelIndex bottom = model_->index(index, 0, QModelIndex());
114
115 QItemSelection selection(top, bottom);
116 selectionModel()->reset(); // set them all to deselected
117 selectionModel()->select(selection, QItemSelectionModel::Select);
118}
119
120bool NavigationWidget::isEnabled(int index) const
121{
122 return model_->item(index)->flags() > Qt::NoItemFlags;
123}
124
125// ******************************************************************************************
126// CLASS
127// ******************************************************************************************
128
129NavDelegate::NavDelegate(QObject* parent) : QStyledItemDelegate(parent)
130{
131}
132
133QSize NavDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
134{
135 return QSize(option.rect.width(), 45);
136}
137
138void NavDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
139{
140 const bool is_selected = option.state & QStyle::State_Selected;
141 const QPalette& palette = QApplication::palette();
142
143 QString nav_name = displayText(index.data(), option.locale);
144
145 painter->save();
146
147 // draw background gradient
148 QLinearGradient background_gradient(option.rect.topLeft(), option.rect.bottomLeft());
149 if (is_selected)
150 {
151 background_gradient.setColorAt(0, palette.color(QPalette::Highlight).lighter(125));
152 background_gradient.setColorAt(1, palette.color(QPalette::Highlight));
153 painter->fillRect(option.rect, QBrush(background_gradient));
154 }
155 else
156 {
157 background_gradient.setColorAt(0, palette.color(QPalette::Light));
158 background_gradient.setColorAt(1, palette.color(QPalette::Light).darker(105));
159 painter->fillRect(option.rect, QBrush(background_gradient));
160 }
161
162 if (!is_selected) // draw shadow
163 {
164 painter->setPen(palette.color(QPalette::Button));
165 painter->drawLine(option.rect.topLeft(), option.rect.topRight());
166 painter->setPen(palette.color(QPalette::Light));
167 const QPoint offset(0, 1);
168 painter->drawLine(option.rect.topLeft() + offset, option.rect.topRight() + offset);
169 }
170
171 QRect text_rect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 10, option.rect.height());
172 QFont text_font(painter->font());
173 text_font.setPixelSize(14); // Set font size
174 painter->setFont(text_font);
175
176 // Font color
177 if (is_selected)
178 {
179 painter->setPen(palette.color(QPalette::HighlightedText));
180 }
181 else if (!option.state.testFlag(QStyle::State_Enabled))
182 {
183 painter->setPen(palette.color(QPalette::Dark));
184 }
185 else
186 {
187 painter->setPen(palette.color(QPalette::ButtonText));
188 }
189
190 painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, nav_name);
191
192 painter->restore();
193}
194} // namespace assistant
195} // 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)