qminmaxwindowwidget.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "qminmaxwindowwidget.h"
  2. #include "qevent.h"
  3. #include "qtimer.h"
  4. QMinMaxWindowWidget::QMinMaxWindowWidget(QWidget *parent)
  5. : QWidget(parent), m_contrl(nullptr)
  6. {
  7. ui.setupUi(this);
  8. setAttribute(Qt::WA_DeleteOnClose, true);
  9. m_contentMargin = QMargins(3, 3, 3, 3);
  10. connect(ui.toolButtonClose, SIGNAL(clicked()), this, SLOT(closeWindow()));
  11. connect(ui.toolButtonMax, SIGNAL(clicked()), this, SLOT(switchWindow()));
  12. connect(ui.toolButtonMin, SIGNAL(clicked()), this, SLOT(minimizedWindow()));
  13. }
  14. QMinMaxWindowWidget::~QMinMaxWindowWidget()
  15. {
  16. }
  17. void QMinMaxWindowWidget::setControlWidget(QWidget * contrl)
  18. {
  19. m_contrl = contrl;
  20. if (m_contrl)m_contrl->installEventFilter(this);
  21. initalizeIcons();
  22. }
  23. bool QMinMaxWindowWidget::eventFilter(QObject * o, QEvent * e)
  24. {
  25. if (o == m_contrl){
  26. if (e->type() == QEvent::Show){
  27. QShowEvent * event = (QShowEvent *)e;
  28. QTimer::singleShot(100, this, &QMinMaxWindowWidget::initalizeIcons);
  29. }
  30. }
  31. return false;
  32. }
  33. void QMinMaxWindowWidget::initalizeIcons()
  34. {
  35. if (m_contrl&&m_contrl->isMaximized()){
  36. if (m_contrl->layout())
  37. {
  38. m_contentMargin = m_contrl->layout()->contentsMargins();
  39. m_contrl->layout()->setContentsMargins(QMargins(0, 0, 0, 0));
  40. }
  41. }
  42. }
  43. void QMinMaxWindowWidget::switchWindow()
  44. {
  45. if (m_contrl){
  46. if (m_contrl->isMaximized()){
  47. m_contrl->showNormal();
  48. if (m_contrl->layout())
  49. {
  50. m_contrl->layout()->setContentsMargins(QMargins(3, 3, 3, 3));
  51. }
  52. ui.toolButtonMax->setToolTip(QString::fromStdWString(L"×î´ó»¯"));
  53. }
  54. else{
  55. if (m_contrl->layout())
  56. {
  57. m_contrl->layout()->setContentsMargins(QMargins(0, 0, 0, 0));
  58. }
  59. m_contrl->showMaximized();
  60. ui.toolButtonMax->setToolTip(QString::fromStdWString(L"»¹Ô­"));
  61. //QIcon icon;
  62. //icon.addFile(QString::fromLocal8Bit(":/conctrl/Resources/0huanyuan.png"));
  63. //ui.toolButtonMax->setIcon(icon);
  64. }
  65. }
  66. }
  67. void QMinMaxWindowWidget::minimizedWindow()
  68. {
  69. if (m_contrl)m_contrl->showMinimized();
  70. }
  71. void QMinMaxWindowWidget::closeWindow()
  72. {
  73. if (m_contrl){
  74. m_contrl->close();
  75. }
  76. }
  77. void QMinMaxWindowWidget::setMaxVisible(bool visible)
  78. {
  79. ui.toolButtonMax->setVisible(visible);
  80. }
  81. void QMinMaxWindowWidget::setMinVisible(bool visible)
  82. {
  83. ui.toolButtonMin->setVisible(visible);
  84. }
  85. void QMinMaxWindowWidget::setCloseVisible(bool visible)
  86. {
  87. ui.toolButtonClose->setVisible(visible);
  88. }