Skip to content
Snippets Groups Projects

MainWindow: Addend bandage fix to keep MainWindow open when the docked OpenGl...

Merged Ludwig Jaeck requested to merge 432-glcontext into main
All threads resolved!
1 file
+ 80
48
Compare changes
  • Side-by-side
  • Inline
+ 80
48
@@ -31,29 +31,38 @@
#include <QButtonGroup>
#include <QCloseEvent>
#include <QMessageBox>
#include <QOpenGLWidget>
#include <QProgressBar>
#include <QPushButton>
#include <QSettings>
#include <QStackedLayout>
#include <QStatusBar>
#include <QToolButton>
#include <QOpenGLWidget>
MainWindow::MainWindow()
: QMainWindow(nullptr), m_progressBar(new QProgressBar), m_viewSelectionButtons(new QButtonGroup(this)),
m_viewsStack(new QStackedLayout), m_viewSelectionButtonsLayout(new QVBoxLayout),
m_projectManager(new ProjectManager(this)), m_actionManager(new ActionManager(this)), m_welcomeView(nullptr),
m_instrumentView(nullptr), m_sampleView(nullptr), m_importDataView(nullptr), m_simulationView(nullptr),
m_jobView(nullptr) {
auto *centralWidget = new QWidget(this);
auto *mainLayout = new QHBoxLayout(centralWidget);
: QMainWindow(nullptr)
, m_progressBar(new QProgressBar)
, m_viewSelectionButtons(new QButtonGroup(this))
, m_viewsStack(new QStackedLayout)
, m_viewSelectionButtonsLayout(new QVBoxLayout)
, m_projectManager(new ProjectManager(this))
, m_actionManager(new ActionManager(this))
, m_welcomeView(nullptr)
, m_instrumentView(nullptr)
, m_sampleView(nullptr)
, m_importDataView(nullptr)
, m_simulationView(nullptr)
, m_jobView(nullptr)
{
auto* centralWidget = new QWidget(this);
auto* mainLayout = new QHBoxLayout(centralWidget);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
m_viewSelectionButtonsLayout->setContentsMargins(0, 0, 0, 0);
m_viewSelectionButtonsLayout->setSpacing(0);
auto *fillerButton = createViewSelectionButton();
auto* fillerButton = createViewSelectionButton();
fillerButton->setMinimumSize(5, 5);
fillerButton->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
fillerButton->setEnabled(false);
@@ -61,7 +70,7 @@ MainWindow::MainWindow()
connect(m_viewSelectionButtons, &QButtonGroup::idClicked, this, &MainWindow::raiseView);
auto *vlayout = new QVBoxLayout;
auto* vlayout = new QVBoxLayout;
vlayout->setContentsMargins(0, 0, 0, 0);
vlayout->setSpacing(0);
vlayout->addLayout(m_viewsStack);
@@ -70,10 +79,11 @@ MainWindow::MainWindow()
// For this reason, the default surface format of the OpenGL widget and the QMainWindow
// probably don't match. Because of the instantiation order, the default surface format
// of the QMainWindow is used. This is a problem if the OpenGL widget is created later.
// The underlying windowing system, like X11, will likely move the MainWindow into the new context.
// This is the reason why the MainWindow is getting closed and reopened.
// To avoid this, because the current OpenGLWidget is getting created only after a project is created/loaded,
// this empty OpenGLWidget is created here to ensure that the MainWindow is assigned to the correct context on initialization.
// The underlying windowing system, like X11, will likely move the MainWindow into the new
// context. This is the reason why the MainWindow is getting closed and reopened. To avoid this,
// because the current OpenGLWidget is getting created only after a project is created/loaded,
// this empty OpenGLWidget is created here to ensure that the MainWindow is assigned to the
// correct context on initialization.
vlayout->addWidget(new QOpenGLWidget());
mainLayout->addLayout(m_viewSelectionButtonsLayout);
@@ -106,24 +116,29 @@ MainWindow::MainWindow()
MainWindow::~MainWindow() = default;
QProgressBar *MainWindow::progressBar() {
QProgressBar* MainWindow::progressBar()
{
return m_progressBar;
}
ProjectManager *MainWindow::projectManager() {
ProjectManager* MainWindow::projectManager()
{
return m_projectManager;
}
QWidget *MainWindow::currentView() const {
QWidget* MainWindow::currentView() const
{
return m_viewsStack->currentWidget();
}
void MainWindow::setCurrentView(int viewId) {
if (auto *btn = m_viewSelectionButtons->button(viewId); btn != nullptr)
void MainWindow::setCurrentView(int viewId)
{
if (auto* btn = m_viewSelectionButtons->button(viewId); btn != nullptr)
btn->click();
}
void MainWindow::raiseView(int viewId) {
void MainWindow::raiseView(int viewId)
{
if (gProjectDocument.has_value() && viewId != WELCOME)
gProjectDocument.value()->setViewId(viewId);
if (m_viewsStack->currentIndex() != viewId) {
@@ -132,12 +147,13 @@ void MainWindow::raiseView(int viewId) {
}
}
void MainWindow::updateTitle() {
void MainWindow::updateTitle()
{
auto const doc = gProjectDocument;
QString location = "not saved yet";
if (doc.has_value() && doc.value()->hasValidNameAndPath())
location = GUI::Util::Path::withTildeHomePath(
QDir::toNativeSeparators(doc.value()->projectFullPath()));
QDir::toNativeSeparators(doc.value()->projectFullPath()));
if (!doc.has_value())
setWindowTitle("BornAgain");
else if (doc.value()->isModified())
@@ -146,26 +162,30 @@ void MainWindow::updateTitle() {
setWindowTitle("BornAgain - " + doc.value()->projectName() + " [" + location + "]");
}
void MainWindow::onFocusRequest(int index) {
void MainWindow::onFocusRequest(int index)
{
m_viewSelectionButtons->button(index)->click();
}
void MainWindow::openRecentProject() {
if (const auto *action = qobject_cast<const QAction *>(sender())) {
void MainWindow::openRecentProject()
{
if (const auto* action = qobject_cast<const QAction*>(sender())) {
auto file = action->data().value<QString>();
m_projectManager->openProject(file);
}
}
void MainWindow::onRunSimulationShortcut() {
void MainWindow::onRunSimulationShortcut()
{
// This clearFocus is needed for the propagation of the current editor value,
// since the simulate method will only change focus after finishing the simulation
if (auto *widget = QApplication::focusWidget())
if (auto* widget = QApplication::focusWidget())
widget->clearFocus();
m_simulationView->simulate();
}
void MainWindow::closeEvent(QCloseEvent *event) {
void MainWindow::closeEvent(QCloseEvent* event)
{
if (gProjectDocument.has_value() && gProjectDocument.value()->jobModel()->hasUnfinishedJobs()) {
QMessageBox::warning(this, "Cannot quit the application.",
"Cannot quit the application while jobs are running.\n"
@@ -181,7 +201,8 @@ void MainWindow::closeEvent(QCloseEvent *event) {
}
}
void MainWindow::initApplication() {
void MainWindow::initApplication()
{
setDockNestingEnabled(true);
setAcceptDrops(true);
@@ -189,7 +210,8 @@ void MainWindow::initApplication() {
setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
}
void MainWindow::initProgressBar() {
void MainWindow::initProgressBar()
{
m_progressBar->hide();
m_progressBar->setTextVisible(false);
m_progressBar->setFixedHeight(QApplication::fontMetrics().boundingRect("M").height());
@@ -197,7 +219,8 @@ void MainWindow::initProgressBar() {
m_viewSelectionButtonsLayout->addWidget(m_progressBar);
}
void MainWindow::initViews() {
void MainWindow::initViews()
{
if (m_welcomeView == nullptr) {
m_welcomeView = new WelcomeView(this);
addView(ViewId::WELCOME, QIcon(":/images/main_welcomeview.svg"), "Welcome",
@@ -205,7 +228,7 @@ void MainWindow::initViews() {
}
if (gProjectDocument.has_value()) {
auto *doc = gProjectDocument.value();
auto* doc = gProjectDocument.value();
m_instrumentView = new InstrumentView(this, doc);
m_sampleView = new SampleView(this, doc);
m_importDataView = new ImportDataView(this, doc);
@@ -236,7 +259,8 @@ void MainWindow::initViews() {
}
}
void MainWindow::readSettings() {
void MainWindow::readSettings()
{
QSettings settings;
if (settings.childGroups().contains(GUI::Constants::S_MAIN_WINDOW)) {
settings.beginGroup(GUI::Constants::S_MAIN_WINDOW);
@@ -247,7 +271,8 @@ void MainWindow::readSettings() {
m_projectManager->readSettings();
}
void MainWindow::writeSettings() {
void MainWindow::writeSettings()
{
QSettings settings;
settings.beginGroup(GUI::Constants::S_MAIN_WINDOW);
settings.setValue(GUI::Constants::S_WINDOW_SIZE, size());
@@ -257,9 +282,10 @@ void MainWindow::writeSettings() {
settings.sync();
}
void MainWindow::addView(ViewId id, const QIcon &icon, const QString &title, const QString &tooltip,
QWidget *view) {
QToolButton *btn = createViewSelectionButton();
void MainWindow::addView(ViewId id, const QIcon& icon, const QString& title, const QString& tooltip,
QWidget* view)
{
QToolButton* btn = createViewSelectionButton();
m_viewSelectionButtonsLayout->insertWidget(id, btn);
btn->setText(title);
@@ -272,7 +298,8 @@ void MainWindow::addView(ViewId id, const QIcon &icon, const QString &title, con
m_viewsStack->insertWidget(id, view);
}
void MainWindow::updateViewSelectionButtonsGeometry() const {
void MainWindow::updateViewSelectionButtonsGeometry() const
{
if (m_viewSelectionButtons->buttons().isEmpty())
return;
@@ -281,7 +308,7 @@ void MainWindow::updateViewSelectionButtonsGeometry() const {
// Find the maximum text extents
int maxTextWidth = 0;
int maxTextHeight = 0;
for (auto *b: m_viewSelectionButtons->buttons()) {
for (auto* b : m_viewSelectionButtons->buttons()) {
const auto r = fontMetrics.boundingRect(b->text());
maxTextWidth = std::max(maxTextWidth, r.width());
maxTextHeight = std::max(maxTextHeight, r.height());
@@ -296,20 +323,21 @@ void MainWindow::updateViewSelectionButtonsGeometry() const {
const int iconExtent = buttonExtent - margin - maxTextHeight;
// set new values in all buttons
for (auto *b: m_viewSelectionButtons->buttons()) {
for (auto* b : m_viewSelectionButtons->buttons()) {
b->setFixedSize(buttonExtent, buttonExtent);
b->setIconSize({iconExtent, iconExtent});
}
// set fixed width in filler and progress bar
auto *filler = m_viewSelectionButtonsLayout->itemAt(m_viewSelectionButtons->buttons().size());
auto* filler = m_viewSelectionButtonsLayout->itemAt(m_viewSelectionButtons->buttons().size());
if (filler)
if (auto *fillerBtn = dynamic_cast<QToolButton *>(filler->widget()); fillerBtn)
if (auto* fillerBtn = dynamic_cast<QToolButton*>(filler->widget()); fillerBtn)
fillerBtn->setFixedWidth(buttonExtent);
m_progressBar->setFixedWidth(buttonExtent);
}
void MainWindow::onDocumentOpenedOrClosed(bool open) {
void MainWindow::onDocumentOpenedOrClosed(bool open)
{
initViews();
updateTitle();
if (open) {
@@ -320,11 +348,13 @@ void MainWindow::onDocumentOpenedOrClosed(bool open) {
}
}
void MainWindow::onDocumentModified() {
void MainWindow::onDocumentModified()
{
updateTitle();
}
void MainWindow::onAboutToCloseDocument() {
void MainWindow::onAboutToCloseDocument()
{
setCurrentView(WELCOME);
while (m_viewSelectionButtons->buttons().size() > 1)
@@ -348,14 +378,16 @@ void MainWindow::onAboutToCloseDocument() {
m_jobView = nullptr;
}
QToolButton *MainWindow::createViewSelectionButton() const {
auto *btn = new QToolButton;
QToolButton* MainWindow::createViewSelectionButton() const
{
auto* btn = new QToolButton;
btn->setObjectName("ViewSelectionButton"); // for addressing in style sheet
btn->setCheckable(true);
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
return btn;
}
void MainWindow::loadProject(QString projectPath) {
void MainWindow::loadProject(QString projectPath)
{
m_projectManager->openProject(projectPath);
}
Loading