From 6962a7bddbd22a2ca281f27d6930f91c28f840a3 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 23 Sep 2018 03:37:53 +0200 Subject: [PATCH] Ref T369, adjusted text message component to support settings --- .../components/textmessagecomponent.cpp | 116 ++++++++++++++---- .../components/textmessagecomponent.h | 42 +++++-- .../components/textmessagecomponent.ui | 63 +++++++--- src/blackgui/textmessagetextedit.cpp | 24 ++-- src/blackgui/textmessagetextedit.h | 17 +-- 5 files changed, 193 insertions(+), 69 deletions(-) diff --git a/src/blackgui/components/textmessagecomponent.cpp b/src/blackgui/components/textmessagecomponent.cpp index 163b98a8d..ea8bdb339 100644 --- a/src/blackgui/components/textmessagecomponent.cpp +++ b/src/blackgui/components/textmessagecomponent.cpp @@ -7,32 +7,31 @@ * contained in the LICENSE file. */ -#include "blackcore/application.h" -#include "blackcore/context/contextaudio.h" -#include "blackcore/context/contextnetwork.h" -#include "blackcore/context/contextownaircraft.h" -#include "blackcore/corefacade.h" +#include "ui_textmessagecomponent.h" #include "blackgui/components/textmessagecomponent.h" +#include "blackgui/views/textmessageview.h" #include "blackgui/dockwidgetinfoarea.h" #include "blackgui/guiapplication.h" #include "blackgui/textmessagetextedit.h" -#include "blackgui/views/textmessageview.h" -#include "blackgui/views/viewbase.h" -#include "blackmisc/verify.h" +#include "blackcore/context/contextaudio.h" +#include "blackcore/context/contextnetwork.h" +#include "blackcore/context/contextownaircraft.h" +#include "blackcore/application.h" +#include "blackcore/corefacade.h" #include "blackmisc/audio/notificationsounds.h" +#include "blackmisc/network/textmessage.h" +#include "blackmisc/network/user.h" #include "blackmisc/aviation/atcstation.h" #include "blackmisc/aviation/callsign.h" #include "blackmisc/aviation/comsystem.h" -#include "blackmisc/compare.h" -#include "blackmisc/iterator.h" -#include "blackmisc/logmessage.h" -#include "blackmisc/network/textmessage.h" -#include "blackmisc/network/user.h" #include "blackmisc/pq/constants.h" #include "blackmisc/pq/frequency.h" #include "blackmisc/pq/units.h" +#include "blackmisc/compare.h" +#include "blackmisc/iterator.h" +#include "blackmisc/logmessage.h" #include "blackmisc/sequence.h" -#include "ui_textmessagecomponent.h" +#include "blackmisc/verify.h" #include #include @@ -72,15 +71,32 @@ namespace BlackGui // le_textMessages is the own line edit bool c = connect(ui->le_textMessages, &QLineEdit::returnPressed, this, &CTextMessageComponent::textMessageEntered); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); + c = connect(ui->gb_Settings, &QGroupBox::toggled, this, &CTextMessageComponent::onSettingsChecked); + Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); c = connect(sGui->getIContextOwnAircraft(), &IContextOwnAircraft::changedAircraftCockpit, this, &CTextMessageComponent::onChangedAircraftCockpit); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); + // style sheet + c = connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CTextMessageComponent::onStyleSheetChanged, Qt::QueuedConnection); + Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); + c = connect(ui->comp_SettingsStyle, &CSettingsTextMessageStyle::changed, this, &CTextMessageComponent::updateSettings, Qt::QueuedConnection); + Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); + if (sGui && sGui->getCoreFacade()) { c = connect(this, &CTextMessageComponent::commandEntered, sGui->getCoreFacade(), &CCoreFacade::parseCommandLine); Q_ASSERT_X(c, Q_FUNC_INFO, "Missing connect"); } Q_UNUSED(c); + + // init by settings + QPointer myself(this); + QTimer::singleShot(250, this, [ = ] + { + // init decoupled when sub components are fully init + if (!myself) { return; } + this->onSettingsChanged(); + }); } CTextMessageComponent::~CTextMessageComponent() @@ -98,9 +114,9 @@ namespace BlackGui { switch (tab) { - case TextMessagesAll: return ui->tb_TextMessagesAll; - case TextMessagesCom1: return ui->tb_TextMessagesCOM1; - case TextMessagesCom2: return ui->tb_TextMessagesCOM2; + case TextMessagesAll: return ui->tb_TextMessagesAll; + case TextMessagesCom1: return ui->tb_TextMessagesCOM1; + case TextMessagesCom2: return ui->tb_TextMessagesCOM2; case TextMessagesUnicom: return ui->tb_TextMessagesUnicom; default: Q_ASSERT_X(false, Q_FUNC_INFO, "Wrong index"); @@ -109,9 +125,16 @@ namespace BlackGui return nullptr; } + CTextMessageTextEdit *CTextMessageComponent::getTextEdit(CTextMessageComponent::Tab tab) const + { + QWidget *w = this->getTabWidget(tab); + if (!w) { return nullptr; } + return this->findChild(); + } + void CTextMessageComponent::selectTabWidget(CTextMessageComponent::Tab tab) { - QWidget *w = getTabWidget(tab); + QWidget *w = this->getTabWidget(tab); if (w) { ui->tw_TextMessages->setCurrentWidget(w); @@ -191,8 +214,8 @@ namespace BlackGui emit this->displayInInfoWindow(CVariant::from(message), 5 * 1000); } } - } - } + } // message + } // for } void CTextMessageComponent::onChangedAircraftCockpit() @@ -200,6 +223,55 @@ namespace BlackGui this->showCurrentFrequenciesFromCockpit(); } + void CTextMessageComponent::onSettingsChecked(bool checked) + { + ui->comp_SettingsOverlay->setVisible(checked); + ui->comp_SettingsStyle->setVisible(checked); + ui->gb_Settings->setFlat(!checked); + } + + void CTextMessageComponent::onSettingsChanged() + { + QList textEdits = this->findAllTextEdit(); + const QString style = this->getStyleSheet(); + for (CTextMessageTextEdit *textEdit : textEdits) + { + if (textEdit->hasTextDocument()) + { + textEdit->setStyleSheetForContent(style); + } + } + ui->comp_SettingsStyle->setStyle(this->getStyleSheet()); + } + + void CTextMessageComponent::onStyleSheetChanged() + { + this->onSettingsChanged(); + } + + void CTextMessageComponent::updateSettings() + { + const QString style = ui->comp_SettingsStyle->getStyle(); + CTextMessageSettings s = m_messageSettings.get(); + s.setStyleSheet(style); + const CStatusMessage m = m_messageSettings.setAndSave(s); + CLogMessage::preformatted(m); + this->onStyleSheetChanged(); + } + + QList CTextMessageComponent::findAllTextEdit() const + { + return this->findChildren(); + } + + QString CTextMessageComponent::getStyleSheet() const + { + const QString styleSheet = m_messageSettings.get().getStyleSheet(); + return styleSheet.isEmpty() && sGui ? + sGui->getStyleSheetUtility().style(CStyleSheetUtility::fileNameTextMessage()) : + styleSheet; + } + bool CTextMessageComponent::isCorrespondingTextMessageTabSelected(CTextMessage textMessage) const { if (!this->isVisibleWidget()) { return false; } @@ -233,7 +305,7 @@ namespace BlackGui bool CTextMessageComponent::isNetworkConnected() const { - return sGui->getIContextNetwork() && sGui->getIContextNetwork()->isConnected() ; + return sGui && sGui->getIContextNetwork() && sGui->getIContextNetwork()->isConnected(); } void CTextMessageComponent::showCurrentFrequenciesFromCockpit() @@ -253,7 +325,7 @@ namespace BlackGui QWidget *CTextMessageComponent::addNewTextMessageTab(const CCallsign &callsign) { Q_ASSERT(!callsign.isEmpty()); - return addNewTextMessageTab(callsign.asString()); + return this->addNewTextMessageTab(callsign.asString()); } QWidget *CTextMessageComponent::addNewTextMessageTab(const QString &tabName) diff --git a/src/blackgui/components/textmessagecomponent.h b/src/blackgui/components/textmessagecomponent.h index 53c2956b5..60e9358f9 100644 --- a/src/blackgui/components/textmessagecomponent.h +++ b/src/blackgui/components/textmessagecomponent.h @@ -12,9 +12,9 @@ #ifndef BLACKGUI_COMPONENTS_TEXTMESSAGECOMPONENT_H #define BLACKGUI_COMPONENTS_TEXTMESSAGECOMPONENT_H -#include "blackgui/blackguiexport.h" #include "blackgui/settings/textmessagesettings.h" #include "blackgui/components/enablefordockwidgetinfoarea.h" +#include "blackgui/blackguiexport.h" #include "blackmisc/simulation/simulatedaircraft.h" #include "blackmisc/identifier.h" #include "blackmisc/variant.h" @@ -28,6 +28,8 @@ namespace Ui { class CTextMessageComponent; } namespace BlackGui { class CDockWidgetInfoArea; + class CTextMessageTextEdit; + namespace Components { //! Text message widget @@ -56,14 +58,6 @@ namespace BlackGui //! \copydoc CEnableForDockWidgetInfoArea::setParentDockWidgetInfoArea virtual bool setParentDockWidgetInfoArea(CDockWidgetInfoArea *parentDockableWidget) override; - signals: - //! Message to be displayed in info window - void displayInInfoWindow(const BlackMisc::CVariant &message, int displayDurationMs) const; - - //! Command line was entered - void commandEntered(const QString commandLine, const BlackMisc::CIdentifier &originator); - - public slots: //! Text messages received void onTextMessageReceived(const BlackMisc::Network::CTextMessageList &messages); @@ -77,14 +71,24 @@ namespace BlackGui //! Display the tab for given callsign void showCorrespondingTab(const BlackMisc::Aviation::CCallsign &callsign); + signals: + //! Message to be displayed in info window + void displayInInfoWindow(const BlackMisc::CVariant &message, int displayDurationMs) const; + + //! Command line was entered + void commandEntered(const QString commandLine, const BlackMisc::CIdentifier &originator); + private: QScopedPointer ui; BlackMisc::CIdentifier m_identifier { "TextMessageComponent", this }; - BlackMisc::CSettingReadOnly m_messageSettings { this }; + BlackMisc::CSetting m_messageSettings { this, &CTextMessageComponent::onSettingsChanged }; //! Enum to widget QWidget *getTabWidget(Tab tab) const; + //! Related text edit + CTextMessageTextEdit *getTextEdit(Tab tab) const; + //! Select given tab void selectTabWidget(Tab tab); @@ -131,6 +135,24 @@ namespace BlackGui //! Cockpit values changed, used to updated some components void onChangedAircraftCockpit(); + //! Settings have been checked (group box visible/invisible) + void onSettingsChecked(bool checked); + + //! Settings have been changed + void onSettingsChanged(); + + //! Style sheet has been changed + void onStyleSheetChanged(); + + //! Update settings + void updateSettings(); + + //! Get all CTextMessageTextEdit child objects + QList findAllTextEdit() const; + + //! Global or settings stylesheet + QString getStyleSheet() const; + //! Close text message tab void closeTextMessageTab(); diff --git a/src/blackgui/components/textmessagecomponent.ui b/src/blackgui/components/textmessagecomponent.ui index aa48a05b1..e307fdbc2 100644 --- a/src/blackgui/components/textmessagecomponent.ui +++ b/src/blackgui/components/textmessagecomponent.ui @@ -6,25 +6,16 @@ 0 0 - 400 - 300 + 260 + 297 Text messages - - QFrame::NoFrame - - - QFrame::Plain - - - 0 - - 1 + 5 0 @@ -36,7 +27,7 @@ 0 - 0 + 5 @@ -231,13 +222,46 @@ - + + + Settings + + + true + + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + + + + + + + command line (e.g. ".com1", ".x", ".msg com1") + + text message text goes here + @@ -264,14 +288,21 @@ QLineEdit
blackgui/lineedithistory.h
+ + BlackGui::Components::CSettingsTextMessageStyle + QFrame +
blackgui/components/settingstextmessagestyle.h
+ 1 +
tw_TextMessages - le_textMessages tvp_TextMessagesAll - tep_TextMessagesUnicom + gb_Settings + le_textMessages tep_TextMessagesCOM1 tep_TextMessagesCOM2 + tep_TextMessagesUnicom diff --git a/src/blackgui/textmessagetextedit.cpp b/src/blackgui/textmessagetextedit.cpp index 0b58293de..8f4d4ec5b 100644 --- a/src/blackgui/textmessagetextedit.cpp +++ b/src/blackgui/textmessagetextedit.cpp @@ -8,7 +8,6 @@ */ #include "blackgui/guiapplication.h" -#include "blackgui/stylesheetutility.h" #include "blackgui/textmessagetextedit.h" #include "blackmisc/aviation/callsign.h" #include "blackmisc/network/textmessage.h" @@ -20,6 +19,7 @@ #include #include #include +#include using namespace BlackMisc; using namespace BlackMisc::Network; @@ -55,10 +55,6 @@ namespace BlackGui connect(m_actionWordWrap, &QAction::triggered, this, &CTextMessageTextEdit::setWordWrap); connect(this, &QTextEdit::customContextMenuRequested, this, &CTextMessageTextEdit::showContextMenuForTextEdit); - - // style sheet - connect(sGui, &CGuiApplication::styleSheetsChanged, this, &CTextMessageTextEdit::onStyleSheetChanged, Qt::QueuedConnection); - onStyleSheetChanged(); } CTextMessageTextEdit::~CTextMessageTextEdit() @@ -75,7 +71,7 @@ namespace BlackGui { m_messages.push_front(textMessage); } - QString html(toHtml(m_messages, m_withSender, m_withRecipient)); + const QString html(toHtml(m_messages, m_withSender, m_withRecipient)); m_textDocument->setHtml(html); } @@ -90,9 +86,18 @@ namespace BlackGui QTextEdit::clear(); } + void CTextMessageTextEdit::redrawHtml() + { + Q_ASSERT_X(m_textDocument, Q_FUNC_INFO, "Missing text document"); + const QString html(toHtml(m_messages, m_withSender, m_withRecipient)); + m_textDocument->setHtml(html); + } + void CTextMessageTextEdit::setStyleSheetForContent(const QString &styleSheet) { + Q_ASSERT_X(m_textDocument, Q_FUNC_INFO, "Missing text document"); m_textDocument->setDefaultStyleSheet(styleSheet); + this->redrawHtml(); } QString CTextMessageTextEdit::toHtml(const CTextMessageList &messages, bool withFrom, bool withTo) @@ -172,13 +177,6 @@ namespace BlackGui menu->exec(this->mapToGlobal(pt)); } - void CTextMessageTextEdit::onStyleSheetChanged() - { - Q_ASSERT(m_textDocument); - QString style(sGui->getStyleSheetUtility().style(CStyleSheetUtility::fileNameTextMessage())); - m_textDocument->setDefaultStyleSheet(style); - } - void CTextMessageTextEdit::keepLastNMessages() { QObject *sender = QObject::sender(); diff --git a/src/blackgui/textmessagetextedit.h b/src/blackgui/textmessagetextedit.h index 98a55788e..5e7381e6c 100644 --- a/src/blackgui/textmessagetextedit.h +++ b/src/blackgui/textmessagetextedit.h @@ -22,7 +22,6 @@ class QAction; class QPoint; class QTextDocument; -class QWidget; namespace BlackMisc { namespace Network { class CTextMessage; } } namespace BlackGui @@ -46,17 +45,22 @@ namespace BlackGui //! Number of displayed messages int count() const; - public slots: + //! Stylesheet for content + void setStyleSheetForContent(const QString &styleSheet); + //! Clear void clear(); + //! Has text document + bool hasTextDocument() const { return m_textDocument; } + + //! Redraw HTML + void redrawHtml(); + private: //! Context menu void showContextMenuForTextEdit(const QPoint &pt); - //! Stylesheet has been changed - void onStyleSheetChanged(); - //! Keep last n messages void keepLastNMessages(); @@ -69,9 +73,6 @@ namespace BlackGui //! Convert to HTML static QString toHtml(const BlackMisc::Network::CTextMessage &message, bool withFrom, bool withTo); - //! Stylesheet for content - void setStyleSheetForContent(const QString &styleSheet); - //! Word wrap void setWordWrap(bool wordWrap);