Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Apr 30, 2023
1 parent 41b78a1 commit 1c021a7
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 137 deletions.
65 changes: 39 additions & 26 deletions lib/tlUI/FloatEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <tlUI/FloatEdit.h>

#include <tlUI/LineEdit.h>

#include <tlCore/StringFormat.h>

namespace tl
Expand All @@ -15,7 +13,6 @@ namespace tl
struct FloatEdit::Private
{
std::shared_ptr<FloatModel> model;
std::shared_ptr<LineEdit> lineEdit;
int digits = 3;
int precision = 2;

Expand All @@ -27,14 +24,13 @@ namespace tl
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
{
IWidget::_init("tl::ui::FloatEdit", context, parent);
LineEdit::_init(context, parent);
_name = "tl::ui::FloatEdit";
TLRENDER_P();

p.lineEdit = LineEdit::create(context, shared_from_this());

setModel(FloatModel::create(context));

_textUpdate();
_floatUpdate();
}

FloatEdit::FloatEdit() :
Expand Down Expand Up @@ -70,16 +66,16 @@ namespace tl
p.model->observeValue(),
[this](float)
{
_textUpdate();
_floatUpdate();
});
p.rangeObserver = observer::ValueObserver<math::FloatRange>::create(
p.model->observeRange(),
[this](const math::FloatRange&)
{
_textUpdate();
_floatUpdate();
});
}
_textUpdate();
_floatUpdate();
}

void FloatEdit::setDigits(int value)
Expand All @@ -88,7 +84,7 @@ namespace tl
if (value == p.digits)
return;
p.digits = value;
_textUpdate();
_floatUpdate();
}

void FloatEdit::setPrecision(int value)
Expand All @@ -97,27 +93,44 @@ namespace tl
if (value == p.precision)
return;
p.precision = value;
_textUpdate();
}

void FloatEdit::setFontRole(FontRole value)
{
_p->lineEdit->setFontRole(value);
_floatUpdate();
}

void FloatEdit::setGeometry(const math::BBox2i& value)
void FloatEdit::keyPressEvent(KeyEvent& event)
{
IWidget::setGeometry(value);
_p->lineEdit->setGeometry(value);
LineEdit::keyPressEvent(event);
TLRENDER_P();
if (!event.accept)
{
switch (event.key)
{
case Key::Down:
event.accept = true;
p.model->subtractStep();
break;
case Key::Up:
event.accept = true;
p.model->addStep();
break;
case Key::PageUp:
event.accept = true;
p.model->addLargeStep();
break;
case Key::PageDown:
event.accept = true;
p.model->subtractLargeStep();
break;
}
}
}

void FloatEdit::sizeHintEvent(const SizeHintEvent& event)
void FloatEdit::keyReleaseEvent(KeyEvent& event)
{
IWidget::sizeHintEvent(event);
_sizeHint = _p->lineEdit->getSizeHint();
LineEdit::keyPressEvent(event);
event.accept = true;
}

void FloatEdit::_textUpdate()
void FloatEdit::_floatUpdate()
{
TLRENDER_P();
std::string text;
Expand All @@ -130,8 +143,8 @@ namespace tl
arg(range.getMin() < 0 ? "-" : "").
arg(0.F, p.precision, p.precision + 1 + p.digits);
}
p.lineEdit->setText(text);
p.lineEdit->setFormat(format);
setText(text);
setFormat(format);
}
}
}
13 changes: 5 additions & 8 deletions lib/tlUI/FloatEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#pragma once

#include <tlUI/IWidget.h>
#include <tlUI/LineEdit.h>
#include <tlUI/FloatModel.h>

namespace tl
{
namespace ui
{
//! Floating point number editor.
class FloatEdit : public IWidget
class FloatEdit : public LineEdit
{
TLRENDER_NON_COPYABLE(FloatEdit);

Expand Down Expand Up @@ -43,14 +43,11 @@ namespace tl
//! Set the display precision.
void setPrecision(int);

//! Set the font role.
void setFontRole(FontRole);

void setGeometry(const math::BBox2i&) override;
void sizeHintEvent(const SizeHintEvent&) override;
void keyPressEvent(KeyEvent&) override;
void keyReleaseEvent(KeyEvent&) override;

private:
void _textUpdate();
void _floatUpdate();

TLRENDER_PRIVATE();
};
Expand Down
20 changes: 9 additions & 11 deletions lib/tlUI/GroupBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace tl
if (value == p.text)
return;
p.text = value;
p.draw.glyphs.clear();
_updates |= Update::Size;
_updates |= Update::Draw;
}
Expand All @@ -71,6 +72,7 @@ namespace tl
if (value == p.fontRole)
return;
p.fontRole = value;
p.draw.glyphs.clear();
_updates |= Update::Size;
_updates |= Update::Draw;
}
Expand Down Expand Up @@ -116,20 +118,16 @@ namespace tl

void GroupBox::clipEvent(bool clipped, const ClipEvent& event)
{
const bool changed = clipped != _clipped;
IWidget::clipEvent(clipped, event);
TLRENDER_P();
if (changed)
if (clipped)
{
if (clipped)
{
p.draw.glyphs.clear();
}
else
{
const auto fontInfo = event.style->getFontRole(p.fontRole, event.displayScale);
p.draw.glyphs = event.fontSystem->getGlyphs(p.text, fontInfo);
}
p.draw.glyphs.clear();
}
else if (p.draw.glyphs.empty())
{
const auto fontInfo = event.style->getFontRole(p.fontRole, event.displayScale);
p.draw.glyphs = event.fontSystem->getGlyphs(p.text, fontInfo);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/tlUI/IButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace tl
void setChecked(bool);

//! Set the text.
void setText(const std::string&);
virtual void setText(const std::string&);

//! Set the font role.
void setFontRole(FontRole);
virtual void setFontRole(FontRole);

//! Set the icon.
void setIcon(const std::string&);
Expand Down
63 changes: 38 additions & 25 deletions lib/tlUI/IntEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <tlUI/IntEdit.h>

#include <tlUI/LineEdit.h>

#include <tlCore/StringFormat.h>

namespace tl
Expand All @@ -15,7 +13,6 @@ namespace tl
struct IntEdit::Private
{
std::shared_ptr<IntModel> model;
std::shared_ptr<LineEdit> lineEdit;
int digits = 3;

std::shared_ptr<observer::ValueObserver<int> > valueObserver;
Expand All @@ -26,14 +23,13 @@ namespace tl
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
{
IWidget::_init("tl::ui::IntEdit", context, parent);
LineEdit::_init(context, parent);
_name = "tl::ui::IntEdit";
TLRENDER_P();

p.lineEdit = LineEdit::create(context, shared_from_this());

setModel(IntModel::create(context));

_textUpdate();
_intUpdate();
}

IntEdit::IntEdit() :
Expand Down Expand Up @@ -69,16 +65,16 @@ namespace tl
p.model->observeValue(),
[this](int)
{
_textUpdate();
_intUpdate();
});
p.rangeObserver = observer::ValueObserver<math::IntRange>::create(
p.model->observeRange(),
[this](const math::IntRange&)
{
_textUpdate();
_intUpdate();
});
}
_textUpdate();
_intUpdate();
}

void IntEdit::setDigits(int value)
Expand All @@ -87,27 +83,44 @@ namespace tl
if (value == p.digits)
return;
p.digits = value;
_textUpdate();
}

void IntEdit::setFontRole(FontRole value)
{
_p->lineEdit->setFontRole(value);
_intUpdate();
}

void IntEdit::setGeometry(const math::BBox2i& value)
void IntEdit::keyPressEvent(KeyEvent& event)
{
IWidget::setGeometry(value);
_p->lineEdit->setGeometry(value);
LineEdit::keyPressEvent(event);
TLRENDER_P();
if (!event.accept)
{
switch (event.key)
{
case Key::Down:
event.accept = true;
p.model->subtractStep();
break;
case Key::Up:
event.accept = true;
p.model->addStep();
break;
case Key::PageUp:
event.accept = true;
p.model->addLargeStep();
break;
case Key::PageDown:
event.accept = true;
p.model->subtractLargeStep();
break;
}
}
}

void IntEdit::sizeHintEvent(const SizeHintEvent& event)
void IntEdit::keyReleaseEvent(KeyEvent& event)
{
IWidget::sizeHintEvent(event);
_sizeHint = _p->lineEdit->getSizeHint();
LineEdit::keyPressEvent(event);
event.accept = true;
}

void IntEdit::_textUpdate()
void IntEdit::_intUpdate()
{
TLRENDER_P();
std::string text;
Expand All @@ -120,8 +133,8 @@ namespace tl
arg(range.getMin() < 0 ? "-" : "").
arg(0, p.digits);
}
p.lineEdit->setText(text);
p.lineEdit->setFormat(format);
setText(text);
setFormat(format);
}
}
}
13 changes: 5 additions & 8 deletions lib/tlUI/IntEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#pragma once

#include <tlUI/IWidget.h>
#include <tlUI/LineEdit.h>
#include <tlUI/IntModel.h>

namespace tl
{
namespace ui
{
//! Integer number editor.
class IntEdit : public IWidget
class IntEdit : public LineEdit
{
TLRENDER_NON_COPYABLE(IntEdit);

Expand Down Expand Up @@ -40,14 +40,11 @@ namespace tl
//! Set the number of digits to display.
void setDigits(int);

//! Set the font role.
void setFontRole(FontRole);

void setGeometry(const math::BBox2i&) override;
void sizeHintEvent(const SizeHintEvent&) override;
void keyPressEvent(KeyEvent&) override;
void keyReleaseEvent(KeyEvent&) override;

private:
void _textUpdate();
void _intUpdate();

TLRENDER_PRIVATE();
};
Expand Down
Loading

0 comments on commit 1c021a7

Please sign in to comment.