Subversion Repositories Scribus

Rev

Rev 2033 | Rev 2036 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1972 craig 1
#include "pdfoptions.h"
2
#include "scribusstructs.h"
3
 
2030 craig 4
PDFOptions::VerifyResults PDFOptions::verify()
1972 craig 5
{
2030 craig 6
        return verify(static_cast<QString*>(NULL));
7
}
8
 
9
PDFOptions::VerifyResults PDFOptions::verify(QString* /*problemDescription*/)
10
{
1972 craig 11
        // TODO: implement this method
2031 craig 12
        return Verify_NoError;
1972 craig 13
}
14
 
2030 craig 15
 
16
const int PDFOptionsIO::formatVersion = 1300;
17
 
18
PDFOptionsIO::PDFOptionsIO(PDFOptions& opts) :
2031 craig 19
        doc("PDFSettings"),
20
        error()
1972 craig 21
{
2030 craig 22
        this->opts = &opts;
1972 craig 23
}
24
 
2034 craig 25
// writeTo(QString) is implemented separately to writeTo(QTextStream)
2033 craig 26
// because we don't want to clobber the output file until we know the
27
// data has been generated ok, and we can't avoid clobbering the file
28
// to create a QTextStream().
2030 craig 29
bool PDFOptionsIO::writeTo(QString outFileName)
1972 craig 30
{
2033 craig 31
        QString xml = buildXMLString();
32
        if (xml == QString::null)
33
                return false;
2031 craig 34
        QFile f(outFileName);
2033 craig 35
        if (!f.open(IO_WriteOnly|IO_Truncate))
36
        {
37
                error = QObject::tr("Couldn't open output file");
38
                return false;
39
        }
2031 craig 40
        QTextStream ts(&f);
2033 craig 41
        ts.setEncoding(QTextStream::UnicodeUTF8);
42
        ts << xml;
43
        error = QString::null;
44
        return true;
1972 craig 45
}
46
 
2030 craig 47
bool PDFOptionsIO::writeTo(QTextStream& outStream)
1972 craig 48
{
2030 craig 49
        if (!outStream.device()->isWritable())
2031 craig 50
        {
51
                error = QObject::tr("Output stream not writeable");
2030 craig 52
                return false;
2031 craig 53
        }
2033 craig 54
        QString xml = buildXMLString();
55
        if (xml == QString::null)
56
                return false;
57
        outStream << xml;
58
        error = QString::null;
59
        return true;
60
}
61
 
