| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QFile> |
| 26 |
#include <QGraphicsScene> |
| 27 |
#include <QGraphicsSvgItem> |
| 28 |
#include <QGraphicsView> |
| 29 |
#include <QMessageBox> |
| 30 |
#include <QMouseEvent> |
| 31 |
#include <QPrinter> |
| 32 |
#include <QPrintDialog> |
| 33 |
#include <QPrintPreviewDialog> |
| 34 |
#include <QProcess> |
| 35 |
#include <QSvgRenderer> |
| 36 |
#include <QScrollBar> |
| 37 |
#include <QThread> |
| 38 |
|
| 39 |
#include <FCConfig.h> |
| 40 |
|
| 41 |
#include <App/Application.h> |
| 42 |
#include <App/Document.h> |
| 43 |
|
| 44 |
#include "GraphvizView.h" |
| 45 |
#include "GraphicsViewZoom.h" |
| 46 |
#include "FileDialog.h" |
| 47 |
#include "MainWindow.h" |
| 48 |
|
| 49 |
|
| 50 |
using namespace Gui; |
| 51 |
namespace sp = std::placeholders; |
| 52 |
|
| 53 |
namespace Gui |
| 54 |
{ |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief The GraphvizWorker class |
| 58 |
* |
| 59 |
* Implements a QThread class that does the actual conversion from dot to |
| 60 |
* svg. All critical communication is done using queued signals. |
| 61 |
* |
| 62 |
*/ |
| 63 |
|
| 64 |
class GraphvizWorker: public QThread |
| 65 |
{ |
| 66 |
Q_OBJECT |
| 67 |
public: |
| 68 |
explicit GraphvizWorker(QObject* parent = nullptr) |
| 69 |
: QThread(parent) |
| 70 |
{} |
| 71 |
|
| 72 |
~GraphvizWorker() override |
| 73 |
{ |
| 74 |
dotProc.moveToThread(this); |
| 75 |
unflattenProc.moveToThread(this); |
| 76 |
} |
| 77 |
|
| 78 |
void setData(const QByteArray& data) |
| 79 |
{ |
| 80 |
str = data; |
| 81 |
} |
| 82 |
|
| 83 |
void startThread() |
| 84 |
{ |
| 85 |
// This doesn't actually run a thread but calls the function |
| 86 |
// directly in the main thread. |
| 87 |
// This is needed because embedding a QProcess into a QThread |
| 88 |
// causes some problems with Qt5. |
| 89 |
run(); |
| 90 |
// Can't use the finished() signal of QThread |
| 91 |
Q_EMIT emitFinished(); |
| 92 |
} |
| 93 |
|
| 94 |
void run() override |
| 95 |
{ |
| 96 |
QByteArray preprocessed = str; |
| 97 |
|
| 98 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 99 |
"User parameter:BaseApp/Preferences/DependencyGraph" |
| 100 |
); |
| 101 |
if (hGrp->GetBool("Unflatten", true)) { |
| 102 |
// Write data to unflatten process |
| 103 |
unflattenProc.write(str); |
| 104 |
unflattenProc.closeWriteChannel(); |
| 105 |
// no error handling: unflatten is optional |
| 106 |
unflattenProc.waitForFinished(); |
| 107 |
preprocessed = unflattenProc.readAll(); |
| 108 |
} |
| 109 |
else { |
| 110 |
unflattenProc.closeWriteChannel(); |
| 111 |
unflattenProc.waitForFinished(); |
| 112 |
} |
| 113 |
|
| 114 |
dotProc.write(preprocessed); |
| 115 |
dotProc.closeWriteChannel(); |
| 116 |
if (!dotProc.waitForFinished()) { |
| 117 |
Q_EMIT error(); |
| 118 |
quit(); |
| 119 |
} |
| 120 |
|
| 121 |
// Emit result; it will get queued for processing in the main thread |
| 122 |
Q_EMIT svgFileRead(dotProc.readAll()); |
| 123 |
} |
| 124 |
|
| 125 |
QProcess* dotProcess() |
| 126 |
{ |
| 127 |
return &dotProc; |
| 128 |
} |
| 129 |
|
| 130 |
QProcess* unflattenProcess() |
| 131 |
{ |
| 132 |
return &unflattenProc; |
| 133 |
} |
| 134 |
|
| 135 |
Q_SIGNALS: |
| 136 |
void svgFileRead(const QByteArray& data); |
| 137 |
void error(); |
| 138 |
void emitFinished(); |
| 139 |
|
| 140 |
private: |
| 141 |
QProcess dotProc, unflattenProc; |
| 142 |
QByteArray str, flatStr; |
| 143 |
}; |
| 144 |
|
| 145 |
// Simple wrapper around QGraphicsView to make panning possible |
| 146 |
class GraphvizGraphicsView final: public QGraphicsView |
| 147 |
{ |
| 148 |
public: |
| 149 |
GraphvizGraphicsView(QGraphicsScene* scene, QWidget* parent); |
| 150 |
~GraphvizGraphicsView() override = default; |
| 151 |
|
| 152 |
GraphvizGraphicsView(const GraphvizGraphicsView&) = delete; |
| 153 |
GraphvizGraphicsView(GraphvizGraphicsView&&) = delete; |
| 154 |
GraphvizGraphicsView& operator=(const GraphvizGraphicsView&) = delete; |
| 155 |
GraphvizGraphicsView& operator=(GraphvizGraphicsView&&) = delete; |
| 156 |
|
| 157 |
protected: |
| 158 |
void mousePressEvent(QMouseEvent* event) override; |
| 159 |
void mouseMoveEvent(QMouseEvent* event) override; |
| 160 |
void mouseReleaseEvent(QMouseEvent* event) override; |
| 161 |
|
| 162 |
private: |
| 163 |
bool isPanning {false}; |
| 164 |
QPoint panStart; |
| 165 |
}; |
| 166 |
|
| 167 |
GraphvizGraphicsView::GraphvizGraphicsView(QGraphicsScene* scene, QWidget* parent) |
| 168 |
: QGraphicsView(scene, parent) |
| 169 |
{} |
| 170 |
|
| 171 |
void GraphvizGraphicsView::mousePressEvent(QMouseEvent* e) |
| 172 |
{ |
| 173 |
if (e && e->button() == Qt::LeftButton) { |
| 174 |
isPanning = true; |
| 175 |
panStart = e->pos(); |
| 176 |
e->accept(); |
| 177 |
QApplication::setOverrideCursor(Qt::ClosedHandCursor); |
| 178 |
} |
| 179 |
|
| 180 |
QGraphicsView::mousePressEvent(e); |
| 181 |
|
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
void GraphvizGraphicsView::mouseMoveEvent(QMouseEvent* e) |
| 186 |
{ |
| 187 |
if (!e) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if (isPanning) { |
| 192 |
auto* horizontalScrollbar = horizontalScrollBar(); |
| 193 |
auto* verticalScrollbar = verticalScrollBar(); |
| 194 |
if (!horizontalScrollbar || !verticalScrollbar) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
auto direction = e->pos() - panStart; |
| 199 |
horizontalScrollbar->setValue(horizontalScrollbar->value() - direction.x()); |
| 200 |
verticalScrollbar->setValue(verticalScrollbar->value() - direction.y()); |
| 201 |
|
| 202 |
panStart = e->pos(); |
| 203 |
e->accept(); |
| 204 |
} |
| 205 |
|
| 206 |
QGraphicsView::mouseMoveEvent(e); |
| 207 |
|
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
void GraphvizGraphicsView::mouseReleaseEvent(QMouseEvent* e) |
| 212 |
{ |
| 213 |
if (e && e->button() & Qt::LeftButton) { |
| 214 |
isPanning = false; |
| 215 |
QApplication::restoreOverrideCursor(); |
| 216 |
e->accept(); |
| 217 |
} |
| 218 |
|
| 219 |
QGraphicsView::mouseReleaseEvent(e); |
| 220 |
|
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
} // namespace Gui |
| 225 |
|
| 226 |
/* TRANSLATOR Gui::GraphvizView */ |
| 227 |
|
| 228 |
GraphvizView::GraphvizView(App::Document& _doc, QWidget* parent) |
| 229 |
: MDIView(nullptr, parent) |
| 230 |
, doc(_doc) |
| 231 |
, nPending(0) |
| 232 |
{ |
| 233 |
// Create scene |
| 234 |
scene = new QGraphicsScene(); |
| 235 |
|
| 236 |
// Create item to hold the graph |
| 237 |
svgItem = new QGraphicsSvgItem(); |
| 238 |
renderer = new QSvgRenderer(this); |
| 239 |
svgItem->setSharedRenderer(renderer); |
| 240 |
scene->addItem(svgItem); |
| 241 |
|
| 242 |
// Create view and zoomer object |
| 243 |
view = new GraphvizGraphicsView(scene, this); |
| 244 |
zoomer = new GraphicsViewZoom(view); |
| 245 |
zoomer->set_modifiers(Qt::NoModifier); |
| 246 |
view->show(); |
| 247 |
|
| 248 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 249 |
"User parameter:BaseApp/Preferences/View" |
| 250 |
); |
| 251 |
bool on = hGrp->GetBool("InvertZoom", true); |
| 252 |
zoomer->set_zoom_inverted(on); |
| 253 |
|
| 254 |
// Set central widget to view |
| 255 |
setCentralWidget(view); |
| 256 |
|
| 257 |
// Create worker thread |
| 258 |
thread = new GraphvizWorker(this); |
| 259 |
connect(thread, &GraphvizWorker::emitFinished, this, &GraphvizView::done); |
| 260 |
connect(thread, &GraphvizWorker::finished, this, &GraphvizView::done); |
| 261 |
connect(thread, &GraphvizWorker::error, this, &GraphvizView::error); |
| 262 |
connect(thread, &GraphvizWorker::svgFileRead, this, &GraphvizView::svgFileRead); |
| 263 |
|
| 264 |
// NOLINTBEGIN |
| 265 |
// Connect signal from document |
| 266 |
recomputeConnection = _doc.signalRecomputed.connect( |
| 267 |
std::bind(&GraphvizView::updateSvgItem, this, sp::_1) |
| 268 |
); |
| 269 |
undoConnection = _doc.signalUndo.connect(std::bind(&GraphvizView::updateSvgItem, this, sp::_1)); |
| 270 |
redoConnection = _doc.signalRedo.connect(std::bind(&GraphvizView::updateSvgItem, this, sp::_1)); |
| 271 |
// NOLINTEND |
| 272 |
|
| 273 |
updateSvgItem(_doc); |
| 274 |
} |
| 275 |
|
| 276 |
GraphvizView::~GraphvizView() |
| 277 |
{ |
| 278 |
delete scene; |
| 279 |
delete view; |
| 280 |
} |
| 281 |
|
| 282 |
void GraphvizView::updateSvgItem(const App::Document& doc) |
| 283 |
{ |
| 284 |
nPending++; |
| 285 |
|
| 286 |
// Skip if thread is working now |
| 287 |
if (nPending > 1) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 292 |
"User parameter:BaseApp/Preferences/Paths" |
| 293 |
); |
| 294 |
QProcess* dotProc = thread->dotProcess(); |
| 295 |
QProcess* flatProc = thread->unflattenProcess(); |
| 296 |
QStringList args, flatArgs; |
| 297 |
// TODO: Make -Granksep flag value variable depending on number of edges, |
| 298 |
// the downside is that the value affects all subgraphs |
| 299 |
args << QLatin1String("-Granksep=2") << QLatin1String("-Goutputorder=edgesfirst") |
| 300 |
<< QLatin1String("-Gsplines=ortho") << QLatin1String("-Tsvg"); |
| 301 |
flatArgs << QLatin1String("-c2 -l2"); |
| 302 |
auto dot = QStringLiteral("dot"); |
| 303 |
auto unflatten = QStringLiteral("unflatten"); |
| 304 |
auto path = QString::fromUtf8(hGrp->GetASCII("Graphviz").c_str()); |
| 305 |
bool pathChanged = false; |
| 306 |
QDir dir; |
| 307 |
if (!path.isEmpty()) { |
| 308 |
dir = QDir(path); |
| 309 |
dot = dir.filePath(QStringLiteral("dot")); |
| 310 |
unflatten = dir.filePath(QStringLiteral("unflatten")); |
| 311 |
} |
| 312 |
dotProc->setEnvironment(QProcess::systemEnvironment()); |
| 313 |
flatProc->setEnvironment(QProcess::systemEnvironment()); |
| 314 |
do { |
| 315 |
flatProc->start(unflatten, flatArgs); |
| 316 |
bool value = flatProc->waitForStarted(); |
| 317 |
Q_UNUSED(value); // quieten code analyzer |
| 318 |
dotProc->start(dot, args); |
| 319 |
if (!dotProc->waitForStarted()) { |
| 320 |
int ret = QMessageBox::warning( |
| 321 |
Gui::getMainWindow(), |
| 322 |
tr("Graphviz not found"), |
| 323 |
QStringLiteral( |
| 324 |
"<html><head/><body>%1 " |
| 325 |
"<a href=\"https://www.freecad.org/wiki/Std_DependencyGraph\">%2" |
| 326 |
"</a><p>%3</p></body></html>" |
| 327 |
) |
| 328 |
.arg( |
| 329 |
tr("Graphviz couldn't be found on your system."), |
| 330 |
tr("Read more about it here."), |
| 331 |
tr("Do you want to specify its installation path if it's already installed?") |
| 332 |
), |
| 333 |
QMessageBox::Yes, |
| 334 |
QMessageBox::No |
| 335 |
); |
| 336 |
if (ret == QMessageBox::No) { |
| 337 |
disconnectSignals(); |
| 338 |
return; |
| 339 |
} |
| 340 |
path = QFileDialog::getExistingDirectory( |
| 341 |
Gui::getMainWindow(), |
| 342 |
tr("Graphviz installation path") |
| 343 |
); |
| 344 |
if (path.isEmpty()) { |
| 345 |
disconnectSignals(); |
| 346 |
return; |
| 347 |
} |
| 348 |
else { |
| 349 |
dir = QDir(path); |
| 350 |
dot = dir.filePath(QStringLiteral("dot")); |
| 351 |
unflatten = dir.filePath(QStringLiteral("unflatten")); |
| 352 |
pathChanged = true; |
| 353 |
} |
| 354 |
} |
| 355 |
else { |
| 356 |
if (pathChanged) { |
| 357 |
hGrp->SetASCII("Graphviz", (const char*)path.toUtf8()); |
| 358 |
} |
| 359 |
break; |
| 360 |
} |
| 361 |
} while (true); |
| 362 |
|
| 363 |
// Create graph in dot format |
| 364 |
std::stringstream stream; |
| 365 |
doc.exportGraphviz(stream); |
| 366 |
graphCode = stream.str(); |
| 367 |
|
| 368 |
// Update worker thread, and start it |
| 369 |
thread->setData(QByteArray(graphCode.c_str(), graphCode.size())); |
| 370 |
thread->startThread(); |
| 371 |
} |
| 372 |
|
| 373 |
void GraphvizView::svgFileRead(const QByteArray& data) |
| 374 |
{ |
| 375 |
// Update renderer with new SVG file, and give message if something went wrong |
| 376 |
if (renderer->load(data)) { |
| 377 |
svgItem->setSharedRenderer(renderer); |
| 378 |
} |
| 379 |
else { |
| 380 |
QMessageBox::warning( |
| 381 |
getMainWindow(), |
| 382 |
tr("Graphviz failed"), |
| 383 |
tr("Graphviz failed to create an image file") |
| 384 |
); |
| 385 |
disconnectSignals(); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void GraphvizView::error() |
| 390 |
{ |
| 391 |
// If the worker fails for some reason, stop giving it more data later |
| 392 |
disconnectSignals(); |
| 393 |
} |
| 394 |
|
| 395 |
void GraphvizView::done() |
| 396 |
{ |
| 397 |
nPending--; |
| 398 |
if (nPending > 0) { |
| 399 |
nPending = 0; |
| 400 |
updateSvgItem(doc); |
| 401 |
thread->startThread(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
void GraphvizView::disconnectSignals() |
| 406 |
{ |
| 407 |
recomputeConnection.disconnect(); |
| 408 |
undoConnection.disconnect(); |
| 409 |
redoConnection.disconnect(); |
| 410 |
} |
| 411 |
|
| 412 |
#include <QObject> |
| 413 |
#include <QGraphicsView> |
| 414 |
|
| 415 |
QByteArray GraphvizView::exportGraph(const QString& format) |
| 416 |
{ |
| 417 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 418 |
"User parameter:BaseApp/Preferences/Paths" |
| 419 |
); |
| 420 |
QProcess dotProc, flatProc; |
| 421 |
QStringList args, flatArgs; |
| 422 |
args << QStringLiteral("-T%1").arg(format); |
| 423 |
flatArgs << QLatin1String("-c2 -l2"); |
| 424 |
|
| 425 |
#ifdef FC_OS_LINUX |
| 426 |
QString path = QString::fromUtf8(hGrp->GetASCII("Graphviz", "/usr/bin").c_str()); |
| 427 |
#else |
| 428 |
QString path = QString::fromUtf8(hGrp->GetASCII("Graphviz").c_str()); |
| 429 |
#endif |
| 430 |
|
| 431 |
#ifdef FC_OS_WIN32 |
| 432 |
QString exe = QStringLiteral("\"%1/dot\"").arg(path); |
| 433 |
QString unflatten = QStringLiteral("\"%1/unflatten\"").arg(path); |
| 434 |
#else |
| 435 |
QString exe = QStringLiteral("%1/dot").arg(path); |
| 436 |
QString unflatten = QStringLiteral("%1/unflatten").arg(path); |
| 437 |
#endif |
| 438 |
|
| 439 |
dotProc.setEnvironment(QProcess::systemEnvironment()); |
| 440 |
dotProc.start(exe, args); |
| 441 |
if (!dotProc.waitForStarted()) { |
| 442 |
return {}; |
| 443 |
} |
| 444 |
|
| 445 |
ParameterGrp::handle depGrp = App::GetApplication().GetParameterGroupByPath( |
| 446 |
"User parameter:BaseApp/Preferences/DependencyGraph" |
| 447 |
); |
| 448 |
if (depGrp->GetBool("Unflatten", true)) { |
| 449 |
flatProc.setEnvironment(QProcess::systemEnvironment()); |
| 450 |
flatProc.start(unflatten, flatArgs); |
| 451 |
if (!flatProc.waitForStarted()) { |
| 452 |
return {}; |
| 453 |
} |
| 454 |
flatProc.write(graphCode.c_str(), graphCode.size()); |
| 455 |
flatProc.closeWriteChannel(); |
| 456 |
if (!flatProc.waitForFinished()) { |
| 457 |
return {}; |
| 458 |
} |
| 459 |
|
| 460 |
dotProc.write(flatProc.readAll()); |
| 461 |
} |
| 462 |
else { |
| 463 |
dotProc.write(graphCode.c_str(), graphCode.size()); |
| 464 |
} |
| 465 |
|
| 466 |
dotProc.closeWriteChannel(); |
| 467 |
if (!dotProc.waitForFinished()) { |
| 468 |
return {}; |
| 469 |
} |
| 470 |
|
| 471 |
return dotProc.readAll(); |
| 472 |
} |
| 473 |
|
| 474 |
bool GraphvizView::onMsg(const char* pMsg, const char**) |
| 475 |
{ |
| 476 |
if (strcmp("Save", pMsg) == 0 || strcmp("SaveAs", pMsg) == 0) { |
| 477 |
QList<QPair<QString, QString>> formatMap; |
| 478 |
formatMap << qMakePair( |
| 479 |
QStringLiteral("%1 (*.gv)").arg(tr("Graphviz format")), |
| 480 |
QStringLiteral("gv") |
| 481 |
); |
| 482 |
formatMap |
| 483 |
<< qMakePair(QStringLiteral("%1 (*.png)").arg(tr("PNG format")), QStringLiteral("png")); |
| 484 |
formatMap << qMakePair( |
| 485 |
QStringLiteral("%1 (*.bmp)").arg(tr("Bitmap format")), |
| 486 |
QStringLiteral("bmp") |
| 487 |
); |
| 488 |
formatMap |
| 489 |
<< qMakePair(QStringLiteral("%1 (*.gif)").arg(tr("GIF format")), QStringLiteral("gif")); |
| 490 |
formatMap |
| 491 |
<< qMakePair(QStringLiteral("%1 (*.jpg)").arg(tr("JPG format")), QStringLiteral("jpg")); |
| 492 |
formatMap |
| 493 |
<< qMakePair(QStringLiteral("%1 (*.svg)").arg(tr("SVG format")), QStringLiteral("svg")); |
| 494 |
formatMap |
| 495 |
<< qMakePair(QStringLiteral("%1 (*.pdf)").arg(tr("PDF format")), QStringLiteral("pdf")); |
| 496 |
|
| 497 |
QStringList filter; |
| 498 |
for (const auto& it : std::as_const(formatMap)) { |
| 499 |
filter << it.first; |
| 500 |
} |
| 501 |
|
| 502 |
QString selectedFilter; |
| 503 |
QString fn = Gui::FileDialog::getSaveFileName( |
| 504 |
this, |
| 505 |
tr("Export graph"), |
| 506 |
QString(), |
| 507 |
filter.join(QLatin1String(";;")), |
| 508 |
&selectedFilter |
| 509 |
); |
| 510 |
if (!fn.isEmpty()) { |
| 511 |
QString format; |
| 512 |
for (const auto& it : std::as_const(formatMap)) { |
| 513 |
if (selectedFilter == it.first) { |
| 514 |
format = it.second; |
| 515 |
break; |
| 516 |
} |
| 517 |
} |
| 518 |
QByteArray buffer; |
| 519 |
if (format == QLatin1String("gv")) { |
| 520 |
std::stringstream str; |
| 521 |
doc.exportGraphviz(str); |
| 522 |
buffer = QByteArray::fromStdString(str.str()); |
| 523 |
} |
| 524 |
else { |
| 525 |
buffer = exportGraph(format); |
| 526 |
} |
| 527 |
if (buffer.isEmpty()) { |
| 528 |
return true; |
| 529 |
} |
| 530 |
QFile file(fn); |
| 531 |
if (file.open(QFile::WriteOnly)) { |
| 532 |
file.write(buffer); |
| 533 |
file.close(); |
| 534 |
} |
| 535 |
} |
| 536 |
return true; |
| 537 |
} |
| 538 |
else if (strcmp("Print", pMsg) == 0) { |
| 539 |
print(); |
| 540 |
return true; |
| 541 |
} |
| 542 |
else if (strcmp("PrintPreview", pMsg) == 0) { |
| 543 |
printPreview(); |
| 544 |
return true; |
| 545 |
} |
| 546 |
else if (strcmp("PrintPdf", pMsg) == 0) { |
| 547 |
printPdf(); |
| 548 |
return true; |
| 549 |
} |
| 550 |
|
| 551 |
return false; |
| 552 |
} |
| 553 |
|
| 554 |
bool GraphvizView::onHasMsg(const char* pMsg) const |
| 555 |
{ |
| 556 |
if (strcmp("Save", pMsg) == 0) { |
| 557 |
return true; |
| 558 |
} |
| 559 |
else if (strcmp("SaveAs", pMsg) == 0) { |
| 560 |
return true; |
| 561 |
} |
| 562 |
else if (strcmp("Print", pMsg) == 0) { |
| 563 |
return true; |
| 564 |
} |
| 565 |
else if (strcmp("PrintPreview", pMsg) == 0) { |
| 566 |
return true; |
| 567 |
} |
| 568 |
else if (strcmp("PrintPdf", pMsg) == 0) { |
| 569 |
return true; |
| 570 |
} |
| 571 |
else if (strcmp("AllowsOverlayOnHover", pMsg) == 0) { |
| 572 |
return true; |
| 573 |
} |
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
void GraphvizView::print(QPrinter* printer) |
| 578 |
{ |
| 579 |
QPainter p(printer); |
| 580 |
QRect rect = printer->pageLayout().paintRectPixels(printer->resolution()); |
| 581 |
view->scene()->render(&p, rect); |
| 582 |
// QByteArray buffer = exportGraph(QStringLiteral("svg")); |
| 583 |
// QSvgRenderer svg(buffer); |
| 584 |
// svg.render(&p, rect); |
| 585 |
p.end(); |
| 586 |
} |
| 587 |
|
| 588 |
void GraphvizView::print() |
| 589 |
{ |
| 590 |
QPrinter printer(QPrinter::HighResolution); |
| 591 |
printer.setFullPage(true); |
| 592 |
printer.setPageOrientation(QPageLayout::Landscape); |
| 593 |
QPrintDialog dlg(&printer, this); |
| 594 |
if (dlg.exec() == QDialog::Accepted) { |
| 595 |
print(&printer); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
void GraphvizView::printPdf() |
| 600 |
{ |
| 601 |
QStringList filter; |
| 602 |
filter << QStringLiteral("%1 (*.pdf)").arg(tr("PDF format")); |
| 603 |
|
| 604 |
QString selectedFilter; |
| 605 |
QString fn = Gui::FileDialog::getSaveFileName( |
| 606 |
this, |
| 607 |
tr("Export graph"), |
| 608 |
QString(), |
| 609 |
filter.join(QLatin1String(";;")), |
| 610 |
&selectedFilter |
| 611 |
); |
| 612 |
if (!fn.isEmpty()) { |
| 613 |
QByteArray buffer = exportGraph(selectedFilter); |
| 614 |
if (buffer.isEmpty()) { |
| 615 |
return; |
| 616 |
} |
| 617 |
QFile file(fn); |
| 618 |
if (file.open(QFile::WriteOnly)) { |
| 619 |
file.write(buffer); |
| 620 |
file.close(); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
void GraphvizView::printPreview() |
| 626 |
{ |
| 627 |
QPrinter printer(QPrinter::HighResolution); |
| 628 |
printer.setFullPage(true); |
| 629 |
printer.setPageOrientation(QPageLayout::Landscape); |
| 630 |
|
| 631 |
QPrintPreviewDialog dlg(&printer, this); |
| 632 |
connect(&dlg, &QPrintPreviewDialog::paintRequested, this, qOverload<QPrinter*>(&GraphvizView::print)); |
| 633 |
dlg.exec(); |
| 634 |
} |
| 635 |
|
| 636 |
#include "moc_GraphvizView.cpp" |
| 637 |
#include "moc_GraphvizView-internal.cpp" |