Commit d8c8397f authored by Emmanuel Promayon's avatar Emmanuel Promayon
Browse files

NEW view menu has now a central viewer switcher

When there is more than one central viewer, the "View" menu
shows actions to switch from one central viewer to the other.
parent 4babb1d9
...@@ -481,6 +481,10 @@ void ImpMainWindow::openDirectory(QAction* emitter) { ...@@ -481,6 +481,10 @@ void ImpMainWindow::openDirectory(QAction* emitter) {
// ------------- updateViewMenu ----------------- // ------------- updateViewMenu -----------------
void ImpMainWindow::updateViewMenu() { void ImpMainWindow::updateViewMenu() {
// From Qt documentation:
// "Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted"
// I.e., the central viewer toggle action are own by viewMenu (they are created and used here), they will
// therefore be no memory leak
viewMenu->clear(); viewMenu->clear();
// insert viewers menu // insert viewers menu
...@@ -495,31 +499,41 @@ void ImpMainWindow::updateViewMenu() { ...@@ -495,31 +499,41 @@ void ImpMainWindow::updateViewMenu() {
// change the central viewer if there is more than one viewer in the central area // change the central viewer if there is more than one viewer in the central area
if (qobject_cast<QStackedWidget*>(centralWidget())->count() > 1) { if (qobject_cast<QStackedWidget*>(centralWidget())->count() > 1) {
viewMenu->addSeparator()->setText(tr("Toggle Central Viewers")); viewMenu->addSeparator()->setText(tr("Toggle Central Viewers"));
for(Viewer *v : viewers) { // for(Viewer *v : viewers) {
int i = 0; // int i = 0;
// beware that getWidget(..) requires a parent and as the widget was setup inside centralWidget()
// it was reparented to centralWidget() → use getWidget(centralWidget()) and not getWidget(this)
// in order not to modify the central widget parenting
while (i<qobject_cast<QStackedWidget*>(centralWidget())->count() && qobject_cast<QStackedWidget*>(centralWidget())->widget(i)!=v->getWidget(centralWidget()))
i++;
if (i<qobject_cast<QStackedWidget*>(centralWidget())->count()) {
viewMenu->addAction(v->objectName());
}
}
// // add a toggle action
// for (int i = 0; i < qobject_cast<QStackedWidget*>(centralWidget())->count(); i++) {
// QWidget* cWidget = qobject_cast<QStackedWidget*>(centralWidget())->widget(i);
// // get the viewer associated with the widget in the central area
// // beware that getWidget(..) requires a parent and as the widget was setup inside centralWidget() // // beware that getWidget(..) requires a parent and as the widget was setup inside centralWidget()
// // it was reparented to centralWidget() → use getWidget(centralWidget()) and not getWidget(this) // // it was reparented to centralWidget() → use getWidget(centralWidget()) and not getWidget(this)
// // in order not to modify the central widget parenting // // in order not to modify the central widget parenting
// auto it = std::find_if(viewers.begin(), viewers.end(), [cWidget,centralWidget=centralWidget()](Viewer * v) { // while (i<qobject_cast<QStackedWidget*>(centralWidget())->count() && qobject_cast<QStackedWidget*>(centralWidget())->widget(i)!=v->getWidget(centralWidget()))
// return v->getWidget(centralWidget) == cWidget; // i++;
// }); // if (i<qobject_cast<QStackedWidget*>(centralWidget())->count()) {
// Viewer* cViewer = (*it); // viewMenu->addAction(v->objectName());
// viewMenu->addAction(cViewer->objectName());
// } // }
// }
// add a toggle action
for (int i = 0; i < qobject_cast<QStackedWidget*>(centralWidget())->count(); i++) {
QWidget* cWidget = qobject_cast<QStackedWidget*>(centralWidget())->widget(i);
// get the viewer associated with the widget in the central area
// Beware that getWidget(..) requires a parent and as the widget was setup inside centralWidget()
// it was reparented to centralWidget() → use getWidget(centralWidget()) and not getWidget(this)
// in order not to modify the central widget parenting
auto it = std::find_if(viewers.begin(), viewers.end(), [&](Viewer * v) {
return v->getWidget(centralWidget()) == cWidget;
});
Viewer* cViewer = (*it);
// viewerAction is own by viewMenu, viewMenu->clear() will delete it
QAction *viewerAction = viewMenu->addAction(cViewer->objectName());
viewerAction->setCheckable(true);
viewerAction->setChecked(cViewer == centralViewer);
QString tipString = "Hide/show the " + cViewer->objectName() + " central viewer";
viewerAction->setStatusTip(tr(tipString.toStdString().c_str()));
viewerAction->setWhatsThis(tr(tipString.toStdString().c_str()));
// add the toggle action slot using C++11 lambda so that everything is contained inside viewMenu
connect(viewerAction, &QAction::toggled, [=](bool) {
setCentralViewer(cViewer);
});
}
} }
// insert viewers on/off actions // insert viewers on/off actions
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment