Skip to content

Commit

Permalink
jx_layout_editor: improve code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed Jan 20, 2024
1 parent 05e2422 commit ec93012
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 24 deletions.
4 changes: 4 additions & 0 deletions todo-jxlayout
Expand Up @@ -4,6 +4,10 @@ when show/hide toolbar, change window size to preserve LayoutContainer size

snap-to-grid for bottom & right is offset

LayoutConfigDialog:
SetWMClass
SetMinSize/SetMaxSize/etc

-----

JXImageWidget
Expand Down
47 changes: 46 additions & 1 deletion tools/jx_layout_editor/code/BaseWidget.cpp
Expand Up @@ -226,7 +226,7 @@ BaseWidget::EditConfiguration
******************************************************************************/

void
bool
BaseWidget::GenerateCode
(
std::ostream& output,
Expand All @@ -237,6 +237,11 @@ BaseWidget::GenerateCode
)
const
{
if (WaitForCodeDependency(*objNames))
{
return false;
}

indent.Print(output);
if (itsIsMemberVarFlag)
{
Expand Down Expand Up @@ -276,6 +281,46 @@ BaseWidget::GenerateCode

PrintConfiguration(output, indent, itsVarName, stringdb);
output << std::endl;
return true;
}

/******************************************************************************
PrepareToGenerateCode (virtual)
******************************************************************************/

void
BaseWidget::PrepareToGenerateCode()
const
{
}

/******************************************************************************
GenerateCodeFinished (virtual)
******************************************************************************/

void
BaseWidget::GenerateCodeFinished()
const
{
}

/******************************************************************************
WaitForCodeDependency (virtual protected)
Return true if need to wait for another object to be created first.
******************************************************************************/

bool
BaseWidget::WaitForCodeDependency
(
const JPtrArray<JString>& objNames
)
const
{
return false;
}

/******************************************************************************
Expand Down
6 changes: 5 additions & 1 deletion tools/jx_layout_editor/code/BaseWidget.h
Expand Up @@ -47,11 +47,14 @@ class BaseWidget : public JXWidget
JPoint GetDragStartPointGlobal() const;

void EditConfiguration(const bool createUndo = true);
void GenerateCode(std::ostream& output, const JString& indent,
bool GenerateCode(std::ostream& output, const JString& indent,
JPtrArray<JString>* objTypes,
JPtrArray<JString>* objNames,
JStringManager* stringdb) const;

virtual void PrepareToGenerateCode() const;
virtual void GenerateCodeFinished() const;

void PrepareToAcceptDrag();

virtual JString GetEnclosureName() const;
Expand All @@ -71,6 +74,7 @@ class BaseWidget : public JXWidget
const JString& indent,
const JString& varName,
JStringManager* stringdb) const;
virtual bool WaitForCodeDependency(const JPtrArray<JString>& objNames) const;

virtual void AddPanels(WidgetParametersDialog* dlog);
virtual void SavePanelData();
Expand Down
54 changes: 52 additions & 2 deletions tools/jx_layout_editor/code/CustomWidget.cpp
Expand Up @@ -74,6 +74,11 @@ CustomWidget::CustomWidget
{
input >> itsClassName >> itsCtorArgs >> itsCreateFlag;

if (vers >= 2)
{
input >> itsDependencyNames;
}

CustomWidgetX();
}

Expand Down Expand Up @@ -113,6 +118,7 @@ CustomWidget::StreamOut
output << itsClassName << std::endl;
output << itsCtorArgs << std::endl;
output << itsCreateFlag << std::endl;
output << itsDependencyNames << std::endl;
}

/******************************************************************************
Expand Down Expand Up @@ -225,6 +231,49 @@ CustomWidget::PrintCtorArgsWithComma
}
}

/******************************************************************************
WaitForCodeDependency (virtual protected)
Return true if need to wait for another object to be created first.
******************************************************************************/

bool
CustomWidget::WaitForCodeDependency
(
const JPtrArray<JString>& objNames
)
const
{
if (itsDependencyNames.IsEmpty())
{
return false;
}

JPtrArray<JString> list(JPtrArrayT::kDeleteAll);
itsDependencyNames.Split(",", &list);

for (auto* d : list)
{
bool found = false;
for (auto* n : objNames)
{
if (*n == *d)
{
found = true;
break;
}
}

if (!found)
{
return true;
}
}

return false;
}

/******************************************************************************
AddPanels (virtual protected)
Expand All @@ -238,7 +287,7 @@ CustomWidget::AddPanels
{
itsPanel =
jnew CustomWidgetPanel(dlog, itsClassName, itsCtorArgs, itsCreateFlag,
WantsInput());
itsDependencyNames, WantsInput());
}

/******************************************************************************
Expand All @@ -250,7 +299,8 @@ void
CustomWidget::SavePanelData()
{
bool wantsInput;
itsPanel->GetValues(&itsClassName, &itsCtorArgs, &itsCreateFlag, &wantsInput);
itsPanel->GetValues(&itsClassName, &itsCtorArgs, &itsCreateFlag,
&itsDependencyNames, &wantsInput);
SetWantsInput(wantsInput);
itsPanel = nullptr;
}
2 changes: 2 additions & 0 deletions tools/jx_layout_editor/code/CustomWidget.h
Expand Up @@ -45,6 +45,7 @@ class CustomWidget : public BaseWidget
void PrintCtorArgsWithComma(std::ostream& output,
const JString& varName,
JStringManager* stringdb) const override;
bool WaitForCodeDependency(const JPtrArray<JString>& objNames) const override;

void AddPanels(WidgetParametersDialog* dlog) override;
void SavePanelData() override;
Expand All @@ -54,6 +55,7 @@ class CustomWidget : public BaseWidget
JString itsClassName;
JString itsCtorArgs;
bool itsCreateFlag;
JString itsDependencyNames;

CustomWidgetPanel* itsPanel; // nullptr unless editing

Expand Down
24 changes: 20 additions & 4 deletions tools/jx_layout_editor/code/CustomWidgetPanel.cpp
Expand Up @@ -30,10 +30,11 @@ CustomWidgetPanel::CustomWidgetPanel
const JString& className,
const JString& ctorArgs,
const bool needsCreate,
const JString& deps,
const bool wantsInput
)
{
BuildPanel(dlog, className, ctorArgs, needsCreate, wantsInput);
BuildPanel(dlog, className, ctorArgs, needsCreate, deps, wantsInput);
}

/******************************************************************************
Expand All @@ -58,6 +59,7 @@ CustomWidgetPanel::BuildPanel
const JString& className,
const JString& ctorArgs,
const bool needsCreate,
const JString& deps,
const bool wantsInput
)
{
Expand All @@ -67,7 +69,7 @@ CustomWidgetPanel::BuildPanel

auto* container =
jnew JXWidgetSet(window,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 0,0, 460,100);
JXWidget::kFixedLeft, JXWidget::kFixedTop, 0,0, 460,130);
assert( container != nullptr );

auto* classNameLabel =
Expand All @@ -80,14 +82,19 @@ CustomWidgetPanel::BuildPanel
JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,39, 80,20);
constructorArgsLabel->SetToLabel(false);

auto* dependencyLabel =
jnew JXStaticText(JGetString("dependencyLabel::CustomWidgetPanel::Panel"),container,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,70, 80,20);
dependencyLabel->SetToLabel(false);

itsWantsInputCB =
jnew JXTextCheckbox(JGetString("itsWantsInputCB::CustomWidgetPanel::Panel"), container,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,70, 160,20);
JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,100, 160,20);
itsWantsInputCB->SetShortcuts(JGetString("itsWantsInputCB::shortcuts::CustomWidgetPanel::Panel"));

itsNeedsCreateCB =
jnew JXTextCheckbox(JGetString("itsNeedsCreateCB::CustomWidgetPanel::Panel"), container,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 280,70, 160,20);
JXWidget::kFixedLeft, JXWidget::kFixedTop, 280,100, 160,20);
itsNeedsCreateCB->SetShortcuts(JGetString("itsNeedsCreateCB::shortcuts::CustomWidgetPanel::Panel"));

itsClassNameInput =
Expand All @@ -98,6 +105,11 @@ CustomWidgetPanel::BuildPanel
jnew JXInputField(container,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 100,40, 340,20);

itsDependencyInput =
jnew JXInputField(container,
JXWidget::kFixedLeft, JXWidget::kFixedTop, 100,70, 340,20);
itsDependencyInput->SetValidationPattern(jnew JRegex("^[_a-z][_a-z0-9,]+$", "i"), "itsDependencyInput::validation::CustomWidgetPanel::Panel");

// end Panel

dlog->AddPanel(this, container);
Expand All @@ -111,6 +123,8 @@ CustomWidgetPanel::BuildPanel
itsCtorArgs->GetText()->SetText(ctorArgs);
itsNeedsCreateCB->SetState(needsCreate);

itsDependencyInput->GetText()->SetText(deps);

itsWantsInputCB->SetState(wantsInput);
}

Expand All @@ -125,11 +139,13 @@ CustomWidgetPanel::GetValues
JString* className,
JString* ctorArgs,
bool* needsCreate,
JString* deps,
bool* wantsInput
)
{
*className = itsClassNameInput->GetText()->GetText();
*ctorArgs = itsCtorArgs->GetText()->GetText();
*deps = itsDependencyInput->GetText()->GetText();
*needsCreate = itsNeedsCreateCB->IsChecked();
*wantsInput = itsWantsInputCB->IsChecked();
}
7 changes: 4 additions & 3 deletions tools/jx_layout_editor/code/CustomWidgetPanel.h
Expand Up @@ -20,12 +20,12 @@ class CustomWidgetPanel : public WidgetPanelBase

CustomWidgetPanel(WidgetParametersDialog* dlog, const JString& className,
const JString& ctorArgs, const bool needsCreate,
const bool wantsInput);
const JString& deps, const bool wantsInput);

~CustomWidgetPanel();

void GetValues(JString* className, JString* ctorArgs, bool* needsCreate,
bool* wantsInput);
JString* deps, bool* wantsInput);

private:

Expand All @@ -35,14 +35,15 @@ class CustomWidgetPanel : public WidgetPanelBase
JXTextCheckbox* itsNeedsCreateCB;
JXInputField* itsClassNameInput;
JXInputField* itsCtorArgs;
JXInputField* itsDependencyInput;

// end Panel

private:

void BuildPanel(WidgetParametersDialog* dlog, const JString& className,
const JString& ctorArgs, const bool needsCreate,
const bool wantsInput);
const JString& deps, const bool wantsInput);
};

#endif

0 comments on commit ec93012

Please sign in to comment.