Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu activation #81

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/include/oclero/qlementine/utils/StateUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ MouseState getMouseState(QStyle::State const& state);
MouseState getMouseState(bool const pressed, bool const hovered, bool const enabled);
MouseState getToolButtonMouseState(QStyle::State const& state);
MouseState getMenuItemMouseState(QStyle::State const& state);
MouseState getComboBoxItemMouseState(QStyle::State const& state);
MouseState getTabItemMouseState(QStyle::State const& state, const bool tabIsHovered);
ColorRole getColorRole(QStyle::State const& state, bool const isDefault);
ColorRole getColorRole(bool checked, bool const isDefault);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/style/Delegates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void ComboBoxDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const
p->setPen(QPen(color, lineW, Qt::SolidLine, Qt::FlatCap));
p->drawLine(QPointF{ x, y1 }, QPointF{ x, y2 });
} else {
const auto mouse = getMenuItemMouseState(opt.state);
const auto mouse = getComboBoxItemMouseState(opt.state);

// Background.
const auto& bgRect = contentRect;
Expand Down
41 changes: 28 additions & 13 deletions lib/src/style/EventFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ bool MenuEventFilter::eventFilter(QObject* watchedObject, QEvent* evt) {

switch (type) {
case QEvent::Type::Show: {
_mousePressed = false;

// Place the QMenu correctly by making up for the drop shadow margins.
// It'll be reset before every show, so we can safely move it every time.
// Submenus should already be placed correctly, so there's no need to translate their geometry.
Expand All @@ -338,7 +340,14 @@ bool MenuEventFilter::eventFilter(QObject* watchedObject, QEvent* evt) {
});
}
} break;
case QEvent::Type::MouseMove: {
if (static_cast<QMouseEvent*>(evt)->buttons()) {
_mousePressed = true;
}
}
break;
case QEvent::Type::MouseButtonPress: {
_mousePressed = true;
const auto* mouseEvt = static_cast<QMouseEvent*>(evt);
const auto mousePos = mouseEvt->pos();
if (const auto* action = _menu->actionAt(mousePos)) {
Expand All @@ -350,6 +359,14 @@ bool MenuEventFilter::eventFilter(QObject* watchedObject, QEvent* evt) {
}
} break;
case QEvent::Type::MouseButtonRelease: {
if (!_mousePressed) {
if (evt == _mouseEventToNotFilter) {
_mouseEventToNotFilter = nullptr; // do not delete the event, it will be deleted for us
return false; // let it go to the widget
}
return true; // ignore
}
_mousePressed = false;
const auto* mouseEvt = static_cast<QMouseEvent*>(evt);
const auto mousePos = mouseEvt->pos();
if (auto* action = _menu->actionAt(mousePos)) {
Expand All @@ -358,14 +375,10 @@ bool MenuEventFilter::eventFilter(QObject* watchedObject, QEvent* evt) {

if (action->menu() == nullptr) {
flashAction(action, _menu, [this, action]() {
// The call to QAction::trigger might destroy the menu or the actions.
const QPointer<QMenu> menu_guard(_menu);
action->trigger();
if (menu_guard) {
if (auto* top_menu = getTopLevelMenu(menu_guard)) {
top_menu->close();
}
}
QPoint p = _menu->actionGeometry(action).center();
_mouseEventToNotFilter = new QMouseEvent(QEvent::MouseButtonRelease, p, _menu->mapToGlobal(p),
Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
QCoreApplication::postEvent(_menu, _mouseEventToNotFilter);
});
return true;
}
Expand All @@ -383,19 +396,22 @@ bool MenuEventFilter::eventFilter(QObject* watchedObject, QEvent* evt) {
ComboboxItemViewFilter::ComboboxItemViewFilter(QComboBox* comboBox, QListView* view)
: QObject(view)
, _comboBox(comboBox)
, _view(view) {
, _view(view)
, _initialMaxHeight(view->maximumHeight()) {
_view->installEventFilter(this);

auto* comboBoxPopup = _view->parentWidget();
comboBoxPopup->installEventFilter(this);

/*
const auto childWidgets = comboBoxPopup->findChildren<QWidget*>();
for (auto* child : childWidgets) {
if (child->inherits("QComboBoxPrivateScroller")) {
child->setFixedHeight(0);
child->setVisible(false);
}
}
*/

_comboBox->installEventFilter(this);
}
Expand Down Expand Up @@ -425,7 +441,7 @@ void ComboboxItemViewFilter::fixViewGeometry() {
const auto borderWidth = qlementineStyle->theme().borderWidth;
const auto width =
std::max(comboBox->width(), _view->sizeHintForColumn(0) + shadowWidth * 2) + hMargin * 2 + borderWidth * 2;
const auto height = viewMinimumSizeHint().height();
const auto height = std::min(800, viewMinimumSizeHint().height());
_view->setFixedWidth(width);
_view->setFixedHeight(height);
_view->parentWidget()->adjustSize();
Expand All @@ -435,11 +451,10 @@ QSize ComboboxItemViewFilter::viewMinimumSizeHint() const {
// QListView::minimumSizeHint() doesn't give the correct minimumHeight,
// so we have to compute it.
const auto rowCount = _view->model()->rowCount();
const auto maxHeight = _view->maximumHeight();
auto height = 0;
for (auto i = 0; i < rowCount && height <= maxHeight; ++i) {
for (auto i = 0; i < rowCount && height <= _initialMaxHeight; ++i) {
const auto rowSizeHint = _view->sizeHintForRow(i);
height = std::min(maxHeight, height + rowSizeHint);
height = std::min(_initialMaxHeight, height + rowSizeHint);
}
// It looks like it is OK for the width, though.
const auto width = _view->sizeHintForColumn(0);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/style/EventFilters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class MenuEventFilter : public QObject {

private:
QMenu* _menu{ nullptr };

// this is roughly copied from how QMenuPrivate::mouseDown detects whether the mouse is down
bool _mousePressed{ false };
QEvent *_mouseEventToNotFilter{}; // please do not dereference it, this is just to close properly the QMenu
};

class ComboboxItemViewFilter : public QObject {
Expand All @@ -84,6 +88,7 @@ class ComboboxItemViewFilter : public QObject {
QSize viewMinimumSizeHint() const;
QComboBox* _comboBox{ nullptr };
QListView* _view{ nullptr };
int _initialMaxHeight{ 0 };
};

// Works for both QTextEdit and QPlainTextEdit
Expand Down
14 changes: 9 additions & 5 deletions lib/src/style/QlementineStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,13 @@ void QlementineStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption* opt
const auto& color = listItemRowBackgroundColor(mouse, alternate);
p->fillRect(optItem->rect, color);

// Draw selection color in the arrow area.
drawPrimitive(PE_PanelItemViewItem, opt, p, w);
// Draw selection color in the arrow area,
// except in comboboxes as selection drawing is handled by the delegate already.
const auto* popup = w->parentWidget();
const auto isComboBoxPopupContainer = popup != nullptr && popup->inherits("QComboBoxPrivateContainer");
if (!isComboBoxPopupContainer) {
drawPrimitive(PE_PanelItemViewItem, opt, p, w);
}
}
return;
case PE_PanelStatusBar: {
Expand Down Expand Up @@ -1434,7 +1439,7 @@ void QlementineStyle::drawControl(ControlElement ce, const QStyleOption* opt, QP
}

// Icon.
const auto iconSpace = optMenuItem->maxIconWidth > 0 ? optMenuItem->maxIconWidth + spacing : 0;
const auto iconSpace = !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus) && optMenuItem->maxIconWidth > 0 ? optMenuItem->maxIconWidth + spacing : 0;
const auto pixmap = getPixmap(optMenuItem->icon, _impl->theme.iconSize, mouse, checkState, w);
if (!pixmap.isNull()) {
const auto& colorizedPixmap = getColorizedPixmap(pixmap, autoIconColor(w), fgColor, fgColor);
Expand Down Expand Up @@ -3812,8 +3817,7 @@ QSize QlementineStyle::sizeFromContents(
const auto shortcutW = hasShortcut ? 3 * spacing - reservedShortcutW : 0;

// Icon.
const auto hasIcon = !optMenuItem->icon.isNull();
const auto iconW = hasIcon ? iconSize.width() + spacing : 0;
const auto iconW = !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus) && optMenuItem->maxIconWidth > 0 ? optMenuItem->maxIconWidth + spacing : 0;

// Check or Radio.
const auto hasCheck =
Expand Down
1 change: 1 addition & 0 deletions lib/src/utils/MenuUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FlashActionHelper : QObject {
if (_onAnimationFinished) {
_onAnimationFinished();
}
deleteLater();
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/src/utils/StateUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ MouseState getMenuItemMouseState(QStyle::State const& state) {
}
}

MouseState getComboBoxItemMouseState(QStyle::State const& state) {
if (!state.testFlag(QStyle::State_Enabled)) {
return MouseState::Disabled;
} else if (state.testFlag(QStyle::State_Sunken)) {
return MouseState::Pressed;
} else if (state.testFlag(QStyle::State_Selected)) {
return MouseState::Hovered;
} else {
return MouseState::Transparent;
}
}

MouseState getTabItemMouseState(QStyle::State const& state, const bool tabIsHovered) {
const auto selected = state.testFlag(QStyle::State_Selected);
if (selected || tabIsHovered) {
Expand Down
Loading