navigation_widget.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2012, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman */
00036 
00037 #include "navigation_widget.h"
00038 #include <QDebug>
00039 #include <iostream>
00040 
00041 namespace moveit_setup_assistant
00042 {
00043 // ******************************************************************************************
00044 // CLASS
00045 // ******************************************************************************************
00046 
00047 NavigationWidget::NavigationWidget(QWidget* parent) : QListView(parent)
00048 {
00049   setItemDelegate(new NavDelegate(this));
00050   setEditTriggers(QAbstractItemView::NoEditTriggers);
00051 
00052   // setAttribute(Qt::WA_MacShowFocusRect, false);
00053 
00054   // Set frame graphics
00055   setFrameShape(QFrame::StyledPanel);
00056   setFrameShadow(QFrame::Raised);
00057   setLineWidth(1);
00058   setMidLineWidth(0);
00059 
00060   // Hard code width and height
00061   setMaximumWidth(160);
00062   setMinimumWidth(160);
00063   setMinimumHeight(300);
00064 
00065   verticalScrollBar()->setPageStep(3);
00066   verticalScrollBar()->setSingleStep(1);
00067 
00068   model_ = new QStandardItemModel(this);
00069   setModel(model_);
00070 }
00071 
00072 void NavigationWidget::setNavs(const QList<QString>& navs)
00073 {
00074   setModel(NULL);
00075   model_->clear();
00076 
00077   for (int i = 0; i < navs.size(); i++)
00078   {
00079     QStandardItem* item = new QStandardItem();
00080     item->setData(QVariant::fromValue(navs.at(i)), Qt::DisplayRole);
00081     item->setFlags(Qt::NoItemFlags);
00082     model_->appendRow(item);
00083   }
00084 
00085   setModel(model_);
00086 }
00087 
00088 void NavigationWidget::setEnabled(const int& index, bool enabled)
00089 {
00090   if (enabled)
00091     model_->item(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled |
00092                                   Qt::ItemIsDropEnabled | Qt::ItemIsEnabled);
00093   else
00094     model_->item(index)->setFlags(Qt::NoItemFlags);
00095 }
00096 
00097 void NavigationWidget::setSelected(const int& index)
00098 {
00099   // First make sure item is enabled
00100   setEnabled(index, true);
00101 
00102   // Select one box from column 0, row index
00103   QModelIndex top = model_->index(index, 0, QModelIndex());
00104   QModelIndex bottom = model_->index(index, 0, QModelIndex());
00105 
00106   QItemSelection selection(top, bottom);
00107   selectionModel()->reset();  // set them all to deselected
00108   selectionModel()->select(selection, QItemSelectionModel::Select);
00109 }
00110 
00111 // ******************************************************************************************
00112 // CLASS
00113 // ******************************************************************************************
00114 
00115 NavDelegate::NavDelegate(QObject* parent) : QStyledItemDelegate(parent)
00116 {
00117 }
00118 
00119 QSize NavDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
00120 {
00121   return QSize(option.rect.width(), 45);
00122 }
00123 
00124 void NavDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
00125 {
00126   const bool isSelected = option.state & QStyle::State_Selected;
00127 
00128   // NavScreen tp = index.data().value<NavScreen>();
00129   QString nav_name = index.data().value<QString>();
00130 
00131   painter->save();
00132 
00133   QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()),
00134                                      QPoint(option.rect.x(), option.rect.y() + option.rect.height()));
00135   if (isSelected)
00136   {
00137     backgroundGradient.setColorAt(0, QColor(109, 164, 219));
00138     backgroundGradient.setColorAt(1, QColor(61, 138, 212));
00139     painter->fillRect(option.rect, QBrush(backgroundGradient));
00140   }
00141   else
00142   {
00143     backgroundGradient.setColorAt(0, QColor(245, 245, 245));
00144     backgroundGradient.setColorAt(1, QColor(240, 240, 240));
00145     painter->fillRect(option.rect, QBrush(backgroundGradient));
00146   }
00147 
00148   painter->setPen(QColor(225, 225, 225));
00149   if (isSelected)
00150   {
00151     painter->setPen(QColor(37, 105, 169));
00152     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
00153     painter->setPen(Qt::transparent);
00154   }
00155   painter->drawLine(option.rect.topLeft(), option.rect.topRight());
00156   if (!isSelected)
00157   {
00158     painter->setPen(QColor(248, 248, 248));
00159     painter->drawLine(QPoint(option.rect.x(), option.rect.y() + 1),
00160                       QPoint(option.rect.x() + option.rect.width(), option.rect.y() + 1));
00161   }
00162 
00163   QRect textRect(option.rect.x() + 10, option.rect.y(), option.rect.width() - 10, option.rect.height());
00164 
00165   QFont textFont(painter->font());
00166   textFont.setPixelSize(14);    // Set font size
00167   textFont.setFamily("Arial");  // Helvetica Neue");
00168   painter->setFont(textFont);
00169 
00170   // Font color
00171   if (isSelected)
00172   {
00173     // Selected
00174     painter->setPen(QColor(229, 229, 229));
00175   }
00176   else if (index.flags().testFlag(Qt::NoItemFlags))
00177   {
00178     // Disabled font color if disabled
00179     painter->setPen(QColor(170, 170, 170));  // TODO: make this work
00180   }
00181   else
00182   {
00183     // Normal
00184     painter->setPen(QColor(69, 69, 69));
00185   }
00186 
00187   painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, nav_name);
00188 
00189   painter->restore();
00190 }
00191 }


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Mon Jul 24 2017 02:22:29