Back to index

scribus-ng  1.3.4.dfsg+svn20071115
tocgenerator.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     begin                : Jun 2005
00009     copyright            : (C) 2005 by Craig Bradney
00010     email                : cbradney@zip.com.au
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 #include "tocgenerator.h"
00022 #include "tocgenerator.moc"
00023 
00024 #include <qmap.h>
00025 #include <qstring.h>
00026 
00027 #include "scribusdoc.h"
00028 #include "pagestructs.h"
00029 #include "pageitem.h"
00030 #include "gtparagraphstyle.h"
00031 #include "gtframestyle.h"
00032 #include "gtwriter.h"
00033 #include "page.h"
00034 
00035 TOCGenerator::TOCGenerator(QObject *parent, const char *name, ScribusDoc *doc) : QObject(parent, name)
00036 {
00037        currDoc=doc;
00038 }
00039 
00040 
00041 TOCGenerator::~TOCGenerator()
00042 {
00043 }
00044 
00045 void TOCGenerator::setDoc(ScribusDoc *doc)
00046 {
00047        currDoc=doc;
00048 }
00049 
00050 PageItem* TOCGenerator::findTargetFrame(const QString &targetFrameName)
00051 {
00052        PageItem* targetFrame=NULL;
00053        if (currDoc!=NULL)
00054        {
00055               for (uint d = 0; d < currDoc->DocItems.count(); ++d)
00056               {
00057                      if (currDoc->DocItems.at(d) !=NULL )
00058                      {
00059                             if (currDoc->DocItems.at(d)->itemType()==PageItem::TextFrame && currDoc->DocItems.at(d)->itemName()==targetFrameName)
00060                             {
00061                                    targetFrame=currDoc->DocItems.at(d);
00062                                    break;
00063                             }
00064                      }
00065               }
00066        }
00067        return targetFrame;
00068 }
00069 
00070 void TOCGenerator::generateDefault()
00071 {
00072        if (currDoc==NULL)
00073               return;
00074        Q_ASSERT(!currDoc->masterPageMode());
00075        for(ToCSetupVector::Iterator tocSetupIt = currDoc->docToCSetups.begin() ; tocSetupIt != currDoc->docToCSetups.end(); ++tocSetupIt )
00076        {
00077               PageItem* tocFrame=findTargetFrame((*tocSetupIt).frameName);
00078               if (tocFrame!=NULL)
00079               {
00080                      PageItem *currentDocItem;
00081                      QMap<QString, QString> tocMap;
00082                      tocMap.clear();
00083                      uint *pageCounter =  new uint[currDoc->DocPages.count()];
00084                      uint pageNumberWidth=QString("%1").arg(currDoc->DocPages.count()).length();
00085                      for (uint i=0;i<currDoc->DocPages.count();++i)
00086                             pageCounter[i]=0;
00087                      unsigned int maxDataWidth=0;
00088                      for (uint d = 0; d < currDoc->DocItems.count(); ++d)
00089                      {
00090                             currentDocItem = currDoc->DocItems.at(d);
00091                             if (currentDocItem!=NULL)
00092                             {
00093                                    //Item not on a page, continue
00094                                    if (currentDocItem->OwnPage==-1)
00095                                           continue;
00096                                    //If we dont want to list non printing frames and this one is set to not print, continue
00097                                    if (!(*tocSetupIt).listNonPrintingFrames && !currentDocItem->printEnabled())
00098                                           continue;
00099                                    ObjectAttribute objattr=currentDocItem->getObjectAttribute((*tocSetupIt).itemAttrName);
00100                                    if (!objattr.name.isNull())
00101                                    {
00102                                           //The key is generated to produce a sequence of numbers for the page numbers
00103                                           //First is the page of the item
00104                                           //Second is an incremented counter for the item so multiple per page works
00105                                           //Third is the section based page number which is actually used in the TOC.
00106                                           QString key=QString("%1,%2,%3").arg(currentDocItem->OwnPage + currDoc->FirstPnum, pageNumberWidth).arg(pageCounter[currentDocItem->OwnPage]++).arg(currDoc->getSectionPageNumberForPageIndex(currentDocItem->OwnPage));
00107                                           tocMap.insert(key, objattr.value);
00108                                           if (objattr.value.length()>maxDataWidth)
00109                                                  maxDataWidth=objattr.value.length();
00110                                    }
00111                             }
00112                      }
00113                      //Set up the gtWriter instance with the selected paragraph style
00114                      gtWriter* writer = new gtWriter(false, tocFrame);
00115                      writer->setUpdateParagraphStyles(false);
00116                      writer->setOverridePStyleFont(false);
00117                      gtFrameStyle* fstyle = writer->getDefaultStyle();
00118                      gtParagraphStyle* pstyle = new gtParagraphStyle(*fstyle);
00119                      pstyle->setName((*tocSetupIt).textStyle);
00120                      writer->setParagraphStyle(pstyle);
00121                      
00122                      QString oldTocPage=QString::null;
00123                      for (QMap<QString, QString>::Iterator tocIt=tocMap.begin();tocIt!=tocMap.end();++tocIt)
00124                      {
00125                             QString tocPage(tocIt.key().section( ',', 2, 2 ).stripWhiteSpace());
00126                             QString tocLine;
00127                             //Start with text or numbers
00128                             if ((*tocSetupIt).pageLocation==End || (*tocSetupIt).pageLocation==NotShown)
00129                                    tocLine = tocIt.data();
00130                             if ((*tocSetupIt).pageLocation==Beginning && oldTocPage!=tocPage)
00131                                    tocLine = tocPage;
00132                             //Add in the tab for the leaders
00133                             tocLine+="\t";
00134                             //End with text or numbers
00135                             if ((*tocSetupIt).pageLocation==Beginning)
00136                                    tocLine += tocIt.data();
00137                             if ((*tocSetupIt).pageLocation==End && oldTocPage!=tocPage)
00138                                    tocLine += tocPage;
00139                             tocLine += "\n";
00140                             writer->append(tocLine);
00141                      }
00142                      if (writer!=NULL)
00143                             delete writer;
00144                      delete[] pageCounter;
00145               }
00146        }
00147 }