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 
37 #include "navigation_widget.h"
38 #include <QApplication>
39 #include <QPainter>
40 #include <QScrollBar>
41 #include <QStandardItemModel>
42 #include <iostream>
43 
44 namespace moveit_setup_assistant
45 {
46 // ******************************************************************************************
47 // CLASS
48 // ******************************************************************************************
49 
50 NavigationWidget::NavigationWidget(QWidget* parent) : QListView(parent)
51 {
52  setItemDelegate(new NavDelegate(this));
53  setEditTriggers(QAbstractItemView::NoEditTriggers);
54 
55  // setAttribute(Qt::WA_MacShowFocusRect, false);
56 
57  // Set frame graphics
58  setFrameShape(QFrame::StyledPanel);
59  setFrameShadow(QFrame::Raised);
60  setLineWidth(1);
61  setMidLineWidth(0);
62 
63  // Hard code width and height
64  setMaximumWidth(160);
65  setMinimumWidth(160);
66  setMinimumHeight(300);
67 
68  verticalScrollBar()->setPageStep(3);
69  verticalScrollBar()->setSingleStep(1);
70 
71  model_ = new QStandardItemModel(this);
72  setModel(model_);
73 }
74 
75 void NavigationWidget::setNavs(const QList<QString>& navs)
76 {
77  setModel(nullptr);
78  model_->clear();
79 
80  for (const QString& nav : navs)
81  {
82  QStandardItem* item = new QStandardItem();
83  item->setData(QVariant::fromValue(nav), Qt::DisplayRole);
84  item->setFlags(Qt::NoItemFlags);
85  model_->appendRow(item);
86  }
87 
88  setModel(model_);
89 }
90 
91 void NavigationWidget::setEnabled(const int index, bool enabled)
92 {
93  if (enabled)
94  model_->item(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
95  Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
96  else
97  model_->item(index)->setFlags(Qt::NoItemFlags);
98 }
99 
100 void NavigationWidget::setSelected(const int index)
101 {
102  // First make sure item is enabled
103  setEnabled(index, true);
104 
105  // Select one box from column 0, row index
106  QModelIndex top = model_->index(index, 0, QModelIndex());
107  QModelIndex bottom = model_->index(index, 0, QModelIndex());
108 
109  QItemSelection selection(top, bottom);
110  selectionModel()->reset(); // set them all to deselected
111  selectionModel()->select(selection, QItemSelectionModel::Select);
112 }
113 
114 // ******************************************************************************************
115 // CLASS
116 // ******************************************************************************************
117 
118 NavDelegate::NavDelegate(QObject* parent) : QStyledItemDelegate(parent)
119 {
120 }
121 
122 QSize NavDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
123 {
124  return QSize(option.rect.width(), 45);
125 }
126 
127 void NavDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
128 {
129  const bool is_selected = option.state & QStyle::State_Selected;
130  const QPalette& palette = QApplication::palette();
131 
132  QString nav_name = displayText(index.data(), option.locale);
133 
134  painter->save();
135 
136  // draw background gradient
137  QLinearGradient background_gradient(option.rect.topLeft(), option.rect.bottomLeft());
138  if (is_selected)
139  {
140  background_gradient.setColorAt(0, palette.color(QPalette::Highlight).lighter(125));
141  background_gradient.setColorAt(1, palette.color(QPalette::Highlight));
142  painter->fillRect(option.rect, QBrush(background_gradient));
143  }
144  else
145  {
146  background_gradient.setColorAt(0, palette.color(QPalette::Light));
147  background_gradient.setColorAt(1, palette.color(QPalette::Light).darker(105));
148  painter->fillRect(option.rect, QBrush(background_gradient));
149  }
150 
151  if (!is_selected) // draw shadow
152  {
153  painter->setPen(palette.color(QPalette::Button));
154  painter->drawLine(option.rect.topLeft(), option.rect.topRight());
155  painter->setPen(palette.color(QPalette::Light));
156  const QPoint offset(0, 1);
157  painter->drawLine(option.rect.topLeft() + offset, option.rect.topRight() + offset);
158  }
159 
160  QRect text_rect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 10, option.rect.height());
161  QFont text_font(painter->font());
162  text_font.setPixelSize(14); // Set font size
163  painter->setFont(text_font);
164 
165  // Font color
166  if (is_selected)
167  painter->setPen(palette.color(QPalette::HighlightedText));
168  else if (!option.state.testFlag(QStyle::State_Enabled))
169  painter->setPen(palette.color(QPalette::Dark));
170  else
171  painter->setPen(palette.color(QPalette::ButtonText));
172 
173  painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, nav_name);
174 
175  painter->restore();
176 }
177 } // namespace moveit_setup_assistant
navigation_widget.h
moveit_setup_assistant::NavDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: navigation_widget.cpp:159
moveit_setup_assistant::NavDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: navigation_widget.cpp:154
moveit_setup_assistant
Definition: compute_default_collisions.h:46
index
unsigned int index
moveit_setup_assistant::NavigationWidget::NavigationWidget
NavigationWidget(QWidget *parent=nullptr)
Definition: navigation_widget.cpp:82


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Sat May 3 2025 02:28:04