summaryrefslogtreecommitdiff
path: root/widgets/CompactButtonSize.hpp
blob: d94554568c79524eab32f153a6e114b1b3206b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef COMPACT_BUTTON_SIZE_HPP
#define COMPACT_BUTTON_SIZE_HPP

#include <QPushButton>
#include <QStyle>
#include <QStyleOptionButton>

// Measure the label and native decoration without the style's recommended
// dialog-button width (75 logical pixels on Windows). Painting stays native.
inline QSize compactButtonSize (QPushButton const * button)
{
  QStyleOptionButton option;
  option.initFrom (button);
  if (button->autoDefault ()) option.features |= QStyleOptionButton::AutoDefaultButton;
  auto contents = button->fontMetrics ().size (Qt::TextShowMnemonic, button->text ());
  // The text is already measured above. An empty option text avoids the
  // Windows style's dialog-width floor without discarding its frame margins.
  return button->style ()->sizeFromContents (QStyle::CT_PushButton, &option, contents, button);
}

#endif