QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 <QPainter>
20 #include <QApplication>
21 
22 QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
23  : QWidget( parent )
24  , mText( text )
25 {
26  int textMinWidth = fontMetrics().width( mText );
27  mTextHeight = fontMetrics().height();
28  mLabelMargin = Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "." ) );
29  mMinWidth = 2 * mLabelMargin + textMinWidth;
30  setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
31  updateGeometry();
32 }
33 
35 {
36  return QSize( mMinWidth, mTextHeight + mLabelMargin );
37 }
38 
40 {
41  return QSize( mMinWidth, mTextHeight + mLabelMargin );
42 }
43 
44 void QgsMenuHeader::paintEvent( QPaintEvent * )
45 {
46  QPainter painter( this );
47  QPalette pal = QPalette( qApp->palette() );
48  QColor headerBgColor = pal.color( QPalette::Mid );
49  QColor headerTextColor = pal.color( QPalette::BrightText );
50 
51  //draw header background
52  painter.setBrush( headerBgColor );
53  painter.setPen( Qt::NoPen );
54  painter.drawRect( QRect( 0, 0, width(), mTextHeight + mLabelMargin ) );
55 
56  //draw header text
57  painter.setPen( headerTextColor );
58  painter.drawText( QPoint( mLabelMargin, 0.25 * mLabelMargin + mTextHeight ), mText );
59  painter.end();
60 }
61 
62 QgsMenuHeaderWidgetAction::QgsMenuHeaderWidgetAction( const QString &text, QObject *parent )
63  : QWidgetAction( parent )
64 {
65  QgsMenuHeader *w = new QgsMenuHeader( text, nullptr );
66  setDefaultWidget( w ); //transfers ownership
67 }
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:152
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.