init
[qstardict] / qstardict / resizablepopup.h
1 /*****************************************************************************
2  * resizablepopup.h - QStarDict, a StarDict clone written with using Qt      *
3  * Copyright (C) 2007 Alexander Rodin                                        *
4  *                                                                           *
5  * This program is free software; you can redistribute it and/or modify      *
6  * it under the terms of the GNU General Public License as published by      *
7  * the Free Software Foundation; either version 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU General Public License for more details.                              *
14  *                                                                           *
15  * You should have received a copy of the GNU General Public License along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #ifndef RESIZABLEPOPUP_H
21 #define RESIZABLEPOPUP_H
22
23 #include <QFrame>
24
25 class QEvent;
26 class QMouseEvent;
27 class QTimerEvent;
28
29 namespace QStarDict
30 {
31
32 /**
33  * The ResizablePopup widget is a resizable top-level window
34  * without decorations.
35  */
36 class ResizablePopup: public QFrame
37 {
38     Q_OBJECT
39
40     public:
41         /**
42          * Construct a ResizablePopup widget.
43          */
44         ResizablePopup(QWidget *parent = 0);
45
46         /**
47          * Return timeout before hiding after mouse leaving.
48          */
49         int timeoutBeforeHide() const
50         { return m_timeoutBeforeHide; }
51         /**
52          * Return a default size of new-shown popup.
53          */
54         const QSize& defaultSize() const
55         { return m_defaultSize; }
56
57     public slots:
58         /**
59          * Set timeout before hiding after mouse leaving.
60          */
61         void setTimeoutBeforeHide(int timeoutBeforeHide)
62         { m_timeoutBeforeHide = timeoutBeforeHide; }
63         /**
64          * Set default size of new-shown popup.
65          */
66         void setDefaultSize(const QSize &defaultSize)
67         { m_defaultSize = defaultSize; }
68         /**
69          * Show popup under mouse cursor.
70          */
71         void popup();
72
73     protected:
74         void enterEvent(QEvent*);
75         void leaveEvent(QEvent*);
76         void mouseMoveEvent(QMouseEvent*);
77         void mousePressEvent(QMouseEvent*);
78         void mouseReleaseEvent(QMouseEvent*);
79         void mouseDoubleClickEvent(QMouseEvent*);
80         void timerEvent(QTimerEvent*);
81         bool event(QEvent *event);
82
83     private:
84         void stopResize();
85         void doResize();
86             
87         enum ResizeDirection
88         {
89             None,
90             Top,
91             Bottom,
92             Left,
93             Right,
94             TopLeft,
95             TopRight,
96             BottomLeft,
97             BottomRight
98         };
99         QPoint m_oldCursorPos;
100         bool m_isMoving;
101         QSize m_defaultSize;
102         ResizeDirection m_resizeDirection;
103         int m_timeoutBeforeHide;
104         int m_timerCloseId;
105         int m_timerResizeId;
106         bool m_isPopuped;
107 };
108
109 }
110
111 #endif // RESIZABLEPOPUP_H
112
113 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
114