UBDocumentNavigator.cpp 12.3 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#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"
31 32
#include "board/UBBoardPaletteManager.h"
#include "core/UBApplicationController.h"
Claudio Valerio's avatar
Claudio Valerio committed
33

34 35
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
36 37 38 39 40 41
/**
 * \brief Constructor
 * @param parent as the parent widget
 * @param name as the object name
 */
UBDocumentNavigator::UBDocumentNavigator(QWidget *parent, const char *name):QGraphicsView(parent)
42 43 44 45 46 47 48
  , mScene(NULL)
  , mCrntItem(NULL)
  , mCrntDoc(NULL)
  , mNbColumns(1)
  , mThumbnailWidth(0)
  , mThumbnailMinWidth(100)
  , bNavig(false)
Claudio Valerio's avatar
Claudio Valerio committed
49 50 51 52 53 54 55
{
    setObjectName(name);
    mScene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(mScene);
    mThumbnailWidth = width() - 2*border();

shibakaneki's avatar
shibakaneki committed
56 57
    setFrameShadow(QFrame::Plain);

Claudio Valerio's avatar
Claudio Valerio committed
58
    connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(addNewPage()));
59
    connect(UBApplication::boardController, SIGNAL(setDocOnPageNavigator(UBDocumentProxy*)), this, SLOT(generateThumbnails()));
Claudio Valerio's avatar
Claudio Valerio committed
60
    connect(mScene, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
61
    connect(UBApplication::boardController, SIGNAL(documentReorganized(int)), this, SLOT(onMovedToIndex(int)));
Claudio Valerio's avatar
Claudio Valerio committed
62 63 64 65 66 67 68 69 70
}

/**
 * \brief Destructor
 */
UBDocumentNavigator::~UBDocumentNavigator()
{
    if(NULL != mCrntItem)
    {
71 72
        delete mCrntItem;
        mCrntItem = NULL;
Claudio Valerio's avatar
Claudio Valerio committed
73 74 75 76
    }

    if(NULL != mScene)
    {
77 78
        delete mScene;
        mScene = NULL;
Claudio Valerio's avatar
Claudio Valerio committed
79 80 81 82 83 84 85 86 87 88 89 90 91
    }
}

/**
 * \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)
    {
92 93
        mCrntDoc = document;
        generateThumbnails();
Claudio Valerio's avatar
Claudio Valerio committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }
}

/**
 * \brief Generate the thumbnails
 */
void UBDocumentNavigator::generateThumbnails()
{
    QList<QGraphicsItem*> items;
    //QList<QUrl> itemsPath;
    QStringList labels;

    // Get the thumbnails
    QList<QPixmap> thumbs = UBThumbnailAdaptor::load(mCrntDoc);

    for(int i = 0; i < thumbs.count(); i++)
    {
111
        QPixmap pix = thumbs.at(i);
112
        QGraphicsPixmapItem* pixmapItem = new UBSceneThumbnailNavigPixmap(pix, mCrntDoc, i);
Claudio Valerio's avatar
Claudio Valerio committed
113

114
        // Get the selected item
115
        if(UBApplication::boardController->activeSceneIndex() == i)
116
        {
117
            mCrntItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(pixmapItem);
118 119 120 121 122
            mCrntItem->setSelected(true);
        }

        items << pixmapItem;
        labels << tr("Page %0").arg(i + 1);
Claudio Valerio's avatar
Claudio Valerio committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    }

    // 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)
    {
140 141
        // Save the current state of the scene
        pScene->setModified(true);
shibakaneki's avatar
shibakaneki committed
142 143 144 145
        if(UBApplication::boardController)
        {
            UBApplication::boardController->persistCurrentScene();
        }
146 147 148 149 150 151

        UBThumbnailAdaptor::persistScene(mCrntDoc->persistencePath(), pScene, iPage);

        // Load it
        QList<QPixmap> thumbs = UBThumbnailAdaptor::load(mCrntDoc);
        QPixmap pix = thumbs.at(iPage);
152
        QGraphicsPixmapItem* pixmapItem = new UBSceneThumbnailNavigPixmap(pix, mCrntDoc, iPage);
153 154 155 156 157 158 159 160 161
        if(pixmapItem)
        {
            // Get the old thumbnail
            QGraphicsItem* pItem = mThumbnails.at(iPage);
            if(NULL != pItem)
            {
                mScene->removeItem(pItem);
                mScene->addItem(pixmapItem);
                mThumbnails.replace(iPage, pixmapItem);
162
                delete pItem;
163 164
            }
        }
Claudio Valerio's avatar
Claudio Valerio committed
165 166 167 168 169 170 171 172 173 174 175 176 177
    }
}

