Back to index

scribus-ng  1.3.4.dfsg+svn20071115
customfdialog.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                           customfdialog.cpp  -  description
00009                              -------------------
00010     begin                : Fri Nov 30 2001
00011     copyright            : (C) 2001 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "customfdialog.h"
00025 #include "customfdialog.moc"
00026 #include <qpixmap.h>
00027 #include <qpainter.h>
00028 #include <qfileinfo.h>
00029 #include <qdir.h>
00030 #include <qdom.h>
00031 #include <qtextcodec.h>
00032 
00033 #include "cmsettings.h"
00034 #include "sccombobox.h"
00035 #include "scribusstructs.h"
00036 #include "scimage.h"
00037 #include "util.h"
00038 
00039 extern QString DocDir;
00040 
00041 ImIconProvider::ImIconProvider(QWidget *pa) : QFileIconProvider(pa)
00042 {
00043        fmts.clear();
00044        QString tmp[] = {"eps", "epsi", "gif", "png", "jpg", "jpeg", "xpm", "tif", "tiff", "bmp", "pbm", "pgm", "ppm", "xbm", "xpm", "psd", "pat"};
00045        size_t array = sizeof(tmp) / sizeof(*tmp);
00046        for (uint a = 0; a < array; ++a)
00047               fmts += tmp[a];
00048        imagepm = loadIcon("16/image-x-generic.png");
00049        pspm = loadIcon("postscript.png");
00050        txtpm = loadIcon("txt.png");
00051        docpm = loadIcon("doc.png");
00052        pdfpm = loadIcon("pdf.png");
00053        oosxdpm = loadIcon("ooo_draw.png");
00054        oosxwpm = loadIcon("ooo_writer.png");
00055        vectorpm = loadIcon("vectorgfx.png");
00056 }
00057 
00058 const QPixmap * ImIconProvider::pixmap(const QFileInfo &fi)
00059 {
00060        QString ext = fi.extension(false).lower();
00061        if (fmts.contains(ext))
00062               return &imagepm;
00063        else
00064        {
00065               ext = fi.extension(true).lower();
00066               if (ext.endsWith("ps"))
00067                      return &pspm;
00068               if (ext.endsWith("txt"))
00069                      return &txtpm;
00070               if (ext.endsWith("scd") || ext.endsWith("scd.gz"))
00071                      return &docpm;
00072               if (ext.endsWith("sla") || ext.endsWith("sla.gz"))
00073                      return &docpm;
00074               if (ext.endsWith("pdf"))
00075                      return &pdfpm;
00076               if (ext.endsWith("sxd"))
00077                      return &oosxdpm;
00078               if (ext.endsWith("sxw"))
00079                      return &oosxwpm;
00080               if (ext.endsWith("svg") || ext.endsWith("svgz"))
00081                      return &vectorpm;
00082               return QFileIconProvider::pixmap(fi);
00083        }
00084 }
00085 
00086 FDialogPreview::FDialogPreview(QWidget *pa) : QLabel(pa)
00087 {
00088        setAlignment(AlignLeft | AlignTop);
00089        setMinimumSize( QSize( 100, 100 ) );
00090        setMaximumSize( QSize( 300, 300 ) );
00091        setScaledContents( false );
00092        setEraseColor( white );
00093        setFrameShape( QLabel::WinPanel );
00094        setFrameShadow( QLabel::Sunken );
00095        updtPix();
00096 }
00097 
00098 void FDialogPreview::updtPix()
00099 {
00100        QPixmap pm;
00101        QRect inside = contentsRect();
00102        pm = QPixmap(inside.width(), inside.height());
00103        pm.fill(white);
00104        setPixmap(pm);
00105 }
00106 
00107 void FDialogPreview::GenPreview(QString name)
00108 {
00109        QPixmap pm;
00110        QString Buffer = "";
00111        QFileInfo fi = QFileInfo(name);
00112        if (fi.isDir())
00113               return;
00114        updtPix();
00115        int w = pixmap()->width();
00116        int h = pixmap()->height();
00117        bool mode = false;
00118        QString ext = fi.extension(false).lower();
00119        QStringList formats;
00120        formats = QStringList::fromStrList(QImageIO::inputFormats());
00121        formats.append("jpg");
00122        formats.append("tif");
00123        formats.append("tiff");
00124        formats.append("psd");
00125        formats.append("eps");
00126        formats.append("epsi");
00127        formats.append("pdf");
00128        formats.append("ps");
00129        formats.append("pat");
00130        QString allFormats = formats.join( " " );
00131        formats.clear();
00132        allFormats = allFormats.lower();
00133        formats = QStringList::split( " ", allFormats );
00134        if (ext.isEmpty())
00135               ext = getImageType(name);
00136        if (formats.contains(ext))
00137        {
00138               ScImage im;
00139               //No doc to send data anyway, so no doc to get into scimage.
00140               CMSettings cms(0, "", 0);
00141               if (im.LoadPicture(name, cms, false, false, ScImage::Thumbnail, 72, &mode))
00142               {
00143                      int ix,iy;
00144                      if ((im.imgInfo.exifDataValid) && (!im.imgInfo.exifInfo.thumbnail.isNull()))
00145                      {
00146                             ix = im.imgInfo.exifInfo.width;
00147                             iy = im.imgInfo.exifInfo.height;
00148                      }
00149                      else
00150                      {
00151                             ix = im.width();
00152                             iy = im.height();
00153                      }
00154                      int xres = im.imgInfo.xres;
00155                      int yres = im.imgInfo.yres;
00156                      QString tmp = "";
00157                      QString tmp2 = "";
00158                      QImage im2;
00159                      if ((ix > w-5) || (iy > h-44))
00160                      {
00161                             double sx = im.width() / static_cast<double>(w-5);
00162                             double sy = im.height() / static_cast<double>(h-44);
00163                             im2 = sy < sx ?  im.smoothScale(qRound(im.width() / sx), qRound(im.height() / sx)) :
00164                                              im.smoothScale(qRound(im.width() / sy), qRound(im.height() / sy));
00165                      }
00166                      else
00167                             im2 = im.qImage(); // no need to copy
00168                      QPainter p;
00169                      QBrush b(QColor(205,205,205), loadIcon("testfill.png"));
00170                      p.begin(pixmap());
00171                      p.fillRect(0, 0, w, h-44, b);
00172                      p.fillRect(0, h-44, w, 44, QColor(255, 255, 255));
00173                      p.drawImage((w - im2.width()) / 2, (h - 44 - im2.height()) / 2, im2);
00174                      p.drawText(2, h-29, tr("Size:")+" "+tmp.setNum(ix)+" x "+tmp2.setNum(iy));
00175                      p.drawText(2, h-17, tr("Resolution:")+" "+tmp.setNum(xres)+" x "+tmp2.setNum(yres)+" "+ tr("DPI"));
00176                      QString cSpace;
00177                      if (((ext == "pdf") || (ext == "eps") || (ext == "epsi") || (ext == "ps")) && (im.imgInfo.type != 7))
00178                             cSpace = tr("Unknown");
00179                      else
00180                      {
00181                             switch (im.imgInfo.colorspace)
00182                             {
00183                                    case 0:
00184                                           cSpace = tr("RGB");
00185                                           break;
00186                                    case 1:
00187                                           cSpace = tr("CMYK");
00188                                           break;
00189                                    case 2:
00190                                           cSpace = tr("Grayscale");
00191                                           break;
00192                                    case 3:
00193                                           cSpace = tr("Duotone");
00194                                           break;
00195                             }
00196                      }
00197                      p.drawText(2, h-5, tr("Colorspace:")+" "+cSpace);
00198                      p.end();
00199                      repaint();
00200               }
00201        }
00202        else
00203        {
00204               if (loadText(name, &Buffer))
00205               {
00206                      if (Buffer.startsWith("<SCRIBUS"))
00207                      {
00208                             QDomDocument docu("scridoc");
00209                             if(!docu.setContent(Buffer))
00210                                    return;
00211                             QDomElement elem=docu.documentElement();
00212                             if ((elem.tagName() != "SCRIBUS") && (elem.tagName() != "SCRIBUSUTF8") && (elem.tagName() != "SCRIBUSUTF8NEW"))
00213                                    return;
00214                             QDomNode DOC=elem.firstChild();
00215                             QDomElement dc=DOC.toElement();
00216                             QString Tit = tr("Title:")+" ";
00217                             QString ti2 = dc.attribute("TITLE");
00218                             if (ti2.isEmpty())
00219                                    ti2= tr("No Title");
00220                             Tit += ti2+"\n";
00221                             QString Aut = tr("Author:")+" ";
00222                             QString au2 = dc.attribute("AUTHOR");
00223                             if (au2.isEmpty())
00224                                    au2 = tr("Unknown");
00225                             Aut += au2;
00226                             setText( tr("Scribus Document")+"\n\n"+Tit+Aut);
00227                      }
00228                      else  if ((ext == "txt") || (ext == "html") || (ext == "xml"))
00229                             setText(Buffer.left(200));
00230               }
00231        }
00232 }
00233 
00234 void FDialogPreview::previewUrl( const QUrl &url )
00235 {
00236        if (url.isLocalFile())
00237        {
00238               QFileInfo finfo(url.path());
00239               if (filePath != finfo.filePath())
00240               {
00241                      filePath = finfo.filePath();
00242                      GenPreview(url.path());
00243               }
00244        }
00245 }
00246 
00247 CustomFDialog::CustomFDialog(QWidget *parent, QString wDir, QString caption, QString filter, int flags)
00248                      : QFileDialog(QString::null, filter, parent, 0, true), optionFlags(flags)
00249 {
00250        setIcon(loadIcon("AppIcon.png"));
00251        setCaption(caption);
00252        cDir = QDir(wDir);
00253        setDir(cDir);
00254        setIconProvider(new ImIconProvider(this));
00255        FDialogPreview *pw;
00256        if (flags & fdDirectoriesOnly)
00257        {
00258               Layout = new QFrame(this);
00259               Layout1 = new QHBoxLayout(Layout);
00260               Layout1->setSpacing( 0 );
00261               Layout1->setMargin( 0 );
00262               SaveZip = new QCheckBox( tr( "&Compress File" ), Layout, "test");
00263               Layout1->addWidget(SaveZip, Qt::AlignLeft);
00264               QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00265               Layout1->addItem( spacer );
00266               addWidgets(0, Layout, 0);
00267               LayoutC = new QFrame(this);
00268               Layout1C = new QHBoxLayout(LayoutC);
00269               Layout1C->setSpacing( 0 );
00270               Layout1C->setMargin( 0 );
00271               WithFonts = new QCheckBox( tr( "&Include Fonts" ), LayoutC, "WithFonts");
00272               Layout1C->addWidget(WithFonts, Qt::AlignLeft);
00273               WithProfiles = new QCheckBox( tr( "&Include ICC Profiles" ), LayoutC, "WithProfiles");
00274               Layout1C->addWidget(WithProfiles, Qt::AlignLeft);
00275               QSpacerItem* spacer2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00276               Layout1C->addItem( spacer2 );
00277               addWidgets(0, LayoutC, 0);
00278               setMode(QFileDialog::DirectoryOnly);
00279        }
00280        else
00281        {
00282               setContentsPreviewEnabled( true );
00283               pw = new FDialogPreview( this );
00284               setContentsPreview( pw, pw );
00285               if (flags & fdCompressFile)
00286               {
00287                      Layout = new QFrame(this);
00288                      Layout1 = new QHBoxLayout(Layout);
00289                      Layout1->setSpacing( 6 );
00290                      Layout1->setMargin( 0 );
00291                      SaveZip = new QCheckBox( tr( "&Compress File" ), Layout, "test");
00292                      Layout1->addWidget(SaveZip);
00293                      QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00294                      Layout1->addItem( spacer );
00295               }
00296               if (flags & fdExistingFiles)
00297                      setMode(QFileDialog::ExistingFile);
00298               else
00299               {
00300                      setMode(QFileDialog::AnyFile);
00301                      if (flags & fdCompressFile)
00302                             addWidgets(0, Layout, 0);
00303               }
00304               if (flags & fdShowCodecs)
00305               {
00306                      LayoutC = new QFrame(this);
00307                      Layout1C = new QHBoxLayout(LayoutC);
00308                      Layout1C->setSpacing( 0 );
00309                      Layout1C->setMargin( 4 );
00310                      TxCodeT = new QLabel(this);
00311                      TxCodeT->setText( tr("Encoding:"));
00312                      TxCodeM = new ScComboBox(true, LayoutC, "Cod");
00313                      TxCodeM->setEditable(false);
00314                      QString tmp_txc[] = {"ISO 8859-1", "ISO 8859-2", "ISO 8859-3",
00315                                                         "ISO 8859-4", "ISO 8859-5", "ISO 8859-6",
00316                                                         "ISO 8859-7", "ISO 8859-8", "ISO 8859-9",
00317                                                         "ISO 8859-10", "ISO 8859-13", "ISO 8859-14",
00318                                                         "ISO 8859-15", "UTF-8", "UTF-16", "KOI8-R", "KOI8-U",
00319                                                         "CP1250", "CP1251", "CP1252", "CP1253",
00320                                                         "CP1254", "CP1255", "CP1256", "CP1257",
00321                                                         "Apple Roman"};
00322                      size_t array = sizeof(tmp_txc) / sizeof(*tmp_txc);
00323                      for (uint a = 0; a < array; ++a)
00324                             TxCodeM->insertItem(tmp_txc[a]);
00325                      QString localEn = QTextCodec::codecForLocale()->name();
00326                      if (localEn == "ISO-10646-UCS-2")
00327                             localEn = "UTF-16";
00328                      bool hasIt = false;
00329                      for (int cc = 0; cc < TxCodeM->count(); ++cc)
00330                      {
00331                             if (TxCodeM->text(cc) == localEn)
00332                             {
00333                                    TxCodeM->setCurrentItem(cc);
00334                                    hasIt = true;
00335                                    break;
00336                             }
00337                      }
00338                      if (!hasIt)
00339                      {
00340                             TxCodeM->insertItem(localEn);
00341                             TxCodeM->setCurrentItem(TxCodeM->count()-1);
00342                      }
00343                      TxCodeM->setMinimumSize(QSize(200, 0));
00344                      Layout1C->addWidget(TxCodeM);
00345                      QSpacerItem* spacer2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00346                      Layout1C->addItem( spacer2 );
00347                      addWidgets(TxCodeT, LayoutC, 0);
00348               }
00349               setPreviewMode((flags & fdShowPreview) ? QFileDialog::Contents : QFileDialog::NoPreview );
00350               setViewMode( QFileDialog::List );
00351               if (flags & fdCompressFile)
00352                      connect(SaveZip, SIGNAL(clicked()), this, SLOT(handleCompress()));
00353        }
00354        HomeB = new QToolButton(this);
00355        HomeB->setIconSet(loadIcon("16/go-home.png"));
00356        #ifdef _WIN32
00357        HomeB->setAutoRaise(true);
00358        #endif
00359        HomeB->setTextLabel( tr("Moves to your Document Directory.\nThis can be set in the Preferences."));
00360        connect(HomeB, SIGNAL(clicked()), this, SLOT(slotHome()));
00361        addToolButton(HomeB);
00362        // default init
00363        extZip = "gz";
00364 }
00365 
00366 CustomFDialog::~CustomFDialog()
00367 {
00368        setIconProvider(0);
00369        const QDir* d(dir());
00370        cDir.setCurrent(d->path());
00371        delete d;
00372 }
00373 
00374 void CustomFDialog::slotHome()
00375 {
00376        setDir(QDir(DocDir));
00377 }
00378 
00379 void CustomFDialog::handleCompress()
00380 {
00381        QFileInfo tmp;
00382        tmp.setFile(selectedFile());
00383        QString e(tmp.extension());
00384        QStringList ex = QStringList::split(".", e);
00385        QString baseExt = "";
00386        for (uint a = 0; a < ex.count(); a++)
00387        {
00388               if ((ex[a] != "sla") && (ex[a] != "SLA") && (ex[a] != "gz") && (ex[a] != "GZ"))
00389                      baseExt += "."+ex[a];
00390        }
00391        if (SaveZip->isChecked())
00392        {
00393               if (e != extZip)
00394                      tmp.setFile(tmp.baseName() + baseExt + "." + extZip);
00395        }
00396        else
00397        {
00398               if (e != ext)
00399                      tmp.setFile(tmp.baseName() + baseExt + "." + ext);
00400        }
00401        setSelection(tmp.fileName());
00402 }
00403 
00404 void CustomFDialog::setExtension(QString e)
00405 {
00406        ext = e;
00407 }
00408 
00409 QString CustomFDialog::extension()
00410 {
00411        return ext;
00412 }
00413 
00414 void CustomFDialog::setZipExtension(QString e)
00415 {
00416        extZip = e;
00417 }
00418 
00419 QString CustomFDialog::zipExtension()
00420 {
00421        return extZip;
00422 }