From c001a0008fec10d36a9d7efc1a018f5f29197779 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Wed, 29 Nov 2017 16:19:37 +0100 Subject: [PATCH] AppFwk : ComboBox : Add previous/next buttons, controlled by attributes --- .../cafPdmUiComboBoxEditor.cpp | 89 +++++++++++++++++-- .../cafUserInterface/cafPdmUiComboBoxEditor.h | 27 ++++-- 2 files changed, 100 insertions(+), 16 deletions(-) diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp index 3b406b7cb7..59032196f2 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp @@ -43,6 +43,7 @@ #include "cafFactory.h" +#include #include #include #include @@ -63,6 +64,14 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName) PdmUiFieldEditorHandle::updateLabelFromField(m_label, uiConfigName); } + // Handle attributes + PdmUiComboBoxEditorAttribute attributes; + caf::PdmUiObjectHandle* uiObject = uiObj(field()->fieldHandle()->ownerObject()); + if (uiObject) + { + uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &attributes); + } + if (!m_comboBox.isNull()) { m_comboBox->setEnabled(!field()->isUiReadOnly(uiConfigName)); @@ -87,14 +96,6 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName) m_comboBox->setCurrentIndex(0); } - // Handle attributes - PdmUiComboBoxEditorAttribute attributes; - caf::PdmUiObjectHandle* uiObject = uiObj(field()->fieldHandle()->ownerObject()); - if (uiObject) - { - uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &attributes); - } - if (attributes.adjustWidthToContents) { m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); @@ -102,6 +103,43 @@ void PdmUiComboBoxEditor::configureAndUpdateUi(const QString& uiConfigName) m_comboBox->blockSignals(false); } + + if (attributes.showPreviousAndNextButtons) + { + if (m_previousItemButton.isNull()) + { + m_previousItemButton = new QToolButton(m_placeholder); + connect(m_previousItemButton, SIGNAL(clicked()), this, SLOT(slotPreviousButtonPressed())); + + m_previousItemButton->setToolTip("Previous"); + m_previousItemButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_ArrowUp)); + } + if (m_nextItemButton.isNull()) + { + m_nextItemButton = new QToolButton(m_placeholder); + connect(m_nextItemButton, SIGNAL(clicked()), this, SLOT(slotNextButtonPressed())); + + m_nextItemButton->setToolTip("Next"); + m_nextItemButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_ArrowDown)); + } + + m_layout->insertWidget(1, m_previousItemButton); + m_layout->insertWidget(2, m_nextItemButton); + } + else + { + if (m_previousItemButton) + { + m_layout->removeWidget(m_previousItemButton); + m_previousItemButton->deleteLater(); + } + + if (m_nextItemButton) + { + m_layout->removeWidget(m_nextItemButton); + m_nextItemButton->deleteLater(); + } + } } //-------------------------------------------------------------------------------------------------- @@ -162,9 +200,16 @@ QWidget* PdmUiComboBoxEditor::createEditorWidget(QWidget * parent) m_comboBox = new CustomQComboBox(parent); m_comboBox->setFocusPolicy(Qt::StrongFocus); + m_placeholder = new QWidget(parent); + + m_layout = new QHBoxLayout(m_placeholder); + m_layout->setContentsMargins(0,0,0,0); + m_layout->setSpacing(0); + m_layout->addWidget(m_comboBox); + connect(m_comboBox, SIGNAL(activated(int)), this, SLOT(slotIndexActivated(int))); - return m_comboBox; + return m_placeholder; } //-------------------------------------------------------------------------------------------------- @@ -190,4 +235,30 @@ void PdmUiComboBoxEditor::slotIndexActivated(int index) +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiComboBoxEditor::slotNextButtonPressed() +{ + int indexCandidate = m_comboBox->currentIndex() + 1; + + if (indexCandidate < m_comboBox->count()) + { + slotIndexActivated(indexCandidate); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiComboBoxEditor::slotPreviousButtonPressed() +{ + int indexCandidate = m_comboBox->currentIndex() - 1; + + if (indexCandidate > -1 && indexCandidate < m_comboBox->count()) + { + slotIndexActivated(indexCandidate); + } +} + } // end namespace caf diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h index d3faf0961d..7afcf69874 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h @@ -39,12 +39,14 @@ #include "cafPdmUiFieldEditorHandle.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include namespace caf { @@ -56,12 +58,15 @@ namespace caf class PdmUiComboBoxEditorAttribute : public PdmUiEditorAttribute { public: - bool adjustWidthToContents; - PdmUiComboBoxEditorAttribute() { adjustWidthToContents = false; + showPreviousAndNextButtons = false; } + +public: + bool adjustWidthToContents; + bool showPreviousAndNextButtons; }; @@ -82,9 +87,17 @@ protected: protected slots: void slotIndexActivated(int index); + void slotNextButtonPressed(); + void slotPreviousButtonPressed(); + private: QPointer m_comboBox; QPointer m_label; + + QPointer m_previousItemButton; + QPointer m_nextItemButton; + QPointer m_layout; + QPointer m_placeholder; };