Skip to content

Commit a931332

Browse files
committed
Improve MDI window management
1 parent 00cf362 commit a931332

File tree

8 files changed

+93
-98
lines changed

8 files changed

+93
-98
lines changed

src/core/gui/core_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ void mbCoreUi::initialize()
123123
m_ui.dockProject->setWidget(m_projectUi);
124124

125125
connect(m_dataViewManager, &mbCoreDataViewManager::dataViewUiContextMenu, this, &mbCoreUi::contextMenuDataViewUi);
126+
connect(m_dataViewManager, &mbCoreDataViewManager::dataViewUiAdd , this, &mbCoreUi::dataViewWindowAdd );
127+
connect(m_dataViewManager, &mbCoreDataViewManager::dataViewUiRemove , this, &mbCoreUi::dataViewWindowRemove );
126128

127129
m_ui.actionWindowViewSubWindow->setCheckable(true);
128130
m_ui.actionWindowViewTabbed->setCheckable(true);
129131
slotWindowManagerViewModeChanged(m_windowManager->viewMode());
130132
connect(m_windowManager, &mbCoreWindowManager::viewModeChanged, this, &mbCoreUi::slotWindowManagerViewModeChanged);
131-
connect(m_windowManager, &mbCoreWindowManager::dataViewWindowAdded, this, &mbCoreUi::dataViewWindowAdd);
132-
connect(m_windowManager, &mbCoreWindowManager::dataViewWindowRemoving, this, &mbCoreUi::dataViewWindowRemove);
133133
this->setCentralWidget(m_windowManager->centralWidget());
134134

135135
// Menu File

src/core/gui/core_windowmanager.cpp

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
#include "core_windowmanager.h"
2424

25+
#include <QEvent>
2526
#include <QMdiArea>
2627
#include <QMdiSubWindow>
2728

@@ -104,9 +105,13 @@ QString mbCoreWindowManager::getMdiSubWindowNameWithPrefix(const QMdiSubWindow *
104105
return QString();
105106
}
106107

107-
void mbCoreWindowManager::showDataViewUi(const mbCoreDataViewUi *ui)
108+
void mbCoreWindowManager::showDataViewUi(mbCoreDataViewUi *ui)
108109
{
109-
showSubWindow(ui);
110+
QMdiSubWindow *sw = m_hashWindows.value(ui);
111+
if (sw)
112+
showSubWindow(sw);
113+
else
114+
dataViewUiAdd(ui);
110115
}
111116

112117
void mbCoreWindowManager::actionWindowViewSubWindow()
@@ -121,9 +126,11 @@ void mbCoreWindowManager::actionWindowViewTabbed()
121126

122127
void mbCoreWindowManager::actionWindowDataViewCloseAll()
123128
{
124-
Q_FOREACH(QMdiSubWindow *sw, m_dataViews)
129+
Q_FOREACH(mbCoreDataViewUi *ui, m_dataViewManager->dataViewUisCore())
125130
{
126-
sw->close();
131+
QMdiSubWindow *sw = m_hashWindows.value(ui);
132+
if (sw)
133+
closeSubWindow(sw);
127134
}
128135
}
129136

@@ -206,6 +213,7 @@ QMdiSubWindow *mbCoreWindowManager::subWindowAdd(QWidget *ui)
206213
sw->setWindowIcon(icon);
207214
m_hashWindows.insert(ui, sw);
208215
m_area->addSubWindow(sw);
216+
sw->installEventFilter(this);
209217
sw->show();
210218
return sw;
211219
}
@@ -216,6 +224,8 @@ QMdiSubWindow *mbCoreWindowManager::subWindowRemove(QWidget *ui)
216224
if (sw)
217225
{
218226
m_hashWindows.remove(ui);
227+
if (m_area->activeSubWindow() == sw)
228+
m_area->activateNextSubWindow();
219229
m_area->removeSubWindow(sw);
220230
sw->setWidget(nullptr);
221231
ui->setParent(nullptr);
@@ -253,10 +263,8 @@ void mbCoreWindowManager::setProject(mbCoreProject *p)
253263
void mbCoreWindowManager::dataViewUiAdd(mbCoreDataViewUi *ui)
254264
{
255265
QMdiSubWindow* sw = subWindowAdd(ui);
256-
m_dataViews.append(sw);
257266
connect(ui, &mbCoreDataViewUi::nameChanged, sw, &QWidget::setWindowTitle);
258267
sw->setWindowTitle(ui->name());
259-
Q_EMIT dataViewWindowAdded(ui);
260268
}
261269

262270
void mbCoreWindowManager::dataViewUiRemove(mbCoreDataViewUi *ui)
@@ -265,9 +273,7 @@ void mbCoreWindowManager::dataViewUiRemove(mbCoreDataViewUi *ui)
265273
QMdiSubWindow* sw = subWindowRemove(ui);
266274
if (sw)
267275
{
268-
Q_EMIT dataViewWindowRemoving(ui);
269-
m_dataViews.removeOne(sw);
270-
delete sw;
276+
sw->deleteLater();
271277
}
272278
}
273279

