Files
qt6-migration-pipeline/Z/src/nglwidgets/ngldialogbuttonbox.cpp
T

446 lines
11 KiB
C++
Executable File

#include "ngldialogbuttonbox.h"
#include <QAction>
#include <QHBoxLayout>
#include <QFrame>
NGLDialogButtonBox::NGLDialogButtonBox(QWidget *parent)
: NGLDialogButtonBox(Qt::Horizontal, parent)
{}
NGLDialogButtonBox::NGLDialogButtonBox(Qt::Orientation orientation,
QWidget *parent)
: QWidget(parent)
, m_orientation(orientation)
, m_buttons(NGLStandardButton::NoButton)
, m_line(nullptr)
, m_bSpacer2ContextAction(nullptr)
, m_bLineVisible(true)
{
setFocusPolicy(Qt::StrongFocus);
init();
}
NGLDialogButtonBox::NGLDialogButtonBox(NGLStandardButton::StandardButtons buttons,
Qt::Orientation orientation,
QWidget *parent)
: QWidget(parent)
, m_orientation(orientation)
, m_buttons(buttons)
, m_line(nullptr)
, m_bSpacer2ContextAction(nullptr)
, m_bLineVisible(true)
{
setFocusPolicy(Qt::StrongFocus);
setStandardButtons(m_buttons);
}
Qt::Orientation NGLDialogButtonBox::orientation() const
{
return m_orientation;
}
void NGLDialogButtonBox::setOrientation(Qt::Orientation orientation)
{
if (m_orientation == orientation)
return;
m_orientation = orientation;
init();
}
NGLStandardButton::StandardButtons NGLDialogButtonBox::standardButtons() const
{
return m_buttons;
}
void NGLDialogButtonBox::setStandardButtons(NGLStandardButton::StandardButtons buttons)
{
m_buttons = buttons;
QMultiMap<int, NGLStandardButton *>::const_iterator it = m_standardButtons.constBegin();
QMultiMap<int, NGLStandardButton *>::const_iterator end = m_standardButtons.constEnd();
for (; it != end; ++it)
{
const QList<QAction *> actions = it.value()->actions();
for (QAction *a : actions)
removeAction(a);
delete it.value();
}
m_standardButtons.clear();
NGLStandardButton::StandardButton i = NGLStandardButton::FirstButton;
while (i <= NGLStandardButton::LastButton)
{
if ((m_buttons & i) == i)
{
NGLStandardButton *button = new NGLStandardButton(i);
addButtonInternal(button);
if (i == NGLStandardButton::Ok)
{
button->setDefault(true);
button->setAutoDefault(true);
}
}
i = (NGLStandardButton::StandardButton)((int) i << 1);
}
init();
}
void NGLDialogButtonBox::addButtonInternal(NGLStandardButton *button)
{
button->setParent(this);
button->setDefault(false);
button->setAutoDefault(false);
m_standardButtons.insert(button->layoutPriority(), button);
}
void NGLDialogButtonBox::addButton(NGLStandardButton *button)
{
addButtonInternal(button);
init();
}
NGLStandardButton *NGLDialogButtonBox::addButton(NGLStandardButton::StandardButton button)
{
NGLStandardButton *pb = new NGLStandardButton(button);
addButton(pb);
return pb;
}
NGLStandardButton *NGLDialogButtonBox::button(NGLStandardButton::StandardButton button)
{
QMultiMap<int, NGLStandardButton *>::const_iterator it = m_standardButtons.constBegin();
QMultiMap<int, NGLStandardButton *>::const_iterator end = m_standardButtons.constEnd();
for (; it != end; ++it)
{
if (it.value()->type() == button)
return it.value();
}
return new NGLStandardButton(nullptr);
}
void NGLDialogButtonBox::removeFromActionList(NGLStandardButton::StandardButton button)
{
m_disabledActions |= button;
init();
}
bool NGLDialogButtonBox::setEnabled(NGLStandardButton::StandardButton button, bool enable)
{
NGLStandardButton *b = this->button(button);
if (!b)
return false;
b->setEnabled(enable);
return true;
}
bool NGLDialogButtonBox::isEnabled(NGLStandardButton::StandardButton button)
{
NGLStandardButton *b = this->button(button);
if (!b)
return false;
return b->isEnabled();
}
bool NGLDialogButtonBox::lineVisible() const
{
return m_bLineVisible;
}
void NGLDialogButtonBox::setLineVisible(bool bVisible)
{
if (m_bLineVisible == bVisible)
return;
m_bLineVisible = bVisible;
init();
}
bool NGLDialogButtonBox::setVisible(NGLStandardButton::StandardButton button, bool visible)
{
NGLStandardButton *b = this->button(button);
if (!b)
return false;
b->setVisible(visible);
return true;
}
void NGLDialogButtonBox::layoutPriorityChanged()
{
QMultiMap<qint32, NGLStandardButton *> standardButtons = m_standardButtons;
m_standardButtons.clear();
QMultiMap<int, NGLStandardButton *>::const_iterator it = standardButtons.constBegin();
QMultiMap<int, NGLStandardButton *>::const_iterator end = standardButtons.constEnd();
for (; it != end; ++it)
{
NGLStandardButton *button = it.value();
m_standardButtons.insert(button->layoutPriority(), button);
}
init();
}
static void addSpacer2(QLayout *l, Qt::Orientation orientation)
{
if (orientation == Qt::Horizontal)
l->addItem(new QSpacerItem(20, 1, QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
else
l->addItem(new QSpacerItem(1, 20, QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
}
void NGLDialogButtonBox::init()
{
delete layout();
delete m_line; m_line = nullptr;
if (m_bSpacer2ContextAction)
{
removeAction(m_bSpacer2ContextAction);
delete m_bSpacer2ContextAction;
m_bSpacer2ContextAction = nullptr;
}
QBoxLayout *l;
QBoxLayout *lMain;
if (m_orientation == Qt::Horizontal)
{
lMain = new QVBoxLayout;
l = new QHBoxLayout;
}
else
{
lMain = new QHBoxLayout;
l = new QVBoxLayout;
}
lMain->setContentsMargins(0,0,0,0);
if (m_bLineVisible)
{
m_line = new QFrame(this);
m_line->setFrameShape(m_orientation == Qt::Horizontal ? QFrame::HLine : QFrame::VLine);
m_line->setFrameShadow(QFrame::Sunken);
lMain->addWidget(m_line);
}
lMain->addLayout(l);
l->setDirection(m_orientation == Qt::Horizontal ? QBoxLayout::RightToLeft : QBoxLayout::BottomToTop);
l->setContentsMargins(0, 0, 0, 0);
setLayout(lMain);
const auto actions = findChildren<QAction*>();
for (auto a : actions)
a->disconnect(a, &QAction::triggered, this, &NGLDialogButtonBox::buttonClicked);
bool bLowestPrioButton = false;
bool bSpacer1Added = false;
bool bSpacer2Added = false;
QMultiMap<int, NGLStandardButton *>::const_iterator it = m_standardButtons.constBegin();
QMultiMap<int, NGLStandardButton *>::const_iterator end = m_standardButtons.constEnd();
for (; it != end; ++it)
{
qint32 prio = it.key();
NGLStandardButton *button = it.value();
if (prio < 30)
bLowestPrioButton = true;
if (prio >= 30 && !bSpacer1Added)
{
bSpacer1Added = true;
if (bLowestPrioButton)
{
QSpacerItem *item = new QSpacerItem(7, 7, QSizePolicy::Fixed, QSizePolicy::Fixed);
l->addItem(item);
}
}
if (prio >= 100 && !bSpacer2Added)
{
bSpacer2Added = true;
addSpacer2(l, m_orientation);
}
if (prio >= 30 && prio < 100)
{
const QList<QAction *> actions = button->actions();
for (QAction *a : actions)
removeAction(a);
if ((m_disabledActions & button->type()) == 0)
{
if (prio >= 100 && !m_bSpacer2ContextAction)
{
m_bSpacer2ContextAction = new QAction(this);
m_bSpacer2ContextAction->setSeparator(true);
addAction(m_bSpacer2ContextAction);
}
addActions(actions);
}
}
l->addWidget(button);
if (button->type() == NGLStandardButton::Library)
{
QAction *ex = button->findChild<QAction *>(QLatin1String("actionExport"));
if (ex)
{
connect(ex, &QAction::triggered,
this, &NGLDialogButtonBox::buttonClicked, Qt::UniqueConnection);
}
QAction *im = button->findChild<QAction *>(QLatin1String("actionImport"));
if (im)
{
connect(im, &QAction::triggered,
this, &NGLDialogButtonBox::buttonClicked, Qt::UniqueConnection);
}
}
else
{
connect(button, &NGLStandardButton::clicked,
this, &NGLDialogButtonBox::buttonClicked, Qt::UniqueConnection);
}
}
if (!bSpacer2Added)
addSpacer2(l, m_orientation);
QMultiMapIterator<int, NGLStandardButton *> mapIt(m_standardButtons);
NGLStandardButton *prevButton = nullptr;
mapIt.toBack();
while (mapIt.hasPrevious())
{
NGLStandardButton *curButton = mapIt.previous().value();
if (prevButton != nullptr)
setTabOrder(prevButton, curButton);
prevButton = curButton;
}
}
void NGLDialogButtonBox::buttonClicked()
{
NGLStandardButton *b = qobject_cast<NGLStandardButton *> (sender());
if (!b)
{
QAction *a = qobject_cast<QAction *> (sender());
if (!a)
return;
if (a->objectName() == QLatin1String("actionExport"))
{
emit onLibraryExport();
return;
}
if (a->objectName() == QLatin1String("actionImport"))
{
emit onLibraryImport();
return;
}
return;
}
switch (b->type())
{
case NGLStandardButton::Ok:
emit onOk();
break;
case NGLStandardButton::Send:
emit onSend();
break;
case NGLStandardButton::New:
emit onNew();
break;
case NGLStandardButton::Delete:
emit onDelete();
break;
case NGLStandardButton::Save:
emit onSave();
break;
case NGLStandardButton::Open:
emit onOpen();
break;
case NGLStandardButton::Yes:
emit onYes();
break;
case NGLStandardButton::No:
emit onNo();
break;
case NGLStandardButton::Close:
emit onClose();
break;
case NGLStandardButton::Cancel:
emit onCancel();
break;
case NGLStandardButton::Help:
emit onHelp();
break;
case NGLStandardButton::Apply:
emit onApply();
break;
case NGLStandardButton::Reset:
emit onReset();
break;
case NGLStandardButton::Print:
emit onPrint();
break;
case NGLStandardButton::Modify:
emit onModify();
break;
case NGLStandardButton::Add:
emit onAdd();
break;
case NGLStandardButton::Remove:
emit onRemove();
break;
case NGLStandardButton::Edit:
emit onEdit();
break;
case NGLStandardButton::Download:
emit onDownload();
break;
case NGLStandardButton::Library:
break;
case NGLStandardButton::Show:
emit onShow();
break;
case NGLStandardButton::ZoomIn:
emit onZoomIn();
break;
case NGLStandardButton::ZoomOut:
emit onZoomOut();
break;
case NGLStandardButton::Hide:
emit onHide();
break;
default:
emit clicked(b);
break;
}
}
void NGLDialogButtonBox::enableImport(bool ena)
{
NGLStandardButton *b = button(NGLStandardButton::Library);
if (!b)
return;
QAction *im = b->findChild<QAction *>(QLatin1String("actionImport"));
if (im)
{
im->setEnabled(ena);
QAction *ex = b->findChild<QAction *>(QLatin1String("actionExport"));
if (ex)
b->setEnabled(ena || ex->isEnabled());
}
}
void NGLDialogButtonBox::enableExport(bool ena)
{
NGLStandardButton *b = button(NGLStandardButton::Library);
if (!b)
return;
QAction *ex = b->findChild<QAction *>(QLatin1String("actionExport"));
if (ex)
{
ex->setEnabled(ena);
QAction *im = b->findChild<QAction *>(QLatin1String("actionImport"));
if (im)
b->setEnabled(ena || im->isEnabled());
}
}