Skip to content

Commit

Permalink
fixed QCocoaButtonActionMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
kleuter committed Jun 17, 2021
1 parent a5ab865 commit 7779d95
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

The library helps to make use of native macOS controls in a Qt app. On Windows (and others?) it should provide a decent fallback.

Tested with Qt 5 (5.6.3 and Qt 5.13.2).
Tested with Qt 5 (5.6.3 and Qt 5.15.2).

Many of those controls are used in our [Macs Fan Control](https://crystalidea.com/macs-fan-control) and [AnyToISO](https://crystalidea.com/anytoiso) apps.

The code is far from being ideal so you're welcome to contribute.

To compile on macOS we use Qt Creator and Visual Studio 2017 Community on Windows
To compile on macOS we use Qt Creator and Visual Studio 2019 Community on Windows

## Some examples:

Expand Down
4 changes: 3 additions & 1 deletion qcocoabutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class QCocoaButton : public QCocoaWidget

QCocoaButton(QWidget *parent, CocoaButtonPrivate *customPrivate);

void showEvent(QShowEvent *event) override;

public slots:

virtual void setText(const QString &text);
Expand Down Expand Up @@ -89,7 +91,7 @@ public slots:

friend class CocoaButtonPrivate;
std::unique_ptr<CocoaButtonPrivate> pimpl;
BezelStyle style;
BezelStyle _bezelStyle = BezelStyle::Rounded;
};

#endif // qcocoabutton_h__
19 changes: 9 additions & 10 deletions qcocoabutton.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@

setupLayout(pimpl->getNSButton());

setBezelStyle(QCocoaButton::Rounded);

//setMinimumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); // без этоо дает странные варнинги в дебаге:
// QWidget::setMinimumSize: (/QCocoaButton) The largest allowed size is (16777215,16777215)
// либо
// QWidget::setMinimumSize: (/QCocoaButton) Negative sizes (-1599184954,30) are not possible

pimpl->updateSize();
// calling a virtual function updateSize from a constructor is a bad idea, it's now done in the showEvent method
}

QCocoaButton::QCocoaButton(QWidget *parent, CocoaButtonPrivate *customPrivate)
Expand All @@ -30,8 +23,13 @@
pimpl.reset(customPrivate);

setupLayout(pimpl->getNSButton());
}

pimpl->updateSize();
void QCocoaButton::showEvent(QShowEvent *event)
{
Q_UNUSED(event);

setBezelStyle(_bezelStyle); // init default bezel style, will call updateSize
}

QCocoaButton::~QCocoaButton()
Expand All @@ -41,7 +39,8 @@

void QCocoaButton::setBezelStyle(BezelStyle bezelStyle)
{
style = bezelStyle;
_bezelStyle = bezelStyle;

switch(bezelStyle) {
case QCocoaButton::Disclosure:
case QCocoaButton::Circular:
Expand Down
6 changes: 3 additions & 3 deletions qcocoabutton_p.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ -(void)clicked {

bool CocoaButtonPrivate::isSpecialButton() // no title and icon
{
return (_cocoaButton->style == QCocoaButton::HelpButton ||
_cocoaButton->style == QCocoaButton::Disclosure ||
_cocoaButton->style == QCocoaButton::RoundedDisclosure);
return (_cocoaButton->_bezelStyle == QCocoaButton::HelpButton ||
_cocoaButton->_bezelStyle == QCocoaButton::Disclosure ||
_cocoaButton->_bezelStyle == QCocoaButton::RoundedDisclosure);
}

void CocoaButtonPrivate::updateSize()
Expand Down
4 changes: 0 additions & 4 deletions qcocoabuttonactionmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ class QCocoaButtonActionMenu : public QCocoaButton
void setMenu(QMenu *menu) override;
void setText(const QString &text) override;

void setFixedWidth(int w); // если мы не хотим, чтобы кнопка себя растягивала при смене текста не ней
int fixedWidth() const { return _fixedWidth; }

QString text() const { return _text;}

private:

QString _text;
int _fixedWidth = 0;

#else

Expand Down
25 changes: 7 additions & 18 deletions qcocoabuttonactionmenu.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#import <AppKit/NSPopUpButton.h>
#import <AppKit/NSPopUpButtonCell.h>

const int FIXED_WIDTH_ICON_ONLY = 44;
const int FIXED_HEIGHT = 28;

class CocoaButtonPrivateActionMenu : public CocoaButtonPrivate
{
public:
Expand Down Expand Up @@ -33,23 +36,16 @@ void setImage(NSImage *img) override

void updateSize() override
{
QCocoaButtonActionMenu *btnCocoa = static_cast<QCocoaButtonActionMenu *>(getButton());

NSRect frame = [_nsButton frame];

[_nsButton setFrame:frame];

if ([_nsButton imagePosition] == NSImageOnly)
_cocoaButton->setFixedSize(44, NSHeight(frame));
_cocoaButton->setFixedSize(FIXED_WIDTH_ICON_ONLY, FIXED_HEIGHT);
else
{
if (btnCocoa->fixedWidth())
_cocoaButton->setFixedSize(btnCocoa->fixedWidth(), NSHeight(frame));
else
{
[_nsButton sizeToFit];
_cocoaButton->setFixedSize(NSWidth(frame), NSHeight(frame));
}
[_nsButton sizeToFit];
_cocoaButton->setFixedSize(NSWidth(frame), FIXED_HEIGHT);
}
}

Expand Down Expand Up @@ -92,7 +88,7 @@ void updateSize() override
QCocoaButtonActionMenu::QCocoaButtonActionMenu(QWidget *parent)
: QCocoaButton(parent, new CocoaButtonPrivateActionMenu(this) )
{
setBezelStyle(QCocoaButton::TexturedRounded);
_bezelStyle = QCocoaButton::TexturedRounded;

NSPopUpButton *btn = static_cast<NSPopUpButton *>(pimpl->getNSButton());
NSPopUpButtonCell *buttonCell = btn.cell;
Expand Down Expand Up @@ -132,10 +128,3 @@ void updateSize() override
pimpl->updateSize();
}
}

void QCocoaButtonActionMenu::setFixedWidth(int w)
{
_fixedWidth = w;

pimpl->updateSize();
}

0 comments on commit 7779d95

Please sign in to comment.