freetype-commit
[Top][All Lists]
Advanced

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

[freetype2-demos] master 9733a16: [ftinspect] Re-implement file watching


From: Werner LEMBERG
Subject: [freetype2-demos] master 9733a16: [ftinspect] Re-implement file watching.
Date: Wed, 18 May 2016 20:42:59 +0000 (UTC)

branch: master
commit 9733a16b13ced0f67f96485121802fb7fee2e7d4
Author: Werner Lemberg <address@hidden>
Commit: Werner Lemberg <address@hidden>

    [ftinspect] Re-implement file watching.
    
    * src/ftinspect.cpp (MainGUI::MainGUI): Initialize file watcher and
    timer.
    (MainGUI::closeFont): Updated.
    (MainGUI::watchCurrentFont): New method.
    (MainGUI::showFont): Handle file watching for both real files and
    softlinks.
    Diplsy a softlink name as italic font.
    (MainGUI::createConnections): Updated.
    
    * src/ftinspect.h (MainGUI): Updated.
---
 ChangeLog         |   17 +++++++++++++++-
 src/ftinspect.cpp |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/ftinspect.h   |    7 +++++++
 3 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 08d2e48..e0527c1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,23 @@
 2016-05-15  Werner Lemberg  <address@hidden>
 
+       [ftinspect] Re-implement file watching.
+
+       * src/ftinspect.cpp (MainGUI::MainGUI): Initialize file watcher and
+       timer.
+       (MainGUI::closeFont): Updated.
+       (MainGUI::watchCurrentFont): New method.
+       (MainGUI::showFont): Handle file watching for both real files and
+       softlinks.
+       Diplsy a softlink name as italic font.
+       (MainGUI::createConnections): Updated.
+
+       * src/ftinspect.h (MainGUI): Updated.
+
+2016-05-15  Werner Lemberg  <address@hidden>
+
        [ftinspect] Redesign file/face/instance navigation.
 
-       Instead no longer peek into a font with a negative argument to
+       Instead of peeking into a font with a negative argument to
        `FT_Face_New', we now directly load appropriate (font, face,
        instance) triplets.
 
diff --git a/src/ftinspect.cpp b/src/ftinspect.cpp
index c390d0c..619df37 100644
--- a/src/ftinspect.cpp
+++ b/src/ftinspect.cpp
@@ -1160,6 +1160,11 @@ MainGUI::MainGUI()
 {
   engine = NULL;
 
+  fontWatcher = new QFileSystemWatcher;
+  // if the current input file is invalid we retry once a second to load it
+  timer = new QTimer;
+  timer->setInterval(1000);
+
   setGraphicsDefaults();
   createLayout();
   createConnections();
@@ -1256,6 +1261,7 @@ MainGUI::closeFont()
   if (currentFontIndex < fontList.size())
   {
     engine->removeFont(currentFontIndex);
+    fontWatcher->removePath(fontList[currentFontIndex]);
     fontList.removeAt(currentFontIndex);
   }
 
@@ -1273,6 +1279,14 @@ MainGUI::closeFont()
 
 
 void
+MainGUI::watchCurrentFont()
+{
+  timer->stop();
+  showFont();
+}
+
+
+void
 MainGUI::showFont()
 {
   // we do lazy computation of FT_Face objects
@@ -1280,7 +1294,32 @@ MainGUI::showFont()
   if (currentFontIndex < fontList.size())
   {
     QString& font = fontList[currentFontIndex];
-    fontFilenameLabel->setText(QFileInfo(font).fileName());
+    QFileInfo fileInfo(font);
+    QString fontName = fileInfo.fileName();
+
+    if (fileInfo.exists())
+    {
+      // Qt's file watcher doesn't handle symlinks;
+      // we thus fall back to polling
+      if (fileInfo.isSymLink())
+      {
+        fontName.prepend("<i>");
+        fontName.append("</i>");
+        timer->start();
+      }
+      else
+        fontWatcher->addPath(font);
+    }
+    else
+    {
+      // On Unix-like systems, the symlink's target gets opened; this
+      // implies that deletion of a symlink doesn't make `engine->loadFont'
+      // fail since it operates on a file handle pointing to the target.
+      // For this reason, we remove the font to enforce a reload.
+      engine->removeFont(currentFontIndex);
+    }
+
+    fontFilenameLabel->setText(fontName);
   }
   else
     fontFilenameLabel->clear();
@@ -1295,6 +1334,16 @@ MainGUI::showFont()
                        currentFaceIndex,
                        currentNamedInstanceIndex);
 
+  if (currentNumberOfGlyphs < 0)
+  {
+    // there might be various reasons why the current
+    // (file, face, instance) triplet is invalid or missing;
+    // we thus start our timer to periodically test
+    // whether the font starts working
+    if (currentFontIndex < fontList.size())
+      timer->start();
+  }
+
   fontNameLabel->setText(QString("%1 %2")
                          .arg(engine->currentFamilyName())
                          .arg(engine->currentStyleName()));
@@ -2205,6 +2254,11 @@ MainGUI::createConnections()
   glyphNavigationMapper->setMapping(toP100Buttonx, 100);
   glyphNavigationMapper->setMapping(toP1000Buttonx, 1000);
   glyphNavigationMapper->setMapping(toEndButtonx, 0x10000);
+
+  connect(fontWatcher, SIGNAL(fileChanged(const QString&)),
+          SLOT(watchCurrentFont()));
+  connect(timer, SIGNAL(timeout()),
+          SLOT(watchCurrentFont()));
 }
 
 
diff --git a/src/ftinspect.h b/src/ftinspect.h
index c9b5506..edbd9ff 100644
--- a/src/ftinspect.h
+++ b/src/ftinspect.h
@@ -29,6 +29,7 @@
 #include <QDoubleSpinBox>
 #include <QFileDialog>
 #include <QFileInfo>
+#include <QFileSystemWatcher>
 #include <QGraphicsItem>
 #include <QGraphicsScene>
 #include <QGraphicsView>
@@ -54,6 +55,7 @@
 #include <QStandardItemModel>
 #include <QStatusBar>
 #include <QTabWidget>
+#include <QTimer>
 #include <QTransform>
 #include <QVariant>
 #include <QVector2D>
@@ -330,6 +332,7 @@ private slots:
   void previousFace();
   void previousFont();
   void previousNamedInstance();
+  void watchCurrentFont();
   void zoom();
 
 private:
@@ -381,6 +384,8 @@ private:
 
   QDoubleSpinBox *sizeDoubleSpinBox;
 
+  QFileSystemWatcher *fontWatcher;
+
   QGraphicsScene *glyphScene;
   QGraphicsView *glyphView;
 
@@ -460,6 +465,8 @@ private:
 
   QTabWidget *tabWidget;
 
+  QTimer *timer;
+
   QVBoxLayout *generalTabLayout;
   QVBoxLayout *leftLayout;
   QVBoxLayout *rightLayout;



reply via email to

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