Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 519b2fe3

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID 519b2fe31200e9a1e14c9c4e5e34e462ae3635bd
  • Vorgänger 8a706d3e
  • Nachfolger 28d2f01a

file, image and pattern validator

Unterschiede anzeigen:

EntityManager.pro
QT += core
QT += sql
#if you need Image Validation you must compile with += gui
#QT += gui
QT -= gui
TARGET = CuteEntityManager
......
src/validators/existvalidator.h \
src/validators/filevalidator.h \
src/validators/imagevalidator.h \
src/validators/uniquevalidator.h
src/validators/uniquevalidator.h \
src/validators/patternvalidator.h
SOURCES += \
src/entity.cpp \
......
src/validators/existvalidator.cpp \
src/validators/filevalidator.cpp \
src/validators/imagevalidator.cpp \
src/validators/uniquevalidator.cpp
src/validators/uniquevalidator.cpp \
src/validators/patternvalidator.cpp
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
src/validators/filevalidator.cpp
#include "filevalidator.h"
#include <QFileInfo>
#include <QMimeType>
#include <QMimeDatabase>
using namespace CuteEntityManager;
FileValidator::FileValidator() : Validator() {
}
/**
* @brief FileValidator::validateParam
* @param value must be the absolute FilePath
* @param param
* @return
*/
ErrorMsg FileValidator::validateParam(QVariant value, Param param) const {
QFileInfo file = QFileInfo(value.toString());
if (param.getName() == "mimeTypes") {
return this->validateMIMEType(file, param);
} else if (param.getName() == "extensions") {
return this->validateExtension(file, param);
} else if (param.getName() == "minSize"
&& file.size() < param.getValue().toLongLong()) {
return ErrorMsg(param.getName(),
"The file <property> is too small. Its size cannot be smaller than " +
param.getValue().toString() + " bytes.");
} else if (param.getName() == "maxSize") {
return ErrorMsg(param.getName(),
"The file <property> is too big. Its size cannot exceed " +
param.getValue().toString() + " bytes.");
}
return ErrorMsg();
}
ErrorMsg FileValidator::validateMIMEType(const QFileInfo &file,
const Param &param) const {
QMimeDatabase mimeDatabase;
QMimeType mimeType = mimeDatabase.mimeTypeForFile(file);
if (param.getValue().canConvert<QStringList>()) {
QStringList list = param.getValue().toStringList();
for (int i = 0; i < list.size(); ++i) {
if (mimeType.inherits(list.at(i))) {
return ErrorMsg();
}
}
return ErrorMsg(param.getName(),
"Only files with these MIME types are allowed: " + list.join(", ") + ".");
} else {
if (!mimeType.inherits(param.getValue().toString())) {
return ErrorMsg(param.getName(),
"Only files with this MIME type are allowed: " +
param.getValue().toString() + ".");
}
}
return ErrorMsg();
}
ErrorMsg FileValidator::validateExtension(const QFileInfo &file,
const Param &param) const {
if (param.getValue().canConvert<QStringList>()) {
QStringList list = param.getValue().toStringList();
for (int i = 0; i < list.size(); ++i) {
if (file.completeSuffix().contains(list.at(i))) {
return ErrorMsg();
}
}
return ErrorMsg(param.getName(),
"Only files with these extension types are allowed: " + list.join(", "));
} else {
if (!file.completeSuffix().contains(param.getValue().toString())) {
return ErrorMsg(param.getName(),
"Only files with this extension type are allowed: " +
param.getValue().toString() + ".");
}
}
return ErrorMsg();
}
src/validators/filevalidator.h
#ifndef FILEVALIDATOR_H
#define FILEVALIDATOR_H
#include "validator.h"
#include <QFileInfo>
namespace CuteEntityManager {
class FileValidator : public Validator {
Q_OBJECT
public:
FileValidator();
virtual ErrorMsg validateParam(QVariant value, Param param) const override;
virtual ErrorMsg validateMIMEType(const QFileInfo &file,
const Param &param) const;
virtual ErrorMsg validateExtension(const QFileInfo &file,
const Param &param) const;
};
}
#endif // FILEVALIDATOR_H
src/validators/imagevalidator.cpp
#include "imagevalidator.h"
#ifdef QT_GUI_LIB
#include <QImage>
#endif
using namespace CuteEntityManager;
ImageValidator::ImageValidator() : FileValidator() {
}
ErrorMsg ImageValidator::validateParam(QVariant value, Param param) const {
ErrorMsg msg = FileValidator::validateParam(value, param);
if (msg.getErrorMsg().isEmpty()) {
msg = this->validateImage(value, param);
}
return msg;
}
ErrorMsg ImageValidator::validateImage(QVariant value, Param param) const {
#ifdef QT_GUI_LIB
//isImage
QImage img = QImage(value.toString());
if (!img.isNull()) {
if (param.getName() == "minWidth"
&& img.width() < param.getValue().toLongLong()) {
ErrorMsg(param.getName(),
"The image " + value.toString() +
" is too smal. The width cannot be smaller than " + param.getValue().toString()
+ ".");
} else if (param.getName() == "maxWidth"
&& img.width() > param.getValue().toLongLong()) {
ErrorMsg(param.getName(),
"The image " + value.toString() +
" is too smal. The width cannot be larger than " + param.getValue().toString() +
".");
} else if (param.getName() == "minHeight"
&& img.height() < param.getValue().toLongLong()) {
ErrorMsg(param.getName(),
"The image " + value.toString() +
" is too smal. The height cannot be smaller than " + param.getValue().toString()
+ ".");
} else if (param.getName() == "maxHeight"
&& img.height() > param.getValue().toLongLong()) {
ErrorMsg(param.getName(),
"The image " + value.toString() +
" is too smal. The height cannot be larger than " + param.getValue().toString()
+ ".");
}
} else {
return ErrorMsg("", "The file " + value.toString() + " is not a valid image.");
}
#else
Q_UNUSED(value)
Q_UNUSED(param)
#endif
return ErrorMsg();
}
src/validators/imagevalidator.h
#ifndef IMAGEVALIDATOR_H
#define IMAGEVALIDATOR_H
#include "filevalidator.h"
namespace CuteEntityManager {
class ImageValidator : public FileValidator {
......
public:
ImageValidator();
ErrorMsg validateParam(QVariant value, Param param) const final override;
virtual ErrorMsg validateImage(QVariant value, Param param) const;
};
}
src/validators/patternvalidator.cpp
#include "patternvalidator.h"
#include <QRegularExpression>
using namespace CuteEntityManager;
PatternValidator::PatternValidator() : Validator() {
}
ErrorMsg PatternValidator::validateParam(QVariant value, Param param) const {
QRegularExpression regExp = QRegularExpression();
if (param.getName().isEmpty()) {
regExp.setPattern(param.getValue().toString());
} else {
regExp.setPattern(param.getName());
}
if (regExp.match(value.toString()).hasMatch()) {
return ErrorMsg();
}
return ErrorMsg(param.getName(), "<property> not matches the pattern.");
}
src/validators/patternvalidator.h
#ifndef PATTERNVALIDATOR_H
#define PATTERNVALIDATOR_H
#include "validator.h"
namespace CuteEntityManager {
class PatternValidator : public Validator {
Q_OBJECT
public:
PatternValidator();
ErrorMsg validateParam(QVariant value, Param param) const final override;
};
}
#endif // PATTERNVALIDATOR_H
src/validators/validator.cpp
hash.insert("required", "RequiredValidator");
hash.insert("unique", "UniqueValidator");
hash.insert("url", "UrlValidator");
hash.insert("pattern", "PatternValidator");
return hash;
}
src/validators/validatorfactory.cpp
#include "requiredvalidator.h"
#include "urlvalidator.h"
#include "uniquevalidator.h"
#include "patternvalidator.h"
using namespace CuteEntityManager;
ValidatorFactory::ValidatorFactory() {
......
ValidatorFactory::registerClass<NumberValidator>();
ValidatorFactory::registerClass<DateValidator>();
ValidatorFactory::registerClass<RequiredValidator>();
ValidatorFactory::registerClass<PatternValidator>();
ValidatorFactory::registerClass<UniqueValidator>();
ValidatorFactory::registerClass<UrlValidator>();
}

Auch abrufbar als: Unified diff