review adjuster class: reduce memory allocations/deallocations, also small speedup for adjuster creation

This commit is contained in:
heckflosse
2019-05-13 15:15:37 +02:00
parent a2e2ace1c8
commit de3d667ed7
4 changed files with 151 additions and 189 deletions

View File

@@ -27,62 +27,59 @@
#define MIN_RESET_BUTTON_HEIGHT 17
static double one2one(double val)
namespace {
double one2one(double val)
{
return val;
}
}
Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep, double vdefault, Gtk::Image *imgIcon1, Gtk::Image *imgIcon2, double2double_fun slider2value_, double2double_fun value2slider_) :
adjustmentName(std::move(vlabel)),
grid(nullptr),
label(nullptr),
imageIcon1(imgIcon1),
automatic(nullptr),
adjusterListener(nullptr),
editedCheckBox(nullptr),
afterReset(false),
blocked(false),
addMode(false),
eventPending(false),
vMin(vmin),
vMax(vmax),
vStep(vstep),
logBase(0),
logPivot(0),
logAnchorMiddle(false),
value2slider(value2slider_ ? value2slider_ : one2one),
slider2value(slider2value_ ? slider2value_ : one2one),
delay(options.adjusterMinDelay)
Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep, double vdefault, Gtk::Image *imgIcon1, Gtk::Image *imgIcon2, double2double_fun slider2value_, double2double_fun value2slider_)
{
set_hexpand(true);
set_vexpand(false);
label = nullptr;
adjusterListener = nullptr;
afterReset = false;
blocked = false;
automatic = nullptr;
eventPending = false;
grid = NULL;
imageIcon1 = imgIcon1;
logBase = 0;
logPivot = 0;
logAnchorMiddle = false;
if (imageIcon1) {
setExpandAlignProperties(imageIcon1, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
}
imageIcon2 = imgIcon2;
if (imageIcon2) {
setExpandAlignProperties(imageIcon2, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
if (imgIcon2) {
setExpandAlignProperties(imgIcon2, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
}
slider2value = slider2value_ ? slider2value_ : one2one;
value2slider = value2slider_ ? value2slider_ : one2one;
vMin = vmin;
vMax = vmax;
vStep = vstep;
addMode = false;
delay = options.adjusterMinDelay;
set_column_spacing(0);
set_column_homogeneous(false);
set_row_spacing(0);
set_row_homogeneous(false);
editedCheckBox = nullptr;
if (!vlabel.empty()) {
adjustmentName = vlabel;
if (!adjustmentName.empty()) {
label = Gtk::manage(new Gtk::Label(adjustmentName));
setExpandAlignProperties(label, true, false, Gtk::ALIGN_START, Gtk::ALIGN_BASELINE);
}
reset = Gtk::manage(new Gtk::Button());
reset->add(*Gtk::manage(new RTImage("undo-small.png", "redo-small.png")));
setExpandAlignProperties(reset, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
reset->set_relief(Gtk::RELIEF_NONE);
@@ -91,17 +88,19 @@ Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep
reset->set_can_focus(false);
spin = Gtk::manage(new MySpinButton());
setExpandAlignProperties(spin, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
spin->set_input_purpose(Gtk::INPUT_PURPOSE_DIGITS);
reset->set_size_request(-1, spin->get_height() > MIN_RESET_BUTTON_HEIGHT ? spin->get_height() : MIN_RESET_BUTTON_HEIGHT);
slider = Gtk::manage(new MyHScale());
setExpandAlignProperties(slider, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
slider->set_draw_value(false);
//slider->set_has_origin(false); // ------------------ This will remove the colored part on the left of the slider's knob
if (vlabel.empty()) {
setLimits(vmin, vmax, vstep, vdefault);
if (adjustmentName.empty()) {
// No label, everything goes in a single row
attach_next_to(*slider, Gtk::POS_LEFT, 1, 1);
@@ -109,9 +108,9 @@ Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep
attach_next_to(*imageIcon1, *slider, Gtk::POS_LEFT, 1, 1);
}
if (imageIcon2) {
attach_next_to(*imageIcon2, *slider, Gtk::POS_RIGHT, 1, 1);
attach_next_to(*spin, *imageIcon2, Gtk::POS_RIGHT, 1, 1);
if (imgIcon2) {
attach_next_to(*imgIcon2, *slider, Gtk::POS_RIGHT, 1, 1);
attach_next_to(*spin, *imgIcon2, Gtk::POS_RIGHT, 1, 1);
} else {
attach_next_to(*spin, *slider, Gtk::POS_RIGHT, 1, 1);
}
@@ -129,9 +128,9 @@ Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep
grid->attach_next_to(*imageIcon1, *slider, Gtk::POS_LEFT, 1, 1);
}
if (imageIcon2) {
grid->attach_next_to(*imageIcon2, Gtk::POS_RIGHT, 1, 1);
grid->attach_next_to(*reset, *imageIcon2, Gtk::POS_RIGHT, 1, 1);
if (imgIcon2) {
grid->attach_next_to(*imgIcon2, Gtk::POS_RIGHT, 1, 1);
grid->attach_next_to(*reset, *imgIcon2, Gtk::POS_RIGHT, 1, 1);
} else {
grid->attach_next_to(*reset, *slider, Gtk::POS_RIGHT, 1, 1);
}
@@ -139,12 +138,8 @@ Adjuster::Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep
attach_next_to(*grid, *label, Gtk::POS_BOTTOM, 2, 1);
}
setLimits (vmin, vmax, vstep, vdefault);
defaultVal = shapeValue (vdefault);
ctorDefaultVal = shapeValue (vdefault);
defaultVal = ctorDefaultVal = shapeValue(vdefault);
editedState = defEditedState = Irrelevant;
autoState = Irrelevant;
sliderChange = slider->signal_value_changed().connect( sigc::mem_fun(*this, &Adjuster::sliderChanged) );
spinChange = spin->signal_value_changed().connect( sigc::mem_fun(*this, &Adjuster::spinChanged), true);
@@ -161,10 +156,8 @@ Adjuster::~Adjuster ()
delayConnection.block(true);
adjusterListener = nullptr;
if (automatic) {
delete automatic;
}
}
void Adjuster::addAutoButton (Glib::ustring tooltip)
{
@@ -185,15 +178,6 @@ void Adjuster::addAutoButton (Glib::ustring tooltip)
}
}
void Adjuster::delAutoButton ()
{
if (automatic) {
removeIfThere(grid, automatic);
delete automatic;
automatic = nullptr;
}
}
void Adjuster::throwOnButtonRelease(bool throwOnBRelease)
{
@@ -288,7 +272,6 @@ void Adjuster::resetValue (bool toInitial)
editedChange.block(false);
}
refreshLabelStyle ();
}
afterReset = true;
@@ -317,30 +300,33 @@ void Adjuster::resetPressed (GdkEventButton* event)
}
}
double Adjuster::shapeValue (double a)
double Adjuster::shapeValue (double a) const
{
double val = round(a * pow(double(10), digits)) / pow(double(10), digits);
const double pow10 = pow(10.0, digits);
const double val = round(a * pow10) / pow10;
return val == -0.0 ? 0.0 : val;
}
void Adjuster::setLimits (double vmin, double vmax, double vstep, double vdefault)
{
sliderChange.block(true);
spinChange.block(true);
for (digits = 0; fabs(vstep * pow(double(10), digits) - floor(vstep * pow(double(10), digits))) > 0.000000000001; digits++);
double pow10 = vstep;
for (digits = 0; fabs(pow10 - floor(pow10)) > 0.000000000001; digits++, pow10 *= 10.0);
const double shapeVal = shapeValue(vdefault);
spin->set_digits(digits);
spin->set_increments(vstep, 2.0 * vstep);
spin->set_range(vmin, vmax);
spin->updateSize();
spin->set_value (shapeValue(vdefault));
spin->set_value(shapeVal);
slider->set_digits(digits);
slider->set_increments(vstep, 2.0 * vstep);
slider->set_range(addMode ? vmin : value2slider(vmin), addMode ? vmax : value2slider(vmax));
setSliderValue(addMode ? shapeValue(vdefault) : value2slider(shapeValue(vdefault)));
//defaultVal = shapeValue (vdefault);
setSliderValue(addMode ? shapeVal : value2slider(shapeVal));
sliderChange.block(false);
spinChange.block(false);
}
@@ -369,7 +355,6 @@ void Adjuster::setAddMode(bool addM)
void Adjuster::spinChanged ()
{
if (delayConnection.connected()) {
delayConnection.disconnect();
}
@@ -400,8 +385,6 @@ void Adjuster::spinChanged ()
editedCheckBox->set_active(true);
editedChange.block(false);
}
refreshLabelStyle ();
}
afterReset = false;
@@ -415,7 +398,7 @@ void Adjuster::sliderChanged ()
}
spinChange.block(true);
double v = shapeValue(getSliderValue());
const double v = shapeValue(getSliderValue());
spin->set_value(addMode ? v : slider2value(v));
spinChange.block(false);
@@ -441,8 +424,6 @@ void Adjuster::sliderChanged ()
editedCheckBox->set_active(true);
editedChange.block(false);
}
refreshLabelStyle ();
}
afterReset = false;
@@ -450,7 +431,6 @@ void Adjuster::sliderChanged ()
void Adjuster::setValue (double a)
{
spinChange.block(true);
sliderChange.block(true);
spin->set_value(shapeValue(a));
@@ -463,7 +443,7 @@ void Adjuster::setValue (double a)
void Adjuster::setAutoValue (bool a)
{
if (automatic) {
bool oldVal = autoChange.block(true);
const bool oldVal = autoChange.block(true);
automatic->set_active(a);
autoChange.block(oldVal);
@@ -509,7 +489,7 @@ bool Adjuster::notifyListenerAutoToggled ()
void Adjuster::setEnabled (bool enabled)
{
bool autoVal = automatic && !editedCheckBox ? automatic->get_active() : true;
const bool autoVal = automatic && !editedCheckBox ? automatic->get_active() : true;
spin->set_sensitive(enabled && autoVal);
slider->set_sensitive(enabled && autoVal);
@@ -529,7 +509,6 @@ void Adjuster::setEditedState (EditedState eState)
}
editedState = eState;
refreshLabelStyle ();
}
}
@@ -576,17 +555,6 @@ void Adjuster::showEditedCB ()
}
}
void Adjuster::refreshLabelStyle ()
{
/* Glib::RefPtr<Gtk::StyleContext> style = label->get_style_context ();
Pango::FontDescription fd = style->get_font ();
fd.set_weight (editedState==Edited ? Pango::WEIGHT_BOLD : Pango::WEIGHT_NORMAL);
style->set_font (fd);
label->set_style (style);
label->queue_draw ();*/
}
void Adjuster::editedToggled ()
{
@@ -597,29 +565,28 @@ void Adjuster::editedToggled ()
eventPending = false;
}
void Adjuster::trimValue (double &val)
void Adjuster::trimValue (double &val) const
{
val = rtengine::LIM(val, vMin, vMax);
}
void Adjuster::trimValue (int &val)
void Adjuster::trimValue (int &val) const
{
val = rtengine::LIM(val, static_cast<int>(vMin), static_cast<int>(vMax));
val = rtengine::LIM<int>(val, vMin, vMax);
}
void Adjuster::trimValue (float &val)
void Adjuster::trimValue (float &val) const
{
val = rtengine::LIM(val, static_cast<float>(vMin), static_cast<float>(vMax));
val = rtengine::LIM<float>(val, vMin, vMax);
}
inline double Adjuster::getSliderValue()
inline double Adjuster::getSliderValue() const
{
double val = slider->get_value();
if (logBase) {
@@ -686,7 +653,7 @@ void Adjuster::setLogScale(double base, double pivot, bool anchorMiddle)
spinChange.block(true);
sliderChange.block(true);
double cur = getSliderValue();
const double cur = getSliderValue();
logBase = base;
logPivot = pivot;
logAnchorMiddle = anchorMiddle;

View File

@@ -43,7 +43,6 @@ protected:
Gtk::Grid* grid;
Gtk::Label* label;
Gtk::Image *imageIcon1;
Gtk::Image *imageIcon2;
MyHScale* slider;
MySpinButton* spin;
Gtk::Button* reset;
@@ -60,7 +59,6 @@ protected:
double ctorDefaultVal; // default value at construction time
EditedState editedState;
EditedState defEditedState;
EditedState autoState;
int digits;
Gtk::CheckButton* editedCheckBox;
bool afterReset;
@@ -75,28 +73,25 @@ protected:
double logPivot;
bool logAnchorMiddle;
double shapeValue (double a);
void refreshLabelStyle ();
double shapeValue (double a) const;
double2double_fun value2slider, slider2value;
double getSliderValue();
double getSliderValue() const;
void setSliderValue(double val);
public:
int delay;
Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep, double vdefault, Gtk::Image *imgIcon1 = nullptr, Gtk::Image *imgIcon2 = nullptr, double2double_fun slider2value = nullptr, double2double_fun value2slider = nullptr);
Adjuster (Glib::ustring vlabel, double vmin, double vmax, double vstep, double vdefault, Gtk::Image *imgIcon1 = nullptr, Gtk::Image *imgIcon2 = nullptr, double2double_fun slider2value_ = nullptr, double2double_fun value2slider_ = nullptr);
~Adjuster () override;
// Add an "Automatic" checkbox next to the reset button.
void addAutoButton(Glib::ustring tooltip = "");
// Remove the "Automatic" checkbox next to the reset button.
void delAutoButton();
// Send back the value of og the Auto checkbox
bool getAutoValue ()
{
return automatic != nullptr ? automatic->get_active () : false;
return automatic ? automatic->get_active() : false;
}
void setAutoValue (bool a);
bool notifyListenerAutoToggled ();
@@ -107,7 +102,7 @@ public:
automatic->set_inconsistent(i);
}
}
bool getAutoInconsistent ()
bool getAutoInconsistent () const
{
return automatic ? automatic->get_inconsistent() : true /* we have to return something */;
}
@@ -118,18 +113,18 @@ public:
}
// return the value trimmed to the limits at construction time
double getValue ()
double getValue () const
{
return shapeValue(spin->get_value());
}
// return the value trimmed to the limits at construction time
int getIntValue ()
int getIntValue () const
{
return spin->get_value_as_int();
}
// return the value trimmed to the limits at construction time,
// method only used by the history manager, so decoration is added if addMode=true
Glib::ustring getTextValue ()
Glib::ustring getTextValue () const
{
if (addMode) {
return Glib::ustring::compose("<i>%1</i>", spin->get_text());
@@ -138,7 +133,7 @@ public:
}
}
void setLabel (Glib::ustring lbl)
void setLabel (const Glib::ustring &lbl)
{
label->set_label(lbl);
}
@@ -165,7 +160,7 @@ public:
}
void setAddMode(bool addM);
bool getAddMode()
bool getAddMode() const
{
return addMode;
};
@@ -177,9 +172,9 @@ public:
void resetValue (bool toInitial);
void resetPressed (GdkEventButton* event);
void editedToggled ();
void trimValue (double &val);
void trimValue (float &val);
void trimValue (int &val);
void trimValue (double &val) const;
void trimValue (float &val) const;
void trimValue (int &val) const;
void setLogScale(double base, double pivot, bool anchorMiddle = false);
};

