Skip to content

Commit

Permalink
core/toolmenu: Highlight the menu item of the current stack widget.
Browse files Browse the repository at this point in the history
The highlighting is done when a tool is detached.

Signed-off-by: andreidanila1 <[email protected]>
  • Loading branch information
andreidanila1 committed Dec 23, 2024
1 parent 08c16a9 commit 0d7a45a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
8 changes: 8 additions & 0 deletions core/include/core/browsemenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class SCOPY_CORE_EXPORT BrowseMenu : public QWidget

ToolMenu *toolMenu() const;

public Q_SLOTS:
void onToolStackChanged(QString id);

Q_SIGNALS:
void requestTool(QString tool);
void requestSave();
Expand All @@ -65,7 +68,12 @@ class SCOPY_CORE_EXPORT BrowseMenu : public QWidget
QSpacerItem *m_spacer;
ToolMenu *m_toolMenu;
QPushButton *m_btnCollapse;
QMap<QString, QPushButton *> m_btnsMap;
bool m_collapsed;

const QString HOME_ID = "home";
const QString PREFERENCES_ID = "preferences";
const QString ABOUT_ID = "about";
};
} // namespace scopy

Expand Down
3 changes: 0 additions & 3 deletions core/include/core/toolmenuitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,19 @@ class ToolMenuItem : public QPushButton
bool eventFilter(QObject *watched, QEvent *event);

void setName(QString str);
void setSelected(bool en);

QString getId() const;
Q_SIGNALS:
void doubleclick();

public Q_SLOTS:
void setDisabled(bool disabled);
void updateItem();

protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);

private:
// QPushButton *m_toolBtn;
CustomPushButton *m_toolRunBtn;

QString m_uuid;
Expand Down
3 changes: 2 additions & 1 deletion core/include/core/toolmenumanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public Q_SLOTS:

Q_SIGNALS:
void requestToolSelect(QString id);
void toolStackChanged(QString id);

private Q_SLOTS:
void updateTool(QWidget *old);
void updateMenuAfterDetach(ToolMenuItem *toolMenuItem, QString id);
void updateToolAttached(bool oldAttach, ToolMenuItem *toolMenuItem);

private:
Expand All @@ -66,7 +68,6 @@ private Q_SLOTS:
void detachSuccesful(ToolMenuItem *toolMenuItem);
void attachSuccesful(ToolMenuItem *toolMenuItem);
void showTool(ToolMenuItem *toolMenuItem);
void selectTool(ToolMenuItem *toolMenuItem, bool on);
void setTmeAttached(ToolMenuEntry *tme);
MenuSectionCollapseWidget *createMenuSectionItem(QString deviceName, QString uri = "");
ToolMenuItem *createToolMenuItem(ToolMenuEntry *tme, QWidget *parent = nullptr);
Expand Down
16 changes: 13 additions & 3 deletions core/src/browsemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ BrowseMenu::BrowseMenu(QWidget *parent)
homeBtn->setCheckable(true);
homeBtn->setIconSize(QSize(32, 32));
homeBtn->setStyleSheet("text-align: left");
connect(homeBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool("home"); });
connect(homeBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool(HOME_ID); });
m_btnsMap[HOME_ID] = homeBtn;

QWidget *saveLoadWidget = new QWidget(m_content);
saveLoadWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
Expand All @@ -87,14 +88,16 @@ BrowseMenu::BrowseMenu(QWidget *parent)
":/gui/icons/" + Style::getAttribute(json::theme::icon_theme_folder) +
"/icons/preferences.svg",
m_content);
connect(preferencesBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool("preferences"); });
connect(preferencesBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool(PREFERENCES_ID); });
preferencesBtn->setCheckable(true);
m_btnsMap[PREFERENCES_ID] = preferencesBtn;

QPushButton *aboutBtn = createBtn(
"About", ":/gui/icons/" + Style::getAttribute(json::theme::icon_theme_folder) + "/icons/info.svg",
m_content);
connect(aboutBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool("about"); });
connect(aboutBtn, &QPushButton::clicked, this, [=]() { Q_EMIT requestTool(ABOUT_ID); });
aboutBtn->setCheckable(true);
m_btnsMap[ABOUT_ID] = aboutBtn;

QLabel *logo = createScopyLogo(m_content);

Expand All @@ -121,6 +124,13 @@ BrowseMenu::~BrowseMenu() {}

ToolMenu *BrowseMenu::toolMenu() const { return m_toolMenu; }

void BrowseMenu::onToolStackChanged(QString id)
{
if(m_btnsMap.contains(id)) {
m_btnsMap[id]->setChecked(true);
}
}

void BrowseMenu::add(QWidget *w, QString name, MenuAlignment position)
{
int spacerIndex = m_contentLay->indexOf(m_spacer);
Expand Down
1 change: 1 addition & 0 deletions core/src/scopymainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ ScopyMainWindow::ScopyMainWindow(QWidget *parent)
connect(dm, &DeviceManager::requestTool, m_toolMenuManager, &ToolMenuManager::showMenuItem);
connect(m_toolMenuManager, &ToolMenuManager::requestToolSelect, ts, &ToolStack::show);
connect(m_toolMenuManager, &ToolMenuManager::requestToolSelect, dtm, &DetachedToolWindowManager::show);
connect(m_toolMenuManager, &ToolMenuManager::toolStackChanged, browseMenu, &BrowseMenu::onToolStackChanged);
connect(hp, &ScopyHomePage::displayNameChanged, m_toolMenuManager, &ToolMenuManager::onDisplayNameChanged);

connect(hp, &ScopyHomePage::newDeviceAvailable, dm, &DeviceManager::addDevice);
Expand Down
4 changes: 0 additions & 4 deletions core/src/toolmenuitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ void ToolMenuItem::setName(QString str)
setText(m_name);
}

void ToolMenuItem::setSelected(bool en) { setDynamicProperty(this, "selected", en); }

void ToolMenuItem::setDisabled(bool disabled) { setDisabled(disabled); }

void ToolMenuItem::updateItem()
{
ToolMenuEntry *tme = dynamic_cast<ToolMenuEntry *>(QObject::sender());
Expand Down
32 changes: 16 additions & 16 deletions core/src/toolmenumanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ void ToolMenuManager::updateTool(QWidget *old)
qDebug(CAT_TOOLMENUMANAGER) << "updating tool for " << tme->name() << " - " << id;
}

void ToolMenuManager::updateMenuAfterDetach(ToolMenuItem *toolMenuItem, QString id)
{
if(id == toolMenuItem->getId()) {
toolMenuItem->setChecked(true);
}
}

void ToolMenuManager::updateToolAttached(bool oldAttach, ToolMenuItem *toolMenuItem)
{
ToolMenuEntry *tme = dynamic_cast<ToolMenuEntry *>(QObject::sender());
Expand Down Expand Up @@ -192,14 +199,14 @@ void ToolMenuManager::updateToolAttached(bool oldAttach, ToolMenuItem *toolMenuI
// the selected tool just attached, so it will be at the top of the stack, therefore highlighted
if(toolMenuItem) {
toolMenuItem->setChecked(true);
toolMenuItem->setSelected(true);
}
} else {
// the top tool just detached, so we need to find the tool that is positioned at the new top of the
// stack.
if(toolMenuItem) {
toolMenuItem->toggle();
QWidget *wAfterDetach = m_ts->currentWidget();
if(!wAfterDetach) {
return;
}
QString toolMenuItemId = m_ts->getKey(wAfterDetach);
Q_EMIT toolStackChanged(toolMenuItemId);
}
}

Expand Down Expand Up @@ -231,7 +238,7 @@ void ToolMenuManager::detachSuccesful(ToolMenuItem *toolMenuItem)
{
QButtonGroup *menuBtnGroup = m_toolMenu->btnGroup();
if(toolMenuItem) {
toolMenuItem->setSelected(false);
toolMenuItem->setCheckable(false);
menuBtnGroup->removeButton(toolMenuItem);
}
}
Expand All @@ -240,6 +247,7 @@ void ToolMenuManager::attachSuccesful(ToolMenuItem *toolMenuItem)
{
QButtonGroup *menuBtnGroup = m_toolMenu->btnGroup();
if(toolMenuItem) {
toolMenuItem->setCheckable(true);
menuBtnGroup->addButton(toolMenuItem);
}
}
Expand All @@ -252,14 +260,6 @@ void ToolMenuManager::showTool(ToolMenuItem *toolMenuItem)
Q_EMIT requestToolSelect(toolMenuItem->getId());
}

void ToolMenuManager::selectTool(ToolMenuItem *toolMenuItem, bool on)
{
QButtonGroup *menuBtnGroup = m_toolMenu->btnGroup();
if(menuBtnGroup->id(toolMenuItem) != -1) {
toolMenuItem->setSelected(on);
}
}

void ToolMenuManager::setTmeAttached(ToolMenuEntry *tme)
{
Preferences *p = Preferences::GetInstance();
Expand Down Expand Up @@ -296,9 +296,9 @@ ToolMenuItem *ToolMenuManager::createToolMenuItem(ToolMenuEntry *tme, QWidget *p
connect(toolMenuItem->getToolRunBtn(), &QPushButton::toggled, tme, &ToolMenuEntry::runToggled);
connect(toolMenuItem->getToolRunBtn(), &QPushButton::clicked, tme, &ToolMenuEntry::runClicked);
connect(toolMenuItem, &QPushButton::clicked, this, [=]() { Q_EMIT requestToolSelect(toolMenuItem->getId()); });
connect(toolMenuItem, &QPushButton::toggled, this,
[this, toolMenuItem](bool on) { selectTool(toolMenuItem, on); });
connect(toolMenuItem, &ToolMenuItem::doubleclick, this, [this, tme]() { setTmeAttached(tme); });
connect(this, &ToolMenuManager::toolStackChanged, this,
[this, toolMenuItem](QString id) { updateMenuAfterDetach(toolMenuItem, id); });
connect(tme, &ToolMenuEntry::updateToolEntry, toolMenuItem, &ToolMenuItem::updateItem);

return toolMenuItem;
Expand Down

0 comments on commit 0d7a45a

Please sign in to comment.