QGIS API Documentation  3.15.0-Master (c0197371fe)
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 
23 QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
24  : QWidget( parent )
25  , mText( text )
26 {
27  int textMinWidth = fontMetrics().boundingRect( mText ).width();
28  mTextHeight = fontMetrics().height();
29 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
30  mLabelMargin = Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "." ) );
31 #else
32  mLabelMargin = Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( '.' );
33 #endif
34  mMinWidth = 2 * mLabelMargin + textMinWidth;
35  setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
36  updateGeometry();
37 }
38 
40 {
41  return QSize( mMinWidth, mTextHeight + mLabelMargin );
42 }
43 
45 {
46  return QSize( mMinWidth, mTextHeight + mLabelMargin );
47 }
48 
49 void QgsMenuHeader::paintEvent( QPaintEvent * )
50 {
51  QPainter painter( this );
52  QPalette pal = QPalette( qApp->palette() );
53  QColor headerBgColor = pal.color( QPalette::Mid );
54  QColor headerTextColor = pal.color( QPalette::BrightText );
55 
56  //draw header background
57  painter.setBrush( headerBgColor );
58  painter.setPen( Qt::NoPen );
59  painter.drawRect( QRect( 0, 0, width(), mTextHeight + mLabelMargin ) );
60 
61  //draw header text
62  painter.setPen( headerTextColor );
63  painter.drawText( QPoint( mLabelMargin, 0.25 * mLabelMargin + mTextHeight ), mText );
64  painter.end();
65 }
66 
67 QgsMenuHeaderWidgetAction::QgsMenuHeaderWidgetAction( const QString &text, QObject *parent )
68  : QWidgetAction( parent )
69 {
70  QgsMenuHeader *w = new QgsMenuHeader( text, nullptr );
71  setDefaultWidget( w ); //transfers ownership
72 }
QgsMenuHeaderWidgetAction(const QString &text, QObject *parent=nullptr)
Constructor for QgsMenuHeaderWidgetAction, showing the specified text.
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:182
QSize minimumSizeHint() const override
void paintEvent(QPaintEvent *event) override
Custom widget for displaying subheaders within a QMenu in a standard style.
Definition: qgsmenuheader.h:32
QSize sizeHint() const override
QgsMenuHeader(const QString &text, QWidget *parent=nullptr)
Constructor for QgsMenuHeader, showing the specified text.