forked from anyox/Qt-Auto-Hide-DockWidget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNXDockWidget.cpp
207 lines (165 loc) · 4.6 KB
/
NXDockWidget.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "NXDockWidget.h"
#include "NXDockWidgetTabButton.h"
#include "NXDockWidgetTitle.h"
#include "NXMainWindow.h"
#include <QDockWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListView>
#include <QPushButton>
#include <QLabel>
#include <QToolBar>
#include "qcoreevent.h"
#include "qevent.h"
#include "NXMainWindow.h"
#include "qcoreapplication.h"
#include <iostream>
#include "qlistwidget.h"
#include "qtimer.h"
#include <QDebug>
NXDockWidget::NXDockWidget(const QString& title)
: QDockWidget(nullptr)
, m_area(Qt::NoDockWidgetArea)
, m_state(DockWidgetState::Unknown)
{
setObjectName("DockWidget");
setAutoFillBackground(true);
m_titleWidget = new NXDockWidgetTitle();
setWindowTitle(title);
setTitleBarWidget(m_titleWidget);
m_layout = new QVBoxLayout;
m_layout->setContentsMargins(0, 2, 0, 0);
QWidget* widget = new QWidget();
widget->setLayout(m_layout);
QDockWidget::setWidget(widget);
connect(m_titleWidget, &NXDockWidgetTitle::menuButton_pressed, this, &NXDockWidget::openTitleMenu);
connect(m_titleWidget, &NXDockWidgetTitle::autoHideButton_pressed, this, &NXDockWidget::autoHideStateToggle);
connect(m_titleWidget, &NXDockWidgetTitle::closeButton_pressed, this, &NXDockWidget::closeDockWidget);
connect(this, &QDockWidget::dockLocationChanged, this, &NXDockWidget::updateDockLocation);
connect(this, &QDockWidget::topLevelChanged, this, &NXDockWidget::updateTopLevelState);
}
NXDockWidget::~NXDockWidget()
{
qDebug() << "NXDockWidget dtor";
}
QString NXDockWidget::windowTitle() const
{
return m_titleWidget->getText();
}
void NXDockWidget::setWindowTitle(const QString& text)
{
QString title = text.isEmpty() ? "Noname" : text;
m_titleWidget->setText(title);
QDockWidget::setWindowTitle(title);
}
void NXDockWidget::closeDockWidget()
{
if(isMinimized()) {
emit signal_pinned(this);
}
setState(DockWidgetState::Closed);
hide();
}
void NXDockWidget::openTitleMenu()
{
// TODO add menu here
QMenu menu;
menu.addAction("Float", this, SLOT(slot_menuAction()));
menu.addAction("Dock", this, SLOT(slot_menuAction()));
menu.addAction("Auto Hide", this, SLOT(slot_menuAction()));
menu.addAction("Hide", this, SLOT(slot_menuAction()));
menu.exec(m_titleWidget->menuPos());
}
void NXDockWidget::autoHideStateToggle()
{
if(isMinimized())
{
setState(DockWidgetState::Docked);
emit signal_pinned(this);
}
else
{
setState(DockWidgetState::Hidden);
emit signal_unpinned(this);
}
}
void NXDockWidget::updateDockLocation(Qt::DockWidgetArea area)
{
m_area = area;
if(m_area != Qt::NoDockWidgetArea) {
updateTopLevelState(false);
}
}
void NXDockWidget::updateTopLevelState(bool topLevel)
{
m_titleWidget->setAutoHideEnadled(false);
if(topLevel)
{
setState(DockWidgetState::Floating);
std::for_each(m_tabifieds.begin(), m_tabifieds.end(), [&](NXDockWidget* dockWidget) {
dockWidget->removeFromTabifiedDocks(this);
} );
clearTabifiedDocks();
emit signal_undocked(this);
}
else
{
setState(DockWidgetState::Docked);
QList<QDockWidget*> tabifiedDockWidgetList = static_cast<NXMainWindow*>(parentWidget())->tabifiedDockWidgets(this);
tabifiedDockWidgetList.push_back(this);
std::for_each(std::begin(tabifiedDockWidgetList), std::end(tabifiedDockWidgetList), [&](QDockWidget* qDockWidget) {
qobject_cast<NXDockWidget*>(qDockWidget)->setTabifiedDocks(tabifiedDockWidgetList);
} );
emit signal_docked(this);
}
}
void NXDockWidget::setState(DockWidgetState state)
{
m_state = state;
switch(state)
{
case DockWidgetState::Docked:
m_titleWidget->setFloating(true);
break;
case DockWidgetState::Floating:
m_titleWidget->setFloating(false);
break;
case DockWidgetState::Hidden:
m_titleWidget->setAutoHideEnadled(true);
break;
case DockWidgetState::Closed:
break;
default:
break;
}
}
bool NXDockWidget::event(QEvent* event)
{
// qDebug() << event->type();
if(event->type() == QEvent::Enter)
{
}
else if(event->type() == QEvent::Leave)
{
}
else if(event->type() == QEvent::FocusOut)
{
}
// Make sure the rest of events are handled
return QDockWidget::event(event);
}
void NXDockWidget::setWidget(QWidget* widget)
{
m_layout->addWidget(widget);
}
void NXDockWidget::setTabifiedDocks(const QList<QDockWidget*>& dockWidgetList)
{
m_tabifieds.clear();
std::transform(std::begin(dockWidgetList), std::end(dockWidgetList), std::back_inserter(m_tabifieds), [&](QDockWidget* qDockWidget) {
return static_cast<NXDockWidget*>(qDockWidget);
} );
}
void NXDockWidget::removeFromTabifiedDocks(NXDockWidget* dockWidget)
{
m_tabifieds.erase(std::remove(std::begin(m_tabifieds), std::end(m_tabifieds), dockWidget), std::end(m_tabifieds));
}