Skip to content

Commit ae8b71e

Browse files
committedJun 2, 2017
New classes QgsMenuHeader, QgsMenuHeaderWidgetAction
Custom widgets designed for displaying subheaders within a QMenu in a standard style (i.e. matching the subheaders shown within the color button drop down menus)
1 parent 19f0b39 commit ae8b71e

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed
 

‎python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
%Include qgsmaptoolpan.sip
120120
%Include qgsmaptoolzoom.sip
121121
%Include qgsmaplayerstylemanagerwidget.sip
122+
%Include qgsmenuheader.sip
122123
%Include qgsmessagebar.sip
123124
%Include qgsmessagebaritem.sip
124125
%Include qgsmessagelogviewer.sip

‎python/gui/qgsmenuheader.sip

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsmenuheader.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsMenuHeader : QWidget
12+
{
13+
%Docstring
14+
Custom widget for displaying subheaders within a QMenu in a standard style.
15+
.. versionadded:: 3.0
16+
.. seealso:: QgsMenuHeaderWidgetAction()
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgsmenuheader.h"
21+
%End
22+
public:
23+
24+
explicit QgsMenuHeader( const QString &text, QWidget *parent /TransferThis/ = 0 );
25+
%Docstring
26+
Constructor for QgsMenuHeader, showing the specified ``text``.
27+
%End
28+
29+
virtual QSize minimumSizeHint() const;
30+
virtual QSize sizeHint() const;
31+
32+
protected:
33+
34+
virtual void paintEvent( QPaintEvent *event );
35+
36+
37+
};
38+
39+
class QgsMenuHeaderWidgetAction: QWidgetAction
40+
{
41+
%Docstring
42+
Custom QWidgetAction for displaying subheaders within a QMenu in a standard style.
43+
.. versionadded:: 3.0
44+
.. seealso:: QgsMenuHeader()
45+
%End
46+
47+
%TypeHeaderCode
48+
#include "qgsmenuheader.h"
49+
%End
50+
public:
51+
52+
QgsMenuHeaderWidgetAction( const QString &text, QObject *parent = 0 );
53+
%Docstring
54+
Constructor for QgsMenuHeaderWidgetAction, showing the specified ``text``.
55+
%End
56+
57+
};
58+
59+
/************************************************************************
60+
* This file has been generated automatically from *
61+
* *
62+
* src/gui/qgsmenuheader.h *
63+
* *
64+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
65+
************************************************************************/

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ SET(QGIS_GUI_SRCS
264264
qgsmaptoolpan.cpp
265265
qgsmaptoolzoom.cpp
266266
qgsmaplayerconfigwidget.cpp
267+
qgsmenuheader.cpp
267268
qgsmessagebar.cpp
268269
qgsmessagebaritem.cpp
269270
qgsmessagelogviewer.cpp
@@ -413,6 +414,7 @@ SET(QGIS_GUI_MOC_HDRS
413414
qgsmaptoolpan.h
414415
qgsmaptoolzoom.h
415416
qgsmaplayerconfigwidget.h
417+
qgsmenuheader.h
416418
qgsmessagebar.h
417419
qgsmessagebaritem.h
418420
qgsmessagelogviewer.h

‎src/gui/qgsmenuheader.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
#define LABEL_SIZE 20 //label rect height
23+
#define LABEL_MARGIN 4 //spacing between label box and text
24+
25+
QgsMenuHeader::QgsMenuHeader( const QString &text, QWidget *parent )
26+
: QWidget( parent )
27+
, mText( text )
28+
{
29+
int textMinWidth = fontMetrics().width( mText );
30+
mMinWidth = 2 * LABEL_MARGIN + textMinWidth;
31+
setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
32+
updateGeometry();
33+
}
34+
35+
QSize QgsMenuHeader::minimumSizeHint() const
36+
{
37+
return QSize( mMinWidth, LABEL_SIZE );
38+
}
39+
40+
QSize QgsMenuHeader::sizeHint() const
41+
{
42+
return QSize( mMinWidth, LABEL_SIZE );
43+
}
44+
45+
void QgsMenuHeader::paintEvent( QPaintEvent * )
46+
{
47+
QPainter painter( this );
48+
QPalette pal = QPalette( qApp->palette() );
49+
QColor headerBgColor = pal.color( QPalette::Mid );
50+
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(), LABEL_SIZE ) );
56+
57+
//draw header text
58+
painter.setPen( headerTextColor );
59+
painter.drawText( QRect( LABEL_MARGIN, 0, width() - 2 * LABEL_MARGIN, LABEL_SIZE ),
60+
Qt::AlignLeft | Qt::AlignVCenter, mText );
61+
painter.end();
62+
}
63+
64+
QgsMenuHeaderWidgetAction::QgsMenuHeaderWidgetAction( const QString &text, QObject *parent )
65+
: QWidgetAction( parent )
66+
{
67+
QgsMenuHeader *w = new QgsMenuHeader( text, nullptr );
68+
setDefaultWidget( w ); //transfers ownership
69+
}

‎src/gui/qgsmenuheader.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/***************************************************************************
2+
qgsmenuheader.h
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+
#ifndef QGSMENUHEADER_H
18+
#define QGSMENUHEADER_H
19+
20+
#include <QWidget>
21+
#include <QWidgetAction>
22+
#include "qgis_gui.h"
23+
#include "qgis.h"
24+
25+
/** \ingroup gui
26+
* \class QgsMenuHeader
27+
* Custom widget for displaying subheaders within a QMenu in a standard style.
28+
* \since QGIS 3.0
29+
* \see QgsMenuHeaderWidgetAction()
30+
*/
31+
class GUI_EXPORT QgsMenuHeader : public QWidget
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
37+
/**
38+
* Constructor for QgsMenuHeader, showing the specified \a text.
39+
*/
40+
explicit QgsMenuHeader( const QString &text, QWidget *parent SIP_TRANSFERTHIS = nullptr );
41+
42+
virtual QSize minimumSizeHint() const override;
43+
virtual QSize sizeHint() const override;
44+
45+
protected:
46+
47+
void paintEvent( QPaintEvent *event ) override;
48+
49+
private:
50+
int mMinWidth = 0;
51+
QString mText;
52+
53+
};
54+
55+
/** \ingroup gui
56+
* \class QgsMenuHeader
57+
* Custom QWidgetAction for displaying subheaders within a QMenu in a standard style.
58+
* \since QGIS 3.0
59+
* \see QgsMenuHeader()
60+
*/
61+
class GUI_EXPORT QgsMenuHeaderWidgetAction: public QWidgetAction
62+
{
63+
Q_OBJECT
64+
65+
public:
66+
67+
/**
68+
* Constructor for QgsMenuHeaderWidgetAction, showing the specified \a text.
69+
*/
70+
QgsMenuHeaderWidgetAction( const QString &text, QObject *parent = nullptr );
71+
72+
};
73+
74+
#endif //QGSMENUHEADER_H

0 commit comments

Comments
 (0)
Please sign in to comment.