gadget API fluency

portability/boost
zetaPRIME 2019-06-30 16:38:39 -04:00
parent 0ab25999cb
commit b5f5aaf282
14 changed files with 90 additions and 65 deletions

View File

@ -30,8 +30,9 @@ namespace Xybrid::UI {
void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
inline void setSize(QSizeF s) { size = s; }
inline void setSize(qreal w, qreal h = 24) { size = {w, h}; }
inline ButtonGadget* setSize(QSizeF s) { size = s; update(); return this; }
inline ButtonGadget* setSize(qreal w, qreal h = 24) { size = {w, h}; update(); return this; }
inline ButtonGadget* setText(const QString& t) { text = t; update(); return this;}
signals:
void clicked();

View File

@ -11,23 +11,23 @@ using namespace Xybrid::UI;
#include <QRadialGradient>
#include <QGraphicsSceneMouseEvent>
template void KnobGadget::bind(double&);
template void KnobGadget::bind(float&);
template void KnobGadget::bind(int8_t&);
template void KnobGadget::bind(uint8_t&);
template void KnobGadget::bind(int16_t&);
template void KnobGadget::bind(uint16_t&);
template void KnobGadget::bind(int32_t&);
template void KnobGadget::bind(uint32_t&);
template KnobGadget* KnobGadget::bind(double&);
template KnobGadget* KnobGadget::bind(float&);
template KnobGadget* KnobGadget::bind(int8_t&);
template KnobGadget* KnobGadget::bind(uint8_t&);
template KnobGadget* KnobGadget::bind(int16_t&);
template KnobGadget* KnobGadget::bind(uint16_t&);
template KnobGadget* KnobGadget::bind(int32_t&);
template KnobGadget* KnobGadget::bind(uint32_t&);
template void KnobGadget::bind(std::atomic<double>&);
template void KnobGadget::bind(std::atomic<float>&);
template void KnobGadget::bind(std::atomic<int8_t>&);
template void KnobGadget::bind(std::atomic<uint8_t>&);
template void KnobGadget::bind(std::atomic<int16_t>&);
template void KnobGadget::bind(std::atomic<uint16_t>&);
template void KnobGadget::bind(std::atomic<int32_t>&);
template void KnobGadget::bind(std::atomic<uint32_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<double>&);
template KnobGadget* KnobGadget::bind(std::atomic<float>&);
template KnobGadget* KnobGadget::bind(std::atomic<int8_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<uint8_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<int16_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<uint16_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<int32_t>&);
template KnobGadget* KnobGadget::bind(std::atomic<uint32_t>&);
double KnobGadget::get() {
double v = fGet();
@ -147,11 +147,6 @@ void KnobGadget::contextMenuEvent(QGraphicsSceneContextMenuEvent* e) {
e->accept();
}
void KnobGadget::setLabel(const QString& s) {
label->setText(s);
dirty = true;
}
void KnobGadget::autoCreate(LayoutGadget* l, NodeLib::ADSR& adsr) {
KnobGadget* k;

View File

@ -53,18 +53,32 @@ namespace Xybrid::UI {
void mouseMoveEvent(QGraphicsSceneMouseEvent*) override;
void contextMenuEvent(QGraphicsSceneContextMenuEvent*) override;
void setLabel(const QString&);
inline KnobGadget* setLabel(const QString& s) { label->setText(s); dirty = true; update(); return this; }
inline KnobGadget* setTextFunc(const std::function<QString(double)>& f) { fText = f; dirty = true; update(); return this; }
inline KnobGadget* setRange(double min, double max, double step = -1, int px = -1) {
this->min = min;
this->max = max;
if (step > 0) this->step = step;
if (px > 0) stepPx = px;
update();
return this;
}
inline KnobGadget* setDefault(double d) { this->defaultVal = d; return this; }
// and binding templates
template<typename T> void bind(T& ref) {
template<typename T> KnobGadget* bind(T& ref) {
auto p = &ref; // pointerize
fGet = [p] { return static_cast<double>(*p); };
fSet = [p](double d) { *p = static_cast<T>(d); };
update();
return this;
}
template<typename T> void bind(std::atomic<T>& atm) {
template<typename T> KnobGadget* bind(std::atomic<T>& atm) {
auto p = &atm;
fGet = [p] { return p->load(); };
fSet = [p](double d) { p->store(static_cast<T>(d)); };
update();
return this;
}
static void autoCreate(LayoutGadget*, NodeLib::ADSR&);

View File

@ -25,15 +25,18 @@ void LabelGadget::paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
QPainterPath path;
path.addText({1, fm.ascent() + 1}, font, text);
auto oc = color.lighter(static_cast<int>(color.saturationF() * 20));
p->fillPath(path, oc);
p->strokePath(path, QPen(oc, 3));
if (outlineEnabled) {
auto oc = color.lighter(static_cast<int>(color.saturationF() * 20));
p->fillPath(path, oc);
p->strokePath(path, QPen(oc, 3));
}
p->fillPath(path, color);
}
void LabelGadget::setText(const QString& t) {
LabelGadget* LabelGadget::setText(const QString& t) {
text = t;
QFontMetricsF fm(font);
size = {fm.width(text) + 2, fm.height() + 2};
update();
return this;
}

View File

@ -8,6 +8,7 @@ namespace Xybrid::UI {
QString text;
QColor color = {255, 255, 255};
bool outlineEnabled = false;
public:
@ -17,7 +18,8 @@ namespace Xybrid::UI {
QRectF boundingRect() const override;
void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override;
void setText(const QString&);
inline void setColor(const QColor& c) { color = c; update(); }
LabelGadget* setText(const QString&);
inline LabelGadget* setColor(const QColor& c) { color = c; update(); return this; }
inline LabelGadget* useOutline(bool b = true) { outlineEnabled = b; update(); return this;}
};
}

View File

@ -51,7 +51,7 @@ LayoutGadget::LayoutGadget(NodeObject* parent, bool vertical) : LayoutGadget(par
});
}
void LayoutGadget::addSpacer() { new LayoutSpacerGadget(this); }
LayoutGadget* LayoutGadget::addSpacer() { new LayoutSpacerGadget(this); return this; }
void LayoutGadget::updateGeometry() {
qreal cur = margin;

View File

@ -14,8 +14,8 @@ namespace Xybrid::UI {
public:
QSizeF minSize = {0, 0};
qreal spacing = 6;
qreal margin = 6;
qreal spacing = 6;
qreal bias = 0.5;
bool vertical = false;
@ -24,7 +24,14 @@ namespace Xybrid::UI {
LayoutGadget(NodeObject* parent, bool vertical = false);
~LayoutGadget() override = default;
void addSpacer();
inline LayoutGadget* setMetrics(qreal margin = -1, qreal spacing = -1, qreal bias = -1) {
if (margin >= 0) this->margin = margin;
if (spacing >= 0) this->spacing = spacing;
if (bias >= 0) this->bias = std::clamp(bias, 0.0, 1.0);
return this;
}
LayoutGadget* addSpacer();
void updateGeometry() override;

View File

@ -150,9 +150,3 @@ void SampleSelectorGadget::mousePressEvent(QGraphicsSceneMouseEvent* e) {
void SampleSelectorGadget::hoverEnterEvent(QGraphicsSceneHoverEvent*) { update(); }
void SampleSelectorGadget::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { update(); }
void SampleSelectorGadget::setSample(std::shared_ptr<Sample> smp, bool signal) {
currentSample = smp;
update();
if (signal) emit sampleSelected(smp);
}

View File

@ -27,10 +27,15 @@ namespace Xybrid::UI {
void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
inline void setSize(const QSizeF& s) { size = s; update(); }
inline void setSize(qreal w, qreal h) { setSize(QSizeF(w, h)); }
inline SampleSelectorGadget* setSize(const QSizeF& s) { size = s; update(); return this; }
inline SampleSelectorGadget* setSize(qreal w, qreal h) { return setSize({w, h}); }
void setSample(std::shared_ptr<Data::Sample>, bool signal = true);
inline SampleSelectorGadget* setSample(std::shared_ptr<Data::Sample> smp, bool signal = true) {
currentSample = smp;
update();
if (signal) emit sampleSelected(smp);
return this;
}
signals:
void sampleSelected(std::shared_ptr<Data::Sample>);

View File

@ -73,15 +73,3 @@ void SelectorGadget::mousePressEvent(QGraphicsSceneMouseEvent* e) {
void SelectorGadget::hoverEnterEvent(QGraphicsSceneHoverEvent*) { update(); }
void SelectorGadget::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { update(); }
void SelectorGadget::setWidth(qreal w) {
width = w;
update();
}
void SelectorGadget::setEntry(const SelectorGadget::Entry& e, bool signal) {
_entry = e;
update();
if (signal) emit onSelect(_entry);
}

View File

@ -33,10 +33,18 @@ namespace Xybrid::UI {
void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
void setWidth(qreal);
inline SelectorGadget* setWidth(qreal w) { width = w; update(); return this; }
const Entry& entry() const { return _entry; }
void setEntry(const Entry&, bool signal = true);
inline const Entry& entry() const { return _entry; }
inline SelectorGadget* setEntry(const Entry& e, bool signal = true) {
_entry = e;
update();
if (signal) emit onSelect(_entry);
return this;
}
inline SelectorGadget* setListFunc(const std::function<std::vector<Entry>()>& f) { fGetList = f; return this; }
inline SelectorGadget* setEditFunc(const std::function<void(QMenu*)>& f) { fEditMenu = f; return this; }
signals:
void onSelect(const Entry&);

View File

@ -47,7 +47,7 @@ void ToggleGadget::paint(QPainter* p, const QStyleOptionGraphicsItem* opt, QWidg
p->setPen(QPen(QBrush(outline), 2));
p->drawEllipse(r);
if (hover) toolTip(text, {0, -1}, color);
if (hover) toolTip(text, toolTipPosition, color);
else toolTip();
}
@ -70,8 +70,9 @@ void ToggleGadget::mouseReleaseEvent(QGraphicsSceneMouseEvent* e) {
void ToggleGadget::hoverEnterEvent(QGraphicsSceneHoverEvent*) { update(); }
void ToggleGadget::hoverLeaveEvent(QGraphicsSceneHoverEvent*) { pressed = false; update(); }
void ToggleGadget::bind(bool& b) {
ToggleGadget* ToggleGadget::bind(bool& b) {
auto p = &b;
fGet = [p] { return *p; };
fSet = [p](bool b) { *p = b; };
return this;
}

View File

@ -19,7 +19,8 @@ namespace Xybrid::UI {
public:
QString text;
QColor color = QColor(127, 127, 127);
QColor color = {127, 127, 127};
QPointF toolTipPosition = {0, -1};
std::function<bool()> fGet = [] { return false; };
std::function<void(bool)> fSet = [](bool) { };
@ -36,7 +37,14 @@ namespace Xybrid::UI {
void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
void bind(bool&);
ToggleGadget* bind(bool&);
inline ToggleGadget* setColor(const QColor& c) { color = c; update(); return this; }
inline ToggleGadget* setToolTip(const QString& t, const QPointF pos = {}) {
text = t;
if (!pos.isNull()) toolTipPosition = pos;
update();
return this;
}
};
}

View File

@ -24,8 +24,7 @@ void GadgetScene::toolTip(QGraphicsItem* g, const QString& s, const QPointF& pos
toolTipObject->setZValue(10000);
}
auto r = g->boundingRect().translated(g->scenePos());
toolTipObject->setText(s);
toolTipObject->setColor(color);
toolTipObject->setText(s)->setColor(color)->useOutline(true);
constexpr double pad = 2.0;
auto tr = toolTipObject->boundingRect();