@@ -286,20 +292,33 @@ void mbCoreWindowManager::subWindowActivated(QMdiSubWindow *sw)
286292
}
287293
}
288294

289-
void mbCoreWindowManager::showSubWindow(const QWidget *ui)
295+
void mbCoreWindowManager::showSubWindow(QMdiSubWindow *sw)
290296
{
291-
QMdiSubWindow *sw = m_hashWindows.value(ui);
292-
if (sw)
293-
{
294-
sw->show();
295-
sw->widget()->show();
296-
m_area->setActiveSubWindow(sw);
297-
}
297+
sw->show();
298+
sw->widget()->show();
299+
m_area->setActiveSubWindow(sw);
298300
}
299301

300-
void mbCoreWindowManager::closeSubWindow(const QWidget *ui)
302+
void mbCoreWindowManager::closeSubWindow(QMdiSubWindow *sw)
301303
{
302-
QMdiSubWindow *sw = m_hashWindows.value(ui);
303-
if (sw)
304-
sw->close();
304+
subWindowRemove(sw->widget());
305+
sw->deleteLater();
305306
}
307+
308+
bool mbCoreWindowManager::eventFilter(QObject *obj, QEvent *e)
309+
{
310+
switch (e->type())
311+
{
312+
case QEvent::Close:
313+
{
314+
QMdiSubWindow *sw = qobject_cast<QMdiSubWindow*>(obj);
315+
//Q_ASSERT (sw != nullptr);
316+
closeSubWindow(sw);
317+
return true;
318+
}
319+
default:
320+
break;
321+
}
322+
return QObject::eventFilter(obj, e);
323+
}
324+

src/core/gui/core_windowmanager.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class MB_EXPORT mbCoreWindowManager : public QObject
7171
virtual QString getMdiSubWindowNameWithPrefix(const QMdiSubWindow *sw) const;
7272

7373
public Q_SLOTS:
74-
void showDataViewUi(const mbCoreDataViewUi *ui);
74+
void showDataViewUi(mbCoreDataViewUi *ui);
7575
void actionWindowViewSubWindow();
7676
void actionWindowViewTabbed();
7777
void actionWindowDataViewCloseAll();
@@ -92,8 +92,6 @@ public Q_SLOTS:
9292

9393
Q_SIGNALS:
9494
void viewModeChanged(int viewMode);
95-
void dataViewWindowAdded(mbCoreDataViewUi *ui);
96-
void dataViewWindowRemoving(mbCoreDataViewUi *ui);
9795

9896
protected Q_SLOTS:
9997
void setProject(mbCoreProject *p);
@@ -104,18 +102,15 @@ protected Q_SLOTS:
104102
virtual void subWindowActivated(QMdiSubWindow *sw);
105103

106104
protected:
107-
void showSubWindow(const QWidget *ui);
108-
void closeSubWindow(const QWidget *ui);
105+
void showSubWindow(QMdiSubWindow *sw);
106+
virtual void closeSubWindow(QMdiSubWindow *sw);
107+
bool eventFilter(QObject *obj, QEvent *e) override;
109108

110109
protected:
111110
QMdiArea *m_area;
112111
mbCoreUi *m_ui;
113112
mbCoreDataViewManager *m_dataViewManager;
114113

115-
// DataViews
116-
typedef QList<QMdiSubWindow*> DataViews_t;
117-
DataViews_t m_dataViews;
118-
119114
// Windows
120115
typedef QHash<const QWidget*, QMdiSubWindow*> HashWindows_t;
121116
HashWindows_t m_hashWindows;

src/server/gui/project/server_projectui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void mbServerProjectUi::selectionChanged(const QItemSelection &selected, const Q
8181
{
8282
mbServerDeviceRef *ref = static_cast<mbServerProjectModel*>(m_model)->deviceRef(ls.first());
8383
if (ref)
84-
mbServer::global()->ui()->windowManager()->setActiveDevice(ref->device());
84+
Q_EMIT deviceClick(ref->device());
8585
}
8686
mbCoreProjectUi::selectionChanged(selected, deselected);
8787
}

src/server/gui/project/server_projectui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class mbServerProjectUi : public mbCoreProjectUi
4444
inline mbServerDeviceRef *selectedDeviceRef() const { return currentDeviceRef(); }
4545

