UBGraphicsWidgetItem.cpp 11.1 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15

Claudio Valerio's avatar
Claudio Valerio committed
16 17 18 19 20 21 22 23 24 25 26 27
#include "UBGraphicsWidgetItem.h"

#include "api/UBWidgetUniboardAPI.h"
#include "api/UBW3CWidgetAPI.h"

#include "UBGraphicsItemDelegate.h"
#include "UBGraphicsWidgetItemDelegate.h"
#include "UBGraphicsDelegateFrame.h"

#include "UBW3CWidget.h"
#include "UBGraphicsScene.h"
#include "UBAppleWidget.h"
28
#include "frameworks/UBFileSystemUtils.h"
Claudio Valerio's avatar
Claudio Valerio committed
29

30 31
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
32 33 34 35 36 37
UBGraphicsWidgetItem::UBGraphicsWidgetItem(QGraphicsItem *parent, int widgetType)
    : UBGraphicsProxyWidget(parent)
    , mWebKitWidget(0)
    , mShouldMoveWidget(false)
    , mUniboardAPI(0)
{
38
    setAcceptDrops(false);
Claudio Valerio's avatar
Claudio Valerio committed
39 40 41
    UBGraphicsWidgetItemDelegate* delegate = new UBGraphicsWidgetItemDelegate(this, widgetType);
    delegate->init();
    setDelegate(delegate);
42
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly
Claudio Valerio's avatar
Claudio Valerio committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
}


UBGraphicsWidgetItem::~UBGraphicsWidgetItem()
{
    // NOOP
}


void UBGraphicsWidgetItem::javaScriptWindowObjectCleared()
{
    if(!mUniboardAPI)
            mUniboardAPI = new UBWidgetUniboardAPI(scene(), this);

57
    mWebKitWidget->page()->mainFrame()->addToJavaScriptWindowObject("sankore", mUniboardAPI);
Claudio Valerio's avatar
Claudio Valerio committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

}


void UBGraphicsWidgetItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    UBGraphicsProxyWidget::mousePressEvent(event);

    // did webkit consume the mouse press ?
    mShouldMoveWidget = !event->isAccepted() && (event->buttons() & Qt::LeftButton);

    mLastMousePos = mapToScene(event->pos());

    event->accept();
}


void UBGraphicsWidgetItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    mShouldMoveWidget = false;

    UBGraphicsProxyWidget::mouseReleaseEvent(event);
}

82 83 84
void UBGraphicsWidgetItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    sendJSEnterEvent();
85
    mDelegate->hoverEnterEvent(event);
86 87 88 89 90
    UBGraphicsProxyWidget::hoverEnterEvent(event);
}
void UBGraphicsWidgetItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    sendJSLeaveEvent();
91
    mDelegate->hoverLeaveEvent(event);
92 93
    UBGraphicsProxyWidget::hoverLeaveEvent(event);
}
Claudio Valerio's avatar
Claudio Valerio committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

bool UBGraphicsWidgetItem::eventFilter(QObject *obj, QEvent *event)
{
    if (mShouldMoveWidget && obj == mWebKitWidget && event->type() == QEvent::MouseMove)
    {
        QMouseEvent *mouseMoveEvent = static_cast<QMouseEvent*>(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::resize ( qreal w, qreal h )
{
    UBGraphicsProxyWidget::resize(w, h);

    mWebKitWidget->resize(w, h);
}


void UBGraphicsWidgetItem::resize ( const QSizeF & size )
{
    resize(size.width(), size.height());
}


void UBGraphicsWidgetItem::geometryChangeRequested(const QRect& geom)
{
    resize(geom.width(), geom.height());
}


void UBGraphicsWidgetItem::initialize()
{
    connect(mWebKitWidget->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared()));

    QPalette palette = mWebKitWidget->page()->palette();
    palette.setBrush(QPalette::Base, QBrush(Qt::transparent));
    mWebKitWidget->page()->setPalette(palette);

    UBGraphicsProxyWidget::setWidget(mWebKitWidget);

    mWebKitWidget->installEventFilter(this);

    UBGraphicsProxyWidget::setMinimumSize(mWebKitWidget->nominalSize());

    connect(mWebKitWidget, SIGNAL(geometryChangeRequested(const QRect&)), this, SLOT(geometryChangeRequested(const QRect&)));

    if (mDelegate && mDelegate->frame() && mWebKitWidget->resizable())
        mDelegate->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing);
160 161

    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly
Claudio Valerio's avatar
Claudio Valerio committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}


UBGraphicsScene* UBGraphicsWidgetItem::scene()
{
    return qobject_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}


UBAbstractWidget* UBGraphicsWidgetItem::widgetWebView()
{
    return mWebKitWidget;
}


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<QString, QString> 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);
}


QString UBGraphicsWidgetItem::datastoreEntry(const QString& key) const
{
    if (mDatastore.contains(key))
        return mDatastore.value(key);
    else
        return "";
}


QMap<QString, QString> UBGraphicsWidgetItem::datastoreEntries() const
{
    return mDatastore;
}


void UBGraphicsWidgetItem::removeDatastoreEntry(const QString& key)
{
    mDatastore.remove(key);
}


void UBGraphicsWidgetItem::removeAllDatastoreEntries()
{
    mDatastore.clear();
}


void UBGraphicsWidgetItem::remove()
251 252 253 254 255 256 257 258
{

    if (mDelegate)
        mDelegate->remove();

}

