freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] gsoc-2022-chariri-2 81f8d52 15/30: [ftinspect] Support


From: Werner Lemberg
Subject: [freetype2-demos] gsoc-2022-chariri-2 81f8d52 15/30: [ftinspect] Support infinite panning
Date: Mon, 11 Jul 2022 07:17:39 -0400 (EDT)

branch: gsoc-2022-chariri-2
commit 81f8d52b38d41e77fcebeb322ae1d37887277bf0
Author: Charlie Jiang <w@chariri.moe>
Commit: Charlie Jiang <w@chariri.moe>

    [ftinspect] Support infinite panning
    
    Now the `Grid` dynamically resizes (and resizes the outer scene as well) so
    one can drag/scroll infinitely on the grid. "Go back to center" is 
especially
    helpful when you get lost.
    
    * src/ftinspect/rendering/grid.hpp, src/ftinspect/rendering/grid.cpp: Add
    `updateRect` to dynamically update bounding rect of the Grid and the scene's
    rect.
    
    * src/ftinspect/maingui.hpp, src/ftinspect/maingui.cpp: Call
    `Grid::updateRect` when necessary. Note that `MainGUI` must manually 
maintain
    the ownership of `gridItem_` pointer to prevent crash.
---
 src/ftinspect/maingui.cpp        | 23 ++++++++++++--
 src/ftinspect/maingui.hpp        |  3 ++
 src/ftinspect/rendering/grid.cpp | 69 +++++++++++++++++++++++++++++-----------
 src/ftinspect/rendering/grid.hpp | 10 ++++--
 4 files changed, 82 insertions(+), 23 deletions(-)

diff --git a/src/ftinspect/maingui.cpp b/src/ftinspect/maingui.cpp
index 53aa019..32e6a01 100644
--- a/src/ftinspect/maingui.cpp
+++ b/src/ftinspect/maingui.cpp
@@ -10,6 +10,7 @@
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QSettings>
+#include <QScrollBar>
 
 #include <freetype/ftdriver.h>
 
@@ -32,7 +33,8 @@ MainGUI::MainGUI(Engine* engine)
 
 MainGUI::~MainGUI()
 {
-  // empty
+  delete gridItem_;
+  gridItem_ = NULL;
 }
 
 
@@ -429,6 +431,7 @@ MainGUI::zoom()
   transform.translate(shift, shift);
 
   glyphView_->setTransform(transform);
+  updateGrid();
 }
 
 
@@ -440,6 +443,16 @@ MainGUI::backToCenter()
     glyphView_->ensureVisible(currentGlyphBitmapItem_);
   else if (currentGlyphPointsItem_)
     glyphView_->ensureVisible(currentGlyphPointsItem_);
+
+  updateGrid();
+}
+
+
+void
+MainGUI::updateGrid()
+{
+  if (gridItem_)
+    gridItem_->updateRect();
 }
 
 
@@ -616,7 +629,6 @@ MainGUI::createLayout()
   fontNameLabel_ = new QLabel(this);
 
   glyphScene_ = new QGraphicsScene(this);
-  glyphScene_->addItem(new Grid(gridPen_, axisPen_));
 
   currentGlyphBitmapItem_ = NULL;
   currentGlyphOutlineItem_ = NULL;
@@ -630,6 +642,9 @@ MainGUI::createLayout()
   glyphView_->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
   glyphView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
   glyphView_->setScene(glyphScene_);
+  
+  gridItem_ = new Grid(glyphView_, gridPen_, axisPen_);
+  glyphScene_->addItem(gridItem_);
 
   // Don't use QGraphicsTextItem: We want this hint to be anchored at the
   // top-left corner.
@@ -782,6 +797,10 @@ MainGUI::createConnections()
           SLOT(wheelResize(QWheelEvent*)));
   connect(glyphView_, SIGNAL(ctrlWheelEvent(QWheelEvent*)), 
           SLOT(wheelZoom(QWheelEvent*)));
+  connect(glyphView_->horizontalScrollBar(), &QScrollBar::valueChanged,
+          this, &MainGUI::updateGrid);
+  connect(glyphView_->verticalScrollBar(), &QScrollBar::valueChanged, this,
+          &MainGUI::updateGrid);
 
   connect(centerGridButton_, SIGNAL(clicked()),
           SLOT(backToCenter()));
diff --git a/src/ftinspect/maingui.hpp b/src/ftinspect/maingui.hpp
index dc45cf1..13d0503 100644
--- a/src/ftinspect/maingui.hpp
+++ b/src/ftinspect/maingui.hpp
@@ -10,6 +10,7 @@
 #include "rendering/glyphoutline.hpp"
 #include "rendering/glyphpointnumbers.hpp"
 #include "rendering/glyphpoints.hpp"
+#include "rendering/grid.hpp"
 #include "widgets/custom_widgets.hpp"
 #include "models/ttsettingscomboboxmodel.hpp"
 #include "panels/settingpanel.hpp"
@@ -86,6 +87,7 @@ private slots:
   void watchCurrentFont();
   void zoom();
   void backToCenter();
+  void updateGrid();
   void wheelZoom(QWheelEvent* event);
   void wheelResize(QWheelEvent* event);
 