2034 craig 62
// Construct and return an XML string representing the settings
2033 craig 63
QString PDFOptionsIO::buildXMLString()
64
{
2030 craig 65
        // Verify to make sure our settings are sane
2033 craig 66
        QString vrfyError;
67
        PDFOptions::VerifyResults vr = opts->verify(&vrfyError);
2030 craig 68
        if (vr != PDFOptions::Verify_NoError)
69
        {
2033 craig 70
                error = QObject::tr("Verification of settings failed: %1").arg(vrfyError);
71
                return QString::null;
2030 craig 72
        }
73
        // Build the document. Initial implementation uses QDom.
74
        root = doc.createElement("ScribusPDFOpts");
75
        root.setAttribute("version", formatVersion);
76
        doc.appendChild(root);
77
        // Fill the guts of the document
2031 craig 78
        buildSettings();
2033 craig 79
        // We're done - return a string containing the document XML
2030 craig 80
        QString xml = doc.toString();
2033 craig 81
        xml.prepend("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
82
        return xml;
2030 craig 83
}
84
 
85
// Build up the stored settings into the passed DOM objects
2031 craig 86
void PDFOptionsIO::buildSettings()
2030 craig 87
{
88
        addElem("thumbnails", opts->Thumbnails);
89
        addElem("articles", opts->Articles);
90
        addElem("useLayers", opts->useLayers);
91
        addElem("compress", opts->Compress);
92
        addElem("compressMethod", opts->CompressMethod);
93
        addElem("quality", opts->Quality);
94
        addElem("recalcPic", opts->RecalcPic);
95
        addElem("bookmarks", opts->Bookmarks);
96
        addElem("picRes", opts->PicRes);
97
        QString pdfVersString;
98
        switch (opts->Version)
99
        {
100
                case PDFOptions::PDFVersion_X3:
101
                        pdfVersString = "X3";
102
                        break;
103
                default:
104
                        pdfVersString = QString::number(opts->Version);
105
                        break;
106
        }
107
        addElem("pdfVersion", pdfVersString);
108
        addElem("resolution", opts->Resolution);
109
        addElem("binding", opts->Binding);
2031 craig 110
        addList("embedFonts", opts->EmbedList);
111
        addList("subsetFonts", opts->SubsetList);
2030 craig 112
        addElem("mirrorH", opts->MirrorH);
113
        addElem("mirrorV", opts->MirrorV);
114
        addElem("rotateDegrees", opts->RotateDeg);
115
        addElem("presentMode", opts->PresentMode);
2031 craig 116
        // FIXME: Save opts->PresentVals
2030 craig 117
        addElem("filename", opts->Datei);
118
        addElem("isGrayscale", opts->isGrayscale);
119
        addElem("useRGB", opts->UseRGB);
120
        addElem("useProfiles", opts->UseProfiles);
121
        addElem("useProfiles2", opts->UseProfiles2);
122
        addElem("useLPI", opts->UseLPI);
2031 craig 123
        // FIXME: Save opts->LPISettings
2030 craig 124
        addElem("solidProf", opts->SolidProf);
125
        addElem("sComp", opts->SComp);
126
        addElem("imageProf", opts->ImageProf);
127
        addElem("embeddedI", opts->EmbeddedI);
128
        addElem("intent2", opts->Intent2);
129
        addElem("printProf", opts->PrintProf);
130
        addElem("info", opts->Info);
131
        addElem("intent", opts->Intent);
132
        addElem("bleedTop", opts->BleedTop);
133
        addElem("bleedLeft", opts->BleedLeft);
134
        addElem("bleedRight", opts->BleedRight);
135
        addElem("bleedBottom", opts->BleedBottom);
136
        addElem("encrypt", opts->Encrypt);
137
        addElem("passOwner", opts->PassOwner);
138
        addElem("passUser", opts->PassUser);
139
        addElem("permissions", opts->Permissions);
140
}
141
 
142
// Convenience functions to add a single-attribute element
143
// of a particular type to the tree.
144
 
145
void PDFOptionsIO::addElem(QString name, bool value)
146
{
147
        QDomElement elem = doc.createElement(name);
148
        elem.setAttribute("value",value);
149
        root.appendChild(elem);
150
}
151
 
152
void PDFOptionsIO::addElem(QString name, QString value)
153
{
154
        QDomElement elem = doc.createElement(name);
155
        elem.setAttribute("value",value);
156
        root.appendChild(elem);
157
}
158
 
159
void PDFOptionsIO::addElem(QString name, int value)
160
{
161
        QDomElement elem = doc.createElement(name);
162
        elem.setAttribute("value",value);
163
        root.appendChild(elem);
164
}
165
 
166
void PDFOptionsIO::addElem(QString name, double value)
167
{
168
        QDomElement elem = doc.createElement(name);
169
        elem.setAttribute("value",value);
170
        root.appendChild(elem);
171
}
172
 
2034 craig 173
// Save a QValueList<String> or QStringList as a list of
174
// <item value=""> elements
2031 craig 175
void PDFOptionsIO::addList(QString name, QValueList<QString>& value)
176
{
177
        // List base element has no attributes, only children
178
        QDomElement listbase = doc.createElement(name);
179
        root.appendChild(listbase);
180
        QValueList<QString>::iterator it;
181
        for (it = value.begin(); it != value.end(); ++it)
182
        {
2034 craig 183
                // Each child is of the form <item value="">
2031 craig 184
                QDomElement child = doc.createElement("item");
185
                child.setAttribute("value",*(it));
186
                listbase.appendChild(child);
187
        }
188
}
2030 craig 189
 
190
bool PDFOptionsIO::readFrom(QTextStream& inStream)
191
{
1972 craig 192
        if (!inStream.device()->isReadable())
193
                return false;
194
        // TODO: implement this method
195
        return false;
196
}
197
 
198
// overload of bool readFrom(QTextStream& inStream)
2030 craig 199
bool PDFOptionsIO::readFrom(QString inFileName)
1972 craig 200
{
201
        QTextStream ts(inFileName, IO_ReadOnly);
202
        return readFrom(ts);
203
}
204
 
2031 craig 205
const QString& PDFOptionsIO::lastError() const
206
{
207
        return error;
208
}