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


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Sun Oct 18 2020 13:19:28