CmQtDynamicTabBar.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "CmQtDynamicTabBar.h"
  2. #include "CmException.h"
  3. #include <QtWidgets/QPushButton>
  4. #include <QtWidgets/QHBoxLayout>
  5. #include <QtGui/QPainter>
  6. namespace CamelotEditor
  7. {
  8. QtDynamicTabBar::QtDynamicTabBar(QWidget* parent)
  9. :QWidget(parent), mHLayout(nullptr), dbg(false)
  10. {
  11. setupUi();
  12. }
  13. void QtDynamicTabBar::addTab(const QString& name)
  14. {
  15. QPushButton* newBtn = new QPushButton(this);
  16. newBtn->setMaximumWidth(100);
  17. newBtn->setText(name);
  18. mTabs.push_back(newBtn);
  19. updateTabs();
  20. }
  21. void QtDynamicTabBar::insertTab(UINT32 idx, const QString& name)
  22. {
  23. if(idx > (UINT32)mTabs.size())
  24. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) +". Valid range: 0 .. " + toString((UINT32)mTabs.size()));
  25. QPushButton* newBtn = new QPushButton(this);
  26. newBtn->setMaximumWidth(100);
  27. newBtn->setText(name);
  28. mTabs.insert(mTabs.begin() + idx, newBtn);
  29. updateTabs();
  30. }
  31. void QtDynamicTabBar::removeTab(UINT32 idx)
  32. {
  33. if(idx >= (UINT32)mTabs.size())
  34. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) +". Valid range: 0 .. " + toString((UINT32)mTabs.size()));
  35. mTabs.erase(mTabs.begin() + idx);
  36. updateTabs();
  37. }
  38. vector<QPolygon>::type QtDynamicTabBar::getDropLocations()
  39. {
  40. QPoint topLeft = mapToGlobal(QPoint(0, 0));
  41. int curX = topLeft.x();
  42. int endX = topLeft.x() + width();
  43. int top = topLeft.y();
  44. int bottom = topLeft.y() + height();
  45. QRect rect(topLeft.x(), topLeft.y(), width(), height());
  46. vector<QPolygon>::type polygons;
  47. QPolygon polygon(4);
  48. polygon[0] = QPoint(curX, top);
  49. polygon[1] = QPoint(curX, bottom);
  50. for(auto iter = mTabs.begin(); iter != mTabs.end(); ++iter)
  51. {
  52. QPoint tabTopLeft = (*iter)->mapToGlobal(QPoint(0, 0));
  53. curX = tabTopLeft.x();
  54. polygon[2] = QPoint(curX, bottom);
  55. polygon[3] = QPoint(curX, top);
  56. polygons.push_back(polygon);
  57. polygon = QPolygon(4);
  58. curX = tabTopLeft.x() + (*iter)->width();
  59. polygon[0] = QPoint(curX, top);
  60. polygon[1] = QPoint(curX, bottom);
  61. }
  62. polygon[2] = QPoint(endX, bottom);
  63. polygon[3] = QPoint(endX, top);
  64. polygons.push_back(polygon);
  65. return polygons;
  66. }
  67. void QtDynamicTabBar::enterEvent(QEvent *e)
  68. {
  69. }
  70. void QtDynamicTabBar::leaveEvent(QEvent *e)
  71. {
  72. }
  73. void QtDynamicTabBar::mousePressEvent(QMouseEvent* event)
  74. {
  75. QWidget::mousePressEvent(event);
  76. }
  77. void QtDynamicTabBar::mouseReleaseEvent(QMouseEvent* event)
  78. {
  79. dbg = true;
  80. repaint();
  81. QWidget::mouseReleaseEvent(event);
  82. }
  83. void QtDynamicTabBar::mouseMoveEvent(QMouseEvent* event)
  84. {
  85. dbg = false;
  86. repaint();
  87. QWidget::mouseMoveEvent(event);
  88. }
  89. void QtDynamicTabBar::paintEvent(QPaintEvent *event)
  90. {
  91. if(dbg)
  92. {
  93. QPainter painter(this);
  94. painter.setRenderHint(QPainter::Antialiasing);
  95. painter.setPen(QColor(208, 208, 208));
  96. painter.setClipping(false);
  97. painter.fillRect(0, 0, 100, 100, QColor(190, 190, 190, 128));
  98. }
  99. }
  100. void QtDynamicTabBar::setupUi()
  101. {
  102. setMouseTracking(true);
  103. mHLayout = new QHBoxLayout(this);
  104. mHLayout->setMargin(0);
  105. setLayout(mHLayout);
  106. }
  107. void QtDynamicTabBar::updateTabs()
  108. {
  109. QLayoutItem* item;
  110. while((item = mHLayout->takeAt(0)) != nullptr)
  111. mHLayout->removeItem(item);
  112. mHLayout->addSpacing(10);
  113. UINT32 idx = 0;
  114. for(auto iter = mTabs.begin(); iter != mTabs.end(); ++iter)
  115. {
  116. (*iter)->disconnect();
  117. /*connect(*iter, &QPushButton::clicked, std::bind(this, &QtDynamicTabBar::tabSelected, idx));*/
  118. mHLayout->addWidget(*iter);
  119. idx++;
  120. }
  121. mHLayout->addStretch(2);
  122. }
  123. void QtDynamicTabBar::tabSelected(UINT32 idx)
  124. {
  125. onTabSelected(idx);
  126. }
  127. }