Back to index

scribus-ng  1.3.4.dfsg+svn20071115
muster.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 "muster.h"
00008 #include "muster.moc"
00009 #include "newtemp.h"
00010 #include "mergedoc.h"
00011 #include <qinputdialog.h>
00012 #include <qlayout.h>
00013 #include <qlistbox.h>
00014 #include <qmessagebox.h>
00015 #include <qpushbutton.h>
00016 #include <qcursor.h>
00017 #include <qstring.h>
00018 #include <qtooltip.h>
00019 #include "page.h"
00020 #include "pagestructs.h"
00021 #include "scribusdoc.h"
00022 #include "scribusview.h"
00023 #include "util.h"
00024 #include "commonstrings.h"
00025 #include "scribus.h"
00026 #include "scribusXml.h"
00027 #include "prefsmanager.h"
00028 
00029 #include "scmessagebox.h"
00030 
00031 MasterPagesPalette::MasterPagesPalette( QWidget* parent, ScribusDoc *pCurrentDoc, ScribusView *pCurrentView, QString masterPageName)
00032               : QDialog( parent, "Muster", false, WDestructiveClose)
00033 {
00034        setCaption( tr( "Edit Master Pages" ) );
00035        setIcon(loadIcon("AppIcon.png"));
00036        currentDoc = pCurrentDoc;
00037        currentView = pCurrentView;
00038        masterPagesLayout = new QVBoxLayout( this, 5, 5 );
00039        buttonLayout = new QHBoxLayout;
00040        buttonLayout->setSpacing( 5 );
00041        buttonLayout->setMargin( 0 );
00042        importButton = new QToolButton(this, "importButton" );
00043        importButton->setPixmap(loadIcon("16/document-open.png"));
00044        newButton = new QToolButton(this, "newButton" );
00045        newButton->setPixmap(loadIcon("16/document-new.png"));
00046        duplicateButton = new QToolButton(this, "DublicateB" );
00047        duplicateButton->setPixmap(loadIcon("16/edit-copy.png"));
00048        deleteButton = new QToolButton(this, "deleteButton" );
00049        deleteButton->setPixmap(loadIcon("16/edit-delete.png"));
00050        buttonLayout->addWidget( newButton );
00051        buttonLayout->addWidget( duplicateButton );
00052        buttonLayout->addWidget( importButton );
00053        buttonLayout->addWidget( deleteButton );
00054        QSpacerItem* spacer = new QSpacerItem( 16, 16, QSizePolicy::Expanding, QSizePolicy::Minimum );
00055        buttonLayout->addItem( spacer );
00056        masterPagesLayout->addLayout( buttonLayout );
00057        masterPageListBox = new QListBox( this, "masterPageListBox" );
00058        masterPageListBox->setMinimumSize( QSize( 100, 240 ) );
00059        masterPagesLayout->addWidget( masterPageListBox );
00060 
00061 
00062        if (masterPageName.isEmpty())
00063               sMuster = currentDoc->MasterNames.begin().key();
00064        else
00065               sMuster = masterPageName;
00066        updateMasterPageList(sMuster);
00067        currentView->showMasterPage(currentDoc->MasterNames[sMuster]);
00068 
00069        setMinimumSize(sizeHint());
00070 
00071        QToolTip::add( duplicateButton, tr( "Duplicate the selected master page" ) );
00072        QToolTip::add( deleteButton, tr( "Delete the selected master page" ) );
00073        QToolTip::add( newButton, tr( "Add a new master page" ) );
00074        QToolTip::add( importButton, tr( "Import master pages from another document" ) );
00075        // signals and slots connections
00076        connect(duplicateButton, SIGNAL(clicked()), this, SLOT(duplicateMasterPage()));
00077        connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteMasterPage()));
00078        connect(newButton, SIGNAL(clicked()), this, SLOT(newMasterPage()));
00079        connect(importButton, SIGNAL(clicked()), this, SLOT(appendPage()));
00080        connect(masterPageListBox, SIGNAL(highlighted(QListBoxItem*)), this, SLOT(selectMasterPage(QListBoxItem*)));
00081        connect(masterPageListBox, SIGNAL(doubleClicked(QListBoxItem*)), this, SLOT(renameMasterPage( QListBoxItem*)));
00082 }
00083 
00084 void MasterPagesPalette::reject()
00085 {
00086        emit finished();
00087        QDialog::reject();
00088 }
00089 
00090 void MasterPagesPalette::closeEvent(QCloseEvent *closeEvent)
00091 {
00092        emit finished();
00093        closeEvent->accept();
00094 }
00095 
00096 void MasterPagesPalette::deleteMasterPage()
00097 {
00098        if ((sMuster == CommonStrings::masterPageNormal) || (sMuster == CommonStrings::trMasterPageNormal) || (sMuster == CommonStrings::trMasterPageNormalLeft) || (sMuster == CommonStrings::trMasterPageNormalMiddle) || (sMuster == CommonStrings::trMasterPageNormalRight))
00099               return;
00100        int exit=ScMessageBox::warning(this,
00101                                      CommonStrings::trWarning,
00102                                      tr("Do you really want to delete this master page?"),
00103                                      CommonStrings::trYesKey,
00104                                      CommonStrings::trNoKey,
00105                                      0, QMessageBox::No, QMessageBox::Yes);
00106        if (exit == 0)
00107        {
00108               currentDoc->scMW()->DeletePage2(currentDoc->MasterNames[sMuster]);
00109               //<<CB TODO Move back into ScribusDoc::deleteMasterPage();
00110               //This must happen after the pages have been reformed (view/doc)
00111               currentDoc->MasterNames.clear();
00112               for (uint a = 0; a < currentDoc->Pages->count(); ++a)
00113                      currentDoc->MasterNames[currentDoc->Pages->at(a)->pageName()] = currentDoc->Pages->at(a)->pageNr();
00114               // and fix up any pages that refer to the deleted master page
00115               uint pageIndex = 0;
00116               QMap<QString,int>::Iterator it = currentDoc->MasterNames.begin();
00117               for (Page* docPage = currentDoc->DocPages.first(); docPage; docPage = currentDoc->DocPages.next() )
00118               {
00119                      if (docPage->MPageNam == sMuster)
00120                      {
00121                             PageLocation pageLoc = currentDoc->locationOfPage(pageIndex);
00122                             if (pageLoc == LeftPage)
00123                             {
00124                                    if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormalLeft))
00125                                           docPage->MPageNam = CommonStrings::trMasterPageNormalLeft;
00126                                    else if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormal))
00127                                           docPage->MPageNam = CommonStrings::trMasterPageNormal;
00128                                    else
00129                                           docPage->MPageNam = it.key();
00130                             }
00131                             else if (pageLoc == RightPage)
00132                             {
00133                                    if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormalRight))
00134                                           docPage->MPageNam = CommonStrings::trMasterPageNormalRight;
00135                                    else if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormal))
00136                                           docPage->MPageNam = CommonStrings::trMasterPageNormal;
00137                                    else
00138                                           docPage->MPageNam = it.key();
00139                             }
00140                             else
00141                             {
00142                                    if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormalMiddle))
00143                                           docPage->MPageNam = CommonStrings::trMasterPageNormalMiddle;
00144                                    else if (currentDoc->MasterNames.contains( CommonStrings::trMasterPageNormal))
00145                                           docPage->MPageNam = CommonStrings::trMasterPageNormal;
00146                                    else
00147                                           docPage->MPageNam = it.key();
00148                             }
00149                      }
00150                      pageIndex++;
00151               }
00152               //>>
00153               
00154               sMuster = it.key();
00155               updateMasterPageList(sMuster);
00156               //currentDoc->MasterPages = currentDoc->Pages;
00157        }
00158 }
00159 
00160 void MasterPagesPalette::duplicateMasterPage()
00161 {
00162        int copyC = 1;
00163        QString potentialMasterPageName(sMuster);
00164        while (currentDoc->MasterNames.contains(potentialMasterPageName))
00165               potentialMasterPageName = tr("Copy #%1 of %2").arg(copyC++).arg(sMuster);
00166 
00167        NewTm *dia = new NewTm(this, tr("&Name:"), tr("New Master Page"), currentDoc, potentialMasterPageName);
00168        if (dia->exec())
00169        {
00170               QString MasterPageName = dia->Answer->text();
00171               while (currentDoc->MasterNames.contains(MasterPageName) || ((MasterPageName == CommonStrings::masterPageNormal) || (MasterPageName == CommonStrings::trMasterPageNormal) || (MasterPageName == CommonStrings::trMasterPageNormalLeft) || (MasterPageName == CommonStrings::trMasterPageNormalMiddle) || (MasterPageName == CommonStrings::trMasterPageNormalRight)))
00172               {
00173                      if (!dia->exec())
00174                      {
00175                             delete dia;
00176                             return;
00177                      }
00178                      MasterPageName = dia->Answer->text();
00179               }
00180               PrefsManager* prefsManager = PrefsManager::instance();
00181               int inde = currentDoc->MasterNames[sMuster];
00182               int nr = currentDoc->Pages->count();
00183               Page* from = currentDoc->Pages->at(inde);
00184               Page* destination = currentDoc->addMasterPage(nr, MasterPageName);
00185               if (currentDoc->currentPageLayout != singlePage)
00186               {
00187                      int lp = dia->Links->currentItem();
00188                      if (lp == 0)
00189                             lp = 1;
00190                      else if (lp == static_cast<int>(dia->Links->count()-1))
00191                             lp = 0;
00192                      else
00193                             lp++;
00194                      destination->LeftPg = lp;
00195               }
00196               destination->initialMargins.Top = from->initialMargins.Top;
00197               destination->initialMargins.Bottom = from->initialMargins.Bottom;
00198               if (currentDoc->pageSets[currentDoc->currentPageLayout].Columns == 1)
00199               {
00200                      destination->initialMargins.Left = from->initialMargins.Left;
00201                      destination->initialMargins.Right = from->initialMargins.Right;
00202               }
00203               else
00204               {
00205                      if (destination->LeftPg != from->LeftPg)
00206                      {
00207                             if (destination->LeftPg > 1)
00208                             {
00209                                    destination->initialMargins.Right = from->initialMargins.Left;
00210                                    destination->initialMargins.Left = from->initialMargins.Left;
00211                             }
00212                             else
00213                             {
00214                                    destination->initialMargins.Left = from->initialMargins.Left;
00215                                    destination->initialMargins.Right = from->initialMargins.Right;
00216                             }
00217                      }
00218                      else
00219                      {
00220                             destination->initialMargins.Left = from->initialMargins.Left;
00221                             destination->initialMargins.Right = from->initialMargins.Right;
00222                      }
00223               }
00224               currentDoc->setCurrentPage(destination);
00225               uint oldItems = currentDoc->Items->count();
00226               uint end2 = currentDoc->MasterItems.count();
00227               int GrMax = currentDoc->GroupCounter;
00228               currentDoc->m_Selection->clear();
00229               if (oldItems>0)
00230               {
00231                      for (uint ite = 0; ite < oldItems; ++ite)
00232                      {
00233                             PageItem *itemToCopy = currentDoc->Items->at(ite);
00234                             if (itemToCopy->OwnPage == inde)
00235                                    currentDoc->m_Selection->addItem(itemToCopy, true);
00236                      }
00237                      ScriXmlDoc *ss = new ScriXmlDoc();
00238                      ss->ReadElem(ss->WriteElem(currentDoc, currentView, currentDoc->m_Selection), prefsManager->appPrefs.AvailFonts, currentDoc, destination->xOffset(), destination->yOffset(), false, true, prefsManager->appPrefs.GFontSub, currentView);
00239                      currentDoc->m_Selection->clear();
00240               }
00241               uint end3 = currentDoc->MasterItems.count();
00242               for (uint a = end2; a < end3; ++a)
00243               {
00244                      PageItem *newItem = currentDoc->MasterItems.at(a);
00245                      newItem->OnMasterPage = MasterPageName;
00246                      newItem->OwnPage = currentDoc->MasterNames[MasterPageName];
00247               }
00248               from->guides.copy(&destination->guides);
00249               currentDoc->GroupCounter = GrMax + 1;
00250               currentView->Deselect(true);
00251               updateMasterPageList(MasterPageName);
00252               currentDoc->setLoading(false);
00253               currentView->reformPages();
00254               currentView->DrawNew();
00255        }
00256        delete dia;
00257 }
00258 
00259 void MasterPagesPalette::newMasterPage()
00260 {
00261        QString MasterPageName;
00262        int nr = currentDoc->Pages->count();
00263        NewTm *dia = new NewTm(this, tr("Name:"), tr("New MasterPage"), currentDoc, tr("New Master Page %1").arg(nr));
00264        if (dia->exec())
00265        {
00266               MasterPageName = dia->Answer->text();
00267               while (currentDoc->MasterNames.contains(MasterPageName) || ((MasterPageName == CommonStrings::masterPageNormal) || (MasterPageName == CommonStrings::trMasterPageNormal) || (MasterPageName == CommonStrings::trMasterPageNormalLeft) || (MasterPageName == CommonStrings::trMasterPageNormalMiddle) || (MasterPageName == CommonStrings::trMasterPageNormalRight)))
00268               {
00269                      if (!dia->exec())
00270                      {
00271                             delete dia;
00272                             return;
00273                      }
00274                      MasterPageName = dia->Answer->text();
00275               }
00276               currentDoc->setCurrentPage(currentDoc->addMasterPage(nr, MasterPageName));
00277               if (currentDoc->currentPageLayout != singlePage)
00278               {
00279                      int lp = dia->Links->currentItem();
00280                      if (lp == 0)
00281                             lp = 1;
00282                      else if (lp == static_cast<int>(dia->Links->count()-1))
00283                             lp = 0;
00284                      else
00285                             lp++;
00286                      currentDoc->Pages->at(nr)->LeftPg = lp;
00287               }
00288               updateMasterPageList(MasterPageName);
00289               currentView->showMasterPage(currentDoc->MasterNames[MasterPageName]);
00290               currentView->reformPages();
00291        }
00292        delete dia;
00293 }
00294 
00295 void MasterPagesPalette::appendPage()
00296 {
00297        //bool atf;
00298        MergeDoc *dia = new MergeDoc(this, true);
00299        if (dia->exec())
00300        {
00301               qApp->setOverrideCursor(QCursor(waitCursor), true);
00302               int nr = currentDoc->Pages->count();
00303               //currentDoc->pageCount = 0;
00304               //atf = currentDoc->usesAutomaticTextFrames();
00305               //currentDoc->setUsesAutomaticTextFrames(false);
00306               //emit createNew(nr);
00307               QString MasterPageName(dia->getMasterPageNameText());
00308               QString MasterPageName2(MasterPageName);
00309               int copyC = 1;
00310               while (currentDoc->MasterNames.contains(MasterPageName2))
00311               {
00312                      MasterPageName2 = tr("Copy #%1 of ").arg(copyC)+MasterPageName;
00313                      copyC++;
00314               }
00315               currentDoc->setCurrentPage(currentDoc->addMasterPage(nr, MasterPageName2));
00316               qApp->processEvents();
00317               //CB TODO: If we are loading to a new name, we rely on doc->onpage in 
00318               //FileLoader::PasteItem as this call doesnt pass in the new destination page
00319               currentDoc->scMW()->loadPage(dia->getFromDoc(), dia->getMasterPageNameItem(), true, MasterPageName2);
00320               qApp->processEvents();
00321               /*
00322               MasterPageName = currentDoc->Pages->at(nr)->PageNam;
00323               MasterPageName2 = MasterPageName;
00324               int copyC = 1;
00325               while (currentDoc->MasterNames.contains(MasterPageName2))
00326               {
00327                      MasterPageName2 = tr("Copy #%1 of ").arg(copyC)+MasterPageName;
00328                      copyC++;
00329               }
00330               currentDoc->MasterNames.insert(MasterPageName2, nr);
00331               currentDoc->Pages->at(nr)->setPageName(MasterPageName2);
00332               currentDoc->Pages->at(nr)->MPageNam = "";
00333               */
00334               updateMasterPageList(MasterPageName2);
00335               //currentDoc->setUsesAutomaticTextFrames(atf);
00336               currentView->showMasterPage(currentDoc->MasterNames[MasterPageName2]);
00337               qApp->setOverrideCursor(QCursor(arrowCursor), true);
00338               //currentDoc->MasterPages = currentDoc->Pages;
00339        }
00340        delete dia;
00341 }
00342 
00343 void MasterPagesPalette::selectMasterPage(QListBoxItem *item)
00344 {
00345        sMuster = item->text();
00346        deleteButton->setEnabled(currentDoc->MasterNames.count() == 1 ? false : true);
00347        if (sMuster == CommonStrings::trMasterPageNormal || sMuster == CommonStrings::masterPageNormal)
00348        {
00349 //            sMuster = CommonStrings::masterPageNormal;
00350               deleteButton->setEnabled(false);
00351        }
00352        else
00353               deleteButton->setEnabled(true);
00354        currentView->showMasterPage(currentDoc->MasterNames[sMuster]);
00355 }
00356 
00357 void MasterPagesPalette::selectMasterPage(QString name)
00358 {
00359        sMuster = name;
00360        deleteButton->setEnabled(currentDoc->MasterNames.count() == 1 ? false : true);
00361        if (sMuster == CommonStrings::trMasterPageNormal)
00362        {
00363               sMuster = CommonStrings::masterPageNormal;
00364               deleteButton->setEnabled(false);
00365        }
00366        else
00367               deleteButton->setEnabled(true);
00368        currentView->showMasterPage(currentDoc->MasterNames[sMuster]);
00369 }
00370 
00371 void MasterPagesPalette::updateMasterPageList(QString MasterPageName)
00372 {
00373        masterPageListBox->clear();
00374        for (QMap<QString,int>::Iterator it = currentDoc->MasterNames.begin(); it != currentDoc->MasterNames.end(); ++it)
00375               masterPageListBox->insertItem(it.key() == CommonStrings::masterPageNormal ? CommonStrings::trMasterPageNormal : it.key());
00376        deleteButton->setEnabled(currentDoc->MasterNames.count() == 1 ? false : true);
00377        if (MasterPageName == CommonStrings::masterPageNormal)
00378        {
00379               MasterPageName = CommonStrings::trMasterPageNormal;
00380               deleteButton->setEnabled(false);
00381        }
00382        masterPageListBox->setSelected(masterPageListBox->index(masterPageListBox->findItem(MasterPageName)), true);
00383 }
00384 
00385 void MasterPagesPalette::renameMasterPage(QListBoxItem * item)
00386 {
00387        QString oldName(item->text());
00388        if ((oldName == CommonStrings::masterPageNormal) || (oldName == CommonStrings::trMasterPageNormal) || (oldName == CommonStrings::trMasterPageNormalLeft) || (oldName == CommonStrings::trMasterPageNormalMiddle) || (oldName == CommonStrings::trMasterPageNormalRight))
00389        {
00390               QMessageBox::information( this, tr("Unable to Rename Master Page"), tr("The Normal page is not allowed to be renamed."), QMessageBox::Ok );
00391               return;
00392        }
00393        bool ok;
00394        QString newName = QInputDialog::getText(
00395                      tr("Rename Master Page"), tr("New Name:"), QLineEdit::Normal,
00396                      oldName, &ok, this );
00397        if (ok && !newName.isEmpty())
00398               if (currentDoc->renameMasterPage( oldName, newName))
00399                      updateMasterPageList(newName);
00400 }