mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2361 AppFwk : Make sure field of FilePath supports space, add more tests
This commit is contained in:
parent
c0d9ef67fa
commit
76b84cbb17
@ -50,7 +50,19 @@ bool caf::FilePath::operator==(const FilePath& other) const
|
||||
QTextStream& operator>>(QTextStream& str, caf::FilePath& filePath)
|
||||
{
|
||||
QString text;
|
||||
str >> text;
|
||||
|
||||
while (str.status() == QTextStream::Ok)
|
||||
{
|
||||
// Read QChar to avoid white space trimming when reading QString
|
||||
QChar singleChar;
|
||||
str >> singleChar;
|
||||
|
||||
if (!singleChar.isNull())
|
||||
{
|
||||
text += singleChar;
|
||||
}
|
||||
}
|
||||
|
||||
filePath.setPath(text);
|
||||
|
||||
return str;
|
||||
|
@ -12,6 +12,8 @@
|
||||
#include "cafPdmReferenceHelper.h"
|
||||
#include "cafPdmValueField.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
class DemoPdmObject: public caf::PdmObjectHandle
|
||||
{
|
||||
@ -36,6 +38,7 @@ public:
|
||||
this->addField(&m_memberStringField, "m_memberStringField");
|
||||
|
||||
|
||||
|
||||
// Default values
|
||||
m_doubleMember = 2.1;
|
||||
m_intMember = 7;
|
||||
@ -87,12 +90,18 @@ public:
|
||||
this->addField(&m_texts, "Texts");
|
||||
this->addField(&m_childArrayField, "DemoPdmObjectects");
|
||||
this->addField(&m_ptrField, "m_ptrField");
|
||||
|
||||
this->addField(&m_singleFilePath, "m_singleFilePath");
|
||||
this->addField(&m_multipleFilePath, "m_multipleFilePath");
|
||||
|
||||
}
|
||||
|
||||
caf::PdmDataValueField<QString > m_texts;
|
||||
caf::PdmChildArrayField<DemoPdmObject*> m_childArrayField;
|
||||
caf::PdmPtrField<InheritedDemoObj*> m_ptrField;
|
||||
|
||||
caf::PdmDataValueField<caf::FilePath> m_singleFilePath;
|
||||
caf::PdmDataValueField<std::vector<caf::FilePath>> m_multipleFilePath;
|
||||
};
|
||||
|
||||
|
||||
@ -589,3 +598,37 @@ TEST(BaseTest, PdmPointer)
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(BaseTest, PdmFilePath)
|
||||
{
|
||||
InheritedDemoObj* d = new InheritedDemoObj;
|
||||
|
||||
QVariant newVal = "path with space";
|
||||
d->m_singleFilePath.setFromQVariant(newVal);
|
||||
|
||||
QVariant var = d->m_singleFilePath.toQVariant();
|
||||
ASSERT_TRUE(newVal == var);
|
||||
|
||||
delete d;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(BaseTest, MultiplePdmFilePath)
|
||||
{
|
||||
InheritedDemoObj* d = new InheritedDemoObj;
|
||||
|
||||
QString newVal = "path with space";
|
||||
d->m_multipleFilePath.v().push_back(newVal);
|
||||
d->m_multipleFilePath.v().push_back(newVal);
|
||||
|
||||
QVariant var = d->m_multipleFilePath.toQVariant();
|
||||
QStringList str = var.toStringList();
|
||||
|
||||
EXPECT_EQ(2, str.size());
|
||||
|
||||
delete d;
|
||||
}
|
||||
|
@ -186,4 +186,42 @@ public:
|
||||
|
||||
};
|
||||
|
||||
|
||||
//==================================================================================================
|
||||
/// Partial specialization for FilePath
|
||||
//==================================================================================================
|
||||
|
||||
template <>
|
||||
class PdmUiFieldSpecialization<caf::FilePath>
|
||||
{
|
||||
public:
|
||||
/// Convert the field value into a QVariant
|
||||
static QVariant convert(const caf::FilePath& value)
|
||||
{
|
||||
return PdmValueFieldSpecialization<caf::FilePath>::convert(value);
|
||||
}
|
||||
|
||||
/// Set the field value from a QVariant
|
||||
static void setFromVariant(const QVariant& variantValue, caf::FilePath& value)
|
||||
{
|
||||
return PdmValueFieldSpecialization<caf::FilePath>::setFromVariant(variantValue, value);
|
||||
}
|
||||
|
||||
static bool isDataElementEqual(const QVariant& variantValue, const QVariant& variantValue2)
|
||||
{
|
||||
return PdmValueFieldSpecialization<caf::FilePath>::isEqual(variantValue, variantValue2);
|
||||
}
|
||||
|
||||
/// Methods to get a list of options for a field, specialized for AppEnum
|
||||
static QList<PdmOptionItemInfo> valueOptions( bool* useOptionsOnly, const caf::FilePath& )
|
||||
{
|
||||
return QList<PdmOptionItemInfo>();
|
||||
}
|
||||
|
||||
/// Methods to retrieve the possible PdmObject pointed to by a field
|
||||
static void childObjects(const PdmDataValueField< caf::FilePath > & field, std::vector<PdmObjectHandle*>* objects)
|
||||
{ }
|
||||
|
||||
};
|
||||
|
||||
} // End namespace caf
|
||||
|
@ -18,7 +18,7 @@ QTextStream& operator<<(QTextStream& str, const std::vector<caf::FilePath>& file
|
||||
trimmedEntries.push_back(text);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < trimmedEntries.size(); i++)
|
||||
for (int i = 0; i < trimmedEntries.size(); i++)
|
||||
{
|
||||
str << trimmedEntries[i];
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "cafPdmReferenceHelper.h"
|
||||
#include "cafPdmXmlObjectHandle.h"
|
||||
#include "cafPdmXmlObjectHandleMacros.h"
|
||||
#include "cafFilePath.h"
|
||||
|
||||
#include <QXmlStreamWriter>
|
||||
|
||||
@ -100,6 +101,9 @@ public:
|
||||
caf::PdmPtrField< caf::PdmObjectHandle* > m_pointerToItem;
|
||||
caf::PdmPtrField< caf::PdmObjectHandle* > m_pointerToDemoObj;
|
||||
|
||||
caf::PdmDataValueField< caf::FilePath > m_singleFilePath;
|
||||
|
||||
|
||||
void setDoubleMember(const double& d) { m_doubleMember = d; std::cout << "setDoubleMember" << std::endl; }
|
||||
double doubleMember() const { std::cout << "doubleMember" << std::endl; return m_doubleMember; }
|
||||
double m_doubleMember;
|
||||
|
@ -198,13 +198,20 @@ public:
|
||||
CAF_PDM_XML_InitField(&m_position, "Position");
|
||||
CAF_PDM_XML_InitField(&m_dir, "Dir");
|
||||
CAF_PDM_XML_InitField(&m_up, "Up");
|
||||
}
|
||||
|
||||
CAF_PDM_XML_InitField(&m_singleFilePath, "m_singleFilePath");
|
||||
CAF_PDM_XML_InitField(&m_multipleFilePath, "m_multipleFilePath");
|
||||
}
|
||||
|
||||
caf::PdmDataValueField<double> m_position;
|
||||
caf::PdmDataValueField<double> m_dir;
|
||||
caf::PdmDataValueField<double> m_up;
|
||||
caf::PdmProxyValueField<double> m_proxyDouble;
|
||||
|
||||
caf::PdmDataValueField<caf::FilePath> m_singleFilePath;
|
||||
caf::PdmDataValueField<std::vector<caf::FilePath>> m_multipleFilePath;
|
||||
|
||||
|
||||
void setDoubleMember(const double& d) { m_doubleMember = d; std::cout << "setDoubleMember" << std::endl; }
|
||||
double doubleMember() const { std::cout << "doubleMember" << std::endl; return m_doubleMember; }
|
||||
|
||||
@ -349,6 +356,37 @@ TEST(BaseTest, QXMLStreamTest)
|
||||
tt = inputStream.readNext();
|
||||
std::cout << inputStream.name().toString().toStdString() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(BaseTest, FilePathSerializing)
|
||||
{
|
||||
SimpleObj* s1 = new SimpleObj;
|
||||
|
||||
QString newVal = "path with space";
|
||||
s1->m_multipleFilePath.v().push_back(newVal);
|
||||
s1->m_multipleFilePath.v().push_back(newVal);
|
||||
|
||||
s1->m_singleFilePath = newVal;
|
||||
|
||||
QString serializedString = s1->writeObjectToXmlString();
|
||||
|
||||
{
|
||||
SimpleObj* ihd1 = new SimpleObj;
|
||||
|
||||
QXmlStreamReader xmlStream(serializedString);
|
||||
|
||||
ihd1->readObjectFromXmlString(serializedString, caf::PdmDefaultObjectFactory::instance());
|
||||
|
||||
EXPECT_EQ(2, ihd1->m_multipleFilePath.v().size());
|
||||
EXPECT_EQ(newVal.toStdString(), ihd1->m_singleFilePath().path().toStdString());
|
||||
|
||||
delete ihd1;
|
||||
}
|
||||
|
||||
delete s1;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user