Quellcode durchsuchen

Add ctkFontButton::fontTextFormat to customize font button text

Fix minor bugs (ctkFontButton(QFont...) constructor ignored the font)
 and improve testing framework
Julien Finet vor 13 Jahren
Ursprung
Commit
45dbd6eb9a

+ 3 - 2
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -36,7 +36,7 @@ set(TEST_SOURCES
   ctkFlatProxyModelTest.cpp
   ctkFittedTextBrowserTest1.cpp
   ctkFlowLayoutTest1.cpp
-  ctkFontButtonTest1.cpp
+  ctkFontButtonTest.cpp
   ctkLayoutManagerTest1.cpp
   ctkMaterialPropertyPreviewLabelTest1.cpp
   ctkMaterialPropertyWidgetTest1.cpp
@@ -171,6 +171,7 @@ set(Tests_MOC_CPP)
 QT4_WRAP_CPP(Tests_MOC_CPP ${Tests_MOC_SRCS})
 QT4_GENERATE_MOCS(
   ctkFlatProxyModelTest.cpp
+  ctkFontButtonTest.cpp
   ctkMessageBoxDontShowAgainTest.cpp
   ctkRangeSliderTest.cpp
   )
@@ -226,7 +227,7 @@ SIMPLE_TEST( ctkFlatProxyModelTest )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
 SIMPLE_TEST( ctkFlowLayoutTest1 )
-SIMPLE_TEST( ctkFontButtonTest1 )
+SIMPLE_TEST( ctkFontButtonTest )
 SIMPLE_TEST( ctkLayoutManagerTest1 )
 SIMPLE_TEST( ctkMaterialPropertyPreviewLabelTest1 )
 SIMPLE_TEST( ctkMaterialPropertyWidgetTest1 )

+ 168 - 0
Libs/Widgets/Testing/Cpp/ctkFontButtonTest.cpp

@@ -0,0 +1,168 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QSignalSpy>
+#include <QTimer>
+#include <QVBoxLayout>
+
+// CTK includes
+#include "ctkFontButton.h"
+#include "ctkTest.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+// ----------------------------------------------------------------------------
+class ctkFontButtonTester: public QObject
+{
+  Q_OBJECT
+private slots:
+
+  void testDefaults();
+  void testDefaultFont();
+
+  void testSetFont();
+  void testSetFont_data();
+
+  void testSetFontTextFormat();
+  void testSetFontTextFormat_data();
+
+  void testUpdateText();
+  void testBrowseFont();
+};
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testDefaults()
+{
+  ctkFontButton fontButton;
+  QCOMPARE(fontButton.currentFont(), qApp->font());
+  QCOMPARE(fontButton.fontTextFormat(), QString("fff-sss"));
+}
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testDefaultFont()
+{
+  QWidget parentWidget;
+  parentWidget.setFont(QFont("Helvetica", 6));
+  ctkFontButton fontButton(&parentWidget);
+
+  QCOMPARE(fontButton.currentFont(), qApp->font());
+
+  fontButton.setFont(QFont("Arial", 9));
+  QCOMPARE(fontButton.currentFont(), qApp->font());
+}
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testSetFont()
+{
+  ctkFontButton fontButton;
+  fontButton.setCurrentFont(QFont("Helvetica", 8));
+
+  QSignalSpy spyFontChanged(&fontButton, SIGNAL(currentFontChanged(QFont)));
+
+  QFETCH(QFont, currentFont);
+  fontButton.setCurrentFont(currentFont);
+
+  QFETCH(QFont, expectedCurrentFont);
+  QFETCH(int, expectedFontChangedSignalCount);
+
+  QCOMPARE(fontButton.currentFont(), expectedCurrentFont);
+  QCOMPARE(spyFontChanged.count(), expectedFontChangedSignalCount);
+}
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testSetFont_data()
+{
+  QTest::addColumn<QFont>("currentFont");
+  QTest::addColumn<QFont>("expectedCurrentFont");
+  QTest::addColumn<int>("expectedFontChangedSignalCount");
+
+  QTest::newRow("invalid") << QFont() << QFont() << 1;
+  QTest::newRow("new family") << QFont("Arial", 8) << QFont("Arial", 8) << 1;
+  QTest::newRow("new size") << QFont("Helvetica", 12) << QFont("Helvetica", 12) << 1;
+  QTest::newRow("new weight") << QFont("Helvetica", 8, QFont::Bold) << QFont("Helvetica", 8, QFont::Bold) << 1;
+  QTest::newRow("new family+size+weight") << QFont("Arial", 12, QFont::Bold) << QFont("Arial", 12, QFont::Bold) << 1;
+}
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testSetFontTextFormat()
+{
+  ctkFontButton fontButton;
+  fontButton.setCurrentFont(QFont("Helvetica", 8, QFont::Normal, true));
+
+  QFETCH(QString, fontTextFormat);
+  fontButton.setFontTextFormat(fontTextFormat);
+  QCOMPARE(fontButton.fontTextFormat(), fontTextFormat);
+
+  QFETCH(QString, expectedText);
+  QCOMPARE(fontButton.text(), expectedText);
+}
+
+// ----------------------------------------------------------------------------
+void ctkFontButtonTester::testSetFontTextFormat_data()
+{
+  QTest::addColumn<QString>("fontTextFormat");
+  QTest::addColumn<QString>("expectedText");
+
+  QTest::newRow("null") << QString() << QString("");
+  QTest::newRow("empty") << QString("") << QString("");
+  QTest::newRow("fff") << QString("fff") << QString("Helvetica");
+  QTest::newRow("ffffff") << QString("ffffff") << QString("HelveticaHelvetica");
+  QTest::newRow("fff-ss-sss-ww-www-bb-bbb-ii-iii-uu-uuu-biu")
+    << QString("fff-ss-sss-ww-www-bb-bbb-ii-iii-uu-uuu-biu")
+    << QString("Helvetica-8-8pt-50-Normal---i-italic----i-");
+  QTest::newRow("biuu") << QString("biuu") << QString("-i-u");
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButtonTester::testUpdateText()
+{
+  ctkFontButton fontButton(QFont("Helvetica", 8));
+  QCOMPARE(fontButton.text(), QString("Helvetica-8pt"));
+
+  fontButton.setText("Custom text");
+  QCOMPARE(fontButton.text(), QString("Custom text"));
+
+  fontButton.setCurrentFont(QFont("Helvetica", 9));
+  QCOMPARE(fontButton.text(), QString("Helvetica-9pt"));
+
+  fontButton.setText("Custom text");
+  QCOMPARE(fontButton.text(), QString("Custom text"));
+
+  fontButton.setFontTextFormat("fff-ss");
+  QCOMPARE(fontButton.text(), QString("Helvetica-9"));
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButtonTester::testBrowseFont()
+{
+  ctkFontButton fontButton;
+  QTimer::singleShot(200, qApp, SLOT(quit()));
+  QTimer::singleShot(100, &fontButton, SLOT(browseFont()));
+  fontButton.show();
+  qApp->exec();
+}
+
+// ----------------------------------------------------------------------------
+CTK_TEST_MAIN(ctkFontButtonTest)
+#include "moc_ctkFontButtonTest.cpp"

+ 0 - 78
Libs/Widgets/Testing/Cpp/ctkFontButtonTest1.cpp

@@ -1,78 +0,0 @@
-/*=========================================================================
-
-  Library:   CTK
-
-  Copyright (c) Kitware Inc.
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-      http://www.apache.org/licenses/LICENSE-2.0.txt
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-
-=========================================================================*/
-
-// Qt includes
-#include <QApplication>
-#include <QSignalSpy>
-#include <QTimer>
-#include <QVBoxLayout>
-
-// CTK includes
-#include "ctkFontButton.h"
-
-// STD includes
-#include <cstdlib>
-#include <iostream>
-
-//-----------------------------------------------------------------------------
-int ctkFontButtonTest1(int argc, char * argv [] )
-{
-  QApplication app(argc, argv);
-  
-  QFont customFont(QFont("Helvetica", 8));
-  QFont customFont2(QFont("Arial", 12));
-  
-  QWidget topLevel;
-  ctkFontButton button;
-  ctkFontButton button2(customFont);
-  
-  QVBoxLayout* layout = new QVBoxLayout(&topLevel);
-  layout->addWidget(&button);
-  layout->addWidget(&button2);
-  
-  button.setCurrentFont(customFont);
-  if (button.currentFont() != customFont)
-    {
-    std::cerr << "ctkFontButton::setFont() failed." << std::endl;
-    return EXIT_FAILURE;
-    }
-
-  QSignalSpy spyFontChanged(&button, SIGNAL(currentFontChanged(QFont)));
-
-  button.setCurrentFont(customFont2);
-  if ( button.currentFont() != customFont2 ||
-       spyFontChanged.count() != 1)
-    {
-    std::cerr<< "ctkFontButton::setCurrentFont failed" << button.currentFont().toString().toStdString() << std::endl;
-    return EXIT_FAILURE;
-    }
-
-  topLevel.show();
-
-  if (argc < 2 || QString(argv[1]) != "-I" )
-    {
-    QTimer::singleShot(200, &app, SLOT(quit()));
-    }
-
-  QTimer::singleShot(100, &button, SLOT(browseFont()));
-
-  return app.exec();
-}
-

+ 84 - 7
Libs/Widgets/ctkFontButton.cpp

@@ -22,6 +22,8 @@
 #include <QApplication>
 #include <QDebug>
 #include <QFontDialog>
+#include <QMetaEnum>
+#include <QMetaObject>
 
 // CTK includes
 #include "ctkFontButton.h"
@@ -37,8 +39,11 @@ protected:
 public:
   ctkFontButtonPrivate(ctkFontButton& object);
   void init();
+  void updateText();
+  QString fullNameWeight()const;
 
   QFont Font;
+  QString FontTextFormat;
 };
 
 //-----------------------------------------------------------------------------
@@ -46,16 +51,72 @@ ctkFontButtonPrivate::ctkFontButtonPrivate(ctkFontButton& object)
   :q_ptr(&object)
 {
   this->Font = qApp->font();
+  this->FontTextFormat = "fff-sss";
 }
 
 //-----------------------------------------------------------------------------
 void ctkFontButtonPrivate::init()
 {
   Q_Q(ctkFontButton);
-  q->setFont(this->Font);
-  q->setText(this->Font.family());
-  
   QObject::connect(q, SIGNAL(clicked()), q, SLOT(browseFont()));
+
+  this->updateText();
+}
+
+
+//-----------------------------------------------------------------------------
+QString ctkFontButtonPrivate::fullNameWeight()const
+{
+  /** \todo: QFont:Weight is not yet in the meta object system
+  QMetaObject meta = QFont::staticMetaObject;
+  for (int i = 0; i < meta.enumeratorCount(); ++i)
+    {
+    QMetaEnum metaEnum = meta.enumerator(i);
+    if (metaEnum.name() == "Weight")
+      {
+      return QString(metaEnum.valueToKey(this->Font.weight()));
+      }
+    }
+  */
+  switch (this->Font.weight())
+    {
+    case QFont::Light:
+      return QString("Light");
+    case QFont::Normal:
+      return QString("Normal");
+    case QFont::DemiBold:
+      return QString("DemiBold");
+    case QFont::Bold:
+      return QString("Bold");
+    case QFont::Black:
+      return QString("Black");
+    default:
+      return QString();
+    }
+  return QString();
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButtonPrivate::updateText()
+{
+  Q_Q(ctkFontButton);
+  QString text = this->FontTextFormat;
+  text.replace("fff", this->Font.family());
+  text.replace("sss", QString("%1pt").arg(this->Font.pointSize()));
+  text.replace("ss", QString("%1").arg(this->Font.pointSize()));
+  text.replace("www", this->fullNameWeight());
+  text.replace("ww", QString("%1").arg(this->Font.weight()));
+  text.replace("biu", QString("%1%2%3")
+    .arg(this->Font.bold() ? 'b' : '-')
+    .arg(this->Font.italic() ? 'i' : '-')
+    .arg(this->Font.underline() ? 'u' : '-'));
+  text.replace("bbb", this->Font.bold() ? "bold" : "");
+  text.replace("bb", this->Font.bold() ? "b" : "");
+  text.replace("iii", this->Font.italic() ? "italic" : "");
+  text.replace("ii", this->Font.italic() ? "i" : "");
+  text.replace("uuu", this->Font.underline() ? "underline" : "");
+  text.replace("uu", this->Font.underline() ? "u" : "");
+  q->setText(text);
 }
 
 //-----------------------------------------------------------------------------
@@ -66,16 +127,16 @@ ctkFontButton::ctkFontButton(QWidget * parentWidget)
   Q_D(ctkFontButton);
   d->init();
 }
-    
+
 //-----------------------------------------------------------------------------
-ctkFontButton::ctkFontButton(const QFont& font, 
+ctkFontButton::ctkFontButton(const QFont& font,
                              QWidget * parentWidget)
   : QPushButton(parentWidget)
   , d_ptr(new ctkFontButtonPrivate(*this))
 {
   Q_D(ctkFontButton);
   d->init();
-  this->setFont(font);
+  this->setCurrentFont(font);
 }
 
 //-----------------------------------------------------------------------------
@@ -96,7 +157,7 @@ void ctkFontButton::setCurrentFont(const QFont& newFont)
   d->Font = newFont;
 
   this->setFont(newFont);
-  this->setText(newFont.family());
+  d->updateText();
 
   emit currentFontChanged(newFont);
 }
@@ -119,3 +180,19 @@ void ctkFontButton::browseFont()
     this->setCurrentFont(newFont);
     }
 }
