Commit be9b314f authored by Christopher Combs's avatar Christopher Combs
Browse files

Added Template and TemplateList classes. Fixes #5117.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/branches/ipce@8300 41f8697f-d340-4b68-9986-7bafba869bb8
parent 3137c844
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#include "ImageList.h"
#include "MosaicSceneWidget.h"
#include "TargetBodyList.h"
#include "TemplateList.h"
#include "WorkOrder.h"

class QAction;
@@ -192,6 +193,8 @@ namespace Isis {
   *                           Fixes #4492.
   *   @history 2017-11-09 Tyler Wilson - Made changes to updateRecentProjects() to handle deleting
   *                           the OpenRecentProjectWorkOrder.  Fixes #5220.
   *   @history 2017-11-03 Christopher Combs - Added support for new Template and TemplateList
   *                           classes. Fixes #5117.
   */
  class Directory : public QObject {
    Q_OBJECT
+28 −37
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@

#include "Project.h"
#include "ProjectItemModel.h"
#include "Template.h"
#include "TemplateList.h"

namespace Isis {
  /**
@@ -41,6 +43,7 @@ namespace Isis {
      WorkOrder(project) {

    m_isUndoable = true;
    m_list = NULL;

    QAction::setText(tr("Import Template"));
    QUndoCommand::setText(tr("Import Template"));
@@ -63,6 +66,7 @@ namespace Isis {
   * Destructor
   */
  ImportTemplateWorkOrder::~ImportTemplateWorkOrder() {
    m_list = NULL;
  }


@@ -129,7 +133,9 @@ namespace Isis {
    }

    QString selectedFilter;
    QStringList templateFileNames = QFileDialog::getOpenFileNames(
    QStringList templateFileNames;

    templateFileNames = QFileDialog::getOpenFileNames(
        qobject_cast<QWidget *>(parent()),
        "Import " + itemType,
        QString(),
@@ -143,14 +149,12 @@ namespace Isis {
      return false;
    }

    // The user must choose a filter to import any file. The type is saved in m_lastChosenFileType
    // The user must choose a filter to import any file. The type is saved in m_fileType
    // Currently, the only options for this will be "maps" and "registrations"
    m_lastChosenFileType = selectedFilter.remove(QRegularExpression(" \\(.*\\)")).toLower();
    m_fileType = selectedFilter.remove(QRegularExpression(" \\(.*\\)")).toLower();
    setInternalData(templateFileNames);

    return true;


  }


@@ -162,40 +166,24 @@ namespace Isis {
   * directory, it will not be copied over.
   */
  void ImportTemplateWorkOrder::execute() {
    QDir templateFolder = project()->addTemplateFolder(m_lastChosenFileType);

    QDir templateFolder = project()->addTemplateFolder(m_fileType + "/import");
    QStringList templateFileNames = internalData();

    QString newFile;
    QString notCopied;
    m_list = new TemplateList(
      templateFolder.dirName(), m_fileType, m_fileType + "/" +templateFolder.dirName() );

    foreach (FileName filename, templateFileNames) {
      newFile = m_lastChosenFileType + "/" + filename.name();

      // If the file is already in the folder, don't copy it over
      if ( !templateFolder.exists(filename.name())) {
        QFile::copy(filename.expanded(), templateFolder.path() + "/" +newFile);
        m_newFileList.append(FileName(newFile));
      }
      else {
        notCopied += filename.name() + "\n";
      }
      QFile::copy(filename.expanded(), templateFolder.path() + "/" + filename.name());
      m_list->append(new Template(
        templateFolder.path() + "/" + filename.name(), m_fileType, templateFolder.dirName()));
    }

    // Let the user know if a file already existed in the templates directory and was not copied
    if (!notCopied.isEmpty()) {
      QMessageBox::information(
        qobject_cast<QWidget *>(parent()),
        "Not all templates were copied",
        "The following already exist in the " + templateFolder.dirName() +
        " directory:\n\n" + notCopied
      );
    }

    if (!m_newFileList.isEmpty()) {
     project()->addTemplates(m_newFileList);
    if (!m_list->isEmpty()) {
     project()->addTemplates(m_list);
     project()->setClean(false);
    }


  }


@@ -206,14 +194,17 @@ namespace Isis {
   * and the ProjectItemModel
   */
  void ImportTemplateWorkOrder::undoExecution() {
    foreach ( FileName filename, m_newFileList) {
      project()->removeTemplate(filename);
      QFile::remove(project()->templateRoot() + "/" + filename.toString());
    if (m_list && project()->templates().size() > 0) {
      m_list->deleteFromDisk( project() );
      ProjectItem *currentItem =
          project()->directory()->model()->findItemData(QVariant::fromValue(filename.toString()));
          project()->directory()->model()->findItemData(QVariant::fromValue(m_list));
      project()->directory()->model()->removeItem(currentItem);
    }

    m_newFileList.clear();
    foreach ( Template *currentTemplate, *m_list) {
      delete currentTemplate;
    }
    delete m_list;
    m_list = NULL;
  }

}
+7 −3
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
 */
 #include "WorkOrder.h"
 #include "ProjectItem.h"
 #include "Template.h"
 #include "TemplateList.h"

 namespace Isis {
   /**
@@ -36,6 +38,8 @@
    * @internal
    *   @history 2017-08-23 Tracie Sucharski - Fixed assignment to itemType in setupExecution to use
    *                          assignment operator rather than comparison operator.
    *   @history 2017-11-03 Christopher Combs - Added support for new Template and TemplateList
    *                          classes. Fixes #5117.
    */
    class ImportTemplateWorkOrder : public WorkOrder {
        Q_OBJECT
@@ -54,8 +58,8 @@
      private:
        ImportTemplateWorkOrder &operator=(const ImportTemplateWorkOrder &rhs);

        QList<FileName> m_newFileList;
        QString m_lastChosenFileType; //!< The file type filter chosen in the QFileDialog
        TemplateList *m_list;
        QString m_fileType; //!< The file type filter chosen in the QFileDialog
    };
 }

+57 −41
Original line number Diff line number Diff line
@@ -70,6 +70,8 @@
#include "ShapeReader.h"
#include "Target.h"
#include "TargetBodyList.h"
#include "Template.h"
#include "TemplateList.h"
#include "WorkOrder.h"
#include "WorkOrderFactory.h"
#include "XmlStackedHandlerReader.h"
@@ -98,6 +100,7 @@ namespace Isis {
    m_imageReader = NULL;
    m_shapeReader = NULL;
    m_shapes = NULL;
    m_templates = NULL;
    m_warnings = NULL;
    m_workOrderHistory = NULL;
    m_isTemporaryProject = true;
@@ -223,6 +226,8 @@ namespace Isis {

    m_targets = new TargetBodyList;

    m_templates = new QList<TemplateList *>;

    m_guiCameras = new GuiCameraList;

    m_bundleSolutionInfo = new QList<BundleSolutionInfo *>;
@@ -392,6 +397,24 @@ namespace Isis {
        warn(msg);
        throw IException(IException::Io, msg, _FILEINFO_);
      }
      if ( !dir.mkdir( templateRoot() ) ) {
        QString msg = QString("Unable to create folder [%1] when trying to initialize project")
                        .arg( templateRoot() );
        warn(msg);
        throw IException(IException::Io, msg, _FILEINFO_);
      }
      if ( !dir.mkdir( templateRoot() + "/maps" ) ) {
        QString msg = QString("Unable to create folder [%1] when trying to initialize project")
                        .arg( templateRoot() );
        warn(msg);
        throw IException(IException::Io, msg, _FILEINFO_);
      }
      if ( !dir.mkdir( templateRoot() + "/registrations" ) ) {
        QString msg = QString("Unable to create folder [%1] when trying to initialize project")
                        .arg( templateRoot() );
        warn(msg);
        throw IException(IException::Io, msg, _FILEINFO_);
      }
    }
    catch (...) {
      warn("Failed to create project directory structure");
@@ -545,6 +568,7 @@ namespace Isis {
    m_images->clear();
    m_shapes->clear();
    m_controls->clear();
    m_templates->clear();
    m_targets->clear();
    m_guiCameras->clear();
    m_bundleSolutionInfo->clear();
@@ -638,13 +662,11 @@ namespace Isis {
      stream.writeEndElement();
    }

    if ( !m_templates.isEmpty() ) {
      stream.writeStartElement("templates");
    if ( !m_templates->isEmpty() ) {
      stream.writeStartElement("templateLists");

      for (int i = 0; i < m_templates.count(); i++) {
        stream.writeStartElement("template");
        stream.writeAttribute("fileName", m_templates.at(i).dir().dirName() + "/" + m_templates.at(i).name());
        stream.writeEndElement();
      for (int i = 0; i < m_templates->count(); i++) {
        m_templates->at(i)->save(stream, this, newProjectRoot);
      }

      stream.writeEndElement();
@@ -954,37 +976,42 @@ namespace Isis {
   *
   * @param newFileList QList of FileNames for each new imported template
   */
  void Project::addTemplates(QList<FileName> newFileList) {
    m_templates.append(newFileList);
    emit templatesAdded(newFileList);
  void Project::addTemplates(TemplateList *templateList) {
    foreach (Template *templateFile, *templateList) {
      connect( this, SIGNAL( projectRelocated(Project *) ),
               templateFile, SLOT( updateFileName(Project *) ) );
    }


  /**
   * Remove a FileName from m_templates
   *
   * @param file FileName to be removed
   */
  void Project::removeTemplate(FileName file) {
    m_templates.removeOne(file);
    m_templates->append(templateList);
    emit templatesAdded(templateList);
  }


  /**
   * Create and navigate to the appropriate template type folder in the project directory.
   *
   * @param prefix The name of the director under templates/ to store the template file.
   */
  QDir Project::addTemplateFolder(QString prefix) {

    QDir templateFolder = templateRoot();
    prefix += "%1";
    int prefixCounter = 0;
    QString numberedPrefix;

    do {
      prefixCounter++;
      numberedPrefix = prefix.arg( QString::number(prefixCounter) );
    }
    while ( templateFolder.exists(numberedPrefix) );

    if ( !templateFolder.mkpath(prefix) ) {
    if ( !templateFolder.mkpath(numberedPrefix) ) {
      throw IException(IException::Io,
          tr("Could not create template directory [%1] in [%2].")
            .arg(prefix).arg( templateFolder.absolutePath() ),
            .arg(numberedPrefix).arg( templateFolder.absolutePath() ),
          _FILEINFO_);
    }

    templateFolder.cd(numberedPrefix);

    return templateFolder;
  }

@@ -1915,8 +1942,8 @@ namespace Isis {
   *
   * @return QList of FileName
   */
  QList<FileName> Project::templates() {
    return m_templates;
  QList<TemplateList *> Project::templates() {
    return *m_templates;
  }


@@ -2041,21 +2068,8 @@ namespace Isis {
   * @param newProjectRoot The new root directory for the project.
   */
  void Project::relocateProjectRoot(QString newProjectRoot) {
    QString oldRoot = templateRoot();
    *m_projectRoot = newProjectRoot;

    emit projectRelocated(this);

    addTemplateFolder("maps");
    addTemplateFolder("registrations");

    // This is a temporary fix until we create an object for Templates
    foreach (FileName templateFile, templates()) {
      QFile::copy(oldRoot + "/" + templateFile.toString(), templateRoot() + "/" + templateFile.toString());
      ProjectItem *currentItem =
          directory()->model()->findItemData(QVariant::fromValue(templateFile.toString()));
      currentItem->setData(QVariant(templateFile.toString()));
    }
  }


@@ -2735,8 +2749,8 @@ namespace Isis {
      else if (localName == "shapeLists") {
        m_shapeLists.append(new ShapeList(m_project, reader()));
      }
      else if (localName == "template") {
        m_templates.append(FileName(m_project->templateRoot() + "/" + atts.value("fileName")));
      else if (localName == "templateList") {
        m_templates.append(new TemplateList(m_project, reader()));
      }
      //  workOrders are stored in history.xml, using same reader as project.xml
      else if (localName == "workOrder") {
@@ -2822,8 +2836,10 @@ namespace Isis {
        m_project->addBundleSolutionInfo(bundleInfo);
      }
    }
    else if (localName == "templates") {
      m_project->addTemplates(m_templates);
    else if (localName == "templateLists") {
      foreach (TemplateList *list, m_templates) {
        m_project->addTemplates(list);
      }
    }

    return XmlStackedHandler::endElement(namespaceURI, localName, qName);
+10 −5
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ class QXmlStreamWriter;
#include "ImageList.h"
#include "ShapeList.h"
#include "TargetBody.h"
#include "TemplateList.h"
#include "XmlStackedHandler.h"

namespace Isis {
@@ -53,6 +54,8 @@ namespace Isis {
  class ImageReader;
  class ProgressBar;
  class ShapeReader;
  class Template;
  class TemplateList;
  class WorkOrder;

  /**
@@ -182,6 +185,8 @@ namespace Isis {
   *   @history 2017-11-08 Ian Humphrey - Changed save() from a void to a bool return value. This
   *                           indicates if the save dialog (for a temp project) is successfully
   *                           saved (i.e. not cancelled). Fixes #5205.
   *   @history 2017-11-03 Christopher Combs - Added support for new Template and TemplateList
   *                           classes. Fixes #5117.
   */
  class Project : public QObject {
    Q_OBJECT
@@ -203,7 +208,7 @@ namespace Isis {
      QDir addShapeFolder(QString prefix);
      void addShapes(QStringList shapeFiles);
      void addShapes(ShapeList newShapes);
      void addTemplates(QList<FileName> templateFiles);
      void addTemplates(TemplateList *templateFiles);
      QDir addTemplateFolder(QString prefix);
      void addBundleSolutionInfo(BundleSolutionInfo *bundleSolutionInfo);
      void loadBundleSolutionInfo(BundleSolutionInfo *bundleSolutionInfo);
@@ -264,7 +269,7 @@ namespace Isis {

      static QString templateRoot(QString projectRoot);
      QString templateRoot() const;
      QList<FileName> templates();
      QList<TemplateList *> templates();
      void removeTemplate(FileName file);

      void deleteAllProjectFiles();
@@ -410,7 +415,7 @@ namespace Isis {
       */
      void workOrderFinished(WorkOrder *);

      void templatesAdded(QList<FileName> newFileList);
      void templatesAdded(TemplateList *newTemplates);



@@ -476,7 +481,7 @@ namespace Isis {
          QList<ShapeList *> m_shapeLists;
          QList<ControlList *> m_controls;
          QList<BundleSolutionInfo *> m_bundleSolutionInfos;
          QList<FileName> m_templates;
          QList<TemplateList *> m_templates;
          WorkOrder *m_workOrder;
      };

@@ -491,7 +496,7 @@ namespace Isis {
      QList<ControlList *> *m_controls;
      QList<ShapeList *> *m_shapes;
      TargetBodyList *m_targets;
      QList<FileName> m_templates;
      QList<TemplateList *> *m_templates;
      GuiCameraList *m_guiCameras;
      QList<BundleSolutionInfo *> *m_bundleSolutionInfo;

Loading