Back to index

scribus-ng  1.3.4.dfsg+svn20071115
mergedoc.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 #include <qdialog.h>
00008 #include <qpushbutton.h>
00009 #include <qlabel.h>
00010 #include <qlineedit.h>
00011 #include <qspinbox.h>
00012 #include <qstring.h>
00013 #include <qlayout.h>
00014 #include <qtooltip.h>
00015 #include <qcombobox.h>
00016 #include <qcheckbox.h>
00017 #include <qtooltip.h>
00018 
00019 #include "scconfig.h"
00020 #include "mergedoc.h"
00021 #include "mergedoc.moc"
00022 #include "commonstrings.h"
00023 #include "customfdialog.h"
00024 #include "fileloader.h"
00025 #include "prefsfile.h"
00026 #include "prefsmanager.h"
00027 #include "sccombobox.h"
00028 #include "scpaths.h"
00029 
00030 #include <qcursor.h>
00031 
00032 extern QPixmap loadIcon(QString nam);
00033 
00034 MergeDoc::MergeDoc( QWidget* parent, bool importMasterPages, int targetDocPageCount, int currentPage ) : 
00035     QDialog( parent, "merge", true, 0 )
00036 {
00037        masterPages = importMasterPages;
00038        setCaption( (masterPages) ? tr("Import Master Page") : tr( "Import Page(s)" ) );
00039        setIcon(loadIcon("AppIcon.png"));
00040 
00041        count = 0;
00042        dialogLayout = new QVBoxLayout( this, 10, 10, "dialogLayout"); 
00043        fromInfoLayout = new QGridLayout(1, 1, 5, "layout");
00044        fromDocData = new QLineEdit( this, "fromDocData" );
00045        fromDocLabel = new QLabel( fromDocData, tr( "&From Document:"), this, "fromDocLabel" );
00046        fromInfoLayout->addWidget( fromDocLabel, 0, 0 );
00047        fromInfoLayout->addWidget( fromDocData, 0, 1 );
00048        changeButton = new QPushButton( tr( "Chan&ge..." ), this, "changeButton" );
00049        changeButton->setAutoDefault( false );
00050        fromInfoLayout->addWidget( changeButton, 0, 2 );
00051        importPageLabel = new QLabel( tr( "&Import Page(s):" ), this, "importPageLabel" );
00052        fromInfoLayout->addWidget( importPageLabel, 1, 0 );
00053        if (masterPages)
00054        {
00055               importPageLabel->setText( tr("&Import Master Page") );
00056               masterPageNameData = new ScComboBox( false, this, "masterPageNameData" );
00057               masterPageNameData->setEnabled(false);
00058               importPageLabel->setBuddy( masterPageNameData );
00059               fromInfoLayout->addWidget( masterPageNameData, 1, 1 );
00060        }
00061        else
00062        {
00063               pageNumberData = new QLineEdit( this, "pageNumberData" );
00064               pageNumberData->setEnabled(false);
00065               importPageLabel->setBuddy( pageNumberData );
00066               QToolTip::add( pageNumberData, "<qt>" + tr( "Insert a comma separated list of tokens import where "
00067                                          "a token can be * for all the pages, 1-5 for "
00068                                          "a range of pages or a single page number.") + "</qt>");
00069               fromInfoLayout->addWidget( pageNumberData, 1, 1 );
00070               fromLabel = new QLabel(this, "fromLabel");
00071               fromLabel->setText( tr(" from 0"));
00072               fromInfoLayout->addWidget( fromLabel, 1, 2 );
00073               createPageData = new QCheckBox( this, "createPageData" );
00074               createPageData->setText( tr("Create Page(s)"));
00075               fromInfoLayout->addWidget( createPageData, 2, 0 );
00076               importWhereData = new ScComboBox( false, this, "positionCombo" );
00077               importWhereData->setEnabled(false);
00078               importWhereData->insertItem( tr("Before Page"));
00079               importWhereData->insertItem( tr("After Page"));
00080               importWhereData->insertItem( tr("At End"));
00081               importWhereData->setCurrentItem( 2 );
00082               fromInfoLayout->addWidget( importWhereData, 2, 1 );
00083               importWherePageData = new QSpinBox( 1, targetDocPageCount, 1, this, "pageSpin" );
00084               importWherePageData->setValue( currentPage );
00085               importWherePageData->setEnabled(false);
00086               fromInfoLayout->addWidget( importWherePageData, 2, 2 );
00087        }
00088 
00089        dialogLayout->addLayout( fromInfoLayout );
00090        importCancelLayout = new QHBoxLayout( 0, 0, 2, "importCancelLayout"); 
00091        QSpacerItem* spacer = new QSpacerItem( 41, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00092        importCancelLayout->addItem( spacer );
00093        importButton = new QPushButton( tr( "&Import" ), this, "importButton" );
00094        importButton->setEnabled(false);
00095        importCancelLayout->addWidget( importButton );
00096        cancelButton = new QPushButton( CommonStrings::tr_Cancel, this, "cancelButton" );
00097        cancelButton->setDefault( true );
00098        importCancelLayout->addWidget( cancelButton );
00099        dialogLayout->addLayout( importCancelLayout );
00100        resize( QSize(350, 134).expandedTo(minimumSizeHint()) );
00101        clearWState( WState_Polished );
00102        connect( importButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
00103        connect( cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
00104        connect( changeButton, SIGNAL( clicked() ), this, SLOT( changeFile() ) );
00105        if (!masterPages)
00106        {
00107               connect( importWhereData, SIGNAL( activated(int) ), this, SLOT( checkDestPageStatus(int) ) );
00108               connect( createPageData, SIGNAL( clicked() ), this, SLOT( enableCreateWidgets() ) );
00109        }
00110 }
00111 
00112 /*
00113  *  Destroys the object and frees any allocated resources
00114  */
00115 MergeDoc::~MergeDoc()
00116 {
00117     // no need to delete child widgets, Qt does it all for us
00118 }
00119 
00120 void MergeDoc::changeFile()
00121 {
00122        QString fn;
00123        int dummy;
00124        count = 0;
00125        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00126        QString wdir = dirs->get("merge", ".");
00127        CustomFDialog *dia = new CustomFDialog(this, wdir, tr("Open"), tr("Documents (*.sla *.sla.gz *.scd *.scd.gz);;All Files (*)"));
00128        if (!fromDocData->text().isEmpty())
00129               dia->setSelection(fromDocData->text());
00130        if (dia->exec() == QDialog::Accepted)
00131        {
00132               fn = dia->selectedFile();
00133               if (!fn.isEmpty())
00134               {
00135                      dirs->set("merge", fn.left(fn.findRev("/")));
00136                      qApp->setOverrideCursor(QCursor(waitCursor), true);
00137                      FileLoader fl(fn);
00138                      if (fl.TestFile() == -1)
00139                      //TODO put in nice user warning
00140                             return;
00141                      QStringList masterPageNames;
00142                      bool ret = false;
00143                      if (masterPages)
00144                             ret = fl.ReadPageCount(fn, &dummy, &count, masterPageNames);
00145                      else
00146                             ret = fl.ReadPageCount(fn, &count, &dummy, masterPageNames);
00147                      qApp->setOverrideCursor(QCursor(arrowCursor), true);
00148                      if ((ret) && (count != 0))
00149                      {
00150                             fromDocData->setText( QDir::convertSeparators(fn) );
00151                             importButton->setEnabled(true);
00152                             if (masterPages)
00153                             {
00154                                    masterPageNameData->clear();
00155                                    masterPageNameData->setEnabled(true);
00156                                    masterPageNameData->insertStringList(masterPageNames);
00157                             }
00158                             else
00159                             {
00160                                    pageNumberData->setEnabled(true);
00161                             }
00162                             if (!masterPages)
00163                                    fromLabel->setText( tr(" from %1").arg(count));
00164                      }
00165               }
00166        }
00167        else
00168        {
00169               fromDocData->setText("");
00170               count = 0;
00171               importButton->setEnabled(false);
00172               if (masterPages)
00173               {
00174                      masterPageNameData->clear();
00175                      masterPageNameData->setEnabled(false);
00176               }
00177               else
00178               {
00179                      pageNumberData->setEnabled(false);
00180               }
00181               if (!masterPages)
00182                      fromLabel->setText( tr(" from %1").arg(count));
00183        }
00184        delete dia;
00185 }
00186 
00187 void MergeDoc::checkDestPageStatus(int positionComboSelection)
00188 {
00189        importWherePageData->setDisabled( positionComboSelection == 2 );
00190 }
00191 
00192 void MergeDoc::enableCreateWidgets()
00193 {
00194        if (createPageData->isChecked())
00195        {
00196               importWhereData->setEnabled(true);
00197               if (importWhereData->currentItem() != 2)
00198                      importWherePageData->setEnabled(true);
00199        }
00200        else
00201        {
00202               importWhereData->setEnabled(false);
00203               importWherePageData->setEnabled(false);
00204        }
00205 }
00206 
00207 const QString MergeDoc::getFromDoc()
00208 {
00209        return ScPaths::separatorsToSlashes(fromDocData->text());
00210 }
00211 
00212 const int MergeDoc::getMasterPageNameItem()
00213 {
00214        return masterPageNameData->currentItem();
00215 }
00216 
00217 const QString MergeDoc::getMasterPageNameText()
00218 {
00219        return masterPageNameData->currentText();
00220 }
00221 
00222 const int MergeDoc::getImportWhere()
00223 {
00224        return importWhereData->currentItem();
00225 }
00226 
00227 const int MergeDoc::getImportWherePage()
00228 {
00229        return importWherePageData->value();
00230 }
00231 
00232 const bool MergeDoc::getCreatePageChecked()
00233 {
00234        return createPageData->isChecked();
00235 }
00236 
00237 const QString MergeDoc::getPageNumbers()
00238 {
00239        return pageNumberData->text();
00240 }
00241 
00242 const int MergeDoc::getPageCounter()
00243 {
00244        return count;
00245 }