Subversion Repositories Scribus

Rev

Rev 19912 | Rev 20326 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17513 jghali 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
 
9
#include <QApplication>
10
#include <QBitmap>
11
#include <QCursor>
12
#include <QDrag>
13
#include <QEvent>
14
#include <QHeaderView>
15
#include <QLabel>
16
#include <QList>
17
#include <QMenu>
18
#include <QMimeData>
19
#include <QMessageBox>
20
#include <QPainter>
21
 
22
#include "commonstrings.h"
20185 craig 23
#include "iconmanager.h"
17513 jghali 24
#include "pagelayout.h"
25
#include "pagepalette_pages.h"
26
#include "pagepalette_widgets.h"
27
#include "sccombobox.h"
28
#include "scpage.h"
29
#include "scribuscore.h"
19093 craig 30
#include "scribusdoc.h"
17513 jghali 31
#include "scribusview.h"
32
 
33
PagePalette_Pages::PagePalette_Pages(QWidget* parent) : QWidget(parent)
34
{
35
        m_scMW = ScCore->primaryMainWindow();
36
 
37
        setupUi(this);
38
 
39
        setSizePolicy( QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
40
 
41
        masterPageList->setMinimumSize(QSize(130,70));
42
        masterPageList->Thumb = false;
43
        masterPageList->setIconSize(QSize(60, 60));
44
 
45
        QHeaderView *Header = pageView->verticalHeader();
18194 fschmid 46
        Header->setSectionsMovable(false);
47
        Header->setSectionResizeMode(QHeaderView::Fixed);
17513 jghali 48
        Header->hide();
49
        Header = pageView->horizontalHeader();
18194 fschmid 50
        Header->setSectionsMovable(false);
51
        Header->setSectionResizeMode(QHeaderView::Fixed);
17513 jghali 52
        Header->hide();
53
        pageView->setSortingEnabled(false);
54
        pageView->setSelectionMode( QAbstractItemView::NoSelection );
55
        pageView->setRowCount(1);
56
        pageView->setColumnCount(1);
57
        pageView->setIconSize(QSize(60, 60));
58
        pageView->setMinimumSize(QSize(130,120));
59
 
60
        trash->setMinimumSize(QSize(32,32));
61
        trash->setMaximumSize(QSize(32,32));
62
 
20185 craig 63
        pix = IconManager::instance()->loadPixmap("32/page-simple.png");
17513 jghali 64
 
65
        currView = 0;
66
        Rebuild();
67
        languageChange();
68
 
69
        connect(masterPageList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(selMasterPage()));
70
        connect(masterPageList, SIGNAL(thumbnailChanged()), this, SLOT(rebuildMasters()));
71
        connect(masterPageList, SIGNAL(DelMaster(QString)), this, SLOT(deleteMasterPage(QString)));
72
 
73
        connect(pageLayout, SIGNAL(selectedLayout(int ))   , this, SLOT(handlePageLayout(int )));
74
        connect(pageLayout, SIGNAL(selectedFirstPage(int )), this, SLOT(handleFirstPage(int )));
75
        connect(pageView  , SIGNAL(Click(int, int, int))   , this, SLOT(pageView_gotoPage(int, int, int)));
76
        connect(pageView  , SIGNAL(movePage(int, int))     , this, SLOT(pageView_movePage(int, int)));
77
        connect(pageView  , SIGNAL(DelPage(int))           , m_scMW, SLOT(deletePage2(int)));
18558 jghali 78
        connect(pageView  , SIGNAL(UseTemp(QString, int))  , this, SLOT(pageView_applyMasterPage(QString, int)));
17513 jghali 79
        connect(pageView  , SIGNAL(NewPage(int, QString))  , m_scMW, SLOT(slotNewPageP(int, QString)));
80
        connect(trash     , SIGNAL(DelPage(int))           , m_scMW, SLOT(deletePage2(int)));
81
        connect(trash     , SIGNAL(DelMaster(QString))     , this, SLOT(deleteMasterPage(QString)));
82
 
83
        connect(this, SIGNAL(gotoPage(int))          , m_scMW, SLOT(selectPagesFromOutlines(int)));
84
}
85
 
86
void PagePalette_Pages::deleteMasterPage(QString tmp)
87
{
88
        if (tmp == CommonStrings::trMasterPageNormal)
89
                return;
90
        QString extraWarn = "";
91
        for (int i=0; i < currView->Doc->DocPages.count(); ++i )
92
        {
93
                if (currView->Doc->DocPages[i]->MPageNam == tmp)
94
                        extraWarn = tr("This master page is used at least once in the document.");
95
        }
19605 jghali 96
        int exit = ScMessageBox::warning(this,
17513 jghali 97
                                      CommonStrings::trWarning,
98
                                      tr("Do you really want to delete this master page?")+"\n"+extraWarn,
19605 jghali 99
                                      QMessageBox::Yes | QMessageBox::No,
100
                                      QMessageBox::NoButton,    // GUI default
101
                                      QMessageBox::Yes);        // batch default
17513 jghali 102
        if (exit == QMessageBox::Yes)
103
        {
104
                bool oldMPMode = currView->Doc->masterPageMode();
105
                int  storedPageNum = currView->Doc->currentPageNumber();
106
                int  storedViewXCoor = currView->horizontalScrollBar()->value();
107
                int  storedViewYCoor = currView->verticalScrollBar()->value();
108
 
109
                currView->Doc->setMasterPageMode(true);
110
                currView->Doc->scMW()->deletePage2(currView->Doc->MasterNames[tmp]);
111
                //<<CB TODO Move back into ScribusDoc::deleteMasterPage();
112
                //This must happen after the pages have been reformed (view/doc)
113
                currView->Doc->rebuildMasterNames();
114
                // Fix up any pages that refer to the deleted master page
115
                currView->Doc->replaceMasterPage(tmp);
116
                currView->Doc->setMasterPageMode(oldMPMode);
117
                currView->Doc->setModified(true);
118
 
119
                currView->Doc->setCurrentPage(currView->Doc->DocPages.at(storedPageNum));
120
                currView->reformPages(false);
121
                currView->setContentsPos(storedViewXCoor, storedViewYCoor);
122
                currView->DrawNew();
123
        }
124
}
125
 
18558 jghali 126
void PagePalette_Pages::pageView_applyMasterPage(QString masterpageName, int pageIndex)
127
{
19586 craig 128
        m_scMW->Apply_MasterPage(masterpageName, pageIndex, false);
129
        currView->reformPages();
130
        currView->DrawNew();
18558 jghali 131
        SeItem* pageItem = pageView->GetPageItem(pageIndex);
132
        if (pageItem)
133
                pageItem->setIcon(createIcon(pageIndex, masterpageName, pix));
134
}
135
 
17513 jghali 136
void PagePalette_Pages::pageView_movePage(int r, int c)
137
{
19910 craig 138
        if (r == c || r == pageView->MaxC)
17513 jghali 139
                return;
140
        if (c > pageView->MaxC)
141
                currView->Doc->movePage(r, r + 1, c, 2);
142
        else
143
                currView->Doc->movePage(r, r + 1, c, 0);
144
        currView->reformPages();
145
        rebuildPages();
146
        currView->DrawNew();
147
}
148
 
149
void PagePalette_Pages::pageView_gotoPage(int r, int c, int b)
150
{
151
        int p;
152
        bool dummy;
153
        if ((b == Qt::LeftButton) && (r != -1) && (c != -1))
154
        {
155
                p = pageView->GetPage(r, c, &dummy);
156
                emit gotoPage(p);
157
        }
158
}
159
 
160
void PagePalette_Pages::enablePalette(const bool enabled)
161
{
162
        pageView->setEnabled(enabled);
163
        masterPageList->setEnabled(enabled);
164
        pageLayout->setEnabled(enabled);
165
}
166
 
167
void PagePalette_Pages::handlePageLayout(int layout)
168
{
169
        pageLayout->selectFirstP(currView->Doc->pageSets()[layout].FirstPage);
170
        currView->Doc->resetPage(layout);
171
        currView->reformPages();
172
        currView->DrawNew();
173
        currView->GotoPage(currView->Doc->currentPageNumber());
174
        rebuildPages();
175
}
176
 
177
void PagePalette_Pages::handleFirstPage(int fp)
178
{
179
        currView->Doc->setPageSetFirstPage(currView->Doc->pagePositioning(), fp);
180
        currView->reformPages();
181
        currView->DrawNew();
182
        currView->GotoPage(currView->Doc->currentPageNumber());
183
        rebuildPages();
184
}
185
 
186
void PagePalette_Pages::rebuildMasters()
187
{
188
        if (m_scMW->scriptIsRunning())
189
                return;
190
        masterPageList->clear();
191
        if (currView == 0)
192
                return;
193
        QPixmap pm;
19618 jghali 194
        QListWidgetItem* item;
17513 jghali 195
        QMap<QString,int>::Iterator it;
196
        for (it = currView->Doc->MasterNames.begin(); it != currView->Doc->MasterNames.end(); ++it)
197
        {
19618 jghali 198
                QString pageLabel = it.key() == CommonStrings::masterPageNormal ? CommonStrings::trMasterPageNormal : it.key();
199
                QString pageName = it.key();
17513 jghali 200
                if (masterPageList->Thumb)
201
                {
202
                        pm = QPixmap::fromImage(currView->MPageToPixmap(it.key(),60));
19618 jghali 203
                        item = new QListWidgetItem(QIcon(pm), pageLabel, masterPageList);
17513 jghali 204
                }
205
                else
19618 jghali 206
                        item = new QListWidgetItem(pageLabel, masterPageList);
207
                item->setData(Qt::UserRole, pageName);
17513 jghali 208
        }
209
}
210
 
211
void PagePalette_Pages::rebuildPages()
212
{
213
        if (m_scMW->scriptIsRunning())
214
                return;
215
        QString str;
216
        disconnect(pageLayout, SIGNAL(selectedLayout(int )), this, SLOT(handlePageLayout(int )));
217
        disconnect(pageLayout, SIGNAL(selectedFirstPage(int )), this, SLOT(handleFirstPage(int )));
218
        pageView->clearContents();
219
        pageView->setRowCount(1);
220
        pageView->setColumnCount(1);
221
        if (currView == 0)
222
        {
223
                connect(pageLayout, SIGNAL(selectedLayout(int )), this, SLOT(handlePageLayout(int )));
224
                connect(pageLayout, SIGNAL(selectedFirstPage(int )), this, SLOT(handleFirstPage(int )));
225
                return;
226
        }
227
        pageLayout->updateLayoutSelector(currView->Doc->pageSets());
228
        pageLayout->selectItem(currView->Doc->pagePositioning());
229
        pageLayout->firstPage->setCurrentIndex(currView->Doc->pageSets()[currView->Doc->pagePositioning()].FirstPage);
230
        pageView->MaxC = currView->Doc->DocPages.count()-1;
231
        int counter = currView->Doc->pageSets()[currView->Doc->pagePositioning()].FirstPage;
232
        int cols = currView->Doc->pageSets()[currView->Doc->pagePositioning()].Columns;
233
        int rows = (currView->Doc->DocPages.count()+counter) / currView->Doc->pageSets()[currView->Doc->pagePositioning()].Columns;
234
        if (((currView->Doc->DocPages.count()+counter) % currView->Doc->pageSets()[currView->Doc->pagePositioning()].Columns) != 0)
235
                rows++;
236
        int rowcounter = 0;
237
        int colmult, rowmult, coladd, rowadd;
238
        if (cols == 1)
239
        {
240
                pageView->setColumnCount(cols);
241
                pageView->setRowCount(rows*2+1);
242
                colmult = 1;
243
                coladd = 0;
244
                rowmult = 2;
245
                rowadd = 1;
246
        }
247
        else
248
        {
249
                pageView->setColumnCount(cols*2);
250
                pageView->setRowCount(rows+1);
251
                colmult = 2;
252
                coladd = 1;
253
                rowmult = 1;
254
                rowadd = 0;
255
        }
256
        for (int rr = 0; rr < pageView->rowCount(); rr++)
257
        {
258
                for (int cc = 0; cc < pageView->columnCount(); cc++)
259
                {
260
                        QTableWidgetItem *tW = new QTableWidgetItem(1001);
261
                        tW->setFlags(Qt::ItemIsEnabled);
262
                        pageView->setItem(rr, cc, tW);
263
                }
264
        }
265
        pageView->coladd = coladd;
266
        pageView->colmult = colmult;
267
        pageView->rowadd = rowadd;
268
        pageView->rowmult = rowmult;
269
        pageView->firstP = counter;
270
        pageView->cols = currView->Doc->pageSets()[currView->Doc->pagePositioning()].Columns;
271
        pageList.clear();
272
        for (int a = 0; a < currView->Doc->DocPages.count(); ++a)
273
        {
274
                str = currView->Doc->DocPages.at(a)->MPageNam;
18558 jghali 275
                SeItem *it = new SeItem(str, a, createIcon(a, str, pix));
17513 jghali 276
                pageList.append(it);
277
                pageView->setItem(rowcounter*rowmult+rowadd, counter*colmult+coladd, (QTableWidgetItem *)it);
278
                pageView->setColumnWidth(counter*colmult+coladd, pix.width());
279
                if (cols == 1)
280
                {
281
                        pageView->setRowHeight(rowcounter*rowmult, 10);
282
                        pageView->setRowHeight(rowcounter*rowmult+rowadd, pix.height());
283
                }
284
                else
285
                        pageView->setRowHeight(rowcounter*rowmult+rowadd, pix.height()+5);
286
                counter++;
287
                if (counter > currView->Doc->pageSets()[currView->Doc->pagePositioning()].Columns-1)
288
                {
289
                        counter = 0;
290
                        rowcounter++;
291
                }
292
        }
293
        pageView->setRowHeight(pageView->rowCount()-1, 10);
294
        counter = 0;
295
        if (cols != 1)
296
        {
297
                for (int c = 0; c < pageView->columnCount(); ++c)
298
                {
299
                        if ((counter % 2) == 0)
300
                                pageView->setColumnWidth(counter, 10);
301
                        else
302
                                pageView->setColumnWidth(counter, pix.width());
303
                        counter++;
304
                }
305
        }
306
        pageView->repaint();
307
        if (currView != 0)
308
                markPage(currView->Doc->currentPageNumber());
309
        connect(pageLayout, SIGNAL(selectedLayout(int )), this, SLOT(handlePageLayout(int )));
310
        connect(pageLayout, SIGNAL(selectedFirstPage(int )), this, SLOT(handleFirstPage(int )));
311
}
312
 
313
void PagePalette_Pages::Rebuild()
314
{
315
        rebuildMasters();
316
        rebuildPages();
317
        enablePalette(currView != 0);
318
}
319
 
320
void PagePalette_Pages::markPage(uint nr)
321
{
322
        if (currView == 0)
323
                return;
324
 
325
        SeItem *it;
326
        for (int a = 0; a < pageList.count(); a++)
327
        {
328
                it = pageList.at(a);
329
                if (it->pageNumber == nr)
330
                {
331
                        pageView->clearSelection();
332
                        pageView->item(it->row(), it->column())->setSelected(true);
333
                        pageView->scrollToItem(pageView->item(it->row(), it->column()), QAbstractItemView::EnsureVisible);
334
                        break;
335
                }
336
        }
337
}
338
 
339
void PagePalette_Pages::setView(ScribusView *view)
340
{
341
        if (m_scMW->scriptIsRunning())
342
                return;
343
        currView = view;
344
}
345
 
346
void PagePalette_Pages::selMasterPage()
347
{
348
        if (masterPageList->CurItem != 0)
19618 jghali 349
        {
350
                QVariant pageVar = masterPageList->CurItem->data(Qt::UserRole);
351
                emit gotoMasterPage(pageVar.toString());
352
        }
17513 jghali 353
}
354
 
18558 jghali 355
QPixmap PagePalette_Pages::createIcon(int nr, QString masterPage, QPixmap pixin)
17513 jghali 356
{
357
        QPainter p;
358
        // Necessary on windows to ensure the pixmap is drawable
359
        QPixmap ret(pixin.width(), pixin.height()); // Qt4, pixin.depth());
360
        if (p.begin(&ret))
361
        {
362
                p.drawPixmap( 0, 0, pixin );
363
//              if( !pixin.mask().isNull() )
364
//                      ret.setMask( pixin.mask() );
365
                p.setBrush(Qt::white);
366
                p.setBackground(Qt::white);
367
                p.setBackgroundMode(Qt::OpaqueMode);
18503 craig 368
                p.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
369
                //p.setFont(QFont("Helvetica", 12, QFont::Bold));
17513 jghali 370
                //QString tmp = tmp.setNum(nr+1);
371
                QString tmp(currView->Doc->getSectionPageNumberForPageIndex(nr));
372
                if (tmp.isEmpty())
373
                        tmp = tmp.setNum(nr+1);
18558 jghali 374
                QRegExp Exp ("([A-Z]*[0-9]*)( *[\\.|\\-|_] *)(.*)");
375
                if (Exp.indexIn(masterPage) != -1)
376
                        masterPage = Exp.cap(1);
18503 craig 377
                QRect d = QRect(0, 0, ret.width(), ret.height());
378
                p.setFont(QFont("Helvetica", 7, QFont::Normal));
18558 jghali 379
                p.drawText(d, Qt::AlignCenter, tmp+"\n"+masterPage);
17513 jghali 380
                p.end();
381
                if( !pixin.mask().isNull() )
382
                        ret.setMask( pixin.mask() );
383
        }
384
        return ret;
385
}
386
 
387
void PagePalette_Pages::changeEvent(QEvent *e)
388
{
389
        if (e->type() == QEvent::LanguageChange)
390
        {
391
                languageChange();
392
                return;
393
        }
394
 
395
        QWidget::changeEvent(e);
396
}
397
 
398
void PagePalette_Pages::languageChange()
399
{
400
        setWindowTitle( tr( "Arrange Pages" ) );
401
        textLabel1->setText( tr( "Available Master Pages:" ) );
402
        textLabel2->setText( tr( "Document Pages:" ) );
403
 
404
        masterPageList->setToolTip( "<qt>" + tr( "List of master pages in the document. Master page names may be dragged onto the page view below to apply master pages, or onto the empty space between pages to create new pages." ) + "</qt>");
405
        pageView->setToolTip( "<qt>" + tr( "List of normal pages in the document, shown with the document layout. Pages may be dragged to rearrange or delete them.")  + "</qt>");
406
        trash->setToolTip( "<qt>" + tr("Drag pages or master pages onto the trash to delete them") + "</qt>");
407
}