BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::ColorEditor Class Reference

Custom editor for QVariant based on QColor. More...

Inheritance diagram for ModelView::ColorEditor:
[legend]
Collaboration diagram for ModelView::ColorEditor:
[legend]

Public Slots

void setData (const QVariant &data)
 Sets the data from model to editor. More...
 

Signals

void dataChanged (QVariant value)
 Emmits signal when data was changed in an editor. More...
 

Public Member Functions

 ColorEditor (QWidget *parent=nullptr)
 
QVariant data () const
 
virtual bool is_persistent () const
 Returns true if editor should remains alive after editing finished. More...
 

Protected Member Functions

void mousePressEvent (QMouseEvent *event) override
 
void setDataIntern (const QVariant &data)
 Saves the data as given by editor's internal components and notifies the model. More...
 

Protected Attributes

QVariant m_data
 

Properties

QVariant value
 

Private Member Functions

QColor currentColor () const
 
void update_components () override
 Should update widget components from m_data, if necessary. More...
 

Private Attributes

LostFocusFilterm_focusFilter
 
QLabel * m_pixmapLabel {nullptr}
 
QLabel * m_textLabel {nullptr}
 

Detailed Description

Custom editor for QVariant based on QColor.

Definition at line 28 of file coloreditor.h.

Constructor & Destructor Documentation

◆ ColorEditor()

ColorEditor::ColorEditor ( QWidget *  parent = nullptr)
explicit

Definition at line 26 of file coloreditor.cpp.

27  : CustomEditor(parent), m_pixmapLabel(new QLabel), m_focusFilter(new LostFocusFilter(this))
28 
29 {
30  setMouseTracking(true);
31  setAutoFillBackground(true);
32 
33  auto layout = new QHBoxLayout;
34  layout->setContentsMargins(4, 0, 0, 0);
35 
36  layout->addWidget(m_pixmapLabel);
37  // layout->addWidget(m_textLabel); // no color name, only color rectangle
38  layout->addStretch(1);
39  setFocusPolicy(Qt::StrongFocus);
40  setAttribute(Qt::WA_InputMethodEnabled);
41 
42  setLayout(layout);
43 }
LostFocusFilter * m_focusFilter
Definition: coloreditor.h:43
CustomEditor(QWidget *parent=nullptr)
Event filter to prevent loss of the focus.

References m_pixmapLabel.

Member Function Documentation

◆ currentColor()

QColor ColorEditor::currentColor ( ) const
private

Definition at line 60 of file coloreditor.cpp.

61 {
62  return m_data.value<QColor>();
63 }

References ModelView::CustomEditor::m_data.

Referenced by mousePressEvent(), and update_components().

◆ data()

QVariant CustomEditor::data ( ) const
inherited

Definition at line 21 of file customeditor.cpp.

22 {
23  return m_data;
24 }

References ModelView::CustomEditor::m_data.

Referenced by ModelView::CustomEditor::setData(), and ModelView::CustomEditor::setDataIntern().

◆ dataChanged

void ModelView::CustomEditor::dataChanged ( QVariant  value)
signalinherited

◆ is_persistent()

bool CustomEditor::is_persistent ( ) const
virtualinherited

Returns true if editor should remains alive after editing finished.

Reimplemented in ModelView::SelectableComboBoxEditor, ModelView::ScientificSpinBoxEditor, ModelView::ComboPropertyEditor, and ModelView::BoolEditor.

Definition at line 28 of file customeditor.cpp.

29 {
30  return false;
31 }

◆ mousePressEvent()

void ColorEditor::mousePressEvent ( QMouseEvent *  event)
overrideprotected

Definition at line 45 of file coloreditor.cpp.

46 {
47  // temporarily installing filter to prevent loss of focus caused by too insistent dialog
48  installEventFilter(m_focusFilter);
49 
50  auto new_color = QColorDialog::getColor(currentColor());
51 
52  removeEventFilter(m_focusFilter);
53 
54  if (new_color.isValid()) {
55  setDataIntern(new_color);
57  }
58 }
void update_components() override
Should update widget components from m_data, if necessary.
Definition: coloreditor.cpp:65
QColor currentColor() const
Definition: coloreditor.cpp:60
void setDataIntern(const QVariant &data)
Saves the data as given by editor's internal components and notifies the model.

References currentColor(), m_focusFilter, ModelView::CustomEditor::setDataIntern(), and update_components().

Here is the call graph for this function:

◆ setData

void CustomEditor::setData ( const QVariant &  data)
slotinherited

Sets the data from model to editor.

Definition at line 35 of file customeditor.cpp.

36 {
37  m_data = data;
39 }
QVariant data() const
virtual void update_components()=0
Should update widget components from m_data, if necessary.

References ModelView::CustomEditor::data(), ModelView::CustomEditor::m_data, and ModelView::CustomEditor::update_components().

Here is the call graph for this function:

◆ setDataIntern()

void CustomEditor::setDataIntern ( const QVariant &  data)
protectedinherited

Saves the data as given by editor's internal components and notifies the model.

Definition at line 43 of file customeditor.cpp.

44 {
45  m_data = data;
47 }
void dataChanged(QVariant value)
Emmits signal when data was changed in an editor.

References ModelView::CustomEditor::data(), ModelView::CustomEditor::dataChanged(), and ModelView::CustomEditor::m_data.

Referenced by mousePressEvent(), ModelView::BoolEditor::onCheckBoxChange(), ModelView::DoubleEditor::onEditingFinished(), ModelView::IntegerEditor::onEditingFinished(), ModelView::ScientificDoubleEditor::onEditingFinished(), ModelView::ScientificSpinBoxEditor::onEditingFinished(), ModelView::ComboPropertyEditor::onIndexChanged(), ModelView::ExternalPropertyComboEditor::onIndexChanged(), and ModelView::SelectableComboBoxEditor::onModelDataChanged().

Here is the call graph for this function:

◆ update_components()

void ColorEditor::update_components ( )
overrideprivatevirtual

Should update widget components from m_data, if necessary.

Implements ModelView::CustomEditor.

Definition at line 65 of file coloreditor.cpp.

66 {
68  throw std::runtime_error("ColorEditor::update_components() -> Error. Wrong variant type");
69 
71  pixmap.fill(currentColor());
72  // m_textLabel->setText(currentColor().name());
73  m_pixmapLabel->setPixmap(pixmap);
74 }
MVVM_VIEWMODEL_EXPORT int DefaultPixmapSize()
Returns int value corresponding to pixmap in standard Qt table/tree decorations.
Definition: styleutils.cpp:17
MVVM_MODEL_EXPORT bool IsColorVariant(const Variant &variant)
Returns true in the case of QColor based variant.

References currentColor(), ModelView::Style::DefaultPixmapSize(), ModelView::Utils::IsColorVariant(), ModelView::CustomEditor::m_data, and m_pixmapLabel.

Referenced by mousePressEvent().

Here is the call graph for this function:

Member Data Documentation

◆ m_data

◆ m_focusFilter

LostFocusFilter* ModelView::ColorEditor::m_focusFilter
private

Definition at line 43 of file coloreditor.h.

Referenced by mousePressEvent().

◆ m_pixmapLabel

QLabel* ModelView::ColorEditor::m_pixmapLabel {nullptr}
private

Definition at line 42 of file coloreditor.h.

Referenced by ColorEditor(), and update_components().

◆ m_textLabel

QLabel* ModelView::ColorEditor::m_textLabel {nullptr}
private

Definition at line 41 of file coloreditor.h.

Property Documentation

◆ value

QVariant ModelView::CustomEditor::value
readwriteinherited

The documentation for this class was generated from the following files: