ctkFontButton.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QFontDialog>
  18. #include <QMetaEnum>
  19. #include <QMetaObject>
  20. // CTK includes
  21. #include "ctkFontButton.h"
  22. //-----------------------------------------------------------------------------
  23. class ctkFontButtonPrivate
  24. {
  25. Q_DECLARE_PUBLIC(ctkFontButton);
  26. protected:
  27. ctkFontButton* const q_ptr;
  28. public:
  29. ctkFontButtonPrivate(ctkFontButton& object);
  30. void init();
  31. void updateText();
  32. QString fullNameWeight()const;
  33. QFont Font;
  34. QString FontTextFormat;
  35. };
  36. //-----------------------------------------------------------------------------
  37. ctkFontButtonPrivate::ctkFontButtonPrivate(ctkFontButton& object)
  38. :q_ptr(&object)
  39. {
  40. this->Font = qApp->font();
  41. this->FontTextFormat = "fff-sss";
  42. }
  43. //-----------------------------------------------------------------------------
  44. void ctkFontButtonPrivate::init()
  45. {
  46. Q_Q(ctkFontButton);
  47. QObject::connect(q, SIGNAL(clicked()), q, SLOT(browseFont()));
  48. this->updateText();
  49. }
  50. //-----------------------------------------------------------------------------
  51. QString ctkFontButtonPrivate::fullNameWeight()const
  52. {
  53. /** \todo: QFont:Weight is not yet in the meta object system
  54. QMetaObject meta = QFont::staticMetaObject;
  55. for (int i = 0; i < meta.enumeratorCount(); ++i)
  56. {
  57. QMetaEnum metaEnum = meta.enumerator(i);
  58. if (metaEnum.name() == "Weight")
  59. {
  60. return QString(metaEnum.valueToKey(this->Font.weight()));
  61. }
  62. }
  63. */
  64. switch (this->Font.weight())
  65. {
  66. case QFont::Light:
  67. return QString("Light");
  68. case QFont::Normal:
  69. return QString("Normal");
  70. case QFont::DemiBold:
  71. return QString("DemiBold");
  72. case QFont::Bold:
  73. return QString("Bold");
  74. case QFont::Black:
  75. return QString("Black");
  76. default:
  77. return QString();
  78. }
  79. return QString();
  80. }
  81. //-----------------------------------------------------------------------------
  82. void ctkFontButtonPrivate::updateText()
  83. {
  84. Q_Q(ctkFontButton);
  85. QString text = this->FontTextFormat;
  86. text.replace("fff", this->Font.family());
  87. text.replace("sss", QString("%1pt").arg(this->Font.pointSize()));
  88. text.replace("ss", QString("%1").arg(this->Font.pointSize()));
  89. text.replace("www", this->fullNameWeight());
  90. text.replace("ww", QString("%1").arg(this->Font.weight()));
  91. text.replace("biu", QString("%1%2%3")
  92. .arg(this->Font.bold() ? 'b' : '-')
  93. .arg(this->Font.italic() ? 'i' : '-')
  94. .arg(this->Font.underline() ? 'u' : '-'));
  95. text.replace("bbb", this->Font.bold() ? "bold" : "");
  96. text.replace("bb", this->Font.bold() ? "b" : "");
  97. text.replace("iii", this->Font.italic() ? "italic" : "");
  98. text.replace("ii", this->Font.italic() ? "i" : "");
  99. text.replace("uuu", this->Font.underline() ? "underline" : "");
  100. text.replace("uu", this->Font.underline() ? "u" : "");
  101. q->setText(text);
  102. }
  103. //-----------------------------------------------------------------------------
  104. ctkFontButton::ctkFontButton(QWidget * parentWidget)
  105. : QPushButton(parentWidget)
  106. , d_ptr(new ctkFontButtonPrivate(*this))
  107. {
  108. Q_D(ctkFontButton);
  109. d->init();
  110. }
  111. //-----------------------------------------------------------------------------
  112. ctkFontButton::ctkFontButton(const QFont& font,
  113. QWidget * parentWidget)
  114. : QPushButton(parentWidget)
  115. , d_ptr(new ctkFontButtonPrivate(*this))
  116. {
  117. Q_D(ctkFontButton);
  118. d->init();
  119. this->setCurrentFont(font);
  120. }
  121. //-----------------------------------------------------------------------------
  122. ctkFontButton::~ctkFontButton()
  123. {
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkFontButton::setCurrentFont(const QFont& newFont)
  127. {
  128. Q_D(ctkFontButton);
  129. if (d->Font == newFont)
  130. {
  131. return;
  132. }
  133. d->Font = newFont;
  134. this->setFont(newFont);
  135. d->updateText();
  136. emit currentFontChanged(newFont);
  137. }
  138. //-----------------------------------------------------------------------------
  139. QFont ctkFontButton::currentFont()const
  140. {
  141. Q_D(const ctkFontButton);
  142. return d->Font;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void ctkFontButton::browseFont()
  146. {
  147. Q_D(ctkFontButton);
  148. bool ok = false;
  149. QFont newFont = QFontDialog::getFont(&ok, d->Font, this);
  150. if (ok)
  151. {
  152. this->setCurrentFont(newFont);
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. void ctkFontButton::setFontTextFormat(const QString& fontTextFormat)
  157. {
  158. Q_D(ctkFontButton);
  159. d->FontTextFormat = fontTextFormat;
  160. d->updateText();
  161. }
  162. //-----------------------------------------------------------------------------
  163. QString ctkFontButton::fontTextFormat()const
  164. {
  165. Q_D(const ctkFontButton);
  166. return d->FontTextFormat;
  167. }