All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
MultiWidgets::Mime Class Reference

Loads different files by using registered loaders based on MIME-type Has two kinds of mappings. More...

#include <MultiWidgets/Mime.hpp>

Inheritance diagram for MultiWidgets::Mime:
Radiant::MimeManager

Classes

class  Loader
 A Loader loads different files based on file extensions or mime types. More...
 
class  LoaderOptions
 This is passed to all the loaders as a parameter when loading Can only be constructed by Mime-class. More...
 

Public Types

typedef std::list< Loader * > MimeLoaderList
 List of different Mime-loaders.
 
typedef std::map< QString,
MimeLoaderList
MimeMap
 Mapping from Mime-descriptors to appropriate Mime-loaders.
 

Public Member Functions

void insertLoader (const QString &type, Loader *loader)
 Insert a instance-specific loader for a given type. More...
 
WidgetPtr load (const QString &filename, bool guessContent=false)
 Tries to load file in a suitable Widget. More...
 
Radiant::TaskPtr loadAsync (const QString &filename, std::function< void(const WidgetPtr &w)> callback, Valuable::Node::ListenerType type)
 Load the given file asynchronously. More...
 
WidgetPtr loadSynchronous (const QString &filename, bool guessContent=false)
 Tries to load file in a suitable Widget synchronously Synchronous loading means that the widget is ready to use after this function returns. More...
 
 Mime ()
 Constructor. More...
 
virtual ~Mime ()
 Destructor Deallocates loaders of this Mime.
 
- Public Member Functions inherited from Radiant::MimeManager
QStringList extensionsByMimeRegexp (const QString &mime) const
 Get a list of file extensions that match the given mime type. More...
 
void insertExtension (const QString &extension, const MimeType &type)
 Add or replace a shared mapping from file extension to mime type. More...
 
const MimeTypemimeTypeByExtension (const QString &ext) const
 Get the mime type by filename extension. More...
 

Static Public Member Functions

static void insertSharedLoader (const QString &type, Loader *loader)
 Insert a global loader shared for all Mime-objects. More...
 
- Static Public Member Functions inherited from Radiant::MimeManager
static void insertSharedExtension (const QString &extension, const MimeType &type)
 Add or replace a shared mapping from file extension to mime type. More...
 

Additional Inherited Members

- Protected Types inherited from Radiant::MimeManager
typedef std::map< QString,
MimeType
ExtensionMap
 Map from file extensions to mime types.
 
- Protected Attributes inherited from Radiant::MimeManager
ExtensionMap m_extensions
 Extension map specific for this instance.
 
- Static Protected Attributes inherited from Radiant::MimeManager
static ExtensionMap s_sharedExtensions
 Global extension map shared by all instances.
 

Detailed Description

Loads different files by using registered loaders based on MIME-type Has two kinds of mappings.

  • file extension -> mime type
  • mime type -> loader
See Also
load

Usage example:

 class MyLoader : public Mime::Loader
 {
   virtual WidgetPtr load(const QString & filename, const Mime::LoaderOptions & options)
   {
     MyWidgetPtr widget = MultiWidgets::create<MyWidget>();
     if (widget->load(filename)) {
       return widget;
     } else {
       delete widget;
       return 0;
     }
   }
 };
 MultiWidgets::Mime mime;
 mime.addLoader("image/png", new MyLoader());
 Widget * w = mime.load("myfile.png");
 if (w) w->changeParent(some_widget);

Predefined shared loaders:

Examples:
DropHandlerExample.cpp.

Constructor & Destructor Documentation

MultiWidgets::Mime::Mime ( )

Constructor.

Initializes predefined shared loaders.

Member Function Documentation

void MultiWidgets::Mime::insertLoader ( const QString &  type,
Loader loader 
)

Insert a instance-specific loader for a given type.

After insertion the loader's ownership is transferred to Mime ie. it dellocates it when necessary

Parameters
typeMime type, eg. image/jpeg
loaderInstance of a loader to register with the type.
static void MultiWidgets::Mime::insertSharedLoader ( const QString &  type,
Loader loader 
)
static

Insert a global loader shared for all Mime-objects.

Mime-class manages the deallocation of the loaders.

Parameters
typeMime type to load
loaderLoader to handle the given mime type
See Also
insertLoader
WidgetPtr MultiWidgets::Mime::load ( const QString &  filename,
bool  guessContent = false 
)

Tries to load file in a suitable Widget.

Widget returned may not be yet ready for the usage because loading requests are asynchronous.

The MIME type is deduced from the file extension. Instance specific loaders take precedence over shared loaders. Multiple loaders can be defined for the same type, the loaders are tried in insertion order. The lookup chain is as follows:

  • Most specific loaders, eg. toplevel/subtype
  • More general toplevel/*

The loaders are tried in order until some loader has successfully loaded the file.

See Also
loadSynchronous
Parameters
filenameFilename to load
guessContentThis paramter is ignored, not implemented yet
Returns
The loaded widget or null pointer if load was not succeed
Radiant::TaskPtr MultiWidgets::Mime::loadAsync ( const QString &  filename,
std::function< void(const WidgetPtr &w)>  callback,
Valuable::Node::ListenerType  type 
)

Load the given file asynchronously.

This function creates a task to load the given filename in a background thread. The given callback function will be executed with the created widget as a parameter when the widget is in Valuable::STATE_READY state. The behaviour is similar to that of MultiWidgets::BaseMediaWidget::onReady(). The callback function can be executed in the main-thread or in the background thread depending on the value of the type parameter.

Parameters
filenamefilename to load
callbackcallback function to execute when loading is complete
typetype of the callback
WidgetPtr MultiWidgets::Mime::loadSynchronous ( const QString &  filename,
bool  guessContent = false 
)

Tries to load file in a suitable Widget synchronously Synchronous loading means that the widget is ready to use after this function returns.

For example in the case of images it means that their size is set correctly but some of the mipmap-levels may still be loading.

The MIME type is deduced from the file extension. Instance specific loaders take precedence over shared loaders. Multiple loaders can be defined for the same type, the loaders are tried in insertion order. The lookup chain is as follows:

  • Most specific loaders, eg. toplevel/subtype
  • More general toplevel/*

The loaders are tried in order until some loader has successfully loaded the file.

See Also
load
Parameters
filenameFilename to load
Returns
The loaded widget or null pointer if the load was unsuccesful
Examples:
DropHandlerExample.cpp.