freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] gsoc-2022-chariri-2 40b5110 14/30: [ftinspect] Improve


From: Werner Lemberg
Subject: [freetype2-demos] gsoc-2022-chariri-2 40b5110 14/30: [ftinspect] Improve mouse scrolling.
Date: Mon, 11 Jul 2022 07:17:38 -0400 (EDT)

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

    [ftinspect] Improve mouse scrolling.
    
    Now Ctrl+Scroll zooms the view, and Shift+Scroll changes the font size.
    
    Added a button to quickly go back to center.
    
    * src/ftinspect/widgets/custom_widgets.hpp,
      src/ftinspect/widgets/custom_widgets.cpp: Intercept Ctrl/Shift+Scroll 
events
      as separate events to emit.
    
    * src/ftinspect/maingui.hpp, src/ftinspect/maingui.cpp: Add 
Ctrl/Shift+Scroll
    event handlers; add a hint at top-left; add "go back to center" button.
---
 src/ftinspect/maingui.cpp                | 62 ++++++++++++++++++++++++++++++--
 src/ftinspect/maingui.hpp                |  5 +++
 src/ftinspect/widgets/custom_widgets.cpp | 13 +++++++
 src/ftinspect/widgets/custom_widgets.hpp |  5 +++
 4 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/src/ftinspect/maingui.cpp b/src/ftinspect/maingui.cpp
index 510f110..53aa019 100644
--- a/src/ftinspect/maingui.cpp
+++ b/src/ftinspect/maingui.cpp
@@ -432,6 +432,40 @@ MainGUI::zoom()
 }
 
 
