root/OpenSceneGraph/trunk/src/osgPlugins/qfont/ReaderQFont.cpp @ 11172

Revision 11172, 2.4 kB (checked in by robert, 5 years ago)

From Mathias Froehlich, "If you want to have that qfont plugin loader, this is the updated
implementation which uses osgQt and includes the changes to make fonts load
without a file on disk."

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 2009-2010 Mathias Froehlich
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13#include <osgDB/FileNameUtils>
14#include <osgDB/Registry>
15#include <osgText/Font>
16
17#include <QtCore/QThread>
18#include <QtGui/QApplication>
19#include <QtGui/QFont>
20#include <QtGui/QFontDatabase>
21
22#include <osgQt/QFontImplementation>
23
24namespace osgQFont {
25
26class ReaderQFont : public osgDB::ReaderWriter
27{
28    public:
29        ReaderQFont()
30        {
31            supportsExtension("qfont", "Qt font meta loader");
32        }
33       
34        virtual const char* className() const { return "QFont Font Reader"; }
35       
36        virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
37        {
38            if (!acceptsExtension(osgDB::getLowerCaseFileExtension(file)))
39                return ReadResult::FILE_NOT_HANDLED;
40
41            if (!QApplication::instance())
42            {
43                osg::notify(osg::WARN) << "Trying to load qfont \"" << file << "\" from within a non qt application!" << std::endl;
44                return ReadResult::FILE_NOT_FOUND;
45            }
46
47            if (!QFontDatabase::supportsThreadedFontRendering() && QApplication::instance()->thread() != QThread::currentThread())
48            {
49                osg::notify(osg::WARN) << "Trying to load qfont \"" << file << "\" from a non gui thread "
50                    "within qt application without threaded font rendering!" << std::endl;
51                return ReadResult::FILE_NOT_FOUND;
52            }
53
54            QFont font;
55            if (!font.fromString(QString::fromStdString(osgDB::getNameLessExtension(file))))
56                return ReadResult::FILE_NOT_FOUND;
57
58            return new osgText::Font(new osgQt::QFontImplementation(font));
59        }
60};
61
62// now register with Registry to instantiate the above
63// reader/writer.
64REGISTER_OSGPLUGIN(qfont, ReaderQFont)
65
66}
Note: See TracBrowser for help on using the browser.