refactor: clean up WideBar a bit
Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
347ae0a9ad
commit
2367903ac6
@ -1,19 +1,21 @@
|
||||
#include "WideBar.h"
|
||||
#include <QToolButton>
|
||||
#include <QMenu>
|
||||
|
||||
class ActionButton : public QToolButton
|
||||
{
|
||||
class ActionButton : public QToolButton {
|
||||
Q_OBJECT
|
||||
public:
|
||||
ActionButton(QAction * action, QWidget * parent = 0) : QToolButton(parent), m_action(action) {
|
||||
public:
|
||||
ActionButton(QAction* action, QWidget* parent = nullptr) : QToolButton(parent), m_action(action)
|
||||
{
|
||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
||||
|
||||
connect(action, &QAction::changed, this, &ActionButton::actionChanged);
|
||||
connect(this, &ActionButton::clicked, action, &QAction::trigger);
|
||||
|
||||
actionChanged();
|
||||
};
|
||||
private slots:
|
||||
void actionChanged() {
|
||||
private slots:
|
||||
void actionChanged()
|
||||
{
|
||||
setEnabled(m_action->isEnabled());
|
||||
setChecked(m_action->isChecked());
|
||||
setCheckable(m_action->isCheckable());
|
||||
@ -23,10 +25,10 @@ private slots:
|
||||
setHidden(!m_action->isVisible());
|
||||
setFocusPolicy(Qt::NoFocus);
|
||||
}
|
||||
private:
|
||||
QAction * m_action;
|
||||
};
|
||||
|
||||
private:
|
||||
QAction* m_action;
|
||||
};
|
||||
|
||||
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
|
||||
{
|
||||
@ -40,116 +42,102 @@ WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
||||
setMovable(false);
|
||||
}
|
||||
|
||||
struct WideBar::BarEntry {
|
||||
enum Type {
|
||||
None,
|
||||
Action,
|
||||
Separator,
|
||||
Spacer
|
||||
} type = None;
|
||||
QAction *qAction = nullptr;
|
||||
QAction *wideAction = nullptr;
|
||||
};
|
||||
|
||||
|
||||
WideBar::~WideBar()
|
||||
{
|
||||
for(auto *iter: m_entries) {
|
||||
delete iter;
|
||||
}
|
||||
}
|
||||
|
||||
void WideBar::addAction(QAction* action)
|
||||
{
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = addWidget(new ActionButton(action, this));
|
||||
entry->wideAction = action;
|
||||
entry->type = BarEntry::Action;
|
||||
BarEntry entry;
|
||||
entry.bar_action = addWidget(new ActionButton(action, this));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
m_entries.push_back(entry);
|
||||
}
|
||||
|
||||
void WideBar::addSeparator()
|
||||
{
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = QToolBar::addSeparator();
|
||||
entry->type = BarEntry::Separator;
|
||||
BarEntry entry;
|
||||
entry.bar_action = QToolBar::addSeparator();
|
||||
entry.type = BarEntry::Type::Separator;
|
||||
|
||||
m_entries.push_back(entry);
|
||||
}
|
||||
|
||||
auto WideBar::getMatching(QAction* act) -> QList<BarEntry*>::iterator
|
||||
auto WideBar::getMatching(QAction* act) -> QList<BarEntry>::iterator
|
||||
{
|
||||
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry * entry) {
|
||||
return entry->wideAction == act;
|
||||
});
|
||||
|
||||
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry const& entry) { return entry.menu_action == act; });
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
void WideBar::insertActionBefore(QAction* before, QAction* action){
|
||||
void WideBar::insertActionBefore(QAction* before, QAction* action)
|
||||
{
|
||||
auto iter = getMatching(before);
|
||||
if(iter == m_entries.end())
|
||||
if (iter == m_entries.end())
|
||||
return;
|
||||
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = insertWidget((*iter)->qAction, new ActionButton(action, this));
|
||||
entry->wideAction = action;
|
||||
entry->type = BarEntry::Action;
|
||||
BarEntry entry;
|
||||
entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
m_entries.insert(iter, entry);
|
||||
}
|
||||
|
||||
void WideBar::insertActionAfter(QAction* after, QAction* action){
|
||||
void WideBar::insertActionAfter(QAction* after, QAction* action)
|
||||
{
|
||||
auto iter = getMatching(after);
|
||||
if(iter == m_entries.end())
|
||||
if (iter == m_entries.end())
|
||||
return;
|
||||
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = insertWidget((*(iter+1))->qAction, new ActionButton(action, this));
|
||||
entry->wideAction = action;
|
||||
entry->type = BarEntry::Action;
|
||||
BarEntry entry;
|
||||
entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this));
|
||||
entry.menu_action = action;
|
||||
entry.type = BarEntry::Type::Action;
|
||||
|
||||
m_entries.insert(iter + 1, entry);
|
||||
}
|
||||
|
||||
void WideBar::insertSpacer(QAction* action)
|
||||
{
|
||||
auto iter = getMatching(action);
|
||||
if(iter == m_entries.end())
|
||||
if (iter == m_entries.end())
|
||||
return;
|
||||
|
||||
QWidget* spacer = new QWidget();
|
||||
auto* spacer = new QWidget();
|
||||
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = insertWidget((*iter)->qAction, spacer);
|
||||
entry->type = BarEntry::Spacer;
|
||||
BarEntry entry;
|
||||
entry.bar_action = insertWidget(iter->bar_action, spacer);
|
||||
entry.type = BarEntry::Type::Spacer;
|
||||
m_entries.insert(iter, entry);
|
||||
}
|
||||
|
||||
void WideBar::insertSeparator(QAction* before)
|
||||
{
|
||||
auto iter = getMatching(before);
|
||||
if(iter == m_entries.end())
|
||||
if (iter == m_entries.end())
|
||||
return;
|
||||
|
||||
auto entry = new BarEntry();
|
||||
entry->qAction = QToolBar::insertSeparator(before);
|
||||
entry->type = BarEntry::Separator;
|
||||
BarEntry entry;
|
||||
entry.bar_action = QToolBar::insertSeparator(before);
|
||||
entry.type = BarEntry::Type::Separator;
|
||||
|
||||
m_entries.insert(iter, entry);
|
||||
}
|
||||
|
||||
QMenu * WideBar::createContextMenu(QWidget *parent, const QString & title)
|
||||
QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
|
||||
{
|
||||
QMenu *contextMenu = new QMenu(title, parent);
|
||||
for(auto & item: m_entries) {
|
||||
switch(item->type) {
|
||||
auto* contextMenu = new QMenu(title, parent);
|
||||
for (auto& item : m_entries) {
|
||||
switch (item.type) {
|
||||
default:
|
||||
case BarEntry::None:
|
||||
case BarEntry::Type::None:
|
||||
break;
|
||||
case BarEntry::Separator:
|
||||
case BarEntry::Spacer:
|
||||
case BarEntry::Type::Separator:
|
||||
case BarEntry::Type::Spacer:
|
||||
contextMenu->addSeparator();
|
||||
break;
|
||||
case BarEntry::Action:
|
||||
contextMenu->addAction(item->wideAction);
|
||||
case BarEntry::Type::Action:
|
||||
contextMenu->addAction(item.menu_action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,16 @@
|
||||
|
||||
#include <QAction>
|
||||
#include <QMap>
|
||||
#include <QMenu>
|
||||
#include <QToolBar>
|
||||
|
||||
class QMenu;
|
||||
|
||||
class WideBar : public QToolBar {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit WideBar(const QString& title, QWidget* parent = nullptr);
|
||||
explicit WideBar(QWidget* parent = nullptr);
|
||||
virtual ~WideBar();
|
||||
~WideBar() override = default;
|
||||
|
||||
void addAction(QAction* action);
|
||||
void addSeparator();
|
||||
@ -25,10 +24,14 @@ class WideBar : public QToolBar {
|
||||
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString());
|
||||
|
||||
private:
|
||||
struct BarEntry;
|
||||
struct BarEntry {
|
||||
enum class Type { None, Action, Separator, Spacer } type = Type::None;
|
||||
QAction* bar_action = nullptr;
|
||||
QAction* menu_action = nullptr;
|
||||
};
|
||||
|
||||
auto getMatching(QAction* act) -> QList<BarEntry*>::iterator;
|
||||
auto getMatching(QAction* act) -> QList<BarEntry>::iterator;
|
||||
|
||||
private:
|
||||
QList<BarEntry*> m_entries;
|
||||
QList<BarEntry> m_entries;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user