瀏覽代碼

Add ctkDirectoryButton::text

Optional text to display on directory button. If no text, display the
absolute directory path.
Julien Finet 13 年之前
父節點
當前提交
e8366125db

+ 14 - 0
Libs/Widgets/Testing/Cpp/ctkDirectoryButtonTest1.cpp

@@ -60,6 +60,20 @@ int ctkDirectoryButtonTest1(int argc, char * argv [] )
     return EXIT_FAILURE;
     }
 
+  button.setText("Click here");
+  if (button.text() != "Click here")
+    {
+    std::cerr << "ctkDirectoryButton::setText() failed." << std::endl;
+    return EXIT_FAILURE;
+    }
+  // Restore text to directory path
+  button.setText(QString());
+  if (button.text() != QString())
+    {
+    std::cerr << "ctkDirectoryButton::setText() failed." << std::endl;
+    return EXIT_FAILURE;
+    }
+
   if (button.icon().pixmap(20).toImage() !=
       defaultIcon.pixmap(20).toImage())
     {

+ 33 - 5
Libs/Widgets/ctkDirectoryButton.cpp

@@ -38,10 +38,12 @@ protected:
 public:
   ctkDirectoryButtonPrivate(ctkDirectoryButton& object);
   void init();
+  void updateDisplayText();
 
   QDir         Directory;
   QPushButton* PushButton;
   QString      DialogCaption;
+  QString      DisplayText;
 #ifdef USE_QFILEDIALOG_OPTIONS
   QFileDialog::Options DialogOptions;
 #else
@@ -77,6 +79,19 @@ void ctkDirectoryButtonPrivate::init()
 }
 
 //-----------------------------------------------------------------------------
+void ctkDirectoryButtonPrivate::updateDisplayText()
+{
+  Q_Q(ctkDirectoryButton);
+  QString buttonText = this->DisplayText;
+  if (buttonText.isNull())
+    {
+    buttonText = this->DisplayAbsolutePath ?
+      this->Directory.absolutePath() : this->Directory.path();
+    }
+  this->PushButton->setText(buttonText);
+}
+
+//-----------------------------------------------------------------------------
 ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
   :QWidget(parentWidget)
   , d_ptr(new ctkDirectoryButtonPrivate(*this))
@@ -86,9 +101,9 @@ ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
   d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
   d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
 }
-    
+
 //-----------------------------------------------------------------------------
-ctkDirectoryButton::ctkDirectoryButton(const QString& dir, 
+ctkDirectoryButton::ctkDirectoryButton(const QString& dir,
                                        QWidget * parentWidget)
   :QWidget(parentWidget)
   , d_ptr(new ctkDirectoryButtonPrivate(*this))
@@ -133,8 +148,7 @@ void ctkDirectoryButton::setDirectory(const QString& dir)
     }
 
   d->Directory = newDirectory;
-
-  d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
+  d->updateDisplayText();
 
   emit directorySelected(d->DisplayAbsolutePath ?
                          newDirectory.absolutePath() :
@@ -165,13 +179,27 @@ const QString& ctkDirectoryButton::caption()const
 }
 
 //-----------------------------------------------------------------------------
+void ctkDirectoryButton::setText(const QString& text)
+{
+  Q_D(ctkDirectoryButton);
+  d->DisplayText = text;
+  d->updateDisplayText();
+}
+
+//-----------------------------------------------------------------------------
+const QString& ctkDirectoryButton::text()const
+{
+  Q_D(const ctkDirectoryButton);
+  return d->DisplayText;
+}
+
+//-----------------------------------------------------------------------------
 void ctkDirectoryButton::setIcon(const QIcon& newIcon)
 {
   Q_D(const ctkDirectoryButton);
   return d->PushButton->setIcon(newIcon);
 }
 
-
 //-----------------------------------------------------------------------------
 QIcon ctkDirectoryButton::icon()const
 {

+ 28 - 7
Libs/Widgets/ctkDirectoryButton.h

@@ -44,7 +44,14 @@ class CTK_WIDGETS_EXPORT ctkDirectoryButton: public QWidget
 {
   Q_OBJECT
   Q_PROPERTY(QString directory READ directory WRITE setDirectory NOTIFY directoryChanged USER true)
+  /// This property holds the title of the file dialog used to select a new directory
+  /// If caption is not set, internally use QWidget::tooltip()
   Q_PROPERTY(QString caption READ caption WRITE setCaption)
+  /// This property holds the text to display on the button. If null (by
+  /// default), the current directory path is displayed instead.
+  Q_PROPERTY(QString text READ text WRITE setText)
+  /// This property holds the icon displayed on the button. QStyle::SP_DirIcon
+  /// by default.
   Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
   /// Qt versions prior to 4.7.0 didn't expose QFileDialog::Options in the
   /// public API. We need to create a custom property that will be used when
@@ -80,22 +87,31 @@ public:
   /// Creates a ctkDirectoryButton that points to the given directory path
   ctkDirectoryButton(const QString& directory, QWidget * parent = 0);
   ctkDirectoryButton(const QIcon& icon, const QString& directory, QWidget * parent = 0);
-  
+
   /// Destructor
   virtual ~ctkDirectoryButton();
 
   /// Set/get the current directory
   /// If path is empty, the program's working directory, ("."), is used.
-  /// By default, \a directory is the current working directory. 
+  /// By default, \a directory is the current working directory.
   void setDirectory(const QString& path);
   QString directory()const;
 
-  ///
-  /// The title of the file dialog used to select a new directory
-  /// If caption is not set, internally use QWidget::tooltip()
+  /// Set the caption of the directory dialog
+  /// \sa caption
   void setCaption(const QString& caption);
+  /// Get the caption of the directory dialog
+  /// \sa setCaption
   const QString& caption()const;
 
+  /// Set the text of the button. If null (not just empty), the directory path
+  /// is used as text. This doesn't set the "directory", just the displayed text.
+  /// \sa setDirectory
+  void setText(const QString& text);
+  /// Return the text of the button if any. Doesn't return the directory path.
+  /// \sa directory
+  const QString& text()const;
+
   ///
   /// The icon of the button
   /// By default use QStyle::SP_DirIcon
@@ -119,11 +135,16 @@ public Q_SLOTS:
 
 Q_SIGNALS:
   /// directoryChanged is emitted when the current directory changes.
-  /// Programatically or by the user via the file dialog that pop up when 
+  /// Programatically or by the user via the file dialog that pop up when
   /// clicking on the button.
+  /// \sa directorySelected
   void directoryChanged(const QString&);
+
   /// directorySelected() is emitted anytime the current directory is set
-  /// (even if the new directory is the same than the current value)
+  /// (even if the new directory is the same than the current value). This is
+  /// particularly useful when the browse dialog is accepted without changing
+  /// the current directory.
+  /// \sa directoryChanged
   void directorySelected(const QString&);
 protected:
   QScopedPointer<ctkDirectoryButtonPrivate> d_ptr;