1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QList>
#include <QPointF>
#include <QPixmap>
#include <QTransform>
#include <QScrollBar>
#include <QFontMetrics>
#include <QGraphicsItem>
#include <QGraphicsPixmapItem>
#include "core/UBApplication.h"
#include "UBDocumentNavigator.h"
#include "board/UBBoardController.h"
#include "adaptors/UBThumbnailAdaptor.h"
#include "adaptors/UBSvgSubsetAdaptor.h"
#include "document/UBDocumentController.h"
#include "domain/UBGraphicsScene.h"
#include "core/memcheck.h"
/**
* \brief Constructor
* @param parent as the parent widget
* @param name as the object name
*/
UBDocumentNavigator::UBDocumentNavigator(QWidget *parent, const char *name):QGraphicsView(parent)
, mScene(NULL)
, mCrntItem(NULL)
, mCrntDoc(NULL)
, mNbColumns(1)
, mThumbnailWidth(0)
, mThumbnailMinWidth(100)
, bNavig(false)
{
setObjectName(name);
mScene = new QGraphicsScene(this);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setScene(mScene);
mThumbnailWidth = width() - 2*border();
setFrameShadow(QFrame::Plain);
connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(addNewPage()));
connect(UBApplication::boardController, SIGNAL(setDocOnPageNavigator(UBDocumentProxy*)), this, SLOT(generateThumbnails()));
connect(mScene, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
}
/**
* \brief Destructor
*/
UBDocumentNavigator::~UBDocumentNavigator()
{
if(NULL != mCrntItem)
{
delete mCrntItem;
mCrntItem = NULL;
}
if(NULL != mScene)
{
delete mScene;
mScene = NULL;
}
}
/**
* \brief Set the current document
* @param document as the new document
*/
void UBDocumentNavigator::setDocument(UBDocumentProxy *document)
{
// Here we set a new document to the navigator. We must clear the current
// content and add all the pages of the given document.
if(document)
{
mCrntDoc = document;
generateThumbnails();
}
}
/**
* \brief Generate the thumbnails
*/
void UBDocumentNavigator::generateThumbnails()
{
QList<QGraphicsItem*> items;
//QList<QUrl> itemsPath;
QStringList labels;
// Get the thumbnails
QList<QPixmap> thumbs = UBThumbnailAdaptor::load(mCrntDoc);
QGraphicsPixmapItem* selection = NULL;
for(int i = 0; i < thumbs.count(); i++)
{
QPixmap pix = thumbs.at(i);
QGraphicsPixmapItem* pixmapItem = new UBSceneThumbnailPixmap(pix, mCrntDoc, i);
// Get the selected item
if(UBApplication::boardController->activeSceneIndex() == i)
{
selection = pixmapItem;
mCrntItem = dynamic_cast<UBSceneThumbnailPixmap*>(pixmapItem);
mCrntItem->setSelected(true);
}
items << pixmapItem;
labels << tr("Page %0").arg(i + 1);
}
// Draw the items
setGraphicsItems(items, labels);
}
/**
* \brief Refresh the given thumbnail
* @param iPage as the given page related thumbnail
*/
void UBDocumentNavigator::updateSpecificThumbnail(int iPage)
{
// Generate the new thumbnail
UBGraphicsScene* pScene = UBApplication::boardController->activeScene();
if(NULL != pScene)
{
// Save the current state of the scene
pScene->setModified(true);
UBSvgSubsetAdaptor::persistScene(mCrntDoc,pScene, iPage);
UBThumbnailAdaptor::persistScene(mCrntDoc->persistencePath(), pScene, iPage);
// Load it
QList<QPixmap> thumbs = UBThumbnailAdaptor::load(mCrntDoc);
QPixmap pix = thumbs.at(iPage);
QGraphicsPixmapItem* pixmapItem = new UBSceneThumbnailPixmap(pix, mCrntDoc, iPage);
if(pixmapItem)
{
// Get the old thumbnail
QGraphicsItem* pItem = mThumbnails.at(iPage);
if(NULL != pItem)
{
mScene->removeItem(pItem);
mScene->addItem(pixmapItem);
mThumbnails.replace(iPage, pixmapItem);
}
}
}
}
/**
* \brief Add a new page to the thumbnails list
*
* This method is called automatically by the board controller each time the user
* adds a new page, duplicates a page or imports a document.
*/
void UBDocumentNavigator::addNewPage()
{
if(!bNavig)
{
generateThumbnails();
if(NULL != mCrntItem)
{
mCrntItem->setSelected(true);
}
}
}
/**
* \brief Set the graphics items of the scene
* @param items as the items list
* @param labels as the page labels
*/
void UBDocumentNavigator::setGraphicsItems(QList<QGraphicsItem *> items, QStringList labels)
{
mThumbnails = items;
mLab = labels;
// First, clear the actual thumbnails
foreach(QGraphicsItem* it, mScene->items())
{
mScene->removeItem(it);
delete it;
}
// Add the new thumbnails
foreach(QGraphicsItem* it, items)
{
mScene->addItem(it);
}
// Add the labels
mLabels.clear();
foreach(QString lb, labels)
{
UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(lb);
mScene->addItem(labelItem);
mLabels << labelItem;
}
// Refresh the scene
refreshScene();
}
/**
* \brief Put the element in the right place in the scene.
*/
void UBDocumentNavigator::refreshScene()
{
int labelSpacing = 0;
if(mLabels.size() > 0)
{
QFontMetrics fm(mLabels.at(0)->font());
labelSpacing = UBSettings::thumbnailSpacing + fm.height();
}
qreal thumbnailHeight = mThumbnailWidth / UBSettings::minScreenRatio;
for(int i = 0; i < mThumbnails.size(); i++)
{
// Get the item
QGraphicsItem* item = mThumbnails.at(i);
// Compute the scale factor
qreal scaleWidth = mThumbnailWidth / item->boundingRect().width();
qreal scaleHeight = thumbnailHeight / item->boundingRect().height();
qreal scaleFactor = qMin(scaleWidth, scaleHeight);
UBThumbnail* pix = dynamic_cast<UBThumbnail*>(item);
if(pix)
{
scaleFactor = qMin(scaleFactor, 1.0);
}
QTransform transform;
transform.scale(scaleFactor, scaleFactor);
// Apply the scaling
item->setTransform(transform);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
int columnIndex = i % mNbColumns;
int rowIndex = i / mNbColumns;
if(pix)
{
pix->setColumn(columnIndex);
pix->setRow(rowIndex);
}
int w = item->boundingRect().width();
int h = item->boundingRect().height();
QPointF pos( border() + (mThumbnailWidth - w * scaleFactor) / 2 + columnIndex * (mThumbnailWidth + border()),
border() + rowIndex * (thumbnailHeight + border() + labelSpacing) + (thumbnailHeight - h * scaleFactor) / 2);
item->setPos(pos);
// Add the labels "Page x"
if(mLabels.size() > i)
{
QFontMetrics fm(mLabels.at(i)->font(), this);
QString elidedText = fm.elidedText(mLab.at(i), Qt::ElideRight, mThumbnailWidth);
mLabels.at(i)->setPlainText(elidedText);
mLabels.at(i)->setWidth(fm.width(elidedText) + 2 * mLabels.at(i)->document()->documentMargin());
pos.setY(pos.y() + (thumbnailHeight + h * scaleFactor) / 2 + 5); // What is this 5 ??
qreal labelWidth = fm.width(elidedText);
pos.setX(border() + (mThumbnailWidth - labelWidth) / 2 + columnIndex * (mThumbnailWidth + border()));
mLabels.at(i)->setPos(pos);
}
}
QScrollBar *vertScrollBar = verticalScrollBar();
int scrollBarThickness = 0;
if (vertScrollBar && vertScrollBar->isVisible())
scrollBarThickness = vertScrollBar->width();
}
/**
* \brief Set the number of thumbnails columns
* @param nbColumns as the number of columns
*/
void UBDocumentNavigator::setNbColumns(int nbColumns)
{
mNbColumns = nbColumns;
}
/**
* \brief Get the number of columns
* @return the number of thumbnails columns
*/
int UBDocumentNavigator::nbColumns()
{
return mNbColumns;
}
/**
* \brief Set the thumbnails minimum width
* @param width as the minimum width
*/
void UBDocumentNavigator::setThumbnailMinWidth(int width)
{
mThumbnailMinWidth = width;
}
/**
* \brief Get the thumbnails minimum width
* @return the minimum thumbnails width
*/
int UBDocumentNavigator::thumbnailMinWidth()
{
return mThumbnailMinWidth;
}
/**
* \brief Get the border size
* @return the border size in pixels
*/
int UBDocumentNavigator::border()
{
return 20;
}
/**
* \brief Handle the resize event
* @param event as the resize event
*/
void UBDocumentNavigator::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
// Update the thumbnails width
mThumbnailWidth = (width() > mThumbnailMinWidth) ? width() - 2*border() : mThumbnailMinWidth;
// Update the scene rect
// QRect sceneRect;
// sceneRect.setWidth(width() - 2*border());
// sceneRect.setHeight(height() - 2*border());
// sceneRect.moveLeft(border());
// sceneRect.moveTop(border());
// scene()->setSceneRect(sceneRect);
// Refresh the scene
refreshScene();
}
/**
* \brief Handle the mouse press event
* @param event as the mouse event
*/
void UBDocumentNavigator::mousePressEvent(QMouseEvent *event)
{
QGraphicsItem* pClickedItem = itemAt(event->pos());
if(NULL != pClickedItem)
{
bNavig = true;
// First, select the clicked item
UBSceneThumbnailPixmap* pCrntItem = dynamic_cast<UBSceneThumbnailPixmap*>(pClickedItem);
if(NULL == pCrntItem)
{
// If we fall here we may have clicked on the label instead of the thumbnail
UBThumbnailTextItem* pTextItem = dynamic_cast<UBThumbnailTextItem*>(pClickedItem);
if(NULL != pTextItem)
{
pCrntItem = dynamic_cast<UBSceneThumbnailPixmap*>(mThumbnails.at(mLabels.indexOf(pTextItem)));
}
}
else
{
if(NULL != mCrntItem && mCrntItem != pCrntItem)
{
// Unselect the previous item
int iOldPage = mThumbnails.indexOf(mCrntItem);
mCrntItem->setSelected(false);
updateSpecificThumbnail(iOldPage);
mCrntItem = pCrntItem;
}
pCrntItem->setSelected(true);
// Then display the related page
emit changeCurrentPage();
refreshScene();
}
bNavig = false;
}
}
/**
* \brief Get the selected page number
* @return the selected page number
*/
int UBDocumentNavigator::selectedPageNumber()
{
int nbr = NO_PAGESELECTED;
if(NULL != mCrntItem)
{
nbr = mThumbnails.indexOf(mCrntItem);
}
return nbr;
}
/**
* \brief Get the current document
* @return the current document
*/
UBDocumentProxy* UBDocumentNavigator::currentDoc()
{
return mCrntDoc;
}
/**
* \brief Occurs when the selection changed
*/
void UBDocumentNavigator::onSelectionChanged()
{
// QList<QGraphicsItem*> qlItems = mScene->selectedItems();
// qDebug() << "The number of selected items is " << qlItems.count();
}