~ubuntu-branches/ubuntu/trusty/scribus-ng/trusty

« back to all changes in this revision

Viewing changes to scribus/docsections.cpp

  • Committer: Package Import Robot
  • Author(s): Oleksandr Moskalenko
  • Date: 2012-02-15 15:57:12 UTC
  • mfrom: (4.2.10 sid)
  • Revision ID: package-import@ubuntu.com-20120215155712-biimoc8o875jht80
Tags: 1.4.0.dfsg+r17300-1
* Prepare a dummy transitional package to converge on the 1.4.0 release.
* debian/NEWS: update the news.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
 
to the COPYING file provided with the program. Following this notice may exist
4
 
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
 
for which a new license (GPL+exception) is in place.
6
 
*/
7
 
/***************************************************************************
8
 
*   Copyright (C) 2005 by Craig Bradney                                   *
9
 
*   cbradney@zip.com.au                                                   *
10
 
*                                                                         *
11
 
*   This program is free software; you can redistribute it and/or modify  *
12
 
*   it under the terms of the GNU General Public License as published by  *
13
 
*   the Free Software Foundation; either version 2 of the License, or     *
14
 
*   (at your option) any later version.                                   *
15
 
*                                                                         *
16
 
*   This program is distributed in the hope that it will be useful,       *
17
 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
 
*   GNU General Public License for more details.                          *
20
 
*                                                                         *
21
 
*   You should have received a copy of the GNU General Public License     *
22
 
*   along with this program; if not, write to the                         *
23
 
*   Free Software Foundation, Inc.,                                       *
24
 
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25
 
***************************************************************************/
26
 
 
27
 
#include "docsections.h"
28
 
 
29
 
// This class implements only the non-GUI parts of the
30
 
// Document Sections dialog. Please use Qt Designer on
31
 
// ui/docsections.ui if you need to modify the layout,
32
 
// widget properties, etc.
33
 
 
34
 
#include <QCheckBox>
35
 
#include <QComboBox>
36
 
#include <QEvent>
37
 
#include <QMessageBox>
38
 
#include <QPushButton>
39
 
#include <QTableWidget>
40
 
#include <QToolTip>
41
 
 
42
 
#include "commonstrings.h"
43
 
#include "pagestructs.h"
44
 
 
45
 
 
46
 
DocSections::DocSections( QWidget* parent )
47
 
        : QWidget( parent )
48
 
