CmQtDynamicTabBar.cpp 4.0 KB

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