Back to index

scribus-ng  1.3.4.dfsg+svn20071115
gtdialogs.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) 2004 by Riku Leino                                      *
00009  *   tsoots@gmail.com                                                      *
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 "gtdialogs.h"
00028 #include "gtdialogs.moc"
00029 #include "prefsmanager.h"
00030 #include "prefscontext.h"
00031 #include "prefsfile.h"
00032 #include "sccombobox.h"
00033 #include <qlabel.h>
00034 #include "commonstrings.h"
00035 
00036 extern QPixmap loadIcon(QString nam);
00037 extern QString DocDir;
00038 
00039 /********* Class gtFileDialog ************************************************************************/
00040 
00041 gtFileDialog::gtFileDialog(const QString& filters, const QStringList& importers, const QString& wdir) : 
00042                QFileDialog(QString::null, filters, 0, 0, true)
00043 {
00044 //     setIcon(loadIcon("AppIcon.png"));
00045        setCaption( tr("Open"));
00046        dir = QDir(wdir);
00047        setDir(dir);
00048        setMode(QFileDialog::ExistingFile);
00049        createWidgets(importers);
00050 }
00051 
00052 void gtFileDialog::createWidgets(const QStringList& importers)
00053 {
00054        importerFrame = new QFrame(this);
00055        importerLayout = new QHBoxLayout(importerFrame);
00056        importerLayout->setSpacing(10);
00057        importerLayout->setMargin(0);
00058 
00059        importerCombo = new ScComboBox(0, importerFrame, "importerCombo");
00060        importerCombo->setMinimumSize(QSize(150, 0));
00061        QToolTip::add(importerCombo, tr("Choose the importer to use"));
00062        importerCombo->insertItem( tr("Automatic"));
00063        importerCombo->insertStringList(importers);
00064        importerLayout->addWidget(importerCombo);
00065 
00066        textOnlyCheckBox = new QCheckBox(importerFrame, "textOnlyCB");
00067        textOnlyCheckBox->setText( tr("Import Text Only"));
00068        QToolTip::add(textOnlyCheckBox, tr("Import text without any formatting"));
00069 //                                        "\nNotice that not all importers provide this feature."));
00070        importerLayout->addWidget(textOnlyCheckBox);
00071 
00072        QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00073        importerLayout->addItem(spacer);
00074 
00075        addWidgets(new QLabel( tr("Importer:"), this), importerFrame, 0);
00076 
00077        encodingFrame = new QFrame(this);
00078        encodingLayout = new QHBoxLayout(encodingFrame);
00079        encodingLayout->setSpacing(10);
00080        encodingLayout->setMargin(0);
00081 
00082        encodingCombo = new ScComboBox(true, encodingFrame, "encodingCombo");
00083        encodingCombo->setEditable(false);
00084        QString tmp_txc[] = {"ISO 8859-1", "ISO 8859-2", "ISO 8859-3", "ISO 8859-4", "ISO 8859-5", "ISO 8859-6",
00085                                       "ISO 8859-7", "ISO 8859-8", "ISO 8859-9", "ISO 8859-10", "ISO 8859-13", "ISO 8859-14",
00086                                       "ISO 8859-15", "UTF-8", "UTF-16", "KOI8-R", "KOI8-U", "CP1250", "CP1251", "CP1252", "CP1253",
00087                                       "CP1254", "CP1255", "CP1256", "CP1257", "Apple Roman"};
00088        size_t array = sizeof(tmp_txc) / sizeof(*tmp_txc);
00089        for (uint a = 0; a < array; ++a)
00090               encodingCombo->insertItem(tmp_txc[a]);
00091        QString localEn = QTextCodec::codecForLocale()->name();
00092        if (localEn == "ISO-10646-UCS-2")
00093               localEn = "UTF-16";
00094        bool hasIt = false;
00095        for (int cc = 0; cc < encodingCombo->count(); ++cc)
00096        {
00097               if (encodingCombo->text(cc) == localEn)
00098               {
00099                      encodingCombo->setCurrentItem(cc);
00100                      hasIt = true;
00101                      break;
00102               }
00103        }
00104        if (!hasIt)
00105        {
00106               encodingCombo->insertItem(localEn);
00107               encodingCombo->setCurrentItem(encodingCombo->count()-1);
00108        }
00109        encodingCombo->setMinimumSize(QSize(150, 0));
00110        encodingLayout->addWidget(encodingCombo);
00111        QSpacerItem* spacer2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00112        encodingLayout->addItem( spacer2 );
00113        addWidgets(new QLabel( tr("Encoding:"), this), encodingFrame, 0);
00114 
00115        HomeB = new QToolButton(this);
00116        HomeB->setIconSet(loadIcon("16/go-home.png"));
00117        #ifdef _WIN32
00118        HomeB->setAutoRaise(true);
00119        #endif
00120 //     HomeB->setTextLabel( tr("Moves to your Document Directory.\nThis can be set in the Preferences."));
00121        connect(HomeB, SIGNAL(clicked()), this, SLOT(slotHome()));
00122        addToolButton(HomeB);
00123 }
00124 
00125 void gtFileDialog::slotHome()
00126 {
00127        setDir(QDir(DocDir));
00128 }
00129 
00130 gtFileDialog::~gtFileDialog()
00131 {
00132 
00133 }
00134 
00135 /********* Class gtImporterDialog*******************************************************************/
00136 
00137 gtImporterDialog::gtImporterDialog(const QStringList& importers, int currentSelection)
00138 {
00139        setCaption( tr("Choose the importer to use"));
00140        setIcon(loadIcon("AppIcon.png"));
00141 
00142        QBoxLayout* layout = new QVBoxLayout(this);
00143 
00144        QBoxLayout* llayout = new QHBoxLayout(0, 5, 5, "llayout");
00145        QLabel* label = new QLabel( tr("Choose the importer to use"), this, "label");
00146        llayout->addWidget(label);
00147        layout->addLayout(llayout);
00148 
00149        QBoxLayout* ilayout = new QHBoxLayout(0, 5, 5, "dlayout2");
00150        importerCombo = new ScComboBox(0, this, "importerCombo2");
00151        importerCombo->setMinimumSize(QSize(150, 0));
00152        QToolTip::add(importerCombo, tr("Choose the importer to use"));
00153        importerCombo->insertStringList(importers);
00154        if (static_cast<int>(importers.count()) > currentSelection)
00155               importerCombo->setCurrentItem(currentSelection);
00156        else
00157               importerCombo->setCurrentItem(0);
00158        ilayout->addWidget(importerCombo);
00159        layout->addLayout(ilayout);
00160 
00161        QBoxLayout* dlayout = new QHBoxLayout(0, 5, 5, "dlayout2");
00162        rememberCheck = new QCheckBox( tr("Remember association"), this, "rememberCheck");
00163        rememberCheck->setChecked(false);
00164        QToolTip::add(rememberCheck, "<qt>" + tr("Remember the file extension - importer association and do not ask again to select an importer for files of this type.") + "</qt>" );
00165        dlayout->addStretch(10);
00166        dlayout->addWidget(rememberCheck);
00167        layout->addLayout(dlayout);
00168 
00169        QBoxLayout* blayout = new QHBoxLayout(0, 5, 5, "blayout2");
00170        blayout->addStretch(10);
00171        okButton = new QPushButton( CommonStrings::tr_OK, this, "okButton2");
00172        blayout->addWidget(okButton);
00173        layout->addLayout(blayout);
00174 
00175        connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
00176 }
00177 
00178 bool gtImporterDialog::shouldRemember()
00179 {
00180        return rememberCheck->isChecked();
00181 }
00182 
00183 QString gtImporterDialog::getImporter()
00184 {
00185        return importerCombo->currentText();
00186 }
00187 
00188 gtImporterDialog::~gtImporterDialog()
00189 {
00190 
00191 }
00192 
00193 /********* Class gtDialogs *************************************************************************/
00194 
00195 gtDialogs::gtDialogs()
00196 {
00197        fdia = NULL;
00198        fileName = "";
00199        encoding = "";
00200        importer = -1;
00201        prefs = PrefsManager::instance()->prefsFile->getContext("gtDialogs");
00202        pwd = QDir::currentDirPath();
00203 }
00204 
00205 bool gtDialogs::runFileDialog(const QString& filters, const QStringList& importers)
00206 {
00207        bool accepted = false;
00208        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00209        QString dir = dirs->get("get_text", ".");
00210        fdia = new gtFileDialog(filters, importers, dir);
00211        
00212        if (fdia->exec() == QDialog::Accepted)
00213        {
00214               fileName = fdia->selectedFile();
00215               if (!fileName.isEmpty())
00216                      accepted = true;
00217               encoding = fdia->encodingCombo->currentText();
00218               if (encoding == "UTF-16")
00219                      encoding = "ISO-10646-UCS-2";
00220               importer = fdia->importerCombo->currentItem() - 1;
00221               dirs->set("get_text", fileName.left(fileName.findRev("/")));
00222        }
00223        QDir::setCurrent(pwd);
00224        return accepted;
00225 }
00226 
00227 bool gtDialogs::runImporterDialog(const QStringList& importers)
00228 {
00229        int curSel = prefs->getInt("curSel", 0);
00230        QString extension = "";
00231        QString shortName = fileName.right(fileName.length() - fileName.findRev("/") - 1);
00232        if (shortName.find(".") == -1)
00233               extension = ".no_extension";
00234        else
00235               extension = fileName.right(fileName.length() - fileName.findRev("."));
00236        int extensionSel = prefs->getInt(extension, -1);
00237        QString imp = prefs->get("remember"+extension, QString("false"));
00238        QString res = "";
00239        bool shouldRemember = false;
00240        bool ok = false;
00241        if (imp != "false")
00242        {
00243               res = imp;
00244               if (importers.contains(res) > 0)
00245                      ok = true;
00246        }
00247        
00248        if (!ok)
00249        {
00250               if ((extensionSel > -1) && (extensionSel < static_cast<int>(importers.count())))
00251                      curSel = extensionSel;
00252               else
00253                      curSel = 0;
00254               gtImporterDialog* idia = new gtImporterDialog(importers, curSel);
00255               if (idia->exec())
00256               {
00257                      res = idia->getImporter();
00258                      shouldRemember = idia->shouldRemember();
00259                      
00260                      ok = true;
00261               }
00262               delete idia;
00263        }
00264 
00265        if (ok)
00266        {
00267               QString fileExtension = "";
00268               for (uint i = 0; i < importers.count(); ++i)
00269               {
00270                      if (importers[i] == res)
00271                      {
00272                             importer = i;
00273                             prefs->set("curSel", static_cast<int>(i));
00274                             if (fileName.find(".") != -1)
00275                             {
00276                                    if (shortName.find(".") == -1)
00277                                           fileExtension = ".no_extension";
00278                                    else
00279                                           fileExtension = fileName.right(fileName.length() - fileName.findRev("."));
00280                                    if (!fileExtension.isEmpty())
00281                                    {
00282                                           prefs->set(fileExtension, static_cast<int>(i));
00283                                           if (shouldRemember)
00284                                                  prefs->set("remember"+fileExtension, res);
00285                                    }
00286                             }
00287                             break;
00288                      }
00289               }
00290        }
00291        return ok;
00292 }
00293 
00294 const QString& gtDialogs::getFileName()
00295 {
00296        return fileName;
00297 }
00298 
00299 const QString& gtDialogs::getEncoding()
00300 {
00301        return encoding;
00302 }
00303 
00304 int gtDialogs::getImporter()
00305 {
00306        return importer;
00307 }
00308 
00309 bool gtDialogs::importTextOnly()
00310 {
00311        bool ret = false;
00312        if (fdia)
00313               ret = fdia->textOnlyCheckBox->isChecked();
00314        return ret;
00315 }
00316 
00317 gtDialogs::~gtDialogs()
00318 {
00319        delete fdia;
00320 }