Skip to content

Commit

Permalink
qt: Switch to QWebEngineView / QWebEnginePage.
Browse files Browse the repository at this point in the history
The capabilities of QWebEngine* are significantly different than those of the previous QWebPage/QWebView, so the QWebView class was substantially refactored to reflect the new API's. Properties and signals exposed in sclang now follow their Qt counterparts more closely, to make maintainence and upgrades more straightforward in the future. Some calls, such as toHTML and toPlainText, have become asynchronous - these are implemented on the sclang side by passing a callback function as the first argument, which is converted to a QcCallback object and used to signal the language with the results. Though this is only used in webview's currently, it should be easily portable to other async callback cases in sclang if needed.
  • Loading branch information
scztt committed Apr 4, 2017
1 parent 85d24b5 commit bda6814
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 163 deletions.
3 changes: 3 additions & 0 deletions QtCollider/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ namespace QtCollider {
QC_DO_SYMBOL(prRelease); \
QC_DO_SYMBOL(Rect); \
QC_DO_SYMBOL(Point); \
QC_DO_SYMBOL(Int8Array); \
QC_DO_SYMBOL(Color); \
QC_DO_SYMBOL(Size); \
QC_DO_SYMBOL(QPalette); \
QC_DO_SYMBOL(Font); \
QC_DO_SYMBOL(QCallback); \
QC_DO_SYMBOL(WebPage); \
QC_DO_SYMBOL(QObject); \
QC_DO_SYMBOL(Layout); \
QC_DO_SYMBOL(TreeViewItem); \
Expand Down
1 change: 1 addition & 0 deletions QtCollider/factories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static void doLoadFactories ()
QC_ADD_FACTORY( QcWaveform );
QC_ADD_FACTORY( QcTextEdit );
QC_ADD_FACTORY( QcTreeWidget );
QC_ADD_FACTORY( QcCallback );
QC_ADD_FACTORY( WebView );
QC_ADD_FACTORY( QcWindow );
QC_ADD_FACTORY( QcScrollWindow );
Expand Down
13 changes: 11 additions & 2 deletions QtCollider/metatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
#include "metatype.hpp"
#include "type_codec.hpp"
#include "Common.h" // Make sure PyrObject* is declared to QMetaType

#include <PyrKernel.h>

#include "widgets/QcWebView.h"

namespace QtCollider {

static QVector<MetaType*> gMetaTypes;
Expand All @@ -46,7 +47,7 @@ void MetaType::initAll()
qRegisterMetaType< QVector<int> >();
qRegisterMetaType<SharedImage>();

gMetaTypes.reserve(20);
gMetaTypes.reserve(30);

qc_init_metatype<bool>();
qc_init_metatype<int>();
Expand All @@ -72,6 +73,8 @@ void MetaType::initAll()
qc_init_metatype< QVector<double> >();
qc_init_metatype< QVector<int> >();
qc_init_metatype< QVariantList >();
qc_init_metatype<QUrl>();
qc_init_metatype<QcCallback*>();
}

MetaType *MetaType::find( PyrSlot *slot )
Expand Down Expand Up @@ -124,6 +127,12 @@ MetaType *MetaType::find( PyrSlot *slot )
else if( isKindOfSlot( slot, SC_CLASS(QPalette) ) ) {
return metaType<QPalette>();
}
else if( isKindOfSlot( slot, SC_CLASS(QCallback) ) ) {
return metaType<QcCallback*>();
}
else if( isKindOfSlot( slot, SC_CLASS(WebPage) ) ) {
return metaType<WebPage*>();
}
else if( isKindOfSlot( slot, SC_CLASS(QObject) ) ) {
return metaType<QObjectProxy*>();
}
Expand Down
2 changes: 0 additions & 2 deletions QtCollider/metatype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

namespace QtCollider {

template <typename T> struct TypeCodec;

struct MetaType
{
public:
Expand Down
19 changes: 19 additions & 0 deletions QtCollider/primitives/prim_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../type_codec.hpp"
#include "../QcApplication.h"
#include "../QObjectProxy.h"
#include "../widgets/QcWebView.h"
#include "../style/style.hpp"
#include "QtCollider.h"

Expand All @@ -34,6 +35,7 @@

#include <PyrKernel.h>

#include <QDesktopServices>
#include <QFontDatabase>
#include <QFontInfo>
#include <QFontMetrics>
Expand Down Expand Up @@ -281,6 +283,22 @@ QC_LANG_PRIMITIVE( Qt_CursorPosition, 0, PyrSlot *r, PyrSlot *a, VMGlobals *g )
return errNone;
}

QC_LANG_PRIMITIVE( Qt_SetUrlHandler, 2, PyrSlot *r, PyrSlot *a, VMGlobals *g )
{
if( !QcApplication::compareThread() ) return QtCollider::wrongThreadError();

QString str = QtCollider::get( a );

if (IsNil(a+1)) {
QDesktopServices::unsetUrlHandler(str);
} else {
QcCallback* cb = QtCollider::get( a+1 );
QDesktopServices::setUrlHandler(str, cb, "onCalled");
}

return errNone;
}

void defineMiscPrimitives()
{
LangPrimitiveDefiner definer;
Expand All @@ -300,6 +318,7 @@ void defineMiscPrimitives()
definer.define<Qt_IsMethodOverridden>();
definer.define<QWebView_ClearMemoryCaches>();
definer.define<Qt_CursorPosition>();
definer.define<Qt_SetUrlHandler>();
}

} // namespace QtCollider
21 changes: 21 additions & 0 deletions QtCollider/type_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ void TypeCodec<QString>::write( PyrSlot *slot, const QString & val )
}


