Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
4430 cbradney 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
304 Franz 7
/***************************************************************************
1184 tsoots 8
 *   Riku Leino, tsoots@gmail.com                                          *
304 Franz 9
 ***************************************************************************/
10
#include "satdialog.h"
2986 craig 11
#include "scribusapi.h"
10713 jghali 12
 
13805 jghali 13
#include <QDir>
10713 jghali 14
#include <QXmlDefaultHandler>
15
#include <QXmlSimpleReader>
16
 
2856 cbradney 17
#include "prefsmanager.h"
9603 subik 18
#include "prefsfile.h"
10713 jghali 19
#include "scpaths.h"
20
#include "scribuscore.h"
10200 cbradney 21
#include "util_icon.h"
304 Franz 22
 
10713 jghali 23
class CategoriesReader : public QXmlDefaultHandler
24
{
25
public:
26
        QStringList categories;
27
        virtual bool startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs)
28
        {
29
                if (name == QLatin1String("template"))
30
                {
31
                        QString cat = attrs.value(QLatin1String("category"));
32
                        if (!categories.contains(cat))
33
                                categories.append(cat);
34
                }
35
                return true;
36
        };
37
};
38
 
10430 cbradney 39
satdialog::satdialog(QWidget* parent, QString tmplName, int pageW, int pageH) : QDialog(parent)
304 Franz 40
{
9603 subik 41
        setupUi(this);
10430 cbradney 42
        setModal(true);
10495 cbradney 43
        setWindowTitle( tr("Save as Template"));
10599 fschmid 44
        setWindowIcon(loadIcon("AppIcon.png"));
308 Franz 45
        readPrefs();
309 Franz 46
 
304 Franz 47
        nameEdit->setText(tmplName);
48
        setupCategories();
308 Franz 49
        setupPageSize(pageW, pageH);
50
        authorEdit->setText(author);
51
        emailEdit->setText(email);
52
 
304 Franz 53
        // Slots and signals connections
9603 subik 54
        connect(detailButton, SIGNAL(stateChanged(int)), this, SLOT(detailClicked(int)));
304 Franz 55
}
56
 
9603 subik 57
void satdialog::detailClicked(int state)
304 Franz 58
{
9603 subik 59
        isFullDetail = state == Qt::Checked;
304 Franz 60
 
9603 subik 61
        psizeLabel->setVisible(isFullDetail);
62
        catsLabel->setVisible(isFullDetail);
63
        colorsLabel->setVisible(isFullDetail);
64
        descrLabel->setVisible(isFullDetail);
65
        usageLabel->setVisible(isFullDetail);
66
        authorLabel->setVisible(isFullDetail);
67
        emailLabel->setVisible(isFullDetail);
68
        psizeEdit->setVisible(isFullDetail);
69
        catsCombo->setVisible(isFullDetail);
70
        colorsEdit->setVisible(isFullDetail);
71
        descrEdit->setVisible(isFullDetail);
72
        usageEdit->setVisible(isFullDetail);
73
        authorEdit->setVisible(isFullDetail);
74
        emailEdit->setVisible(isFullDetail);
304 Franz 75
}
76
 
9603 subik 77
void satdialog::readPrefs()
304 Franz 78
{
2856 cbradney 79
        prefs = PrefsManager::instance()->prefsFile->getPluginContext("satemplate");
415 Franz 80
        author = prefs->get("author", "");
81
        email = prefs->get("email", "");
82
        isFullDetail = prefs->getBool("isFullDetail", false);
9603 subik 83
        detailClicked(isFullDetail ? Qt::Checked : Qt::Unchecked);
84
        detailButton->setCheckState(isFullDetail ? Qt::Checked : Qt::Unchecked);
308 Franz 85
}
86
 
87
void satdialog::writePrefs()
88
{
415 Franz 89
        prefs->set("author", authorEdit->text());
90
        prefs->set("email", emailEdit->text());
91
        prefs->set("isFullDetail", isFullDetail);
308 Franz 92
}
93
 
10713 jghali 94
QString satdialog::findTemplateXml(QString dir)
95
{
96
        QString lang = ScCore->getGuiLanguage();
97
        QString tmp = dir + "/template." + lang + ".xml";
98
        if (QFile::exists(tmp))
99
                return tmp;
100
 
101
        if (lang.length() > 2)
102
        {
103
                tmp = dir + "/template." + lang.left(2) + ".xml";
104
                if (QFile::exists(tmp))
105
                        return tmp;
106
        }
107
        return dir + "/template.xml";  
108
}
109
 
110
void satdialog::addCategories(const QString& dir)
111
{
112
        // Read categories from the dir itself
113
        QString tmplFile = findTemplateXml(dir);
114
        if (QFile::exists(tmplFile))
115
                readCategories(tmplFile);
116
 
117
        // And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
118
        QDir tmpldir(dir);
119
        if (tmpldir.exists())
120
        {
15417 craig 121
                tmpldir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
10713 jghali 122
                QStringList dirs = tmpldir.entryList();
123
                for (int i = 0; i < dirs.size(); ++i)
124
                {
15417 craig 125
                        tmplFile = findTemplateXml(dir + "/" + dirs[i]);
126
                        if (QFile::exists(tmplFile))
127
                                readCategories(tmplFile);
10713 jghali 128
                }
129
        }
130
}
131
 
132
void satdialog::readCategories(const QString& fileName)
133
{
134
        QFile file(fileName);
135
        CategoriesReader catReader;
136
        QXmlInputSource  xmlSource(&file);
137
        QXmlSimpleReader reader;
138
        reader.setContentHandler(&catReader);
139
        reader.parse(&xmlSource);
140
        QStringList& categories = catReader.categories;
141
        for (int i = 0; i < categories.count(); ++i)
142
        {
143
                QString category = categories.at(i);
144
                if (!category.isEmpty() && !cats.contains(category))
145
                        cats.insert(category, category);
146
        }
147
}
148
 
