#include "nglstandardbutton.h" #include #include #include #include NGLStandardButton::NGLStandardButton(QWidget *parent) : NGLStandardButton(NoButton, parent) {} NGLStandardButton::NGLStandardButton(StandardButton type, QWidget *parent) : QPushButton(parent) , m_type(type) , m_layoutPriority(-1) { init(); } void NGLStandardButton::init() { setType(m_type); } NGLStandardButton::StandardButton NGLStandardButton::type() const { return m_type; } void NGLStandardButton::setType(StandardButton t) { m_type = t; qDeleteAll(m_actionList); m_actionList.clear(); const char *iconName = nullptr; QString text; QString actionText; QKeySequence key; const char *objectName = nullptr; switch (t) { case Ok: iconName = ":icons/png/ok.png"; text = tr("&Ok"); objectName = "btnOk"; break; case Send: iconName = ":icons/svg/messages.svg"; text = tr("&Send"); key = QKeySequence(Qt::CTRL | Qt::Key_Return); objectName = "btnSend"; break; case New: iconName = ":icons/png/messages_new.png"; text = tr("&New"); key = QKeySequence::New; objectName = "btnNew"; break; case Delete: iconName = ":icons/png/deleteall.png"; text = tr("&Delete"); key = QKeySequence::Delete; objectName = "btnDelete"; break; case Save: iconName = ":icons/png/save.png"; text = tr("Sa&ve"); key = QKeySequence::Save; objectName = "btnSave"; break; case Open: iconName = ":icons/png/address_book.png"; text = tr("&Open"); key = QKeySequence::Open; objectName = "btnOpen"; break; case Yes: iconName = ":icons/png/ok.png"; text = tr("&Yes"); objectName = "btnYes"; break; case No: iconName = ":icons/png/delete.png"; text = tr("&No"); objectName = "btnNo"; break; case Close: iconName = ":icons/png/delete_exercise.png"; text = tr("&Close"); objectName = "btnClose"; break; case Cancel: iconName = ":icons/png/delete_exercise.png"; text = tr("&Cancel"); objectName = "btnCancel"; break; case Help: iconName = ":icons/svg/ueb_index.svg"; text = QString(); actionText = tr("Help"); setMaximumWidth(sizeHint().height()); key = QKeySequence::HelpContents; objectName = "btnHelp"; break; case Apply: iconName = ":icons/png/ok.png"; text = tr("&Apply"); objectName = "btnApply"; break; case Reset: iconName = ":icons/png/undo_act.png"; text = tr("Re&set"); objectName = "btnReset"; break; case Print: iconName = ":icons/png/printer.png"; text = QString(); actionText = tr("Print"); setMaximumWidth(sizeHint().height()); key = QKeySequence::Print; objectName = "btnPrint"; break; case Add: iconName = ":icons/png/add.png"; text = tr("A&dd"); objectName = "btnAdd"; break; case Remove: iconName = ":icons/png/remove.png"; text = tr("Re&move"); objectName = "btnRemove"; break; case Edit: iconName = ":icons/png/rename_exercise.png"; text = tr("&Edit"); objectName = "btnEdit"; break; case Modify: iconName = ":icons/png/ok.png"; text = tr("Modify"); objectName = "btnModify"; break; case Hide: iconName = ":icons/png/ok.png"; text = tr("Hide"); objectName = "btnHide"; break; case Download: iconName = ":icons/png/download.png"; text = tr("Download"); objectName = "btnDownload"; break; case Library: { iconName = ":icons/png/address_book.png"; text.clear(); setMaximumWidth(minimumSizeHint().width() * 4 / 5); QMenu *menu = new QMenu(this); setMenu(menu); QAction *actionImport = menu->addAction(QIcon(QLatin1String(":icons/svg/import_docs.svg")), tr("Import from library")); actionImport->setObjectName(QLatin1String("actionImport")); connect(actionImport, &QAction::triggered, this, QOverload::of(&QAbstractButton::clicked), Qt::QueuedConnection); m_actionList += actionImport; QAction *actionExport = menu->addAction(QIcon(QLatin1String(":icons/svg/export_docs.svg")), tr("Export to library")); actionExport->setObjectName(QLatin1String("actionExport")); connect(actionExport, &QAction::triggered, this, QOverload::of(&QAbstractButton::clicked), Qt::QueuedConnection); m_actionList += actionExport; objectName = "btnLibrary"; break; } case Show: { iconName = ":icons/png/preview.png"; text = tr("Show"); objectName = "btnShow"; break; } case ZoomIn: { iconName = ":icons/png/zoom_in.png"; text.clear(); objectName = "btnZoomIn"; break; } case ZoomOut: { iconName = ":icons/png/zoom_out.png"; text.clear(); objectName = "btnZoomOut"; break; } default: text = QLatin1String("ButtonText"); break; }; if (!m_defaultText.isEmpty()) text = tr(m_defaultText.constData()); if (!m_defaultToolTip.isEmpty()) { QString toolTip = tr(m_defaultToolTip.constData()); setToolTip(toolTip); } if (iconName) setIcon(QIcon(QString::fromLatin1(iconName))); setText(text); if (!key.isEmpty()) setShortcut(key); if (objectName) setObjectName(QString::fromUtf8(objectName)); if (m_actionList.isEmpty()) { QAction *action = new QAction(this); action->setText(text.isEmpty() ? actionText : text); action->setIcon(icon()); action->setEnabled(isEnabled()); action->setToolTip(toolTip()); action->setWhatsThis(whatsThis()); connect(action, &QAction::triggered, this, QOverload::of(&QAbstractButton::clicked), Qt::QueuedConnection); m_actionList += action; } } void NGLStandardButton::setLayoutPriority(qint32 priority) { m_layoutPriority = priority; } qint32 NGLStandardButton::layoutPriority() const { if (m_layoutPriority >= 0) return m_layoutPriority; switch (m_type) { case Close: return 10; case Cancel: return 11; case Apply: return 13; case Ok: return 15; case No: return 31; case Yes: return 32; case ZoomIn: return 36; case ZoomOut: return 37; case Show: return 38; case Download: return 44; case Save: return 45; case Open: return 47; case Reset: return 50; case Send: return 50; case Modify: return 51; case Hide: return 52; case Delete: return 55; case Remove: return 56; case New: return 60; case Add: return 61; case Edit: return 63; case Library: return 105; case Print: return 110; case Help: return 120; default: break; }; return 50; } void NGLStandardButton::changeEvent(QEvent *e) { switch (e->type()) { case QEvent::LanguageChange: setType(m_type); break; case QEvent::EnabledChange: { QAction *a = findChild(); if (a) a->setEnabled(isEnabled()); break; } default: break; } QPushButton::changeEvent(e); } QList NGLStandardButton::actions() const { return m_actionList; } void NGLStandardButton::setDefaultText(const char *text) { m_defaultText = QByteArray(text); if (!m_defaultText.isEmpty()) setText(tr(m_defaultText.constData())); } void NGLStandardButton::setDefaultToolTip(const char *toolTip) { m_defaultToolTip = QByteArray(toolTip); if (!m_defaultToolTip.isEmpty()) setToolTip(tr(m_defaultToolTip.constData())); }