25 #include <ktemporaryfile.h>
26 #include <kio/netaccess.h>
29 #include <QApplication>
32 #include <QImageWriter>
38 #include <QSvgGenerator>
43 #define DBG_IEM "UMLViewImageExporterModel"
58 if (!s_supportedImageTypesList.size()) {
61 Q_FOREACH(
const QByteArray& it, qImageFormats) {
62 const QString format = it.toLower();
63 if (!s_supportedImageTypesList.contains(format))
64 s_supportedImageTypesList << format;
67 if (!s_supportedImageTypesList.contains(
"dot"))
68 s_supportedImageTypesList <<
"dot";
69 if (!s_supportedImageTypesList.contains(
"eps"))
70 s_supportedImageTypesList <<
"eps";
71 if (!s_supportedImageTypesList.contains(
"svg"))
72 s_supportedImageTypesList <<
"svg";
74 s_supportedImageTypesList.sort();
76 return s_supportedImageTypesList;
87 if (!s_supportedMimeTypesList.size()) {
89 for (QStringList::ConstIterator it = imageTypes.begin(); it != imageTypes.end(); ++it) {
91 if (!mimeType.isNull())
92 s_supportedMimeTypesList.append(mimeType);
96 return s_supportedMimeTypesList;
109 const QString imgType = imageType.toLower();
110 if (QString(
"bmp") == imgType)
return "image/bmp";
111 if (QString(
"dot") == imgType)
return "image/x-dot";
112 if (QString(
"jpeg") == imgType)
return "image/jpeg";
113 if (QString(
"pbm") == imgType)
return "image/x-portable-bitmap";
114 if (QString(
"pgm") == imgType)
return "image/x-portable-graymap";
115 if (QString(
"png") == imgType)
return "image/png";
116 if (QString(
"ppm") == imgType)
return "image/x-portable-pixmap";
117 if (QString(
"xbm") == imgType)
return "image/x-xbitmap";
118 if (QString(
"xpm") == imgType)
return "image/x-xpixmap";
119 if (QString(
"eps") == imgType)
return "image/x-eps";
120 if (QString(
"svg") == imgType)
return "image/svg+xml";
134 if (QString(
"image/bmp") == mimeType)
return "bmp";
135 if (QString(
"image/x-dot") == mimeType)
return "dot";
136 if (QString(
"image/jpeg") == mimeType)
return "jpeg";
137 if (QString(
"image/x-portable-bitmap") == mimeType)
return "pbm";
138 if (QString(
"image/x-portable-graymap") == mimeType)
return "pgm";
139 if (QString(
"image/png") == mimeType)
return "png";
140 if (QString(
"image/x-portable-pixmap") == mimeType)
return "ppm";
141 if (QString(
"image/x-xbitmap") == mimeType)
return "xbm";
142 if (QString(
"image/x-xpixmap") == mimeType)
return "xpm";
143 if (QString(
"image/x-eps") == mimeType)
return "eps";
144 if (QString(
"image/svg+xml") == mimeType)
return "svg";
192 foreach (
UMLView *view, views) {
193 KUrl url = directory;
194 url.addPath(getDiagramFileName(view->
umlScene(), imageType, useFolders));
197 if (!returnString.isNull()) {
199 errors.append(view->
umlScene()->
name() +
": " + returnString);
224 return i18n(
"Empty scene");
228 if (!prepareDirectory(url)) {
229 return i18n(
"Can not create directory: %1", url.directory());
236 KTemporaryFile tmpFile;
237 if (url.isLocalFile()) {
238 fileName = url.toLocalFile();
241 fileName = tmpFile.fileName();
245 if (rect.isEmpty()) {
246 return i18n(
"Can not save an empty diagram");
250 if (!exportViewTo(scene, imageType, fileName)) {
251 return i18n(
"A problem occurred while saving diagram in %1", fileName);
255 if (!url.isLocalFile()) {
256 if (!KIO::NetAccess::upload(tmpFile.fileName(), url,
UMLApp::app())) {
257 return i18n(
"There was a problem saving file: %1", url.path());
271 QString UMLViewImageExporterModel::getDiagramFileName(
UMLScene* scene,
const QString &imageType,
bool useFolders )
const
275 qApp->processEvents();
279 return scene->
name() +
'.' + imageType.toLower();;
296 bool UMLViewImageExporterModel::prepareDirectory(
const KUrl &url)
const
299 KUrl directory = url;
300 directory.setPath(
"");
303 QStringList dirs = url.directory().split(QDir::separator(), QString::SkipEmptyParts);
304 for (QStringList::ConstIterator it = dirs.constBegin() ; it != dirs.constEnd(); ++it) {
305 directory.addPath(*it);
307 if (!KIO::NetAccess::exists(directory, KIO::NetAccess::SourceSide,
UMLApp::app())) {
309 if (!KIO::NetAccess::mkdir(directory,
UMLApp::app())) {
327 bool UMLViewImageExporterModel::exportViewTo(
UMLScene* scene,
const QString &imageType,
const QString &fileName)
const
338 if (imageMimeType ==
"image/x-dot") {
339 if (!exportViewToDot(scene, fileName)) {
342 }
else if (imageMimeType ==
"image/x-eps") {
343 if (!exportViewToEps(scene, fileName,
true)) {
346 }
else if (imageMimeType ==
"image/svg+xml") {
347 if (!exportViewToSvg(scene, fileName)) {
351 if (!exportViewToPixmap(scene, imageType, fileName)) {
367 bool UMLViewImageExporterModel::exportViewToDot(
UMLScene* scene,
const QString &fileName)
const
375 bool result = dot.
createDotFile(scene, fileName, QLatin1String(
"export"));
377 DEBUG(
DBG_IEM) <<
"saving to file " << fileName << result;
391 bool UMLViewImageExporterModel::exportViewToEps(
UMLScene* scene,
const QString &fileName,
bool isEPS)
const
398 bool exportSuccessful =
true;
409 if (isEPS ==
false) {
410 printer =
new QPrinter(QPrinter::PrinterResolution);
412 printer =
new QPrinter(QPrinter::ScreenResolution);
414 printer->setOutputFileName(fileName);
415 printer->setOutputFormat(QPrinter::PostScriptFormat);
416 printer->setColorMode(QPrinter::Color);
420 QPainter *painter =
new QPainter(printer);
428 painter->translate(-rect.x(), -rect.y());
431 int resolution = printer->resolution();
438 rect.setWidth(
int(ceil(rect.width() * 72.0/resolution)));
439 rect.setHeight(
int(ceil(rect.height() * 72.0/resolution)));
440 exportSuccessful = fixEPS(fileName, rect);
445 return exportSuccessful;
456 bool UMLViewImageExporterModel::exportViewToSvg(
UMLScene* scene,
const QString &fileName)
const
463 bool exportSuccessful;
466 QSvgGenerator generator;
467 generator.setFileName(fileName);
468 generator.setSize(rect.toRect().size());
469 generator.setViewBox(QRect(0, 0, rect.width(), rect.height()));
470 QPainter painter(&generator);
480 painter.translate(0, 0);
485 exportSuccessful =
true;
491 DEBUG(
DBG_IEM) <<
"saving to file " << fileName <<
" successful=" << exportSuccessful;
492 return exportSuccessful;
505 bool UMLViewImageExporterModel::exportViewToPixmap(
UMLScene* scene,
const QString &imageType,
const QString &fileName)
const
513 QPixmap diagram(rect.width(), rect.height());
515 bool exportSuccessful = diagram.save(fileName, qPrintable(imageType.toUpper()));
517 <<
", imageType=" << imageType
518 <<
", width=" << rect.width()
519 <<
", height=" << rect.height()
520 <<
", successful=" << exportSuccessful;
521 return exportSuccessful;
532 bool UMLViewImageExporterModel::fixEPS(
const QString &fileName,
const QRectF& rect)
const
535 QFile epsfile(fileName);
536 if (! epsfile.open(QIODevice::ReadOnly)) {
540 QTextStream ts(&epsfile);
541 QString fileContent = ts.readAll();
545 QRegExp rx(
"%%BoundingBox:\\s*(-?[\\d\\.:]+)\\s*(-?[\\d\\.:]+)\\s*(-?[\\d\\.:]+)\\s*(-?[\\d\\.:]+)");
546 const int pos = rx.indexIn(fileContent);
548 uError() << fileName <<
": cannot find %%BoundingBox";
553 if (! epsfile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
554 uError() << fileName <<
": cannot open file for writing";
560 const double epsleft = rx.cap(1).toFloat();
561 const double epstop = rx.cap(4).toFloat();
562 const int left = int(floor(epsleft));
563 const int right = int(ceil(epsleft)) + rect.width();
564 const int top = int(ceil(epstop)) + 1;
565 const int bottom = int(floor(epstop)) - rect.height() + 1;
568 fileContent.replace(pos, rx.cap(0).length(),
569 QString(
"%%BoundingBox: %1 %2 %3 %4").arg(left).arg(bottom).arg(right).arg(top));
The class DotGenerator provides export of diagrams as dot files.
QString treeViewBuildDiagramName(Uml::ID::Type id)
Build the diagram name from the tree view.
QString name() const
Return the name of the diagram.
UMLView instances represent diagrams.
static UMLApp * app()
Get the last created instance of this class.
QList< UMLView * > UMLViewList
virtual ~UMLViewImageExporterModel()
Destructor for UMLViewImageExporterModel.
bool createDotFile(UMLScene *scene, const QString &fileName, const QString &variant="default")
Create dot file using displayed widgets and associations of the provided scene.
UMLDoc * document() const
Returns a pointer to the current document connected to the KMainWindow instance.
static QStringList supportedImageTypes()
Returns a QStringList containing all the supported image types to use when exporting.
static QStringList supportedMimeTypes()
Returns a QStringList containing all the supported mime types to use when exporting.
static QString imageTypeToMimeType(const QString &imageType)
Returns the mime type for an image type.
QString exportView(UMLScene *scene, const QString &imageType, const KUrl &url) const
Exports the view to the url using the 'imageType' for the image.
QRectF diagramRect()
Gets the smallest area to print.
The base class for UML application windows.
void getDiagram(QPixmap &diagram, const QRectF &rect)
Returns the PNG picture of the paste operation.
UMLViewList viewIterator()
Return the list of views for this document.
void forceUpdateWidgetFontMetrics(QPainter *painter)
Force the widget font metrics to be updated next time the widgets are drawn.
UMLScene * umlScene() const
Getter for the scene.
UMLViewImageExporterModel()
Constructor for UMLViewImageExporterModel.
Exports an UMLView in various image formats.
static QString mimeTypeToImageType(const QString &mimeType)
Returns the image type for a mime type.
void clearSelected()
Clear the selected widgets list.
QStringList exportAllViews(const QString &imageType, const KUrl &directory, bool useFolders) const
Exports all the views in the document to the directory specified in the url using the 'imageType' for...
#define DEBUG_REGISTER(src)
UMLScene instances represent diagrams.
Uml::ID::Type ID() const
Returns the ID of the diagram.