309 Franz 149
void satdialog::setupCategories()
308 Franz 150
{
304 Franz 151
        // en will be used in template.xml and it will be then replaced with the lang when used for users
152
        // to get the categories in their language.
16710 jghali 153
        cats.insert(QString("Advertisements"), QObject::tr("Advertisements"));
154
        cats.insert(QString("Announcements") , QObject::tr("Announcements"));
10713 jghali 155
        cats.insert(QString("Brochures")  , QObject::tr("Brochures"));
16710 jghali 156
        cats.insert(QString("Business Cards"), QObject::tr("Business Cards"));
157
        cats.insert(QString("Calendars")  , QObject::tr("Calendars"));
158
        cats.insert(QString("Cards")      , QObject::tr("Cards"));
10713 jghali 159
        cats.insert(QString("Catalogs")   , QObject::tr("Catalogs"));
16710 jghali 160
        cats.insert(QString("Envelopes")  , QObject::tr("Envelopes"));
10713 jghali 161
        cats.insert(QString("Flyers")     , QObject::tr("Flyers"));
16710 jghali 162
        cats.insert(QString("Grids")      , QObject::tr("Grids"));
163
        cats.insert(QString("Folds")      , QObject::tr("Folds"));
164
        cats.insert(QString("Labels")     , QObject::tr("Labels"));
10713 jghali 165
        cats.insert(QString("Letterheads"), QObject::tr("Letterheads"));
16710 jghali 166
        cats.insert(QString("Magazines")  , QObject::tr("Magazines"));
167
        cats.insert(QString("Media Cases")  , QObject::tr("Media Cases"));
10713 jghali 168
        cats.insert(QString("Menus")      , QObject::tr("Menus"));
16710 jghali 169
        cats.insert(QString("Newsletters"), QObject::tr("Newsletters"));
170
        cats.insert(QString("Own Templates"), QObject::tr("Own Templates"));
10713 jghali 171
        cats.insert(QString("PDF Forms")  , QObject::tr("PDF Forms"));
172
        cats.insert(QString("PDF Presentations") , QObject::tr("PDF Presentations"));
173
        cats.insert(QString("Posters")    , QObject::tr("Posters"));
16710 jghali 174
        cats.insert(QString("Programs")   , QObject::tr("Programs"));
175
        cats.insert(QString("Signs")      , QObject::tr("Signs"));
10713 jghali 176
        cats.insert(QString("Text Documents"), QObject::tr("Text Documents"));
177
 
178
        QString scribusHome  = ScPaths::getApplicationDataDir();
179
        QString scribusShare = ScPaths::instance().templateDir();
309 Franz 180
 
10713 jghali 181
        addCategories(scribusHome + "/templates");
182
        addCategories(scribusShare);
15417 craig 183
        QString userTemplateDir(PrefsManager::instance()->appPrefs.pathPrefs.documentTemplates);
184
        if ((!userTemplateDir.isNull()) && (!userTemplateDir.isEmpty()))
185
                addCategories(userTemplateDir);
186
 
10713 jghali 187
        QStringList list;
188
        QMap<QString, QString>::ConstIterator it;
189
        for (it = cats.constBegin(); it != cats.constEnd(); ++it)
190
                list.append(it.value());
191
        list.sort();
10599 fschmid 192
        catsCombo->addItem("");
10713 jghali 193
        catsCombo->addItems(list);
304 Franz 194
        catsCombo->setEditable(true);
195
}
196
 
308 Franz 197
void satdialog::setupPageSize(int w, int h)
198
{
309 Franz 199
        QString sizelist[] =
200
                {"2380x3368", "1684x2380", "1190x1684", "842x1190", "595x842", "421x595", "297x421", "210x297", "148x210",
201
             "105x148", "2836x4008", "2004x2836", "1418x2004", "1002x1418", "709x1002", "501x709", "355x501", "250x355",
202
                 "178x250", "125x178", "89x125", "462x649", "298x683", "312x624", "542x720", "595x935", "1224x792", "612x1008",
203
                 "612x792", "792x1225"};
204
        QString namelist[] =
205
                {"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8",
206
                 "A9", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
207
                 "B8", "B9", "B10", "C5E", "Comm10E", "DLE", "Executive", "Folio", "Ledger", tr("Legal"),
208
                 tr("Letter"), tr("Tabloid")};
308 Franz 209
 
210
        QString orient, search, psize;
211
        QString wString = QString("%1").arg(w);
212
        QString hString = QString("%1").arg(h);
213
        if (w > h)
214
        {
7123 fschmid 215
                orient = ", "+ tr("landscape");
308 Franz 216
                search = hString+"x"+wString;
217
        }
218
        else if (w < h)
219
        {
7123 fschmid 220
                orient = ", "+ tr("portrait");
308 Franz 221
                search = wString+"x"+hString;
222
        }
223
        int index = -1;
309 Franz 224
        for (int i = 0; i < 30; ++i)
308 Franz 225
        {
309 Franz 226
                if (sizelist[i] == search)
308 Franz 227
                {
228
                        index = i;
309 Franz 229
                        psize = namelist[i];
308 Franz 230
                        break;
231
                }
232
        }
309 Franz 233
 
308 Franz 234
        if (index == -1)
235
        {
309 Franz 236
                psize = tr("custom")+", "+wString+"x"+hString;
308 Franz 237
                orient = "";
238
        }
239
 
240
        psizeEdit->setText(psize + orient);
241
}
242
 
304 Franz 243
satdialog::~satdialog()
244
{
308 Franz 245
        writePrefs();
304 Franz 246
}