From bdebed7d17bb4133d8dc3dcd02f7c995de940d05 Mon Sep 17 00:00:00 2001 From: Max Wilfinger Date: Sat, 24 Feb 2024 08:43:20 +0100 Subject: [PATCH] display current active document in main window title --- src/Gui/MDIView.cpp | 14 +++ src/Gui/MDIView.h | 3 + src/Gui/MainWindow.cpp | 198 ++++++++++++++++++++++--------------- src/Gui/MainWindow.h | 3 + src/Gui/StartupProcess.cpp | 26 +---- 5 files changed, 140 insertions(+), 104 deletions(-) diff --git a/src/Gui/MDIView.cpp b/src/Gui/MDIView.cpp index c24fa18ee1..21a38997eb 100644 --- a/src/Gui/MDIView.cpp +++ b/src/Gui/MDIView.cpp @@ -448,4 +448,18 @@ void MDIView::setCurrentViewMode(ViewMode mode) } } +QString MDIView::buildWindowTitle() +{ + QString windowTitle; + if (Gui::Document* document = getGuiDocument()) { + if (document->isModified()) { + windowTitle = QString::fromUtf8("* "); + } + + windowTitle.append(QString::fromUtf8(getAppDocument()->getName())); + } + + return windowTitle; +} + #include "moc_MDIView.cpp" diff --git a/src/Gui/MDIView.h b/src/Gui/MDIView.h index 724c2797ae..a190540b7f 100644 --- a/src/Gui/MDIView.h +++ b/src/Gui/MDIView.h @@ -74,6 +74,9 @@ public: void onRelabel(Gui::Document *pDoc) override; virtual void viewAll(); + /// build window title + QString buildWindowTitle(); + /// Message handler bool onMsg(const char* pMsg,const char** ppReturn) override; /// Message handler test diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 1a7a1c8cad..bd890c5738 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -1270,6 +1270,7 @@ void MainWindow::tabChanged(MDIView* view) { Q_UNUSED(view); updateActions(); + getMainWindow()->setWindowTitle(view->buildWindowTitle()); } void MainWindow::tabCloseRequested(int index) @@ -1665,6 +1666,7 @@ void MainWindow::_updateActions() Application::Instance->commandManager().testActive(); } d->actionUpdateDelay = 0; + getMainWindow()->setWindowTitle(activeWindow()->buildWindowTitle()); } void MainWindow::updateEditorActions() @@ -2368,6 +2370,7 @@ void MainWindow::changeEvent(QEvent *e) d->activeView = view; Application::Instance->viewActivated(view); } + getMainWindow()->setWindowTitle(view->buildWindowTitle()); } } } @@ -2509,97 +2512,132 @@ QMdiArea *MainWindow::getMdiArea() const return d->mdiArea; } -// ---------------------------------------------------------- - -StatusBarObserver::StatusBarObserver() - : WindowParameter("OutputWindow") +void MainWindow::setWindowTitle(const QString& string) { - msg = QString::fromLatin1("#statusBar{color: #000000}"); // black - wrn = QString::fromLatin1("#statusBar{color: #ffaa00}"); // orange - err = QString::fromLatin1("#statusBar{color: #ff0000}"); // red - Base::Console().AttachObserver(this); - getWindowParameter()->Attach(this); - getWindowParameter()->NotifyAll(); -} + QString title; + QString appname = + QString(static_cast(QCoreApplication::instance())->applicationName()); -StatusBarObserver::~StatusBarObserver() -{ - getWindowParameter()->Detach(this); - Base::Console().DetachObserver(this); -} + // allow to disable version number + ParameterGrp::handle hGen = +App::GetApplication().GetParameterGroupByPath( + "User parameter:BaseApp/Preferences/General"); + bool showVersion = hGen->GetBool("ShowVersionInTitle", true); -void StatusBarObserver::OnChange(Base::Subject &rCaller, const char * sReason) -{ - ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); - auto format = QString::fromLatin1("#statusBar{color: %1}"); - if (strcmp(sReason, "colorText") == 0) { - unsigned long col = rclGrp.GetUnsigned( sReason ); - this->msg = format.arg(App::Color::fromPackedRGB(col).name()); + if (showVersion) { + // set main window title with FreeCAD Version + auto config = App::Application::Config(); + QString major = QString::fromUtf8(config["BuildVersionMajor"].c_str()); + QString minor = QString::fromUtf8(config["BuildVersionMinor"].c_str()); + QString point = QString::fromUtf8(config["BuildVersionPoint"].c_str()); + QString suffix = QString::fromUtf8(config["BuildVersionSuffix"].c_str()); + title = QString::fromUtf8("%1 %2.%3.%4%5").arg(appname, major, minor, point, suffix); } - else if (strcmp(sReason, "colorWarning") == 0) { - unsigned long col = rclGrp.GetUnsigned( sReason ); - this->wrn = format.arg(App::Color::fromPackedRGB(col).name()); - } - else if (strcmp(sReason, "colorError") == 0) { - unsigned long col = rclGrp.GetUnsigned( sReason ); - this->err = format.arg(App::Color::fromPackedRGB(col).name()); - } - else if (strcmp(sReason, "colorCritical") == 0) { - unsigned long col = rclGrp.GetUnsigned( sReason ); - this->critical = format.arg(QColor((col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff).name()); - } -} - -void StatusBarObserver::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level, - Base::IntendedRecipient recipient, Base::ContentType content) -{ - (void) notifiername; - - // Do not log untranslated messages, or messages intended only to a developer to status bar - if( recipient == Base::IntendedRecipient::Developer || - content == Base::ContentType::Untranslated || - content == Base::ContentType::Untranslatable ) - return; - - int messageType = -1; - switch(level){ - case Base::LogStyle::Warning: - messageType = MainWindow::Wrn; - break; - case Base::LogStyle::Message: - messageType = MainWindow::Msg; - break; - case Base::LogStyle::Error: - messageType = MainWindow::Err; - break; - case Base::LogStyle::Log: - messageType = MainWindow::Log; - break; - case Base::LogStyle::Critical: - messageType = MainWindow::Critical; - break; - default: - break; + else { + title = appname; } - // Send the event to the main window to allow thread-safety. Qt will delete it when done. - auto ev = new CustomMessageEvent(messageType, QString::fromUtf8(msg.c_str())); - QApplication::postEvent(getMainWindow(), ev); + if (!string.isEmpty()) { + title = QString::fromUtf8("%1 - %2").arg(string, title); + } + + QMainWindow::setWindowTitle(title); } -// ------------------------------------------------------------- + // ---------------------------------------------------------- -int ActionStyleEvent::EventType = -1; + StatusBarObserver::StatusBarObserver() + : WindowParameter("OutputWindow") + { + msg = QString::fromLatin1("#statusBar{color: #000000}"); // black + wrn = QString::fromLatin1("#statusBar{color: #ffaa00}"); // orange + err = QString::fromLatin1("#statusBar{color: #ff0000}"); // red + Base::Console().AttachObserver(this); + getWindowParameter()->Attach(this); + getWindowParameter()->NotifyAll(); + } -ActionStyleEvent::ActionStyleEvent(Style type) - : QEvent(QEvent::Type(EventType)), type(type) -{ -} + StatusBarObserver::~StatusBarObserver() + { + getWindowParameter()->Detach(this); + Base::Console().DetachObserver(this); + } -ActionStyleEvent::Style ActionStyleEvent::getType() const -{ - return type; -} + void StatusBarObserver::OnChange(Base::Subject & rCaller, const char* sReason) + { + ParameterGrp& rclGrp = ((ParameterGrp&)rCaller); + auto format = QString::fromLatin1("#statusBar{color: %1}"); + if (strcmp(sReason, "colorText") == 0) { + unsigned long col = rclGrp.GetUnsigned(sReason); + this->msg = format.arg(App::Color::fromPackedRGB(col).name()); + } + else if (strcmp(sReason, "colorWarning") == 0) { + unsigned long col = rclGrp.GetUnsigned(sReason); + this->wrn = format.arg(App::Color::fromPackedRGB(col).name()); + } + else if (strcmp(sReason, "colorError") == 0) { + unsigned long col = rclGrp.GetUnsigned(sReason); + this->err = format.arg(App::Color::fromPackedRGB(col).name()); + } + else if (strcmp(sReason, "colorCritical") == 0) { + unsigned long col = rclGrp.GetUnsigned(sReason); + this->critical = format.arg( + QColor((col >> 24) & 0xff, (col >> 16) & 0xff, (col >> 8) & 0xff).name()); + } + } + + void StatusBarObserver::SendLog(const std::string& notifiername, + const std::string& msg, + Base::LogStyle level, + Base::IntendedRecipient recipient, + Base::ContentType content) + { + (void)notifiername; + + // Do not log untranslated messages, or messages intended only to a developer to status bar + if (recipient == Base::IntendedRecipient::Developer + || content == Base::ContentType::Untranslated + || content == Base::ContentType::Untranslatable) + return; + + int messageType = -1; + switch (level) { + case Base::LogStyle::Warning: + messageType = MainWindow::Wrn; + break; + case Base::LogStyle::Message: + messageType = MainWindow::Msg; + break; + case Base::LogStyle::Error: + messageType = MainWindow::Err; + break; + case Base::LogStyle::Log: + messageType = MainWindow::Log; + break; + case Base::LogStyle::Critical: + messageType = MainWindow::Critical; + break; + default: + break; + } + + // Send the event to the main window to allow thread-safety. Qt will delete it when done. + auto ev = new CustomMessageEvent(messageType, QString::fromUtf8(msg.c_str())); + QApplication::postEvent(getMainWindow(), ev); + } + + // ------------------------------------------------------------- + + int ActionStyleEvent::EventType = -1; + + ActionStyleEvent::ActionStyleEvent(Style type) + : QEvent(QEvent::Type(EventType)) + , type(type) + {} + + ActionStyleEvent::Style ActionStyleEvent::getType() const + { + return type; + } #include "moc_MainWindow.cpp" diff --git a/src/Gui/MainWindow.h b/src/Gui/MainWindow.h index dc5aee647d..299df4d1d7 100644 --- a/src/Gui/MainWindow.h +++ b/src/Gui/MainWindow.h @@ -266,6 +266,9 @@ public Q_SLOTS: void showMessage (const QString & message, int timeout = 0); + // Set main window title + void setWindowTitle(const QString& string); + protected: /** * This method checks if the main window can be closed by checking all open documents and views. diff --git a/src/Gui/StartupProcess.cpp b/src/Gui/StartupProcess.cpp index e294f7ce27..9c5e7e571b 100644 --- a/src/Gui/StartupProcess.cpp +++ b/src/Gui/StartupProcess.cpp @@ -237,30 +237,8 @@ void StartupPostProcess::execute() void StartupPostProcess::setWindowTitle() { - // allow to disable version number - ParameterGrp::handle hGen = - App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General"); - bool showVersion = hGen->GetBool("ShowVersionInTitle", true); - - QString appName = QCoreApplication::applicationName(); - if (appName.isEmpty()) { - appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str()); - } - if (showVersion) { - // set main window title with FreeCAD Version - std::map& config = App::Application::Config(); - QString major = QString::fromLatin1(config["BuildVersionMajor"].c_str()); - QString minor = QString::fromLatin1(config["BuildVersionMinor"].c_str()); - QString point = QString::fromLatin1(config["BuildVersionPoint"].c_str()); - QString suffix = QString::fromLatin1(config["BuildVersionSuffix"].c_str()); - QString title = QString::fromLatin1("%1 %2.%3.%4%5").arg( - appName, major, minor, point, suffix - ); - mainWindow->setWindowTitle(title); - } - else { - mainWindow->setWindowTitle(appName); - } + // empty window title QString sets default title (app + version) + mainWindow->setWindowTitle(QString()); } void StartupPostProcess::setProcessMessages()