From 143a58bccce1b8a4de9f80ce9402ac07c16471ce Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Thu, 28 Nov 2013 11:57:25 +0100 Subject: [PATCH] Added QGraphicsWebView to yosys-svgviewer --- libs/svgviewer/mainwindow.cpp | 13 ++++++++ libs/svgviewer/mainwindow.h | 1 + libs/svgviewer/svgview.cpp | 56 +++++++++++++++++++++++++---------- libs/svgviewer/svgview.h | 4 +++ 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/libs/svgviewer/mainwindow.cpp b/libs/svgviewer/mainwindow.cpp index f8f72e5c4..70da140eb 100644 --- a/libs/svgviewer/mainwindow.cpp +++ b/libs/svgviewer/mainwindow.cpp @@ -41,6 +41,7 @@ #include "mainwindow.h" #include +#include #include #include "svgview.h" @@ -60,10 +61,18 @@ MainWindow::MainWindow() menuBar()->addMenu(fileMenu); QMenu *viewMenu = new QMenu(tr("&View"), this); + + m_interactiveAction = viewMenu->addAction(tr("&Interactive")); + m_interactiveAction->setEnabled(false); + m_interactiveAction->setCheckable(true); + m_interactiveAction->setChecked(false); + connect(m_interactiveAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewInteractive(bool))); + m_backgroundAction = viewMenu->addAction(tr("&Background")); m_backgroundAction->setEnabled(false); m_backgroundAction->setCheckable(true); m_backgroundAction->setChecked(false); + m_backgroundAction->setVisible(false); connect(m_backgroundAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewBackground(bool))); m_outlineAction = viewMenu->addAction(tr("&Outline")); @@ -136,6 +145,7 @@ void MainWindow::openFile(const QString &path, bool reload) QMessageBox::critical(this, tr("Open SVG File"), QString("Could not open file '%1'.").arg(fileName)); + m_interactiveAction->setEnabled(false); m_outlineAction->setEnabled(false); m_backgroundAction->setEnabled(false); return; @@ -157,6 +167,7 @@ void MainWindow::openFile(const QString &path, bool reload) connect(m_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(reloadFile())); } + m_interactiveAction->setEnabled(true); m_outlineAction->setEnabled(true); m_backgroundAction->setEnabled(true); @@ -169,6 +180,8 @@ void MainWindow::openFile(const QString &path, bool reload) void MainWindow::reloadFile() { + // give the writer ~100 ms to finish writing + usleep(100000); openFile(m_currentPath, true); } diff --git a/libs/svgviewer/mainwindow.h b/libs/svgviewer/mainwindow.h index 5b549e92a..dfb5586d4 100644 --- a/libs/svgviewer/mainwindow.h +++ b/libs/svgviewer/mainwindow.h @@ -72,6 +72,7 @@ private: QAction *m_glAction; QAction *m_imageAction; QAction *m_highQualityAntialiasingAction; + QAction *m_interactiveAction; QAction *m_backgroundAction; QAction *m_outlineAction; diff --git a/libs/svgviewer/svgview.cpp b/libs/svgviewer/svgview.cpp index d41f542d0..8c11098b6 100644 --- a/libs/svgviewer/svgview.cpp +++ b/libs/svgviewer/svgview.cpp @@ -56,6 +56,7 @@ SvgView::SvgView(QWidget *parent) : QGraphicsView(parent) , m_renderer(Native) , m_svgItem(0) + , m_webview(0) , m_backgroundItem(0) , m_outlineItem(0) { @@ -91,35 +92,44 @@ void SvgView::openFile(const QFile &file) QGraphicsScene *s = scene(); + fn = file.fileName(); + if (fn[0] != '/') { + char cwd_buffer[4096]; + if (getcwd(cwd_buffer, 4096) != NULL) + fn = cwd_buffer + ("/" + fn); + } + bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false); bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true); + bool useWebview = (m_webview ? m_webview->isVisible() : false); s->clear(); resetTransform(); -#if 0 - QGraphicsWebView *webview = new QGraphicsWebView(); - QString fn = file.fileName(); - if (fn[0] != '/') { - char cwd_buffer[4096]; - if (getcwd(cwd_buffer, 4096) != NULL) - fn = cwd_buffer + ("/" + fn); - } - webview->load(QUrl::fromLocalFile(fn)); - webview->setResizesToContents(true); - m_svgItem = webview; -#else m_svgItem = new QGraphicsSvgItem(file.fileName()); -#endif m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape); m_svgItem->setCacheMode(QGraphicsItem::NoCache); + m_svgItem->setVisible(!useWebview); m_svgItem->setZValue(1); + s->addItem(m_svgItem); + + if (useWebview) { + m_webview = new QGraphicsWebView(); + m_webview->load(QUrl::fromLocalFile(fn)); + m_webview->setResizesToContents(true); + m_webview->setZoomFactor(0.75); + m_webview->setVisible(useWebview); + m_webview->setZValue(1); + s->addItem(m_webview); + } else + m_webview = NULL; m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect()); m_backgroundItem->setBrush(Qt::white); m_backgroundItem->setPen(Qt::NoPen); m_backgroundItem->setVisible(drawBackground); m_backgroundItem->setZValue(0); + s->addItem(m_backgroundItem); m_outlineItem = new QGraphicsRectItem(m_svgItem->boundingRect()); QPen outline(Qt::black, 2, Qt::DashLine); @@ -128,9 +138,6 @@ void SvgView::openFile(const QFile &file) m_outlineItem->setBrush(Qt::NoBrush); m_outlineItem->setVisible(drawOutline); m_outlineItem->setZValue(2); - - s->addItem(m_backgroundItem); - s->addItem(m_svgItem); s->addItem(m_outlineItem); s->setSceneRect(m_outlineItem->boundingRect().adjusted(-10, -10, 10, 10)); @@ -158,6 +165,23 @@ void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing) #endif } +void SvgView::setViewInteractive(bool enable) +{ + if (!m_svgItem) + return; + if (!m_webview) { + m_webview = new QGraphicsWebView(); + m_webview->load(QUrl::fromLocalFile(fn)); + m_webview->setResizesToContents(true); + m_webview->setZoomFactor(0.75); + m_webview->setVisible(false); + m_webview->setZValue(1); + m_svgItem->scene()->addItem(m_webview); + } + m_svgItem->setVisible(!enable); + m_webview->setVisible(enable); +} + void SvgView::setViewBackground(bool enable) { if (!m_backgroundItem) diff --git a/libs/svgviewer/svgview.h b/libs/svgviewer/svgview.h index 944401ac9..833bb52e0 100644 --- a/libs/svgviewer/svgview.h +++ b/libs/svgviewer/svgview.h @@ -42,6 +42,7 @@ #define SVGVIEW_H #include +#include QT_BEGIN_NAMESPACE class QWheelEvent; @@ -64,6 +65,7 @@ public: public slots: void setHighQualityAntialiasing(bool highQualityAntialiasing); + void setViewInteractive(bool enable); void setViewBackground(bool enable); void setViewOutline(bool enable); @@ -74,7 +76,9 @@ protected: private: RendererType m_renderer; + QString fn; QGraphicsItem *m_svgItem; + QGraphicsWebView *m_webview; QGraphicsRectItem *m_backgroundItem; QGraphicsRectItem *m_outlineItem; -- 2.30.2