Ejemplo n.º 1
0
void PreviewWidget::setCurrentDataset(ImportDescriptor* pDataset)
{
   ImportDescriptor* pActiveDataset = getCurrentDataset();
   if (pDataset == pActiveDataset)
   {
      return;
   }

   // Do nothing if the new current data set is not a data set in the current file
   if (pDataset != NULL)
   {
      QMap<QString, vector<ImportDescriptor*> >::const_iterator iter = mDatasets.find(mCurrentFile);
      if (iter != mDatasets.end())
      {
         vector<ImportDescriptor*> fileDatasets = iter.value();
         if (std::find(fileDatasets.begin(), fileDatasets.end(), pDataset) == fileDatasets.end())
         {
            return;
         }
      }
   }

   mpCurrentDataset = pDataset;

   // Delete the current preview
   destroyPreview();

   // Activate the label indicating that no data set preview is available
   mpDatasetStack->setCurrentIndex(0);

   // Check for no active data set
   if (mpCurrentDataset == NULL)
   {
      emit currentDatasetChanged(mpCurrentDataset);
      return;
   }

   // Only show the preview if the widget is visible or if the data set is imported
   if ((isVisible() == false) || (mpCurrentDataset->isImported() == false))
   {
      if (pActiveDataset != NULL)
      {
         emit currentDatasetChanged(NULL);
      }

      return;
   }

   if (mpImporter != NULL)
   {
      Service<PlugInManagerServices> pManager;

      Progress* pProgress = pManager->getProgress(dynamic_cast<PlugIn*>(mpImporter));
      if (pProgress != NULL)
      {
         pProgress->attach(SIGNAL_NAME(Subject, Modified), Slot(this, &PreviewWidget::progressUpdated));
         pProgress->updateProgress("Getting preview...", 0, NORMAL);
      }

      // Activate the progress bar
      mpDatasetStack->setCurrentIndex(1);

      // Update the data set label
      QMap<QString, vector<ImportDescriptor*> >::const_iterator iter = mDatasets.find(mCurrentFile);
      VERIFYNRV(iter != mDatasets.end());

      vector<ImportDescriptor*> fileDatasets = iter.value();

      unsigned int iIndex = 0;
      unsigned int numDatasets = fileDatasets.size();
      for (iIndex = 0; iIndex < numDatasets; ++iIndex)
      {
         ImportDescriptor* pCurrentDataset = fileDatasets[iIndex];
         if (pCurrentDataset == pDataset)
         {
            break;
         }
      }

      VERIFYNRV(iIndex < numDatasets);

      const DataDescriptor* pDescriptor = mpCurrentDataset->getDataDescriptor();
      VERIFYNRV(pDescriptor != NULL);

      mpDatasetLabel->setText("<b>Data Set (" + QString::number(iIndex + 1) + " of " +
         QString::number(numDatasets) + "):</b>  " + QString::fromStdString(pDescriptor->getName()));

      // Process events to erase the no preview available page
      qApp->processEvents();

      // Get the preview from the importer
      mpImporterWidget = mpImporter->getPreview(pDescriptor, pProgress);
      if (mpImporterWidget != NULL)
      {
         // Display the preview widget
         SpatialDataView* pView = dynamic_cast<SpatialDataView*>(mpImporterWidget);
         if (pView != NULL)
         {
            ChippingWidget* pChippingWidget = new ChippingWidget(pView,
               dynamic_cast<const RasterDataDescriptor*>(pDescriptor), mpPreview);
            VERIFYNRV(pChippingWidget != NULL);

            VERIFYNR(connect(pChippingWidget, SIGNAL(chipChanged()), this, SLOT(updateCurrentDataset())));
            mpImporterWidget = pChippingWidget;
         }
         else
         {
            mpImporterWidget->setParent(mpPreview);
         }

         QGridLayout* pGrid = dynamic_cast<QGridLayout*>(mpPreview->layout());
         if (pGrid != NULL)
         {
            pGrid->addWidget(mpImporterWidget, 0, 0);
         }

         // Activate the preview widget
         mpDatasetStack->setCurrentIndex(2);

         // Notify of changes
         emit currentDatasetChanged(mpCurrentDataset);
      }
      else
      {
         mpDatasetStack->setCurrentIndex(0);

         if (pActiveDataset != NULL)
         {
            emit currentDatasetChanged(NULL);
         }
      }

      if (pProgress != NULL)
      {
         pProgress->detach(SIGNAL_NAME(Subject, Modified), Slot(this, &PreviewWidget::progressUpdated));
      }
   }
}