1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
#ifndef CC_FILE_IO_FILTER_HEADER
#define CC_FILE_IO_FILTER_HEADER
//qCC_db
#include <ccHObject.h>
#include <ccHObjectCaster.h>
//Local
#include "ccGlobalShiftManager.h"
class QWidget;
//! Typical I/O filter errors
enum CC_FILE_ERROR {CC_FERR_NO_ERROR,
CC_FERR_BAD_ARGUMENT,
CC_FERR_UNKNOWN_FILE,
CC_FERR_WRONG_FILE_TYPE,
CC_FERR_WRITING,
CC_FERR_READING,
CC_FERR_NO_SAVE,
CC_FERR_NO_LOAD,
CC_FERR_BAD_ENTITY_TYPE,
CC_FERR_CANCELED_BY_USER,
CC_FERR_NOT_ENOUGH_MEMORY,
CC_FERR_MALFORMED_FILE,
CC_FERR_CONSOLE_ERROR,
CC_FERR_BROKEN_DEPENDENCY_ERROR,
CC_FERR_FILE_WAS_WRITTEN_BY_UNKNOWN_PLUGIN,
CC_FERR_THIRD_PARTY_LIB_FAILURE,
CC_FERR_THIRD_PARTY_LIB_EXCEPTION,
CC_FERR_NOT_IMPLEMENTED,
};
//! Generic file I/O filter
/** Gives static access to file loader.
Must be implemented by any specific I/O filter.
**/
class FileIOFilter
{
public: //initialization
//! Destructor
virtual ~FileIOFilter() {}
//! Generic loading parameters
struct LoadParameters
{
//! Default constructor
LoadParameters()
: shiftHandlingMode(ccGlobalShiftManager::DIALOG_IF_NECESSARY)
, alwaysDisplayLoadDialog(true)
, coordinatesShiftEnabled(nullptr)
, coordinatesShift(nullptr)
, preserveShiftOnSave(true)
, autoComputeNormals(false)
, parentWidget(nullptr)
, sessionStart(true)
{}
//! How to handle big coordinates
ccGlobalShiftManager::Mode shiftHandlingMode;
//! Wether to always display a dialog (if any), even if automatic guess is possible
bool alwaysDisplayLoadDialog;
//! Whether shift on load has been applied after loading (optional)
bool* coordinatesShiftEnabled;
//! If applicable, applied shift on load (optional)
CCVector3d* coordinatesShift;
//! If applicable, whether shift should be preserved or not (optional)
bool preserveShiftOnSave;
//! Whether normals should be computed at loading time (if possible - e.g. for gridded clouds) or not
bool autoComputeNormals;
//! Parent widget (if any)
QWidget* parentWidget;
//! Session start (whether the load action is the first of a session)
bool sessionStart;
};
//! Generic saving parameters
struct SaveParameters
{
//! Default constructor
SaveParameters()
: alwaysDisplaySaveDialog(true)
, parentWidget(nullptr)
{}
//! Wether to always display a dialog (if any), even if automatic guess is possible
bool alwaysDisplaySaveDialog;
//! Parent widget (if any)
QWidget* parentWidget;
};
//! Shared type
typedef QSharedPointer<FileIOFilter> Shared;
public: //public interface (to be reimplemented by each I/O filter)
//! Returns whether this I/O filter can import files
virtual bool importSupported() const { return false; }
//! Loads one or more entities from a file
/** This method must be implemented by children classes.
\param filename file to load
\param container container to store loaded entities
\param parameters generic loading parameters
\return error
**/
virtual CC_FILE_ERROR loadFile( const QString& filename,
ccHObject& container,
LoadParameters& parameters)
{
return CC_FERR_NOT_IMPLEMENTED;
}
//! Returns whether this I/O filter can export files
virtual bool exportSupported() const { return false; }
//! Saves an entity (or a group of) to a file
/** This method must be implemented by children classes.
\param entity entity (or group of) to save
\param filename filename
\param parameters generic saving parameters
\return error
**/
virtual CC_FILE_ERROR saveToFile( ccHObject* entity,
const QString& filename,
const SaveParameters& parameters)
{
return CC_FERR_NOT_IMPLEMENTED;
}
//! Returns the file filter(s) for this I/O filter
/** E.g. 'ASCII file (*.asc)'
\param onImport whether the requested filters are for import or export
\return list of filters
**/
virtual QStringList getFileFilters(bool onImport) const = 0;
//! Returns the default file extension
virtual QString getDefaultExtension() const = 0;
//! Returns whether a specific file can be loaded by this filter
/** Used when a file is dragged over the application window for instance.
Should remain simple (guess based on the file extension, etc.)
**/
//virtual bool canLoad(QString filename) const = 0;
//! Returns whether a specific extension can be loaded by this filter
/** \param upperCaseExt upper case extension
\return whether the extension is (theoretically) handled by this filter
**/
virtual bool canLoadExtension(const QString& upperCaseExt) const = 0;
//! Returns whether this I/O filter can save the specified type of entity
/** \param type entity type
\param multiple whether the filter can save multiple instances of this entity at once
\param exclusive whether the filter can only save this type of entity if selected or if it can be mixed with other types
\return whether the entity type can be saved
**/
virtual bool canSave(CC_CLASS_ENUM type, bool& multiple, bool& exclusive) const = 0;
public: //static methods
//! Loads one or more entities from a file with a known filter
/** Shortcut to FileIOFilter::loadFile
\param filename filename
\param parameters generic loading parameters
\param filter input filter
\param[out] result file error code
\return loaded entities (or 0 if an error occurred)
**/
QCC_IO_LIB_API static ccHObject* LoadFromFile( const QString& filename,
LoadParameters& parameters,
Shared filter,
CC_FILE_ERROR& result);
//! Loads one or more entities from a file with known type
/** Shortcut to the other version of FileIOFilter::LoadFromFile
\param filename filename
\param parameters generic loading parameters
\param[out] result file error code
\param fileFilter input filter 'file filter' (if empty, the best I/O filter will be guessed from the file extension)
\return loaded entities (or 0 if an error occurred)
**/
QCC_IO_LIB_API static ccHObject* LoadFromFile( const QString& filename,
LoadParameters& parameters,
CC_FILE_ERROR& result,
QString fileFilter = QString());
//! Saves an entity (or a group of) to a specific file thanks to a given filter
/** Shortcut to FileIOFilter::saveFile
\param entities entity to save (can be a group of other entities)
\param filename filename
\param parameters saving parameters
\param filter output filter
\return error type (if any)
**/
QCC_IO_LIB_API static CC_FILE_ERROR SaveToFile( ccHObject* entities,
const QString& filename,
const SaveParameters& parameters,
Shared filter);
//! Saves an entity (or a group of) to a specific file thanks to a given filter
/** Shortcut to the other version of FileIOFilter::SaveToFile
\param entities entity to save (can be a group of other entities)
\param filename filename
\param parameters saving parameters
\param fileFilter output filter 'file filter'
\return error type (if any)
**/
QCC_IO_LIB_API static CC_FILE_ERROR SaveToFile( ccHObject* entities,
const QString& filename,
const SaveParameters& parameters,
const QString& fileFilter);
//! Shortcut to the ccGlobalShiftManager mechanism specific for files
/** \param[in] P sample point (typically the first loaded)
\param[out] Pshift global shift
\param[out] preserveCoordinateShift whether shift sould be preserved on save
\param[in] loadParameters loading parameters
\param[in] useInputCoordinatesShiftIfPossible whether to use the input 'PShift' vector if possible
\return whether global shift has been defined/enabled
**/
QCC_IO_LIB_API static bool HandleGlobalShift( const CCVector3d& P,
CCVector3d& Pshift,
bool& preserveCoordinateShift,
LoadParameters& loadParameters,
bool useInputCoordinatesShiftIfPossible = false);
//! Displays (to console) the message corresponding to a given error code
/** \param err error code
\param action "saving", "reading", etc.
\param filename corresponding file
**/
QCC_IO_LIB_API static void DisplayErrorMessage(CC_FILE_ERROR err,
const QString& action,
const QString& filename);
//! Returns whether special characters are present in the input string
QCC_IO_LIB_API static bool CheckForSpecialChars(const QString& filename);
public: //loading "sessions" management
//! Indicates to the I/O filters that a new loading/saving session has started (for "Apply all" buttons for instance)
QCC_IO_LIB_API static void ResetSesionCounter();
//! Indicates to the I/O filters that a new loading/saving action has started
/** \return the updated session counter
**/
QCC_IO_LIB_API static unsigned IncreaseSesionCounter();
public: //global filters registration mechanism
//! Init internal filters (should be called once)
QCC_IO_LIB_API static void InitInternalFilters();
//! Registers a new filter
QCC_IO_LIB_API static void Register(Shared filter);
//! Returns the filter corresponding to the given 'file filter'
QCC_IO_LIB_API static Shared GetFilter(const QString& fileFilter, bool onImport);
//! Returns the best filter (presumably) to open a given file extension
QCC_IO_LIB_API static Shared FindBestFilterForExtension(const QString& ext);
//! Type of a I/O filters container
typedef std::vector< FileIOFilter::Shared > FilterContainer;
//! Returns the set of all registered filters
QCC_IO_LIB_API static const FilterContainer& GetFilters();
//! Unregisters all filters
/** Should be called at the end of the application
**/
QCC_IO_LIB_API static void UnregisterAll();
//! Called when the filter is unregistered
/** Does nothing by default **/
virtual void unregister() {}
};
#endif
|