From f3a449f519ec354fd304b3717293d4ec30c30700 Mon Sep 17 00:00:00 2001 From: Yimgo Date: Mon, 6 Aug 2012 10:12:12 +0200 Subject: [PATCH] Cosmetics in UBGrahicsWebView and UBGraphicsWidgetItem classes. --- src/domain/UBGraphicsWebView.cpp | 38 +- src/domain/UBGraphicsWebView.h | 3 - src/domain/UBGraphicsWidgetItem.cpp | 1019 +++++++++++++-------------- src/domain/UBGraphicsWidgetItem.h | 277 +++----- 4 files changed, 591 insertions(+), 746 deletions(-) diff --git a/src/domain/UBGraphicsWebView.cpp b/src/domain/UBGraphicsWebView.cpp index de9da3e7..39c4f931 100644 --- a/src/domain/UBGraphicsWebView.cpp +++ b/src/domain/UBGraphicsWebView.cpp @@ -13,20 +13,18 @@ * along with this program. If not, see . */ -#include "UBGraphicsWebView.h" - #include #include +#include "UBGraphicsWebView.h" #include "UBGraphicsScene.h" #include "UBGraphicsItemDelegate.h" - #include "UBGraphicsDelegateFrame.h" #include "core/memcheck.h" -UBGraphicsWebView::UBGraphicsWebView(QGraphicsItem* parent) : - QGraphicsWebView(parent) +UBGraphicsWebView::UBGraphicsWebView(QGraphicsItem* parent) + : QGraphicsWebView(parent) { setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object); @@ -68,30 +66,17 @@ void UBGraphicsWebView::setUuid(const QUuid &pUuid) void UBGraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (mDelegate->mousePressEvent(event)) - { - //NOOP - } - else - { - // QT Proxy Widget is a bit lazy, we force the selection ... + if (!mDelegate->mousePressEvent(event)) + setSelected(true); /* forcing selection */ - setSelected(true); - } QGraphicsWebView::mousePressEvent(event); } void UBGraphicsWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if (mDelegate->mouseMoveEvent(event)) - { - // NOOP; - } - else - { + if (!mDelegate->mouseMoveEvent(event)) QGraphicsWebView::mouseMoveEvent(event); - } } @@ -103,7 +88,7 @@ void UBGraphicsWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void UBGraphicsWebView::wheelEvent(QGraphicsSceneWheelEvent *event) { - if( mDelegate->weelEvent(event) ) + if (mDelegate->weelEvent(event)) { QGraphicsWebView::wheelEvent(event); event->accept(); @@ -113,20 +98,18 @@ void UBGraphicsWebView::wheelEvent(QGraphicsSceneWheelEvent *event) void UBGraphicsWebView::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event) -// NOOP + /* NOOP */ } void UBGraphicsWebView::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event) -// NOOP + /* NOOP */ } void UBGraphicsWebView::setDelegate(UBGraphicsItemDelegate* pDelegate) { if (mDelegate) - { delete mDelegate; - } mDelegate = pDelegate; } @@ -140,8 +123,7 @@ void UBGraphicsWebView::resize(qreal w, qreal h) void UBGraphicsWebView::resize(const QSizeF & pSize) { - if (pSize != size()) - { + if (pSize != size()) { QGraphicsWebView::setMaximumSize(pSize.width(), pSize.height()); QGraphicsWebView::resize(pSize.width(), pSize.height()); if (mDelegate) diff --git a/src/domain/UBGraphicsWebView.h b/src/domain/UBGraphicsWebView.h index 508f60bb..988c4cbd 100644 --- a/src/domain/UBGraphicsWebView.h +++ b/src/domain/UBGraphicsWebView.h @@ -19,7 +19,6 @@ #include #include - #include "UBItem.h" #include "UBResizableGraphicsItem.h" @@ -56,8 +55,6 @@ class UBGraphicsWebView: public QGraphicsWebView, public UBItem, public UBResiza virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); virtual bool event(QEvent *event); - - virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); }; diff --git a/src/domain/UBGraphicsWidgetItem.cpp b/src/domain/UBGraphicsWidgetItem.cpp index 55307c18..2944c4b5 100644 --- a/src/domain/UBGraphicsWidgetItem.cpp +++ b/src/domain/UBGraphicsWidgetItem.cpp @@ -13,55 +13,50 @@ * along with this program. If not, see . */ -#include "UBGraphicsWidgetItem.h" #include #include -#include "api/UBWidgetUniboardAPI.h" -#include "api/UBW3CWidgetAPI.h" - +#include "UBGraphicsWidgetItem.h" +#include "UBGraphicsScene.h" #include "UBGraphicsItemDelegate.h" #include "UBGraphicsWidgetItemDelegate.h" #include "UBGraphicsDelegateFrame.h" -#include "UBGraphicsScene.h" -#include "frameworks/UBFileSystemUtils.h" -#include "web/UBWebPage.h" -#include "network/UBNetworkAccessManager.h" +#include "api/UBWidgetUniboardAPI.h" +#include "api/UBW3CWidgetAPI.h" + + #include "board/UBBoardController.h" + #include "core/memcheck.h" #include "core/UBApplicationController.h" #include "core/UBApplication.h" #include "core/UBSettings.h" -#include "web/UBWebKitUtils.h" -#include "web/UBWebController.h" + +#include "frameworks/UBFileSystemUtils.h" #include "frameworks/UBPlatformUtils.h" -#include "board/UBBoardController.h" +#include "network/UBNetworkAccessManager.h" -QStringList UBGraphicsWidgetItem::sInlineJavaScripts; -bool UBGraphicsWidgetItem::sInlineJavaScriptLoaded = false; +#include "web/UBWebPage.h" +#include "web/UBWebKitUtils.h" +#include "web/UBWebController.h" -bool UBGraphicsW3CWidgetItem::sTemplateLoaded = false; -QMap UBGraphicsW3CWidgetItem::sNPAPIWrapperTemplates; -QString UBGraphicsW3CWidgetItem::sNPAPIWrappperConfigTemplate; +bool UBGraphicsWidgetItem::sInlineJavaScriptLoaded = false; +QStringList UBGraphicsWidgetItem::sInlineJavaScripts; UBGraphicsWidgetItem::UBGraphicsWidgetItem(QGraphicsItem *parent, int widgetType) : UBGraphicsWebView(parent) - , mIsResizable(false) , mInitialLoadDone(false) - , mLoadIsErronous(false) , mIsFreezable(true) + , mIsResizable(false) + , mLoadIsErronous(false) , mCanBeContent(0) , mCanBeTool(0) - , mShouldMoveWidget(false) - , mUniboardAPI(0) , mIsFrozen(false) , mIsTakingSnapshot(false) + , mShouldMoveWidget(false) + , mUniboardAPI(0) { - setAcceptDrops(true); - UBGraphicsWidgetItemDelegate* delegate = new UBGraphicsWidgetItemDelegate(this, widgetType); - delegate->init(); - setDelegate(delegate); setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly QGraphicsWebView::setPage(new UBWebPage(this)); @@ -75,6 +70,7 @@ UBGraphicsWidgetItem::UBGraphicsWidgetItem(QGraphicsItem *parent, int widgetType page()->setNetworkAccessManager(UBNetworkAccessManager::defaultAccessManager()); + setAcceptDrops(true); setAutoFillBackground(false); QPalette pagePalette = page()->palette(); @@ -86,12 +82,71 @@ UBGraphicsWidgetItem::UBGraphicsWidgetItem(QGraphicsItem *parent, int widgetType pagePalette.setBrush(QPalette::Base, QBrush(Qt::transparent)); viewPalette.setBrush(QPalette::Window, QBrush(Qt::transparent)); setPalette(viewPalette); + + UBGraphicsWidgetItemDelegate* delegate = new UBGraphicsWidgetItemDelegate(this, widgetType); + delegate->init(); + setDelegate(delegate); } UBGraphicsWidgetItem::~UBGraphicsWidgetItem() { - // NOOP + /* NOOP */ +} + +void UBGraphicsWidgetItem::initialize() +{ + installEventFilter(this); + UBGraphicsWebView::setMinimumSize(nominalSize()); + setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); // Necessary to set if we want z value to be assigned correctly + + if (mDelegate && mDelegate->frame() && resizable()) + mDelegate->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing); + + QPalette palette = page()->palette(); + palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); + page()->setPalette(palette); + + connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); + connect(page(), SIGNAL(geometryChangeRequested(const QRect&)), this, SLOT(geometryChangeRequested(const QRect&))); + connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(mainFrameLoadFinished (bool))); +} + +QUrl UBGraphicsWidgetItem::mainHtml() +{ + return mMainHtmlUrl; +} + +void UBGraphicsWidgetItem::loadMainHtml() +{ + load(mMainHtmlUrl); +} + +QUrl UBGraphicsWidgetItem::widgetUrl() +{ + return mWidgetUrl; +} +QString UBGraphicsWidgetItem::mainHtmlFileName() +{ + return mMainHtmlFileName; +} + +bool UBGraphicsWidgetItem::hasEmbededObjects() +{ + if (page()->mainFrame()) { + QList htmlObjects = UBWebKitUtils::objectsInFrame(page()->mainFrame()); + return htmlObjects.length() > 0; + } + + return false; +} + +bool UBGraphicsWidgetItem::hasEmbededFlash() +{ + if (hasEmbededObjects()) + return page()->mainFrame()->toHtml().contains("application/x-shockwave-flash"); + else + return false; } bool UBGraphicsWidgetItem::canBeContent() @@ -130,124 +185,244 @@ bool UBGraphicsWidgetItem::canBeTool() #endif } -void UBGraphicsWidgetItem::loadMainHtml() +QString UBGraphicsWidgetItem::preference(const QString& key) const { - load(mMainHtmlUrl); + return mPreferences.value(key); } -void UBGraphicsWidgetItem::mainFrameLoadFinished (bool ok) +void UBGraphicsWidgetItem::setPreference(const QString& key, QString value) { - mInitialLoadDone = true; - mLoadIsErronous = !ok; + if (key == "" || (mPreferences.contains(key) && mPreferences.value(key) == value)) + return; - update(boundingRect()); + mPreferences.insert(key, value); + if (scene()) + scene()->setModified(true); } -bool UBGraphicsWidgetItem::hasEmbededObjects() +QMap UBGraphicsWidgetItem::preferences() const { - if (page()->mainFrame()) { - QList htmlObjects = UBWebKitUtils::objectsInFrame(page()->mainFrame()); - return htmlObjects.length() > 0; - } + return mPreferences; +} - return false; + +void UBGraphicsWidgetItem::removePreference(const QString& key) +{ + mPreferences.remove(key); } -bool UBGraphicsWidgetItem::hasEmbededFlash() + +void UBGraphicsWidgetItem::removeAllPreferences() { - if (hasEmbededObjects()) - { - return page()->mainFrame()->toHtml().contains("application/x-shockwave-flash"); - } + mPreferences.clear(); +} + +QString UBGraphicsWidgetItem::datastoreEntry(const QString& key) const +{ + if (mDatastore.contains(key)) + return mDatastore.value(key); else - { - return false; - } + return QString(); } -QString UBGraphicsWidgetItem::iconFilePath(const QUrl& pUrl) +void UBGraphicsWidgetItem::setDatastoreEntry(const QString& key, QString value) { - // TODO UB 4.x read config.xml widget.icon param first + if (key == "" || (mDatastore.contains(key) && mDatastore.value(key) == value)) + return; - QStringList files; + mDatastore.insert(key, value); + if (scene()) + scene()->setModified(true); +} - files << "icon.svg"; // W3C widget default 1 - files << "icon.ico"; // W3C widget default 2 - files << "icon.png"; // W3C widget default 3 - files << "icon.gif"; // W3C widget default 4 +QMap UBGraphicsWidgetItem::datastoreEntries() const +{ + return mDatastore; +} - files << "Icon.png"; // Apple widget default - QString file = UBFileSystemUtils::getFirstExistingFileFromList(pUrl.toLocalFile(), files); +void UBGraphicsWidgetItem::removeDatastoreEntry(const QString& key) +{ + mDatastore.remove(key); +} - // default - if (file.length() == 0) - { - file = QString(":/images/defaultWidgetIcon.png"); - } - return file; +void UBGraphicsWidgetItem::removeAllDatastoreEntries() +{ + mDatastore.clear(); +} + +UBGraphicsItemDelegate* UBGraphicsWidgetItem::Delegate() const +{ + return mDelegate; +} + +void UBGraphicsWidgetItem::remove() +{ + if (mDelegate) + mDelegate->remove(); +} + +void UBGraphicsWidgetItem::removeScript() +{ + if (page() && page()->mainFrame()) + page()->mainFrame()->evaluateJavaScript("if(widget && widget.onremove) { widget.onremove();}"); +} + +void UBGraphicsWidgetItem::processDropEvent(QDropEvent *event) +{ + return mUniboardAPI->ProcessDropEvent(event); +} +bool UBGraphicsWidgetItem::isDropableData(const QMimeData *data) const +{ + return mUniboardAPI->isDropableData(data); +} + +QUrl UBGraphicsWidgetItem::getOwnFolder() const { + return ownFolder; +} + +void UBGraphicsWidgetItem::setOwnFolder(const QUrl &newFolder) +{ + ownFolder = newFolder; +} + +void UBGraphicsWidgetItem::setSnapshotPath(const QUrl &newFilePath) +{ + SnapshotFile = newFilePath; +} + +QUrl UBGraphicsWidgetItem::getSnapshotPath() +{ + return SnapshotFile; +} + +void UBGraphicsWidgetItem::clearSource() +{ + UBFileSystemUtils::deleteDir(getOwnFolder().toLocalFile()); + UBFileSystemUtils::deleteFile(getSnapshotPath().toLocalFile()); +} + +void UBGraphicsWidgetItem::setUuid(const QUuid &pUuid) +{ + UBItem::setUuid(pUuid); + setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene +} + +QSize UBGraphicsWidgetItem::nominalSize() const +{ + return mNominalSize; +} + +bool UBGraphicsWidgetItem::hasLoadedSuccessfully() const +{ + return (mInitialLoadDone && !mLoadIsErronous); +} + +bool UBGraphicsWidgetItem::freezable() +{ + return mIsFreezable; +} + +bool UBGraphicsWidgetItem::resizable() +{ + return mIsResizable; +} + +bool UBGraphicsWidgetItem::isFrozen() +{ + return mIsFrozen; +} + +QPixmap UBGraphicsWidgetItem::snapshot() +{ + return mSnapshot; +} + +QPixmap UBGraphicsWidgetItem::takeSnapshot() +{ + mIsTakingSnapshot = true; + + QPixmap pixmap(size().toSize()); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + + QStyleOptionGraphicsItem options; + paint(&painter, &options); + + mIsTakingSnapshot = false; + + return pixmap; +} + +void UBGraphicsWidgetItem::setSnapshot(const QPixmap& pix) +{ + mSnapshot = pix; +} + +UBGraphicsScene* UBGraphicsWidgetItem::scene() +{ + return qobject_cast(QGraphicsItem::scene()); +} + +int UBGraphicsWidgetItem::widgetType(const QUrl& pUrl) +{ + QString mime = UBFileSystemUtils::mimeTypeFromFileName(pUrl.toString()); + + if (mime == "application/vnd.apple-widget") + return UBWidgetType::Apple; + else if (mime == "application/widget") + return UBWidgetType::W3C; + else + return UBWidgetType::Other; } QString UBGraphicsWidgetItem::widgetName(const QUrl& widgetPath) { QString name; QString version; - QFile w3CConfigFile(widgetPath.toLocalFile() + "/config.xml"); QFile appleConfigFile(widgetPath.toLocalFile() + "/Info.plist"); - if (w3CConfigFile.exists() && w3CConfigFile.open(QFile::ReadOnly)) - { + if (w3CConfigFile.exists() && w3CConfigFile.open(QFile::ReadOnly)) { QDomDocument doc; doc.setContent(w3CConfigFile.readAll()); QDomElement root = doc.firstChildElement("widget"); - if (!root.isNull()) - { + if (!root.isNull()) { QDomElement nameElement = root.firstChildElement("name"); if (!nameElement.isNull()) name = nameElement.text(); - version = root.attribute("version", ""); } - w3CConfigFile.close(); } - else if (appleConfigFile.exists() && appleConfigFile.open(QFile::ReadOnly)) - { + else if (appleConfigFile.exists() && appleConfigFile.open(QFile::ReadOnly)) { QDomDocument doc; doc.setContent(appleConfigFile.readAll()); QDomElement root = doc.firstChildElement("plist"); - if (!root.isNull()) - { + if (!root.isNull()) { QDomElement dictElement = root.firstChildElement("dict"); - if (!dictElement.isNull()) - { + if (!dictElement.isNull()) { QDomNodeList childNodes = dictElement.childNodes(); - // looking for something like - // .. - // CFBundleDisplayName - // brain scans - // .. + /* looking for something like + * .. + * CFBundleDisplayName + * brain scans + * .. + */ - for(int i = 0; i < childNodes.count() - 1; i++) - { - if (childNodes.at(i).isElement()) - { + for(int i = 0; i < childNodes.count() - 1; i++) { + if (childNodes.at(i).isElement()) { QDomElement elKey = childNodes.at(i).toElement(); - if (elKey.text() == "CFBundleDisplayName") - { - if (childNodes.at(i + 1).isElement()) - { + if (elKey.text() == "CFBundleDisplayName") { + if (childNodes.at(i + 1).isElement()) { QDomElement elValue = childNodes.at(i + 1).toElement(); name = elValue.text(); } } - else if (elKey.text() == "CFBundleShortVersionString") - { - if (childNodes.at(i + 1).isElement()) - { + else if (elKey.text() == "CFBundleShortVersionString") { + if (childNodes.at(i + 1).isElement()) { QDomElement elValue = childNodes.at(i + 1).toElement(); version = elValue.text(); } @@ -256,114 +431,39 @@ QString UBGraphicsWidgetItem::widgetName(const QUrl& widgetPath) } } } - appleConfigFile.close(); } - QString result; - if (name.length() > 0) - { + if (name.length() > 0) { result = name; - if (version.length() > 0) - { + if (version.length() > 0) { result += " "; result += version; } } - return result; } -int UBGraphicsWidgetItem::widgetType(const QUrl& pUrl) +QString UBGraphicsWidgetItem::iconFilePath(const QUrl& pUrl) { - QString mime = UBFileSystemUtils::mimeTypeFromFileName(pUrl.toString()); + /* TODO UB 4.x read config.xml widget.icon param first */ - if (mime == "application/vnd.apple-widget") - { - return UBWidgetType::Apple; - } - else if (mime == "application/widget") - { - return UBWidgetType::W3C; - } - else - { - return UBWidgetType::Other; - } -} - -void UBGraphicsWidgetItem::injectInlineJavaScript() -{ - if (!sInlineJavaScriptLoaded) - { - sInlineJavaScripts = UBApplication::applicationController->widgetInlineJavaScripts(); - sInlineJavaScriptLoaded = true; - } + QStringList files; - foreach(QString script, sInlineJavaScripts) - { - page()->mainFrame()->evaluateJavaScript(script); - } -} + files << "icon.svg"; /* W3C widget default 1 */ + files << "icon.ico"; /* W3C widget default 2 */ + files << "icon.png"; /* W3C widget default 3 */ + files << "icon.gif"; /* W3C widget default 4 */ + files << "Icon.png"; /* Apple widget default */ -void UBGraphicsWidgetItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -{ - if (mIsFrozen) - { - painter->drawPixmap(0, 0, mSnapshot); - } - else if(mIsTakingSnapshot || (mInitialLoadDone && !mLoadIsErronous)) - { - QGraphicsWebView::paint(painter, option, widget); - } - else + QString file = UBFileSystemUtils::getFirstExistingFileFromList(pUrl.toLocalFile(), files); + /* default */ + if (file.length() == 0) { - QString message = tr("Loading ..."); - - // this is the right way of doing but we receive two callback and the one return always that the - // load as failed... to check -// if (mLoadIsErronous) -// message = tr("Cannot load content"); -// else -// message = tr("Loading ..."); - - painter->setFont(QFont("Arial", 12)); - - QFontMetrics fm = painter->fontMetrics(); - QRect txtBoundingRect = fm.boundingRect(message); - - txtBoundingRect.moveCenter(rect().center().toPoint()); - txtBoundingRect.adjust(-10, -5, 10, 5); - - painter->setPen(Qt::NoPen); - painter->setBrush(UBSettings::paletteColor); - painter->drawRoundedRect(txtBoundingRect, 3, 3); - - painter->setPen(Qt::white); - painter->drawText(rect(), Qt::AlignCenter, message); + file = QString(":/images/defaultWidgetIcon.png"); } -} - -QPixmap UBGraphicsWidgetItem::takeSnapshot() -{ - mIsTakingSnapshot = true; - - QPixmap pixmap(size().toSize()); - pixmap.fill(Qt::transparent); - QPainter painter(&pixmap); - - QStyleOptionGraphicsItem options; - paint(&painter, &options); - - mIsTakingSnapshot = false; - - return pixmap; -} - -void UBGraphicsWidgetItem::setSnapshot(const QPixmap& pix) -{ - mSnapshot = pix; + return file; } void UBGraphicsWidgetItem::freeze() @@ -373,29 +473,11 @@ void UBGraphicsWidgetItem::freeze() setSnapshot(pix); } - void UBGraphicsWidgetItem::unFreeze() { mIsFrozen = false; } -void UBGraphicsWidgetItem::javaScriptWindowObjectCleared() -{ - injectInlineJavaScript(); - - if(!mUniboardAPI) - mUniboardAPI = new UBWidgetUniboardAPI(scene(), this); - - page()->mainFrame()->addToJavaScriptWindowObject("sankore", mUniboardAPI); - -} - -void UBGraphicsWidgetItem::setUuid(const QUuid &pUuid) -{ - UBItem::setUuid(pUuid); - setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene -} - void UBGraphicsWidgetItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { UBGraphicsWebView::mousePressEvent(event); @@ -412,217 +494,141 @@ void UBGraphicsWidgetItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { mShouldMoveWidget = false; - UBGraphicsWebView::mouseReleaseEvent(event); -} - -void UBGraphicsWidgetItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) -{ - sendJSEnterEvent(); - mDelegate->hoverEnterEvent(event); - UBGraphicsWebView::hoverEnterEvent(event); -} -void UBGraphicsWidgetItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) -{ - sendJSLeaveEvent(); - mDelegate->hoverLeaveEvent(event); - UBGraphicsWebView::hoverLeaveEvent(event); -} -void UBGraphicsWidgetItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) -{ - UBGraphicsWebView::hoverMoveEvent(event); -} - -bool UBGraphicsWidgetItem::eventFilter(QObject *obj, QEvent *event) -{ - if (mShouldMoveWidget && obj == this && event->type() == QEvent::MouseMove) - { - QMouseEvent *mouseMoveEvent = static_cast(event); - - if (mouseMoveEvent->buttons() & Qt::LeftButton) - { - QPointF scenePos = mapToScene(mouseMoveEvent->pos()); - - QPointF newPos = pos() + scenePos - mLastMousePos; - - setPos(newPos); - - mLastMousePos = scenePos; - - event->accept(); - - return true; - } - } - - //standard event processing - return QObject::eventFilter(obj, event); -} - - - -void UBGraphicsWidgetItem::geometryChangeRequested(const QRect& geom) -{ - resize(geom.width(), geom.height()); -} - - -void UBGraphicsWidgetItem::initialize() -{ - connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); - - QPalette palette = page()->palette(); - palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); - page()->setPalette(palette); - - installEventFilter(this); - - UBGraphicsWebView::setMinimumSize(nominalSize()); - - connect(page(), SIGNAL(geometryChangeRequested(const QRect&)), this, SLOT(geometryChangeRequested(const QRect&))); - connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(mainFrameLoadFinished (bool))); - - if (mDelegate && mDelegate->frame() && resizable()) - mDelegate->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing); - - setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly -} - - -UBGraphicsScene* UBGraphicsWidgetItem::scene() -{ - return qobject_cast(QGraphicsItem::scene()); -} - -void UBGraphicsWidgetItem::setPreference(const QString& key, QString value) -{ - if (key == "" || (mPreferences.contains(key) && mPreferences.value(key) == value)) - return; - - mPreferences.insert(key, value); - if (scene()) - scene()->setModified(true); -} - - -QString UBGraphicsWidgetItem::preference(const QString& key) const -{ - return mPreferences.value(key); -} - - -QMap UBGraphicsWidgetItem::preferences() const -{ - return mPreferences; -} - - -void UBGraphicsWidgetItem::removePreference(const QString& key) -{ - mPreferences.remove(key); -} - - -void UBGraphicsWidgetItem::removeAllPreferences() -{ - mPreferences.clear(); -} - - -void UBGraphicsWidgetItem::setDatastoreEntry(const QString& key, QString value) -{ - if (key == "" || (mDatastore.contains(key) && mDatastore.value(key) == value)) - return; - - mDatastore.insert(key, value); - if (scene()) - scene()->setModified(true); -} - + UBGraphicsWebView::mouseReleaseEvent(event); +} -QString UBGraphicsWidgetItem::datastoreEntry(const QString& key) const +void UBGraphicsWidgetItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { - if (mDatastore.contains(key)) - return mDatastore.value(key); - else - return ""; + sendJSEnterEvent(); + mDelegate->hoverEnterEvent(event); + UBGraphicsWebView::hoverEnterEvent(event); } - - -QMap UBGraphicsWidgetItem::datastoreEntries() const +void UBGraphicsWidgetItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { - return mDatastore; + sendJSLeaveEvent(); + mDelegate->hoverLeaveEvent(event); + UBGraphicsWebView::hoverLeaveEvent(event); } - - -void UBGraphicsWidgetItem::removeDatastoreEntry(const QString& key) +void UBGraphicsWidgetItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { - mDatastore.remove(key); + UBGraphicsWebView::hoverMoveEvent(event); } - -void UBGraphicsWidgetItem::removeAllDatastoreEntries() +bool UBGraphicsWidgetItem::eventFilter(QObject *obj, QEvent *event) { - mDatastore.clear(); -} + if (mShouldMoveWidget && obj == this && event->type() == QEvent::MouseMove) { + QMouseEvent *mouseMoveEvent = static_cast(event); + + if (mouseMoveEvent->buttons() & Qt::LeftButton) { + QPointF scenePos = mapToScene(mouseMoveEvent->pos()); + QPointF newPos = pos() + scenePos - mLastMousePos; -void UBGraphicsWidgetItem::remove() -{ + setPos(newPos); - if (mDelegate) - mDelegate->remove(); + mLastMousePos = scenePos; -} + event->accept(); -void UBGraphicsWidgetItem::removeScript() -{ - if (page() && page()->mainFrame()) - { - page()->mainFrame()->evaluateJavaScript("if(widget && widget.onremove) { widget.onremove();}"); + return true; + } } + + /* standard event processing */ + return QObject::eventFilter(obj, event); } + void UBGraphicsWidgetItem::sendJSEnterEvent() { if (page() && page()->mainFrame()) - { page()->mainFrame()->evaluateJavaScript("if(widget && widget.onenter) { widget.onenter();}"); - } } + void UBGraphicsWidgetItem::sendJSLeaveEvent() { if (page() && page()->mainFrame()) - { page()->mainFrame()->evaluateJavaScript("if(widget && widget.onleave) { widget.onleave();}"); +} + +void UBGraphicsWidgetItem::injectInlineJavaScript() +{ + if (!sInlineJavaScriptLoaded) { + sInlineJavaScripts = UBApplication::applicationController->widgetInlineJavaScripts(); + sInlineJavaScriptLoaded = true; + } + + foreach(QString script, sInlineJavaScripts) + page()->mainFrame()->evaluateJavaScript(script); +} + +void UBGraphicsWidgetItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + if (mIsFrozen) + painter->drawPixmap(0, 0, mSnapshot); + else if(mIsTakingSnapshot || (mInitialLoadDone && !mLoadIsErronous)) + QGraphicsWebView::paint(painter, option, widget); + else { + QString message = tr("Loading ..."); + + /* this is the right way of doing but we receive two callback and the one return always that the + * load as failed... to check + if (mLoadIsErronous) + message = tr("Cannot load content"); + else + message = tr("Loading ..."); + */ + + painter->setFont(QFont("Arial", 12)); + + QFontMetrics fm = painter->fontMetrics(); + QRect txtBoundingRect = fm.boundingRect(message); + + txtBoundingRect.moveCenter(rect().center().toPoint()); + txtBoundingRect.adjust(-10, -5, 10, 5); + + painter->setPen(Qt::NoPen); + painter->setBrush(UBSettings::paletteColor); + painter->drawRoundedRect(txtBoundingRect, 3, 3); + + painter->setPen(Qt::white); + painter->drawText(rect(), Qt::AlignCenter, message); } } -void UBGraphicsWidgetItem::clearSource() +void UBGraphicsWidgetItem::geometryChangeRequested(const QRect& geom) { - UBFileSystemUtils::deleteDir(getOwnFolder().toLocalFile()); - UBFileSystemUtils::deleteFile(getSnapshotPath().toLocalFile()); + resize(geom.width(), geom.height()); } -void UBGraphicsWidgetItem::processDropEvent(QDropEvent *event) +void UBGraphicsWidgetItem::javaScriptWindowObjectCleared() { - return mUniboardAPI->ProcessDropEvent(event); + injectInlineJavaScript(); + + if(!mUniboardAPI) + mUniboardAPI = new UBWidgetUniboardAPI(scene(), this); + + page()->mainFrame()->addToJavaScriptWindowObject("sankore", mUniboardAPI); + } -bool UBGraphicsWidgetItem::isDropableData(const QMimeData *data) const + +void UBGraphicsWidgetItem::mainFrameLoadFinished (bool ok) { - return mUniboardAPI->isDropableData(data); + mInitialLoadDone = true; + mLoadIsErronous = !ok; + update(boundingRect()); } + + UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent) : UBGraphicsWidgetItem(parent) { QString path = pWidgetUrl.toLocalFile(); - if (!path.endsWith(".wdgt") && !path.endsWith(".wdgt/")) - { + if (!path.endsWith(".wdgt") && !path.endsWith(".wdgt/")) { int lastSlashIndex = path.lastIndexOf("/"); if (lastSlashIndex > 0) - { path = path.mid(0, lastSlashIndex + 1); - } } QFile plistFile(path + "/Info.plist"); @@ -636,9 +642,7 @@ UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGr int mainHtmlIndexEnd = plist.indexOf("", mainHtmlIndexStart); if (mainHtmlIndex > -1 && mainHtmlIndexStart > -1 && mainHtmlIndexEnd > -1) - { mMainHtmlFileName = plist.mid(mainHtmlIndexStart + 8, mainHtmlIndexEnd - mainHtmlIndexStart - 8); - } mMainHtmlUrl = pWidgetUrl; mMainHtmlUrl.setPath(pWidgetUrl.path() + "/" + mMainHtmlFileName); @@ -657,34 +661,42 @@ UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGr UBGraphicsAppleWidgetItem::~UBGraphicsAppleWidgetItem() { - // NOOP + /* NOOP */ +} + +int UBGraphicsAppleWidgetItem::type() const +{ + return Type; } +void UBGraphicsAppleWidgetItem::setUuid(const QUuid &pUuid) +{ + UBItem::setUuid(pUuid); + setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene +} UBItem* UBGraphicsAppleWidgetItem::deepCopy() const { UBGraphicsAppleWidgetItem *appleWidget = new UBGraphicsAppleWidgetItem(QGraphicsWebView::url(), parentItem()); foreach(QString key, mPreferences.keys()) - { appleWidget->setPreference(key, mPreferences.value(key)); - } foreach(QString key, mDatastore.keys()) - { appleWidget->setDatastoreEntry(key, mDatastore.value(key)); - } appleWidget->setSourceUrl(this->sourceUrl()); return appleWidget; } -void UBGraphicsAppleWidgetItem::setUuid(const QUuid &pUuid) -{ - UBItem::setUuid(pUuid); - setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene -} + + + + +bool UBGraphicsW3CWidgetItem::sTemplateLoaded = false; +QString UBGraphicsW3CWidgetItem::sNPAPIWrappperConfigTemplate; +QMap UBGraphicsW3CWidgetItem::sNPAPIWrapperTemplates; UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent) : UBGraphicsWidgetItem(parent) @@ -693,16 +705,13 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi QString path = pWidgetUrl.toLocalFile(); QDir potentialDir(path); - if (!path.endsWith(".wgt") && !path.endsWith(".wgt/") && !potentialDir.exists()) - { + if (!path.endsWith(".wgt") && !path.endsWith(".wgt/") && !potentialDir.exists()) { int lastSlashIndex = path.lastIndexOf("/"); if (lastSlashIndex > 0) - { path = path.mid(0, lastSlashIndex + 1); - } } - if(!path.endsWith("/")) + if (!path.endsWith("/")) path += "/"; int width = 300; @@ -715,8 +724,7 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi doc.setContent(configFile.readAll()); QDomNodeList widgetDomList = doc.elementsByTagName("widget"); - if (widgetDomList.count() > 0) - { + if (widgetDomList.count() > 0) { QDomElement widgetElement = widgetDomList.item(0).toElement(); width = widgetElement.attribute("width", "300").toInt(); @@ -724,70 +732,52 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi mMetadatas.id = widgetElement.attribute("id", ""); - //some early widget (<= 4.3.4) where using identifier instead of id + /* some early widget (<= 4.3.4) where using identifier instead of id */ if (mMetadatas.id.length() == 0) mMetadatas.id = widgetElement.attribute("identifier", ""); mMetadatas.version = widgetElement.attribute("version", ""); - // TODO UB 4.x map properly ub namespace + /* TODO UB 4.x map properly ub namespace */ mIsResizable = widgetElement.attribute("ub:resizable", "false") == "true"; mIsFreezable = widgetElement.attribute("ub:freezable", "true") == "true"; QString roles = widgetElement.attribute("ub:roles", "content tool").trimmed().toLower(); - //------------------------------// + /* ------------------------------ */ - if( roles == "" || roles.contains("tool") ) - { + if (roles == "" || roles.contains("tool")) mCanBeTool = UBGraphicsWidgetItem::type_ALL; - } - if( roles.contains("twin") ) - { + if (roles.contains("twin")) mCanBeTool |= UBGraphicsWidgetItem::type_WIN; - } - if( roles.contains("tmac") ) - { + if (roles.contains("tmac")) mCanBeTool |= UBGraphicsWidgetItem::type_MAC; - } - if( roles.contains("tunix") ) - { + if (roles.contains("tunix")) mCanBeTool |= UBGraphicsWidgetItem::type_UNIX; - } - //---------// + /* --------- */ - if( roles == "" || roles.contains("content") ) - { + if (roles == "" || roles.contains("content")) mCanBeContent = UBGraphicsWidgetItem::type_ALL; - } - if( roles.contains("cwin") ) - { + if (roles.contains("cwin")) mCanBeContent |= UBGraphicsWidgetItem::type_WIN; - } - if( roles.contains("cmac") ) - { + if (roles.contains("cmac")) mCanBeContent |= UBGraphicsWidgetItem::type_MAC; - } - if( roles.contains("cunix") ) - { + if (roles.contains("cunix")) mCanBeContent |= UBGraphicsWidgetItem::type_UNIX; - } //------------------------------// QDomNodeList contentDomList = widgetElement.elementsByTagName("content"); - if (contentDomList.count() > 0) - { + if (contentDomList.count() > 0) { QDomElement contentElement = contentDomList.item(0).toElement(); - mMainHtmlFileName = contentElement.attribute("src", ""); } @@ -796,8 +786,7 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi QDomNodeList authorDomList = widgetElement.elementsByTagName("author"); - if (authorDomList.count() > 0) - { + if (authorDomList.count() > 0) { QDomElement authorElement = authorDomList.item(0).toElement(); mMetadatas.author = authorElement.text(); @@ -807,43 +796,35 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi QDomNodeList propertiesDomList = widgetElement.elementsByTagName("preference"); - /*for (uint i = 0; i < propertiesDomList.length(); i++) - { + for (uint i = 0; i < propertiesDomList.length(); i++) { QDomElement preferenceElement = propertiesDomList.at(i).toElement(); QString prefName = preferenceElement.attribute("name", ""); - if (prefName.length() > 0) - { + if (prefName.length() > 0) { QString prefValue = preferenceElement.attribute("value", ""); bool readOnly = (preferenceElement.attribute("readonly", "false") == "true"); mPreferences.insert(prefName, PreferenceValue(prefValue, readOnly)); } - }*/ + } } - if (mMainHtmlFileName.length() == 0) - { + if (mMainHtmlFileName.length() == 0) { QFile defaultStartFile(path + "index.htm"); if (defaultStartFile.exists()) - { mMainHtmlFileName = "index.htm"; - } - else - { + else { QFile secondDefaultStartFile(path + "index.html"); if (secondDefaultStartFile.exists()) - { mMainHtmlFileName = "index.html"; - } } } mMainHtmlUrl = pWidgetUrl; mMainHtmlUrl.setPath(pWidgetUrl.path() + "/" + mMainHtmlFileName); - // is it a valid local file ? + /* is it a valid local file ? */ QFile f(mMainHtmlUrl.toLocalFile()); if(!f.exists()) @@ -864,28 +845,12 @@ UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphi UBGraphicsW3CWidgetItem::~UBGraphicsW3CWidgetItem() { - // NOOP + /* NOOP */ } - -void UBGraphicsW3CWidgetItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) +int UBGraphicsW3CWidgetItem::type() const { - UBGraphicsScene::RenderingContext rc = UBGraphicsScene::Screen; - - if (scene()) - rc = scene()->renderingContext(); - - if ((!hasLoadedSuccessfully()) && (rc == UBGraphicsScene::NonScreen || rc == UBGraphicsScene::PdfExport)) - { - if (!snapshot().isNull()) - { - painter->drawPixmap(0, 0, snapshot()); - } - } - else - { - UBGraphicsWebView::paint(painter, option, widget); - } + return Type; } void UBGraphicsW3CWidgetItem::setUuid(const QUuid &pUuid) @@ -894,17 +859,6 @@ void UBGraphicsW3CWidgetItem::setUuid(const QUuid &pUuid) setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene } -void UBGraphicsW3CWidgetItem::javaScriptWindowObjectCleared() -{ - UBGraphicsWidgetItem::javaScriptWindowObjectCleared(); - - if(!mW3CWidgetAPI) - mW3CWidgetAPI = new UBW3CWidgetAPI(this); - - page()->mainFrame()->addToJavaScriptWindowObject("widget", mW3CWidgetAPI); - -} - UBItem* UBGraphicsW3CWidgetItem::deepCopy() const { UBGraphicsW3CWidgetItem *copy = new UBGraphicsW3CWidgetItem(QGraphicsWebView::url(), parentItem()); @@ -921,28 +875,40 @@ UBItem* UBGraphicsW3CWidgetItem::deepCopy() const copy->resize(this->size().width(), this->size().height()); foreach(QString key, UBGraphicsWidgetItem::preferences().keys()) - { copy->setPreference(key, UBGraphicsWidgetItem::preferences().value(key)); - } foreach(QString key, mDatastore.keys()) - { copy->setDatastoreEntry(key, mDatastore.value(key)); - } return copy; } -bool UBGraphicsW3CWidgetItem::hasNPAPIWrapper(const QString& pMimeType) +void UBGraphicsW3CWidgetItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { - loadNPAPIWrappersTemplates(); + UBGraphicsScene::RenderingContext rc = UBGraphicsScene::Screen; - return sNPAPIWrapperTemplates.contains(pMimeType); + if (scene()) + rc = scene()->renderingContext(); + + if ((!hasLoadedSuccessfully()) && (rc == UBGraphicsScene::NonScreen || rc == UBGraphicsScene::PdfExport)) { + if (!snapshot().isNull()) + painter->drawPixmap(0, 0, snapshot()); + } + else + UBGraphicsWebView::paint(painter, option, widget); +} + +QMap UBGraphicsW3CWidgetItem::preferences() +{ + return mPreferences; } +UBGraphicsW3CWidgetItem::Metadata UBGraphicsW3CWidgetItem::metadatas() const +{ + return mMetadatas; +} -QString UBGraphicsW3CWidgetItem::createNPAPIWrapper(const QString& url, - const QString& pMimeType, const QSize& sizeHint, const QString& pName) +QString UBGraphicsW3CWidgetItem::createNPAPIWrapper(const QString& url, const QString& pMimeType, const QSize& sizeHint, const QString& pName) { const QString userWidgetPath = UBSettings::settings()->userInteractiveDirectory() + "/" + tr("Web"); QDir userWidgetDir(userWidgetPath); @@ -950,11 +916,7 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapper(const QString& url, return createNPAPIWrapperInDir(url, userWidgetDir, pMimeType, sizeHint, pName); } - - -QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, const QDir& pDir, - const QString& pMimeType, const QSize& sizeHint, - const QString& pName) +QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, const QDir& pDir, const QString& pMimeType, const QSize& sizeHint, const QString& pName) { QString url = pUrl; url = UBFileSystemUtils::removeLocalFilePrefix(url); @@ -965,25 +927,22 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, co if (name.length() == 0) name = fi.baseName(); - if (fi.exists()){ + if (fi.exists()) url = fi.fileName(); - } loadNPAPIWrappersTemplates(); QString htmlTemplate; - if (pMimeType.length() > 0 && sNPAPIWrapperTemplates.contains(pMimeType)){ + if (pMimeType.length() > 0 && sNPAPIWrapperTemplates.contains(pMimeType)) htmlTemplate = sNPAPIWrapperTemplates.value(pMimeType); - } else { QString extension = UBFileSystemUtils::extension(url); - if (sNPAPIWrapperTemplates.contains(extension)) htmlTemplate = sNPAPIWrapperTemplates.value(extension); } - if (htmlTemplate.length() > 0){ + if (htmlTemplate.length() > 0) { htmlTemplate = htmlTemplate.replace(QString("{in.url}"), url) .replace(QString("{in.width}"), QString("%1").arg(sizeHint.width())) .replace(QString("{in.height}"), QString("%1").arg(sizeHint.height())); @@ -1003,15 +962,11 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, co QDir widgetLibraryDir(widgetLibraryPath); if (widgetLibraryDir.exists()) - { if (!UBFileSystemUtils::deleteDir(widgetLibraryDir.path())) - { qWarning() << "Cannot delete old widget " << widgetLibraryDir.path(); - } - } widgetLibraryDir.mkpath(widgetLibraryPath); - if (fi.exists()){ + if (fi.exists()) { QString target = widgetLibraryPath + "/" + fi.fileName(); QString source = pUrl; source = UBFileSystemUtils::removeLocalFilePrefix(source); @@ -1020,10 +975,9 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, co QFile configFile(widgetLibraryPath + "/config.xml"); - if (!configFile.open(QIODevice::WriteOnly)) - { + if (!configFile.open(QIODevice::WriteOnly)) { qWarning() << "Cannot open file " << configFile.fileName(); - return ""; + return QString(); } QTextStream outConfig(&configFile); @@ -1034,10 +988,9 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, co QFile indexFile(widgetLibraryPath + "/index.htm"); - if (!indexFile.open(QIODevice::WriteOnly)) - { + if (!indexFile.open(QIODevice::WriteOnly)) { qWarning() << "Cannot open file " << indexFile.fileName(); - return ""; + return QString(); } QTextStream outIndex(&indexFile); @@ -1049,34 +1002,24 @@ QString UBGraphicsW3CWidgetItem::createNPAPIWrapperInDir(const QString& pUrl, co return widgetLibraryPath; } else - { - return ""; - } + return QString(); } - -QString UBGraphicsW3CWidgetItem::createHtmlWrapperInDir(const QString& html, const QDir& pDir, - const QSize& sizeHint, const QString& pName) +QString UBGraphicsW3CWidgetItem::createHtmlWrapperInDir(const QString& html, const QDir& pDir, const QSize& sizeHint, const QString& pName) { - QString widgetPath = pDir.path() + "/" + pName + ".wgt"; widgetPath = UBFileSystemUtils::nextAvailableFileName(widgetPath); QDir widgetDir(widgetPath); if (!widgetDir.exists()) - { widgetDir.mkpath(widgetDir.path()); - } QFile configFile(widgetPath + "/" + "config.xml"); if (configFile.exists()) - { configFile.remove(configFile.fileName()); - } - if (!configFile.open(QIODevice::WriteOnly)) - { + if (!configFile.open(QIODevice::WriteOnly)) { qWarning() << "Cannot open file " << configFile.fileName(); return ""; } @@ -1104,13 +1047,10 @@ QString UBGraphicsW3CWidgetItem::createHtmlWrapperInDir(const QString& html, con QFile widgetHtmlFile(fullHtmlFileName); if (widgetHtmlFile.exists()) - { widgetHtmlFile.remove(widgetHtmlFile.fileName()); - } - if (!widgetHtmlFile.open(QIODevice::WriteOnly)) - { + if (!widgetHtmlFile.open(QIODevice::WriteOnly)) { qWarning() << "cannot open file " << widgetHtmlFile.fileName(); - return ""; + return QString(); } QTextStream outStartFile(&widgetHtmlFile); @@ -1129,7 +1069,6 @@ QString UBGraphicsW3CWidgetItem::createHtmlWrapperInDir(const QString& html, con widgetHtmlFile.close(); return widgetPath; - } QString UBGraphicsW3CWidgetItem::freezedWidgetPage() @@ -1143,13 +1082,14 @@ QString UBGraphicsW3CWidgetItem::freezedWidgetPage() if (!wrapperFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "can't open wrapper file " + freezedWidgetDefaultContentFilePath; defaultcontent = ""; - } else { + } + else { QByteArray arr = wrapperFile.readAll(); - if (!arr.isEmpty()) { - defaultcontent = QString(arr); - } else { + if (!arr.isEmpty()) + defaultcontent = QString(arr); + else { qDebug() << "content of " + freezedWidgetDefaultContentFilePath + "is empty"; - defaultcontent = ""; + defaultcontent = QString(); } } } @@ -1157,29 +1097,42 @@ QString UBGraphicsW3CWidgetItem::freezedWidgetPage() return defaultcontent; } +bool UBGraphicsW3CWidgetItem::hasNPAPIWrapper(const QString& pMimeType) +{ + loadNPAPIWrappersTemplates(); + + return sNPAPIWrapperTemplates.contains(pMimeType); +} + +void UBGraphicsW3CWidgetItem::javaScriptWindowObjectCleared() +{ + UBGraphicsWidgetItem::javaScriptWindowObjectCleared(); + + if(!mW3CWidgetAPI) + mW3CWidgetAPI = new UBW3CWidgetAPI(this); + + page()->mainFrame()->addToJavaScriptWindowObject("widget", mW3CWidgetAPI); + +} + void UBGraphicsW3CWidgetItem::loadNPAPIWrappersTemplates() { - if (!sTemplateLoaded) - { + if (!sTemplateLoaded) { sNPAPIWrapperTemplates.clear(); QString etcPath = UBPlatformUtils::applicationResourcesDirectory() + "/etc/"; QDir etcDir(etcPath); - foreach(QString fileName, etcDir.entryList()) - { - if (fileName.startsWith("npapi-wrapper") && (fileName.endsWith(".htm") || fileName.endsWith(".html"))) - { + foreach(QString fileName, etcDir.entryList()) { + if (fileName.startsWith("npapi-wrapper") && (fileName.endsWith(".htm") || fileName.endsWith(".html"))) { QString htmlContent = UBFileSystemUtils::readTextFile(etcPath + fileName); - if (htmlContent.length() > 0) - { + if (htmlContent.length() > 0) { QStringList tokens = fileName.split("."); - if (tokens.length() >= 4) - { + if (tokens.length() >= 4) { QString mime = tokens.at(tokens.length() - 4 ); mime += "/" + tokens.at(tokens.length() - 3); @@ -1191,14 +1144,11 @@ void UBGraphicsW3CWidgetItem::loadNPAPIWrappersTemplates() } } } - sNPAPIWrappperConfigTemplate = UBFileSystemUtils::readTextFile(etcPath + "npapi-wrapper.config.xml"); - sTemplateLoaded = true; } } - QString UBGraphicsW3CWidgetItem::textForSubElementByLocale(QDomElement rootElement, QString subTagName, QLocale locale) { QDomNodeList subList = rootElement.elementsByTagName(subTagName); @@ -1208,10 +1158,8 @@ QString UBGraphicsW3CWidgetItem::textForSubElementByLocale(QDomElement rootEleme if (lang.length() > 2) lang[2] = QLatin1Char('-'); - if (subList.count() > 1) - { - for(int i = 0; i < subList.count(); i++) - { + if (subList.count() > 1) { + for(int i = 0; i < subList.count(); i++) { QDomNode node = subList.at(i); QDomElement element = node.toElement(); @@ -1222,11 +1170,10 @@ QString UBGraphicsW3CWidgetItem::textForSubElementByLocale(QDomElement rootEleme } } - if (subList.count() >= 1) - { + if (subList.count() >= 1) { QDomElement element = subList.item(0).toElement(); return element.text(); } - return ""; + return QString(); } diff --git a/src/domain/UBGraphicsWidgetItem.h b/src/domain/UBGraphicsWidgetItem.h index 80edc768..133502fb 100644 --- a/src/domain/UBGraphicsWidgetItem.h +++ b/src/domain/UBGraphicsWidgetItem.h @@ -19,10 +19,10 @@ #include #include -#include "core/UB.h" - #include "UBGraphicsWebView.h" +#include "core/UB.h" + class UBWidgetUniboardAPI; class UBGraphicsScene; class UBW3CWidgetAPI; @@ -46,143 +46,72 @@ class UBGraphicsWidgetItem : public UBGraphicsWebView UBGraphicsWidgetItem(QGraphicsItem *parent = 0, int widgetType = 0); ~UBGraphicsWidgetItem(); - virtual UBGraphicsScene* scene(); - virtual void initialize(); - virtual UBItem* deepCopy() const = 0; + QUrl mainHtml(); + void loadMainHtml(); + QUrl widgetUrl(); + QString mainHtmlFileName(); + + bool hasEmbededObjects(); + bool hasEmbededFlash(); + + bool canBeContent(); + bool canBeTool(); - /* preferences */ - void setPreference(const QString& key, QString value); QString preference(const QString& key) const; + void setPreference(const QString& key, QString value); QMap preferences() const; void removePreference(const QString& key); void removeAllPreferences(); - /* datastore */ - - void setDatastoreEntry(const QString& key, QString value); QString datastoreEntry(const QString& key) const; + void setDatastoreEntry(const QString& key, QString value); QMap datastoreEntries() const; void removeDatastoreEntry(const QString& key); void removeAllDatastoreEntries(); - virtual UBGraphicsItemDelegate* Delegate() const {return mDelegate; } + virtual UBGraphicsItemDelegate* Delegate() const; virtual void remove(); void removeScript(); - QString downloadUrl(const QString &fileUrl, const QString &extention); - QString downloadWeb(const QString &fileUrl); void processDropEvent(QDropEvent *event); bool isDropableData(const QMimeData *data) const; - virtual void setOwnFolder(const QUrl &newFolder) {ownFolder = newFolder;} - virtual QUrl getOwnFolder() const {return ownFolder;} - virtual void setSnapshotPath(const QUrl &newFilePath){SnapshotFile = newFilePath;} - virtual QUrl getSnapshotPath(){return SnapshotFile;} + virtual QUrl getOwnFolder() const; + virtual void setOwnFolder(const QUrl &newFolder); + virtual void setSnapshotPath(const QUrl &newFilePath); + virtual QUrl getSnapshotPath(); virtual void clearSource(); - virtual void setUuid(const QUuid &pUuid); - - /* from UBAbstractWidget */ - void loadMainHtml(); - QUrl mainHtml() - { - return mMainHtmlUrl; - } - - QUrl widgetUrl() - { - return mWidgetUrl; - } + virtual void setUuid(const QUuid &pUuid); - QString mainHtmlFileName() - { - return mMainHtmlFileName; - } + QSize nominalSize() const; - bool hasEmbededObjects(); - bool hasEmbededFlash(); + bool hasLoadedSuccessfully() const; - QSize nominalSize() const - { - return mNominalSize; - } + bool freezable(); + bool resizable(); + bool isFrozen(); - bool canBeContent(); - bool canBeTool(); - - bool hasLoadedSuccessfully() const - { - return (mInitialLoadDone && !mLoadIsErronous); - } - - bool freezable() - { - return mIsFreezable; - } + QPixmap snapshot(); + void setSnapshot(const QPixmap& pix); + QPixmap takeSnapshot(); - bool resizable() - { - return mIsResizable; - } + virtual UBItem* deepCopy() const = 0; + virtual UBGraphicsScene* scene(); - static QString iconFilePath(const QUrl& pUrl); - static QString widgetName(const QUrl& pUrl); static int widgetType(const QUrl& pUrl); - - bool isFrozen() - { - return mIsFrozen; - } - - QPixmap snapshot(){ - return mSnapshot; - } - - void setSnapshot(const QPixmap& pix); - - QPixmap takeSnapshot(); + static QString widgetName(const QUrl& pUrl); + static QString iconFilePath(const QUrl& pUrl); public slots: void freeze(); void unFreeze(); - /* end from */ protected: - - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - - virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); - virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); - virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); - - - virtual bool eventFilter(QObject *obj, QEvent *event); - - virtual void sendJSEnterEvent(); - virtual void sendJSLeaveEvent(); - - QMap mPreferences; - QMap mDatastore; - - /* from UBAbstractWidget*/ - bool mMouseIsPressed; - bool mFirstReleaseAfterMove; - QUrl mMainHtmlUrl; - QString mMainHtmlFileName; - QUrl mWidgetUrl; - QSize mNominalSize; - bool mIsResizable; - bool mInitialLoadDone; - bool mLoadIsErronous; - - bool mIsFreezable; - int mCanBeContent; - int mCanBeTool; enum OSType { type_NONE = 0, // 0000 @@ -192,33 +121,49 @@ class UBGraphicsWidgetItem : public UBGraphicsWebView type_ALL = 7, // 0111 }; - virtual void injectInlineJavaScript(); - virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + bool mFirstReleaseAfterMove; + bool mInitialLoadDone; + bool mIsFreezable; + bool mIsResizable; + bool mLoadIsErronous; + bool mMouseIsPressed; + int mCanBeContent; + int mCanBeTool; + QSize mNominalSize; + QString mMainHtmlFileName; + QUrl mMainHtmlUrl; + QUrl mWidgetUrl; + QMap mDatastore; + QMap mPreferences; - //virtual void dropEvent(QDropEvent *); - /* end from */ + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event); + virtual bool eventFilter(QObject *obj, QEvent *event); + virtual void sendJSEnterEvent(); + virtual void sendJSLeaveEvent(); + virtual void injectInlineJavaScript(); + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); protected slots: void geometryChangeRequested(const QRect& geom); virtual void javaScriptWindowObjectCleared(); - /* from UBAbstractWidget*/ void mainFrameLoadFinished(bool ok); - /* end from */ private: - QPointF mLastMousePos; - bool mShouldMoveWidget; + bool mIsFrozen; + bool mIsTakingSnapshot; + bool mShouldMoveWidget; UBWidgetUniboardAPI* mUniboardAPI; + QPixmap mSnapshot; + QPointF mLastMousePos; QUrl ownFolder; - QUrl SnapshotFile; + QUrl SnapshotFile; - /* from UBAbstractWidget*/ - static QStringList sInlineJavaScripts; static bool sInlineJavaScriptLoaded; - bool mIsFrozen; - QPixmap mSnapshot; - bool mIsTakingSnapshot; - /* end from */ + static QStringList sInlineJavaScripts; }; class UBGraphicsAppleWidgetItem : public UBGraphicsWidgetItem @@ -227,17 +172,16 @@ class UBGraphicsAppleWidgetItem : public UBGraphicsWidgetItem public: UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent = 0); - ~UBGraphicsAppleWidgetItem(); + ~UBGraphicsAppleWidgetItem(); - enum { Type = UBGraphicsItemType::AppleWidgetItemType }; - - virtual int type() const - { - return Type; - } - - virtual UBItem* deepCopy() const; + virtual int type() const; virtual void setUuid(const QUuid &pUuid); + virtual UBItem* deepCopy() const; + + enum + { + Type = UBGraphicsItemType::AppleWidgetItemType + }; }; @@ -246,43 +190,13 @@ class UBGraphicsW3CWidgetItem : public UBGraphicsWidgetItem Q_OBJECT public: - UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent = 0); - ~UBGraphicsW3CWidgetItem(); - - enum { Type = UBGraphicsItemType::W3CWidgetItemType }; - - virtual int type() const - { - return Type; - } - - virtual UBItem* deepCopy() const; - - virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ); - virtual void setUuid(const QUuid &pUuid); - - static QString createNPAPIWrapper(const QString& url, - const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), - const QString& pName = QString()); - - static QString createNPAPIWrapperInDir(const QString& url, const QDir& pDir, - const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), - const QString& pName = QString()); - - static QString createHtmlWrapperInDir(const QString& html, const QDir& pDir, - const QSize& sizeHint, const QString& pName); - - static QString freezedWidgetPage(); - - static bool hasNPAPIWrapper(const QString& pMimeType); - class PreferenceValue { public: PreferenceValue() { - // NOOP + /* NOOP */ } @@ -292,8 +206,8 @@ class UBGraphicsW3CWidgetItem : public UBGraphicsWidgetItem readonly = pReadonly; } - QString value; bool readonly; + QString value; }; class Metadata @@ -308,39 +222,44 @@ class UBGraphicsW3CWidgetItem : public UBGraphicsWidgetItem QString version; }; - QMap preferences() - { - return mPreferences; - } + enum + { + Type = UBGraphicsItemType::W3CWidgetItemType + }; - Metadata metadatas() const - { - return mMetadatas; - } + UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent = 0); + ~UBGraphicsW3CWidgetItem(); + + virtual int type() const; + virtual void setUuid(const QUuid &pUuid); + virtual UBItem* deepCopy() const; + + virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ); + + QMap preferences(); + Metadata metadatas() const; + + static QString createNPAPIWrapper(const QString& url, const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), const QString& pName = QString()); + static QString createNPAPIWrapperInDir(const QString& url, const QDir& pDir, const QString& pMimeType = QString(), const QSize& sizeHint = QSize(300, 150), const QString& pName = QString()); + static QString createHtmlWrapperInDir(const QString& html, const QDir& pDir, const QSize& sizeHint, const QString& pName); + static QString freezedWidgetPage(); + static bool hasNPAPIWrapper(const QString& pMimeType); Metadata mMetadatas; private slots: - virtual void javaScriptWindowObjectCleared(); private: - - QMap mPreferences; + static void loadNPAPIWrappersTemplates(); + static QString textForSubElementByLocale(QDomElement rootElement, QString subTagName, QLocale locale); UBW3CWidgetAPI* mW3CWidgetAPI; + QMap mPreferences; static bool sTemplateLoaded; - - static QMap sNPAPIWrapperTemplates; - static QString sNPAPIWrappperConfigTemplate; - - static void loadNPAPIWrappersTemplates(); - - static QString textForSubElementByLocale(QDomElement rootElement, QString subTagName, QLocale locale); - - + static QMap sNPAPIWrapperTemplates; }; #endif // UBGRAPHICSWIDGETITEM_H