Back to index

scribus-ng  1.3.4.dfsg+svn20071115
docsections.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) 2005 by Craig Bradney                                   *
00009 *   cbradney@zip.com.au                                                   *
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 "docsections.h"
00028 #include "docsections.moc"
00029 
00030 // This class implements only the non-GUI parts of the
00031 // Document Sections dialog. Please use Qt Designer on
00032 // ui/docsectionsbase.ui if you need to modify the layout,
00033 // widget properties, etc.
00034 
00035 #include <qmessagebox.h>
00036 #include <qpushbutton.h>
00037 #include <qtable.h>
00038 #include <qtooltip.h>
00039 
00040 #include "commonstrings.h"
00041 #include "pagestructs.h"
00042 
00043 
00044 DocSections::DocSections( QWidget* parent )
00045        : DocSectionsBase( parent, "DocSections" )
00046 {
00047        languageChange();
00048        connect( sectionsTable, SIGNAL(valueChanged(int, int)), this, SLOT(tableItemChanged(int, int)));
00049        connect( addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
00050        connect( deleteButton, SIGNAL(clicked()), this, SLOT(deleteEntry()));
00051 }
00052 
00053 DocSections::~DocSections()
00054 {
00055 }
00056 
00057 void DocSections::languageChange()
00058 {
00059        QToolTip::add(addButton, "<qt>"+ tr("Add a page numbering section to the document. The new section will be added after the currently selected section.") + "</qt>");
00060        QToolTip::add(deleteButton, "<qt>"+ tr("Delete the currently selected section.") + "</qt>");
00061        QToolTip::add(sectionsTable, "<qt>"+ tr("<b>Name:</b> Optional name for section eg. Index<br/>"
00062                                                                              "<b>Shown:</b> Select to show the page numbers in this section if there is one or more text frames setup to do so.<br/>"
00063                                                                              "<b>From:</b> The page index for this section to start at.<br/>"
00064                                                                              "<b>To:</b> The page index for this section to stop at.<br/>"
00065                                                                              "<b>Style:</b> Select the page number style to be used.<br/>"
00066                                                                              "<b>Start:</b> The index within the Style's range to star at. Eg. If Start=2 and Style=a,b,c, ..., the numbers will begin at b. For the first section in the document this replaces the older First Page Number in the new file window.") +"</qt>");
00067 }
00068 
00069 void DocSections::setup(const DocumentSectionMap docSections, int maxPageIndex)
00070 {
00071        localSections=docSections;
00072        m_maxpageindex=maxPageIndex;
00073        
00074        styles << tr("1, 2, 3, ...") << tr("i, ii, iii, ...") << tr("I, II, III, ...") << tr("a, b, c, ...") << tr("A, B, C, ...") << CommonStrings::tr_None;
00075        
00076        updateTable();
00077 }
00078 
00079 void DocSections::updateTable()
00080 {
00081        sectionsTable->setNumRows(localSections.count());
00082        int row=0;
00083        for(DocumentSectionMap::Iterator it = localSections.begin(); it!= localSections.end(); ++it)
00084        {
00085               uint i=0;
00086               //Name
00087               QTableItem *item1 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, (*it).name);
00088               sectionsTable->setItem(row, i++, item1);
00089               //Active
00090               QCheckTableItem *item2 = new QCheckTableItem(sectionsTable,"");
00091               item2->setChecked((*it).active);
00092               sectionsTable->setItem(row, i++, item2);
00093               //FromIndex
00094               QTableItem *item3 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, QString::number((*it).fromindex+1));
00095               sectionsTable->setItem(row, i++, item3);
00096               //ToIndex
00097               QTableItem *item4 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, QString::number((*it).toindex+1));
00098               sectionsTable->setItem(row, i++, item4);
00099               //Style
00100               QComboTableItem *item5 = new QComboTableItem(sectionsTable, styles);
00101               sectionsTable->setItem(row, i++, item5);
00102               if ((*it).type==Type_None)
00103                      item5->setCurrentItem(styles.count()-1);
00104               else
00105                      item5->setCurrentItem((*it).type);
00106               //Start Page Number
00107               QTableItem *item6 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, QString::number((*it).sectionstartindex));
00108               sectionsTable->setItem(row, i++, item6);
00109               //End Page Number
00110               /*
00111               QTableItem *item7 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, QString::number((*it).sectionstartindex + (*it).toindex - (*it).fromindex));
00112               item7->setEnabled(false);
00113               sectionsTable->setItem(row, i++, item7);
00114               */
00115               sectionsTable->verticalHeader()->setLabel(row, QString("%1").arg(row));
00116               row++;
00117        }
00118        for (int i=0;i<6;++i)
00119               sectionsTable->adjustColumn(i);
00120        deleteButton->setEnabled(localSections.count()>1);
00121 }
00122 
00123 void DocSections::tableItemChanged( int row, int col )
00124 {
00125        bool outOfRange=false;
00126        uint newDocPageSpec;
00127 
00128        switch (col)
00129        {
00130        case 0:
00131               localSections[row].name=sectionsTable->text(row, col);
00132               break;
00133        case 1:
00134               localSections[row].active=static_cast<QCheckTableItem*>(sectionsTable->item(row, col))->isChecked();
00135               break;
00136        case 2:
00137        case 3:
00138               // Validate to/from page specification before conversion to an index
00140               newDocPageSpec=sectionsTable->text(row, col).toUInt();
00141               if (newDocPageSpec==0)
00142               {
00143                      newDocPageSpec=1;
00144                      outOfRange=true;
00145               }
00146               else
00147               if (newDocPageSpec>m_maxpageindex+1)
00148               {
00149                      newDocPageSpec=m_maxpageindex+1;
00150                      outOfRange=true;
00151               }
00152               // Now, since newDocPageSpec >= 1, convert to index
00153               --newDocPageSpec;
00154               if (col==2)
00155                      localSections[row].fromindex=newDocPageSpec;
00156               else
00157                      localSections[row].toindex=newDocPageSpec;
00158               break;
00159        case 4:
00160               {
00161                      QComboTableItem* qcti=dynamic_cast<QComboTableItem*>(sectionsTable->item(row,col));
00162                      if (qcti!=NULL)
00163                      {
00164                             uint index=qcti->currentItem();
00165                             if (index<styles.count()-1)
00166                                    localSections[row].type=(DocumentSectionType)index;
00167                             else 
00168                                    if (index==styles.count()-1)
00169                                    localSections[row].type=Type_None;
00170                      }
00171               }
00172               break;
00173        case 5:
00174               localSections[row].sectionstartindex=sectionsTable->text(row, col).toUInt();
00175               break;
00176        default:
00177               break;
00178        }
00179        
00180        if (outOfRange)
00181        {
00182               updateTable();
00183               QMessageBox::warning(parentWidget(), tr("Page Number Out Of Bounds"),"<qt>"+ tr("The value you have entered is outside the range of page numbers in the current document (%1-%2).").arg(1).arg(m_maxpageindex+1)+"</qt>",QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
00184        }
00185 }
00186 
00187 void DocSections::addEntry()
00188 {
00189        int currRow=sectionsTable->currentRow();
00190        bool found=false;
00191        DocumentSectionMap::Iterator it = localSections.begin();
00192        int count=0;
00193        for(; it!= localSections.end(); ++it)
00194        {
00195               if(count==currRow)
00196               {
00197                      found=true;
00198                      break;
00199               }
00200               ++count;
00201        }
00202        if (!found) //End of map, just append
00203        {
00204               struct DocumentSection blank;
00205               uint count=localSections.count();
00206               blank.number=count;
00207               blank.name=QString::number(count);
00208               blank.fromindex=m_maxpageindex+1;
00209               blank.toindex=m_maxpageindex+1;
00210               blank.type=Type_1_2_3;
00211               blank.sectionstartindex=1;
00212               blank.reversed=false;
00213               blank.active=true;
00214               localSections.insert(count, blank);
00215        }
00216        else
00217        {
00218               //Now, copy to a temp map
00219               DocumentSectionMap tempSections(localSections);
00220               localSections.clear();
00221               //Copy the temp map entries over. When we find the number of the current row, also insert a new entry.
00222               uint i=0;
00223               for(DocumentSectionMap::Iterator it2 = tempSections.begin(); it2!= tempSections.end(); ++it2)
00224               {
00225                      it2.data().number=i;
00226                      localSections.insert(i, it2.data());
00227                      
00228                      if ((*it).number==i)
00229                      {
00230                             struct DocumentSection blank;
00231                             blank.number=++i;
00232                             blank.name=QString::number(i);
00233                             blank.fromindex=(*it).toindex+1+1;
00234                             blank.toindex=(*it).toindex+2+1;
00235                             blank.type=Type_1_2_3;
00236                             blank.sectionstartindex=1;
00237                             blank.reversed=false;
00238                             blank.active=true;
00239                             localSections.insert(i, blank);
00240                      }
00241                      ++i;
00242               }
00243        }
00244        updateTable();
00245 }
00246 
00247 void DocSections::deleteEntry()
00248 {
00249        int currRow=sectionsTable->currentRow();
00250        if (currRow==0 && localSections.count()==1)
00251               return;
00252        bool found=false;
00253        DocumentSectionMap::Iterator it = localSections.begin();
00254        int count=0;
00255        for(; it!= localSections.end(); ++it)
00256        {
00257               if(count==currRow)
00258               {
00259                      found=true;
00260                      break;
00261               }
00262               ++count;
00263        }
00264        if (found)
00265        {
00266               //If we arent at the start, copy the toindex of the current item
00267               //to the toindex of the previous item
00268               if (it!=localSections.begin())
00269               {
00270                      DocumentSectionMap::Iterator it2(it);
00271                      (*--it2).toindex=(*it).toindex;
00272               }
00273               //Delete the currently selected entry
00274               localSections.remove(it);
00275               //Now, copy to a temp map and reinsert with consecutive keys again
00276               DocumentSectionMap tempSections(localSections);
00277               localSections.clear();
00278               uint i=0;
00279               it = tempSections.begin();
00280               for(; it!= tempSections.end(); ++it)
00281               {
00282                      it.data().number=i;
00283                      localSections.insert(i++, it.data());
00284               }
00285               int newCount=localSections.count();
00286               //int preIndex=QMAX(currentIndex-1, 0);
00287               localSections[0].fromindex=0;
00288               localSections[newCount-1].toindex=m_maxpageindex;
00289               updateTable();
00290        }
00291 }
00292 
00293 const DocumentSectionMap& DocSections::getNewSections()
00294 {
00295        return localSections;
00296 }