forked from oddukgi/tearofftabwindow-qt
/
cwindowmanager.cpp
160 lines (124 loc) · 3.93 KB
/
cwindowmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "cwindowmanager.h"
/* Local */
#include "mainwindow.h"
#include "cqtabwidget.h"
/* Qt */
#include <QTimer>
#include <QTabBar>
#include <QTabWidget>
#include <QApplication>
#include <QDebug>
//
//static variables
//
CWindowManager* CWindowManager::m_instance = NULL;
//
// functions
//
CWindowManager::CWindowManager()
{
}
CWindowManager* CWindowManager::getInstance()
{
if(CWindowManager::m_instance == NULL)
CWindowManager::m_instance = new CWindowManager();
return CWindowManager::m_instance;
}
/**
* @brief 빈 메인윈도우를 지운다.
*/
void CWindowManager::removeEmptyWindow()
{
QWidgetList list = qApp->topLevelWidgets();
for(int i = 0; i < list.count(); i++) {
MainWindow* item = static_cast<MainWindow*>(list[i]);
if(item->m_tabwidget->count() == 0) {
item->setAttribute(Qt::WA_DeleteOnClose, true);
item->close();
}
}
}
MainWindow* CWindowManager::findMainWindowOf(QWidget *widget)
{
QWidget *parentWidget = widget;
MainWindow *mainwindow = NULL;
while(parentWidget != NULL) {
mainwindow = qobject_cast<MainWindow*>(parentWidget);
if(mainwindow != NULL){
break;
}
parentWidget = parentWidget->parentWidget();
}
return mainwindow;
}
/**
* @brief 스크린 상의 모든 MainWindow 중에서 마우스가 탭영역(1,2,3)에 올라가있는 메인윈도우를 찾는다.
*
* @param except 제외할 메인 윈도우.
* @return MainWindow
*
*/
MainWindow* CWindowManager::findMainWindowByCursorOnTabWithout(MainWindow* except)
{
MainWindow* windowToGo = 0;
QWidgetList list = qApp->topLevelWidgets();
QWidgetList::iterator it;
// 모든 MainWindow를 순회하면서 마우스가 탭 영역에 올라가있는 MainWindow를 찾는다.
for(it = list.begin(); it != list.end(); ++it) {
MainWindow *item = reinterpret_cast<MainWindow*>(*it);
if(except && except == item) {
continue;
}
if(CWindowManager::isCursorOnTabWithEmptyArea(item)) {
windowToGo = item;
break;
}
}
return windowToGo;
}
MainWindow *CWindowManager::findMainWindowByCursorOnTab()
{
return CWindowManager::findMainWindowByCursorOnTabWithout(0);
}
/**
* @brief 마우스 커서가 MainWindow의 탭영역(1,2,3)에 올라가있는지 확인한다.
*
* +-------------------------------+-+-+-+
* | TITLE BAR |_|O|X|
* +----------+----------+---------+-+-+-+
* |##1(Tab)##|##2(Tab)##|###3(Empty)####|
* +----------+----------+---------------+
* | |
* | 4 |
* | |
* +-------------------------------------+
*
* TabBar::geometry()는 1,2영역을 나타내고, TabWidget::geometry()는 1,2,3,4영역을 나타낸다.
* 이 두 geometry()를 연산해서 1,2,3 영역을 찾는다.
*
* @param mainWindow 대상 메인 윈도
* @return bool
*
*/
bool CWindowManager::isCursorOnTabWithEmptyArea(MainWindow *mainWindow)
{
QRect barRect;
QRect barRectG;
QRect widgetRect;
QRect widgetRectG;
QTabBar* tabBar = 0;
QTabWidget* tabWidget = 0;
if(!mainWindow)
return false;
tabBar = mainWindow->getTabBar();
tabWidget = mainWindow->getTabWidget();
barRect = tabBar->geometry();
widgetRect = tabWidget->geometry();
barRectG.setTopLeft(tabBar->mapToGlobal(barRect.topLeft()));
barRectG.setBottomRight(tabBar->mapToGlobal(barRect.bottomRight()));
widgetRectG.setTopLeft(tabWidget->mapToGlobal(widgetRect.topLeft()));
widgetRectG.setBottomRight(tabWidget->mapToGlobal(widgetRect.bottomRight()));
widgetRectG.setTopLeft(QPoint(widgetRectG.topLeft().x(), barRectG.topLeft().y()));
widgetRectG.setBottomRight(QPoint(widgetRectG.bottomRight().x(), barRectG.bottomRight().y()));
return widgetRectG.contains(QCursor::pos());
}