mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
AppFwk : Support notification when key is pressed in Line/ComboBox editors
This commit is contained in:
parent
89af752fa9
commit
7dfa84b0d9
@ -45,6 +45,8 @@ set(PROJECT_FILES
|
||||
MenuItemProducer.h
|
||||
TamComboBox.h
|
||||
TamComboBox.cpp
|
||||
LineEditAndPushButtons.h
|
||||
LineEditAndPushButtons.cpp
|
||||
)
|
||||
|
||||
# add the executable
|
||||
|
@ -0,0 +1,188 @@
|
||||
|
||||
#include "LineEditAndPushButtons.h"
|
||||
|
||||
#include "cafPdmUiLineEditor.h"
|
||||
#include "cafPdmUiListEditor.h"
|
||||
#include "cafPdmUiPushButtonEditor.h"
|
||||
#include "cafPdmUiTreeSelectionEditor.h"
|
||||
|
||||
CAF_PDM_SOURCE_INIT(LineEditAndPushButtons, "LineEditAndPushButtons");
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
LineEditAndPushButtons::LineEditAndPushButtons()
|
||||
{
|
||||
CAF_PDM_InitObject("Line Edit And Push Buttons", "", "", "");
|
||||
|
||||
CAF_PDM_InitFieldNoDefault(&m_statusTextField, "StatusTextField", "Status Text", "", "", "");
|
||||
CAF_PDM_InitFieldNoDefault(&m_textField, "TextField", "Text", "", "", "");
|
||||
CAF_PDM_InitFieldNoDefault(&m_textListField, "TextListField", "Text List Field", "", "", "");
|
||||
|
||||
CAF_PDM_InitFieldNoDefault(&m_pushButton_a, "PushButtonA", "Rotate", "", "", "");
|
||||
m_pushButton_a.uiCapability()->setUiEditorTypeName(caf::PdmUiPushButtonEditor::uiEditorTypeName());
|
||||
m_pushButton_a.uiCapability()->setUiLabelPosition(caf::PdmUiItemInfo::HIDDEN);
|
||||
|
||||
CAF_PDM_InitFieldNoDefault(&m_pushButtonReplace, "PushButtonB", "Replace (CTRL + Enter)", "", "", "");
|
||||
m_pushButtonReplace.uiCapability()->setUiEditorTypeName(caf::PdmUiPushButtonEditor::uiEditorTypeName());
|
||||
m_pushButtonReplace.uiCapability()->setUiLabelPosition(caf::PdmUiItemInfo::HIDDEN);
|
||||
|
||||
CAF_PDM_InitFieldNoDefault(&m_pushButtonClear, "PushButtonC", "Clear (Alt + Enter)", "", "", "");
|
||||
m_pushButtonClear.uiCapability()->setUiEditorTypeName(caf::PdmUiPushButtonEditor::uiEditorTypeName());
|
||||
m_pushButtonClear.uiCapability()->setUiLabelPosition(caf::PdmUiItemInfo::HIDDEN);
|
||||
|
||||
CAF_PDM_InitFieldNoDefault(&m_pushButtonAppend, "PushButtonD", "Append (Shift + Enter)", "", "", "");
|
||||
m_pushButtonAppend.uiCapability()->setUiEditorTypeName(caf::PdmUiPushButtonEditor::uiEditorTypeName());
|
||||
m_pushButtonAppend.uiCapability()->setUiLabelPosition(caf::PdmUiItemInfo::HIDDEN);
|
||||
|
||||
std::vector<QString> items;
|
||||
items.push_back("sldkfj");
|
||||
items.push_back("annet sldkfj");
|
||||
items.push_back("kort");
|
||||
items.push_back("veldig langt");
|
||||
items.push_back("kort");
|
||||
|
||||
m_textListField = items;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::fieldChangedByUi(const caf::PdmFieldHandle* changedField,
|
||||
const QVariant& oldValue,
|
||||
const QVariant& newValue)
|
||||
{
|
||||
if (changedField == &m_pushButton_a)
|
||||
{
|
||||
rotateContent();
|
||||
}
|
||||
|
||||
if (changedField == &m_textField)
|
||||
{
|
||||
auto mods = QGuiApplication::keyboardModifiers();
|
||||
|
||||
// Use global keyboard modifiers to trigger different events when content is changed in editor changes
|
||||
|
||||
if (mods & Qt::ShiftModifier)
|
||||
appendText();
|
||||
else if (mods & Qt::ControlModifier)
|
||||
replaceText();
|
||||
else if (mods & Qt::AltModifier)
|
||||
clearText();
|
||||
else
|
||||
{
|
||||
m_statusTextField = m_textField;
|
||||
}
|
||||
}
|
||||
|
||||
if (changedField == &m_pushButtonReplace)
|
||||
{
|
||||
replaceText();
|
||||
}
|
||||
if (changedField == &m_pushButtonClear)
|
||||
{
|
||||
clearText();
|
||||
}
|
||||
if (changedField == &m_pushButtonAppend)
|
||||
{
|
||||
appendText();
|
||||
}
|
||||
|
||||
m_pushButton_a = false;
|
||||
m_pushButtonReplace = false;
|
||||
m_pushButtonClear = false;
|
||||
m_pushButtonAppend = false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) {}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::defineEditorAttribute(const caf::PdmFieldHandle* field,
|
||||
QString uiConfigName,
|
||||
caf::PdmUiEditorAttribute* attribute)
|
||||
{
|
||||
if (field == &m_textField)
|
||||
{
|
||||
auto myAttr = dynamic_cast<caf::PdmUiLineEditorAttribute*>(attribute);
|
||||
if (myAttr)
|
||||
{
|
||||
myAttr->notifyWhenTextIsEdited = true;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
auto myAttr = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>(attribute);
|
||||
if (myAttr)
|
||||
{
|
||||
if (field == &m_pushButton_a)
|
||||
{
|
||||
myAttr->m_buttonText = "&Push Me";
|
||||
}
|
||||
if (field == &m_pushButtonReplace)
|
||||
{
|
||||
myAttr->m_buttonText = "Replace (Ctrl + Enter)";
|
||||
}
|
||||
if (field == &m_pushButtonClear)
|
||||
{
|
||||
myAttr->m_buttonText = "Clear (Alt + Enter)";
|
||||
}
|
||||
if (field == &m_pushButtonAppend)
|
||||
{
|
||||
myAttr->m_buttonText = "Append (Shift + Enter)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::rotateContent()
|
||||
{
|
||||
auto original = m_textListField.value();
|
||||
|
||||
std::list<QString> newContent;
|
||||
newContent.insert(newContent.begin(), original.begin(), original.end());
|
||||
|
||||
auto firstItem = newContent.front();
|
||||
newContent.pop_front();
|
||||
newContent.push_back(firstItem);
|
||||
|
||||
std::vector<QString> tmp;
|
||||
tmp.insert(tmp.begin(), newContent.begin(), newContent.end());
|
||||
|
||||
m_textListField = tmp;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::appendText()
|
||||
{
|
||||
auto original = m_textListField.value();
|
||||
original.push_back(m_textField);
|
||||
|
||||
m_textListField = original;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::replaceText()
|
||||
{
|
||||
clearText();
|
||||
appendText();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void LineEditAndPushButtons::clearText()
|
||||
{
|
||||
m_textListField = std::vector<QString>();
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "cafPdmField.h"
|
||||
#include "cafPdmObject.h"
|
||||
#include "cafPdmProxyValueField.h"
|
||||
|
||||
class LineEditAndPushButtons : public caf::PdmObject
|
||||
{
|
||||
CAF_PDM_HEADER_INIT;
|
||||
|
||||
public:
|
||||
LineEditAndPushButtons();
|
||||
|
||||
void fieldChangedByUi(const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue) override;
|
||||
|
||||
private:
|
||||
void defineUiOrdering(QString uiConfigName, caf::PdmUiOrdering& uiOrdering) override;
|
||||
|
||||
void defineEditorAttribute(const caf::PdmFieldHandle* field,
|
||||
QString uiConfigName,
|
||||
caf::PdmUiEditorAttribute* attribute) override;
|
||||
|
||||
void rotateContent();
|
||||
void appendText();
|
||||
void replaceText();
|
||||
void clearText();
|
||||
|
||||
private:
|
||||
caf::PdmField<QString> m_textField;
|
||||
caf::PdmField<QString> m_statusTextField;
|
||||
caf::PdmField<std::vector<QString>> m_textListField;
|
||||
|
||||
caf::PdmField<bool> m_pushButton_a;
|
||||
|
||||
caf::PdmField<bool> m_pushButtonReplace;
|
||||
caf::PdmField<bool> m_pushButtonClear;
|
||||
caf::PdmField<bool> m_pushButtonAppend;
|
||||
};
|
@ -4,6 +4,7 @@
|
||||
#include "MainWindow.h"
|
||||
|
||||
#include "CustomObjectEditor.h"
|
||||
#include "LineEditAndPushButtons.h"
|
||||
#include "ManyGroups.h"
|
||||
#include "MenuItemProducer.h"
|
||||
#include "TamComboBox.h"
|
||||
@ -54,10 +55,9 @@ class DemoPdmObjectGroup : public caf::PdmDocument
|
||||
public:
|
||||
DemoPdmObjectGroup()
|
||||
{
|
||||
CAF_PDM_InitFieldNoDefault(&objects, "PdmObjects", "", "", "", "")
|
||||
CAF_PDM_InitFieldNoDefault(&objects, "PdmObjects", "MyRootObject", "", "", "");
|
||||
|
||||
objects.uiCapability()
|
||||
->setUiHidden(true);
|
||||
objects.uiCapability()->setUiHidden(true);
|
||||
}
|
||||
|
||||
public:
|
||||
@ -1200,6 +1200,8 @@ void MainWindow::buildTestModel()
|
||||
SingleEditorPdmObject* singleEditorObj = new SingleEditorPdmObject;
|
||||
m_testRoot->objects.push_back(singleEditorObj);
|
||||
|
||||
m_testRoot->objects.push_back(new LineEditAndPushButtons);
|
||||
|
||||
auto tamComboBox = new TamComboBox;
|
||||
m_testRoot->objects.push_back(tamComboBox);
|
||||
|
||||
|
@ -284,6 +284,20 @@ void PdmUiComboBoxEditor::configureAndUpdateUi( const QString& uiConfigName )
|
||||
m_comboBox->lineEdit()->setPlaceholderText( m_attributes.placeholderText );
|
||||
}
|
||||
|
||||
if ( m_attributes.notifyWhenTextIsEdited )
|
||||
{
|
||||
connect( m_comboBox,
|
||||
SIGNAL( editTextChanged( const QString& ) ),
|
||||
this,
|
||||
SLOT( slotEditTextChanged( const QString& ) ) );
|
||||
|
||||
if ( m_interactiveEditText == m_comboBox->lineEdit()->text() && m_interactiveEditCursorPosition > -1 )
|
||||
{
|
||||
m_comboBox->lineEdit()->setCursorPosition( m_interactiveEditCursorPosition );
|
||||
m_comboBox->lineEdit()->deselect();
|
||||
}
|
||||
}
|
||||
|
||||
if ( m_attributes.minimumWidth != -1 )
|
||||
{
|
||||
m_comboBox->setMinimumWidth( m_attributes.minimumWidth );
|
||||
@ -455,6 +469,14 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
PdmUiComboBoxEditor::PdmUiComboBoxEditor()
|
||||
: m_interactiveEditCursorPosition( -1 )
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@ -509,6 +531,19 @@ void PdmUiComboBoxEditor::slotIndexActivated( int index )
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiComboBoxEditor::slotEditTextChanged( const QString& text )
|
||||
{
|
||||
if ( text == m_interactiveEditText ) return;
|
||||
|
||||
m_interactiveEditText = text;
|
||||
m_interactiveEditCursorPosition = m_comboBox->lineEdit()->cursorPosition();
|
||||
|
||||
this->setValueToField( text );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -63,6 +63,7 @@ public:
|
||||
enableEditableContent = false;
|
||||
minimumWidth = -1;
|
||||
iconSize = QSize( 14, 14 );
|
||||
notifyWhenTextIsEdited = false;
|
||||
}
|
||||
|
||||
public:
|
||||
@ -80,6 +81,8 @@ public:
|
||||
QSize iconSize;
|
||||
QIcon nextIcon;
|
||||
QIcon previousIcon;
|
||||
|
||||
bool notifyWhenTextIsEdited;
|
||||
};
|
||||
|
||||
//==================================================================================================
|
||||
@ -91,7 +94,7 @@ class PdmUiComboBoxEditor : public PdmUiFieldEditorHandle
|
||||
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
|
||||
|
||||
public:
|
||||
PdmUiComboBoxEditor() {}
|
||||
PdmUiComboBoxEditor();
|
||||
~PdmUiComboBoxEditor() override {}
|
||||
|
||||
protected:
|
||||
@ -102,6 +105,7 @@ protected:
|
||||
|
||||
protected slots:
|
||||
void slotIndexActivated( int index );
|
||||
void slotEditTextChanged( const QString& );
|
||||
|
||||
void slotNextButtonPressed();
|
||||
void slotPreviousButtonPressed();
|
||||
@ -116,6 +120,9 @@ private:
|
||||
QPointer<QWidget> m_placeholder;
|
||||
|
||||
PdmUiComboBoxEditorAttribute m_attributes;
|
||||
|
||||
QString m_interactiveEditText;
|
||||
int m_interactiveEditCursorPosition;
|
||||
};
|
||||
|
||||
} // end namespace caf
|
||||
|
@ -141,6 +141,11 @@ void PdmUiLineEditor::configureAndUpdateUi( const QString& uiConfigName )
|
||||
{
|
||||
m_lineEdit->setPlaceholderText( leab.placeholderText );
|
||||
}
|
||||
|
||||
if ( leab.notifyWhenTextIsEdited )
|
||||
{
|
||||
connect( m_lineEdit, SIGNAL( textEdited( const QString& ) ), this, SLOT( slotEditingFinished() ) );
|
||||
}
|
||||
}
|
||||
|
||||
bool fromMenuOnly = true;
|
||||
@ -220,7 +225,10 @@ void PdmUiLineEditor::configureAndUpdateUi( const QString& uiConfigName )
|
||||
displayString = displayStringAttrib.m_displayString;
|
||||
}
|
||||
|
||||
m_lineEdit->setText( displayString );
|
||||
if ( displayString != m_lineEdit->text() )
|
||||
{
|
||||
m_lineEdit->setText( displayString );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ public:
|
||||
maximumWidth = -1;
|
||||
selectAllOnFocusEvent = false;
|
||||
placeholderText = "";
|
||||
notifyWhenTextIsEdited = false;
|
||||
}
|
||||
|
||||
public:
|
||||
@ -77,6 +78,7 @@ public:
|
||||
int maximumWidth;
|
||||
bool selectAllOnFocusEvent;
|
||||
QString placeholderText;
|
||||
bool notifyWhenTextIsEdited;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user