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