Skip to content

Commit

Permalink
Create DateStyleSelectorWidget.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Mar 23, 2024
1 parent b2564f7 commit 8b5dbc0
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 95 deletions.
102 changes: 7 additions & 95 deletions src/QtJsonWidget/IJsonWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include "QtJsonWidget/object/ObjectGridLayoutWidget.h"
#include "QtJsonWidget/string/StringLineEditWidget.h"
#include "QtJsonWidget/string/color/ColorDialogButtonWidget.h"
#include "QtJsonWidget/string/date/CalendarWidget.h"
#include "QtJsonWidget/string/date/DateEditWidget.h"
#include "QtJsonWidget/string/date/DateStyleSelectorWidget.h"
#include "QtJsonWidget/string/enum/ComboBoxWidget.h"

#include <QEvent>
Expand Down Expand Up @@ -106,23 +105,6 @@ class TypeSelectorWidget : public IJsonWidget
return "";
}

enum class EDateStyle
{
Calendar,
DateEdit,
};

//--------------------------------------------------------------------------
static QString tr(EDateStyle style)
{
switch (style)
{
case EDateStyle::Calendar: return IJsonWidget::tr("Calendar");
case EDateStyle::DateEdit: return IJsonWidget::tr("Date edit");
}
return "";
}

public:
//--------------------------------------------------------------------------
TypeSelectorWidget(const JsonReferenceResolver& jsonReferenceResolver,
Expand All @@ -144,11 +126,6 @@ class TypeSelectorWidget : public IJsonWidget
formatLayout.addWidget(&stringFormatComboBox);
layout.addLayout(&formatLayout);

styleLayout.addWidget(&styleLabel);
fillStyleComboBoxes();
styleLayout.addWidget(&dateStyleComboBox);
layout.addLayout(&styleLayout);

widget = std::make_unique<null::LabelWidget>();
layout.addWidget(widget.get());
if (typeComboBox.count() != 0)
Expand All @@ -167,12 +144,6 @@ class TypeSelectorWidget : public IJsonWidget
this,
&TypeSelectorWidget::stringFormatCurrentIndexChanged);

QObject::connect(&dateStyleComboBox,
static_cast<void (QComboBox::*)(int index)>(
&QComboBox::currentIndexChanged),
this,
&TypeSelectorWidget::replaceWidget);

setLayout(&layout);
retranslateUi();
}
Expand Down Expand Up @@ -204,7 +175,6 @@ class TypeSelectorWidget : public IJsonWidget
{
typeLabel.setText(IJsonWidget::tr("type:"));
formatLabel.setText(IJsonWidget::tr("format:"));
styleLabel.setText(IJsonWidget::tr("style:"));

for (int i = 0; i != typeComboBox.count(); ++i)
{
Expand All @@ -216,11 +186,6 @@ class TypeSelectorWidget : public IJsonWidget
stringFormatComboBox.setItemText(
i, tr(EStringFormat(stringFormatComboBox.itemData(i).toInt())));
}
for (int i = 0; i != dateStyleComboBox.count(); ++i)
{
dateStyleComboBox.setItemText(
i, tr(EDateStyle(dateStyleComboBox.itemData(i).toInt())));
}
}

private:
Expand Down Expand Up @@ -287,13 +252,6 @@ class TypeSelectorWidget : public IJsonWidget
stringFormatComboBox.addItem("", int(EStringFormat::Date));
}

//--------------------------------------------------------------------------
void fillStyleComboBoxes()
{
dateStyleComboBox.addItem("", int(EDateStyle::Calendar));
dateStyleComboBox.addItem("", int(EDateStyle::DateEdit));
}

//--------------------------------------------------------------------------
void replaceWidget()
{
Expand All @@ -302,11 +260,8 @@ class TypeSelectorWidget : public IJsonWidget
const auto stringFormat = static_cast<EStringFormat>(
stringFormatComboBox.itemData(stringFormatComboBox.currentIndex())
.toInt());
const auto dateStyle = static_cast<EDateStyle>(
dateStyleComboBox.itemData(dateStyleComboBox.currentIndex())
.toInt());

auto newWidget = makeWidget(type, stringFormat, dateStyle);
auto newWidget = makeWidget(type, stringFormat);
layout.replaceWidget(widget.get(), newWidget.get());
widget = std::move(newWidget);
QObject::connect(widget.get(),
Expand All @@ -327,52 +282,27 @@ class TypeSelectorWidget : public IJsonWidget
{
formatLabel.show();
stringFormatComboBox.show();
styleLabel.hide();
dateStyleComboBox.hide();
break;
}
default:
{
formatLabel.hide();
stringFormatComboBox.hide();
styleLabel.hide();
dateStyleComboBox.hide();
break;
}
}
replaceWidget();
}

//--------------------------------------------------------------------------
void stringFormatCurrentIndexChanged(int index)
void stringFormatCurrentIndexChanged()
{
const auto stringFormat = static_cast<EStringFormat>(
stringFormatComboBox.itemData(index).toInt());

stringFormatComboBox.show();
switch (stringFormat)
{
default:
case EStringFormat::Color:
case EStringFormat::String:
{
styleLabel.hide();
dateStyleComboBox.hide();
break;
}
case EStringFormat::Date:
{
styleLabel.show();
dateStyleComboBox.show();
break;
}
}
replaceWidget();
}

//--------------------------------------------------------------------------
std::unique_ptr<IJsonWidget>
makeWidget(EType type, EStringFormat format, EDateStyle dateStyle)
makeWidget(EType type, EStringFormat format)
{
switch (type)
{
Expand All @@ -393,14 +323,8 @@ class TypeSelectorWidget : public IJsonWidget
return std::make_unique<string::LineEditWidget>(schema);
case EStringFormat::Date:
{
switch (dateStyle)
{
default:
case EDateStyle::Calendar:
return std::make_unique<CalendarWidget>(schema);
case EDateStyle::DateEdit:
return std::make_unique<DateEditWidget>(schema);
}
return std::make_unique<DateStyleSelectorWidget>(
schema);
}
case EStringFormat::Color:
return std::make_unique<ColorDialogButtonWidget>(
Expand All @@ -423,13 +347,10 @@ class TypeSelectorWidget : public IJsonWidget
QVBoxLayout layout;
QHBoxLayout typeLayout;
QHBoxLayout formatLayout;
QHBoxLayout styleLayout;
QLabel typeLabel;
QLabel formatLabel;
QLabel styleLabel;
QComboBox typeComboBox;
QComboBox stringFormatComboBox;
QComboBox dateStyleComboBox;
std::unique_ptr<IJsonWidget> widget;
};

Expand Down Expand Up @@ -492,16 +413,7 @@ makeWidget(const JsonReferenceResolver& jsonReferenceResolver,
}
else if (format == json_keys::format_date)
{
const auto style = json[json_keys::key_style];
if (style == json_keys::style_calendar)
{
return std::make_unique<CalendarWidget>(json);
}
else if (style == json_keys::style_dateedit)
{
return std::make_unique<DateEditWidget>(json);
}
else { return std::make_unique<CalendarWidget>(json); }
return std::make_unique<DateStyleSelectorWidget>(json);
}
return std::make_unique<string::LineEditWidget>(json);
}
Expand Down
115 changes: 115 additions & 0 deletions src/QtJsonWidget/string/date/DateStyleSelectorWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "DateStyleSelectorWidget.h"

#include "QtJsonWidget/string/date/CalendarWidget.h"
#include "QtJsonWidget/string/date/DateEditWidget.h"
#include "QtJsonWidget/jsonKeys.h"

namespace
{

//----------------------------------------------------------------------
enum class EStyle
{
Calendar,
DateEdit,
};

//----------------------------------------------------------------------
QString tr(EStyle style)
{
switch (style)
{
case EStyle::Calendar: return IJsonWidget::tr("Calendar");
case EStyle::DateEdit: return IJsonWidget::tr("Date edit");
}
return "";
}

//----------------------------------------------------------------------
std::optional<EStyle> toEStyle(QString s)
{
if (s == json_keys::style_calendar) { return EStyle::Calendar; }
else if (s == json_keys::style_dateedit) { return EStyle::DateEdit; }
else { return std::nullopt; }
}

//--------------------------------------------------------------------------
std::unique_ptr<IJsonWidget> makeWidget(EStyle integerStyle,
QJsonValue schema)
{
switch (integerStyle)
{
default:
case EStyle::Calendar:
return std::make_unique<CalendarWidget>(schema);
case EStyle::DateEdit:
return std::make_unique<DateEditWidget>(schema);
}
}
} // namespace

//--------------------------------------------------------------------------
DateStyleSelectorWidget::DateStyleSelectorWidget(QJsonValue schema) :
schema(schema)
{
styleComboBox.addItem("", int(EStyle::Calendar));
styleComboBox.addItem("", int(EStyle::DateEdit));

const auto style = toEStyle(schema[json_keys::key_style].toString());
constexpr auto defaultStyle = EStyle::Calendar;
widget = makeWidget(style.value_or(defaultStyle), schema);
styleComboBox.setCurrentIndex(
styleComboBox.findData(static_cast<int>(style.value_or(defaultStyle))));
if (style)
{
styleComboBox.hide();
styleLabel.hide();
}

hLayout.addWidget(&styleLabel);
hLayout.addWidget(&styleComboBox);

vLayout.addLayout(&hLayout);
vLayout.addWidget(widget.get());

QObject::connect(
&styleComboBox,
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this,
&DateStyleSelectorWidget::replaceWidget);

setLayout(&vLayout);
retranslateUi();
}

//--------------------------------------------------------------------------
void DateStyleSelectorWidget::changeEvent(QEvent* event) /* override */
{
if (event->type() == QEvent::LanguageChange) { retranslateUi(); }
QWidget::changeEvent(event);
}

//--------------------------------------------------------------------------
void DateStyleSelectorWidget::retranslateUi()
{
styleLabel.setText(IJsonWidget::tr("style:"));
for (int i = 0; i != styleComboBox.count(); ++i)
{
styleComboBox.setItemText(
i, ::tr(EStyle(styleComboBox.itemData(i).toInt())));
}
}

//--------------------------------------------------------------------------
void DateStyleSelectorWidget::replaceWidget()
{
const auto integerStyle = static_cast<EStyle>(
styleComboBox.itemData(styleComboBox.currentIndex()).toInt());

auto newWidget = makeWidget(integerStyle, schema);
newWidget->fromQJson(widget->toQJson());
vLayout.replaceWidget(widget.get(), newWidget.get());
widget = std::move(newWidget);
QObject::connect(
widget.get(), &IJsonWidget::hasChanged, this, &IJsonWidget::hasChanged);
}
31 changes: 31 additions & 0 deletions src/QtJsonWidget/string/date/DateStyleSelectorWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "QtJsonWidget/IJsonWidget.h"

#include <QComboBox>
#include <QEvent>
#include <QLabel>
#include <optional>

class DateStyleSelectorWidget : public IJsonWidget
{
public:
explicit DateStyleSelectorWidget(QJsonValue schema);

QJsonValue toQJson() const override { return widget->toQJson(); }
void fromQJson(QJsonValue value) override { widget->fromQJson(value); }
void changeEvent(QEvent* event) override;

void retranslateUi();

private:
void replaceWidget();

private:
QJsonValue schema;
QVBoxLayout vLayout;
QHBoxLayout hLayout;
QLabel styleLabel;
QComboBox styleComboBox;
std::unique_ptr<IJsonWidget> widget;
};

0 comments on commit 8b5dbc0

Please sign in to comment.