toolbox_model.cpp
Go to the documentation of this file.
00001 #include <pcl/apps/cloud_composer/qt.h>
00002 #include <pcl/apps/cloud_composer/toolbox_model.h>
00003 #include <pcl/apps/cloud_composer/tool_interface/abstract_tool.h>
00004 #include <pcl/apps/cloud_composer/tool_interface/tool_factory.h>
00005 #include <pcl/apps/cloud_composer/project_model.h>
00006 #include <pcl/apps/cloud_composer/items/cloud_composer_item.h>
00007 
00008 pcl::cloud_composer::ToolBoxModel::ToolBoxModel (QTreeView* tool_view, QTreeView* parameter_view_, QObject* parent)
00009 : QStandardItemModel (parent)
00010 , tool_view_ (tool_view)
00011 , parameter_view_ (parameter_view_)
00012 , project_model_ (0)
00013 {
00014   
00015 }
00016 
00017 pcl::cloud_composer::ToolBoxModel::ToolBoxModel (const ToolBoxModel&)
00018   : QStandardItemModel ()
00019 {
00020 }
00021 
00022 pcl::cloud_composer::ToolBoxModel::~ToolBoxModel ()
00023 {
00024 }
00025 
00026 void
00027 pcl::cloud_composer::ToolBoxModel::addTool (ToolFactory* tool_factory)
00028 {
00029   //qDebug () << "Icon name:"<< tool_factory->getIconName ();
00030   QIcon new_tool_icon = QIcon (tool_factory->getIconName ());
00031   QStandardItem* new_tool_item = new QStandardItem (new_tool_icon, tool_factory->getPluginName ());
00032   new_tool_item->setEditable (false);
00033   
00034   new_tool_item->setData (QVariant::fromValue (tool_factory), FACTORY);
00035   PropertiesModel* new_tool_parameters= tool_factory->createToolParameterModel (this);  
00036   new_tool_item->setData (QVariant::fromValue (new_tool_parameters), PARAMETER_MODEL);
00037   
00038   tool_items.insert (new_tool_item);
00039   QStandardItem* group_item = addToolGroup (tool_factory->getToolGroupName ());
00040   group_item->appendRow (new_tool_item); 
00041   //Expand the view for this tool group
00042   QModelIndex group_index = this->indexFromItem(group_item);
00043   tool_view_->setExpanded (group_index, true);
00044   
00045 }
00046 
00047 void
00048 pcl::cloud_composer::ToolBoxModel::setSelectionModel (QItemSelectionModel* selection_model)
00049 {
00050   selection_model_ = selection_model;
00051 }
00052   
00053 QStandardItem*
00054 pcl::cloud_composer::ToolBoxModel::addToolGroup (QString tool_group_name)
00055 {
00056   QList <QStandardItem*> matches_name = findItems (tool_group_name);
00057   if (matches_name.size () == 0)
00058   {
00059     QStandardItem* new_group_item = new QStandardItem (tool_group_name);
00060     appendRow (new_group_item);
00061     new_group_item->setSelectable (false);
00062     new_group_item->setEditable (false);
00063 
00064     return new_group_item;
00065   }
00066   else if (matches_name.size () > 1)
00067   {
00068     qWarning () << "Multiple tool groups with same name in ToolBoxModel!!";
00069   }
00070   
00071   return matches_name.value (0);
00072   
00073 }
00074 
00075 void 
00076 pcl::cloud_composer::ToolBoxModel::activeProjectChanged(ProjectModel* new_model, ProjectModel* previous_model)
00077 {
00078   //Disconnect old project model signal for selection change
00079   if (project_model_)
00080   {
00081     disconnect (project_model_->getSelectionModel (), SIGNAL (selectionChanged (QItemSelection,QItemSelection)),
00082                 this, SLOT (selectedItemChanged (QItemSelection,QItemSelection)));
00083     disconnect (project_model_, SIGNAL (modelChanged()),
00084                 this, SLOT (modelChanged()));
00085     
00086   } 
00087   qDebug () << "Active project changed in ToolBox Model!";
00088   project_model_ = new_model;
00089   
00090   //Update enabled tools, make connection for doing this automatically
00091   if (project_model_)
00092   {  
00093     updateEnabledTools (project_model_->getSelectionModel ()->selection ());
00094     connect (project_model_->getSelectionModel (), SIGNAL (selectionChanged (QItemSelection,QItemSelection)),
00095                 this, SLOT (selectedItemChanged (QItemSelection,QItemSelection)));
00096     connect (project_model_, SIGNAL (modelChanged()),
00097              this, SLOT (modelChanged()));
00098   }
00099 
00100 }
00101 
00102 void
00103 pcl::cloud_composer::ToolBoxModel::selectedToolChanged (const QModelIndex & current, const QModelIndex &)
00104 {
00105   //qDebug() << "Selected Tool changed";
00106   if (!parameter_view_)
00107   {
00108     qCritical () << "Toolbox parameter view not set!!!";
00109     return;
00110   }  
00111   QVariant parameter_model = current.data (PARAMETER_MODEL);
00112   parameter_view_->setModel ( parameter_model.value <PropertiesModel*> ());
00113   parameter_view_->expandAll ();
00114 }
00115 
00116 
00117 void
00118 pcl::cloud_composer::ToolBoxModel::toolAction ()
00119 {
00120   QModelIndex current_index = selection_model_->currentIndex ();
00121   if (!current_index.isValid ())
00122   {
00123     QMessageBox::warning (qobject_cast<QWidget *>(this->parent ()), "No Tool Selected", "Cannot execute action, no tool selected!");
00124     return;
00125   }
00126   ToolFactory* tool_factory = (current_index.data (FACTORY)).value <ToolFactory*> ();
00127   PropertiesModel* parameter_model = (current_index.data (PARAMETER_MODEL)).value <PropertiesModel*> ();
00128   //
00129   AbstractTool* tool = tool_factory->createTool (parameter_model);
00130   
00131   emit enqueueToolAction (tool);
00132 }
00133 
00134 void 
00135 pcl::cloud_composer::ToolBoxModel::selectedItemChanged ( const QItemSelection & selected, const QItemSelection & deselected )
00136 {
00137   updateEnabledTools (selected);
00138 }
00139 
00140 void 
00141 pcl::cloud_composer::ToolBoxModel::enableAllTools ()
00142 {
00143   foreach (QStandardItem* tool, tool_items)
00144   {
00145     tool->setEnabled (true);
00146   }
00147 }
00148 
00149 void
00150 pcl::cloud_composer::ToolBoxModel::modelChanged ()
00151 {
00152   updateEnabledTools (project_model_->getSelectionModel ()->selection ());  
00153 }
00154 void
00155 pcl::cloud_composer::ToolBoxModel::updateEnabledTools (const QItemSelection current_selection)
00156 {
00157   //qDebug () << "UPDATING ENABLED TOOLS!";
00158   QModelIndexList current_indices = current_selection.indexes ();
00159   QMultiMap < int, QStandardItem* > type_items_map;
00160   foreach (QModelIndex current, current_indices)
00161   {
00162     if (current.isValid ())
00163     {
00164       QStandardItem* current_item = project_model_->itemFromIndex (current);
00165       type_items_map.insert (current_item->type (), current_item);
00166     }
00167   }
00168   enableAllTools ();
00169   QList <QStandardItem*> enabled_tools = tool_items.toList (); 
00170   QMap <QStandardItem*,QString> disabled_tools;
00171   QMutableListIterator<QStandardItem*> enabled_itr(enabled_tools);
00172   //Go through tools, removing from enabled list if they fail to pass tests
00173   while (enabled_itr.hasNext()) 
00174   {
00175     QStandardItem* tool_item = enabled_itr.next ();
00176     ToolFactory* tool_factory = (tool_item->data (FACTORY)).value <ToolFactory*> ();
00177     CloudComposerItem::ItemType input_type = tool_factory->getInputItemType ();
00178     QList <CloudComposerItem::ItemType> required_children_types = tool_factory->getRequiredInputChildrenTypes();
00179     //Check if enough items for tool are selected
00180     if ( tool_factory-> getNumInputItems() > current_indices.size() )
00181     {
00182         enabled_itr.remove ();
00183         disabled_tools.insert (tool_item, tr("Tool Requires %1 Items (%2 Selected)").arg(tool_factory-> getNumInputItems()).arg(current_indices.size ()));
00184     }
00185     //Check if selection includes at least one item with correct input type
00186     else if ( ! type_items_map.keys ().contains (input_type))
00187     {
00188       enabled_itr.remove ();
00189       disabled_tools.insert (tool_item, tr("Tool Requires item type %1 selected").arg (ITEM_TYPES_STRINGS.value (input_type - QStandardItem::UserType)));
00190     }
00191     //Check if any of selected items have required children
00192     else if ( required_children_types.size () > 0)
00193     {  
00194       QList <QStandardItem*> matching_selected_items = type_items_map.values (input_type);
00195       bool found_valid_items = false;
00196       QList <CloudComposerItem::ItemType> missing_children = required_children_types;
00197       foreach (QStandardItem* item, matching_selected_items)
00198       {
00199         QList <CloudComposerItem::ItemType> found_children_types;
00200         if (!item->hasChildren ())
00201           continue;
00202         
00203         //Find types of all children
00204         for (int i = 0; i < item->rowCount(); ++i)
00205           found_children_types.append ( static_cast<CloudComposerItem::ItemType>(item->child (i)->type ()));
00206         //Make temporary copy, remove type from it if is present as child
00207         QList <CloudComposerItem::ItemType> req_children_temp = required_children_types;
00208         foreach (CloudComposerItem::ItemType type, found_children_types)
00209           req_children_temp.removeAll (type);
00210         //If temporary is empty, we found all required children
00211         if (req_children_temp.isEmpty ())
00212         {
00213           found_valid_items = true;
00214           break;
00215         }
00216         //Otherwise, set missing children list
00217         if (req_children_temp.size () < missing_children.size ())
00218           missing_children = req_children_temp;
00219 
00220 
00221       }
00222       //If we didn't find all required children
00223       if (!found_valid_items)
00224       {
00225         enabled_itr.remove ();
00226         QString missing_children_string;
00227         foreach (CloudComposerItem::ItemType type, missing_children)
00228           missing_children_string.append (" "+ITEM_TYPES_STRINGS.value (type - QStandardItem::UserType));
00229         disabled_tools.insert (tool_item, tr ("Tool Requires child item of type(s) %1").arg (missing_children_string));
00230       }
00231     }
00232   }
00233   foreach (QStandardItem* tool, tool_items)
00234   {
00235     if (enabled_tools.contains (tool))
00236     {
00237       //qDebug () << tool->text() << " is enabled!";
00238       tool->setToolTip (tool->text() + " is enabled");
00239     }
00240     else
00241     {
00242      // qDebug () << tool->text() << " disabled: "<<disabled_tools.value (tool);
00243       tool->setToolTip (disabled_tools.value (tool));
00244       tool->setEnabled (false);
00245     }
00246   }
00247   
00248   
00249   
00250 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:36:35