cards/qmlapplicationviewer/qmlapplicationviewer.cpp
author John Kern <johnk@symbian.org>
Mon, 25 Oct 2010 10:35:32 -0700
changeset 3 2e16639599b7
permissions -rwxr-xr-x
flipping cards example
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     1
// checksum 0xdf1f version 0x10008
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     2
#include "qmlapplicationviewer.h"
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     3
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     4
#include <QtCore/QCoreApplication>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     5
#include <QtCore/QDir>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     6
#include <QtCore/QFileInfo>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     7
#include <QtDeclarative/QDeclarativeComponent>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     8
#include <QtDeclarative/QDeclarativeEngine>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
     9
#include <QtDeclarative/QDeclarativeContext>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    10
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    11
#if defined(QMLJSDEBUGGER)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    12
#include <jsdebuggeragent.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    13
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    14
#if defined(QMLOBSERVER)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    15
#include <qdeclarativeviewobserver.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    16
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    17
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    18
#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    19
#include <eikenv.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    20
#include <eikappui.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    21
#include <aknenv.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    22
#include <aknappui.h>
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    23
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    24
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    25
class QmlApplicationViewerPrivate
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    26
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    27
    QString mainQmlFile;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    28
    friend class QmlApplicationViewer;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    29
    static QString adjustPath(const QString &path);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    30
};
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    31
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    32
QString QmlApplicationViewerPrivate::adjustPath(const QString &path)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    33
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    34
#ifdef Q_OS_UNIX
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    35
#ifdef Q_OS_MAC
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    36
    if (!QDir::isAbsolutePath(path))
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    37
        return QCoreApplication::applicationDirPath()
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    38
                + QLatin1String("/../Resources/") + path;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    39
#else
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    40
    const QString pathInShareDir = QCoreApplication::applicationDirPath()
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    41
        + QLatin1String("/../share/")
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    42
        + QFileInfo(QCoreApplication::applicationFilePath()).fileName()
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    43
        + QLatin1Char('/') + path;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    44
    if (QFileInfo(pathInShareDir).exists())
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    45
        return pathInShareDir;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    46
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    47
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    48
    return path;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    49
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    50
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    51
QmlApplicationViewer::QmlApplicationViewer(QWidget *parent) :
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    52
    QDeclarativeView(parent),
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    53
    m_d(new QmlApplicationViewerPrivate)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    54
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    55
    connect(engine(), SIGNAL(quit()), SLOT(close()));
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    56
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    57
#ifdef QMLJSDEBUGGER
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    58
    new QmlJSDebugger::JSDebuggerAgent(engine());
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    59
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    60
#ifdef QMLOBSERVER
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    61
    new QmlJSDebugger::QDeclarativeViewObserver(this, parent);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    62
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    63
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    64
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    65
QmlApplicationViewer::~QmlApplicationViewer()
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    66
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    67
    delete m_d;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    68
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    69
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    70
void QmlApplicationViewer::setMainQmlFile(const QString &file)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    71
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    72
    m_d->mainQmlFile = QmlApplicationViewerPrivate::adjustPath(file);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    73
    setSource(QUrl::fromLocalFile(m_d->mainQmlFile));
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    74
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    75
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    76
void QmlApplicationViewer::addImportPath(const QString &path)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    77
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    78
    engine()->addImportPath(QmlApplicationViewerPrivate::adjustPath(path));
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    79
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    80
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    81
void QmlApplicationViewer::setOrientation(Orientation orientation)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    82
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    83
#ifdef Q_OS_SYMBIAN
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    84
    if (orientation != Auto) {
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    85
#if defined(ORIENTATIONLOCK)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    86
        const CAknAppUiBase::TAppUiOrientation uiOrientation =
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    87
                (orientation == LockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    88
                    : CAknAppUi::EAppUiOrientationLandscape;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    89
        CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    90
        TRAPD(error,
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    91
            if (appUi)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    92
                appUi->SetOrientationL(uiOrientation);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    93
        );
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    94
#else // ORIENTATIONLOCK
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    95
        qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    96
#endif // ORIENTATIONLOCK
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    97
    }
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    98
#elif defined(Q_WS_MAEMO_5)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
    99
    Qt::WidgetAttribute attribute;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   100
    switch (orientation) {
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   101
    case LockPortrait:
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   102
        attribute = Qt::WA_Maemo5PortraitOrientation;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   103
        break;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   104
    case LockLandscape:
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   105
        attribute = Qt::WA_Maemo5LandscapeOrientation;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   106
        break;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   107
    case Auto:
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   108
    default:
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   109
        attribute = Qt::WA_Maemo5AutoOrientation;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   110
        break;
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   111
    }
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   112
    setAttribute(attribute, true);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   113
#else // Q_OS_SYMBIAN
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   114
    Q_UNUSED(orientation);
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   115
#endif // Q_OS_SYMBIAN
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   116
}
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   117
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   118
void QmlApplicationViewer::show()
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   119
{
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   120
#ifdef Q_OS_SYMBIAN
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   121
    showFullScreen();
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   122
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   123
    showMaximized();
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   124
#else
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   125
    QDeclarativeView::show();
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   126
#endif
2e16639599b7 flipping cards example
John Kern <johnk@symbian.org>
parents:
diff changeset
   127
}