CmQtDynamicTabBar.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "CmQtDynamicTabBar.h"
  2. #include "CmException.h"
  3. #include <QtWidgets/QPushButton>
  4. #include <QtWidgets/QHBoxLayout>
  5. namespace CamelotEditor
  6. {
  7. QtDynamicTabBar::QtDynamicTabBar(QWidget* parent)
  8. :QWidget(parent), mHLayout(nullptr)
  9. {
  10. setupUi();
  11. }
  12. void QtDynamicTabBar::addTab(const QString& name)
  13. {
  14. QPushButton* newBtn = new QPushButton(this);
  15. newBtn->setMaximumWidth(100);
  16. newBtn->setText(name);
  17. mTabs.push_back(newBtn);
  18. updateTabs();
  19. }
  20. void QtDynamicTabBar::insertTab(UINT32 idx, const QString& name)
  21. {
  22. if(idx >= (UINT32)mTabs.size())
  23. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) +". Valid range: 0 .. " + toString((UINT32)mTabs.size()));
  24. QPushButton* newBtn = new QPushButton(this);
  25. newBtn->setMaximumWidth(100);
  26. newBtn->setText(name);
  27. mTabs.insert(mTabs.begin() + idx, newBtn);
  28. updateTabs();
  29. }
  30. void QtDynamicTabBar::removeTab(UINT32 idx)
  31. {
  32. if(idx >= (UINT32)mTabs.size())
  33. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) +". Valid range: 0 .. " + toString((UINT32)mTabs.size()));
  34. mTabs.erase(mTabs.begin() + idx);
  35. updateTabs();
  36. }
  37. void QtDynamicTabBar::setupUi()
  38. {
  39. mHLayout = new QHBoxLayout(this);
  40. mHLayout->setMargin(0);
  41. setLayout(mHLayout);
  42. }
  43. void QtDynamicTabBar::updateTabs()
  44. {
  45. QLayoutItem* item;
  46. while((item = mHLayout->takeAt(0)) != nullptr)
  47. mHLayout->removeItem(item);
  48. UINT32 idx = 0;
  49. for(auto iter = mTabs.begin(); iter != mTabs.end(); ++iter)
  50. {
  51. (*iter)->disconnect();
  52. /*connect(*iter, &QPushButton::clicked, std::bind(this, &QtDynamicTabBar::tabSelected, idx));*/
  53. mHLayout->addWidget(*iter);
  54. idx++;
  55. }
  56. mHLayout->addStretch(2);
  57. }
  58. void QtDynamicTabBar::tabSelected(UINT32 idx)
  59. {
  60. onTabSelected(idx);
  61. }
  62. }