View File

@@ -99,7 +99,7 @@ DirPyrEqualizer::DirPyrEqualizer () : FoldableToolPanel(this, "dirpyrequalizer",
ss += Glib::ustring::compose(" (%1)", M("TP_DIRPYREQUALIZER_LUMACOARSEST"));
}
multiplier[i] = Gtk::manage ( new Adjuster (ss, 0, 4, 0.01, 1.0) );
multiplier[i] = Gtk::manage(new Adjuster(std::move(ss), 0, 4, 0.01, 1.0));
multiplier[i]->setAdjusterListener(this);
pack_start(*multiplier[i]);
}

View File

@@ -290,7 +290,7 @@ Wavelet::Wavelet() :
ss = Glib::ustring::compose( "%1", (i + 1));
}
correction[i] = Gtk::manage ( new Adjuster (ss, -100, 350, 1, 0) );
correction[i] = Gtk::manage(new Adjuster(std::move(ss), -100, 350, 1, 0));
correction[i]->setAdjusterListener(this);
levBox->pack_start(*correction[i]);
}
@@ -398,7 +398,7 @@ Wavelet::Wavelet() :
ss = Glib::ustring::compose( "%1", (i + 1));
}
correctionch[i] = Gtk::manage ( new Adjuster (ss, -100, 100, 1, 0) );
correctionch[i] = Gtk::manage(new Adjuster(std::move(ss), -100, 100, 1, 0));
correctionch[i]->setAdjusterListener(this);
chBox->pack_start(*correctionch[i]);
}