4646
Q_SIGNALS:
47+
void deviceClick(mbServerDevice *device);
4748
void deviceDoubleClick(mbServerDeviceRef *device);
4849
void deviceContextMenu(mbServerDeviceRef *device);
4950

src/server/gui/server_ui.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,21 @@ void mbServerUi::initialize()
191191

192192
// Mdi Area
193193
m_deviceManager = new mbServerDeviceManager(this);
194-
connect(m_deviceManager, &mbServerDeviceManager::deviceUiContextMenu, this, &mbServerUi::contextMenuDevice);
194+
connect(m_deviceManager, &mbServerDeviceManager::deviceUiContextMenu, this, &mbServerUi::contextMenuDevice );
195+
connect(m_deviceManager, &mbServerDeviceManager::deviceUiAdd , this, &mbServerUi::deviceWindowAdd );
196+
connect(m_deviceManager, &mbServerDeviceManager::deviceUiRemove , this, &mbServerUi::deviceWindowRemove);
195197

196198
m_scriptManager = new mbServerScriptManager(this);
199+
connect(m_scriptManager, &mbServerScriptManager::scriptEditorAdd , this, &mbServerUi::scriptWindowAdd );
200+
connect(m_scriptManager, &mbServerScriptManager::scriptEditorRemove, this, &mbServerUi::scriptWindowRemove);
201+
197202
m_dataViewManager = new mbServerDataViewManager(this);
198203

199204
m_windowManager = new mbServerWindowManager(this, m_deviceManager, m_scriptManager, dataViewManager());
200-
connect(windowManager(), &mbServerWindowManager::deviceWindowAdded , this, &mbServerUi::deviceWindowAdd);
201-
connect(windowManager(), &mbServerWindowManager::deviceWindowRemoving, this, &mbServerUi::deviceWindowRemove);
202-
connect(windowManager(), &mbServerWindowManager::scriptWindowAdded , this, &mbServerUi::scriptWindowAdd);
203-
connect(windowManager(), &mbServerWindowManager::scriptWindowRemoving, this, &mbServerUi::scriptWindowRemove);
204205

205206
// Project Inspector
206207
m_projectUi = new mbServerProjectUi(ui->dockProject);
208+
connect(projectUi(), &mbServerProjectUi::deviceClick, windowManager(), &mbServerWindowManager::setActiveDevice);
207209
connect(projectUi(), &mbServerProjectUi::deviceDoubleClick, this, &mbServerUi::editDeviceRef );
208210
connect(projectUi(), &mbServerProjectUi::deviceContextMenu, this, &mbServerUi::contextMenuDeviceRef);
209211

src/server/gui/server_windowmanager.cpp

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,6 @@ mbServerDevice *mbServerWindowManager::activeDevice() const
150150
return m_deviceManager->activeDevice();
151151
}
152152

