-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexportmanager.cpp
More file actions
93 lines (71 loc) · 3.25 KB
/
Copy pathexportmanager.cpp
File metadata and controls
93 lines (71 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "exportmanager.h"
#include <QPagedPaintDevice>
#include <QtGlobal>
#include <QtPrintSupport/QPrintDialog>
#include <iostream>
bool ExportManager::fileExportCommon( QQuickTextDocument* document, const QUrl& fileURL ) {
if( Q_UNLIKELY( !fileURL.isValid() ) ) {
std::cerr << "Invalid file URL: \"" << fileURL.toString().toStdString().c_str() << "\"" << std::endl;
return false;
}
writer.setFileName( fileURL.toLocalFile() );
if( Q_LIKELY( writer.write( document->textDocument() ) ) ) {
std::cout << "file written successfully" << std::endl;
return true;
} else {
std::cerr << "file written unsuccessfully" << std::endl;
return false;
}
}
void ExportManager::printCommon( const QString& docName ) {
printer.setCreator( "ScriptDragon" );
printer.setDocName( docName );
}
Q_INVOKABLE void ExportManager::textDocumentToHTMLFile( QQuickTextDocument* document, const QUrl& fileURL ) {
writer.setFormat( "HTML" );
fileExportCommon( document, fileURL );
}
Q_INVOKABLE void ExportManager::textDocumentToOpenDocumentFile( QQuickTextDocument* document, const QUrl& fileURL ) {
writer.setFormat( "ODF" );
fileExportCommon( document, fileURL );
}
Q_INVOKABLE void ExportManager::textDocumentToPlainTextFile( QQuickTextDocument* document, const QUrl& fileURL ) {
writer.setFormat( "plaintext" );
fileExportCommon( document, fileURL );
}
Q_INVOKABLE void ExportManager::textDocumentToPrintout( QQuickTextDocument* document ) {
printer.setOutputFileName( "" ); //Disable printing to file because printing to file causes printerName() to be blank.
printCommon( "Script" );
//bool shouldPrint = showPrintDialog( "Print script" ); //FIXME: Uncomment this line to cause an error.
bool shouldPrint = true;
if( Q_LIKELY( shouldPrint ) ) {
document->textDocument()->print( &printer );
}
}
Q_INVOKABLE void ExportManager::textDocumentToPDF( QQuickTextDocument* document, const QUrl& fileURL ) {
if( Q_UNLIKELY( !fileURL.isValid() ) ) {
std::cerr << "Invalid file URL: \"" << fileURL.toString().toStdString().c_str() << "\"" << std::endl;
return;
}
//-----begin page size workaround part 1-----
printer.setOutputFileName( "" ); //Disable printing to file so we can get the default printer's default page size.
QPagedPaintDevice::PageSize pageSize = printer.pageSize();
//-----end page size workaround part 1-----
printer.setOutputFileName( fileURL.toLocalFile() ); //Enable printing to file.
//-----begin page size workaround part 2-----
printer.setPageSize( pageSize );
//-----end page size workaround part 2-----
printer.setOutputFormat( QPrinter::PdfFormat ); //This isn't currently necessary, since currently all file names passed to this function will have the pdf extension, but it doesn't hurt and could in the future prevent non-PDF output
printCommon( fileURL.fileName() );
std::cout << "textDocumentToPDF():\tfileURL: \"" << fileURL.toString().toStdString().c_str() << "\"\toutputFileName: \"" << printer.outputFileName().toStdString().c_str() << "\"" << std::endl;
document->textDocument()->print( &printer );
}
bool ExportManager::showPrintDialog( const QString& windowTitle ) {
QPrintDialog dialog(&printer);
dialog.setWindowTitle( windowTitle );
if( Q_LIKELY( dialog.exec() == QDialog::Accepted ) ) {
return true;
} else {
return false;
}
}