mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Added text content frame.
This commit is contained in:
parent
7b15ecac2b
commit
a2ca399dfa
@ -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
|
||||
|
179
ApplicationCode/UserInterface/RiuTextContentFrame.cpp
Normal file
179
ApplicationCode/UserInterface/RiuTextContentFrame.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
#include "RiuTextContentFrame.h"
|
||||
|
||||
#include "RiaApplication.h"
|
||||
#include "RiaFontCache.h"
|
||||
#include "RiaPreferences.h"
|
||||
|
||||
#include "RiuGuiTheme.h"
|
||||
|
||||
#include <QPaintEvent>
|
||||
#include <QPainter>
|
||||
#include <QTextDocument>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
//#include <mathml/qwt_mathml_text_engine.h>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
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( "<body><font color='%1' ><b>%2</b></font></body>" )
|
||||
.arg( textColor.name() )
|
||||
.arg( formattedTitle.replace( "\n", "<br />" ) ) );
|
||||
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( "<body><font color='%1'>%2</font></body>" )
|
||||
.arg( textColor.name() )
|
||||
.arg( formattedTitle.replace( "\n", "<br />" ) ) );
|
||||
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;
|
||||
}
|
66
ApplicationCode/UserInterface/RiuTextContentFrame.h
Normal file
66
ApplicationCode/UserInterface/RiuTextContentFrame.h
Normal file
@ -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 <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
#include "RiuAbstractOverlayContentFrame.h"
|
||||
|
||||
#include <QFrame>
|
||||
#include <QString>
|
||||
|
||||
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;
|
||||
};
|
Loading…
Reference in New Issue
Block a user