153-
void mbServerWindowManager::setActiveDevice(mbServerDevice *device)
154-
{
155-
mbServerDeviceUi *ui = m_deviceManager->deviceUi(device);
156-
if (ui)
157-
{
158-
QMdiSubWindow *sw = m_hashWindows.value(ui);
159-
if (sw)
160-
m_area->setActiveSubWindow(sw);
161-
}
162-
}
163-
164153
void mbServerWindowManager::showScriptModule(mbServerScriptModule *sm)
165154
{
166155
mbServerScriptModuleEditor *se = m_scriptManager->scriptModuleEditor(sm);
@@ -181,7 +170,11 @@ void mbServerWindowManager::showDeviceScript(mbServerDevice *device, mbServerDev
181170

182171
void mbServerWindowManager::showScriptEditor(mbServerBaseScriptEditor *scriptEditor)
183172
{
184-
showSubWindow(scriptEditor);
173+
QMdiSubWindow *sw = m_hashWindows.value(scriptEditor);
174+
if (sw)
175+
showSubWindow(sw);
176+
else
177+
scriptEditorAdd(scriptEditor);
185178
}
186179

187180
void mbServerWindowManager::setActiveScriptEditor(mbServerBaseScriptEditor *scriptEditor)
@@ -191,24 +184,39 @@ void mbServerWindowManager::setActiveScriptEditor(mbServerBaseScriptEditor *scri
191184
m_area->setActiveSubWindow(sw);
192185
}
193186

194-
void mbServerWindowManager::showDeviceUi(const mbServerDeviceUi *ui)
187+
void mbServerWindowManager::setActiveDevice(mbServerDevice *device)
188+
{
189+
mbServerDeviceUi *ui = m_deviceManager->deviceUi(device);
190+
if (ui)
191+
showDeviceUi(ui);
192+
}
193+
194+
void mbServerWindowManager::showDeviceUi(mbServerDeviceUi *ui)
195195
{
196-
showSubWindow(ui);
196+
QMdiSubWindow *sw = m_hashWindows.value(ui);
197+
if (sw)
198+
showSubWindow(sw);
199+
else
200+
deviceUiAdd(ui);
197201
}
198202

199203
void mbServerWindowManager::actionWindowDeviceCloseAll()
200204
{
201-
Q_FOREACH(QMdiSubWindow *sw, m_devices)
205+
Q_FOREACH(mbServerDeviceUi *ui, m_deviceManager->deviceUis())
202206
{
203-
sw->close();
207+
QMdiSubWindow *sw = m_hashWindows.value(ui);
208+
if (sw)
209+
closeSubWindow(sw);
204210
}
205211
}
206212

207213
void mbServerWindowManager::actionWindowScriptCloseAll()
208214
{
209-
Q_FOREACH(QMdiSubWindow *sw, m_scriptEditors)
215+
Q_FOREACH(mbServerBaseScriptEditor *ui, m_scriptManager->scriptEditors())
210216
{
211-
sw->close();
217+
QMdiSubWindow *sw = m_hashWindows.value(ui);
218+
if (sw)
219+
closeSubWindow(sw);
212220
}
213221
}
214222

@@ -222,10 +230,8 @@ void mbServerWindowManager::actionWindowCloseAll()
222230
void mbServerWindowManager::deviceUiAdd(mbServerDeviceUi *ui)
223231
{
224232
QMdiSubWindow* sw = subWindowAdd(ui);
225-
m_devices.append(sw);
226233
connect(ui, &mbServerDeviceUi::nameChanged, sw, &QWidget::setWindowTitle);
227234
sw->setWindowTitle(ui->name());
228-
Q_EMIT deviceWindowAdded(ui);
229235
}
230236

231237
void mbServerWindowManager::deviceUiRemove(mbServerDeviceUi *ui)
@@ -234,20 +240,15 @@ void mbServerWindowManager::deviceUiRemove(mbServerDeviceUi *ui)
234240
QMdiSubWindow* sw = subWindowRemove(ui);
235241
if (sw)
236242
{
237-
Q_EMIT deviceWindowRemoving(ui);
238-
m_devices.removeOne(sw);
239-
delete sw;
243+
sw->deleteLater();
240244
}
241245
}
242246

243247
void mbServerWindowManager::scriptEditorAdd(mbServerBaseScriptEditor *ui)
244248
{
245249
QMdiSubWindow* sw = subWindowAdd(ui);
246-
m_scriptEditors.append(sw);
247250
connect(ui, &mbServerBaseScriptEditor::nameChanged, sw, &QWidget::setWindowTitle);
248251
sw->setWindowTitle(ui->name());
249-
sw->installEventFilter(this);
250-
Q_EMIT scriptWindowAdded(ui);
251252
}
252253

253254
void mbServerWindowManager::scriptEditorRemove(mbServerBaseScriptEditor *ui)
@@ -256,30 +257,10 @@ void mbServerWindowManager::scriptEditorRemove(mbServerBaseScriptEditor *ui)
256257
QMdiSubWindow* sw = subWindowRemove(ui);
257258
if (sw)
258259
{
259-
Q_EMIT scriptWindowRemoving(ui);
260-
m_scriptEditors.removeOne(sw);
261260
sw->deleteLater();
262261
}
263262
}
264263

265-
bool mbServerWindowManager::eventFilter(QObject *obj, QEvent *e)
266-
{
267-
switch (e->type())
268-
{
269-
case QEvent::Close:
270-
{
271-
QMdiSubWindow *sw = qobject_cast<QMdiSubWindow*>(obj);
272-
//Q_ASSERT (sw != nullptr);
273-
mbServerBaseScriptEditor *ui = qobject_cast<mbServerBaseScriptEditor*>(sw->widget());
274-
m_scriptManager->removeScriptEditor(ui);
275-
return true;
276-
}
277-
default:
278-
break;
279-
}
280-
return QObject::eventFilter(obj, e);
281-
}
282-
283264
void mbServerWindowManager::subWindowActivated(QMdiSubWindow *sw)
284265
{
285266
if (sw)
@@ -302,3 +283,11 @@ void mbServerWindowManager::subWindowActivated(QMdiSubWindow *sw)
302283
mbCoreWindowManager::subWindowActivated(sw);
303284
}
304285
}
286+
287+
void mbServerWindowManager::closeSubWindow(QMdiSubWindow *sw)
288+
{
289+
if (mbServerBaseScriptEditor *ui = qobject_cast<mbServerBaseScriptEditor*>(sw->widget()))
290+
m_scriptManager->removeScriptEditor(ui);
291+
else
292+
mbCoreWindowManager::closeSubWindow(sw);
293+
}

0 commit comments

Comments
 (0)