@@ -108,6 +110,7 @@ private:
   GlyphPoints *currentGlyphPointsItem_;
   GlyphPointNumbers *currentGlyphPointNumbersItem_;
   GlyphBitmap *currentGlyphBitmapItem_;
+  Grid *gridItem_ = NULL;
   QLabel* mouseUsageHint_;
 
   QAction *aboutAct_;
diff --git a/src/ftinspect/rendering/grid.cpp b/src/ftinspect/rendering/grid.cpp
index 01b17cb..875afc0 100644
--- a/src/ftinspect/rendering/grid.cpp
+++ b/src/ftinspect/rendering/grid.cpp
@@ -7,25 +7,51 @@
 
 #include <QPainter>
 #include <QStyleOptionGraphicsItem>
+#include <QGraphicsWidget>
+#include <QGraphicsView>
 
 
-Grid::Grid(const QPen& gridP,
+Grid::Grid(QGraphicsView* parentView, 
+           const QPen& gridP,
            const QPen& axisP)
 : gridPen_(gridP),
-  axisPen_(axisP)
+  axisPen_(axisP),
+  parentView_(parentView)
 {
  // empty
+  updateRect();
 }
 
 
 QRectF
 Grid::boundingRect() const
 {
-  // XXX fix size
+  return rect_;
+}
+
+
+void
+Grid::updateRect()
+{
+  auto viewport = parentView_->mapToScene(parentView_->viewport()->geometry())
+                         .boundingRect()
+                         .toRect();
+  int minX = std::min(viewport.left() - 10, -100);
+  int minY = std::min(viewport.top() - 10, -100);
+  int maxX = std::max(viewport.right() + 10, 100);
+  int maxY = std::max(viewport.bottom() + 10, 100);
+
+  auto newSceneRect = QRectF(QPointF(minX - 20, minY - 20), 
+                             QPointF(maxX + 20, maxY + 20));
+  if (sceneRect_ != newSceneRect && scene())
+  {
+    scene()->setSceneRect(newSceneRect);
+    sceneRect_ = newSceneRect;
+  }
 
   // no need to take care of pen width
-  return QRectF(-100, -100,
-                200, 200);
+  rect_ = QRectF(QPointF(minX, minY), 
+                QPointF(maxX, maxY));
 }
 
 
@@ -35,8 +61,14 @@ Grid::boundingRect() const
 void
 Grid::paint(QPainter* painter,
             const QStyleOptionGraphicsItem* option,
-            QWidget*)
+            QWidget* widget)
 {
+  auto br = boundingRect().toRect();
+  int minX = br.left();
+  int minY = br.top();
+  int maxX = br.right();
+  int maxY = br.bottom();
+
   const qreal lod = option->levelOfDetailFromTransform(
                               painter->worldTransform());
 
@@ -59,8 +91,8 @@ Grid::paint(QPainter* painter,
     else if (lod > 40)
       halfLength = 2;
 
-    for (qreal x = -100; x < 100; x++)
-      for (qreal y = -100; y < 100; y++)
+    for (qreal x = minX; x < maxX; x++)
+      for (qreal y = minY; y < maxY; y++)
       {
         painter->drawLine(QLineF(x + 0.5, y + 0.5 - halfLength / lod,
                                  x + 0.5, y + 0.5 + halfLength / lod));
@@ -72,21 +104,20 @@ Grid::paint(QPainter* painter,
   // don't draw grid if magnification is too small
   if (lod >= 5)
   {
-    // XXX fix size
-    for (int x = -100; x <= 100; x++)
-      painter->drawLine(x, -100,
-                        x, 100);
-    for (int y = -100; y <= 100; y++)
-      painter->drawLine(-100, y,
-                        100, y);
+    for (int x = minX; x <= maxX; x++)
+      painter->drawLine(x, minY,
+                        x, maxY);
+    for (int y = minY; y <= maxY; y++)
+      painter->drawLine(minX, y,
+                        maxX, y);
   }
 
   painter->setPen(axisPen_);
 
-  painter->drawLine(0, -100,
-                    0, 100);
-  painter->drawLine(-100, 0,
-                    100, 0);
+  painter->drawLine(0, minY,
+                    0, maxY);
+  painter->drawLine(minX, 0,
+                    maxX, 0);
 }
 
 
diff --git a/src/ftinspect/rendering/grid.hpp b/src/ftinspect/rendering/grid.hpp
index 9740c17..dd4d6c4 100644
--- a/src/ftinspect/rendering/grid.hpp
+++ b/src/ftinspect/rendering/grid.hpp
@@ -8,21 +8,27 @@
 #include <QGraphicsItem>
 #include <QPen>
 
-
 class Grid
 : public QGraphicsItem
 {
 public:
-  Grid(const QPen& gridPen,
+  Grid(QGraphicsView* parentView,
+       const QPen& gridPen,
        const QPen& axisPen);
   QRectF boundingRect() const;
   void paint(QPainter* painter,
              const QStyleOptionGraphicsItem* option,
              QWidget* widget);
 
+  void updateRect(); // there's no signal/slots for QGraphicsItem.
+
 private:
   QPen gridPen_;
   QPen axisPen_;
+
+  QGraphicsView* parentView_;
+  QRectF rect_;
+  QRectF sceneRect_;
 };
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]