Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
6368 fschmid 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
*/
7
/***************************************************************************
8
*   Copyright (C) 2006 by Franz Schmid                                   *
9
*   franz.schmid@altmuehlnet.de                                                   *
10
*                                                                         *
11
*   This program is free software; you can redistribute it and/or modify  *
12
*   it under the terms of the GNU General Public License as published by  *
13
*   the Free Software Foundation; either version 2 of the License, or     *
14
*   (at your option) any later version.                                   *
15
*                                                                         *
16
*   This program is distributed in the hope that it will be useful,       *
17
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
*   GNU General Public License for more details.                          *
20
*                                                                         *
21
*   You should have received a copy of the GNU General Public License     *
22
*   along with this program; if not, write to the                         *
23
*   Free Software Foundation, Inc.,                                       *
24
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25
***************************************************************************/
26
 
27
#include "patterndialog.h"
28
#include "patterndialog.moc"
29
#include "customfdialog.h"
30
#include "prefsmanager.h"
31
#include "prefsfile.h"
32
#include "util.h"
6372 fschmid 33
#include "stencilreader.h"
34
#include "scconfig.h"
6368 fschmid 35
#include <qpixmap.h>
36
#include <qimage.h>
37
 
6372 fschmid 38
PatternDialog::PatternDialog(QWidget* parent, QMap<QString, ScPattern> *docPatterns, ScribusDoc *doc, ScribusMainWindow *scMW) : patternDialogBase(parent)
6368 fschmid 39
{
6369 fschmid 40
        m_doc = doc;
6372 fschmid 41
        mainWin = scMW;
6368 fschmid 42
        patternView->clear();
43
        for (QMap<QString, ScPattern>::Iterator it = docPatterns->begin(); it != docPatterns->end(); ++it)
44
        {
45
                dialogPatterns.insert(it.key(), it.data());
46
        }
47
        updatePatternList();
48
        buttonRemove->setEnabled(false);
49
        connect(buttonOK, SIGNAL(clicked()), this, SLOT(accept()));
50
        connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
51
        connect(buttonLoad, SIGNAL(clicked()), this, SLOT(loadPattern()));
52
        connect(buttonRemove, SIGNAL(clicked()), this, SLOT(removePattern()));
53
        connect(patternView, SIGNAL(clicked(QIconViewItem*)), this, SLOT(patternSelected(QIconViewItem*)));
54
}
55
 
56
void PatternDialog::updatePatternList()
57
{
58
        patternView->clear();
59
        for (QMap<QString, ScPattern>::Iterator it = dialogPatterns.begin(); it != dialogPatterns.end(); ++it)
60
        {
61
                QPixmap pm;
62
                if (it.data().getPattern()->width() > it.data().getPattern()->height())
63
                        pm.convertFromImage(it.data().getPattern()->scaleWidth(48));
64
                else
65
                        pm.convertFromImage(it.data().getPattern()->scaleHeight(48));
66
                (void) new QIconViewItem(patternView, it.key(), pm);
67
        }
68
}
69
 
