1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <QtInstanceWidget.hxx>
11 #include <QtInstanceWidget.moc>
13 #include <QtInstanceContainer.hxx>
15 #include <vcl/transfer.hxx>
16 #include <vcl/qt/QtUtils.hxx>
18 /** Name of QObject property used for the help ID. */
19 const char* const PROPERTY_HELP_ID
= "help-id";
21 QtInstanceWidget::QtInstanceWidget(QWidget
* pWidget
)
26 connect(qApp
, &QApplication::focusChanged
, this, &QtInstanceWidget::applicationFocusChanged
);
29 void QtInstanceWidget::set_sensitive(bool bSensitive
)
32 QtInstance
& rQtInstance
= GetQtInstance();
33 if (!rQtInstance
.IsMainThread())
35 rQtInstance
.RunInMainThread([&] { set_sensitive(bSensitive
); });
40 m_pWidget
->setEnabled(bSensitive
);
43 bool QtInstanceWidget::get_sensitive() const
46 QtInstance
& rQtInstance
= GetQtInstance();
47 if (!rQtInstance
.IsMainThread())
49 bool bSensitive
= false;
50 rQtInstance
.RunInMainThread([&] { bSensitive
= get_sensitive(); });
55 return m_pWidget
->isEnabled();
58 bool QtInstanceWidget::get_visible() const
61 QtInstance
& rQtInstance
= GetQtInstance();
62 if (!rQtInstance
.IsMainThread())
64 bool bVisible
= false;
65 rQtInstance
.RunInMainThread([&] { bVisible
= get_visible(); });
70 return m_pWidget
->isVisible();
73 bool QtInstanceWidget::is_visible() const
76 QtInstance
& rQtInstance
= GetQtInstance();
77 if (!rQtInstance
.IsMainThread())
79 bool bVisible
= false;
80 rQtInstance
.RunInMainThread([&] { bVisible
= is_visible(); });
86 QWidget
* pTopLevel
= m_pWidget
->topLevelWidget();
88 return m_pWidget
->isVisibleTo(pTopLevel
) && pTopLevel
->isVisible();
91 void QtInstanceWidget::set_can_focus(bool bCanFocus
)
94 QtInstance
& rQtInstance
= GetQtInstance();
95 if (!rQtInstance
.IsMainThread())
97 rQtInstance
.RunInMainThread([&] { set_can_focus(bCanFocus
); });
103 m_pWidget
->setFocusPolicy(Qt::FocusPolicy::StrongFocus
);
105 m_pWidget
->setFocusPolicy(Qt::FocusPolicy::NoFocus
);
108 void QtInstanceWidget::grab_focus()
111 QtInstance
& rQtInstance
= GetQtInstance();
112 if (!rQtInstance
.IsMainThread())
114 rQtInstance
.RunInMainThread([&] { grab_focus(); });
119 m_pWidget
->setFocus();
122 bool QtInstanceWidget::has_focus() const
125 QtInstance
& rQtInstance
= GetQtInstance();
126 if (!rQtInstance
.IsMainThread())
129 rQtInstance
.RunInMainThread([&] { bFocus
= has_focus(); });
134 return m_pWidget
->hasFocus();
137 bool QtInstanceWidget::is_active() const { return has_focus(); }
139 bool QtInstanceWidget::has_child_focus() const
142 QtInstance
& rQtInstance
= GetQtInstance();
143 if (!rQtInstance
.IsMainThread())
145 bool bChildFocus
= false;
146 rQtInstance
.RunInMainThread([&] { bChildFocus
= has_child_focus(); });
150 QWidget
* pFocusWidget
= QApplication::focusWidget();
154 QWidget
* pParent
= pFocusWidget
->parentWidget();
157 if (pParent
== m_pWidget
)
159 pParent
= pParent
->parentWidget();
164 void QtInstanceWidget::show()
167 QtInstance
& rQtInstance
= GetQtInstance();
168 if (!rQtInstance
.IsMainThread())
170 rQtInstance
.RunInMainThread([&] { show(); });
178 void QtInstanceWidget::hide()
181 QtInstance
& rQtInstance
= GetQtInstance();
182 if (!rQtInstance
.IsMainThread())
184 rQtInstance
.RunInMainThread([&] { hide(); });
192 void QtInstanceWidget::set_size_request(int nWidth
, int nHeight
)
195 GetQtInstance().RunInMainThread(
196 [&] { m_pWidget
->setMinimumSize(std::max(0, nWidth
), std::max(0, nHeight
)); });
199 Size
QtInstanceWidget::get_size_request() const
204 GetQtInstance().RunInMainThread([&] { aSize
= toSize(m_pWidget
->minimumSize()); });
208 Size
QtInstanceWidget::get_preferred_size() const
213 GetQtInstance().RunInMainThread([&] { aPreferredSize
= toSize(m_pWidget
->sizeHint()); });
215 return aPreferredSize
;
218 float QtInstanceWidget::get_approximate_digit_width() const
223 GetQtInstance().RunInMainThread(
224 [&] { fWidth
= m_pWidget
->fontMetrics().horizontalAdvance("0123456789") / 10.0; });
228 int QtInstanceWidget::get_text_height() const
233 GetQtInstance().RunInMainThread([&] { nHeight
= m_pWidget
->fontMetrics().height(); });
237 Size
QtInstanceWidget::get_pixel_size(const OUString
& rText
) const
242 GetQtInstance().RunInMainThread(
243 [&] { aSize
= toSize(m_pWidget
->fontMetrics().boundingRect(toQString(rText
)).size()); });
248 vcl::Font
QtInstanceWidget::get_font()
250 assert(false && "Not implemented yet");
254 OUString
QtInstanceWidget::get_buildable_name() const { return OUString(); }
256 void QtInstanceWidget::set_buildable_name(const OUString
&) {}
258 void QtInstanceWidget::setHelpId(QWidget
& rWidget
, const OUString
& rHelpId
)
261 GetQtInstance().RunInMainThread(
262 [&] { rWidget
.setProperty(PROPERTY_HELP_ID
, toQString(rHelpId
)); });
265 void QtInstanceWidget::set_help_id(const OUString
& rHelpId
) { setHelpId(*m_pWidget
, rHelpId
); }
267 OUString
QtInstanceWidget::get_help_id() const
270 QtInstance
& rQtInstance
= GetQtInstance();
271 if (!rQtInstance
.IsMainThread())
274 rQtInstance
.RunInMainThread([&] { sHelpId
= get_help_id(); });
278 const QVariant aHelpIdVariant
= m_pWidget
->property(PROPERTY_HELP_ID
);
279 if (!aHelpIdVariant
.isValid())
282 assert(aHelpIdVariant
.canConvert
<QString
>());
283 return toOUString(aHelpIdVariant
.toString());
286 void QtInstanceWidget::set_hexpand(bool) { assert(false && "Not implemented yet"); }
288 bool QtInstanceWidget::get_hexpand() const
290 assert(false && "Not implemented yet");
294 void QtInstanceWidget::set_vexpand(bool) { assert(false && "Not implemented yet"); }
296 bool QtInstanceWidget::get_vexpand() const
298 assert(false && "Not implemented yet");
302 void QtInstanceWidget::set_margin_top(int) { assert(false && "Not implemented yet"); }
304 void QtInstanceWidget::set_margin_bottom(int) { assert(false && "Not implemented yet"); }
306 void QtInstanceWidget::set_margin_start(int) { assert(false && "Not implemented yet"); }
308 void QtInstanceWidget::set_margin_end(int) { assert(false && "Not implemented yet"); }
310 int QtInstanceWidget::get_margin_top() const
312 assert(false && "Not implemented yet");
316 int QtInstanceWidget::get_margin_bottom() const
318 assert(false && "Not implemented yet");
322 int QtInstanceWidget::get_margin_start() const
324 assert(false && "Not implemented yet");
328 int QtInstanceWidget::get_margin_end() const
330 assert(false && "Not implemented yet");
334 void QtInstanceWidget::set_accessible_name(const OUString
& rName
)
337 QtInstance
& rQtInstance
= GetQtInstance();
338 if (!rQtInstance
.IsMainThread())
340 rQtInstance
.RunInMainThread([&] { set_accessible_name(rName
); });
345 m_pWidget
->setAccessibleName(toQString(rName
));
348 void QtInstanceWidget::set_accessible_description(const OUString
& rDescription
)
351 QtInstance
& rQtInstance
= GetQtInstance();
352 if (!rQtInstance
.IsMainThread())
354 rQtInstance
.RunInMainThread([&] { set_accessible_description(rDescription
); });
359 m_pWidget
->setAccessibleDescription(toQString(rDescription
));
362 OUString
QtInstanceWidget::get_accessible_name() const
365 QtInstance
& rQtInstance
= GetQtInstance();
366 if (!rQtInstance
.IsMainThread())
369 rQtInstance
.RunInMainThread([&] { sName
= get_accessible_name(); });
374 return toOUString(m_pWidget
->accessibleName());
377 OUString
QtInstanceWidget::get_accessible_description() const
380 QtInstance
& rQtInstance
= GetQtInstance();
381 if (!rQtInstance
.IsMainThread())
383 OUString sDescription
;
384 rQtInstance
.RunInMainThread([&] { sDescription
= get_accessible_description(); });
389 return toOUString(m_pWidget
->accessibleDescription());
392 OUString
QtInstanceWidget::get_accessible_id() const
395 QtInstance
& rQtInstance
= GetQtInstance();
396 if (!rQtInstance
.IsMainThread())
399 rQtInstance
.RunInMainThread([&] { sId
= get_accessible_id(); });
404 #if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
405 return toOUString(m_pWidget
->accessibleIdentifier());
411 void QtInstanceWidget::set_accessible_relation_labeled_by(weld::Widget
*)
413 assert(false && "Not implemented yet");
416 void QtInstanceWidget::set_tooltip_text(const OUString
& rTip
)
419 QtInstance
& rQtInstance
= GetQtInstance();
420 if (!rQtInstance
.IsMainThread())
422 rQtInstance
.RunInMainThread([&] { set_tooltip_text(rTip
); });
427 m_pWidget
->setToolTip(toQString(rTip
));
430 OUString
QtInstanceWidget::get_tooltip_text() const
433 QtInstance
& rQtInstance
= GetQtInstance();
434 if (!rQtInstance
.IsMainThread())
436 OUString sToolTipText
;
437 rQtInstance
.RunInMainThread([&] { sToolTipText
= get_tooltip_text(); });
442 return toOUString(m_pWidget
->toolTip());
445 void QtInstanceWidget::set_cursor_data(void*) { assert(false && "Not implemented yet"); }
447 void QtInstanceWidget::grab_add() { assert(false && "Not implemented yet"); }
449 bool QtInstanceWidget::has_grab() const
451 assert(false && "Not implemented yet");
455 void QtInstanceWidget::grab_remove() { assert(false && "Not implemented yet"); }
457 bool QtInstanceWidget::get_extents_relative_to(const Widget
& rRelative
, int& rX
, int& rY
,
458 int& rWidth
, int& rHeight
) const
463 GetQtInstance().RunInMainThread([&] {
464 QRect aGeometry
= m_pWidget
->geometry();
465 rWidth
= aGeometry
.width();
466 rHeight
= aGeometry
.height();
467 const QtInstanceWidget
* pRelativeWidget
= dynamic_cast<const QtInstanceWidget
*>(&rRelative
);
468 if (!pRelativeWidget
)
471 QPoint aRelativePos
= m_pWidget
->mapTo(pRelativeWidget
->getQWidget(), QPoint(0, 0));
472 rX
= aRelativePos
.x();
473 rY
= aRelativePos
.y();
480 bool QtInstanceWidget::get_direction() const
485 GetQtInstance().RunInMainThread(
486 [&] { bRTL
= m_pWidget
->layoutDirection() == Qt::LayoutDirection::RightToLeft
; });
490 void QtInstanceWidget::set_direction(bool bRTL
)
494 GetQtInstance().RunInMainThread([&] {
495 m_pWidget
->setLayoutDirection(bRTL
? Qt::LayoutDirection::RightToLeft
496 : Qt::LayoutDirection::LeftToRight
);
500 void QtInstanceWidget::freeze(){};
502 void QtInstanceWidget::thaw(){};
504 void QtInstanceWidget::set_busy_cursor(bool bBusy
)
508 GetQtInstance().RunInMainThread([&] {
513 assert(m_nBusyCount
>= 0);
515 if (m_nBusyCount
== 1)
516 m_pWidget
->setCursor(Qt::BusyCursor
);
517 else if (m_nBusyCount
== 0)
518 m_pWidget
->unsetCursor();
522 std::unique_ptr
<weld::Container
> QtInstanceWidget::weld_parent() const
524 QWidget
* pParentWidget
= m_pWidget
->parentWidget();
528 return std::make_unique
<QtInstanceContainer
>(pParentWidget
);
531 void QtInstanceWidget::queue_resize()
535 GetQtInstance().RunInMainThread([&] { m_pWidget
->adjustSize(); });
538 void QtInstanceWidget::help_hierarchy_foreach(const std::function
<bool(const OUString
&)>&)
540 assert(false && "Not implemented yet");
543 OUString
QtInstanceWidget::strip_mnemonic(const OUString
& rLabel
) const
545 return rLabel
.replaceFirst("&", "");
548 OUString
QtInstanceWidget::escape_ui_str(const OUString
& rLabel
) const
550 // preserve literal '&'
551 return rLabel
.replaceAll("&", "&&");
554 VclPtr
<VirtualDevice
> QtInstanceWidget::create_virtual_device() const
556 VclPtr
<VirtualDevice
> xRet
= VclPtr
<VirtualDevice
>::Create();
557 xRet
->SetBackground(COL_TRANSPARENT
);
561 css::uno::Reference
<css::datatransfer::dnd::XDropTarget
> QtInstanceWidget::get_drop_target()
563 assert(false && "Not implemented yet");
567 css::uno::Reference
<css::datatransfer::clipboard::XClipboard
>
568 QtInstanceWidget::get_clipboard() const
570 return GetSystemClipboard();
573 void QtInstanceWidget::connect_get_property_tree(const Link
<tools::JsonWriter
&, void>&)
575 // not implemented for the Qt variant
578 void QtInstanceWidget::get_property_tree(tools::JsonWriter
&)
580 // not implemented for the Qt variant
583 void QtInstanceWidget::call_attention_to() { assert(false && "Not implemented yet"); }
585 void QtInstanceWidget::set_stack_background() { assert(false && "Not implemented yet"); }
587 void QtInstanceWidget::set_title_background() { assert(false && "Not implemented yet"); }
589 void QtInstanceWidget::set_toolbar_background() { assert(false && "Not implemented yet"); }
591 void QtInstanceWidget::set_highlight_background() { assert(false && "Not implemented yet"); }
593 void QtInstanceWidget::set_background(const Color
& rBackColor
)
595 QPalette aPalette
= getQWidget()->palette();
596 aPalette
.setColor(QPalette::Base
, toQColor(rBackColor
));
597 getQWidget()->setPalette(aPalette
);
598 getQWidget()->setBackgroundRole(QPalette::Base
);
601 void QtInstanceWidget::draw(OutputDevice
&, const Point
&, const Size
&)
603 assert(false && "Not implemented yet");
606 void QtInstanceWidget::applicationFocusChanged(QWidget
* pOldFocus
, QWidget
* pNewFocus
)
610 if (pOldFocus
== m_pWidget
)
612 else if (pNewFocus
== m_pWidget
)
616 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */