Sfoglia il codice sorgente

Add ctkFontButton a pushbutton to select a QFont

Julien Finet 14 anni fa
parent
commit
0c3bcbd8fc

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -52,6 +52,8 @@ SET(KIT_SRCS
   ctkFileDialog.h
   ctkFittedTextBrowser.cpp
   ctkFittedTextBrowser.h
+  ctkFontButton.cpp
+  ctkFontButton.h
   ctkIconEnginePlugin.cpp
   ctkIconEnginePlugin.h
   ctkMaterialPropertyPreviewLabel.cpp
@@ -138,6 +140,7 @@ SET(KIT_MOC_SRCS
   ctkDynamicSpacer.h
   ctkFileDialog.h
   ctkFittedTextBrowser.h
+  ctkFontButton.h
   ctkIconEnginePlugin.h
   ctkMaterialPropertyPreviewLabel.h
   ctkMaterialPropertyWidget.h

+ 3 - 0
Libs/Widgets/Plugins/CMakeLists.txt

@@ -39,6 +39,8 @@ SET(PLUGIN_SRCS
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.cpp
   ctkFittedTextBrowserPlugin.h
+  ctkFontButtonPlugin.cpp
+  ctkFontButtonPlugin.h
   ctkMaterialPropertyPreviewLabelPlugin.cpp
   ctkMaterialPropertyPreviewLabelPlugin.h
   ctkMaterialPropertyWidgetPlugin.cpp
@@ -84,6 +86,7 @@ SET(PLUGIN_MOC_SRCS
   ctkDynamicSpacerPlugin.h
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.h
+  ctkFontButtonPlugin.h
   ctkMaterialPropertyWidgetPlugin.h
   ctkMaterialPropertyPreviewLabelPlugin.h
   ctkMatrixWidgetPlugin.h

+ 5 - 5
Libs/Widgets/Plugins/ctkDirectoryButtonPlugin.cpp

@@ -23,17 +23,17 @@
 #include "ctkDirectoryButton.h"
 
 //-----------------------------------------------------------------------------
-ctkDirectoryButtonPlugin::ctkDirectoryButtonPlugin(QObject *_parent)
-        : QObject(_parent)
+ctkDirectoryButtonPlugin::ctkDirectoryButtonPlugin(QObject* pluginParent)
+  : QObject(pluginParent)
 {
 
 }
 
 //-----------------------------------------------------------------------------
-QWidget *ctkDirectoryButtonPlugin::createWidget(QWidget *_parent)
+QWidget *ctkDirectoryButtonPlugin::createWidget(QWidget* widgetParent)
 {
-  ctkDirectoryButton* _widget = new ctkDirectoryButton(_parent);
-  return _widget;
+  ctkDirectoryButton* newWidget = new ctkDirectoryButton(widgetParent);
+  return newWidget;
 }
 
 //-----------------------------------------------------------------------------

+ 3 - 3
Libs/Widgets/Plugins/ctkDirectoryButtonPlugin.h

@@ -24,9 +24,9 @@
 // CTK includes
 #include "ctkWidgetsAbstractPlugin.h"
 
-class CTK_WIDGETS_PLUGINS_EXPORT ctkDirectoryButtonPlugin :
-  public QObject,
-  public ctkWidgetsAbstractPlugin
+class CTK_WIDGETS_PLUGINS_EXPORT ctkDirectoryButtonPlugin
+  : public QObject
+  , public ctkWidgetsAbstractPlugin
 {
   Q_OBJECT
 

+ 69 - 0
Libs/Widgets/Plugins/ctkFontButtonPlugin.cpp

@@ -0,0 +1,69 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkFontButtonPlugin.h"
+#include "ctkFontButton.h"
+
+//-----------------------------------------------------------------------------
+ctkFontButtonPlugin::ctkFontButtonPlugin(QObject* pluginParent)
+  : QObject(pluginParent)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+QWidget *ctkFontButtonPlugin::createWidget(QWidget* widgetParent)
+{
+  ctkFontButton* newWidget = new ctkFontButton(widgetParent);
+  return newWidget;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkFontButtonPlugin::domXml() const
+{
+  return "<widget class=\"ctkFontButton\" \
+          name=\"FontButton\">\n"
+          "</widget>\n";
+}
+
+// --------------------------------------------------------------------------
+QIcon ctkFontButtonPlugin::icon() const
+{
+  return QIcon(":/Icons/pushbutton.png");
+}
+
+//-----------------------------------------------------------------------------
+QString ctkFontButtonPlugin::includeFile() const
+{
+  return "ctkFontButton.h";
+}
+
+//-----------------------------------------------------------------------------
+bool ctkFontButtonPlugin::isContainer() const
+{
+  return false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkFontButtonPlugin::name() const
+{
+  return "ctkFontButton";
+}

+ 44 - 0
Libs/Widgets/Plugins/ctkFontButtonPlugin.h

@@ -0,0 +1,44 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkFontButtonPlugin_h
+#define __ctkFontButtonPlugin_h
+
+// CTK includes
+#include "ctkWidgetsAbstractPlugin.h"
+
+class CTK_WIDGETS_PLUGINS_EXPORT ctkFontButtonPlugin
+  : public QObject
+  , public ctkWidgetsAbstractPlugin
+{
+  Q_OBJECT
+
+public:
+  ctkFontButtonPlugin(QObject *_parent = 0);
+  
+  QWidget *createWidget(QWidget *_parent);
+  QString  domXml() const;
+  QIcon    icon() const;
+  QString  includeFile() const;
+  bool     isContainer() const;
+  QString  name() const;
+};
+
+#endif

+ 2 - 0
Libs/Widgets/Plugins/ctkWidgetsPlugins.h

@@ -38,6 +38,7 @@
 #include "ctkDoubleSliderPlugin.h"
 #include "ctkDynamicSpacerPlugin.h"
 #include "ctkFittedTextBrowserPlugin.h"
+#include "ctkFontButtonPlugin.h"
 #include "ctkMaterialPropertyPreviewLabelPlugin.h"
 #include "ctkMaterialPropertyWidgetPlugin.h"
 #include "ctkMatrixWidgetPlugin.h"
@@ -75,6 +76,7 @@ public:
             << new ctkDoubleSliderPlugin
             << new ctkDynamicSpacerPlugin
             << new ctkFittedTextBrowserPlugin
+            << new ctkFontButtonPlugin
             << new ctkMaterialPropertyPreviewLabelPlugin
             << new ctkMaterialPropertyWidgetPlugin
             << new ctkMatrixWidgetPlugin

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

@@ -22,6 +22,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkDynamicSpacerTest2.cpp
   ctkFileDialogTest1.cpp
   ctkFittedTextBrowserTest1.cpp
+  ctkFontButtonTest1.cpp
   ctkMaterialPropertyPreviewLabelTest1.cpp
   ctkMaterialPropertyWidgetTest1.cpp
   ctkMatrixWidgetTest1.cpp
@@ -96,6 +97,7 @@ SIMPLE_TEST( ctkDynamicSpacerTest1 )
 SIMPLE_TEST( ctkDynamicSpacerTest2 )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
+SIMPLE_TEST( ctkFontButtonTest1 )
 SIMPLE_TEST( ctkMaterialPropertyPreviewLabelTest1 )
 SIMPLE_TEST( ctkMaterialPropertyWidgetTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest1 )

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

@@ -0,0 +1,78 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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(const 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();
+}
+

+ 121 - 0
Libs/Widgets/ctkFontButton.cpp

@@ -0,0 +1,121 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QDebug>
+#include <QFontDialog>
+
+// CTK includes
+#include "ctkFontButton.h"
+
+//-----------------------------------------------------------------------------
+class ctkFontButtonPrivate
+{
+  Q_DECLARE_PUBLIC(ctkFontButton);
+
+protected:
+  ctkFontButton* const q_ptr;
+
+public:
+  ctkFontButtonPrivate(ctkFontButton& object);
+  void init();
+
+  QFont Font;
+};
+
+//-----------------------------------------------------------------------------
+ctkFontButtonPrivate::ctkFontButtonPrivate(ctkFontButton& object)
+  :q_ptr(&object)
+{
+  this->Font = qApp->font();
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButtonPrivate::init()
+{
+  Q_Q(ctkFontButton);
+  q->setFont(this->Font);
+  q->setText(this->Font.family());
+  
+  QObject::connect(q, SIGNAL(clicked()), q, SLOT(browseFont()));
+}
+
+//-----------------------------------------------------------------------------
+ctkFontButton::ctkFontButton(QWidget * parentWidget)
+  : QPushButton(parentWidget)
+  , d_ptr(new ctkFontButtonPrivate(*this))
+{
+  Q_D(ctkFontButton);
+  d->init();
+}
+    
+//-----------------------------------------------------------------------------
+ctkFontButton::ctkFontButton(const QFont& font, 
+                             QWidget * parentWidget)
+  : QPushButton(parentWidget)
+  , d_ptr(new ctkFontButtonPrivate(*this))
+{
+  Q_D(ctkFontButton);
+  d->init();
+  this->setFont(font);
+}
+
+//-----------------------------------------------------------------------------
+ctkFontButton::~ctkFontButton()
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButton::setCurrentFont(const QFont& newFont)
+{
+  Q_D(ctkFontButton);
+
+  if (d->Font == newFont)
+    {
+    return;
+    }
+
+  d->Font = newFont;
+
+  this->setFont(newFont);
+  this->setText(newFont.family());
+
+  emit currentFontChanged(newFont);
+}
+
+//-----------------------------------------------------------------------------
+QFont ctkFontButton::currentFont()const
+{
+  Q_D(const ctkFontButton);
+  return d->Font;
+}
+
+//-----------------------------------------------------------------------------
+void ctkFontButton::browseFont()
+{
+  Q_D(ctkFontButton);
+  bool ok = false;
+  QFont newFont = QFontDialog::getFont(&ok, d->Font, this);
+  if (ok)
+    {
+    this->setCurrentFont(newFont);
+    }
+}

+ 70 - 0
Libs/Widgets/ctkFontButton.h

@@ -0,0 +1,70 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkFontButton_h
+#define __ctkFontButton_h
+
+// Qt includes
+#include <QFont>
+#include <QPushButton>
+
+// CTK includes
+#include "ctkWidgetsExport.h"
+class ctkFontButtonPrivate;
+
+class CTK_WIDGETS_EXPORT ctkFontButton: public QPushButton
+{
+  Q_OBJECT
+  Q_PROPERTY(QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY currentFontChanged USER true)
+
+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);
+  
+  /// Destructor
+  virtual ~ctkFontButton();
+
+  /// Set/get the current font
+  void setCurrentFont(const QFont& newFont);
+  QFont currentFont()const;
+
+public slots:
+  /// browse() opens a pop up where the user can select a new font.
+  /// browse() is automatically called when the button is clicked.
+  void browseFont();
+
+signals:
+  /// Fired anytime the current font changed.
+  /// Programatically or by the user via the file dialog that pop up when 
+  /// clicking on the button.
+  void currentFontChanged(const QFont&);
+protected:
+  QScopedPointer<ctkFontButtonPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkFontButton);
+  Q_DISABLE_COPY(ctkFontButton);
+};
+
+#endif