Advertisement
Ruslan_F

Untitled

Apr 10th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.95 KB | None | 0 0
  1. #pragma once
  2.  
  3. #define PIXELS_TO_ACT 3
  4. #include <QtGui>
  5. #include "StyleEngene.h"
  6. template <class T>
  7. class BaseWindow : public T
  8. {
  9. public:
  10.     enum ResizeMode { NoResize, AllowResize};
  11.     enum TitleMode { FullTitle = 0, MaximizeModeOff = 1, MinimizeModeOff = 2, MaxMinOff = MaximizeModeOff | MinimizeModeOff, OnlyCloseButton = MaxMinOff, FullScreenMode = 4 };
  12.     BaseWindow(TitleMode titleMode, ResizeMode resizeMode, QWidget* parent = 0);
  13.     void showMaximized();
  14.     void showNormal();
  15.     void setGeometry(QRect geo);
  16.     void setTitle(QString title);
  17.     bool isMaximized();
  18. protected:
  19.     bool m_bIsMaximized;
  20.     QRect preMaximizeGeomentry;
  21.     QHBoxLayout* centralLayout;
  22.     /**
  23.     * @brief dragPosition Increment of the position movement.
  24.     */
  25.     QPoint dragPosition;
  26.     ResizeMode m_resizeMode;
  27.  
  28.     /**
  29.     * @brief m_titleMode Flags that defines the current getTitleBar() mode.
  30.     */
  31.     TitleMode m_titleMode;
  32.     /**
  33.     * @brief moveWidget Specifies if the window is in move action.
  34.     */
  35.     bool moveWidget;
  36.     /**
  37.     * @brief inResizeZone Specifies if the mouse is in resize zone.
  38.     */
  39.     bool inResizeZone;
  40.     /**
  41.     * @brief allowToResize Specifies if the mouse is allowed to resize.
  42.     */
  43.     bool allowToResize;
  44.     /**
  45.     * @brief resizeVerSup Specifies if the resize is in the top of the window.
  46.     */
  47.     bool resizeVerSup;
  48.     /**
  49.     * @brief resizeHorEsq Specifies if the resize is in the left of the window.
  50.     */
  51.     bool resizeHorEsq;
  52.     /**
  53.     * @brief resizeDiagSupEsq Specifies if the resize is in the top left of the window.
  54.     */
  55.     bool resizeDiagSupEsq;
  56.     /**
  57.     * @brief resizeDiagSupDer Specifies if the resize is in the top right of the window.
  58.     */
  59.     bool resizeDiagSupDer;
  60.  
  61.     /**
  62.     * @brief mouseMoveEvent Overloaded member that moves of resizes depending of the
  63.     * configuration sotred at mousePressEvent().
  64.     * @param e The mouse event.
  65.     */
  66.     virtual void mouseMoveEvent(QMouseEvent* e);
  67.     /**
  68.     * @brief mousePressEvent Overloaded member that stores that changes the cursor and
  69.     * configures the side that is changing.
  70.     * @param e The mouse event.
  71.     */
  72.     virtual void mousePressEvent(QMouseEvent* e);
  73.     /**
  74.     * @brief mouseReleaseEvent Overloaded member that removes the configuration set in mousePressEvent().
  75.     * @param e The mouse event.
  76.     */
  77.     virtual void mouseReleaseEvent(QMouseEvent* e);
  78.     /**
  79.     * @brief mouseDoubleClickEvent Overloadad member that maximize/restore the window if is
  80.     * doubleclicked and the position of the mouse is not the top left of the window (menu zone).
  81.     * @param e The mouse event.
  82.     */
  83.     virtual void mouseDoubleClickEvent(QMouseEvent* e);
  84.     /**
  85.     * @brief paintEvent Overloaded method that allows to customize the styles of the window.
  86.     */
  87.     virtual void paintEvent(QPaintEvent*);
  88.     /**
  89.     * @brief resizeWindow Method that calculates the resize and new position of the window an
  90.     * does this actions.
  91.     * @param e The mouse event to calculate the new position and size.
  92.     */
  93.     virtual void resizeWindow(QMouseEvent* e);
  94.  
  95.     virtual void moveWindow(QMouseEvent* e);
  96.     virtual void setupCustomWindow();
  97.  
  98.     virtual void setupWindowIcons();
  99.     virtual QPushButton* getMinBtn();
  100.     virtual QPushButton* getMaxBtn();
  101.     virtual QPushButton* getCloseBtn();
  102.     virtual QWidget* getTitleBar();
  103.     virtual QWidget* centralWidget();
  104.     virtual QLabel* getTitleLabel();
  105.     virtual QLabel* getTitleIcon();
  106. public slots:
  107.     virtual void maximizeBtnClicked();
  108.     virtual void minimizeBtnClicked();
  109. };
  110.  
  111. template <class T>
  112. void BaseWindow<T>::setTitle(QString title)
  113. {
  114.     if (getTitleLabel() != NULL)
  115.     {
  116.         getTitleLabel()->setText(title);
  117.     }
  118. }
  119.  
  120. template <class T>
  121. bool BaseWindow<T>::isMaximized()
  122. {
  123.     return m_bIsMaximized;
  124. }
  125.  
  126. template <class T>
  127. void BaseWindow<T>::setGeometry(QRect geo)
  128. {
  129.     if (!m_bIsMaximized)
  130.     {
  131.         preMaximizeGeomentry = geo;
  132.     }
  133.  
  134.     T::setGeometry(geo);
  135. }
  136.  
  137. template <class T>
  138. void BaseWindow<T>::showNormal()
  139. {
  140.     setGeometry(preMaximizeGeomentry);
  141.     T::showNormal();
  142.     m_bIsMaximized = false;
  143. }
  144.  
  145. template <class T>
  146. void BaseWindow<T>::showMaximized()
  147. {
  148.     QDesktopWidget* desktop = QApplication::desktop();
  149.     preMaximizeGeomentry = geometry();
  150.     m_bIsMaximized = true;
  151.     setGeometry(desktop->availableGeometry());
  152.     T::showNormal();
  153. }
  154.  
  155. template <class T>
  156. QLabel* BaseWindow<T>::getTitleIcon()
  157. {
  158.     return NULL;
  159. }
  160.  
  161. template <class T>
  162. QLabel* BaseWindow<T>::getTitleLabel()
  163. {
  164.     return NULL;
  165. }
  166.  
  167. template <class T>
  168. QWidget* BaseWindow<T>::centralWidget()
  169. {
  170.     return NULL;
  171. }
  172.  
  173. template <class T>
  174. QWidget* BaseWindow<T>::getTitleBar()
  175. {
  176.     return NULL;
  177. }
  178.  
  179. template <class T>
  180. QPushButton* BaseWindow<T>::getCloseBtn()
  181. {
  182.     return NULL;
  183. }
  184.  
  185. template <class T>
  186. QPushButton* BaseWindow<T>::getMaxBtn()
  187. {
  188.     return NULL;
  189. }
  190.  
  191. template <class T>
  192. QPushButton* BaseWindow<T>::getMinBtn()
  193. {
  194.     return NULL;
  195. }
  196.  
  197. template <class T>
  198. BaseWindow<T>::BaseWindow(TitleMode titleMode, ResizeMode resizeMode, QWidget* parent) : T(parent), m_bIsMaximized(false)
  199. {
  200.     m_titleMode = titleMode;
  201.     m_resizeMode = resizeMode;
  202. }
  203.  
  204. template <class T>
  205. void BaseWindow<T>::setupWindowIcons()
  206. {
  207.     StyleEngene* style = StyleEngene::getInstance();
  208.  
  209.     if((m_titleMode & BaseWindow::MinimizeModeOff) == 0 && getMinBtn() != NULL)
  210.     {
  211.         getMinBtn()->setIcon(style->getIcon("app_min"));
  212.     }
  213.  
  214.     if((m_titleMode & BaseWindow::MaximizeModeOff) == 0 && getMaxBtn() != NULL)
  215.     {
  216.         if (m_bIsMaximized)
  217.         {
  218.             getMaxBtn()->setIcon(style->getIcon("app_reset"));
  219.         }
  220.         else
  221.         {
  222.             getMaxBtn()->setIcon(style->getIcon("app_max"));
  223.         }
  224.     }
  225.  
  226.     getCloseBtn()->setIcon(style->getIcon("app_close"));
  227. }
  228.  
  229. template <class T>
  230. void BaseWindow<T>::setupCustomWindow()
  231. {
  232.     setWindowFlags(Qt::FramelessWindowHint);
  233.     setMouseTracking(true);
  234.     getTitleBar()->setMouseTracking(true);
  235.     getTitleLabel()->setMouseTracking(true);
  236.     getTitleIcon()->setMouseTracking(true);
  237.     getCloseBtn()->setMouseTracking(true);
  238.  
  239.     if(centralWidget() == NULL)
  240.     {
  241.         qDebug() << "Central widget is null";
  242.     }
  243.     else
  244.     {
  245.         centralWidget()->setMouseTracking(true);
  246.     }
  247.  
  248.     if ((m_titleMode & BaseWindow::MinimizeModeOff) == BaseWindow::MinimizeModeOff && getMinBtn() != NULL)
  249.     {
  250.         getMinBtn()->setMouseTracking(true);
  251.         connect(getMinBtn(), SIGNAL(clicked()), this, SLOT(minimizeBtnClicked()));
  252.     }
  253.     else if(getMinBtn() != NULL)
  254.     {
  255.         getMinBtn()->hide();
  256.     }
  257.  
  258.     if ((m_titleMode & BaseWindow::MaximizeModeOff) == BaseWindow::MinimizeModeOff && getMaxBtn() != NULL)
  259.     {
  260.         getMaxBtn()->setMouseTracking(true);
  261.         connect(getMaxBtn(), SIGNAL(clicked()), this, SLOT(maximizeBtnClicked()));
  262.     }
  263.     else if(getMaxBtn() != NULL)
  264.     {
  265.         getMaxBtn()->hide();
  266.     }
  267.  
  268.     connect(getCloseBtn(), SIGNAL(clicked()), this, SLOT(close()));
  269.     moveWidget = false;
  270.     inResizeZone = false;
  271.     allowToResize = false;
  272.     resizeVerSup = false;
  273.     resizeHorEsq = false;
  274.     resizeDiagSupEsq = false;
  275.     resizeDiagSupDer = false;
  276. }
  277.  
  278.  
  279. template <class T>
  280. void BaseWindow<T>::maximizeBtnClicked()
  281. {
  282.     StyleEngene* styleEngine = StyleEngene::getInstance();
  283.  
  284.     if(isFullScreen() || isMaximized())
  285.     {
  286.         getMaxBtn()->setIcon(styleEngine->getIcon("app_max"));
  287.         showNormal();
  288.     }
  289.     else
  290.     {
  291.         getMaxBtn()->setIcon(styleEngine->getIcon("app_reset"));
  292.         showMaximized();
  293.     }
  294. }
  295.  
  296. template <class T>
  297. void BaseWindow<T>::minimizeBtnClicked()
  298. {
  299.     if(isMinimized())
  300.     {
  301.         setWindowState(windowState() & ~Qt::WindowMinimized);
  302.     }
  303.     else
  304.     {
  305.         setWindowState(windowState() | Qt::WindowMinimized);
  306.     }
  307. }
  308. template <class T>
  309. void BaseWindow<T>::moveWindow(QMouseEvent* e)
  310. {
  311.     if(e->buttons() & Qt::LeftButton)
  312.     {
  313.         move(e->globalPos() - dragPosition);
  314.         e->accept();
  315.     }
  316. }
  317.  
  318. template <class T>
  319. void BaseWindow<T>::resizeWindow(QMouseEvent* e)
  320. {
  321.     if(allowToResize)
  322.     {
  323.         int xMouse = e->pos().x();
  324.         int yMouse = e->pos().y();
  325.         int wWidth = geometry().width();
  326.         int wHeight = geometry().height();
  327.  
  328.         if(cursor().shape() == Qt::SizeVerCursor)
  329.         {
  330.             if(resizeVerSup)
  331.             {
  332.                 int newY = geometry().y() + yMouse;
  333.                 int newHeight = wHeight - yMouse;
  334.  
  335.                 if(newHeight > minimumSizeHint().height())
  336.                 {
  337.                     resize(wWidth, newHeight);
  338.                     move(geometry().x(), newY);
  339.                 }
  340.             }
  341.             else
  342.             {
  343.                 resize(wWidth, yMouse + 1);
  344.             }
  345.         }
  346.         else if(cursor().shape() == Qt::SizeHorCursor)
  347.         {
  348.             if(resizeHorEsq)
  349.             {
  350.                 int newX = geometry().x() + xMouse;
  351.                 int newWidth = wWidth - xMouse;
  352.  
  353.                 if(newWidth > minimumSizeHint().width())
  354.                 {
  355.                     resize(newWidth, wHeight);
  356.                     move(newX, geometry().y());
  357.                 }
  358.             }
  359.             else
  360.             {
  361.                 resize(xMouse, wHeight);
  362.             }
  363.         }
  364.         else if(cursor().shape() == Qt::SizeBDiagCursor)
  365.         {
  366.             int newX = 0;
  367.             int newWidth = 0;
  368.             int newY = 0;
  369.             int newHeight = 0;
  370.  
  371.             if(resizeDiagSupDer)
  372.             {
  373.                 newX = geometry().x();
  374.                 newWidth = xMouse;
  375.                 newY = geometry().y() + yMouse;
  376.                 newHeight = wHeight - yMouse;
  377.             }
  378.             else
  379.             {
  380.                 newX = geometry().x() + xMouse;
  381.                 newWidth = wWidth - xMouse;
  382.                 newY = geometry().y();
  383.                 newHeight = yMouse;
  384.             }
  385.  
  386.             if(newWidth >= minimumSizeHint().width() && newHeight >= minimumSizeHint().height())
  387.             {
  388.                 resize(newWidth, newHeight);
  389.                 move(newX, newY);
  390.             }
  391.             else if(newWidth >= minimumSizeHint().width())
  392.             {
  393.                 resize(newWidth, wHeight);
  394.                 move(newX, geometry().y());
  395.             }
  396.             else if(newHeight >= minimumSizeHint().height())
  397.             {
  398.                 resize(wWidth, newHeight);
  399.                 move(geometry().x(), newY);
  400.             }
  401.         }
  402.         else if(cursor().shape() == Qt::SizeFDiagCursor)
  403.         {
  404.             if(resizeDiagSupEsq)
  405.             {
  406.                 int newX = geometry().x() + xMouse;
  407.                 int newWidth = wWidth - xMouse;
  408.                 int newY = geometry().y() + yMouse;
  409.                 int newHeight = wHeight - yMouse;
  410.  
  411.                 if(newWidth >= minimumSizeHint().width() && newHeight >= minimumSizeHint().height())
  412.                 {
  413.                     resize(newWidth, newHeight);
  414.                     move(newX, newY);
  415.                 }
  416.                 else if(newWidth >= minimumSizeHint().width())
  417.                 {
  418.                     resize(newWidth, wHeight);
  419.                     move(newX, geometry().y());
  420.                 }
  421.                 else if(newHeight >= minimumSizeHint().height())
  422.                 {
  423.                     resize(wWidth, newHeight);
  424.                     move(geometry().x(), newY);
  425.                 }
  426.             }
  427.             else
  428.             {
  429.                 resize(xMouse + 1, yMouse + 1);
  430.             }
  431.         }
  432.  
  433.         e->accept();
  434.     }
  435. }
  436.  
  437. template <class T>
  438. void BaseWindow<T>::paintEvent(QPaintEvent*)
  439. {
  440.     QStyleOption opt;
  441.     opt.init(this);
  442.     QPainter p(this);
  443.     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  444. }
  445.  
  446. template <class T>
  447. void BaseWindow<T>::mouseDoubleClickEvent(QMouseEvent* e)
  448. {
  449.     QPoint position = e->pos();
  450.     QRect geometry = getTitleIcon()->geometry();
  451.  
  452.     if(position.x() < geometry.right() && position.y() < geometry.bottom()
  453.             && position.x() >= geometry.x() && position.y() >= geometry.y()
  454.             && getTitleIcon()->isVisible())
  455.     {
  456.         close();
  457.     }
  458.     else if(position.x() < getTitleBar()->geometry().width()
  459.             && position.y() < getTitleBar()->geometry().height()
  460.             && m_titleMode != FullScreenMode)
  461.     {
  462.         maximizeBtnClicked();
  463.     }
  464.  
  465.     e->accept();
  466. }
  467.  
  468. template <class T>
  469. void BaseWindow<T>::mouseReleaseEvent(QMouseEvent* e)
  470. {
  471.     moveWidget = false;
  472.     allowToResize = false;
  473.     resizeVerSup = false;
  474.     resizeHorEsq = false;
  475.     resizeDiagSupEsq = false;
  476.     resizeDiagSupDer = false;
  477.     e->accept();
  478. }
  479.  
  480. template <class T>
  481. void BaseWindow<T>::mousePressEvent(QMouseEvent* e)
  482. {
  483.     if(e->button() == Qt::LeftButton)
  484.     {
  485.         QPoint pos = e->pos();
  486.  
  487.         if(inResizeZone && m_resizeMode == AllowResize)
  488.         {
  489.             allowToResize = true;
  490.  
  491.             if(pos.y() <= PIXELS_TO_ACT)
  492.             {
  493.                 if(pos.x() <= PIXELS_TO_ACT)
  494.                 {
  495.                     resizeDiagSupEsq = true;
  496.                 }
  497.                 else if(pos.x() >= geometry().width() - PIXELS_TO_ACT)
  498.                 {
  499.                     resizeDiagSupDer = true;
  500.                 }
  501.                 else
  502.                 {
  503.                     resizeVerSup = true;
  504.                 }
  505.             }
  506.             else if(pos.x() <= PIXELS_TO_ACT)
  507.             {
  508.                 resizeHorEsq = true;
  509.             }
  510.         }
  511.         else if(pos.x() >= PIXELS_TO_ACT && pos.x() < getTitleBar()->geometry().width()
  512.                 && pos.y() >= PIXELS_TO_ACT && pos.y() < getTitleBar()->geometry().height())
  513.         {
  514.             moveWidget = true;
  515.             dragPosition = e->globalPos() - frameGeometry().topLeft();
  516.         }
  517.     }
  518.  
  519.     e->accept();
  520. }
  521.  
  522. template <class T>
  523. void BaseWindow<T>::mouseMoveEvent(QMouseEvent* e)
  524. {
  525.     int xMouse = e->globalPos().x();
  526.     int yMouse = e->globalPos().y();
  527.     int wLeft = geometry().left();
  528.     int wTop = geometry().top();
  529.     int wWidth = geometry().width();
  530.     int wHeight = geometry().height();
  531.     bool isResizeEnabled = m_resizeMode == AllowResize;
  532.  
  533.     //qDebug() << moveWidget << xMouse << yMouse << wWidth << wHeight << allowToResize << isResizeEnabled;
  534.     if(moveWidget)
  535.     {
  536.         inResizeZone = false;
  537.  
  538.         if(isMaximized())
  539.         {
  540.             maximizeBtnClicked();
  541.         }
  542.  
  543.         moveWindow(e);
  544.     }
  545.     else if(allowToResize)
  546.     {
  547.         resizeWindow(e);
  548.     }
  549.     else if(isResizeEnabled)
  550.     {
  551.         if ((xMouse >= wLeft + wWidth - PIXELS_TO_ACT && xMouse <= wLeft + wWidth + PIXELS_TO_ACT) || allowToResize)
  552.         {
  553.             inResizeZone = true;
  554.  
  555.             if (yMouse >= wTop + wHeight - PIXELS_TO_ACT && yMouse <= wTop + wHeight + PIXELS_TO_ACT)
  556.             {
  557.                 setCursor(Qt::SizeFDiagCursor);
  558.             }
  559.             else if (yMouse >= wTop - PIXELS_TO_ACT && yMouse <= wTop + PIXELS_TO_ACT)
  560.             {
  561.                 setCursor(Qt::SizeBDiagCursor);
  562.             }
  563.             else
  564.             {
  565.                 setCursor(Qt::SizeHorCursor);
  566.             }
  567.  
  568.             resizeWindow(e);
  569.         }
  570.         //Cursor part esquerra
  571.         else if ((xMouse >= wLeft - PIXELS_TO_ACT && xMouse <= wLeft + PIXELS_TO_ACT) || allowToResize)
  572.         {
  573.             inResizeZone = true;
  574.  
  575.             if (yMouse >= wTop + wHeight - PIXELS_TO_ACT && yMouse <= wTop + wHeight + PIXELS_TO_ACT)
  576.             {
  577.                 setCursor(Qt::SizeBDiagCursor);
  578.             }
  579.             else if (yMouse >= wTop - PIXELS_TO_ACT && yMouse <= wTop + PIXELS_TO_ACT)
  580.             {
  581.                 setCursor(Qt::SizeFDiagCursor);
  582.             }
  583.             else
  584.             {
  585.                 setCursor(Qt::SizeHorCursor);
  586.             }
  587.  
  588.             resizeWindow(e);
  589.         }
  590.         //Cursor part inferior
  591.         else if ((yMouse >= wTop + wHeight - PIXELS_TO_ACT && yMouse <= wTop + wHeight + PIXELS_TO_ACT) || allowToResize)
  592.         {
  593.             inResizeZone = true;
  594.             setCursor(Qt::SizeVerCursor);
  595.             resizeWindow(e);
  596.         }
  597.         //Cursor part superior
  598.         else if ((yMouse >= wTop - PIXELS_TO_ACT && yMouse <= wTop + PIXELS_TO_ACT) || allowToResize)
  599.         {
  600.             inResizeZone = true;
  601.             setCursor(Qt::SizeVerCursor);
  602.             resizeWindow(e);
  603.         }
  604.         else
  605.         {
  606.             inResizeZone = false;
  607.             setCursor(Qt::ArrowCursor);
  608.         }
  609.     }
  610.  
  611.     e->accept();
  612. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement