maxLibQt
RoundedMessageBox.h
Go to the documentation of this file.
1 /*
2  RoundedMessageBox
3  https://github.com/mpaperno/maxLibQt
4 
5  COPYRIGHT: (c)2019 Maxim Paperno; All Rights Reserved.
6  Contact: http://www.WorldDesign.com/contact
7 
8  LICENSE:
9 
10  Commercial License Usage
11  Licensees holding valid commercial licenses may use this file in
12  accordance with the terms contained in a written agreement between
13  you and the copyright holder.
14 
15  GNU General Public License Usage
16  Alternatively, this file may be used under the terms of the GNU
17  General Public License as published by the Free Software Foundation,
18  either version 3 of the License, or (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
26 */
27 
28 #ifndef ROUNDEDMESSAGEBOX_H
29 #define ROUNDEDMESSAGEBOX_H
30 
31 #include <QMessageBox>
32 #include <QPainter>
33 #include <QPalette>
34 #include <QStyle>
35 #include <QStyleOption>
36 
105 {
106  Q_OBJECT
107  public:
108  explicit RoundedMessageBox(QWidget *parent = nullptr) :
110  {
111  // The FramelessWindowHint flag and WA_TranslucentBackground attribute are vital.
114  }
115 
116  qreal radius = 0.0;
117  qreal borderWidth = -1.0;
118 
119  protected:
120  void paintEvent(QPaintEvent *) override
121  {
123  return; // nothing to do
124 
125  QPainter p(this);
127 
128  // Have style sheet?
130  // Let QStylesheetStyle have its way with us.
131  QStyleOption opt;
132  opt.initFrom(this);
133  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
134  p.end();
135  return;
136  }
137 
138  // Paint thyself.
139  QRectF rect(QPointF(0, 0), size());
140  // Check for a border size.
141  qreal penWidth = borderWidth;
142  if (penWidth < 0.0) {
143  QStyleOption opt;
144  opt.initFrom(this);
145  penWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this);
146  }
147  // Got pen?
148  if (penWidth > 0.0) {
149  p.setPen(QPen(palette().brush(foregroundRole()), penWidth));
150  // Ensure border fits inside the available space.
151  const qreal dlta = penWidth * 0.5;
152  rect.adjust(dlta, dlta, -dlta, -dlta);
153  }
154  else {
155  // QPainter comes with a default 1px pen when initialized on a QWidget.
156  p.setPen(Qt::NoPen);
157  }
158  // Set the brush from palette role.
159  p.setBrush(palette().brush(backgroundRole()));
160  // Got radius? Otherwise draw a quicker rect.
161  if (radius > 0.0)
163  else
164  p.drawRect(rect);
165 
166  // C'est finí
167  p.end();
168  }
169 };
170 
171 #endif // ROUNDEDMESSAGEBOX_H
The RoundedMessageBox class is a frameless QMessageBox implementation.
const QPalette & palette() const const
bool end()
void setRenderHint(QPainter::RenderHint hint, bool on)
AbsoluteSize
RoundedMessageBox(QWidget *parent=nullptr)
QStyle * style() const const
virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QPalette::ColorRole foregroundRole() const const
void initFrom(const QWidget *widget)
QSize size() const const
void drawRect(const QRectF &rectangle)
bool testAttribute(Qt::WidgetAttribute attribute) const const
void setPen(const QColor &color)
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
QPalette::ColorRole backgroundRole() const const
void setBrush(const QBrush &brush)
QRect rect() const const
WA_TranslucentBackground
void setWindowFlags(Qt::WindowFlags type)
void paintEvent(QPaintEvent *) override
PM_DefaultFrameWidth
qreal borderWidth
-1 = use style hint frame width; 0 = no border; > 0 = use this width.
void adjust(int dx1, int dy1, int dx2, int dy2)
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const=0
qreal radius
desired radius in absolute pixels
QObject * parent() const const
FramelessWindowHint