Back to index

scribus-ng  1.3.4.dfsg+svn20071115
patterndialog.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008 *   Copyright (C) 2006 by Franz Schmid                                   *
00009 *   franz.schmid@altmuehlnet.de                                                   *
00010 *                                                                         *
00011 *   This program is free software; you can redistribute it and/or modify  *
00012 *   it under the terms of the GNU General Public License as published by  *
00013 *   the Free Software Foundation; either version 2 of the License, or     *
00014 *   (at your option) any later version.                                   *
00015 *                                                                         *
00016 *   This program is distributed in the hope that it will be useful,       *
00017 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019 *   GNU General Public License for more details.                          *
00020 *                                                                         *
00021 *   You should have received a copy of the GNU General Public License     *
00022 *   along with this program; if not, write to the                         *
00023 *   Free Software Foundation, Inc.,                                       *
00024 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025 ***************************************************************************/
00026 
00027 #include "patterndialog.h"
00028 #include "patterndialog.moc"
00029 #include "customfdialog.h"
00030 #include "prefsmanager.h"
00031 #include "prefsfile.h"
00032 #include "util.h"
00033 #include "stencilreader.h"
00034 #include "scconfig.h"
00035 #include <qpixmap.h>
00036 #include <qimage.h>
00037 #include <qdir.h>
00038 #include <qfiledialog.h>
00039 #include <qapplication.h>
00040 #include <qeventloop.h>
00041 #include <qcursor.h>
00042 
00043 PatternDialog::PatternDialog(QWidget* parent, QMap<QString, ScPattern> *docPatterns, ScribusDoc *doc, ScribusMainWindow *scMW) : patternDialogBase(parent)
00044 {
00045        m_doc = doc;
00046        mainWin = scMW;
00047        patternView->clear();
00048        patternView->setMinimumWidth(175);
00049        for (QMap<QString, ScPattern>::Iterator it = docPatterns->begin(); it != docPatterns->end(); ++it)
00050        {
00051               dialogPatterns.insert(it.key(), it.data());
00052        }
00053        origPatterns = docPatterns->keys();
00054        updatePatternList();
00055        buttonRemove->setEnabled(false);
00056        connect(buttonOK, SIGNAL(clicked()), this, SLOT(accept()));
00057        connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
00058        connect(buttonLoad, SIGNAL(clicked()), this, SLOT(loadPattern()));
00059        connect(buttonLoadDir, SIGNAL(clicked()), this, SLOT(loadPatternDir()));
00060        connect(buttonRemove, SIGNAL(clicked()), this, SLOT(removePattern()));
00061        connect(buttonRemoveAll, SIGNAL(clicked()), this, SLOT(removeAllPatterns()));
00062        connect(patternView, SIGNAL(clicked(QIconViewItem*)), this, SLOT(patternSelected(QIconViewItem*)));
00063 }
00064 
00065 void PatternDialog::updatePatternList()
00066 {
00067        patternView->clear();
00068        patternView->setMaxItemWidth(48);
00069        patternView->setWordWrapIconText(true);
00070        for (QMap<QString, ScPattern>::Iterator it = dialogPatterns.begin(); it != dialogPatterns.end(); ++it)
00071        {
00072               QPixmap pm;
00073               if (it.data().getPattern()->width() >= it.data().getPattern()->height())
00074                      pm.convertFromImage(it.data().getPattern()->scaleWidth(48));
00075               else
00076                      pm.convertFromImage(it.data().getPattern()->scaleHeight(48));
00077               QIconViewItem *item = new QIconViewItem(patternView, it.key(), pm);
00078               item->setDragEnabled(false);
00079        }
00080 }
00081 
00082 void PatternDialog::loadPatternDir()
00083 {
00084        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00085        QString wdir = dirs->get("patterns", ".");
00086        QString fileName = QFileDialog::getExistingDirectory(wdir, this, "d", tr("Choose a Directory"), true);
00087        if (!fileName.isEmpty())
00088        {
00089               QStringList formats;
00090               formats += "eps";
00091               formats += "epsi";
00092               formats += "pdf";
00093               QString form1 = "";
00094               for ( uint i = 0; i < QImageIO::inputFormats().count(); ++i )
00095               {
00096                      form1 = QString(QImageIO::inputFormats().at(i)).lower();
00097                      if (form1 == "jpeg")
00098                             form1 = "jpg";
00099                      if ((form1 == "png") || (form1 == "xpm") || (form1 == "gif"))
00100                      formats += form1;
00101                      else if (form1 == "jpg")
00102                      {
00103                             formats += "jpg";
00104                             formats += "jpeg";
00105                      }
00106               }
00107               formats += "tif";
00108               formats += "tiff";
00109               formats += "psd";
00110               formats += "pat";
00111               QDir d(fileName, "*", QDir::Name, QDir::Files | QDir::Readable | QDir::NoSymLinks);
00112               if ((d.exists()) && (d.count() != 0))
00113               {
00114                      mainWin->setStatusBarInfoText( tr("Loading Patterns"));
00115                      mainWin->mainWindowProgressBar->reset();
00116                      mainWin->mainWindowProgressBar->setTotalSteps(d.count() * 2);
00117                      qApp->setOverrideCursor(QCursor(waitCursor), true);
00118                      qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00119                      for (uint dc = 0; dc < d.count(); ++dc)
00120                      {
00121                             mainWin->mainWindowProgressBar->setProgress(dc);
00122                             qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00123                             QFileInfo fi(QDir::cleanDirPath(QDir::convertSeparators(fileName + "/" + d[dc])));
00124                             QString ext = fi.extension(true).lower();
00125                             if ((ext == "sml") || (ext == "shape") || (ext == "sce"))
00126                                    loadVectors(QDir::cleanDirPath(QDir::convertSeparators(fileName + "/" + d[dc])));
00127                      }
00128                      for (uint dc = 0; dc < d.count(); ++dc)
00129                      {
00130                             mainWin->mainWindowProgressBar->setProgress(d.count() + dc);
00131                             qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00132                             QFileInfo fi(QDir::cleanDirPath(QDir::convertSeparators(fileName + "/" + d[dc])));
00133                             QString ext = fi.extension(true).lower();
00134                             if ((ext == "sml") || (ext == "shape") || (ext == "sce"))
00135                                    continue;
00136                             else if (formats.contains(ext))
00137                             {
00138                                    QString patNam = fi.baseName().stripWhiteSpace().simplifyWhiteSpace().replace(" ", "_");
00139                                    if (!dialogPatterns.contains(patNam))
00140                                    {
00141                                           ScPattern pat = ScPattern();
00142                                           pat.setDoc(m_doc);
00143                                           pat.setPattern(QDir::cleanDirPath(QDir::convertSeparators(fileName + "/" + d[dc])));
00144                                           dialogPatterns.insert(patNam, pat);
00145                                    }
00146                             }
00147                             else
00148                                    continue;
00149                      }
00150                      d.cdUp();
00151                      dirs->set("patterns", d.absPath());
00152                      qApp->setOverrideCursor(QCursor(arrowCursor), true);
00153                      mainWin->setStatusBarInfoText("");
00154                      mainWin->mainWindowProgressBar->reset();
00155               }
00156               updatePatternList();
00157        }
00158 }
00159 
00160 void PatternDialog::loadPattern()
00161 {
00162        QString fileName;
00163        QString formats = "Scribus Objects (*.sce *.SCE);;Dia Shapes (*.shape *.SHAPE);;Kivio Stencils (*.sml *.SML);;EPS (*.eps *.EPS);;EPSI (*.epsi *.EPSI);;PDF (*.pdf *.PDF);;";
00164        QString form1 = "";
00165        QString form2 = "";
00166        for ( uint i = 0; i < QImageIO::inputFormats().count(); ++i )
00167        {
00168               form1 = QString(QImageIO::inputFormats().at(i)).lower();
00169               form2 = QString(QImageIO::inputFormats().at(i)).upper();
00170               if (form1 == "jpeg")
00171               {
00172                      form1 = "jpg";
00173                      form2 = "JPG";
00174               }
00175               if ((form1 == "png") || (form1 == "xpm") || (form1 == "gif"))
00176               {
00177                      formats += form2 + " (*."+form1+" *."+form2+");;";
00178               }
00179               else if (form1 == "jpg")
00180               {
00181                      // JPEG is a special case because both .jpg and .jpeg
00182                      // are acceptable extensions.
00183                      formats += "JPEG (*.jpg *.jpeg *.JPG *.JPEG);;";
00184               }
00185        }
00186        formats += "TIFF (*.tif *.tiff *.TIF *.TIFF);;";
00187        formats += "PSD (*.psd *.PSD);;";
00188        formats += "Gimp Patterns (*.pat *.PAT);;";
00189        formats += tr("All Files (*)");
00190        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00191        QString wdir = dirs->get("patterns", ".");
00192        CustomFDialog dia(this, wdir, tr("Open"), formats, fdExistingFiles);
00193        if (dia.exec() == QDialog::Accepted)
00194               fileName = dia.selectedFile();
00195        else
00196               return;
00197        if (!fileName.isEmpty())
00198        {
00199               PrefsManager::instance()->prefsFile->getContext("dirs")->set("patterns", fileName.left(fileName.findRev("/")));
00200               QFileInfo fi(fileName);
00201               if ((fi.extension(true).lower() == "sml") || (fi.extension(true).lower() == "shape") || (fi.extension(true).lower() == "sce"))
00202               {
00203                      loadVectors(fileName);
00204                      updatePatternList();
00205               }
00206               else
00207               {
00208                      QString patNam = fi.baseName().stripWhiteSpace().simplifyWhiteSpace().replace(" ", "_");
00209                      ScPattern pat = ScPattern();
00210                      pat.setDoc(m_doc);
00211                      pat.setPattern(fileName);
00212                      if (!dialogPatterns.contains(patNam))
00213                      {
00214                             dialogPatterns.insert(patNam, pat);
00215                             updatePatternList();
00216                      }
00217               }
00218        }
00219 }
00220 
00221 void PatternDialog::loadVectors(QString data)
00222 {
00223        QFileInfo fi(data);
00224        QString patNam = fi.baseName().stripWhiteSpace().simplifyWhiteSpace().replace(" ", "_");
00225        if (fi.extension(true).lower() == "sml")
00226        {
00227               QString f = "";
00228               loadText(data, &f);
00229               StencilReader *pre = new StencilReader();
00230               data = pre->createObjects(f);
00231               delete pre;
00232        }
00233        else if (fi.extension(true).lower() == "shape")
00234        {
00235               QString f = "";
00236               loadText(data, &f);
00237               StencilReader *pre = new StencilReader();
00238               data = pre->createShape(f);
00239               delete pre;
00240        }
00241        else if (fi.extension(true).lower() == "sce")
00242        {
00243               QString f = "";
00244               loadText(data, &f);
00245               data = f;
00246        }
00247        uint ac = m_doc->Items->count();
00248        bool savedAlignGrid = m_doc->useRaster;
00249        bool savedAlignGuides = m_doc->SnapGuides;
00250        m_doc->useRaster = false;
00251        m_doc->SnapGuides = false;
00252        mainWin->slotElemRead(data, m_doc->currentPage()->xOffset(), m_doc->currentPage()->yOffset(), false, true, m_doc, m_doc->view());
00253        m_doc->useRaster = savedAlignGrid;
00254        m_doc->SnapGuides = savedAlignGuides;
00255        uint ae = m_doc->Items->count();
00256        for (uint as = ac; as < ae; ++as)
00257        {
00258               PageItem* ite = m_doc->Items->at(ac);
00259               if (ite->itemType() == PageItem::PathText)
00260               {
00261                      ite->Frame = true;
00262                      ite->updatePolyClip();
00263               }
00264               else if (ite->itemType() == PageItem::TextFrame)
00265               {
00266                      if ( ite->prevInChain() == 0 )
00267                             ite->asTextFrame()->layout();
00268               }
00269        }
00270        ScPattern pat = ScPattern();
00271        pat.setDoc(m_doc);
00272        PageItem* currItem = m_doc->Items->at(ac);
00273        pat.pattern = currItem->DrawObj_toImage();
00274        pat.width = currItem->gWidth;
00275        pat.height = currItem->gHeight;
00276        for (uint as = ac; as < ae; ++as)
00277        {
00278               pat.items.append(m_doc->Items->take(ac));
00279        }
00280        if (!dialogPatterns.contains(patNam))
00281        {
00282               dialogPatterns.insert(patNam, pat);
00283        }
00284        for (QMap<QString, ScPattern>::Iterator it = m_doc->docPatterns.begin(); it != m_doc->docPatterns.end(); ++it)
00285        {
00286               if (!origPatterns.contains(it.key()))
00287                      dialogPatterns.insert(it.key(), it.data());
00288        }
00289 }
00290 
00291 void PatternDialog::patternSelected(QIconViewItem* it)
00292 {
00293        if (it)
00294               buttonRemove->setEnabled(true);
00295        else
00296        {
00297               buttonRemove->setEnabled(false);
00298               patternView->clearSelection();
00299        }
00300 }
00301 
00302 void PatternDialog::removeAllPatterns()
00303 {
00304        dialogPatterns.clear();
00305        updatePatternList();
00306 }
00307 
00308 void PatternDialog::removePattern()
00309 {
00310        QIconViewItem *it = patternView->currentItem();
00311        if (it)
00312        {
00313               QStringList patterns2Del;
00314               QStringList mainPatterns = dialogPatterns.keys();
00315               for (uint a = 0; a < mainPatterns.count(); a++)
00316               {
00317                      if (mainPatterns[a] != it->text())
00318                      {
00319                             QStringList subPatterns;
00320                             subPatterns = getUsedPatternsHelper(mainPatterns[a], subPatterns);
00321                             if (subPatterns.contains(it->text()))
00322                                    patterns2Del.append(mainPatterns[a]);
00323                      }
00324               }
00325 //            QStringList subPatterns;
00326 //            subPatterns = getUsedPatternsHelper(it->text(), subPatterns);
00327 //            if (!subPatterns.isEmpty())
00328 //                   patterns2Del += subPatterns;
00329               patterns2Del.append(it->text());
00330               for (uint a = 0; a < patterns2Del.count(); a++)
00331               {
00332                      dialogPatterns.remove(patterns2Del[a]);
00333               }
00334               updatePatternList();
00335        }
00336 }
00337 
00338 QStringList PatternDialog::getUsedPatternsHelper(QString pattern, QStringList &results)
00339 {
00340        ScPattern *pat = &dialogPatterns[pattern];
00341        QStringList pats;
00342        pats.clear();
00343        for (uint c = 0; c < pat->items.count(); ++c)
00344        {
00345               if ((!results.contains(pat->items.at(c)->pattern())) && (pat->items.at(c)->GrType == 8))
00346                      pats.append(pat->items.at(c)->pattern());
00347        }
00348        if (!pats.isEmpty())
00349        {
00350               for (uint c = 0; c < pats.count(); ++c)
00351               {
00352                      getUsedPatternsHelper(pats[c], results);
00353               }
00354        }
00355        return pats;
00356 }