QUrl TypeCodec<QUrl>::read( PyrSlot *slot )
{
if( IsSym(slot) ) {
return QUrl(QString::fromUtf8( slotRawSymbol(slot)->name ));
}
else if( isKindOfSlot( slot, class_string ) ) {
int len = slotRawObject( slot )->size;
return QUrl(QString::fromUtf8( slotRawString(slot)->s, len ));
} else {
return QUrl();
}
}


void TypeCodec<QUrl>::write( PyrSlot *slot, const QUrl & val )
{
PyrString *str = newPyrString( gMainVMGlobals->gc,
val.toString().toUtf8().constData(), 0, true );
SetObject( slot, str );
}

QPointF TypeCodec<QPointF>::read( PyrSlot *slot )
{
PyrSlot *slots = slotRawObject( slot )->slots;
Expand Down
43 changes: 42 additions & 1 deletion QtCollider/type_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@
#include <QPalette>
#include <QWidget>
#include <QVector>
#include <QUrl>
#include <QVariantList>

class QObjectProxy;

namespace QtCollider {

template <typename T> struct TypeCodec { };
template <typename T, typename EnabledT=void> struct TypeCodec { };

// Forwarding from QtCollider namespace to TypeCodec

Expand Down Expand Up @@ -211,6 +212,18 @@ template <> struct TypeCodec<QString>
static void write( PyrSlot *slot, const QString & val );
};

template <> struct TypeCodec<QUrl>
{
static QUrl read( PyrSlot *slot );

static QUrl safeRead( PyrSlot *slot )
{
return read(slot);
}

static void write( PyrSlot *slot, const QUrl & val );
};

template <> struct TypeCodec<QPointF>
{
static QPointF read( PyrSlot *slot );
Expand Down Expand Up @@ -425,6 +438,34 @@ template <> struct TypeCodec<QVariantList>
static void write( PyrSlot *slot, const QVariantList & );
};

template<typename QObjectT>
struct TypeCodec<QObjectT, void>
{
static QObjectT read( PyrSlot *slot )
{
return safeRead(slot);
}

static QObjectT safeRead( PyrSlot *slot )
{
QObjectProxy* proxy = TypeCodec<QObjectProxy*>::safeRead(slot);

if (proxy) {
QObjectT action = qobject_cast<QObjectT>(proxy->object());
return action;
} else {
return 0;
}
}

static void write(PyrSlot * slot, QObjectT object)
{
QObject* qobject = qobject_cast<QObject*>(object);
TypeCodec<QObject*>::write(slot, qobject);
}

};

} // namespace QtCollider

#endif // QT_COLLIDER_TYPE_CODEC_INCLUDED
Loading

0 comments on commit bda6814

Please sign in to comment.