70
void PatternDialog::loadPattern()
71
{
72
        QString fileName;
6378 fschmid 73
        QString formats = "Scribus Objects (*.sce *.SCE);;Dia Shapes (*.shape *.SHAPE);;Kivio Stencils (*.sml *.SML);;EPS (*.eps *.EPS);;PDF (*.pdf *.PDF);;";
6372 fschmid 74
        QString form1 = "";
75
        QString form2 = "";
76
        for ( uint i = 0; i < QImageIO::inputFormats().count(); ++i )
77
        {
78
                form1 = QString(QImageIO::inputFormats().at(i)).lower();
79
                form2 = QString(QImageIO::inputFormats().at(i)).upper();
80
                if (form1 == "jpeg")
81
                {
82
                        form1 = "jpg";
83
                        form2 = "JPG";
84
                }
85
                if ((form1 == "png") || (form1 == "xpm") || (form1 == "gif"))
86
                {
87
                        formats += form2 + " (*."+form1+" *."+form2+");;";
88
                }
89
                else if (form1 == "jpg")
90
                {
91
                        // JPEG is a special case because both .jpg and .jpeg
92
                        // are acceptable extensions.
93
                        formats += "JPEG (*.jpg *.jpeg *.JPG *.JPEG);;";
94
                }
95
        }
96
#ifdef HAVE_TIFF
97
        formats += "TIFF (*.tif *.tiff *.TIF *.TIFF);;";
98
#endif
99
        formats += "PSD (*.psd *.PSD);;";
6378 fschmid 100
        formats += tr("All Files (*)");
6368 fschmid 101
        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
102
        QString wdir = dirs->get("patterns", ".");
6372 fschmid 103
        CustomFDialog dia(this, wdir, tr("Open"), formats, fdExistingFiles);
6368 fschmid 104
        if (dia.exec() == QDialog::Accepted)
105
                fileName = dia.selectedFile();
106
        else
107
                return;
108
        if (!fileName.isEmpty())
109
        {
110
                PrefsManager::instance()->prefsFile->getContext("dirs")->set("patterns", fileName.left(fileName.findRev("/")));
111
                QFileInfo fi(fileName);
6372 fschmid 112
                if ((fi.extension(true).lower() == "sml") || (fi.extension(true).lower() == "shape") || (fi.extension(true).lower() == "sce"))
113
                        loadVectors(fileName);
114
                else
6368 fschmid 115
                {
6372 fschmid 116
                        QString patNam = fi.baseName().stripWhiteSpace().simplifyWhiteSpace().replace(" ", "_");
117
                        ScPattern pat = ScPattern();
118
                        pat.setDoc(m_doc);
119
                        pat.setPattern(fileName);
120
                        if (!dialogPatterns.contains(patNam))
121
                        {
122
                                dialogPatterns.insert(patNam, pat);
123
                                updatePatternList();
124
                        }
6368 fschmid 125
                }
126
        }
127
}
128
 
6372 fschmid 129
void PatternDialog::loadVectors(QString data)
130
{
131
        QFileInfo fi(data);
132
        QString patNam = fi.baseName().stripWhiteSpace().simplifyWhiteSpace().replace(" ", "_");
133
        if (fi.extension(true).lower() == "sml")
134
        {
135
                QString f = "";
136
                loadText(data, &f);
137
                StencilReader *pre = new StencilReader();
138
                data = pre->createObjects(f);
139
                delete pre;
140
        }
141
        else if (fi.extension(true).lower() == "shape")
142
        {
143
                QString f = "";
144
                loadText(data, &f);
145
                StencilReader *pre = new StencilReader();
146
                data = pre->createShape(f);
147
                delete pre;
148
        }
149
        else if (fi.extension(true).lower() == "sce")
150
        {
151
                QString f = "";
152
                loadText(data, &f);
153
                data = f;
154
        }
155
        uint ac = m_doc->Items->count();
156
        bool savedAlignGrid = m_doc->useRaster;
157
        bool savedAlignGuides = m_doc->SnapGuides;
158
        m_doc->useRaster = false;
159
        m_doc->SnapGuides = false;
160
        mainWin->slotElemRead(data, m_doc->currentPage()->xOffset(), m_doc->currentPage()->yOffset(), false, true, m_doc, m_doc->view());
161
        m_doc->useRaster = savedAlignGrid;
162
        m_doc->SnapGuides = savedAlignGuides;
163
        uint ae = m_doc->Items->count();
164
        ScPattern pat = ScPattern();
165
        pat.setDoc(m_doc);
166
        PageItem* currItem = m_doc->Items->at(ac);
167
        pat.pattern = currItem->DrawObj_toImage();
168
        for (uint as = ac; as < ae; ++as)
169
        {
170
                pat.items.append(m_doc->Items->take(ac));
171
        }
172
        if (!dialogPatterns.contains(patNam))
173
        {
174
                dialogPatterns.insert(patNam, pat);
175
                updatePatternList();
176
        }
177
}
178
 
6368 fschmid 179
void PatternDialog::patternSelected(QIconViewItem* it)
180
{
181
        if (it)
182
                buttonRemove->setEnabled(true);
183
        else
184
        {
185
                buttonRemove->setEnabled(false);
186
                patternView->clearSelection();
187
        }
188
}
189
 
190
void PatternDialog::removePattern()
191
{
192
        QIconViewItem *it = patternView->currentItem();
193
        if (it)
194
        {
195
                dialogPatterns.remove(it->text());
196
                updatePatternList();
197
        }
198
}