+void
+MainGUI::backToCenter()
+{
+  glyphView_->centerOn(0, 0);
+  if (currentGlyphBitmapItem_)
+    glyphView_->ensureVisible(currentGlyphBitmapItem_);
+  else if (currentGlyphPointsItem_)
+    glyphView_->ensureVisible(currentGlyphPointsItem_);
+}
+
+
+void
+MainGUI::wheelZoom(QWheelEvent* event)
+{
+  int numSteps = event->angleDelta().y() / 120;
+  int zoomAfter = zoomSpinBox_->value() + numSteps;
+  zoomAfter = std::max(zoomSpinBox_->minimum(),
+                       std::min(zoomAfter, zoomSpinBox_->maximum()));
+  zoomSpinBox_->setValue(zoomAfter);
+  // TODO: Zoom relative to viewport left-bottom?
+}
+
+
+void
+MainGUI::wheelResize(QWheelEvent* event)
+{
+  int numSteps = event->angleDelta().y() / 120;
+  double sizeAfter = sizeDoubleSpinBox_->value() + numSteps * 0.5;
+  sizeAfter = std::max(sizeDoubleSpinBox_->minimum(),
+                       std::min(sizeAfter, sizeDoubleSpinBox_->maximum()));
+  sizeDoubleSpinBox_->setValue(sizeAfter);
+}
+
+
 void
 MainGUI::setGraphicsDefaults()
 {
@@ -597,8 +631,19 @@ MainGUI::createLayout()
   glyphView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
   glyphView_->setScene(glyphScene_);
 
+  // Don't use QGraphicsTextItem: We want this hint to be anchored at the
+  // top-left corner.
+  mouseUsageHint_ = new QLabel(tr("Scroll: Grid Up/Down\n"
+    "Alt + Scroll: Grid Left/Right\n"
+    "Ctrl + Scroll: Adjust Zoom (Relative to cursor)\n"
+    "Shift + Scroll: Adjust Font Size"), glyphView_);
+  mouseUsageHint_->setMargin(10);
+  auto hintFont = font();
+  hintFont.setPixelSize(24);
+  mouseUsageHint_->setFont(hintFont);
+
   sizeLabel_ = new QLabel(tr("Size "), this);
-  sizeLabel_->setAlignment(Qt::AlignRight);
+  sizeLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
   sizeDoubleSpinBox_ = new QDoubleSpinBox;
   sizeDoubleSpinBox_->setAlignment(Qt::AlignRight);
   sizeDoubleSpinBox_->setDecimals(1);
@@ -610,7 +655,7 @@ MainGUI::createLayout()
   unitsComboBox_->insertItem(Units_pt, "pt");
 
   dpiLabel_ = new QLabel(tr("DPI "), this);
-  dpiLabel_->setAlignment(Qt::AlignRight);
+  dpiLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
   dpiSpinBox_ = new QSpinBox(this);
   dpiSpinBox_->setAlignment(Qt::AlignRight);
   dpiSpinBox_->setRange(10, 600);
@@ -628,13 +673,15 @@ MainGUI::createLayout()
   toEndButtonx_ = new QPushButtonx(">|", this);
 
   zoomLabel_ = new QLabel(tr("Zoom Factor"), this);
-  zoomLabel_->setAlignment(Qt::AlignRight);
+  zoomLabel_->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
   zoomSpinBox_ = new QSpinBoxx(this);
   zoomSpinBox_->setAlignment(Qt::AlignRight);
   zoomSpinBox_->setRange(1, 1000 - 1000 % 64);
   zoomSpinBox_->setKeyboardTracking(false);
   zoomLabel_->setBuddy(zoomSpinBox_);
 
+  centerGridButton_ = new QPushButton("Go Back to Grid Center", this);
+
   previousFontButton_ = new QPushButton(tr("Previous Font"), this);
   nextFontButton_ = new QPushButton(tr("Next Font"), this);
   previousFaceButton_ = new QPushButton(tr("Previous Face"), this);
@@ -674,6 +721,8 @@ MainGUI::createLayout()
   sizeLayout_->addStretch(1);
   sizeLayout_->addWidget(zoomLabel_);
   sizeLayout_->addWidget(zoomSpinBox_);
+  sizeLayout_->addStretch(1);
+  sizeLayout_->addWidget(centerGridButton_);
   sizeLayout_->addStretch(2);
 
   fontLayout = new QGridLayout;
@@ -729,6 +778,13 @@ MainGUI::createConnections()
 
   connect(zoomSpinBox_, SIGNAL(valueChanged(int)),
           SLOT(zoom()));
+  connect(glyphView_, SIGNAL(shiftWheelEvent(QWheelEvent*)), 
+          SLOT(wheelResize(QWheelEvent*)));
+  connect(glyphView_, SIGNAL(ctrlWheelEvent(QWheelEvent*)), 
+          SLOT(wheelZoom(QWheelEvent*)));
+
+  connect(centerGridButton_, SIGNAL(clicked()),
+          SLOT(backToCenter()));
 
   connect(previousFontButton_, SIGNAL(clicked()),
           SLOT(previousFont()));
diff --git a/src/ftinspect/maingui.hpp b/src/ftinspect/maingui.hpp
index 969392f..dc45cf1 100644
--- a/src/ftinspect/maingui.hpp
+++ b/src/ftinspect/maingui.hpp
@@ -85,6 +85,9 @@ private slots:
   void previousNamedInstance();
   void watchCurrentFont();
   void zoom();
+  void backToCenter();
+  void wheelZoom(QWheelEvent* event);
+  void wheelResize(QWheelEvent* event);
 
 private:
   Engine* engine_;
@@ -105,6 +108,7 @@ private:
   GlyphPoints *currentGlyphPointsItem_;
   GlyphPointNumbers *currentGlyphPointNumbersItem_;
   GlyphBitmap *currentGlyphBitmapItem_;
+  QLabel* mouseUsageHint_;
 
   QAction *aboutAct_;
   QAction *aboutQtAct_;
@@ -148,6 +152,7 @@ private:
   QPen outlinePen_;
   QPen segmentPen_;
 
+  QPushButton *centerGridButton_;
   QPushButton *nextFaceButton_;
   QPushButton *nextFontButton_;
   QPushButton *nextNamedInstanceButton_;
diff --git a/src/ftinspect/widgets/custom_widgets.cpp 
b/src/ftinspect/widgets/custom_widgets.cpp
index 6856a26..f0969d7 100644
--- a/src/ftinspect/widgets/custom_widgets.cpp
+++ b/src/ftinspect/widgets/custom_widgets.cpp
@@ -4,6 +4,7 @@
 
 #include "custom_widgets.hpp"
 
+#include <qevent.h>
 #include <QStandardItemModel>
 #include <QScrollBar>
 #include <QStyleOptionButton>
@@ -19,6 +20,18 @@ QGraphicsViewx::QGraphicsViewx(QWidget* parent)
 }
 
 
+void
+QGraphicsViewx::wheelEvent(QWheelEvent* event)
+{
+  if (event->modifiers() & Qt::ShiftModifier)
+    emit shiftWheelEvent(event);
+  else if (event->modifiers() & Qt::ControlModifier)
+    emit ctrlWheelEvent(event);
+  else
+    QGraphicsView::wheelEvent(event);
+}
+
+
 void
 QGraphicsViewx::scrollContentsBy(int dx,
                                  int dy)
diff --git a/src/ftinspect/widgets/custom_widgets.hpp 
b/src/ftinspect/widgets/custom_widgets.hpp
index 18dd6d2..9895419 100644
--- a/src/ftinspect/widgets/custom_widgets.hpp
+++ b/src/ftinspect/widgets/custom_widgets.hpp
@@ -26,7 +26,12 @@ class QGraphicsViewx
 public:
   QGraphicsViewx(QWidget* parent);
 
+signals:
+  void shiftWheelEvent(QWheelEvent* event);
+  void ctrlWheelEvent(QWheelEvent* event);
+
 protected:
+  void wheelEvent(QWheelEvent* event);
   void resizeEvent(QResizeEvent* event);
   void scrollContentsBy(int dx,
                         int dy);



reply via email to

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