office-gobmx/vcl/qt5/QtInstanceSpinButton.cxx
Michael Weghorn 70825e677f tdf#130857 qt weld: Notify about spinbox combined value+text change
While QtInstanceEntry generally takes care of handling signals
for the spinbox's QLineEdit, this doesn't work when the value
is changed as a result of setting a new spinbox value (e.g.
by using the spinbox buttons), as the QLineEdit signals are blocked
then, see QAbstractSpinBoxPrivate::updateEdit in qtbase [1].
Therefore, connect the QDoubleSpinBox::textChanged signal
to the slot that calls signal_changed() instead to ensure
it gets called nonetheless, and disconnect from the other signal.

While at it, also add a SolarMutexGuard when calling the
signal.

This fixes the issue noticed with the "Go to Page"
dialog mentioned in previous commit

    Change-Id: I1f24cf3925e945ae78a9f1646535e08736cd8786

    Author: Michael Weghorn <m.weghorn@posteo.de>
    Date:   Wed Nov 27 23:53:58 2024 +0100

        tdf#130857 qt weld: Support "Go to Page" dialog

as:

> One issue seen with the dialog is that by using the
> up arrow of the spinbox to increase the value, it
> is currently possible to increase the value
> beyond the maximum value (last page number),
> while this is not the case when typing a number
> into the box manually.
>
> This is because the GotoPageDlg::PageModifiedHdl
> handler currently only gets called for the
> latter case, not the former one, despite
>
>     Change-Id: Ie19bc852f4ceed0fa79565302975376db7126ea4
>     Author: Michael Weghorn <m.weghorn@posteo.de>
>     Date:   Wed Nov 27 22:53:55 2024 +0100
>
>         tdf#130857 qt weld: Also notify about programmatic text changes
>
> and will be addressed in a separate commit.

[1] https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qabstractspinbox.cpp?id=ced47a590aeb85953a16eaf362887f14c2815c45#n1790

Change-Id: Ifba9a0877442f9e84f0103d8aab202ae3583c5cf
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177451
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
2024-11-28 08:38:26 +01:00

110 lines
3.5 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QtInstanceSpinButton.hxx>
#include <QtInstanceSpinButton.moc>
#include <vcl/qt/QtUtils.hxx>
QtInstanceSpinButton::QtInstanceSpinButton(QtDoubleSpinBox* pSpinBox)
: QtInstanceEntry(pSpinBox->lineEdit())
, m_pSpinBox(pSpinBox)
{
assert(pSpinBox);
connect(m_pSpinBox, QOverload<double>::of(&QtDoubleSpinBox::valueChanged), this,
&QtInstanceSpinButton::handleValueChanged);
// While QtInstanceEntry generally takes care of handling signals
// for the spinbox's QLineEdit, this doesn't work when the value
// is changed as a result of setting a new spinbox value (e.g.
// by using the spinbox buttons), as the QLineEdit signals are blocked
// then, see QAbstractSpinBoxPrivate::updateEdit in qtbase:
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qabstractspinbox.cpp?id=ced47a590aeb85953a16eaf362887f14c2815c45#n1790
// Therefore, connect the QDoubleSpinBox::textChanged signal
// to the slot that calls signal_changed() instead to ensure
// it gets called nonetheless, and disconnect from the other signal.
disconnect(pSpinBox->lineEdit(), &QLineEdit::textChanged, this, nullptr);
connect(m_pSpinBox, &QDoubleSpinBox::textChanged, this,
&QtInstanceSpinButton::handleTextChanged);
}
QWidget* QtInstanceSpinButton::getQWidget() const { return m_pSpinBox; }
void QtInstanceSpinButton::set_value(sal_Int64 nValue)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { (m_pSpinBox->setValue(nValue)); });
}
sal_Int64 QtInstanceSpinButton::get_value() const
{
SolarMutexGuard g;
sal_Int64 nValue;
GetQtInstance().RunInMainThread([&] { nValue = std::round(m_pSpinBox->value()); });
return nValue;
}
void QtInstanceSpinButton::set_range(sal_Int64 nMin, sal_Int64 nMax)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { (m_pSpinBox->setRange(nMin, nMax)); });
}
void QtInstanceSpinButton::get_range(sal_Int64& rMin, sal_Int64& rMax) const
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
rMin = std::round(m_pSpinBox->minimum());
rMax = std::round(m_pSpinBox->maximum());
});
}
void QtInstanceSpinButton::set_increments(sal_Int64 nStep, sal_Int64)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { m_pSpinBox->setSingleStep(nStep); });
}
void QtInstanceSpinButton::get_increments(sal_Int64& rStep, sal_Int64& rPage) const
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
rStep = std::round(m_pSpinBox->singleStep());
rPage = rStep;
});
}
void QtInstanceSpinButton::set_digits(unsigned int nDigits)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] { m_pSpinBox->setDecimals(nDigits); });
}
unsigned int QtInstanceSpinButton::get_digits() const
{
SolarMutexGuard g;
unsigned int nDigits = 0;
GetQtInstance().RunInMainThread([&] { nDigits = o3tl::make_unsigned(m_pSpinBox->decimals()); });
return nDigits;
}
void QtInstanceSpinButton::handleValueChanged()
{
SolarMutexGuard aGuard;
signal_value_changed();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */