BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
coloreditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/viewmodel/mvvm/editors/coloreditor.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QColorDialog>
20 #include <QHBoxLayout>
21 #include <QLabel>
22 #include <stdexcept>
23 
24 using namespace ModelView;
25 
26 ColorEditor::ColorEditor(QWidget* parent)
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 }
44 
45 void ColorEditor::mousePressEvent(QMouseEvent*)
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 }
59 
61 {
62  return m_data.value<QColor>();
63 }
64 
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 }
void update_components() override
Should update widget components from m_data, if necessary.
Definition: coloreditor.cpp:65
ColorEditor(QWidget *parent=nullptr)
Definition: coloreditor.cpp:26
LostFocusFilter * m_focusFilter
Definition: coloreditor.h:43
QColor currentColor() const
Definition: coloreditor.cpp:60
void mousePressEvent(QMouseEvent *event) override
Definition: coloreditor.cpp:45
Base class for all custom variant editors.
Definition: customeditor.h:26
void setDataIntern(const QVariant &data)
Saves the data as given by editor's internal components and notifies the model.
Event filter to prevent loss of the focus.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
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.
materialitems.h Collection of materials to populate MaterialModel.