From f9e12cc32007e614ee73a2eac78ab5ca3ce04d6c Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Thu, 20 Sep 2012 12:01:57 +0300 Subject: [PATCH] UBToolWidget is QWidget again. He not depends on scene. Still have some mesh with undo stack and with widget content. --- src/board/UBBoardController.cpp | 26 +++-- src/board/UBBoardController.h | 1 + src/board/UBBoardView.cpp | 6 +- src/gui/UBToolWidget.cpp | 189 +++++++++++++++----------------- src/gui/UBToolWidget.h | 55 ++++------ 5 files changed, 134 insertions(+), 143 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index e9cf5191..c77c7c72 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1782,6 +1782,13 @@ qreal UBBoardController::currentZoom() return 1.0; } +void UBBoardController::removeTool(UBToolWidget* toolWidget) +{ + toolWidget->hide(); + + delete toolWidget; +} + void UBBoardController::hide() { UBApplication::mainWindow->actionLibrary->setChecked(false); @@ -2309,30 +2316,29 @@ void UBBoardController::togglePodcast(bool checked) void UBBoardController::moveGraphicsWidgetToControlView(UBGraphicsWidgetItem* graphicsWidget) { - graphicsWidget->remove(); - mActiveScene->setURStackEnable(false); + graphicsWidget->remove(false); UBGraphicsItem *toolW3C = duplicateItem(dynamic_cast(graphicsWidget)); UBGraphicsWidgetItem *copyedGraphicsWidget = NULL; if (UBGraphicsWidgetItem::Type == toolW3C->type()) copyedGraphicsWidget = static_cast(toolW3C); - UBToolWidget *toolWidget = new UBToolWidget(copyedGraphicsWidget); - mActiveScene->addItem(toolWidget); - qreal ssf = 1 / UBApplication::boardController->systemScaleFactor(); - - toolWidget->setScale(ssf); - toolWidget->setPos(graphicsWidget->scenePos()); + UBToolWidget *toolWidget = new UBToolWidget(copyedGraphicsWidget, mControlView); mActiveScene->setURStackEnable(true); + + QPoint controlViewPos = mControlView->mapFromScene(graphicsWidget->sceneBoundingRect().center()); + toolWidget->centerOn(mControlView->mapTo(mControlContainer, controlViewPos)); + toolWidget->show(); } void UBBoardController::moveToolWidgetToScene(UBToolWidget* toolWidget) { - UBGraphicsWidgetItem *graphicsWidgetItem = addW3cWidget(toolWidget->graphicsWidgetItem()->widgetUrl(), QPointF(0, 0)); + UBGraphicsWidgetItem *graphicsWidgetItem = addW3cWidget(toolWidget->toolWidget()->widgetUrl(), QPointF(0, 0)); graphicsWidgetItem->setPos(toolWidget->pos()); - toolWidget->remove(); + toolWidget->hide(); + delete toolWidget; graphicsWidgetItem->setSelected(true); } diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index fc014b18..99d0cd90 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -200,6 +200,7 @@ class UBBoardController : public UBDocumentContainer void hideMessage(); void setDisabled(bool disable); void setColorIndex(int pColorIndex); + void removeTool(UBToolWidget* toolWidget); void hide(); void show(); void setWidePageSize(bool checked); diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 3ccc7640..a654c765 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -553,8 +553,8 @@ Here we determines cases when items should to get mouse press event at pressing return false; break; - case UBToolWidget::Type: - return true; + //case UBToolWidget::Type: + // return true; case QGraphicsWebView::Type: return true; @@ -1123,7 +1123,7 @@ UBBoardView::mouseReleaseEvent (QMouseEvent *event) DelegateButton::Type != movingItem->type() && QGraphicsSvgItem::Type != movingItem->type() && UBGraphicsDelegateFrame::Type != movingItem->type() && - UBToolWidget::Type != movingItem->type() && +// UBToolWidget::Type != movingItem->type() && UBGraphicsCache::Type != movingItem->type() && QGraphicsWebView::Type != movingItem->type() && // for W3C widgets as Tools. !(!isMultipleSelectionEnabled() && movingItem->parentItem() && UBGraphicsWidgetItem::Type == movingItem->type() && UBGraphicsGroupContainerItem::Type == movingItem->parentItem()->type())) diff --git a/src/gui/UBToolWidget.cpp b/src/gui/UBToolWidget.cpp index 5b8a601b..0b9d3440 100644 --- a/src/gui/UBToolWidget.cpp +++ b/src/gui/UBToolWidget.cpp @@ -32,32 +32,32 @@ QPixmap* UBToolWidget::sClosePixmap = 0; QPixmap* UBToolWidget::sUnpinPixmap = 0; -UBToolWidget::UBToolWidget(const QUrl& pUrl, QGraphicsItem *pParent) - : QGraphicsWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) +UBToolWidget::UBToolWidget(const QUrl& pUrl, QWidget *pParent) + : QWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) , mShouldMoveWidget(false) , mContentMargin(0) , mFrameWidth(0) - , mGraphicsWebView(0) - , mGraphicsWidgetItem(0) + , mWebView(0) + , mToolWidget(0) { int widgetType = UBGraphicsWidgetItem::widgetType(pUrl); if (widgetType == UBWidgetType::Apple) - mGraphicsWidgetItem = new UBGraphicsAppleWidgetItem(pUrl, this); + mToolWidget = new UBGraphicsAppleWidgetItem(pUrl); else if (widgetType == UBWidgetType::W3C) - mGraphicsWidgetItem = new UBGraphicsW3CWidgetItem(pUrl, this); + mToolWidget = new UBGraphicsW3CWidgetItem(pUrl); else qDebug() << "UBToolWidget::UBToolWidget: Unknown widget Type"; initialize(); } -UBToolWidget::UBToolWidget(UBGraphicsWidgetItem *pWidget, QGraphicsItem *pParent) - : QGraphicsWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) +UBToolWidget::UBToolWidget(UBGraphicsWidgetItem *pWidget, QWidget *pParent) + : QWidget(pParent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint) , mShouldMoveWidget(false) , mContentMargin(0) , mFrameWidth(0) - , mGraphicsWebView(0) - , mGraphicsWidgetItem(pWidget) + , mWebView(0) + , mToolWidget(pWidget) { initialize(); javaScriptWindowObjectCleared(); @@ -76,50 +76,66 @@ void UBToolWidget::initialize() if(!sUnpinPixmap) sUnpinPixmap = new QPixmap(":/images/unpin.svg"); - UBGraphicsScene *wscene = dynamic_cast(mGraphicsWidgetItem->scene()); + UBGraphicsScene *wscene = dynamic_cast(mToolWidget->scene()); if (wscene) { - wscene->removeItemFromDeletion(mGraphicsWidgetItem); - wscene->removeItem(mGraphicsWidgetItem); + wscene->removeItemFromDeletion(mToolWidget); + wscene->removeItem(mToolWidget); } - mGraphicsWidgetItem->setParent(this); - - QGraphicsLinearLayout *graphicsLayout = new QGraphicsLinearLayout(Qt::Vertical, this); + mWebView = new QWebView(this); + + QPalette palette = mWebView->page()->palette(); + palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); + mWebView->page()->setPalette(palette); + + + mWebView->installEventFilter(this); + mFrameWidth = UBSettings::settings()->objectFrameWidth; mContentMargin = sClosePixmap->width() / 2 + mFrameWidth; - graphicsLayout->setContentsMargins(mContentMargin, mContentMargin, mContentMargin, mContentMargin); - setPreferredSize(mGraphicsWidgetItem->preferredWidth() + mContentMargin * 2, mGraphicsWidgetItem->preferredHeight() + mContentMargin * 2); + setLayout(new QVBoxLayout()); + layout()->setContentsMargins(mContentMargin, mContentMargin, mContentMargin, mContentMargin); + layout()->addWidget(mWebView); - mGraphicsWebView = new QGraphicsWebView(this); - connect(mGraphicsWebView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); - mGraphicsWebView->load(mGraphicsWidgetItem->mainHtml()); - graphicsLayout->addItem(mGraphicsWebView); + setFixedSize(mToolWidget->boundingRect().width() + mContentMargin * 2, mToolWidget->boundingRect().height() + mContentMargin * 2); - mGraphicsWebView->setAcceptDrops(false); - mGraphicsWebView->settings()->setAttribute(QWebSettings::PluginsEnabled, true); - mGraphicsWebView->setAttribute(Qt::WA_OpaquePaintEvent, false); + connect(mWebView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared())); + mWebView->load(mToolWidget->mainHtml()); - QPalette palette = mGraphicsWebView->page()->palette(); - palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); - mGraphicsWebView->page()->setPalette(palette); - setLayout(graphicsLayout); + mWebView->setAcceptDrops(false); + mWebView->settings()->setAttribute(QWebSettings::PluginsEnabled, true); + mWebView->setAttribute(Qt::WA_OpaquePaintEvent, false); + connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(javaScriptWindowObjectCleared())); } -bool UBToolWidget::event(QEvent *event) + +bool UBToolWidget::eventFilter(QObject *obj, QEvent *event) { - if (event->type() == QEvent::ShortcutOverride) - event->accept(); - return QGraphicsWidget::event(event); + if (mShouldMoveWidget && obj == mWebView && event->type() == QEvent::MouseMove) + { + QMouseEvent *mouseMoveEvent = static_cast(event); + + if (mouseMoveEvent->buttons() & Qt::LeftButton) + { + move(pos() - mMousePressPos + mWebView->mapTo(this, mouseMoveEvent->pos())); + + event->accept(); + return true; + } + } + + // standard event processing + return QObject::eventFilter(obj, event); } -void UBToolWidget::mousePressEvent(QGraphicsSceneMouseEvent *event) +void UBToolWidget::mousePressEvent(QMouseEvent *event) { - QGraphicsWidget::mousePressEvent(event); + QWidget::mousePressEvent(event); /* did webkit consume the mouse press ? */ mShouldMoveWidget = !event->isAccepted() && (event->buttons() & Qt::LeftButton); @@ -128,108 +144,85 @@ void UBToolWidget::mousePressEvent(QGraphicsSceneMouseEvent *event) update(); } -void UBToolWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +void UBToolWidget::mouseMoveEvent(QMouseEvent *event) { if(mShouldMoveWidget && (event->buttons() & Qt::LeftButton)) { - setPos(pos() - mMousePressPos + event->pos()); + move(pos() - mMousePressPos + event->pos()); event->accept(); } - QGraphicsWidget::mouseMoveEvent(event); + QWidget::mouseMoveEvent(event); } -void UBToolWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +void UBToolWidget::mouseReleaseEvent(QMouseEvent *event) { mShouldMoveWidget = false; if (event->pos().x() >= 0 && event->pos().x() < sClosePixmap->width() && event->pos().y() >= 0 && event->pos().y() < sClosePixmap->height()) { - remove(); + UBApplication::boardController->removeTool(this); event->accept(); } - else if (mGraphicsWidgetItem->canBeContent() && event->pos().x() >= mContentMargin && event->pos().x() < mContentMargin + sUnpinPixmap->width() && event->pos().y() >= 0 && event->pos().y() < sUnpinPixmap->height()) { + else if (mToolWidget->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 - QGraphicsWidget::mouseReleaseEvent(event); /* don't propgate to parent, the widget is deleted in UBApplication */ + QWidget::mouseReleaseEvent(event); /* don't propgate to parent, the widget is deleted in UBApplication */ } -void UBToolWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +void UBToolWidget::paintEvent(QPaintEvent *event) { - QGraphicsWidget::paint(painter, option, widget); - - /* painting lightweight toolbar */ - if (isActiveWindow()) { - painter->setRenderHint(QPainter::Antialiasing); - painter->setPen(Qt::NoPen); - painter->setBrush(QColor(127, 127, 127, 127)); - - painter->drawRoundedRect(QRectF(sClosePixmap->width() / 2, sClosePixmap->height() / 2, preferredWidth() - sClosePixmap->width(), mFrameWidth), mFrameWidth / 2, mFrameWidth / 2); - painter->drawPixmap(0, 0, *sClosePixmap); - if (mGraphicsWidgetItem->canBeContent()) - painter->drawPixmap(mContentMargin, 0, *sUnpinPixmap); + QWidget::paintEvent(event); + + //if (isActiveWindow()) + { + QPainter painter(this); + 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.drawPixmap(0, 0, *sClosePixmap); + + if (mToolWidget->canBeContent()) + painter.drawPixmap(mContentMargin, 0, *sUnpinPixmap); } } void UBToolWidget::javaScriptWindowObjectCleared() { - UBWidgetUniboardAPI *uniboardAPI = new UBWidgetUniboardAPI(UBApplication::boardController->activeScene(), mGraphicsWidgetItem); + UBWidgetUniboardAPI *uniboardAPI = new UBWidgetUniboardAPI(UBApplication::boardController->activeScene(), mToolWidget); - mGraphicsWebView->page()->mainFrame()->addToJavaScriptWindowObject("sankore", uniboardAPI); + mWebView->page()->mainFrame()->addToJavaScriptWindowObject("sankore", uniboardAPI); - UBGraphicsW3CWidgetItem *graphicsW3cWidgetItem = dynamic_cast(mGraphicsWidgetItem); + UBGraphicsW3CWidgetItem *graphicsW3cWidgetItem = dynamic_cast(mToolWidget); if (graphicsW3cWidgetItem) { UBW3CWidgetAPI* widgetAPI = new UBW3CWidgetAPI(graphicsW3cWidgetItem); - mGraphicsWebView->page()->mainFrame()->addToJavaScriptWindowObject("widget", widgetAPI); + mWebView->page()->mainFrame()->addToJavaScriptWindowObject("widget", widgetAPI); } } -UBGraphicsWidgetItem* UBToolWidget::graphicsWidgetItem() const +UBGraphicsWidgetItem* UBToolWidget::toolWidget() const { - return mGraphicsWidgetItem; + return mToolWidget; } -QPointF UBToolWidget::naturalCenter() const +QPoint UBToolWidget::naturalCenter() const { - if (mGraphicsWebView) - return mGraphicsWebView->geometry().center(); + if (mWebView) + return mWebView->geometry().center(); else - return QPointF(0, 0); -} - -void UBToolWidget::centerOn(const QPointF& pos) -{ - QGraphicsWidget::setPos(pos - QPointF(preferredWidth() / 2, preferredHeight() / 2)); -} - -void UBToolWidget::remove() -{ - mGraphicsWebView->setHtml(QString()); - scene()->removeItem(this); -} - -UBGraphicsScene* UBToolWidget::scene() -{ - return qobject_cast(QGraphicsItem::scene()); -} - -QPointF UBToolWidget::pos() const -{ - return QPointF(QGraphicsItem::pos().x() + mContentMargin * scale(), QGraphicsItem::pos().y() + mContentMargin * scale()); -} - -void UBToolWidget::setPos(const QPointF &point) -{ - UBToolWidget::setPos(point.x(), point.y()); -} - -void UBToolWidget::setPos(qreal x, qreal y) -{ - QGraphicsItem::setPos(x - mContentMargin * scale(), y - mContentMargin * scale()); + return QPoint(0, 0); } -int UBToolWidget::type() const +void UBToolWidget::centerOn(const QPoint& pos) { - return Type; + QWidget::move(pos - QPoint(width() / 2, height() / 2)); } diff --git a/src/gui/UBToolWidget.h b/src/gui/UBToolWidget.h index 94939d0f..df171ae3 100644 --- a/src/gui/UBToolWidget.h +++ b/src/gui/UBToolWidget.h @@ -17,60 +17,51 @@ #define UBTOOLWIDGET_H_ #include -#include -#include "core/UB.h" -class UBGraphicsScene; class UBGraphicsWidgetItem; +class QWidget; +class UBGraphicsScene; +class QWebView; -class UBToolWidget : public QGraphicsWidget +class UBToolWidget : public QWidget { - Q_OBJECT + Q_OBJECT; public: - UBToolWidget(const QUrl& pUrl, QGraphicsItem *pParent = 0); - UBToolWidget(UBGraphicsWidgetItem* pGraphicsWidgetItem, QGraphicsItem *pParent = 0); + UBToolWidget(const QUrl& pUrl, QWidget* pParent = 0); + UBToolWidget(UBGraphicsWidgetItem* pWidget, QWidget* pParent = 0); virtual ~UBToolWidget(); - UBGraphicsWidgetItem* graphicsWidgetItem() const; - QPointF naturalCenter() const; + void centerOn(const QPoint& pos); - void centerOn(const QPointF& pos); - void remove(); + QPoint naturalCenter() const; - virtual UBGraphicsScene* scene(); - virtual QPointF pos() const; - virtual void setPos(const QPointF &point); - virtual void setPos(qreal x, qreal y); - virtual int type() const; - - enum - { - Type = UBGraphicsItemType::ToolWidgetItemType - }; + UBGraphicsWidgetItem *toolWidget() const; protected: void initialize(); + virtual void paintEvent(QPaintEvent *event); + + virtual void mousePressEvent(QMouseEvent *event); + virtual void mouseMoveEvent(QMouseEvent *event); + virtual void mouseReleaseEvent(QMouseEvent *event); - virtual bool event(QEvent *event); - virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual bool eventFilter(QObject *obj, QEvent *event); private slots: void javaScriptWindowObjectCleared(); protected: - bool mShouldMoveWidget; - int mContentMargin; - int mFrameWidth; - QGraphicsWebView *mGraphicsWebView; - UBGraphicsWidgetItem *mGraphicsWidgetItem; - QPointF mMousePressPos; + QWebView *mWebView; + UBGraphicsWidgetItem *mToolWidget; static QPixmap *sClosePixmap; static QPixmap *sUnpinPixmap; + + QPoint mMousePressPos; + bool mShouldMoveWidget; + int mContentMargin; + int mFrameWidth; }; #endif /* UBTOOLWIDGET_H_ */