/* -*- 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 #include #include /** * Name of the property to set on a QPushButton that holds the * VCL response code of that button. */ const char* const PROPERTY_VCL_RESPONSE_CODE = "response-code"; QtInstanceMessageDialog::QtInstanceMessageDialog(QMessageBox* pMessageDialog) : QtInstanceDialog(pMessageDialog) , m_pMessageDialog(pMessageDialog) , m_aRunAsyncFunc(nullptr) { assert(m_pMessageDialog); } void QtInstanceMessageDialog::set_primary_text(const rtl::OUString& rText) { m_pMessageDialog->setText(toQString(rText)); } void QtInstanceMessageDialog::set_secondary_text(const rtl::OUString& rText) { m_pMessageDialog->setInformativeText(toQString(rText)); } weld::Container* QtInstanceMessageDialog::weld_message_area() { return nullptr; } OUString QtInstanceMessageDialog::get_primary_text() const { assert(m_pMessageDialog); return toOUString(m_pMessageDialog->text()); } OUString QtInstanceMessageDialog::get_secondary_text() const { assert(m_pMessageDialog); return toOUString(m_pMessageDialog->informativeText()); } void QtInstanceMessageDialog::add_button(const OUString& rText, int nResponse, const OUString&) { assert(m_pMessageDialog); QPushButton* pButton = m_pMessageDialog->addButton(vclToQtStringWithAccelerator(rText), QMessageBox::ButtonRole::ActionRole); pButton->setProperty(PROPERTY_VCL_RESPONSE_CODE, QVariant::fromValue(nResponse)); } void QtInstanceMessageDialog::set_default_response(int nResponse) { assert(m_pMessageDialog); const QList aButtons = m_pMessageDialog->buttons(); for (QAbstractButton* pAbstractButton : aButtons) { if (pAbstractButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt() == nResponse) { QPushButton* pButton = dynamic_cast(pAbstractButton); assert(pButton); m_pMessageDialog->setDefaultButton(pButton); return; } } } int QtInstanceMessageDialog::run() { m_pMessageDialog->exec(); QAbstractButton* pClickedButton = m_pMessageDialog->clickedButton(); if (!pClickedButton) return RET_CLOSE; return pClickedButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt(); } bool QtInstanceMessageDialog::runAsync(const std::shared_ptr& rxOwner, const std::function& func) { assert(m_pMessageDialog); m_xRunAsyncDialogController = rxOwner; m_aRunAsyncFunc = func; connect(m_pMessageDialog, &QDialog::finished, this, &QtInstanceMessageDialog::dialogFinished); m_pMessageDialog->open(); return true; } bool QtInstanceMessageDialog::runAsync(std::shared_ptr const& rxSelf, const std::function& func) { assert(m_pMessageDialog); assert(rxSelf.get() == this); m_xRunAsyncDialog = rxSelf; m_aRunAsyncFunc = func; connect(m_pMessageDialog, &QDialog::finished, this, &QtInstanceMessageDialog::dialogFinished); m_pMessageDialog->open(); return true; } void QtInstanceMessageDialog::response(int nResponse) { assert(m_pMessageDialog); m_pMessageDialog->done(nResponse); } void QtInstanceMessageDialog::dialogFinished(int nResult) { assert(m_aRunAsyncFunc); disconnect(m_pMessageDialog, &QDialog::finished, this, &QtInstanceMessageDialog::dialogFinished); // use local variables for these, as members might have got de-allocated by the time they're reset std::shared_ptr xRunAsyncDialog = m_xRunAsyncDialog; std::shared_ptr xRunAsyncDialogController = m_xRunAsyncDialogController; std::function aFunc = m_aRunAsyncFunc; m_aRunAsyncFunc = nullptr; m_xRunAsyncDialogController.reset(); m_xRunAsyncDialog.reset(); // if a button was clicked, use its response code, otherwise the passed one int nRet = nResult; if (QAbstractButton* pClickedButton = m_pMessageDialog->clickedButton()) nRet = pClickedButton->property(PROPERTY_VCL_RESPONSE_CODE).toInt(); SolarMutexGuard g; aFunc(nRet); xRunAsyncDialogController.reset(); xRunAsyncDialog.reset(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */