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 the 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 // ******************************************************************************************
00045 // CLASS
00046 // ******************************************************************************************
00047 
00048 NavigationWidget::NavigationWidget(QWidget *parent) :
00049   QListView(parent)
00050 {
00051   setItemDelegate(new NavDelegate(this));
00052   setEditTriggers(QAbstractItemView::NoEditTriggers);
00053 
00054   //setAttribute(Qt::WA_MacShowFocusRect, false);
00055 
00056   // Set frame graphics
00057   setFrameShape(QFrame::StyledPanel);
00058   setFrameShadow(QFrame::Raised);
00059   setLineWidth(1);
00060   setMidLineWidth(0);
00061 
00062   // Hard code width and height
00063   setMaximumWidth(160);
00064   setMinimumWidth(160);
00065   setMinimumHeight(300);
00066 
00067   verticalScrollBar()->setPageStep(3);
00068   verticalScrollBar()->setSingleStep(1);
00069 
00070   model_ = new QStandardItemModel(this);
00071   setModel(model_);
00072 }
00073 
00074 void NavigationWidget::setNavs(const QList<QString> &navs)
00075 {
00076   setModel(NULL);
00077   model_->clear();
00078 
00079   for(int i = 0; i < navs.size(); i ++)
00080   {
00081     QStandardItem *item = new QStandardItem();
00082     item->setData(QVariant::fromValue(navs.at(i)), Qt::DisplayRole);
00083     item->setFlags( Qt::NoItemFlags );
00084     model_->appendRow(item);
00085   }
00086 
00087   setModel(model_);
00088 }
00089 
00090 void NavigationWidget::setEnabled( const int &index, bool enabled )
00091 {
00092   if( enabled )
00093     model_->item( index )->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled );
00094   else
00095     model_->item( index )->setFlags( Qt::NoItemFlags );
00096 }
00097 
00098 void NavigationWidget::setSelected( const int &index )
00099 {
00100   // First make sure item is enabled
00101   setEnabled( index, true );
00102 
00103   // Select one box from column 0, row index
00104   QModelIndex top = model_->index(index, 0, QModelIndex());
00105   QModelIndex bottom = model_->index(index, 0, QModelIndex());
00106 
00107   QItemSelection selection(top, bottom);
00108   selectionModel()->reset(); // set them all to deselected
00109   selectionModel()->select(selection, QItemSelectionModel::Select);
00110 }
00111 
00112 
00113 // ******************************************************************************************
00114 // CLASS
00115 // ******************************************************************************************
00116 
00117 NavDelegate::NavDelegate(QObject *parent) :
00118   QStyledItemDelegate(parent)
00119 {
00120 }
00121 
00122 QSize NavDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
00123 {
00124   return QSize(option.rect.width(), 45);
00125 }
00126 
00127 void NavDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00128 {
00129   const bool isSelected = option.state & QStyle::State_Selected;
00130 
00131   //NavScreen tp = index.data().value<NavScreen>();
00132   QString nav_name = index.data().value<QString>();
00133 
00134   painter->save();
00135 
00136   QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()), QPoint(option.rect.x(), option.rect.y()+option.rect.height()));
00137   if(isSelected)
00138   {
00139     backgroundGradient.setColorAt(0, QColor(109, 164, 219));
00140     backgroundGradient.setColorAt(1, QColor(61, 138, 212));
00141     painter->fillRect(option.rect, QBrush(backgroundGradient));
00142   }
00143   else
00144   {
00145     backgroundGradient.setColorAt(0, QColor(245, 245, 245));
00146     backgroundGradient.setColorAt(1, QColor(240, 240, 240));
00147     painter->fillRect(option.rect, QBrush(backgroundGradient));
00148   }
00149 
00150   painter->setPen(QColor(225, 225, 225));
00151   if(isSelected)
00152   {
00153     painter->setPen(QColor(37, 105, 169));
00154     painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
00155     painter->setPen(Qt::transparent);
00156   }
00157   painter->drawLine(option.rect.topLeft(), option.rect.topRight());
00158   if(!isSelected)
00159   {
00160     painter->setPen(QColor(248, 248, 248));
00161     painter->drawLine(QPoint(option.rect.x(), option.rect.y()+1), QPoint(option.rect.x()+option.rect.width(), option.rect.y()+1));
00162   }
00163 
00164   QRect textRect(option.rect.x()+10, option.rect.y(), option.rect.width()-10, option.rect.height());
00165 
00166   QFont textFont(painter->font());
00167   textFont.setPixelSize(14); // Set font size
00168   textFont.setFamily("Arial"); //Helvetica Neue");
00169   painter->setFont(textFont);
00170 
00171   // Font color
00172   if(isSelected)
00173   {
00174     // Selected
00175     painter->setPen(QColor(229, 229, 229));
00176   }
00177   else if( index.flags().testFlag( Qt::NoItemFlags ) )
00178   {
00179     // Disabled font color if disabled
00180     painter->setPen(QColor(170, 170, 170)); // TODO: make this work
00181   }
00182   else
00183   {
00184     // Normal
00185     painter->setPen(QColor(69, 69, 69));
00186   }
00187 
00188   painter->drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, nav_name );
00189 
00190   painter->restore();
00191 }
00192 
00193 
00194 }


moveit_setup_assistant
Author(s): Dave Coleman
autogenerated on Mon Oct 6 2014 02:32:27