From a75ce3026134157ebf3b02c4b4f42f55e05873ae Mon Sep 17 00:00:00 2001 From: Yimgo Date: Fri, 3 Aug 2012 14:32:42 +0200 Subject: [PATCH] Tool widgets supported. The old code has to be removed and remaining bugs fixed. --- src/board/UBBoardController.cpp | 99 +++----------------- src/board/UBBoardController.h | 6 -- src/domain/UBGraphicsScene.cpp | 3 + src/domain/UBGraphicsScene.h | 3 + src/gui/UBToolWidget.cpp | 157 +++++++++++++++----------------- src/gui/UBToolWidget.h | 29 +++--- 6 files changed, 108 insertions(+), 189 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index bf7571d1..184ec2a1 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1543,52 +1543,15 @@ qreal UBBoardController::currentZoom() return 1.0; } - -UBToolWidget* UBBoardController::addTool(const QUrl& toolUrl) -{ - return addTool(toolUrl, mControlView->mapToScene(mControlView->rect().center())); -} - - -UBToolWidget* UBBoardController::addTool(const QUrl& toolUrl, QPointF scenePos) -{ - UBToolWidget *toolWidget = new UBToolWidget(toolUrl, mMainWindow); // Deleted in UBBoardController::removeTool - QPoint pos = mControlView->mapToGlobal(mControlView->mapFromScene(scenePos)); - pos -= QPoint(toolWidget->width() / 2, toolWidget->height() / 2); - - toolWidget->move(pos); - - mTools.append(toolWidget); - - toolWidget->show(); - - return toolWidget; -} - - -void UBBoardController::removeTool(UBToolWidget* toolWidget) -{ - toolWidget->hide(); - - mTools.removeAll(toolWidget); - - delete toolWidget; -} - - void UBBoardController::hide() { UBApplication::mainWindow->actionLibrary->setChecked(false); - - controlViewHidden(); } void UBBoardController::show() { UBApplication::mainWindow->actionLibrary->setChecked(false); - - controlViewShown(); } @@ -1813,25 +1776,6 @@ void UBBoardController::grabScene(const QRectF& pSceneRect) } } - -void UBBoardController::controlViewHidden() -{ - foreach(UBToolWidget* tool, mTools) - { - tool->hide(); - } -} - - -void UBBoardController::controlViewShown() -{ - foreach(UBToolWidget* tool, mTools) - { - tool->show(); - } -} - - UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos) { QUuid uuid = QUuid::createUuid(); @@ -2098,56 +2042,39 @@ void UBBoardController::togglePodcast(bool checked) void UBBoardController::moveGraphicsWidgetToControlView(UBGraphicsWidgetItem* graphicsWidget) { - /*QPoint controlViewPos = mControlView->mapFromScene(graphicsWidget->sceneBoundingRect().center()); - - graphicsWidget->setSelected(false); - - UBAbstractWidget *aw = graphicsWidget->widgetWebView(); - graphicsWidget->setWidget(0); + graphicsWidget->remove(); - UBToolWidget *toolWidget = new UBToolWidget(aw, mControlContainer); - - graphicsWidget->scene()->removeItem(graphicsWidget); // TODO UB 4.6 probably leaking the frame - - toolWidget->centerOn(mControlView->mapTo(mControlContainer, controlViewPos)); - - toolWidget->show();*/ + UBToolWidget *toolWidget = new UBToolWidget(graphicsWidget); + mActiveScene->addItem(toolWidget); } void UBBoardController::moveToolWidgetToScene(UBToolWidget* toolWidget) { - /*int xIsOdd = toolWidget->width() % 2; - int yIsOdd = toolWidget->height() % 2; - - QPoint mainWindowCenter = toolWidget->mapTo(mMainWindow, QPoint(toolWidget->width(), toolWidget->height()) / 2); - - UBAbstractWidget* webWidget = toolWidget->webWidget(); - webWidget->setParent(0); + QPoint mainWindowCenter = (QPointF(toolWidget->preferredWidth(), toolWidget->preferredHeight()) / 2).toPoint(); - UBGraphicsWidgetItem* graphicsWidget = 0; + UBGraphicsWidgetItem *graphicsWidgetItem = toolWidget->graphicsWidgetItem(); + graphicsWidgetItem->setParent(0); - UBW3CWidget* w3cWidget = qobject_cast(webWidget); - if (w3cWidget) + if (qobject_cast(graphicsWidgetItem)) { - graphicsWidget = new UBGraphicsW3CWidgetItem(w3cWidget); + graphicsWidgetItem = qobject_cast(graphicsWidgetItem); } else { - UBAppleWidget* appleWidget = qobject_cast(webWidget); - if (appleWidget) + if (qobject_cast(graphicsWidgetItem)) { - graphicsWidget = new UBGraphicsAppleWidgetItem(appleWidget); + graphicsWidgetItem = qobject_cast(graphicsWidgetItem); } } QPoint controlViewCenter = mControlView->mapFrom(mMainWindow, mainWindowCenter); - QPointF scenePos = mControlView->mapToScene(controlViewCenter) + QPointF(xIsOdd * 0.5, yIsOdd * 0.5); + QPointF scenePos = mControlView->mapToScene(controlViewCenter); - mActiveScene->addGraphicsWidget(graphicsWidget, scenePos); + mActiveScene->addGraphicsWidget(graphicsWidgetItem, scenePos); toolWidget->hide(); - toolWidget->deleteLater();*/ + toolWidget->deleteLater(); } diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index d5f2b0f6..d02b6588 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -195,17 +195,12 @@ class UBBoardController : public UBDocumentContainer void hideMessage(); void setDisabled(bool disable); void setColorIndex(int pColorIndex); - UBToolWidget* addTool(const QUrl& toolUrl, QPointF scenePos); - UBToolWidget* addTool(const QUrl& toolUrl); - void removeTool(UBToolWidget* toolWidget); void hide(); void show(); void setWidePageSize(bool checked); void setRegularPageSize(bool checked); void stylusToolChanged(int tool); void grabScene(const QRectF& pSceneRect); - void controlViewHidden(); - void controlViewShown(); UBGraphicsMediaItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos); UBGraphicsMediaItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos); UBGraphicsWidgetItem *addW3cWidget(const QUrl& pUrl, const QPointF& pos); @@ -275,7 +270,6 @@ class UBBoardController : public UBDocumentContainer QColor mPenColorOnLightBackground; QColor mMarkerColorOnDarkBackground; QColor mMarkerColorOnLightBackground; - QList mTools; qreal mSystemScaleFactor; bool mCleanupDone; QMap > mActionTexts; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index b9fb2172..de503788 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -31,6 +31,7 @@ #include "gui/UBMagnifer.h" #include "gui/UBMainWindow.h" +#include "gui/UBToolWidget.h" #include "tools/UBGraphicsRuler.h" #include "tools/UBGraphicsProtractor.h" @@ -1464,6 +1465,8 @@ void UBGraphicsScene::addGraphicsWidget(UBGraphicsWidgetItem* graphicsWidget, co UBApplication::boardController->controlView()->setFocus(); } + + UBGraphicsW3CWidgetItem* UBGraphicsScene::addOEmbed(const QUrl& pContentUrl, const QPointF& pPos) { QStringList widgetPaths = UBPersistenceManager::persistenceManager()->allWidgets(UBSettings::settings()->applicationApplicationsLibraryDirectory()); diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 9cf76d4b..0c280491 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -35,6 +35,7 @@ class UBGraphicsAudioItem; class UBGraphicsWidgetItem; class UBGraphicsW3CWidgetItem; class UBGraphicsAppleWidgetItem; +class UBToolWidget; class UBGraphicsPDFItem; class UBGraphicsTextItem; class UBGraphicsRuler; @@ -133,6 +134,8 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem UBGraphicsW3CWidgetItem* addW3CWidget(const QUrl& pWidgetUrl, const QPointF& pPos = QPointF(0, 0)); void addGraphicsWidget(UBGraphicsWidgetItem* graphicsWidget, const QPointF& pPos = QPointF(0, 0)); + + UBGraphicsMediaItem* addMedia(const QUrl& pMediaFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0)); UBGraphicsMediaItem* addVideo(const QUrl& pVideoFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0)); UBGraphicsMediaItem* addAudio(const QUrl& pAudioFileUrl, bool shouldPlayAsap, const QPointF& pPos = QPointF(0, 0)); diff --git a/src/gui/UBToolWidget.cpp b/src/gui/UBToolWidget.cpp index 7c4caa3a..665711c6 100644 --- a/src/gui/UBToolWidget.cpp +++ b/src/gui/UBToolWidget.cpp @@ -23,9 +23,6 @@ #include "core/UBApplication.h" #include "core/UBSettings.h" -#include "domain/UBAbstractWidget.h" -#include "domain/UBAppleWidget.h" -#include "domain/UBW3CWidget.h" #include "domain/UBGraphicsScene.h" #include "domain/UBGraphicsWidgetItem.h" @@ -41,38 +38,31 @@ QPixmap* UBToolWidget::sClosePixmap = 0; QPixmap* UBToolWidget::sUnpinPixmap = 0; -UBToolWidget::UBToolWidget(const QUrl& pUrl, QWidget* pParent) - : QWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) - , mToolWidget(0) +UBToolWidget::UBToolWidget(const QUrl& pUrl, QGraphicsItem *pParent) + : QGraphicsWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) + , mGraphicsWidgetItem(0) , mShouldMoveWidget(false) { - int widgetType = UBAbstractWidget::widgetType(pUrl); + int widgetType = UBGraphicsWidgetItem::widgetType(pUrl); - if(widgetType == UBWidgetType::Apple) - { - mToolWidget = new UBAppleWidget(pUrl, this); - } - else if(widgetType == UBWidgetType::W3C) - { - mToolWidget = new UBW3CWidget(pUrl, this); - } + if (widgetType == UBWidgetType::Apple) + mGraphicsWidgetItem = new UBGraphicsAppleWidgetItem(pUrl, this); + else if (widgetType == UBWidgetType::W3C) + mGraphicsWidgetItem = new UBGraphicsW3CWidgetItem(pUrl, this); else - { qDebug() << "UBToolWidget::UBToolWidget: Unknown widget Type"; - return; - } initialize(); } -UBToolWidget::UBToolWidget(UBAbstractWidget* pWidget, QWidget* pParent) - : QWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) - , mToolWidget(pWidget) +UBToolWidget::UBToolWidget(UBGraphicsWidgetItem *pWidget, QGraphicsItem *pParent) + : QGraphicsWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) , mShouldMoveWidget(false) { - mToolWidget->setParent(this); - mToolWidget->loadMainHtml(); + mGraphicsWidgetItem = pWidget; + mGraphicsWidgetItem->setParent(this); + mGraphicsWidgetItem->loadMainHtml(); initialize(); @@ -88,85 +78,81 @@ UBToolWidget::~UBToolWidget() void UBToolWidget::initialize() { - mToolWidget->setAcceptDrops(false); - - if(!sClosePixmap) - { + if (!sClosePixmap) sClosePixmap = new QPixmap(":/images/close.svg"); - } if(!sUnpinPixmap) - { sUnpinPixmap = new QPixmap(":/images/unpin.svg"); - } + + QGraphicsLinearLayout *graphicsLayout = new QGraphicsLinearLayout(Qt::Vertical); - mToolWidget->settings()->setAttribute(QWebSettings::PluginsEnabled, true); + mFrameWidth = UBSettings::settings()->objectFrameWidth; + mContentMargin = sClosePixmap->width() / 2 + mFrameWidth; + graphicsLayout->setContentsMargins(mContentMargin, mContentMargin, mContentMargin, mContentMargin); + setPreferredSize(mGraphicsWidgetItem->preferredWidth() + mContentMargin * 2, mGraphicsWidgetItem->preferredHeight() + mContentMargin * 2); - mToolWidget->setAttribute(Qt::WA_OpaquePaintEvent, false); + mGraphicsWidgetItem->setAcceptDrops(false); - QPalette palette = mToolWidget->page()->palette(); - palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); - mToolWidget->page()->setPalette(palette); + mGraphicsWidgetItem->settings()->setAttribute(QWebSettings::PluginsEnabled, true); - connect(mToolWidget->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); - connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(javaScriptWindowObjectCleared())); + mGraphicsWidgetItem->setAttribute(Qt::WA_OpaquePaintEvent, false); - setLayout(new QVBoxLayout()); + QPalette palette = mGraphicsWidgetItem->page()->palette(); + palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); + mGraphicsWidgetItem->page()->setPalette(palette); - mFrameWidth = UBSettings::settings()->objectFrameWidth; - mContentMargin = sClosePixmap->width() / 2 + mFrameWidth; + connect(mGraphicsWidgetItem->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); + + QGraphicsWebView *addedGraphicsWebView = new QGraphicsWebView(); + addedGraphicsWebView->load(mGraphicsWidgetItem->mainHtml()); + graphicsLayout->addItem(addedGraphicsWebView); - layout()->setContentsMargins(mContentMargin, mContentMargin, mContentMargin, mContentMargin); - layout()->addWidget(mToolWidget); + setLayout(graphicsLayout); - setFixedSize(mToolWidget->width() + mContentMargin * 2, mToolWidget->height() + mContentMargin * 2); + connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(javaScriptWindowObjectCleared())); - mToolWidget->installEventFilter(this); + mGraphicsWidgetItem->installEventFilter(this); } void UBToolWidget::javaScriptWindowObjectCleared() { - UBWidgetUniboardAPI* uniboardAPI = new UBWidgetUniboardAPI(UBApplication::boardController->activeScene()); + UBWidgetUniboardAPI *uniboardAPI = new UBWidgetUniboardAPI(UBApplication::boardController->activeScene()); - mToolWidget->page()->mainFrame()->addToJavaScriptWindowObject("sankore", uniboardAPI); + mGraphicsWidgetItem->page()->mainFrame()->addToJavaScriptWindowObject("sankore", uniboardAPI); - /*UBW3CWidget* w3c = dynamic_cast(mToolWidget); - if (w3c) + UBGraphicsW3CWidgetItem *graphicsW3cWidgetItem = dynamic_cast(mGraphicsWidgetItem); + if (graphicsW3cWidgetItem) { - UBW3CWidgetAPI* widgetAPI = new UBW3CWidgetAPI(w3c); - mToolWidget->page()->mainFrame()->addToJavaScriptWindowObject("widget", widgetAPI); - }*/ + UBW3CWidgetAPI* widgetAPI = new UBW3CWidgetAPI(graphicsW3cWidgetItem); + mGraphicsWidgetItem->page()->mainFrame()->addToJavaScriptWindowObject("widget", widgetAPI); + } } -void UBToolWidget::paintEvent(QPaintEvent *event) +void UBToolWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - QWidget::paintEvent(event); + QGraphicsWidget::paint(painter, option, widget); if (isActiveWindow()) { - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - painter.setPen(Qt::NoPen); - painter.setBrush(QColor(127, 127, 127, 127)); + painter->setRenderHint(QPainter::Antialiasing); + painter->setPen(Qt::NoPen); + painter->setBrush(QColor(127, 127, 127, 127)); - painter.drawRoundedRect(QRectF(sClosePixmap->width() / 2 - , sClosePixmap->height() / 2 - , width() - sClosePixmap->width() - , mFrameWidth), mFrameWidth / 2, mFrameWidth / 2); + painter->drawRoundedRect(QRectF(sClosePixmap->width() / 2, sClosePixmap->height() / 2, preferredWidth() - sClosePixmap->width(), mFrameWidth), mFrameWidth / 2, mFrameWidth / 2); - painter.drawPixmap(0, 0, *sClosePixmap); + painter->drawPixmap(0, 0, *sClosePixmap); - if (mToolWidget->canBeContent()) - painter.drawPixmap(mContentMargin, 0, *sUnpinPixmap); + if (mGraphicsWidgetItem->canBeContent()) + painter->drawPixmap(mContentMargin, 0, *sUnpinPixmap); } } -void UBToolWidget::mousePressEvent ( QMouseEvent * event ) +void UBToolWidget::mousePressEvent(QGraphicsSceneMouseEvent *event) { - QWidget::mousePressEvent(event); + QGraphicsWidget::mousePressEvent(event); // did webkit consume the mouse press ? mShouldMoveWidget = !event->isAccepted() && (event->buttons() & Qt::LeftButton); @@ -179,50 +165,50 @@ void UBToolWidget::mousePressEvent ( QMouseEvent * event ) } -void UBToolWidget::mouseMoveEvent ( QMouseEvent * event ) +void UBToolWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if(mShouldMoveWidget && (event->buttons() & Qt::LeftButton)) { - move(pos() - mMousePressPos + event->pos()); + setPos(pos() - mMousePressPos + event->pos()); event->accept(); } - QWidget::mouseMoveEvent(event); + QGraphicsWidget::mouseMoveEvent(event); } -void UBToolWidget::mouseReleaseEvent(QMouseEvent * event) +void UBToolWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { mShouldMoveWidget = false; if (event->pos().x() >= 0 && event->pos().x() < sClosePixmap->width() && event->pos().y() >= 0 && event->pos().y() < sClosePixmap->height()) { - UBApplication::boardController->removeTool(this); + scene()->removeItem(this); event->accept(); } - else if (mToolWidget->canBeContent() && event->pos().x() >= mContentMargin && event->pos().x() < mContentMargin + sUnpinPixmap->width() + else if (mGraphicsWidgetItem->canBeContent() && event->pos().x() >= mContentMargin && event->pos().x() < mContentMargin + sUnpinPixmap->width() && event->pos().y() >= 0 && event->pos().y() < sUnpinPixmap->height()) { UBApplication::boardController->moveToolWidgetToScene(this); event->accept(); } else - QWidget::mouseReleaseEvent(event); // don't propgate to parent, the widget is deleted in UBApplication::boardController->removeTool + QGraphicsWidget::mouseReleaseEvent(event); // don't propgate to parent, the widget is deleted in UBApplication::boardController->removeTool } bool UBToolWidget::eventFilter(QObject *obj, QEvent *event) { - if (mShouldMoveWidget && obj == mToolWidget && event->type() == QEvent::MouseMove) + if (mShouldMoveWidget && obj == mGraphicsWidgetItem && event->type() == QEvent::MouseMove) { QMouseEvent *mouseMoveEvent = static_cast(event); if (mouseMoveEvent->buttons() & Qt::LeftButton) { - move(pos() - mMousePressPos + mToolWidget->mapTo(this, mouseMoveEvent->pos())); + setPos(pos() - mMousePressPos + mGraphicsWidgetItem->mapToItem(this, mouseMoveEvent->pos())); event->accept(); return true; @@ -234,22 +220,27 @@ bool UBToolWidget::eventFilter(QObject *obj, QEvent *event) } -void UBToolWidget::centerOn(const QPoint& pos) +void UBToolWidget::centerOn(const QPointF& pos) { - QWidget::move(pos - QPoint(width() / 2, height() / 2)); + QGraphicsWidget::setPos(pos - QPointF(preferredWidth() / 2, preferredHeight() / 2)); } -QPoint UBToolWidget::naturalCenter() const +QPointF UBToolWidget::naturalCenter() const { - if (mToolWidget) - return mToolWidget->geometry().center(); + if (mGraphicsWidgetItem) + return mGraphicsWidgetItem->geometry().center(); else - return QPoint(0, 0); + return QPointF(0, 0); } -UBAbstractWidget* UBToolWidget::webWidget() const +UBGraphicsWidgetItem* UBToolWidget::graphicsWidgetItem() const +{ + return mGraphicsWidgetItem; +} + +UBGraphicsScene* UBToolWidget::scene() { - return mToolWidget; + return qobject_cast(QGraphicsItem::scene()); } diff --git a/src/gui/UBToolWidget.h b/src/gui/UBToolWidget.h index f0fb0c7f..a56db78c 100644 --- a/src/gui/UBToolWidget.h +++ b/src/gui/UBToolWidget.h @@ -18,32 +18,33 @@ #include -class UBAbstractWidget; +class UBGraphicsWidgetItem; class QWidget; class UBGraphicsScene; -class UBToolWidget : public QWidget +class UBToolWidget : public QGraphicsWidget { - Q_OBJECT; + Q_OBJECT public: - UBToolWidget(const QUrl& pUrl, QWidget* pParent = 0); - UBToolWidget(UBAbstractWidget* pWidget, QWidget* pParent = 0); + UBToolWidget(const QUrl& pUrl, QGraphicsItem *pParent = 0); + UBToolWidget(UBGraphicsWidgetItem* pGraphicsWidgetItem, QGraphicsItem *pParent = 0); virtual ~UBToolWidget(); - void centerOn(const QPoint& pos); + void centerOn(const QPointF& pos); - QPoint naturalCenter() const; + QPointF naturalCenter() const; - UBAbstractWidget* webWidget() const; + UBGraphicsWidgetItem* graphicsWidgetItem() const; + virtual UBGraphicsScene* scene(); protected: void initialize(); - virtual void paintEvent(QPaintEvent *); + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - virtual void mousePressEvent ( QMouseEvent * event ); - virtual void mouseMoveEvent ( QMouseEvent * event ); - virtual void mouseReleaseEvent ( QMouseEvent * event ); + virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual bool eventFilter(QObject *obj, QEvent *event); @@ -55,9 +56,9 @@ class UBToolWidget : public QWidget static QPixmap *sClosePixmap; static QPixmap *sUnpinPixmap; - UBAbstractWidget *mToolWidget; + UBGraphicsWidgetItem *mGraphicsWidgetItem; - QPoint mMousePressPos; + QPointF mMousePressPos; bool mShouldMoveWidget;