QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
20#include "qgis.h"
21
22#include <QApplication>
23#include <QPainter>
24
25#include "moc_qgsmenuheader.cpp"
26
27QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
28 : QWidget( parent )
29 , mText( text )
30{
31 const int textMinWidth = fontMetrics().boundingRect( mText ).width();
32 mTextHeight = fontMetrics().height();
33 mLabelMargin = Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( '.' );
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
49void QgsMenuHeader::paintEvent( QPaintEvent * )
50{
51 QPainter painter( this );
52 const QPalette pal = QPalette( qApp->palette() );
53 const QColor headerBgColor = pal.color( QPalette::Mid );
54 const 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
67QgsMenuHeaderWidgetAction::QgsMenuHeaderWidgetAction( const QString &text, QObject *parent )
68 : QWidgetAction( parent )
69{
70 QgsMenuHeader *w = new QgsMenuHeader( text, nullptr );
71 setDefaultWidget( w ); //transfers ownership
72}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:6222
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.
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