{
49
 
        setupUi(this);
50
 
        languageChange();
51
 
        connect (sectionsTable, SIGNAL(cellChanged(int,int)), this, SLOT(tableItemChanged(int,int)));
52
 
        connect( addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
53
 
        connect( deleteButton, SIGNAL(clicked()), this, SLOT(deleteEntry()));
54
 
}
55
 
 
56
 
DocSections::~DocSections()
57
 
{
58
 
}
59
 
 
60
 
void DocSections::changeEvent(QEvent *e)
61
 
{
62
 
        if (e->type() == QEvent::LanguageChange)
63
 
        {
64
 
                languageChange();
65
 
        }
66
 
        else
67
 
                QWidget::changeEvent(e);
68
 
}
69
 
 
70
 
void DocSections::languageChange()
71
 
{
72
 
        addButton->setToolTip( "<qt>"+ tr("Add a page numbering section to the document. The new section will be added after the currently selected section.") + "</qt>");
73
 
        deleteButton->setToolTip( "<qt>"+ tr("Delete the currently selected section.") + "</qt>");
74
 
        sectionsTable->setToolTip( "<qt>"+ tr("<b>Name:</b> Optional name for section eg. Index<br/>"
75
 
                                                                                        "<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/>"
76
 
                                                                                        "<b>From:</b> The page index for this section to start at.<br/>"
77
 
                                                                                        "<b>To:</b> The page index for this section to stop at.<br/>"
78
 
                                                                                        "<b>Style:</b> Select the page number style to be used.<br/>"
79
 
                                                                                        "<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>");
80
 
}
81
 
 
82
 
void DocSections::setup(const DocumentSectionMap docSections, int maxPageIndex)
83
 
{
84
 
        localSections=docSections;
85
 
        m_maxpageindex=maxPageIndex;
86
 
        
87
 
        styles << tr("1, 2, 3, ...") << tr("i, ii, iii, ...") << tr("I, II, III, ...") << tr("a, b, c, ...") << tr("A, B, C, ...") << CommonStrings::tr_None;
88
 
        
89
 
        updateTable();
90
 
}
91
 
 
92
 
void DocSections::updateTable()
93
 
{
94
 
        sectionsTable->setRowCount(localSections.count());
95
 
        int row=0;
96
 
        for(DocumentSectionMap::Iterator it = localSections.begin(); it!= localSections.end(); ++it)
97
 
        {
98
 
                uint i=0;
99
 
                //Name
100
 
                QTableWidgetItem *item1 = new QTableWidgetItem((*it).name);
101
 
                sectionsTable->setItem(row, i++, item1);
102
 
                //Active
103
 
                QCheckBox *item2 = new QCheckBox();
104
 
                item2->setChecked((*it).active);
105
 
                sectionsTable->setCellWidget(row, i++, item2);
106
 
                //FromIndex
107
 
                QTableWidgetItem *item3 = new QTableWidgetItem(QString::number((*it).fromindex+1));
108
 
                sectionsTable->setItem(row, i++, item3);
109
 
                //ToIndex
110
 
                QTableWidgetItem *item4 = new QTableWidgetItem(QString::number((*it).toindex+1));
111
 
                sectionsTable->setItem(row, i++, item4);
112
 
                //Style
113
 
                QComboBox *item5 = new QComboBox();
114
 
                item5->addItems(styles);
115
 
                sectionsTable->setCellWidget(row, i++, item5);
116
 
                if ((*it).type==Type_None)
117
 
                        item5->setCurrentIndex(styles.count()-1);
118
 
                else
119
 
                        item5->setCurrentIndex((*it).type);
120
 
                //Start Page Number
121
 
                QTableWidgetItem *item6 = new QTableWidgetItem(QString::number((*it).sectionstartindex));
122
 
                sectionsTable->setItem(row, i++, item6);
123
 
                //End Page Number
124
 
                /*
125
 
                QTableItem *item7 = new QTableItem(sectionsTable, QTableItem::WhenCurrent, QString::number((*it).sectionstartindex + (*it).toindex - (*it).fromindex));
126
 
                item7->setEnabled(false);
127
 
                sectionsTable->setItem(row, i++, item7);
128
 
                */
129
 
                QTableWidgetItem *t=sectionsTable->verticalHeaderItem(row);
130
 
                if (t!=NULL)
131
 
                        t->setText(QString("%1").arg(row));
132
 
                row++;
133
 
        }
134
 
        deleteButton->setEnabled(localSections.count()>1);
135
 
}
136
 
 
137
 
void DocSections::tableItemChanged( int row, int col )
138
 
{
139
 
        bool outOfRange=false;
140
 
        uint newDocPageSpec;
141
 
 
142
 
        switch (col)
143
 
        {
144
 
        case 0:
145
 
                localSections[row].name=sectionsTable->item(row, col)->text();
146
 
                break;
147
 
        case 1:
148
 
                {
149
 
                        QCheckBox* qcti=dynamic_cast<QCheckBox*>(sectionsTable->cellWidget(row,col));
150
 
                        if (qcti!=NULL)
151
 
                                localSections[row].active=qcti->isChecked();
152
 
                }
153
 
                break;
154
 
        case 2:
155
 
        case 3:
156
 
                // Validate to/from page specification before conversion to an index
157
 
                //!!!   There is still a problem here if m_maxpageindex == MAX_UINT ;)
158
 
                newDocPageSpec=sectionsTable->item(row, col)->text().toUInt();
159
 
                if (newDocPageSpec==0)
160
 
                {
161
 
                        newDocPageSpec=1;
162
 
                        outOfRange=true;
163
 
                }
164
 
                else
165
 
                if (newDocPageSpec>m_maxpageindex+1)
166
 
                {
167
 
                        newDocPageSpec=m_maxpageindex+1;
168
 
                        outOfRange=true;
169
 
                }
170
 
                // Now, since newDocPageSpec >= 1, convert to index
171
 
                --newDocPageSpec;
172
 
                if (col==2)
173
 
                        localSections[row].fromindex=newDocPageSpec;
174
 
                else
175
 
                        localSections[row].toindex=newDocPageSpec;
176
 
                break;
177
 
        case 4:
178
 
                {
179
 
                        QComboBox* qcti=dynamic_cast<QComboBox*>(sectionsTable->cellWidget(row,col));
180
 
                        if (qcti!=NULL)
181
 
                        {
182
 
                                int index=qcti->currentIndex();
183
 
                                if (index<styles.count()-1)
184
 
                                        localSections[row].type=(DocumentSectionType)index;
185
 
                                else 
186
 
                                        if (index==styles.count()-1)
187
 
                                        localSections[row].type=Type_None;
188
 
                        }
189
 
                }
190
 
                break;
191
 
        case 5:
192
 
                localSections[row].sectionstartindex=sectionsTable->item(row, col)->text().toUInt();
193
 
                break;
194
 
        default:
195
 
                break;
196
 
        }
197
 
        
198
 
        if (outOfRange)
199
 
        {
200
 
                updateTable();
201
 
                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);
202
 
        }
203
 
}
204
 
 
205
 
void DocSections::addEntry()
206
 
{
207
 
        int currRow=sectionsTable->currentRow();
208
 
        bool found=false;
209
 
        DocumentSectionMap::Iterator it = localSections.begin();
210
 
        int count=0;
211
 
        for(; it!= localSections.end(); ++it)
212
 
        {
213
 
                if(count==currRow)
214
 
                {
215
 
                        found=true;
216
 
                        break;
217
 
                }
218
 
                ++count;
219
 
        }
220
 
        if (!found) //End of map, just append
221
 
        {
222
 
                struct DocumentSection blank;
223
 
                uint count=localSections.count();
224
 
                blank.number=count;
225
 
                blank.name=QString::number(count);
226
 
                blank.fromindex=m_maxpageindex+1;
227
 
                blank.toindex=m_maxpageindex+1;
228
 
                blank.type=Type_1_2_3;
229
 
                blank.sectionstartindex=1;
230
 
                blank.reversed=false;
231
 
                blank.active=true;
232
 
                localSections.insert(count, blank);
233
 
        }
234
 
        else
235
 
        {
236
 
                //Now, copy to a temp map
237
 
                DocumentSectionMap tempSections(localSections);
238
 
                localSections.clear();
239
 
                //Copy the temp map entries over. When we find the number of the current row, also insert a new entry.
240
 
                uint i=0;
241
 
                for(DocumentSectionMap::Iterator it2 = tempSections.begin(); it2!= tempSections.end(); ++it2)
242
 
                {
243
 
                        it2.value().number=i;
244
 
                        localSections.insert(i, it2.value());
245
 
                        
246
 
                        if ((*it).number==i)
247
 
                        {
248
 
                                struct DocumentSection blank;
249
 
                                blank.number=++i;
250
 
                                blank.name=QString::number(i);
251
 
                                blank.fromindex=(*it).toindex+1+1;
252
 
                                blank.toindex=(*it).toindex+2+1;
253
 
                                blank.type=Type_1_2_3;
254
 
                                blank.sectionstartindex=1;
255
 
                                blank.reversed=false;
256
 
                                blank.active=true;
257
 
                                localSections.insert(i, blank);
258
 
                        }
259
 
                        ++i;
260
 
                }
261
 
        }
262
 
        updateTable();
263
 
}
264
 
 
265
 
void DocSections::deleteEntry()
266
 
{
267
 
        int currRow=sectionsTable->currentRow();
268
 
        if (currRow==0 && localSections.count()==1)
269
 
                return;
270
 
        bool found=false;
271
 
        DocumentSectionMap::Iterator it = localSections.begin();
272
 
        int count=0;
273
 
        for(; it!= localSections.end(); ++it)
274
 
        {
275
 
                if(count==currRow)
276
 
                {
277
 
                        found=true;
278
 
                        break;
279
 
                }
280
 
                ++count;
281
 
        }
282
 
        if (found)
283
 
        {
284
 
                //If we arent at the start, copy the toindex of the current item
285
 
                //to the toindex of the previous item
286
 
                if (it!=localSections.begin())
287
 
                {
288
 
                        DocumentSectionMap::Iterator it2(it);
289
 
                        (*--it2).toindex=(*it).toindex;
290
 
                }
291
 
                //Delete the currently selected entry
292
 
                localSections.erase(it);
293
 
                //Now, copy to a temp map and reinsert with consecutive keys again
294
 
                DocumentSectionMap tempSections(localSections);
295
 
                localSections.clear();
296
 
                uint i=0;
297
 
                it = tempSections.begin();
298
 
                for(; it!= tempSections.end(); ++it)
299
 
                {
300
 
                        it.value().number=i;
301
 
                        localSections.insert(i++, it.value());
302
 
                }
303
 
                int newCount=localSections.count();
304
 
                //int preIndex=qMax(currentIndex-1, 0);
305
 
                localSections[0].fromindex=0;
306
 
                localSections[newCount-1].toindex=m_maxpageindex;
307
 
                updateTable();
308
 
        }
309
 
}
310
 
 
311
 
const DocumentSectionMap& DocSections::getNewSections()
312
 
{
313
 
        return localSections;
314
 
}