QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
qgsmenuheader.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmenuheader.cpp
3 -----------------
4 begin : June 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsmenuheader.h"
19#include "qgis.h"
20#include <QPainter>
21#include <QApplication>
22
23QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
24 : QWidget( parent )
25 , mText( text )
26{
27 const int textMinWidth = fontMetrics().boundingRect( mText ).width();
28 mTextHeight = fontMetrics().height();
29 mLabelMargin = Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( '.' );
30 mMinWidth = 2 * mLabelMargin + textMinWidth;
31 setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
32 updateGeometry();
33}
34
36{
37 return QSize( mMinWidth, mTextHeight + mLabelMargin );
38}
39
41{
42 return QSize( mMinWidth, mTextHeight + mLabelMargin );
43}
44
45void QgsMenuHeader::paintEvent( QPaintEvent * )
46{
47 QPainter painter( this );
48 const QPalette pal = QPalette( qApp->palette() );
49 const QColor headerBgColor = pal.color( QPalette::Mid );
50 const QColor headerTextColor = pal.color( QPalette::BrightText );
51
52 //draw header background
53 painter.setBrush( headerBgColor );
54 painter.setPen( Qt::NoPen );
55 painter.drawRect( QRect( 0, 0, width(), mTextHeight + mLabelMargin ) );
56
57 //draw header text
58 painter.setPen( headerTextColor );
59 painter.drawText( QPoint( mLabelMargin, 0.25 * mLabelMargin + mTextHeight ), mText );
60 painter.end();
61}
62
63QgsMenuHeaderWidgetAction::QgsMenuHeaderWidgetAction( const QString &text, QObject *parent )
64 : QWidgetAction( parent )
65{
66 QgsMenuHeader *w = new QgsMenuHeader( text, nullptr );
67 setDefaultWidget( w ); //transfers ownership
68}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:4867
QgsMenuHeaderWidgetAction(const QString &text, QObject *parent=nullptr)
Constructor for QgsMenuHeaderWidgetAction, showing the specified text.
Custom widget for displaying subheaders within a QMenu in a standard style.
Definition: qgsmenuheader.h:32
void paintEvent(QPaintEvent *event) override
QgsMenuHeader(const QString &text, QWidget *parent=nullptr)
Constructor for QgsMenuHeader, showing the specified text.
QSize sizeHint() const override
QSize minimumSizeHint() const override