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