Commit c81b011b authored by John Tourtellott's avatar John Tourtellott Committed by Kitware Robot

Merge topic 'resource-file-extension' into release

8189e1e9 Update filename logic when saving resources
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: T.J. Corona's avatarT.J. Corona <tj.corona@kitware.com>
Merge-request: !1457
parents 8ceeb621 8189e1e9
Pipeline #128682 running with stage
......@@ -51,26 +51,6 @@ Write::Result Write::operateInternal()
return this->createResult(smtk::operation::Operation::Outcome::FAILED);
}
// Ensure the resource suffix is .smtk so readers can subsequently read the
// file.
std::string filename = resource->location();
if (smtk::common::Paths::extension(resource->location()) != ".smtk")
{
filename = smtk::common::Paths::directory(filename) + "/" +
smtk::common::Paths::stem(filename) + ".smtk";
// If a file already exists with this name, append a distinguishing string
// to the name.
if (smtk::common::Paths::fileExists(filename))
{
std::string id = resource->id().toString();
filename = smtk::common::Paths::directory(filename) + "/" +
smtk::common::Paths::stem(filename) + "_" + id.substr(0, 8) + ".smtk";
}
resource->setLocation(filename);
}
{
std::ofstream file(resource->location());
if (!file.good())
......
......@@ -28,10 +28,12 @@
#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QObject>
#include <QTextStream>
void initSaveResourceBehaviorResources()
{
......@@ -75,24 +77,14 @@ pqSaveResourceReaction::State pqSaveResourceReaction::saveResource()
return pqSaveResourceAsReaction::saveResourceAs();
}
// Ensure the resource suffix is .smtk so readers can subsequently read the
// file.
// Ensure the resource suffix is .smtk so readers can subsequently read the file.
std::string filename = resource->location();
if (smtk::common::Paths::extension(resource->location()) != ".smtk")
std::string ext = smtk::common::Paths::extension(filename);
if (ext != ".smtk")
{
filename = smtk::common::Paths::directory(filename) + "/" +
smtk::common::Paths::stem(filename) + ".smtk";
// If a file already exists with this name, append a distinguishing string
// to the name.
if (smtk::common::Paths::fileExists(filename))
{
std::string id = resource->id().toString();
filename = smtk::common::Paths::directory(filename) + "/" +
smtk::common::Paths::stem(filename) + "_" + id.substr(0, 8) + ".smtk";
}
resource->setLocation(filename);
qWarning() << "Invalid resource filename " << QString::fromStdString(filename)
<< ": must have \".smtk\" extension";
return pqSaveResourceReaction::State::Aborted;
}
if (smtk::resource::Manager::Ptr manager = resource->manager())
......@@ -149,13 +141,31 @@ pqSaveResourceReaction::State pqSaveResourceAsReaction::saveResourceAs()
if (fileDialog.exec() == QDialog::Accepted)
{
QString fname = fileDialog.getSelectedFiles()[0];
QString qname = fileDialog.getSelectedFiles()[0];
std::string filename = qname.toStdString();
// Ensure the resource suffix is .smtk so readers can subsequently read the file.
std::string ext = smtk::common::Paths::extension(filename);
if (ext.empty())
{
// If no extension provided, then add .smtk
filename += ".smtk";
}
else if (ext != ".smtk")
{
// But if extension is not .smtk, then abort
QWidget* mainWidget = pqCoreUtilities::mainWidget();
QString text;
QTextStream qs(&text);
qs << "Invalid filename " << qname << ": must have \".smtk\" extension";
QMessageBox::critical(mainWidget, "Invalid File Name", text);
return pqSaveResourceReaction::State::Aborted;
}
if (smtk::resource::Manager::Ptr manager = resource->manager())
{
return manager->write(resource, fname.toStdString())
? pqSaveResourceReaction::State::Succeeded
: pqSaveResourceReaction::State::Failed;
return manager->write(resource, filename) ? pqSaveResourceReaction::State::Succeeded
: pqSaveResourceReaction::State::Failed;
}
else
{
......
......@@ -19,6 +19,7 @@
#include "smtk/attribute/ModelEntityItem.h"
#include "smtk/attribute/Resource.h"
#include "smtk/attribute/StringItem.h"
#include "smtk/common/Paths.h"
#include "smtk/resource/Manager.h"
......@@ -63,7 +64,8 @@ smtk::resource::ResourcePtr vtkSMTKAttributeReader::GenerateResource() const
this->Wrapper->GetResourceManager()->remove(this->Resource);
}
auto resource = smtk::attribute::Resource::create();
resource->setLocation(this->FileName);
auto name = smtk::common::Paths::stem(this->FileName);
resource->setName(name);
if (this->Wrapper)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment