algorithm_widget.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------
00012 //---------------------------------------------------
00013 #include "algorithm_widget.h"
00014 
00015 #include "../controller/controller.h"
00016 #include "config_widget.h"
00017 #include "../algorithms/algorithm_config.h"
00018 
00019 #include <QtGui/QGridLayout>
00020 #include <QComboBox>
00021 #include <QSpacerItem>
00022 #include <QMessageBox>
00023 #include <QPushButton>
00024 
00025 #include <QListWidget>
00026 
00027 namespace imedgine 
00028 {
00029 //--------------------------------------------------
00030 
00031 AlgorithmWidget::AlgorithmWidget(
00032     algorithm_name_container_type list_of_algorithms,
00033     QWidget* parent, Qt::WFlags f)
00034   : QWidget(parent, f), config_widget_(0)
00035   {
00036   algorithm_name_container_type::const_iterator iterator =
00037     list_of_algorithms.begin();
00038   
00039     if(list_of_algorithms.size() == 0)
00040     {
00041       QMessageBox::critical(0, "Imedgine",
00042                             tr("Could not get the algorithms"));
00043       return;
00044     }
00045     setupUi();
00046     
00047     algorithm_selector_->addItem(QString(tr("-")));
00048       
00049     std::string old_key;
00050     while(iterator != list_of_algorithms.end())
00051     {
00052       std::string key = iterator->first;
00053       if (key != old_key)
00054       {
00055         //algorithm_selector_->addItem(QString(key.c_str()));
00056       }
00057       algorithm_selector_->addItem(QString((iterator->second).c_str()));
00058       old_key = key;
00059       ++iterator;
00060     }
00061     initializeConnections();
00062   }
00063 
00064 //--------------------------------------------------
00065   
00066   AlgorithmWidget::~AlgorithmWidget()
00067   {
00068     
00069   }
00070 
00071 //--------------------------------------------------
00072   
00073   void AlgorithmWidget::setupUi()
00074   {
00075     base_layout_ = new QGridLayout(this);
00076     algorithm_selector_ = new QComboBox();
00077     base_layout_->addWidget(algorithm_selector_, 0, 0, 1, 2);
00078     
00079     ok_button_ = new QPushButton(tr("OK", "AlgorithmWidget"));
00080     cancel_button_ = new QPushButton(tr("Cancel", "AlgorithmWidget"));
00081     base_layout_->addWidget(ok_button_, 3, 0, 1, 1);
00082     base_layout_->addWidget(cancel_button_, 3, 1, 1, 1);
00083     ok_button_->setEnabled(false);
00084     cancel_button_->setEnabled(false);
00085     
00086     configuration_layout_ = new QGridLayout();
00087     base_layout_->addLayout(configuration_layout_,1,0,2,2);
00088     
00089     QFrame* selector_frame_ = new QFrame();
00090     selector_frame_->setFrameShape(QFrame::StyledPanel);
00091     selector_frame_->setFrameShadow(QFrame::Raised);
00092 
00093     configuration_layout_->addWidget(selector_frame_,0,0);
00094     
00095     selector_layout_ = new QGridLayout(selector_frame_);
00096     
00097     QFrame* configuration_frame_ = new QFrame();
00098     configuration_frame_->setFrameShape(QFrame::StyledPanel);
00099     configuration_frame_->setFrameShadow(QFrame::Raised);
00100     
00101     configuration_layout_->addWidget(configuration_frame_, 1,0);
00102     
00103     configuration_widget_layout_ = new QGridLayout(configuration_frame_);
00104     
00105   }
00106 
00107   //--------------------------------------------------
00108   
00109   void AlgorithmWidget::initializeConnections()
00110   {
00111     connect(algorithm_selector_,SIGNAL(currentIndexChanged(const QString&)), 
00112             this, SLOT(algorithmChosen(const QString&)));
00113 
00114     connect(ok_button_,SIGNAL(pressed()), 
00115             this, SLOT(okPressed()));
00116 
00117   }
00118 
00119   //--------------------------------------------------
00120   
00121   void AlgorithmWidget::okPressed()
00122   {
00123     config_widget_->applyConfiguration();
00124     Controller::getInstance().runAlgorithm(
00125       algorithm_selector_->currentText().toStdString(),
00126       config_widget_->getConfiguration());
00127   }
00128   
00129   //--------------------------------------------------
00130 
00131   void AlgorithmWidget::datasetChanged(dataset_key_type const& dataset_name)
00132   {
00133     //FIXME it would be better if this wasn't necessary and adding a new
00134     //FIXME dataset would keep the selected old ones. Problem here is that
00135     //FIXME the addDatasetsToDropdown method always checks for possible
00136     //FIXME combinations using already_set_datasets_ and therefore everywhere
00137     //FIXME only displays slices if a slice was already set.
00138     already_set_datasets_.clear(); 
00139     for(std::vector<std::pair<QLabel*,QComboBox*> >::iterator iterator =
00140         label_with_drop_down_vector_.begin(); 
00141         iterator != label_with_drop_down_vector_.end(); ++iterator)
00142     {
00143       QLabel* temp_label = (*iterator).first;
00144       QComboBox* temp_combo_box = (*iterator).second;
00145       if (temp_combo_box->isEnabled())
00146       {
00147         addDatasetsToDropdown(temp_combo_box, temp_label->text().toStdString());
00148       }
00149     }
00150   }
00151 
00152 //--------------------------------------------------
00153   
00154   void AlgorithmWidget::algorithmChosen(const QString& chosen_algorithm)
00155   {
00156     if(chosen_algorithm == "-")
00157     {
00158       removeConfigurationThings();
00159       return;
00160     }
00161     
00162     if(config_widget_ != 0)
00163     {
00164       removeConfigurationThings();
00165     }
00166     
00167     std::string algorithm = chosen_algorithm.toStdString();
00168     try
00169     {
00170       if (!Controller::getInstance().isAlgorithmLoaded(algorithm))
00171       {
00172         Controller::getInstance().loadAlgorithm(algorithm);
00173       }
00174     }
00175     catch (AlgorithmNotFoundException const&)
00176     {
00177       //TODO: notify application user or something
00178       return;
00179     }
00180     
00181     //config_widget_ = Controller::getInstance().createConfigWidget(algorithm);
00182     //configuration_widget_layout_->addWidget(config_widget_, 0, 0);
00183     //ok_button_->setEnabled(true);
00184     //cancel_button_->setEnabled(true);
00185     
00186     definition_ = Controller::getInstance().getAlgorithmDefinition(algorithm);
00187     
00188     AlgorithmDefinition::dataset_definition_collection_type input_datasets = definition_->getInputDatasets();
00189      
00190     unsigned int counter = 0; 
00191     for(AlgorithmDefinition::dataset_definition_collection_type::const_iterator iter=input_datasets.begin(); 
00192         iter!=input_datasets.end(); ++iter)
00193     {
00194       std::string name = iter->first;
00195       AlgorithmDefinition::dataset_definition_pointer_type dataset_definition = iter->second;
00196 
00197       //create datatset attributes. only attribute that can be set immediately is the in place attribute
00198       InputDatasetAttributes dataset_attributes;
00199       dataset_attributes.is_in_place = dataset_definition->isInPlace();
00200       input_dataset_attributes_.insert(std::make_pair(name, dataset_attributes));
00201       
00202       QLabel* temp_label = new QLabel(name.c_str());
00203       selector_layout_->addWidget(temp_label, counter, 0);
00204       QComboBox* temp_combo_box = new QComboBox();
00205       selector_layout_->addWidget(temp_combo_box, counter, 1);
00206       temp_combo_box->setEnabled(false);
00207       if (counter == 0) 
00208       {
00209         addDatasetsToDropdown(temp_combo_box, name);
00210       }
00211 
00212       connect(temp_combo_box, SIGNAL(currentIndexChanged(const QString&)), 
00213               this, SLOT(inputDatasetSelected(const QString&)));
00214       
00215       label_with_drop_down_vector_.push_back(std::make_pair(temp_label, temp_combo_box));
00216       ++counter;
00217     }
00218     
00219     label_with_drop_down_vector_[0].second->setEnabled(true);
00220   }
00221 
00222 
00223   void AlgorithmWidget::addDatasetsToDropdown(QComboBox* box, std::string name)
00224   {
00225     AlgorithmDefinition::accepted_datasets_type accepted_datasets =
00226       definition_->getPossibleCombinations(name, already_set_datasets_); 
00227     
00228     dataset_key_container_type dataset_keys;
00229     Controller::getInstance().getDatasetKeysOfTypes(accepted_datasets, dataset_keys);
00230     
00231     //handle 2d slices of 3d volumes
00232     for(AlgorithmDefinition::accepted_datasets_type::const_iterator accepted_ds_iter = accepted_datasets.begin();
00233         accepted_ds_iter!=accepted_datasets.end();
00234         ++accepted_ds_iter) 
00235     {
00236       dataset_key_container_type volume_dataset_keys;
00237       if (*accepted_ds_iter == ONE_BYTE_IMAGE_PIXEL_DATASET) 
00238       { 
00239         Controller::getInstance().getDatasetKeysOfType(ONE_BYTE_VOLUME_PIXEL_DATASET, volume_dataset_keys);
00240       }
00241       if (*accepted_ds_iter == TWO_BYTE_IMAGE_PIXEL_DATASET) 
00242       {  
00243         Controller::getInstance().getDatasetKeysOfType(TWO_BYTE_VOLUME_PIXEL_DATASET, volume_dataset_keys);
00244       }
00245       if (*accepted_ds_iter == FOUR_BYTE_IMAGE_PIXEL_DATASET) 
00246       {  
00247         Controller::getInstance().getDatasetKeysOfType(FOUR_BYTE_VOLUME_PIXEL_DATASET, volume_dataset_keys);
00248       }
00249       for(dataset_key_container_type::const_iterator volume_dataset_keys_iter=volume_dataset_keys.begin();
00250           volume_dataset_keys_iter != volume_dataset_keys.end();
00251           ++volume_dataset_keys_iter)
00252       {
00253         dataset_keys.push_back(*volume_dataset_keys_iter + ": Coronal");
00254         dataset_value_to_info_.insert(std::make_pair(*volume_dataset_keys_iter + ": Coronal", std::make_pair(*volume_dataset_keys_iter,CORONAL_SLICE_PART)));
00255         dataset_keys.push_back(*volume_dataset_keys_iter + ": Axial");
00256         dataset_value_to_info_.insert(std::make_pair(*volume_dataset_keys_iter + ": Axial", std::make_pair(*volume_dataset_keys_iter,AXIAL_SLICE_PART)));
00257         dataset_keys.push_back(*volume_dataset_keys_iter + ": Sagittal");
00258         dataset_value_to_info_.insert(std::make_pair(*volume_dataset_keys_iter + ": Sagittal", std::make_pair(*volume_dataset_keys_iter,SAGITTAL_SLICE_PART)));
00259       }
00260     }
00261     
00262     box->clear();
00263     box->addItem("-");
00264     for (dataset_key_container_type::const_iterator ds_iterator=dataset_keys.begin();
00265          ds_iterator != dataset_keys.end(); ++ds_iterator)
00266     {
00267       box->addItem((*ds_iterator).c_str());
00268     }
00269   }
00270 
00271 //--------------------------------------------------
00272   
00273   void AlgorithmWidget::removeConfigurationThings()
00274   {
00275     already_set_datasets_.clear();
00276     for(std::vector<std::pair<QLabel*,QComboBox*> >::iterator iterator =
00277         label_with_drop_down_vector_.begin(); 
00278         iterator != label_with_drop_down_vector_.end(); ++iterator)
00279     {
00280       QLabel* temp_label = (*iterator).first;
00281       QComboBox* temp_combo_box = (*iterator).second;
00282       
00283       selector_layout_->removeWidget(temp_label);
00284       selector_layout_->removeWidget(temp_combo_box);
00285       
00286       delete temp_label;
00287       delete temp_combo_box;
00288     }
00289     
00290     label_with_drop_down_vector_.clear();
00291     
00292     configuration_widget_layout_->removeWidget(config_widget_);
00293     delete config_widget_;
00294     config_widget_ = 0;
00295   }
00296 
00297 //--------------------------------------------------
00298 
00299   void AlgorithmWidget::setSliceTypeForVolumeType(dataset_type& type)
00300   {
00301     if (type == ONE_BYTE_VOLUME_PIXEL_DATASET)
00302     {
00303       type = ONE_BYTE_IMAGE_PIXEL_DATASET;
00304     }
00305     else if (type == TWO_BYTE_VOLUME_PIXEL_DATASET)
00306     {
00307       type = TWO_BYTE_IMAGE_PIXEL_DATASET;
00308     }
00309     else if (type == FOUR_BYTE_VOLUME_PIXEL_DATASET)
00310     {
00311       type = FOUR_BYTE_IMAGE_PIXEL_DATASET;
00312     }
00313   }
00314 
00315 //--------------------------------------------------
00316   
00317   void AlgorithmWidget::inputDatasetSelected(const QString& selected_dataset)
00318   {
00319     unsigned int input_dataset_position = 0;
00320     bool dataset_found = false;
00321    
00322     std::string selected_ds = selected_dataset.toStdString();
00323     if (selected_ds != "-" && selected_ds != "")
00324     {
00325       dropdown_value_to_dataset_info_map_type::const_iterator ds_value_to_info_iter = dataset_value_to_info_.find(selected_ds);
00326       
00327       dataset_part_type dataset_part = WHOLE_DATASET_PART;
00328       std::string ds_name = selected_ds;
00329       if (ds_value_to_info_iter != dataset_value_to_info_.end())
00330       {
00331         ds_name = ds_value_to_info_iter->second.first;
00332         dataset_part = ds_value_to_info_iter->second.second; 
00333       }
00334       
00335       std::string name;
00336       for (unsigned int counter = 0; counter < label_with_drop_down_vector_.size(); 
00337             ++counter)
00338       {
00339         if(sender() == label_with_drop_down_vector_[counter].second)
00340         {
00341           QLabel *label = label_with_drop_down_vector_[counter].first;
00342           name = label->text().toStdString();
00343           dataset_pointer_type dataset = Controller::getInstance().getDataset(ds_name);
00344           dataset_type ds_type = dataset->getDatasetType();
00345           if (dataset_part != WHOLE_DATASET_PART)
00346           {
00347             setSliceTypeForVolumeType(ds_type);
00348           }
00349           
00350           already_set_datasets_.erase(name);
00351           already_set_datasets_.insert(std::make_pair(name, ds_type));
00352           
00353           InputDatasetAttributes attributes = input_dataset_attributes_[label->text().toStdString()];
00354           attributes.dataset_part = dataset_part;
00355           attributes.dataset_key = ds_name;
00356           input_dataset_attributes_[label->text().toStdString()] = attributes;
00357           
00358           input_dataset_position = counter + 1;
00359           dataset_found = true;
00360           break;
00361         }
00362       }
00363 
00364       if (dataset_found && input_dataset_position != label_with_drop_down_vector_.size())
00365       {
00366         label_with_drop_down_vector_[input_dataset_position].second->setEnabled(true);
00367         addDatasetsToDropdown(label_with_drop_down_vector_[input_dataset_position].second,
00368            label_with_drop_down_vector_[input_dataset_position].first->text().toStdString());
00369       }
00370 
00371       //last one selected
00372       if (input_dataset_position == label_with_drop_down_vector_.size())
00373       {
00374         if (config_widget_ != 0)
00375         {
00376           configuration_widget_layout_->removeWidget(config_widget_);
00377           delete config_widget_;
00378           config_widget_ = 0;
00379         }
00380         config_widget_ = Controller::getInstance().createConfigWidget(algorithm_selector_->currentText().toStdString());
00381         configuration_widget_layout_->addWidget(config_widget_, 0, 0);
00382         ok_button_->setEnabled(true);
00383         cancel_button_->setEnabled(true);
00384 
00385         config_dataset_id_to_input_map_type input_ds_map;
00386         for (std::vector<std::pair<QLabel*,QComboBox*> >::const_iterator ds_iterator =
00387              label_with_drop_down_vector_.begin();
00388              ds_iterator != label_with_drop_down_vector_.end();++ds_iterator)
00389         {
00390           QLabel *label = ds_iterator->first;
00391           dataset_name_to_attributes_map_type::const_iterator name_to_attributes_iter =
00392               input_dataset_attributes_.find(label->text().toStdString());
00393           if (name_to_attributes_iter != input_dataset_attributes_.end())
00394           {
00395             InputDatasetAttributes attributes = name_to_attributes_iter->second;
00396             input_ds_map.insert(std::make_pair(label->text().toStdString(), attributes));
00397           }
00398           else
00399           {
00400             //TODO throw exception
00401           } 
00402         }
00403         
00404         if (config_widget_ != 0)
00405         {
00406           // set the metadata for the whole input datasets within the configuration
00407           config_dataset_id_to_input_map_type::const_iterator input_ds_iter = 
00408               input_ds_map.begin();
00409           for ( ; input_ds_iter != input_ds_map.end(); ++input_ds_iter)
00410           {
00411             if (input_ds_iter->second.dataset_part == WHOLE_DATASET_PART)
00412             {
00413               try
00414               {
00415                 config_widget_->setMetadataForInputDataset(input_ds_iter->first, 
00416                   Controller::getInstance().getMetadataFor(
00417                     input_ds_iter->second.dataset_key));
00418               }
00419               catch (DatasetNotFoundException const&)
00420               {
00421                 continue;
00422               }
00423             }
00424           }
00425         }
00426         
00427         std::map<config_dataset_id_type, std::pair<dataset_key_type, dataset_type> > 
00428             new_dataset_keys_and_types_map;
00429         
00430         Controller::getInstance().setInputDatasetsInConfiguration(
00431             config_widget_->getConfiguration(), input_ds_map, new_dataset_keys_and_types_map);
00432         
00433         
00434         if (config_widget_ != 0)
00435         {
00436           std::map<config_dataset_id_type, std::pair<dataset_key_type, dataset_type> >::const_iterator 
00437               new_keys_and_types_iter = new_dataset_keys_and_types_map.begin();
00438           for ( ; new_keys_and_types_iter != new_dataset_keys_and_types_map.end();
00439                   ++new_keys_and_types_iter)
00440           {
00441             try
00442             {
00443               config_widget_->setMetadataForInputDataset(
00444                   new_keys_and_types_iter->first, 
00445                     Controller::getInstance().getMetadataFor(
00446                         new_keys_and_types_iter->second.first));
00447             }
00448             catch (DatasetNotFoundException const&)
00449             {
00450               continue;
00451             }
00452           }
00453         
00454           // Show the configuration widget
00455           if (!config_widget_->showConfiguration())
00456           {
00457            //TODO handle error in configuration widget
00458            return;
00459           }
00460         }
00461                 
00462         config_dataset_id_to_output_map_type ds_id_to_output_map;
00463         definition_->setOutputDatasetIds(ds_id_to_output_map);
00464 
00465         Controller::getInstance().setOutputDatasetsInConfiguration(
00466             config_widget_->getConfiguration(), ds_id_to_output_map, new_dataset_keys_and_types_map);
00467         
00468         if (new_dataset_keys_and_types_map.empty() == false)
00469         {
00470           std::vector<std::pair<dataset_key_type, dataset_type> > new_keys_and_types_array;
00471           
00472           std::map<config_dataset_id_type, std::pair<dataset_key_type, dataset_type> >::const_iterator 
00473               new_keys_and_types_iter = new_dataset_keys_and_types_map.begin();
00474           
00475           for ( ; new_keys_and_types_iter != new_dataset_keys_and_types_map.end();
00476                   ++new_keys_and_types_iter)
00477           {
00478             new_keys_and_types_array.push_back(new_keys_and_types_iter->second);
00479           }
00480           emit notifyNewDatasetGeneration(new_keys_and_types_array);
00481         }
00482       }
00483     }
00484   }
00485 }

Generated on Sun Aug 13 18:19:39 2006 for iMEDgine by  doxygen 1.4.6