|
| 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 | +} |
0 commit comments