qtmaterialtabs.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "qtmaterialtabs.h"
  2. #include "qtmaterialtabs_p.h"
  3. #include <QtWidgets/QHBoxLayout>
  4. #include "qtmaterialtabs_internal.h"
  5. #include "lib/qtmaterialstyle.h"
  6. /*!
  7. * \QtMaterialTabsPrivate
  8. * \internal
  9. */
  10. QtMaterialTabsPrivate::QtMaterialTabsPrivate(QtMaterialTabs *q)
  11. : q_ptr(q)
  12. {
  13. }
  14. QtMaterialTabsPrivate::~QtMaterialTabsPrivate()
  15. {
  16. }
  17. void QtMaterialTabsPrivate::QtMaterialTabsPrivate::init()
  18. {
  19. Q_Q(QtMaterialTabs);
  20. inkBar = new QtMaterialTabsInkBar(q);
  21. tabLayout = new QHBoxLayout;
  22. rippleStyle = Material::CenteredRipple;
  23. tab = -1;
  24. showHalo = true;
  25. useThemeColors = true;
  26. q->setLayout(tabLayout);
  27. q->setStyle(&QtMaterialStyle::instance());
  28. tabLayout->setSpacing(0);
  29. tabLayout->setMargin(0);
  30. }
  31. /*!
  32. * \QtMaterialTabs
  33. */
  34. QtMaterialTabs::QtMaterialTabs(QWidget *parent)
  35. : QWidget(parent),
  36. d_ptr(new QtMaterialTabsPrivate(this))
  37. {
  38. d_func()->init();
  39. }
  40. QtMaterialTabs::~QtMaterialTabs()
  41. {
  42. }
  43. void QtMaterialTabs::setUseThemeColors(bool value)
  44. {
  45. Q_D(QtMaterialTabs);
  46. d->useThemeColors = value;
  47. }
  48. bool QtMaterialTabs::useThemeColors() const
  49. {
  50. Q_D(const QtMaterialTabs);
  51. return d->useThemeColors;
  52. }
  53. void QtMaterialTabs::setHaloVisible(bool value)
  54. {
  55. Q_D(QtMaterialTabs);
  56. d->showHalo = value;
  57. updateTabs();
  58. }
  59. bool QtMaterialTabs::isHaloVisible() const
  60. {
  61. Q_D(const QtMaterialTabs);
  62. return d->showHalo;
  63. }
  64. void QtMaterialTabs::setRippleStyle(Material::RippleStyle style)
  65. {
  66. Q_D(QtMaterialTabs);
  67. d->rippleStyle = style;
  68. updateTabs();
  69. }
  70. Material::RippleStyle QtMaterialTabs::rippleStyle() const
  71. {
  72. Q_D(const QtMaterialTabs);
  73. return d->rippleStyle;
  74. }
  75. void QtMaterialTabs::setInkColor(const QColor &color)
  76. {
  77. Q_D(QtMaterialTabs);
  78. d->inkColor = color;
  79. MATERIAL_DISABLE_THEME_COLORS
  80. d->inkBar->update();
  81. update();
  82. }
  83. QColor QtMaterialTabs::inkColor() const
  84. {
  85. Q_D(const QtMaterialTabs);
  86. if (d->useThemeColors || !d->inkColor.isValid()) {
  87. return QtMaterialStyle::instance().themeColor("accent1");
  88. } else {
  89. return d->inkColor;
  90. }
  91. }
  92. void QtMaterialTabs::setBackgroundColor(const QColor &color)
  93. {
  94. Q_D(QtMaterialTabs);
  95. d->backgroundColor = color;
  96. MATERIAL_DISABLE_THEME_COLORS
  97. updateTabs();
  98. update();
  99. }
  100. QColor QtMaterialTabs::backgroundColor() const
  101. {
  102. Q_D(const QtMaterialTabs);
  103. if (d->useThemeColors || !d->backgroundColor.isValid()) {
  104. return QtMaterialStyle::instance().themeColor("primary1");
  105. } else {
  106. return d->backgroundColor;
  107. }
  108. }
  109. void QtMaterialTabs::setTextColor(const QColor &color)
  110. {
  111. Q_D(QtMaterialTabs);
  112. d->textColor = color;
  113. MATERIAL_DISABLE_THEME_COLORS
  114. updateTabs();
  115. update();
  116. }
  117. QColor QtMaterialTabs::textColor() const
  118. {
  119. Q_D(const QtMaterialTabs);
  120. if (d->useThemeColors || !d->textColor.isValid()) {
  121. return QtMaterialStyle::instance().themeColor("canvas");
  122. } else {
  123. return d->textColor;
  124. }
  125. }
  126. void QtMaterialTabs::setCurrentTab(QtMaterialTab *tab)
  127. {
  128. Q_D(QtMaterialTabs);
  129. setCurrentTab(d->tabLayout->indexOf(tab));
  130. }
  131. void QtMaterialTabs::setCurrentTab(int index)
  132. {
  133. Q_D(QtMaterialTabs);
  134. setTabActive(d->tab, false);
  135. d->tab = index;
  136. setTabActive(index, true);
  137. d->inkBar->animate();
  138. emit currentChanged(index);
  139. }
  140. void QtMaterialTabs::addTab(const QString &text, const QIcon &icon)
  141. {
  142. Q_D(QtMaterialTabs);
  143. QtMaterialTab *tab = new QtMaterialTab(this);
  144. tab->setText(text);
  145. tab->setHaloVisible(isHaloVisible());
  146. tab->setRippleStyle(rippleStyle());
  147. if (!icon.isNull()) {
  148. tab->setIcon(icon);
  149. tab->setIconSize(QSize(22, 22));
  150. }
  151. d->tabLayout->addWidget(tab);
  152. if (-1 == d->tab) {
  153. d->tab = 0;
  154. d->inkBar->refreshGeometry();
  155. d->inkBar->raise();
  156. tab->setActive(true);
  157. }
  158. }
  159. int QtMaterialTabs::currentIndex() const
  160. {
  161. Q_D(const QtMaterialTabs);
  162. return d->tab;
  163. }
  164. void QtMaterialTabs::setTabActive(int index, bool active)
  165. {
  166. Q_D(QtMaterialTabs);
  167. QtMaterialTab *tab;
  168. if (index > -1) {
  169. tab = static_cast<QtMaterialTab *>(d->tabLayout->itemAt(index)->widget());
  170. if (tab) {
  171. tab->setActive(active);
  172. }
  173. }
  174. }
  175. void QtMaterialTabs::updateTabs()
  176. {
  177. Q_D(QtMaterialTabs);
  178. QtMaterialTab *tab;
  179. for (int i = 0; i < d->tabLayout->count(); ++i) {
  180. QLayoutItem *item = d->tabLayout->itemAt(i);
  181. if ((tab = static_cast<QtMaterialTab *>(item->widget()))) {
  182. tab->setRippleStyle(d->rippleStyle);
  183. tab->setHaloVisible(d->showHalo);
  184. tab->setBackgroundColor(backgroundColor());
  185. tab->setForegroundColor(textColor());
  186. }
  187. }
  188. }