Skip to content

Commit

Permalink
Make room for more items when toolbars are aligned vertically (#2970)
Browse files Browse the repository at this point in the history
Decrease toolbar icon sizes and make more room from items when toolbars are in vertical mode

Re #2964
  • Loading branch information
ptsavol authored Oct 4, 2024
1 parent f11d7bb commit 87b6a13
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
40 changes: 27 additions & 13 deletions spinetoolbox/widgets/project_item_drag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def _reset(self):
self.drag_start_pos = None
self.pixmap = None
self.mime_data = None
self.setCursor(Qt.OpenHandCursor)
self.setCursor(Qt.CursorShape.OpenHandCursor)

def mousePressEvent(self, event):
super().mousePressEvent(event)
self.setCursor(Qt.ClosedHandCursor)
self.setCursor(Qt.CursorShape.ClosedHandCursor)

def mouseMoveEvent(self, event):
"""Start dragging action if needed"""
super().mouseMoveEvent(event)
if not event.buttons() & Qt.LeftButton:
if not event.buttons() & Qt.MouseButton.LeftButton:
return
if not self.drag_start_pos:
return
Expand All @@ -62,7 +62,7 @@ def mouseReleaseEvent(self, event):

def enterEvent(self, event):
super().enterEvent(event)
self.setCursor(Qt.OpenHandCursor)
self.setCursor(Qt.CursorShape.OpenHandCursor)


class NiceButton(QToolButton):
Expand All @@ -73,20 +73,27 @@ def __init__(self, *args, **kwargs):
self.setFont(font)

def setText(self, text):
super().setText(fill(text, width=12, break_long_words=False))
if self.toolButtonStyle() == Qt.ToolButtonStyle.ToolButtonTextUnderIcon:
super().setText(fill(text, width=12, break_long_words=False))
elif self.toolButtonStyle() == Qt.ToolButtonStyle.ToolButtonTextBesideIcon:
txt_l = text.strip().split() # Remove all newlines
txt = " ".join(txt_l)
trunc = txt[:12] + ".." if len(txt) > 14 else txt # Truncate text to 14 characters with .. in the end
super().setText(trunc)

def set_orientation(self, orientation):
if orientation == Qt.Orientation.Horizontal:
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
self.setStyleSheet("QToolButton{margin: 16px 2px 2px 2px;}")
else:
self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
self.setStyleSheet("QToolButton{margin: 2px;}")


class ProjectItemButtonBase(ProjectItemDragMixin, NiceButton):
def __init__(self, toolbox, item_type, icon, parent=None):
def __init__(self, toolbox, item_type, icon, style, parent=None):
super().__init__(parent=parent)
self.setToolButtonStyle(style)
self._toolbox = toolbox
self.item_type = item_type
self._icon = icon
Expand All @@ -113,7 +120,7 @@ def _handle_drag_about_to_start(self):
def mousePressEvent(self, event):
"""Register drag start position"""
super().mousePressEvent(event)
if event.button() == Qt.LeftButton:
if event.button() == Qt.MouseButton.LeftButton:
self.drag_start_pos = event.position().toPoint()
self.pixmap = self.icon().pixmap(self.iconSize())
self.mime_data = QMimeData()
Expand All @@ -126,11 +133,15 @@ def _make_mime_data_text(self):
class ProjectItemButton(ProjectItemButtonBase):
double_clicked = Signal()

def __init__(self, toolbox, item_type, icon, parent=None):
super().__init__(toolbox, item_type, icon, parent=parent)
def __init__(self, toolbox, item_type, icon, style, parent=None):
super().__init__(toolbox, item_type, icon, style, parent=parent)
self.setToolTip(f"<p>Drag-and-drop this onto the Design View to create a new <b>{item_type}</b> item.</p>")
self.setText(item_type)

def set_orientation(self, orientation):
super().set_orientation(orientation)
self.setText(self.item_type)

def _make_mime_data_text(self):
return ",".join([self.item_type, ""])

Expand All @@ -139,12 +150,11 @@ def mouseDoubleClickEvent(self, event):


class ProjectItemSpecButton(ProjectItemButtonBase):
def __init__(self, toolbox, item_type, icon, spec_name="", parent=None):
super().__init__(toolbox, item_type, icon, parent=parent)
def __init__(self, toolbox, item_type, icon, style, spec_name="", parent=None):
super().__init__(toolbox, item_type, icon, style, parent=parent)
self._spec_name = None
self._index = None
self.spec_name = spec_name
self.setText(self.spec_name)

@property
def spec_name(self):
Expand All @@ -156,6 +166,10 @@ def spec_name(self, spec_name):
self.setText(self._spec_name)
self.setToolTip(f"<p>Drag-and-drop this onto the Design View to create a new <b>{self.spec_name}</b> item.</p>")

def set_orientation(self, orientation):
super().set_orientation(orientation)
self.setText(self.spec_name)

def _make_mime_data_text(self):
return ",".join([self.item_type, self.spec_name])

Expand Down
25 changes: 18 additions & 7 deletions spinetoolbox/widgets/toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ def __init__(self, name, toolbox):
self.setObjectName(name.replace(" ", "_"))
self._toolbox = toolbox
self.addWidget(_TitleWidget(self.name(), self))
self.setIconSize(QSize(20, 20))

def name(self):
return self._name

def paintEvent(self, ev):
super().paintEvent(ev)
if self.orientation() == Qt.Vertical:
if self.orientation() == Qt.Orientation.Vertical:
return
layout = self.layout()
title_pos_x = (
Expand Down Expand Up @@ -157,6 +158,8 @@ def _make_tool_button(self, icon, text, slot=None, tip=None):
QToolButton: created button
"""
button = NiceButton()
style = self._get_toolbutton_style()
button.setToolButtonStyle(style)
button.setIcon(icon)
button.setText(text)
if tip is not None:
Expand All @@ -172,6 +175,11 @@ def _icon_from_factory(self, factory):
icon_color = factory.icon_color().darker(120)
return ColoredIcon(icon_file_name, icon_color, self.iconSize(), colored=colored)

def _get_toolbutton_style(self):
if self.orientation() == Qt.Orientation.Horizontal:
return Qt.ToolButtonStyle.ToolButtonTextUnderIcon
return Qt.ToolButtonStyle.ToolButtonTextBesideIcon


class PluginToolBar(ToolBar):
"""A plugin toolbar."""
Expand Down Expand Up @@ -203,7 +211,8 @@ def setup(self, plugin_specs, disabled_names):
for spec in specs:
factory = self._toolbox.item_factories[spec.item_type]
icon = self._icon_from_factory(factory)
button = ProjectItemSpecButton(self._toolbox, spec.item_type, icon, spec.name)
style = self._get_toolbutton_style()
button = ProjectItemSpecButton(self._toolbox, spec.item_type, icon, style, spec.name)
button.setIconSize(self.iconSize())
if spec.name in disabled_names:
button.setEnabled(False)
Expand Down Expand Up @@ -245,7 +254,8 @@ def _add_spec(self, row):
next_row += 1
factory = self._toolbox.item_factories[spec.item_type]
icon = self._icon_from_factory(factory)
button = ProjectItemSpecButton(self._toolbox, spec.item_type, icon, spec.name)
style = self._get_toolbutton_style()
button = ProjectItemSpecButton(self._toolbox, spec.item_type, icon, style, spec.name)
button.setIconSize(self.iconSize())
action = (
self._insert_tool_button(self._actions[next_spec.name], button)
Expand Down Expand Up @@ -289,10 +299,10 @@ def setup(self):
).setIcon(self._icon_from_factory(factory))
menu.addSeparator()
menu.addAction("From specification file...", self._toolbox.import_specification).setIcon(
QIcon(CharIconEngine("\uf067", color=Qt.darkGreen))
QIcon(CharIconEngine("\uf067", color=Qt.GlobalColor.darkGreen))
)
button = self._make_tool_button(QIcon(CharIconEngine("\uf067", color=Qt.darkGreen)), "New...")
button.setPopupMode(QToolButton.InstantPopup)
button = self._make_tool_button(QIcon(CharIconEngine("\uf067", color=Qt.GlobalColor.darkGreen)), "New...")
button.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup)
button.setMenu(menu)


Expand Down Expand Up @@ -335,7 +345,8 @@ def _add_project_item_button(self, item_type, factory):
if factory.is_deprecated():
return
icon = self._icon_from_factory(factory)
button = ProjectItemButton(self._toolbox, item_type, icon)
style = self._get_toolbutton_style()
button = ProjectItemButton(self._toolbox, item_type, icon, style)
self._add_tool_button(button)
self._buttons.append(button)

Expand Down

0 comments on commit 87b6a13

Please sign in to comment.