+
+//-----------------------------------------------------------------------------
+void ctkFontButton::setFontTextFormat(const QString& fontTextFormat)
+{
+  Q_D(ctkFontButton);
+  d->FontTextFormat = fontTextFormat;
+  d->updateText();
+}
+
+//-----------------------------------------------------------------------------
+QString ctkFontButton::fontTextFormat()const
+{
+  Q_D(const ctkFontButton);
+  return d->FontTextFormat;
+}
+

+ 33 - 1
Libs/Widgets/ctkFontButton.h

@@ -33,15 +33,39 @@ class ctkFontButtonPrivate;
 class CTK_WIDGETS_EXPORT ctkFontButton: public QPushButton
 {
   Q_OBJECT
+
+  /// Application QFont by default
   Q_PROPERTY(QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY currentFontChanged USER true)
 
+  /// This property holds the format of the text of the pushbutton. These
+  /// expressions may be used in place of the current font:
+  /// * fff family
+  /// * sss pointSize with suffix "pt". e.g. "9pt"
+  /// * ss pointSize with no suffix. e.g. "9"
+  /// * www full name weight e.g. Bold (if font is bold)
+  /// * ww weight. e.g. 50 (if font is normal)
+  /// * biu 3 character string where the first character is 'b' if bold or - otherwise,
+  /// the second character is 'i' if the font italic or - otherwise and the third
+  /// characis is 'u' if the font is underline or '-' otherwise. e.g. "bi-" if the font
+  /// is bold and italic but not underline
+  /// * bbb 'bold' if bold, nothing otherwise
+  /// * bb 'b' if bold, nothing otherwise
+  /// * iii 'italic' if italic, nothing otherwise
+  /// * ii 'i' if italic, nothing otherwise
+  /// * uuu 'underline' if underline, nothing otherwise
+  /// * uu 'u' if underline, nothing otherwise
+  /// Note that the listing order matches the replacement order.
+  /// fff-sss by default.
+  Q_PROPERTY(QString fontTextFormat READ fontTextFormat WRITE setFontTextFormat)
+
 public:
   /// Constructor
   /// Creates a default ctkFontButton initialized with QApplication font
   ctkFontButton(QWidget * parent = 0);
+
   /// Constructor
   /// Creates a ctkFontButton with a given font
-  ctkFontButton(const QFont& font, QWidget * parent = 0);
+  ctkFontButton(const QFont& currentFont, QWidget * parent = 0);
   
   /// Destructor
   virtual ~ctkFontButton();
@@ -50,6 +74,14 @@ public:
   void setCurrentFont(const QFont& newFont);
   QFont currentFont()const;
 
+  /// Set the font text format
+  /// \sa fontTextFormat, fontTextFormat()
+  void setFontTextFormat(const QString& fontTextFormat);
+
+  /// Get the font text format
+  /// \sa fontTextFormat, setFontTextFormat()
+  QString fontTextFormat()const;
+
 public Q_SLOTS:
   /// browse() opens a pop up where the user can select a new font.
   /// browse() is automatically called when the button is clicked.