tdf#143148 Use pragma once instead of include guards
[LibreOffice.git] / vcl / qt5 / QtInstanceWidget.cxx
blob3c016ef919020e710ce01e8f572cafe1b30a9f24
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
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)
22 : m_pWidget(pWidget)
24 assert(pWidget);
26 connect(qApp, &QApplication::focusChanged, this, &QtInstanceWidget::applicationFocusChanged);
29 void QtInstanceWidget::set_sensitive(bool bSensitive)
31 SolarMutexGuard g;
32 QtInstance& rQtInstance = GetQtInstance();
33 if (!rQtInstance.IsMainThread())
35 rQtInstance.RunInMainThread([&] { set_sensitive(bSensitive); });
36 return;
39 assert(m_pWidget);
40 m_pWidget->setEnabled(bSensitive);
43 bool QtInstanceWidget::get_sensitive() const
45 SolarMutexGuard g;
46 QtInstance& rQtInstance = GetQtInstance();
47 if (!rQtInstance.IsMainThread())
49 bool bSensitive = false;
50 rQtInstance.RunInMainThread([&] { bSensitive = get_sensitive(); });
51 return bSensitive;
54 assert(m_pWidget);
55 return m_pWidget->isEnabled();
58 bool QtInstanceWidget::get_visible() const
60 SolarMutexGuard g;
61 QtInstance& rQtInstance = GetQtInstance();
62 if (!rQtInstance.IsMainThread())
64 bool bVisible = false;
65 rQtInstance.RunInMainThread([&] { bVisible = get_visible(); });
66 return bVisible;
69 assert(m_pWidget);
70 return m_pWidget->isVisible();
73 bool QtInstanceWidget::is_visible() const
75 SolarMutexGuard g;
76 QtInstance& rQtInstance = GetQtInstance();
77 if (!rQtInstance.IsMainThread())
79 bool bVisible = false;
80 rQtInstance.RunInMainThread([&] { bVisible = is_visible(); });
81 return bVisible;
84 assert(m_pWidget);
86 QWidget* pTopLevel = m_pWidget->topLevelWidget();
87 assert(pTopLevel);
88 return m_pWidget->isVisibleTo(pTopLevel) && pTopLevel->isVisible();
91 void QtInstanceWidget::set_can_focus(bool bCanFocus)
93 SolarMutexGuard g;
94 QtInstance& rQtInstance = GetQtInstance();
95 if (!rQtInstance.IsMainThread())
97 rQtInstance.RunInMainThread([&] { set_can_focus(bCanFocus); });
98 return;
101 assert(m_pWidget);
102 if (bCanFocus)
103 m_pWidget->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
104 else
105 m_pWidget->setFocusPolicy(Qt::FocusPolicy::NoFocus);
108 void QtInstanceWidget::grab_focus()
110 SolarMutexGuard g;
111 QtInstance& rQtInstance = GetQtInstance();
112 if (!rQtInstance.IsMainThread())
114 rQtInstance.RunInMainThread([&] { grab_focus(); });
115 return;
118 assert(m_pWidget);
119 m_pWidget->setFocus();
122 bool QtInstanceWidget::has_focus() const
124 SolarMutexGuard g;
125 QtInstance& rQtInstance = GetQtInstance();
126 if (!rQtInstance.IsMainThread())
128 bool bFocus = false;
129 rQtInstance.RunInMainThread([&] { bFocus = has_focus(); });
130 return bFocus;
133 assert(m_pWidget);
134 return m_pWidget->hasFocus();
137 bool QtInstanceWidget::is_active() const { return has_focus(); }
139 bool QtInstanceWidget::has_child_focus() const
141 SolarMutexGuard g;
142 QtInstance& rQtInstance = GetQtInstance();
143 if (!rQtInstance.IsMainThread())
145 bool bChildFocus = false;
146 rQtInstance.RunInMainThread([&] { bChildFocus = has_child_focus(); });
147 return bChildFocus;
150 QWidget* pFocusWidget = QApplication::focusWidget();
151 if (!pFocusWidget)
152 return false;
154 QWidget* pParent = pFocusWidget->parentWidget();
155 while (pParent)
157 if (pParent == m_pWidget)
158 return true;
159 pParent = pParent->parentWidget();
161 return false;
164 void QtInstanceWidget::show()
166 SolarMutexGuard g;
167 QtInstance& rQtInstance = GetQtInstance();
168 if (!rQtInstance.IsMainThread())
170 rQtInstance.RunInMainThread([&] { show(); });
171 return;
174 assert(m_pWidget);
175 m_pWidget->show();
178 void QtInstanceWidget::hide()
180 SolarMutexGuard g;
181 QtInstance& rQtInstance = GetQtInstance();
182 if (!rQtInstance.IsMainThread())
184 rQtInstance.RunInMainThread([&] { hide(); });
185 return;
188 assert(m_pWidget);
189 m_pWidget->hide();
192 void QtInstanceWidget::set_size_request(int nWidth, int nHeight)
194 SolarMutexGuard g;
195 GetQtInstance().RunInMainThread(
196 [&] { m_pWidget->setMinimumSize(std::max(0, nWidth), std::max(0, nHeight)); });
199 Size QtInstanceWidget::get_size_request() const
201 SolarMutexGuard g;
203 Size aSize;
204 GetQtInstance().RunInMainThread([&] { aSize = toSize(m_pWidget->minimumSize()); });
205 return aSize;
208 Size QtInstanceWidget::get_preferred_size() const
210 SolarMutexGuard g;
212 Size aPreferredSize;
213 GetQtInstance().RunInMainThread([&] { aPreferredSize = toSize(m_pWidget->sizeHint()); });
215 return aPreferredSize;
218 float QtInstanceWidget::get_approximate_digit_width() const
220 SolarMutexGuard g;
222 float fWidth = 0;
223 GetQtInstance().RunInMainThread(
224 [&] { fWidth = m_pWidget->fontMetrics().horizontalAdvance("0123456789") / 10.0; });
225 return fWidth;
228 int QtInstanceWidget::get_text_height() const
230 SolarMutexGuard g;
232 int nHeight = 0;
233 GetQtInstance().RunInMainThread([&] { nHeight = m_pWidget->fontMetrics().height(); });
234 return nHeight;
237 Size QtInstanceWidget::get_pixel_size(const OUString& rText) const
239 SolarMutexGuard g;
241 Size aSize;
242 GetQtInstance().RunInMainThread(
243 [&] { aSize = toSize(m_pWidget->fontMetrics().boundingRect(toQString(rText)).size()); });
245 return aSize;
248 vcl::Font QtInstanceWidget::get_font()
250 assert(false && "Not implemented yet");
251 return vcl::Font();
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)
260 SolarMutexGuard g;
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
269 SolarMutexGuard g;
270 QtInstance& rQtInstance = GetQtInstance();
271 if (!rQtInstance.IsMainThread())
273 OUString sHelpId;
274 rQtInstance.RunInMainThread([&] { sHelpId = get_help_id(); });
275 return sHelpId;
278 const QVariant aHelpIdVariant = m_pWidget->property(PROPERTY_HELP_ID);
279 if (!aHelpIdVariant.isValid())
280 return OUString();
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");
291 return true;
294 void QtInstanceWidget::set_vexpand(bool) { assert(false && "Not implemented yet"); }
296 bool QtInstanceWidget::get_vexpand() const
298 assert(false && "Not implemented yet");
299 return true;
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");
313 return 0;
316 int QtInstanceWidget::get_margin_bottom() const
318 assert(false && "Not implemented yet");
319 return 0;
322 int QtInstanceWidget::get_margin_start() const
324 assert(false && "Not implemented yet");
325 return 0;
328 int QtInstanceWidget::get_margin_end() const
330 assert(false && "Not implemented yet");
331 return 0;
334 void QtInstanceWidget::set_accessible_name(const OUString& rName)
336 SolarMutexGuard g;
337 QtInstance& rQtInstance = GetQtInstance();
338 if (!rQtInstance.IsMainThread())
340 rQtInstance.RunInMainThread([&] { set_accessible_name(rName); });
341 return;
344 assert(m_pWidget);
345 m_pWidget->setAccessibleName(toQString(rName));
348 void QtInstanceWidget::set_accessible_description(const OUString& rDescription)
350 SolarMutexGuard g;
351 QtInstance& rQtInstance = GetQtInstance();
352 if (!rQtInstance.IsMainThread())
354 rQtInstance.RunInMainThread([&] { set_accessible_description(rDescription); });
355 return;
358 assert(m_pWidget);
359 m_pWidget->setAccessibleDescription(toQString(rDescription));
362 OUString QtInstanceWidget::get_accessible_name() const
364 SolarMutexGuard g;
365 QtInstance& rQtInstance = GetQtInstance();
366 if (!rQtInstance.IsMainThread())
368 OUString sName;
369 rQtInstance.RunInMainThread([&] { sName = get_accessible_name(); });
370 return sName;
373 assert(m_pWidget);
374 return toOUString(m_pWidget->accessibleName());
377 OUString QtInstanceWidget::get_accessible_description() const
379 SolarMutexGuard g;
380 QtInstance& rQtInstance = GetQtInstance();
381 if (!rQtInstance.IsMainThread())
383 OUString sDescription;
384 rQtInstance.RunInMainThread([&] { sDescription = get_accessible_description(); });
385 return sDescription;
388 assert(m_pWidget);
389 return toOUString(m_pWidget->accessibleDescription());
392 OUString QtInstanceWidget::get_accessible_id() const
394 SolarMutexGuard g;
395 QtInstance& rQtInstance = GetQtInstance();
396 if (!rQtInstance.IsMainThread())
398 OUString sId;
399 rQtInstance.RunInMainThread([&] { sId = get_accessible_id(); });
400 return sId;
403 assert(m_pWidget);
404 #if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
405 return toOUString(m_pWidget->accessibleIdentifier());
406 #else
407 return OUString();
408 #endif
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)
418 SolarMutexGuard g;
419 QtInstance& rQtInstance = GetQtInstance();
420 if (!rQtInstance.IsMainThread())
422 rQtInstance.RunInMainThread([&] { set_tooltip_text(rTip); });
423 return;
426 assert(m_pWidget);
427 m_pWidget->setToolTip(toQString(rTip));
430 OUString QtInstanceWidget::get_tooltip_text() const
432 SolarMutexGuard g;
433 QtInstance& rQtInstance = GetQtInstance();
434 if (!rQtInstance.IsMainThread())
436 OUString sToolTipText;
437 rQtInstance.RunInMainThread([&] { sToolTipText = get_tooltip_text(); });
438 return sToolTipText;
441 assert(m_pWidget);
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");
452 return false;
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
460 SolarMutexGuard g;
462 bool bRet = false;
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)
469 return;
471 QPoint aRelativePos = m_pWidget->mapTo(pRelativeWidget->getQWidget(), QPoint(0, 0));
472 rX = aRelativePos.x();
473 rY = aRelativePos.y();
474 bRet = true;
477 return bRet;
480 bool QtInstanceWidget::get_direction() const
482 SolarMutexGuard g;
484 bool bRTL = false;
485 GetQtInstance().RunInMainThread(
486 [&] { bRTL = m_pWidget->layoutDirection() == Qt::LayoutDirection::RightToLeft; });
487 return bRTL;
490 void QtInstanceWidget::set_direction(bool bRTL)
492 SolarMutexGuard g;
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)
506 SolarMutexGuard g;
508 GetQtInstance().RunInMainThread([&] {
509 if (bBusy)
510 ++m_nBusyCount;
511 else
512 --m_nBusyCount;
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();
525 if (!pParentWidget)
526 return nullptr;
528 return std::make_unique<QtInstanceContainer>(pParentWidget);
531 void QtInstanceWidget::queue_resize()
533 SolarMutexGuard g;
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);
558 return xRet;
561 css::uno::Reference<css::datatransfer::dnd::XDropTarget> QtInstanceWidget::get_drop_target()
563 assert(false && "Not implemented yet");
564 return nullptr;
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)
608 SolarMutexGuard g;
610 if (pOldFocus == m_pWidget)
611 signal_focus_out();
612 else if (pNewFocus == m_pWidget)
613 signal_focus_in();
616 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */