ResInsight/Fwk/AppFwk/cafUserInterface/QMinimizePanel.cpp
2024-01-07 18:28:36 +01:00

460 lines
16 KiB
C++

//##################################################################################################
//
// QMinimizePanel
// Copyright (C) 2017 Ceetron Solutions AS
//
// This class may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma warning( disable : 4125 )
#include "QMinimizePanel.h"
#include <QApplication>
#include <QDebug>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QPushButton>
#include <QRegularExpression>
#include <QResizeEvent>
#include <QStyle>
#include <QVBoxLayout>
#include <algorithm>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
static const struct
{
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
unsigned char pixel_data[7 * 10 * 4 + 1];
} expandDownArrow = {
7,
10,
4,
"QRY\317445A\0\0\0\0\0\0\0\0\0\0\0\0"
"445AJJN\317OQW\256OPW\317445#\0\0\0"
"\0"
"445#IJP\317HIN\256445#MOT\317LMS\317445#IJP\317GHM\317445#\0\0\0\0"
"4"
"45#DEK\317??C\317BBG\317445#\0\0\0\0\0\0\0\0\0\0\0\0"
"445#>?B\317445#\0\0"
"\0\0\0\0\0\0LNT\317445A\0\0\0\0\0\0\0\0\0\0\0\0"
"445ACEI\317JKR\256IJP\317"
"445#\0\0\0\0"
"445#DEH\317BCJ\256445#GHO\317EGK\317445#DEH\317BDI\317445#"
"\0\0\0\0"
"445#@AE\317??C\317??B\317445#\0\0\0\0\0\0\0\0\0\0\0\0"
"445#<<?"
"\317445#\0\0\0\0\0\0\0\0",
};
QIcon createExpandDownIcon()
{
QImage img( expandDownArrow.pixel_data, expandDownArrow.width, expandDownArrow.height, QImage::Format_ARGB32 );
QPixmap pxMap;
pxMap = QPixmap::fromImage( img );
return QIcon( pxMap );
}
static const QIcon& expandDownIcon()
{
static QIcon expandDownIcon( createExpandDownIcon() );
return expandDownIcon;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
static const struct
{
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
unsigned char pixel_data[7 * 10 * 4 + 1];
} expandUpArrow = {
7,
10,
4,
"\0\0\0\0\0\0\0\0"
"445#<<?\317445#\0\0\0\0\0\0\0\0\0\0\0\0"
"445#@AE\317??"
"C\317??B\317445#\0\0\0\0"
"445#GHO\317EGK\317445#DEH\317BDI\317445#JKR\256"
"IJP\317445#\0\0\0\0"
"445#DEH\317BCJ\256LNT\317445A\0\0\0\0\0\0\0\0\0\0\0"
"\0"
"445ACEI\317\0\0\0\0\0\0\0\0"
"445#>?B\317445#\0\0\0\0\0\0\0\0\0\0\0\0"
""
"445#DEK\317??C\317BBG\317445#\0\0\0\0"
"445#MOT\317LMS\317445#IJP\317GH"
"M\317445#OQW\256OPW\317445#\0\0\0\0"
"445#IJP\317HIN\256QRY\317445A\0\0\0"
"\0\0\0\0\0\0\0\0\0"
"445AJJN\317",
};
QIcon createExpandUpIcon()
{
QImage img( expandUpArrow.pixel_data, expandUpArrow.width, expandUpArrow.height, QImage::Format_ARGB32 );
QPixmap pxMap;
pxMap = QPixmap::fromImage( img );
return QIcon( pxMap );
}
static const QIcon& expandUpIcon()
{
static QIcon expandUpIcon( createExpandUpIcon() );
return expandUpIcon;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMinimizePanel::QMinimizePanel( QWidget* parent /*=0*/ )
: QFrame( parent )
{
m_titleBackground =
"qlineargradient(spread:pad, x1:0 y1:0, x2:0 y2:1, stop:0 rgba(150, 150, 150, 20), stop:1 rgba(0, 0, 0, 50))";
m_border = "1px solid darkgray";
m_background = "rgba(255, 250, 250, 85)";
m_collapseIcon = expandUpIcon();
m_expandIcon = expandDownIcon();
this->initialize( "" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMinimizePanel::QMinimizePanel( const QString& title, QWidget* parent /*=0*/ )
: QFrame( parent )
{
this->initialize( title );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMinimizePanel::~QMinimizePanel()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QFrame* QMinimizePanel::contentFrame()
{
return m_contentFrame;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setTitle( const QString& title )
{
m_titleLabel->setText( title );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::title() const
{
return m_titleLabel->text();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::enableFrame( bool showFrame )
{
if ( showFrame )
{
this->setFrameStyle( static_cast<int>( QFrame::StyledPanel ) | static_cast<int>( QFrame::Plain ) );
m_titleFrame->show();
m_titleLabel->show();
m_collapseButton->show();
m_contentFrame->setObjectName( "FramedGroupContent" );
}
else
{
this->setFrameStyle( QFrame::NoFrame );
m_titleFrame->hide();
m_titleLabel->hide();
m_collapseButton->hide();
m_contentFrame->setObjectName( "UnframedGroupContent" );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool QMinimizePanel::isExpanded() const
{
return !m_contentFrame->isHidden();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getBackground() const
{
return m_background;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setBackground( QString background )
{
m_background = background;
m_contentFrame->setStyleSheet( contentFrameStyleSheet() );
m_contentFrame->style()->unpolish( m_contentFrame );
m_contentFrame->style()->polish( m_contentFrame );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getTitleBackground() const
{
return m_titleBackground;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setTitleBackground( QString background )
{
m_titleBackground = background;
m_titleFrame->setStyleSheet( titleFrameStyleSheet() );
m_titleFrame->style()->unpolish( m_titleFrame );
m_titleFrame->style()->polish( m_titleFrame );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getBorder() const
{
return m_border;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setBorder( QString border )
{
m_border = border;
m_contentFrame->setStyleSheet( contentFrameStyleSheet() );
m_contentFrame->style()->unpolish( m_contentFrame );
m_contentFrame->style()->polish( m_contentFrame );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getExpandIconPath() const
{
return m_expandIconPath;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setExpandIconPath( QString path )
{
m_expandIconPath = path;
m_expandIcon = QIcon( path );
if ( !isExpanded() ) m_collapseButton->setIcon( m_expandIcon );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getCollapseIconPath() const
{
return m_expandIconPath;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setCollapseIconPath( QString path )
{
m_collapseIconPath = path;
m_collapseIcon = QIcon( path );
if ( isExpanded() ) m_collapseButton->setIcon( m_collapseIcon );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::getIconSize() const
{
return m_iconSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setIconSize( QString size )
{
m_iconSize = size;
QRegularExpression sizeRegExp = QRegularExpression( "\\s*([0-9]+)px\\s*" );
if ( sizeRegExp.match( size ).hasMatch() )
{
m_collapseButton->setIconSize(
QSize( sizeRegExp.match( size ).captured( 1 ).toInt(), sizeRegExp.match( size ).captured( 1 ).toInt() ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::setExpanded( bool isExpanded )
{
if ( m_contentFrame->isHidden() != isExpanded ) return;
m_contentFrame->setVisible( isExpanded );
if ( isExpanded )
{
m_collapseButton->setIcon( m_collapseIcon );
this->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
}
else
{
m_collapseButton->setIcon( m_expandIcon );
this->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
}
this->QWidget::updateGeometry();
emit expandedChanged( isExpanded );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::toggleExpanded()
{
setExpanded( m_contentFrame->isHidden() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void QMinimizePanel::initialize( const QString& title )
{
this->setFrameStyle( static_cast<int>( QFrame::StyledPanel ) | static_cast<int>( QFrame::Plain ) );
QVBoxLayout* fullLayout = new QVBoxLayout( this );
fullLayout->setContentsMargins( 0, 0, 0, 0 );
fullLayout->setSpacing( 0 );
{ // Title
m_titleFrame = new QFrame();
fullLayout->addWidget( m_titleFrame, 0 );
fullLayout->setStretch( 0, 0 );
m_titleFrame->setObjectName( "GroupTitleFrame" );
m_titleFrame->setStyleSheet( titleFrameStyleSheet() );
QHBoxLayout* titleLayout = new QHBoxLayout();
titleLayout->setContentsMargins( 4, 2, 0, 2 );
m_titleFrame->setLayout( titleLayout );
m_titleFrame->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
{
m_titleLabel = new QLabel( title );
QPalette titleLabelPalette = m_titleLabel->palette();
m_titleLabel->setPalette( titleLabelPalette );
titleLayout->addWidget( m_titleLabel, 1, Qt::AlignLeft );
}
{
m_collapseButton = new QPushButton();
m_collapseButton->setFlat( true );
m_collapseButton->setIcon( m_collapseIcon );
m_collapseButton->setDefault( false );
m_collapseButton->setAutoDefault( false );
m_collapseButton->setIconSize( QSize( 16, 16 ) );
m_collapseButton->setMaximumSize( QSize( 16, 16 ) );
titleLayout->addWidget( m_collapseButton, 0, Qt::AlignRight );
}
}
{
m_contentFrame = new QFrame();
m_contentFrame->setStyleSheet( contentFrameStyleSheet() );
m_contentFrame->setObjectName( "GroupContentFrame" );
fullLayout->addWidget( m_contentFrame, 1 );
fullLayout->setStretch( 1, 1 );
}
connect( m_collapseButton, SIGNAL( clicked() ), this, SLOT( toggleExpanded() ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::titleFrameStyleSheet()
{
return QString( "QFrame#GroupTitleFrame "
"{"
" border-top: none; border-left: none; border-right: none; border-bottom: none;"
" background: %0;"
"}" )
.arg( m_titleBackground );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString QMinimizePanel::contentFrameStyleSheet()
{
return QString( "QFrame#FramedGroupContent"
"{"
" border-top: %0; border-left: none; border-right: none; border-bottom: none; "
" background: %1;"
"}"
"QFrame#UnframedGroupContent"
"{"
" border-top: none; border-left: none; border-right: none; border-bottom: none; "
"}" )
.arg( m_border, m_background );
}