void UBGraphicsWidgetItem::removeScript()
Claudio Valerio's avatar
Claudio Valerio committed
259 260 261 262 263 264
{
    if (mWebKitWidget && mWebKitWidget->page() && mWebKitWidget->page()->mainFrame())
    {
        mWebKitWidget->page()->mainFrame()->evaluateJavaScript("if(widget && widget.onremove) { widget.onremove();}");
    }
}
265 266 267 268 269 270 271 272 273 274 275 276 277 278
void UBGraphicsWidgetItem::sendJSEnterEvent()
{
    if (mWebKitWidget && mWebKitWidget->page() && mWebKitWidget->page()->mainFrame())
    {
        mWebKitWidget->page()->mainFrame()->evaluateJavaScript("if(widget && widget.onenter) { widget.onenter();}");
    }
}
void UBGraphicsWidgetItem::sendJSLeaveEvent()
{
    if (mWebKitWidget && mWebKitWidget->page() && mWebKitWidget->page()->mainFrame())
    {
        mWebKitWidget->page()->mainFrame()->evaluateJavaScript("if(widget && widget.onleave) { widget.onleave();}");
    }
}
279 280 281 282 283 284 285

void UBGraphicsWidgetItem::clearSource()
{
    UBFileSystemUtils::deleteDir(getOwnFolder().toLocalFile());
    UBFileSystemUtils::deleteFile(getSnapshotPath().toLocalFile());
}

286 287 288 289
void UBGraphicsWidgetItem::processDropEvent(QDropEvent *event)
{
    return mUniboardAPI->ProcessDropEvent(event);
}
Ivan Ilin's avatar
Ivan Ilin committed
290 291 292 293
bool UBGraphicsWidgetItem::isDropableData(const QMimeData *data) const
{
    return mUniboardAPI->isDropableData(data);
}
Claudio Valerio's avatar
Claudio Valerio committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent)
    : UBGraphicsWidgetItem(parent)
{
    mWebKitWidget = new UBAppleWidget(pWidgetUrl, 0);
    initialize();
}


UBGraphicsAppleWidgetItem::UBGraphicsAppleWidgetItem(UBAppleWidget *appleWidget, QGraphicsItem *parent)
    : UBGraphicsWidgetItem(parent)

{
    mWebKitWidget = appleWidget;

    initialize();
}


UBGraphicsAppleWidgetItem::~UBGraphicsAppleWidgetItem()
{
    // NOOP
}


UBItem* UBGraphicsAppleWidgetItem::deepCopy() const
{
    UBGraphicsAppleWidgetItem *appleWidget = new UBGraphicsAppleWidgetItem(mWebKitWidget->widgetUrl(), 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;

}


UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(const QUrl& pWidgetUrl, QGraphicsItem *parent, int widgetType)
    : UBGraphicsWidgetItem(parent, widgetType)
    , mW3CWidgetAPI(0)
{
    mW3CWidget = new UBW3CWidget(pWidgetUrl, 0);
    mWebKitWidget = mW3CWidget;
    initialize();
347
    setOwnFolder(pWidgetUrl);
Claudio Valerio's avatar
Claudio Valerio committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
}

UBGraphicsW3CWidgetItem::UBGraphicsW3CWidgetItem(UBW3CWidget *w3cWidget, QGraphicsItem *parent, int widgetType)
    : UBGraphicsWidgetItem(parent)
    , mW3CWidget(w3cWidget)
    , mW3CWidgetAPI(0)
{
    Q_UNUSED(widgetType);
    mWebKitWidget = mW3CWidget;
    initialize();
}


UBGraphicsW3CWidgetItem::~UBGraphicsW3CWidgetItem()
{
    // NOOP
}


void UBGraphicsW3CWidgetItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    UBGraphicsScene::RenderingContext rc = UBGraphicsScene::Screen;

    if (scene())
      rc =  scene()->renderingContext();

    if ((!w3cWidget()->hasLoadedSuccessfully()) && (rc == UBGraphicsScene::NonScreen || rc == UBGraphicsScene::PdfExport))
    {
        if (!w3cWidget()->snapshot().isNull())
        {
           painter->drawPixmap(0, 0, w3cWidget()->snapshot());
        }
    }
    else
    {
        UBGraphicsProxyWidget::paint(painter, option, widget);
    }
}

void UBGraphicsW3CWidgetItem::javaScriptWindowObjectCleared()
{
    UBGraphicsWidgetItem::javaScriptWindowObjectCleared();

    if(!mW3CWidgetAPI)
        mW3CWidgetAPI = new UBW3CWidgetAPI(this);

    mWebKitWidget->page()->mainFrame()->addToJavaScriptWindowObject("widget", mW3CWidgetAPI);

}


UBW3CWidget::Metadata UBGraphicsW3CWidgetItem::metadatas() const
{
    return mW3CWidget->metadatas();
}


UBW3CWidget* UBGraphicsW3CWidgetItem::w3cWidget() const
{
    return mW3CWidget;
}


UBItem* UBGraphicsW3CWidgetItem::deepCopy() const
{
    UBGraphicsW3CWidgetItem *copy = new UBGraphicsW3CWidgetItem(mWebKitWidget->widgetUrl(), parentItem());

    copy->setPos(this->pos());
    copy->setTransform(this->transform());
    copy->setFlag(QGraphicsItem::ItemIsMovable, true);
    copy->setFlag(QGraphicsItem::ItemIsSelectable, true);
    copy->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
    copy->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked));
    copy->setUuid(this->uuid()); // this is OK for now as long as Widgets are imutable
    copy->setSourceUrl(this->sourceUrl());

    copy->resize(this->size());

    foreach(QString key, mPreferences.keys())
    {
        copy->setPreference(key, mPreferences.value(key));
    }

    foreach(QString key, mDatastore.keys())
    {
        copy->setDatastoreEntry(key, mDatastore.value(key));
    }

    return copy;
}