mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Merge pull request #4923 from OPM/system-stylesheet-manager
Implement style sheet manager for more robush CSS management
This commit is contained in:
commit
6c45c88619
@ -86,6 +86,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiuDraggableOverlayFrame.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuMdiMaximizeWindowGuard.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuMainWindowTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuComparisonViewMover.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuWidgetStyleSheet.h
|
||||
)
|
||||
|
||||
set (SOURCE_GROUP_SOURCE_FILES
|
||||
@ -171,6 +172,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiuDraggableOverlayFrame.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuMdiMaximizeWindowGuard.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuMainWindowTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuComparisonViewMover.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiuWidgetStyleSheet.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
@ -148,6 +148,8 @@ void RiuGridPlotWindow::addPlot( RiuQwtPlotWidget* plotWidget )
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuGridPlotWindow::insertPlot( RiuQwtPlotWidget* plotWidget, size_t index )
|
||||
{
|
||||
plotWidget->setDraggable( true ); // Becomes draggable when added to a grid plot window
|
||||
|
||||
m_plotWidgets.insert( static_cast<int>( index ), plotWidget );
|
||||
|
||||
RiuQwtPlotLegend* legend = new RiuQwtPlotLegend( this );
|
||||
@ -245,13 +247,13 @@ void RiuGridPlotWindow::setSelectionsVisible( bool visible )
|
||||
{
|
||||
for ( RiuQwtPlotWidget* plotWidget : m_plotWidgets )
|
||||
{
|
||||
if ( !visible )
|
||||
if ( visible && caf::SelectionManager::instance()->isSelected( plotWidget->plotOwner(), 0 ) )
|
||||
{
|
||||
plotWidget->setDefaultStyleSheet( false );
|
||||
plotWidget->setWidgetState( RiuWidgetStyleSheet::SELECTED );
|
||||
}
|
||||
else
|
||||
{
|
||||
plotWidget->setSelected( caf::SelectionManager::instance()->isSelected( plotWidget->plotOwner(), 0 ) );
|
||||
plotWidget->setWidgetState( RiuWidgetStyleSheet::DEFAULT );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -413,7 +415,7 @@ void RiuGridPlotWindow::dragMoveEvent( QDragMoveEvent* event )
|
||||
int insertBeforeIndex = visiblePlotWidgets.size();
|
||||
for ( int visibleIndex = 0; visibleIndex < visiblePlotWidgets.size(); ++visibleIndex )
|
||||
{
|
||||
visiblePlotWidgets[visibleIndex]->setDefaultStyleSheet();
|
||||
visiblePlotWidgets[visibleIndex]->setWidgetState( RiuWidgetStyleSheet::DEFAULT );
|
||||
|
||||
if ( visiblePlotWidgets[visibleIndex]->frameIsInFrontOfThis( newRect ) )
|
||||
{
|
||||
@ -422,17 +424,16 @@ void RiuGridPlotWindow::dragMoveEvent( QDragMoveEvent* event )
|
||||
}
|
||||
if ( insertBeforeIndex >= 0 && insertBeforeIndex < visiblePlotWidgets.size() )
|
||||
{
|
||||
visiblePlotWidgets[insertBeforeIndex]->setStyleSheetForThisObject(
|
||||
"border-left: 2px solid red; border-top: none; border-bottom: none; border-right: none;" );
|
||||
visiblePlotWidgets[insertBeforeIndex]->setWidgetState( RiuWidgetStyleSheet::DRAG_TARGET_BEFORE );
|
||||
}
|
||||
|
||||
if ( insertBeforeIndex > 0 )
|
||||
{
|
||||
int insertAfterIndex = insertBeforeIndex - 1;
|
||||
visiblePlotWidgets[insertAfterIndex]->setStyleSheetForThisObject(
|
||||
"border-left: none; border-top: none; border-bottom: none; border-right: 2px solid red;" );
|
||||
visiblePlotWidgets[insertAfterIndex]->setWidgetState( RiuWidgetStyleSheet::DRAG_TARGET_AFTER );
|
||||
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -444,7 +445,7 @@ void RiuGridPlotWindow::dragLeaveEvent( QDragLeaveEvent* event )
|
||||
{
|
||||
for ( int tIdx = 0; tIdx < m_plotWidgets.size(); ++tIdx )
|
||||
{
|
||||
m_plotWidgets[tIdx]->setDefaultStyleSheet();
|
||||
m_plotWidgets[tIdx]->setWidgetState( RiuWidgetStyleSheet::DEFAULT );
|
||||
}
|
||||
}
|
||||
|
||||
@ -455,7 +456,7 @@ void RiuGridPlotWindow::dropEvent( QDropEvent* event )
|
||||
{
|
||||
for ( int tIdx = 0; tIdx < m_plotWidgets.size(); ++tIdx )
|
||||
{
|
||||
m_plotWidgets[tIdx]->setDefaultStyleSheet();
|
||||
m_plotWidgets[tIdx]->setWidgetState( RiuWidgetStyleSheet::DEFAULT );
|
||||
}
|
||||
|
||||
if ( this->geometry().contains( event->pos() ) )
|
||||
@ -521,7 +522,14 @@ void RiuGridPlotWindow::onSelectionManagerSelectionChanged( const std::set<int>&
|
||||
isSelected = isSelected ||
|
||||
caf::SelectionManager::instance()->isSelected( plotWidget->plotOwner(), changedLevel );
|
||||
}
|
||||
plotWidget->setSelected( isSelected );
|
||||
if ( isSelected )
|
||||
{
|
||||
plotWidget->setWidgetState( RiuWidgetStyleSheet::SELECTED );
|
||||
}
|
||||
else
|
||||
{
|
||||
plotWidget->setWidgetState( RiuWidgetStyleSheet::DEFAULT );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,9 +28,8 @@
|
||||
|
||||
#include "RiuPlotMainWindowTools.h"
|
||||
#include "RiuQwtCurvePointTracker.h"
|
||||
#include "RiuQwtPlotTools.h"
|
||||
|
||||
#include "RiuQwtLinearScaleEngine.h"
|
||||
#include "RiuQwtPlotTools.h"
|
||||
#include "RiuQwtScalePicker.h"
|
||||
|
||||
#include "cafAssert.h"
|
||||
@ -62,6 +61,7 @@
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuQwtPlotWidget::RiuQwtPlotWidget( RimPlotInterface* plotTrackDefinition, QWidget* parent )
|
||||
: QwtPlot( parent )
|
||||
, m_draggable( false )
|
||||
{
|
||||
m_plotOwner = dynamic_cast<caf::PdmObject*>( plotTrackDefinition );
|
||||
CAF_ASSERT( m_plotOwner );
|
||||
@ -90,6 +90,14 @@ bool RiuQwtPlotWidget::isChecked() const
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::setDraggable( bool draggable )
|
||||
{
|
||||
m_draggable = draggable;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -415,46 +423,6 @@ QPoint RiuQwtPlotWidget::dragStartPosition() const
|
||||
return m_clickPosition;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::setDefaultStyleSheet( bool includeHoverFrame )
|
||||
{
|
||||
setStyleSheetForThisObject( "background-color: white; border: 1px solid transparent;" );
|
||||
if ( includeHoverFrame )
|
||||
{
|
||||
QString styleSheet = createHoverStyleSheet( "dotted" );
|
||||
appendStyleSheetForThisObject( styleSheet, "hover" );
|
||||
}
|
||||
this->canvas()->setStyleSheet( QString( "QwtPlotCanvas#%1 { background-color: white; border: 1px solid black; }" )
|
||||
.arg( this->canvas()->objectName() ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::setStyleSheetForThisObject( const QString& content, const QString& state /*= "" */ )
|
||||
{
|
||||
QString stateTag = !state.isEmpty() ? QString( ":%1" ).arg( state ) : "";
|
||||
QString stylesheet = QString( "QwtPlot#%1%2 { %3 }\n" ).arg( this->objectName() ).arg( stateTag ).arg( content );
|
||||
setStyleSheet( stylesheet );
|
||||
this->canvas()->setStyleSheet( QString( "QwtPlotCanvas#%1 { background-color: white; border: 1px solid black; }" )
|
||||
.arg( this->canvas()->objectName() ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::appendStyleSheetForThisObject( const QString& content, const QString& state /*= ""*/ )
|
||||
{
|
||||
QString stateTag = !state.isEmpty() ? QString( ":%1" ).arg( state ) : "";
|
||||
QString stylesheet = QString( "QwtPlot#%1%2 { %3 }\n" ).arg( this->objectName() ).arg( stateTag ).arg( content );
|
||||
QString completeStyleSheet = this->styleSheet() + stylesheet;
|
||||
setStyleSheet( completeStyleSheet );
|
||||
this->canvas()->setStyleSheet( QString( "QwtPlotCanvas#%1 { background-color: white; border: 1px solid black; }" )
|
||||
.arg( this->canvas()->objectName() ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -478,19 +446,23 @@ void RiuQwtPlotWidget::scheduleReplot()
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::setSelected( bool selected )
|
||||
void RiuQwtPlotWidget::setWidgetState( RiuWidgetStyleSheet::StateTag widgetState )
|
||||
{
|
||||
if ( selected )
|
||||
// Set all existing dynamic properties to false
|
||||
for ( QByteArray existingProperty : dynamicPropertyNames() )
|
||||
{
|
||||
QColor defaultHighlightColor = this->palette().highlight().color();
|
||||
QString styleSheet = createHoverStyleSheet( "solid" );
|
||||
setStyleSheetForThisObject( styleSheet, "hover" );
|
||||
appendStyleSheetForThisObject( QString( "border: 1px solid %1;" ).arg( defaultHighlightColor.name() ) );
|
||||
setProperty( existingProperty, false );
|
||||
}
|
||||
else
|
||||
|
||||
// Set current property state to true
|
||||
QString propertyName = RiuWidgetStyleSheet::propertyName( widgetState );
|
||||
if ( !propertyName.isEmpty() )
|
||||
{
|
||||
setDefaultStyleSheet();
|
||||
setProperty( propertyName.toLatin1(), true );
|
||||
}
|
||||
|
||||
// Trigger style update
|
||||
m_plotStyleSheet.refreshWidget( this );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -523,15 +495,15 @@ bool RiuQwtPlotWidget::eventFilter( QObject* watched, QEvent* event )
|
||||
|
||||
bool toggleItemInSelection = ( mouseEvent->modifiers() & Qt::ControlModifier ) != 0;
|
||||
|
||||
if ( mouseEvent->type() == QMouseEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton )
|
||||
if ( m_draggable && mouseEvent->type() == QMouseEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton )
|
||||
{
|
||||
m_clickPosition = mouseEvent->pos();
|
||||
}
|
||||
|
||||
if ( watched == this && !this->canvas()->geometry().contains( mouseEvent->pos() ) )
|
||||
{
|
||||
if ( mouseEvent->type() == QMouseEvent::MouseMove && ( mouseEvent->buttons() & Qt::LeftButton ) &&
|
||||
!m_clickPosition.isNull() )
|
||||
if ( m_draggable && mouseEvent->type() == QMouseEvent::MouseMove &&
|
||||
( mouseEvent->buttons() & Qt::LeftButton ) && !m_clickPosition.isNull() )
|
||||
{
|
||||
int dragLength = ( mouseEvent->pos() - m_clickPosition ).manhattanLength();
|
||||
if ( dragLength >= QApplication::startDragDistance() )
|
||||
@ -591,6 +563,20 @@ void RiuQwtPlotWidget::hideEvent( QHideEvent* event )
|
||||
QwtPlot::hideEvent( event );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::showEvent( QShowEvent* event )
|
||||
{
|
||||
m_plotStyleSheet = createPlotStyleSheet();
|
||||
m_plotStyleSheet.applyToWidget( this );
|
||||
|
||||
m_canvasStyleSheet = createCanvasStyleSheet();
|
||||
m_canvasStyleSheet.applyToWidget( canvas() );
|
||||
|
||||
QwtPlot::showEvent( event );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -620,26 +606,6 @@ void RiuQwtPlotWidget::selectPoint( QwtPlotCurve* curve, int pointNumber ) {}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuQwtPlotWidget::clearPointSelection() {}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuQwtPlotWidget::createHoverStyleSheet( const QString& borderType )
|
||||
{
|
||||
QColor highlightColor = this->palette().highlight().color();
|
||||
QColor backgroundColor = this->palette().background().color();
|
||||
QColor blendedHighlightColor = RiaColorTools::blendQColors( highlightColor, backgroundColor, 2, 1 );
|
||||
QColor nearlyBackgroundColor = RiaColorTools::blendQColors( highlightColor, backgroundColor, 1, 20 );
|
||||
|
||||
QString styleSheet = QString( "border: 1px %1 %2; background: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, "
|
||||
"stop: 0 %3, stop: 0.04 %4, stop:1 %5 );" )
|
||||
.arg( borderType )
|
||||
.arg( highlightColor.name() )
|
||||
.arg( blendedHighlightColor.name() )
|
||||
.arg( nearlyBackgroundColor.name() )
|
||||
.arg( backgroundColor.name() );
|
||||
return styleSheet;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -656,6 +622,48 @@ void RiuQwtPlotWidget::onAxisSelected( QwtScaleWidget* scale, bool toggleItemInS
|
||||
plotDefinition()->onAxisSelected( axisId, toggleItemInSelection );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuWidgetStyleSheet RiuQwtPlotWidget::createPlotStyleSheet() const
|
||||
{
|
||||
QColor backgroundColor = QColor( "white" );
|
||||
QColor highlightColor = this->palette().highlight().color();
|
||||
QColor blendedHighlightColor = RiaColorTools::blendQColors( highlightColor, backgroundColor, 1, 5 );
|
||||
QColor nearlyBackgroundColor = RiaColorTools::blendQColors( highlightColor, backgroundColor, 1, 30 );
|
||||
|
||||
RiuWidgetStyleSheet styleSheet;
|
||||
styleSheet.set( "background-color", backgroundColor.name() );
|
||||
styleSheet.set( "border", "1 px solid transparent" );
|
||||
|
||||
styleSheet.state( RiuWidgetStyleSheet::SELECTED ).set( "border", QString( "1px solid %1" ).arg( highlightColor.name() ) );
|
||||
|
||||
if ( m_draggable )
|
||||
{
|
||||
QString backgroundGradient = QString( QString( "qlineargradient( x1 : 1, y1 : 0, x2 : 1, y2 : 1,"
|
||||
"stop: 0 %1, stop: 0.02 %2, stop:1 %3 )" )
|
||||
.arg( blendedHighlightColor.name() )
|
||||
.arg( nearlyBackgroundColor.name() )
|
||||
.arg( backgroundColor.name() ) );
|
||||
|
||||
styleSheet.state( RiuWidgetStyleSheet::HOVER ).set( "background", backgroundGradient );
|
||||
}
|
||||
styleSheet.state( RiuWidgetStyleSheet::DRAG_TARGET_BEFORE ).set( "border-left", "1px solid red" );
|
||||
styleSheet.state( RiuWidgetStyleSheet::DRAG_TARGET_AFTER ).set( "border-right", "1px solid red" );
|
||||
return styleSheet;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuWidgetStyleSheet RiuQwtPlotWidget::createCanvasStyleSheet() const
|
||||
{
|
||||
RiuWidgetStyleSheet styleSheet;
|
||||
styleSheet.set( "background-color", "#FAFAFA" );
|
||||
styleSheet.set( "border", "1px solid black" );
|
||||
return styleSheet;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -663,7 +671,6 @@ void RiuQwtPlotWidget::setDefaults()
|
||||
{
|
||||
setEnabledAxes( { QwtPlot::xTop, QwtPlot::yLeft } );
|
||||
RiuQwtPlotTools::setCommonPlotBehaviour( this );
|
||||
setDefaultStyleSheet();
|
||||
}
|
||||
|
||||
void RiuQwtPlotWidget::selectPlotOwner( bool toggleItemInSelection )
|
||||
|
@ -19,11 +19,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "qwt_plot.h"
|
||||
#include "RiuWidgetStyleSheet.h"
|
||||
|
||||
#include "cafPdmObject.h"
|
||||
#include "cafPdmPointer.h"
|
||||
|
||||
#include "qwt_plot.h"
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
#include <set>
|
||||
@ -58,6 +60,8 @@ public:
|
||||
|
||||
bool isChecked() const;
|
||||
|
||||
void setDraggable( bool draggable );
|
||||
|
||||
int axisTitleFontSize( QwtPlot::Axis axis ) const;
|
||||
int axisValueFontSize( QwtPlot::Axis axis ) const;
|
||||
void setAxisFontsAndAlignment( QwtPlot::Axis,
|
||||
@ -75,7 +79,6 @@ public:
|
||||
void setAxisRange( QwtPlot::Axis axis, double min, double max );
|
||||
|
||||
void setAxisInverted( QwtPlot::Axis axis );
|
||||
|
||||
void setAxisLabelsAndTicksEnabled( QwtPlot::Axis axis, bool enable );
|
||||
|
||||
void enableGridLines( QwtPlot::Axis axis, bool majorGridLines, bool minorGridLines );
|
||||
@ -90,27 +93,24 @@ public:
|
||||
|
||||
bool frameIsInFrontOfThis( const QRect& frameGeometry );
|
||||
QPoint dragStartPosition() const;
|
||||
void setDefaultStyleSheet( bool includeHoverFrame = true );
|
||||
void setStyleSheetForThisObject( const QString& content, const QString& state = "" );
|
||||
void appendStyleSheetForThisObject( const QString& content, const QString& state = "" );
|
||||
int widthScaleFactor() const;
|
||||
|
||||
int widthScaleFactor() const;
|
||||
|
||||
void scheduleReplot();
|
||||
void setSelected( bool selected );
|
||||
void setWidgetState( RiuWidgetStyleSheet::StateTag widgetState );
|
||||
|
||||
protected:
|
||||
QSize sizeHint() const override;
|
||||
QSize minimumSizeHint() const override;
|
||||
bool eventFilter( QObject* watched, QEvent* event ) override;
|
||||
void hideEvent( QHideEvent* event ) override;
|
||||
void showEvent( QShowEvent* event ) override;
|
||||
|
||||
void applyAxisTitleToQwt( QwtPlot::Axis axis );
|
||||
|
||||
virtual void selectPoint( QwtPlotCurve* curve, int pointNumber );
|
||||
virtual void clearPointSelection();
|
||||
|
||||
QString createHoverStyleSheet( const QString& borderType = "solid" );
|
||||
|
||||
private:
|
||||
void setDefaults();
|
||||
void selectPlotOwner( bool toggleItemInSelection = false );
|
||||
@ -122,12 +122,16 @@ private:
|
||||
void resetCurveHighlighting();
|
||||
void onAxisSelected( QwtScaleWidget* scale, bool toggleItemInSelection );
|
||||
|
||||
RiuWidgetStyleSheet createPlotStyleSheet() const;
|
||||
RiuWidgetStyleSheet createCanvasStyleSheet() const;
|
||||
|
||||
private:
|
||||
caf::PdmPointer<caf::PdmObject> m_plotOwner;
|
||||
QPoint m_clickPosition;
|
||||
std::map<QwtPlot::Axis, QString> m_axisTitles;
|
||||
std::map<QwtPlot::Axis, bool> m_axisTitlesEnabled;
|
||||
QPointer<QwtPlotPicker> m_plotPicker;
|
||||
bool m_draggable;
|
||||
|
||||
struct CurveColors
|
||||
{
|
||||
@ -139,5 +143,8 @@ private:
|
||||
std::map<QwtPlotCurve*, CurveColors> m_originalCurveColors;
|
||||
std::map<QwtPlotCurve*, double> m_originalZValues;
|
||||
|
||||
RiuWidgetStyleSheet m_plotStyleSheet;
|
||||
RiuWidgetStyleSheet m_canvasStyleSheet;
|
||||
|
||||
friend class RiaPlotWindowRedrawScheduler;
|
||||
};
|
||||
|
180
ApplicationCode/UserInterface/RiuWidgetStyleSheet.cpp
Normal file
180
ApplicationCode/UserInterface/RiuWidgetStyleSheet.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 "RiuWidgetStyleSheet.h"
|
||||
|
||||
#include "cafAssert.h"
|
||||
|
||||
#include <QStringList>
|
||||
#include <QStyle>
|
||||
#include <QWidget>
|
||||
|
||||
namespace caf
|
||||
{
|
||||
template <>
|
||||
void RiuWidgetStyleSheet::StateTagEnum::setUp()
|
||||
{
|
||||
addItem( RiuWidgetStyleSheet::DEFAULT, "", "" );
|
||||
addItem( RiuWidgetStyleSheet::SELECTED, "selected", "selected" );
|
||||
addItem( RiuWidgetStyleSheet::DRAG_TARGET_BEFORE, "dragTargetBefore", "drop Before" );
|
||||
addItem( RiuWidgetStyleSheet::DRAG_TARGET_AFTER, "dragTargetAfter", "drop After" );
|
||||
addItem( RiuWidgetStyleSheet::HOVER, "hover", "hover" );
|
||||
setDefault( RiuWidgetStyleSheet::DEFAULT );
|
||||
}
|
||||
|
||||
} // namespace caf
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuWidgetStyleSheet::State::State( const QString& stateString )
|
||||
: m_stateString( stateString )
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuWidgetStyleSheet::State::set( const QString& key, const QString& value )
|
||||
{
|
||||
m_content[key] = value;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::State::get( const QString& key ) const
|
||||
{
|
||||
auto it = m_content.find( key );
|
||||
return it != m_content.end() ? it->second : "";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::State::fullText( const QString& className, const QString& objectName ) const
|
||||
{
|
||||
QString styleContent;
|
||||
for ( auto keyValuePair : m_content )
|
||||
{
|
||||
styleContent += QString( " %1: %2;\n" ).arg( keyValuePair.first ).arg( keyValuePair.second );
|
||||
}
|
||||
|
||||
QString objectNameTag = !objectName.isEmpty() ? QString( "#%1" ).arg( objectName ) : "";
|
||||
|
||||
QString fullStyleSheet =
|
||||
QString( "%1%2%3\n{\n%4}" ).arg( className ).arg( objectNameTag ).arg( m_stateString ).arg( styleContent );
|
||||
return fullStyleSheet;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuWidgetStyleSheet::RiuWidgetStyleSheet()
|
||||
{
|
||||
// Add the default state
|
||||
m_states.insert( std::make_pair( DEFAULT, State( "" ) ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuWidgetStyleSheet::set( const QString& key, const QString& value )
|
||||
{
|
||||
state( DEFAULT ).set( key, value );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::get( const QString& key ) const
|
||||
{
|
||||
auto it = m_states.find( DEFAULT );
|
||||
return it != m_states.end() ? it->second.get( key ) : "";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RiuWidgetStyleSheet::State& RiuWidgetStyleSheet::state( StateTag stateTag )
|
||||
{
|
||||
auto itBoolPair = m_states.insert( std::make_pair( stateTag, State( buildStateString( stateTag ) ) ) );
|
||||
auto it = itBoolPair.first;
|
||||
|
||||
return it->second;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::propertyName( StateTag state )
|
||||
{
|
||||
if ( state < PSEUDO_STATE_LIMIT )
|
||||
{
|
||||
return StateTagEnum::text( state );
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuWidgetStyleSheet::applyToWidget( QWidget* widget ) const
|
||||
{
|
||||
QString completeStyleSheet = fullText( QString( widget->metaObject()->className() ), widget->objectName() );
|
||||
widget->setStyleSheet( completeStyleSheet );
|
||||
refreshWidget( widget );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuWidgetStyleSheet::refreshWidget( QWidget* widget ) const
|
||||
{
|
||||
widget->style()->unpolish( widget );
|
||||
widget->style()->polish( widget );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::fullText( const QString& className, const QString& objectName ) const
|
||||
{
|
||||
QStringList textForAllStates;
|
||||
for ( auto it = m_states.begin(); it != m_states.end(); ++it )
|
||||
{
|
||||
textForAllStates.push_back( it->second.fullText( className, objectName ) );
|
||||
}
|
||||
return textForAllStates.join( "\n" );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RiuWidgetStyleSheet::buildStateString( StateTag state )
|
||||
{
|
||||
QString stateString;
|
||||
if ( state == HOVER )
|
||||
{
|
||||
stateString += ":hover";
|
||||
}
|
||||
else
|
||||
{
|
||||
stateString += QString( "[%1=true]" ).arg( propertyName( state ) );
|
||||
}
|
||||
return stateString;
|
||||
}
|
84
ApplicationCode/UserInterface/RiuWidgetStyleSheet.h
Normal file
84
ApplicationCode/UserInterface/RiuWidgetStyleSheet.h
Normal file
@ -0,0 +1,84 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 "cafAppEnum.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <map>
|
||||
|
||||
class RiuWidgetStyleSheetManager;
|
||||
class QWidget;
|
||||
|
||||
//==================================================================================================
|
||||
//
|
||||
//
|
||||
//
|
||||
//==================================================================================================
|
||||
class RiuWidgetStyleSheet
|
||||
{
|
||||
public:
|
||||
enum StateTag
|
||||
{
|
||||
DEFAULT = 0x0000,
|
||||
// Dynamic Properties:
|
||||
SELECTED = 0x0001,
|
||||
DRAG_TARGET_BEFORE = 0x0002,
|
||||
DRAG_TARGET_AFTER = 0x0004,
|
||||
// Pseudo States:
|
||||
PSEUDO_STATE_LIMIT = 0x1000,
|
||||
HOVER = 0x1000
|
||||
};
|
||||
typedef caf::AppEnum<StateTag> StateTagEnum;
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
State( const QString& stateString );
|
||||
void set( const QString& key, const QString& value );
|
||||
QString get( const QString& key ) const;
|
||||
QString fullText( const QString& className, const QString& objectName ) const;
|
||||
|
||||
private:
|
||||
QString m_stateString;
|
||||
std::map<QString, QString> m_content;
|
||||
};
|
||||
|
||||
public:
|
||||
RiuWidgetStyleSheet();
|
||||
|
||||
void set( const QString& key, const QString& value );
|
||||
QString get( const QString& key ) const;
|
||||
|
||||
State& state( StateTag stateTag );
|
||||
|
||||
static QString propertyName( StateTag stateTag );
|
||||
|
||||
void applyToWidget( QWidget* widget ) const;
|
||||
void refreshWidget( QWidget* widget ) const;
|
||||
|
||||
private:
|
||||
friend class RiuWidgetStyleSheetManager;
|
||||
QString fullText( const QString& className, const QString& objectName ) const;
|
||||
|
||||
static QString buildStateString( StateTag stateTag );
|
||||
|
||||
private:
|
||||
std::map<int, State> m_states;
|
||||
};
|
Loading…
Reference in New Issue
Block a user