Warning, file /graphics/glaxnimate/src/gui/widgets/enum_combo.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "enum_combo.hpp"
0008 
0009 #include <cstring>
0010 
0011 #include "model/shapes/fill.hpp"
0012 #include "model/shapes/stroke.hpp"
0013 #include "model/shapes/polystar.hpp"
0014 
0015 #include "glaxnimate_app.hpp"
0016 
0017 using namespace glaxnimate::gui;
0018 using namespace glaxnimate;
0019 
0020 EnumCombo::EnumCombo(const QMetaEnum& meta_enum, int current_value, QWidget* parent)
0021     : QComboBox(parent), meta_enum(meta_enum)
0022 {
0023     populate(current_value);
0024 }
0025 
0026 EnumCombo::EnumCombo(QWidget* parent)
0027     : QComboBox(parent)
0028 {
0029 }
0030 
0031 void EnumCombo::set_data(const QMetaEnum& meta_enum, int current_value)
0032 {
0033     clear();
0034     this->meta_enum = meta_enum;
0035     populate(current_value);
0036 }
0037 
0038 void EnumCombo::populate(int current_value)
0039 {
0040     for ( int i = 0; i < meta_enum.keyCount(); i++ )
0041     {
0042         auto data = data_for(meta_enum, meta_enum.value(i));
0043         addItem(QIcon::fromTheme(data.second), data.first, meta_enum.value(i));
0044         if ( meta_enum.value(i) == current_value )
0045             setCurrentIndex(count() - 1);
0046     }
0047 }
0048 
0049 std::pair<QString, const char*> EnumCombo::data_for(const QMetaEnum& meta_enum, int value)
0050 {
0051     if ( std::strcmp(meta_enum.name(), "Rule") == 0 )
0052     {
0053         switch ( model::Fill::Rule(value) )
0054         {
0055             case model::Fill::NonZero:
0056                 return {i18n("NonZero"), "fill-rule-nonzero"};
0057             case model::Fill::EvenOdd:
0058                 return {i18n("Even Odd"), "fill-rule-even-odd"};
0059         }
0060     }
0061     else if ( std::strcmp(meta_enum.name(), "Cap") == 0 )
0062     {
0063         switch ( model::Stroke::Cap(value) )
0064         {
0065             case model::Stroke::ButtCap:
0066                 return {i18n("Butt"), "stroke-cap-butt"};
0067             case model::Stroke::RoundCap:
0068                 return {i18n("Round"), "stroke-cap-round"};
0069             case model::Stroke::SquareCap:
0070                 return {i18n("Square"), "stroke-cap-square"};
0071         }
0072     }
0073     else if ( std::strcmp(meta_enum.name(), "Join") == 0 )
0074     {
0075         switch ( model::Stroke::Join(value) )
0076         {
0077             case model::Stroke::MiterJoin:
0078                 return {i18n("Miter"), "stroke-cap-miter"};
0079             case model::Stroke::RoundJoin:
0080                 return {i18n("Round"), "stroke-join-round"};
0081             case model::Stroke::BevelJoin:
0082                 return {i18n("Bevel"), "stroke-cap-bevel"};
0083         }
0084     }
0085     else if ( std::strcmp(meta_enum.name(), "StarType") == 0 )
0086     {
0087         switch ( model::PolyStar::StarType(value) )
0088         {
0089             case model::PolyStar::Star:
0090                 return {i18n("Star"), "draw-star"};
0091             case model::PolyStar::Polygon:
0092                 return {i18n("Polygon"), "draw-polygon"};
0093         }
0094     }
0095     else if ( std::strcmp(meta_enum.name(), "GradientType") == 0 )
0096     {
0097         switch ( model::Gradient::GradientType(value) )
0098         {
0099             case model::Gradient::Linear:
0100                 return {i18n("Linear"), "paint-gradient-linear"};
0101             case model::Gradient::Radial:
0102                 return {i18n("Radial"), "paint-gradient-radial"};
0103             case model::Gradient::Conical:
0104                 return {i18n("Conical"), "paint-gradient-conical"};
0105         }
0106     }
0107 
0108     return {meta_enum.valueToKey(value), "paint-unknown"};
0109 }
0110 
0111 void EnumCombo::retranslate()
0112 {
0113     for ( int i = 0; i < count(); i++ )
0114     {
0115         setItemText(i, data_for(meta_enum, meta_enum.value(i)).first);
0116     }
0117 }
0118 
0119 int EnumCombo::current_value() const
0120 {
0121     return itemData(currentIndex()).toInt();
0122 }
0123 
0124 void EnumCombo::set_current_value(int value)
0125 {
0126     for ( int i = 0; i < count(); i++ )
0127         if ( meta_enum.value(i) == value )
0128         {
0129             setCurrentIndex(i);
0130             break;
0131         }
0132 }
0133 
0134 bool EnumCombo::set_data_from_qvariant(const QVariant& data)
0135 {
0136     clear();
0137     int value = 0;
0138     if ( inspect_qvariant(data, meta_enum, value) )
0139     {
0140         set_data(meta_enum,  data.toInt());
0141         return true;
0142     }
0143     return false;
0144 }
0145 
0146 bool EnumCombo::inspect_qvariant(const QVariant& data, QMetaEnum& meta_enum, int& value)
0147 {
0148     const QMetaObject* mo = QMetaType(data.userType()).metaObject();
0149     if ( !mo )
0150         return false;
0151 
0152     int index = mo->indexOfEnumerator(
0153         model::detail::naked_type_name(data.typeName()).toStdString().c_str()
0154     );
0155     if ( index == -1 )
0156         return false;
0157 
0158     meta_enum = mo->enumerator(index);
0159     value = data.toInt();
0160     return true;
0161 }
0162 
0163 
0164 std::pair<QString, const char *> EnumCombo::data_for(const QVariant& data)
0165 {
0166     QMetaEnum meta_enum;
0167     int value = 0;
0168     if ( inspect_qvariant(data, meta_enum, value) )
0169         return data_for(meta_enum, value);
0170 
0171     return {"", "paint-unknown"};
0172 }