office-gobmx/vcl/qt5/QtInstanceButton.cxx
Michael Weghorn fac6d5da61 tdf#130857 qt weld: Prefer custom handler for dialog buttons
If a custom handler is set for a button in a dialog's
button box, skip the default handling (to close the
dialog with the corresponding response code, unless
it's the help button) in QtInstanceDialog::handleButtonClick,
so that the custom handler gets processed instead
(see QtInstanceButton::buttonClicked).

This is similar to what the VCL implementation
(see SalInstanceButton's ClickHdl) and the
gtk3 implementation (see
GtkInstanceDialog::asyncresponse) do.

In order to be able to determine whether
a custom click handler is set, make
weld::Button::connect_clicked virtual
and override it in QtInstanceButton::connect_clicked
to set a property on the QPushButton object
to indicate that a handler is set.
Introduce a static helper method
QtInstanceButton::hasCustomClickHandler
that evaluates this property.

Without this change in place, pressing the
"Rename" button in Writer's "Rename Object"
dialog that will be declared as supported
in upcoming commit

    Change-Id: I0ac8f66b977f5d999bb197cc68e8ce5533fe1ebd
    Author: Michael Weghorn <m.weghorn@posteo.de>
    Date:   Fri Oct 25 10:41:45 2024 +0200

        tdf#130857 qt weld: Declare support for sw "Rename Object" dialog

would not actually result in a bookmark getting
renamed, only the dialog would be closed.

Change-Id: If3e762100349ecd06c4e9415e9a8f9a44ec6491c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/175633
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
2024-10-25 18:44:49 +02:00

104 lines
2.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 <QtInstanceButton.hxx>
#include <vcl/qt/QtUtils.hxx>
// Name of QObject property to indicate whether a click handler
// was set on the QPushButton: If that property is set and has a value
// of true, then a custom click handler is set, otherwise not.
const char* const PROPERTY_CLICK_HANDLER_SET = "click-handler-set";
QtInstanceButton::QtInstanceButton(QPushButton* pButton)
: QtInstanceWidget(pButton)
, m_pButton(pButton)
{
assert(m_pButton);
connect(m_pButton, &QPushButton::clicked, this, &QtInstanceButton::buttonClicked);
}
void QtInstanceButton::set_label(const OUString& rText)
{
SolarMutexGuard g;
QtInstance& rQtInstance = GetQtInstance();
if (!rQtInstance.IsMainThread())
{
rQtInstance.RunInMainThread([&] { set_label(rText); });
return;
}
assert(m_pButton);
m_pButton->setText(toQString(rText));
}
void QtInstanceButton::set_image(VirtualDevice* /*pDevice*/)
{
assert(false && "Not implemented yet");
}
void QtInstanceButton::set_image(const css::uno::Reference<css::graphic::XGraphic>& /*rImage*/)
{
assert(false && "Not implemented yet");
}
void QtInstanceButton::set_from_icon_name(const OUString& rIconName)
{
SolarMutexGuard g;
GetQtInstance().RunInMainThread([&] {
QPixmap aIcon = loadQPixmapIcon(rIconName);
m_pButton->setIcon(aIcon);
});
}
OUString QtInstanceButton::get_label() const
{
SolarMutexGuard g;
QtInstance& rQtInstance = GetQtInstance();
if (!rQtInstance.IsMainThread())
{
OUString sLabel;
rQtInstance.RunInMainThread([&] { sLabel = get_label(); });
return sLabel;
}
assert(m_pButton);
return toOUString(m_pButton->text());
}
void QtInstanceButton::set_font(const vcl::Font& /*rFont*/)
{
assert(false && "Not implemented yet");
}
void QtInstanceButton::set_custom_button(VirtualDevice* /*pDevice*/)
{
assert(false && "Not implemented yet");
}
void QtInstanceButton::connect_clicked(const Link<Button&, void>& rLink)
{
weld::Button::connect_clicked(rLink);
m_pButton->setProperty(PROPERTY_CLICK_HANDLER_SET, QVariant::fromValue(rLink.IsSet()));
}
bool QtInstanceButton::hasCustomClickHandler(QAbstractButton& rButton)
{
QVariant aProp = rButton.property(PROPERTY_CLICK_HANDLER_SET);
if (!aProp.isValid())
return false;
assert(aProp.canConvert<bool>());
return aProp.toBool();
}
void QtInstanceButton::buttonClicked() { signal_clicked(); }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */