diff --git a/ApplicationCode/UserInterface/CMakeLists_files.cmake b/ApplicationCode/UserInterface/CMakeLists_files.cmake index 0c06b7dd46..43b607be47 100644 --- a/ApplicationCode/UserInterface/CMakeLists_files.cmake +++ b/ApplicationCode/UserInterface/CMakeLists_files.cmake @@ -190,6 +190,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiuFileDialogTools.cpp ${CMAKE_CURRENT_LIST_DIR}/RiuGuiTheme.cpp ${CMAKE_CURRENT_LIST_DIR}/RiuQssSyntaxHighlighter.cpp ${CMAKE_CURRENT_LIST_DIR}/RiuTextEditWithCompletion.cpp +${CMAKE_CURRENT_LIST_DIR}/RiuTextContentFrame.cpp ) @@ -261,6 +262,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiuAbstractLegendFrame.h ${CMAKE_CURRENT_LIST_DIR}/RiuCategoryLegendFrame.h ${CMAKE_CURRENT_LIST_DIR}/RiuScalarMapperLegendFrame.h ${CMAKE_CURRENT_LIST_DIR}/RiuTextEditWithCompletion.h +${CMAKE_CURRENT_LIST_DIR}/RiuTextContentFrame.h ) list(APPEND QT_UI_FILES diff --git a/ApplicationCode/UserInterface/RiuTextContentFrame.cpp b/ApplicationCode/UserInterface/RiuTextContentFrame.cpp new file mode 100644 index 0000000000..ea14fa803e --- /dev/null +++ b/ApplicationCode/UserInterface/RiuTextContentFrame.cpp @@ -0,0 +1,179 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// ResInsight is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// +#include "RiuTextContentFrame.h" + +#include "RiaApplication.h" +#include "RiaFontCache.h" +#include "RiaPreferences.h" + +#include "RiuGuiTheme.h" + +#include +#include +#include + +#include + +//#include + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RiuTextContentFrame::RiuTextContentFrame( QWidget* parent, const QString& title, const QString& text ) + : RiuAbstractOverlayContentFrame( parent ) + , m_title( title ) + , m_text( text ) +{ + setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QSize RiuTextContentFrame::sizeHint() const +{ + LayoutInfo layout( QSize( 200, 100 ) ); // Use default size + layoutInfo( &layout ); + + QFontMetrics fontMetrics( this->font() ); + QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 2000, 200 ), Qt::AlignLeft | Qt::TextWordWrap, m_title ); + QRect textRect = fontMetrics.boundingRect( QRect( 0, 0, 2000, 200 ), Qt::AlignLeft | Qt::TextWordWrap, m_text ); + + int preferredContentHeight = titleRect.height() + layout.lineSpacing + textRect.height(); + int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom(); + + int preferredWidth = std::max( titleRect.width(), textRect.width() ) + layout.margins.left() + layout.margins.right(); + + preferredWidth = std::min( preferredWidth, 2000 ); + + return QSize( preferredWidth, preferredHeight ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QSize RiuTextContentFrame::minimumSizeHint() const +{ + LayoutInfo layout( QSize( 200, 100 ) ); // Use default size + layoutInfo( &layout ); + QFont titleFont = this->font(); + titleFont.setBold( true ); + QFontMetrics fontMetrics( titleFont ); + QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 2000, 200 ), Qt::AlignLeft | Qt::TextWordWrap, m_title ); + fontMetrics = QFontMetrics( this->font() ); + QRect textRect = fontMetrics.boundingRect( QRect( 0, 0, 2000, 200 ), Qt::AlignLeft | Qt::TextWordWrap, m_text ); + + int preferredContentHeight = titleRect.height() + layout.lineSpacing + textRect.height(); + int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom(); + int preferredWidth = std::max( titleRect.width(), textRect.width() ) + layout.margins.left() + layout.margins.right(); + + preferredWidth = std::min( preferredWidth, 2000 ); + + return QSize( preferredWidth, preferredHeight ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuTextContentFrame::renderTo( QPainter* painter, const QRect& targetRect ) +{ + QFont font = this->font(); + font.setPixelSize( + caf::FontTools::pointSizeToPixelSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() ) ); + this->setFont( font ); + + QColor textColor = RiuGuiTheme::getColorByVariableName( "textColor" ); + + LayoutInfo layout( QSize( targetRect.width(), targetRect.height() ) ); + layoutInfo( &layout ); + + painter->save(); + + painter->setFont( this->font() ); + painter->translate( targetRect.topLeft() ); + + { + painter->save(); + painter->translate( QPoint( layout.margins.left(), layout.margins.top() ) ); + painter->setPen( QPen( textColor ) ); + QTextDocument td; + td.setDocumentMargin( 0.0 ); + td.setDefaultFont( this->font() ); + QString formattedTitle = m_title; + td.setHtml( QString( "%2" ) + .arg( textColor.name() ) + .arg( formattedTitle.replace( "\n", "
" ) ) ); + td.drawContents( painter ); + painter->restore(); + } + + { + painter->save(); + painter->translate( QPoint( layout.margins.left(), layout.margins.top() + layout.lineSpacing * 2 ) ); + painter->setPen( QPen( textColor ) ); + + /* + QwtMathMLTextEngine textEngine = QwtMathMLTextEngine(); + textEngine.draw( painter, + QRect( targetRect.left() + layout.margins.left(), + targetRect.top() + layout.margins.top() + layout.lineSpacing * 2, + targetRect.width() - layout.margins.left() - layout.margins.right(), + targetRect.height() - layout.margins.top() - layout.margins.bottom() - + layout.margins.top() + layout.lineSpacing * 2 ), + Qt::AlignLeft, + m_text ); + + */ + QTextDocument td; + td.setDocumentMargin( 0.0 ); + td.setDefaultFont( this->font() ); + QString formattedTitle = m_text; + td.setHtml( QString( "%2" ) + .arg( textColor.name() ) + .arg( formattedTitle.replace( "\n", "
" ) ) ); + td.drawContents( painter ); + + painter->restore(); + } + + painter->restore(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuTextContentFrame::paintEvent( QPaintEvent* e ) +{ + QPainter painter( this ); + renderTo( &painter, e->rect() ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RiuTextContentFrame::layoutInfo( LayoutInfo* layout ) const +{ + QFontMetrics fontMetrics( this->font() ); + QStringList titleLines = m_text.split( "\n", QString::SkipEmptyParts ); + + layout->charHeight = fontMetrics.height(); + layout->charAscent = fontMetrics.ascent(); + layout->lineSpacing = fontMetrics.lineSpacing(); + layout->margins = QMargins( 8, 8, 8, 8 ); + layout->tickTextLeadSpace = 5; +} diff --git a/ApplicationCode/UserInterface/RiuTextContentFrame.h b/ApplicationCode/UserInterface/RiuTextContentFrame.h new file mode 100644 index 0000000000..d5f4d211ac --- /dev/null +++ b/ApplicationCode/UserInterface/RiuTextContentFrame.h @@ -0,0 +1,66 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// ResInsight is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// +#pragma once + +#include "RiuAbstractOverlayContentFrame.h" + +#include +#include + +class RiuTextContentFrame : public RiuAbstractOverlayContentFrame +{ + Q_OBJECT + +public: + RiuTextContentFrame( QWidget* parent, const QString& title, const QString& text ); + + QSize sizeHint() const override; + QSize minimumSizeHint() const override; + + void renderTo( QPainter* painter, const QRect& targetRect ) override; + +protected: + struct LayoutInfo + { + LayoutInfo( const QSize& size ) + { + charHeight = 0; + charAscent = 0; + lineSpacing = 0; + margins = QMargins( 0, 0, 0, 0 ); + + overallLegendSize = size; + } + + int charHeight; + int charAscent; + int lineSpacing; + QMargins margins; + int tickTextLeadSpace; + QSize overallLegendSize; + }; + + void paintEvent( QPaintEvent* e ) override; + +private: + virtual void layoutInfo( LayoutInfo* layout ) const; + +protected: + QString m_title; + QString m_text; +};