/**
 * \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)
    {
178
        generateThumbnails();
179 180
        if(NULL != mCrntItem)
        {
181
            mCrntItem->setSelected(true);
182
        }
Claudio Valerio's avatar
Claudio Valerio committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
}

/**
 * \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())
    {
198 199
        mScene->removeItem(it);
        delete it;
Claudio Valerio's avatar
Claudio Valerio committed
200 201 202 203 204
    }

    // Add the new thumbnails
    foreach(QGraphicsItem* it, items)
    {
205
        mScene->addItem(it);
Claudio Valerio's avatar
Claudio Valerio committed
206 207 208 209 210 211
    }

    // Add the labels
    mLabels.clear();
    foreach(QString lb, labels)
    {
212 213
        UBThumbnailTextItem *labelItem = new UBThumbnailTextItem(lb);
        mScene->addItem(labelItem);
Claudio Valerio's avatar
Claudio Valerio committed
214

215
        mLabels << labelItem;
Claudio Valerio's avatar
Claudio Valerio committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }

    // 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)
    {
231 232
        QFontMetrics fm(mLabels.at(0)->font());
        labelSpacing = UBSettings::thumbnailSpacing + fm.height();
Claudio Valerio's avatar
Claudio Valerio committed
233 234 235 236 237 238
    }

    qreal thumbnailHeight = mThumbnailWidth / UBSettings::minScreenRatio;

    for(int i = 0; i < mThumbnails.size(); i++)
    {
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
        // 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);
        }
Claudio Valerio's avatar
Claudio Valerio committed
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
    }
}

/**
 * \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
350 351 352 353 354 355
    //    QRect sceneRect;
    //    sceneRect.setWidth(width() - 2*border());
    //    sceneRect.setHeight(height() - 2*border());
    //    sceneRect.moveLeft(border());
    //    sceneRect.moveTop(border());
    //    scene()->setSceneRect(sceneRect);
Claudio Valerio's avatar
Claudio Valerio committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369

    // 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)
    {
370 371 372
        bNavig = true;

        // First, select the clicked item
373
        UBSceneThumbnailNavigPixmap* pCrntItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(pClickedItem);
374 375 376 377 378 379 380

        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)
            {
381
                pCrntItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(mThumbnails.at(mLabels.indexOf(pTextItem)));
382 383
            }
        }
Claudio Valerio's avatar
Claudio Valerio committed
384 385 386 387 388 389 390 391 392 393
        else
        {
            if(NULL != mCrntItem && mCrntItem != pCrntItem)
            {
                // Unselect the previous item
                int iOldPage = mThumbnails.indexOf(mCrntItem);
                updateSpecificThumbnail(iOldPage);
                mCrntItem = pCrntItem;
            }

394 395 396 397
            // HACK: for an unknown reason, the mousePressEvent of the item is not
            //       called when a click occurs on it. So I created this method in
            //       order to handle the click.
            mCrntItem->notifyClick(mapToScene(event->pos()));
Claudio Valerio's avatar
Claudio Valerio committed
398 399 400 401 402

            // Then display the related page
            emit changeCurrentPage();
            refreshScene();
        }
403
        bNavig = false;
Claudio Valerio's avatar
Claudio Valerio committed
404 405 406 407 408 409 410 411 412 413 414 415 416
    }
}

/**
 * \brief Get the selected page number
 * @return the selected page number
 */
int UBDocumentNavigator::selectedPageNumber()
{
    int nbr = NO_PAGESELECTED;

    if(NULL != mCrntItem)
    {
417
        nbr = mThumbnails.indexOf(mCrntItem);
Claudio Valerio's avatar
Claudio Valerio committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    }

    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()
{
437 438
    //    QList<QGraphicsItem*> qlItems = mScene->selectedItems();
    //    qDebug() << "The number of selected items is " << qlItems.count();
Claudio Valerio's avatar
Claudio Valerio committed
439
}
440 441 442 443 444 445 446

/**
 * \brief Occurs when a page has been moved to another index in the document
 * @param index as the new index
 */
void UBDocumentNavigator::onMovedToIndex(int index)
{
shibakaneki's avatar
shibakaneki committed
447 448 449 450 451 452 453 454
    if(index < mThumbnails.size()){
        UBSceneThumbnailNavigPixmap* pItem = dynamic_cast<UBSceneThumbnailNavigPixmap*>(mThumbnails.at(index));
        if(NULL != pItem)
        {
            mCrntItem = pItem;
            mCrntItem->setSelected(true);
            centerOn(mCrntItem);
        }
455 456
    }
}