Added QGraphicsWebView to yosys-svgviewer

This commit is contained in:
Clifford Wolf 2013-11-28 11:57:25 +01:00
parent 1268182f0b
commit 143a58bccc
4 changed files with 59 additions and 17 deletions

View File

@ -41,6 +41,7 @@
#include "mainwindow.h"
#include <QtGui>
#include <unistd.h>
#include <QFileSystemWatcher>
#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);
}

View File

@ -72,6 +72,7 @@ private:
QAction *m_glAction;
QAction *m_imageAction;
QAction *m_highQualityAntialiasingAction;
QAction *m_interactiveAction;
QAction *m_backgroundAction;
QAction *m_outlineAction;

View File

@ -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();
bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false);
bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true);
s->clear();
resetTransform();
#if 0
QGraphicsWebView *webview = new QGraphicsWebView();
QString fn = file.fileName();
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
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();
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)

View File

@ -42,6 +42,7 @@
#define SVGVIEW_H
#include <QGraphicsView>
#include <QGraphicsWebView>
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;