2019-07-23 04:18:14 +05:30
|
|
|
#include "WideBar.h"
|
|
|
|
#include <QToolButton>
|
2019-07-25 04:32:30 +05:30
|
|
|
#include <QMenu>
|
2019-07-23 04:18:14 +05:30
|
|
|
|
|
|
|
class ActionButton : public QToolButton
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
ActionButton(QAction * action, QWidget * parent = 0) : 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() {
|
|
|
|
setEnabled(m_action->isEnabled());
|
|
|
|
setChecked(m_action->isChecked());
|
|
|
|
setCheckable(m_action->isCheckable());
|
|
|
|
setText(m_action->text());
|
|
|
|
setIcon(m_action->icon());
|
|
|
|
setToolTip(m_action->toolTip());
|
|
|
|
setHidden(!m_action->isVisible());
|
2019-07-25 04:32:30 +05:30
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
private:
|
|
|
|
QAction * m_action;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
|
|
|
|
{
|
|
|
|
setFloatable(false);
|
|
|
|
setMovable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
|
|
|
{
|
|
|
|
setFloatable(false);
|
|
|
|
setMovable(false);
|
|
|
|
}
|
|
|
|
|
2019-07-25 04:32:30 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
void WideBar::addAction(QAction* action)
|
|
|
|
{
|
2019-07-25 04:32:30 +05:30
|
|
|
auto entry = new BarEntry();
|
|
|
|
entry->qAction = addWidget(new ActionButton(action, this));
|
|
|
|
entry->wideAction = action;
|
|
|
|
entry->type = BarEntry::Action;
|
|
|
|
m_entries.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WideBar::addSeparator()
|
|
|
|
{
|
|
|
|
auto entry = new BarEntry();
|
|
|
|
entry->qAction = QToolBar::addSeparator();
|
|
|
|
entry->type = BarEntry::Separator;
|
|
|
|
m_entries.push_back(entry);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
2022-01-29 00:02:42 +05:30
|
|
|
void WideBar::insertActionBefore(QAction* before, QAction* action){
|
|
|
|
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [before](BarEntry * entry) {
|
|
|
|
return entry->wideAction == before;
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
m_entries.insert(iter, entry);
|
|
|
|
}
|
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
void WideBar::insertSpacer(QAction* action)
|
|
|
|
{
|
2019-07-25 04:32:30 +05:30
|
|
|
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [action](BarEntry * entry) {
|
|
|
|
return entry->wideAction == action;
|
|
|
|
});
|
|
|
|
if(iter == m_entries.end()) {
|
2019-07-23 04:18:14 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
QWidget* spacer = new QWidget();
|
|
|
|
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2019-07-25 04:32:30 +05:30
|
|
|
|
|
|
|
auto entry = new BarEntry();
|
|
|
|
entry->qAction = insertWidget((*iter)->qAction, spacer);
|
|
|
|
entry->type = BarEntry::Spacer;
|
|
|
|
m_entries.insert(iter, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu * WideBar::createContextMenu(QWidget *parent, const QString & title)
|
|
|
|
{
|
|
|
|
QMenu *contextMenu = new QMenu(title, parent);
|
|
|
|
for(auto & item: m_entries) {
|
|
|
|
switch(item->type) {
|
|
|
|
default:
|
|
|
|
case BarEntry::None:
|
|
|
|
break;
|
|
|
|
case BarEntry::Separator:
|
|
|
|
case BarEntry::Spacer:
|
|
|
|
contextMenu->addSeparator();
|
|
|
|
break;
|
|
|
|
case BarEntry::Action:
|
|
|
|
contextMenu->addAction(item->wideAction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contextMenu;
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#include "WideBar.moc"
|