From c2eca928c5a7be0444fcb5c41ffe77b557714575 Mon Sep 17 00:00:00 2001 From: Javier Date: Sat, 12 Sep 2020 02:21:04 +0200 Subject: when zooming out, place pages on a grid layout --- afdnotebook.cc | 2 ++ main.cc | 3 +++ mainwindow.ui | 6 +++--- notebookmodel.cc | 20 +++++++++---------- notebookmodel.h | 3 +++ notebookview.cc | 61 +++++++++++++++++++++++++++++++++++++++++++------------- notebookview.h | 3 +++ pageitem.cc | 13 ++++++++++++ pageitem.h | 3 +++ stfstrokeitem.cc | 13 +++++++++--- stfstrokeitem.h | 6 ++++-- 11 files changed, 101 insertions(+), 32 deletions(-) diff --git a/afdnotebook.cc b/afdnotebook.cc index c4a4964..913986e 100644 --- a/afdnotebook.cc +++ b/afdnotebook.cc @@ -121,6 +121,8 @@ QPixmap AfdNotebook::getPageBackground(int page) const QString file = QString("userdata/lsac_data/%1.png").arg(p.gfx->basename); QImage img; + qDebug() << "Loading page background " << file; + if (!img.load(_dir.filePath(file), "PNG")) { qWarning() << "Could not load background file:" << _dir.absoluteFilePath(file); return pix; diff --git a/main.cc b/main.cc index 406a729..cfd8bc2 100644 --- a/main.cc +++ b/main.cc @@ -18,6 +18,7 @@ #include "mainwindow.h" #include +#include int main(int argc, char *argv[]) { @@ -30,6 +31,8 @@ int main(int argc, char *argv[]) app.setApplicationName("scribiu"); app.setApplicationVersion("1.2"); + QPixmapCache::setCacheLimit(100 * 1024); + MainWindow w; w.show(); diff --git a/mainwindow.ui b/mainwindow.ui index c04ff30..5c1774c 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -56,7 +56,7 @@ false - 28 + 29 false @@ -160,7 +160,7 @@ - + 0 0 @@ -302,7 +302,7 @@ 0 0 718 - 29 + 27 diff --git a/notebookmodel.cc b/notebookmodel.cc index 8a58ebf..bfe183b 100644 --- a/notebookmodel.cc +++ b/notebookmodel.cc @@ -291,6 +291,7 @@ void NotebookModel::refreshPen(const QString &name) return; } + _iconCache.clear(); _watcher.addPath(penDir.canonicalPath()); QStringList &curNotebooks = _notebooks[name]; @@ -359,22 +360,21 @@ QDir NotebookModel::notebookDir(const QString &pen, const QString ¬ebook) con QIcon NotebookModel::getNotebookIcon(const QString &pen, const QString ¬ebook) const { - static QStringList candidates; - if (candidates.isEmpty()) { - candidates << "userdata/icon/Notebook.png" - << "userdata/icon/active_64x64.png" - << "userdata/icon/active_32x32.png" - << "userdata/icon/active_16x16.png"; - } - QIcon icon; - QDir dir = notebookDir(pen, notebook); - if (dir.exists()) { + + QIcon icon = _iconCache.value(dir.path()); + if (icon.isNull() && dir.exists()) { + static const QStringList candidates{"userdata/icon/Notebook.png", + "userdata/icon/active_64x64.png", + "userdata/icon/active_32x32.png", + "userdata/icon/active_16x16.png"}; foreach (const QString &candidate, candidates) { if (dir.exists(candidate)) { icon.addFile(dir.filePath(candidate)); } } + + _iconCache.insert(dir.path(), icon); } return icon; diff --git a/notebookmodel.h b/notebookmodel.h index 1d69e14..f847c77 100644 --- a/notebookmodel.h +++ b/notebookmodel.h @@ -22,6 +22,7 @@ #include #include #include +#include class NotebookModel : public QAbstractItemModel { @@ -72,6 +73,8 @@ private: QFileSystemWatcher _watcher; QStringList _pens; QHash _notebooks; + + mutable QHash _iconCache; }; #endif // NOTEBOOKMODEL_H diff --git a/notebookview.cc b/notebookview.cc index 1f97ead..833f8e8 100644 --- a/notebookview.cc +++ b/notebookview.cc @@ -25,7 +25,7 @@ NotebookView::NotebookView(QWidget *parent) : QGraphicsView(parent), _nb(new AfdNotebook(this)), _replay(new PaperReplay(this)), - _zoom(100), _curPage(0) + _numColumns(1), _zoom(100), _curPage(0) { setScene(new QGraphicsScene(this)); setTransformationAnchor(AnchorUnderMouse); @@ -102,7 +102,12 @@ void NotebookView::setZoom(int zoom) { if (zoom != _zoom) { _zoom = zoom; + int oldNumColumns = _numColumns; calculateScale(); + if (_numColumns != oldNumColumns) { + layoutPages(); + } + emit zoomChanged(_zoom); } } @@ -118,7 +123,7 @@ QRect NotebookView::getCurPageTrim() const QImage NotebookView::exportPage(int pageNum) const { - const QRect pageTrim = getCurPageTrim(); + const QRect pageTrim = _nb->getPageTrim(pageNum); QImage image(pageTrim.width() / 4, pageTrim.height() / 4, QImage::Format_RGB32); QPainter painter(&image); painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); @@ -162,6 +167,16 @@ void NotebookView::nextPage() } } +void NotebookView::focusOnPage(int pageNum) +{ + if (_pages.contains(pageNum)) { + if (_zoom < 100) { + setZoom(100); + } + setCurPage(pageNum); + } +} + void NotebookView::resizeEvent(QResizeEvent *event) { QGraphicsView::resizeEvent(event); @@ -226,21 +241,11 @@ bool NotebookView::createPages() _maxPageSize.setHeight(box.height()); } _pages.insert(pageNum, page); - } - - calculateScale(); - - qreal curY = 0; - foreach (PageItem *page, _pages) { - QRectF box = page->boundingRect(); - page->setPos((_maxPageSize.width() - box.width()) / 2.0, curY); - curY += box.height(); - curY += PAGE_SEPARATION; - scene()->addItem(page); } - scene()->setSceneRect(0, 0, _maxPageSize.width(), curY); + calculateScale(); + layoutPages(); return true; } @@ -253,12 +258,40 @@ void NotebookView::calculateScale() qreal baseScale = qMin(viewRect.width() / _maxPageSize.width(), viewRect.height() / _maxPageSize.height()); resetTransform(); + _numColumns = 1; + scale(baseScale, baseScale); + if (_zoom < 100) { qreal s = 0.25 + ((_zoom / 100.0) * 0.75); + int potentialColumns = viewRect.width() / (_maxPageSize.width() * baseScale * s); + if (potentialColumns >= 2) { + _numColumns = std::min(potentialColumns, _pages.size()); + } scale(s, s); } else if (_zoom > 100) { qreal s = 1.0 + (_zoom - 100) * 0.015; scale(s, s); } } + +void NotebookView::layoutPages() +{ + const int numRows = (_pages.size() + 1) / _numColumns; + const QSizeF pageSpace( _maxPageSize.width() + PAGE_SEPARATION, + _maxPageSize.height() + PAGE_SEPARATION); + + int pageIndex = 0; + foreach (PageItem *page, _pages) { + QRectF box = page->boundingRect(); + qreal curX = (pageIndex % _numColumns) * pageSpace.width(); + qreal curY = (pageIndex / _numColumns) * pageSpace.height(); + page->setPos(curX + (_maxPageSize.width() - box.width()) / 2.0, + curY + (_maxPageSize.height() - box.height()) / 2.0); + pageIndex++; + } + + scene()->setSceneRect(0, 0, + pageSpace.width() * _numColumns, + pageSpace.height() * numRows); +} diff --git a/notebookview.h b/notebookview.h index c9f5686..720a1a2 100644 --- a/notebookview.h +++ b/notebookview.h @@ -69,6 +69,7 @@ public slots: void setZoom(int zoom); void prevPage(); void nextPage(); + void focusOnPage(int pageNum); protected: void resizeEvent(QResizeEvent *event); @@ -78,6 +79,7 @@ private: void removePages(); bool createPages(); void calculateScale(); + void layoutPages(); private: AfdNotebook *_nb; @@ -86,6 +88,7 @@ private: QString _replayPath; QMap _pages; QSizeF _maxPageSize; + int _numColumns; int _zoom; int _curPage; }; diff --git a/pageitem.cc b/pageitem.cc index b5be4cf..9260ecd 100644 --- a/pageitem.cc +++ b/pageitem.cc @@ -18,6 +18,8 @@ #include #include +#include +#include "notebookview.h" #include "pageitem.h" #include "stfgraphicsitem.h" @@ -65,6 +67,17 @@ int PageItem::pageNum() const return _pageNum; } +void PageItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + QGraphicsView *view = scene()->views().first(); + if (NotebookView *nbview = qobject_cast(view)) { + nbview->focusOnPage(_pageNum); + event->accept(); + return; + } + QGraphicsItem::mouseDoubleClickEvent(event); +} + void PageItem::createStrokes() { QStringList pens = _nb->penSerials(); diff --git a/pageitem.h b/pageitem.h index 831ca61..0963276 100644 --- a/pageitem.h +++ b/pageitem.h @@ -37,6 +37,9 @@ public: int pageNum() const; +protected: + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; + private: void createStrokes(); diff --git a/stfstrokeitem.cc b/stfstrokeitem.cc index dbc7ca0..335e166 100644 --- a/stfstrokeitem.cc +++ b/stfstrokeitem.cc @@ -29,7 +29,7 @@ StfStrokeItem::StfStrokeItem(const QPainterPath &stroke, const PaperReplay::Sess _startTime(startTime), _endTime(endTime) { QPen pen(Qt::black, 8.0f); - if (_session.isValid()) { + if (hasPaperReplay()) { pen.setColor(Qt::darkGreen); setCursor(Qt::PointingHandCursor); } @@ -41,16 +41,23 @@ int StfStrokeItem::type() const return Type; } +bool StfStrokeItem::hasPaperReplay() const +{ + return _session.isValid() && !_session.fileName().isEmpty(); +} + void StfStrokeItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (_session.isValid() && !_session.fileName().isEmpty()) { + if (event->button() == Qt::LeftButton && hasPaperReplay()) { QGraphicsView *view = scene()->views().first(); if (NotebookView *nbview = qobject_cast(view)) { - event->accept(); qint64 time = _startTime - _session.startTime(); if (time < 10) time = 0; nbview->requestPaperReplay(_session.fileName(), time); + event->accept(); + return; } } + QGraphicsItem::mousePressEvent(event); } diff --git a/stfstrokeitem.h b/stfstrokeitem.h index 5e7a253..eaf8d8a 100644 --- a/stfstrokeitem.h +++ b/stfstrokeitem.h @@ -29,10 +29,12 @@ public: enum { Type = UserType + 't' }; - int type() const; + int type() const override; + + bool hasPaperReplay() const; protected: - void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mousePressEvent(QGraphicsSceneMouseEvent *event) override; private: PaperReplay::Session _session; -- cgit v1.2.3