Debian Codegraph @

vcl/qt5/QtBuilder.cxx

raw symbols SCIP
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.
4 *
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 */
9
10 #include <QtBuilder.hxx>
11
12 #include <QtDoubleSpinBox.hxx>
13 #include <QtExpander.hxx>
14 #include <QtInstanceMenu.hxx>
15 #include <QtInstanceMessageDialog.hxx>
16 #include <QtInstanceNotebook.hxx>
17 #include <QtInstanceTreeView.hxx>
18 #include <QtHyperlinkLabel.hxx>
19
20 #include <vcl/qt/QtUtils.hxx>
21
22 #include <QtCore/QSortFilterProxyModel>
23 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
24 #include <QtGui/QActionGroup>
25 #endif
26 #include <QtGui/QStandardItemModel>
27 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
28 #include <QtWidgets/QActionGroup>
29 #endif
30 #include <QtWidgets/QButtonGroup>
31 #include <QtWidgets/QCalendarWidget>
32 #include <QtWidgets/QCheckBox>
33 #include <QtWidgets/QComboBox>
34 #include <QtWidgets/QDialog>
35 #include <QtWidgets/QDialogButtonBox>
36 #include <QtWidgets/QGroupBox>
37 #include <QtWidgets/QLabel>
38 #include <QtWidgets/QLineEdit>
39 #include <QtWidgets/QListView>
40 #include <QtWidgets/QLayout>
41 #include <QtWidgets/QProgressBar>
42 #include <QtWidgets/QPushButton>
43 #include <QtWidgets/QRadioButton>
44 #include <QtWidgets/QScrollArea>
45 #include <QtWidgets/QScrollBar>
46 #include <QtWidgets/QSlider>
47 #include <QtWidgets/QSplitter>
48 #include <QtWidgets/QTabWidget>
49 #include <QtWidgets/QToolBar>
50 #include <QtWidgets/QToolButton>
51 #include <QtWidgets/QWizard>
52
53 namespace
54 {
55 QString convertAccelerator(const OUString& rText)
56 {
57 // preserve literal '&'s and use '&' instead of '_' for the accelerator
58 return toQString(rText.replaceAll("&", "&&").replace('_', '&'));
59 }
60 }
61
62 QtBuilder::QtBuilder(QWidget* pParent, std::u16string_view sUIRoot, const OUString& rUIFile)
63 : WidgetBuilder(sUIRoot, rUIFile, false)
64 {
65 SolarMutexGuard g;
66
67 GetQtInstance().RunInMainThread([&] {
68 processUIFile(pParent);
69
70 // tweak widget hierarchy (remove unnecessary parent widgets)
71 for (const std::pair<QWidget*, QWidget*>& rPair : m_aWidgetReplacements)
72 replaceWidget(rPair.first, rPair.second);
73 });
74 }
75
76 QtBuilder::~QtBuilder() {}
77
78 QWidget* QtBuilder::get_by_name(const OUString& rId)
79 {
80 auto aIt = m_aWidgets.find(rId);
81 if (aIt != m_aWidgets.end())
82 return aIt->second;
83
84 return nullptr;
85 }
86
87 OUString QtBuilder::getDialogId()
88 {
89 for (const std::pair<const OUString, QWidget*>& rEntry : m_aWidgets)
90 if (qobject_cast<QDialog*>(rEntry.second))
91 return rEntry.first;
92
93 return OUString();
94 }
95
96 void QtBuilder::insertComboBoxOrListBoxItems(QObject* pObject, stringmap& rMap,
97 const std::vector<ComboBoxTextItem>& rItems)
98 {
99 if (QComboBox* pComboBox = qobject_cast<QComboBox*>(pObject))
100 {
101 for (const ComboBoxTextItem& rItem : rItems)
102 {
103 QVariant aUserData;
104 if (!rItem.m_sId.isEmpty())
105 aUserData = QVariant::fromValue(toQString(rItem.m_sId));
106
107 pComboBox->addItem(toQString(rItem.m_sItem), aUserData);
108 }
109
110 const int nActiveId = BuilderBase::extractActive(rMap);
111 pComboBox->setCurrentIndex(nActiveId);
112 return;
113 }
114
115 assert(false && "list boxes are not supported yet");
116 }
117
118 QObject* QtBuilder::insertObject(QObject* pParent, const OUString& rClass, std::string_view sType,
119 const OUString& rId, stringmap& rProps, stringmap&, stringmap&)
120 {
121 QObject* pCurrentChild = makeObject(pParent, rClass, sType, rId, rProps);
122
123 rProps.clear();
124
125 return pCurrentChild;
126 }
127
128 QObject* QtBuilder::makeObject(QObject* pParent, std::u16string_view sName, std::string_view sType,
129 const OUString& rId, stringmap& rMap)
130 {
131 // ignore placeholders
132 if (sName.empty())
133 return nullptr;
134
135 // nothing to do for this one
136 if (sName == u"GtkTreeSelection")
137 return nullptr;
138
139 QWidget* pParentWidget = qobject_cast<QWidget*>(pParent);
140 QLayout* pParentLayout = qobject_cast<QLayout*>(pParent);
141
142 QObject* pObject = nullptr;
143 // in case a QLayout is created, an additional QWidget parent
144 // will also be created because that is needed for QtInstanceContainer
145 QWidget* pLayoutParentWidget = nullptr;
146
147 if (sName == u"GtkMessageDialog")
148 {
149 QMessageBox* pMessageBox = new QMessageBox(pParentWidget);
150 setMessageDialogProperties(*pMessageBox, rMap);
151 pObject = pMessageBox;
152 }
153 else if (sName == u"GtkAssistant")
154 {
155 pObject = new QWizard(pParentWidget);
156 }
157 else if (sName == u"GtkBox")
158 {
159 // for a QMessageBox, return the existing layout instead of creating a new one
160 if (QMessageBox* pMessageBox = qobject_cast<QMessageBox*>(pParent))
161 {
162 pObject = pMessageBox->layout();
163 assert(pObject && "QMessageBox has no layout");
164 }
165 else
166 {
167 QWidget* pBoxParentWidget = pParentWidget;
168 // Unless this is the direct GtkBox child of a GtkDialog, create a parent widget
169 // that can be used to create a QtInstanceContainer for this box
170 if (!qobject_cast<QDialog*>(pParentWidget))
171 {
172 pLayoutParentWidget = new QWidget(pParentWidget);
173 pBoxParentWidget = pLayoutParentWidget;
174 }
175
176 const bool bVertical = hasOrientationVertical(rMap);
177 if (bVertical)
178 pObject = new QVBoxLayout(pBoxParentWidget);
179 else
180 pObject = new QHBoxLayout(pBoxParentWidget);
181 }
182 }
183 else if (sName == u"GtkButtonBox")
184 {
185 QWidget* pTopLevel = windowForObject(pParent);
186 if (QMessageBox* pMessageBox = qobject_cast<QMessageBox*>(pTopLevel))
187 {
188 // for a QMessageBox, return the existing button box instead of creating a new one
189 QDialogButtonBox* pButtonBox = QtInstanceDialog::findButtonBox(pMessageBox);
190 assert(pButtonBox && "Could not find QMessageBox's button box");
191 pObject = pButtonBox;
192
193 // skip adding to layout below, button box is already contained in dialog
194 pParentLayout = nullptr;
195 }
196 else
197 {
198 QDialogButtonBox* pButtonBox = new QDialogButtonBox(pParentWidget);
199 if (hasOrientationVertical(rMap))
200 pButtonBox->setOrientation(Qt::Vertical);
201 pObject = pButtonBox;
202 }
203 }
204 else if (sName == u"GtkButton")
205 {
206 QPushButton* pButton = new QPushButton(pParentWidget);
207 setButtonProperties(*pButton, rMap, pParentWidget);
208 pObject = pButton;
209 }
210 else if (sName == u"GtkCalendar")
211 {
212 pObject = new QCalendarWidget(pParentWidget);
213 }
214 else if (sName == u"GtkCellRendererPixbuf")
215 {
216 return enableTreeViewColumnDataRole(pParentWidget, Qt::DecorationRole);
217 }
218 else if (sName == u"GtkCellRendererText")
219 {
220 return enableTreeViewColumnDataRole(pParentWidget, Qt::DisplayRole);
221 }
222 else if (sName == u"GtkCellRendererToggle")
223 {
224 return enableTreeViewColumnDataRole(pParentWidget, Qt::CheckStateRole);
225 }
226 else if (sName == u"GtkCheckButton")
227 {
228 QCheckBox* pCheckBox = new QCheckBox(pParentWidget);
229 setCheckButtonProperties(*pCheckBox, rMap, pParentWidget);
230 pObject = pCheckBox;
231 }
232 else if (sName == u"GtkComboBox" || sName == u"GtkComboBoxText")
233 {
234 QComboBox* pComboBox = new QComboBox(pParentWidget);
235 pComboBox->setEditable(extractEntry(rMap));
236 pObject = pComboBox;
237 }
238 else if (sName == u"GtkDialog")
239 {
240 QDialog* pDialog = new QDialog(pParentWidget);
241 setDialogProperties(*pDialog, rMap);
242 pObject = pDialog;
243 }
244 else if (sName == u"GtkDrawingArea")
245 {
246 pObject = new QLabel(pParentWidget);
247 }
248 else if (sName == u"GtkEntry")
249 {
250 QLineEdit* pLineEdit = new QLineEdit(pParentWidget);
251 setEntryProperties(*pLineEdit, rMap);
252 pObject = pLineEdit;
253 }
254 else if (sName == u"GtkExpander")
255 {
256 pObject = new QtExpander(pParentWidget);
257 }
258 else if (sName == u"GtkFrame")
259 {
260 pObject = new QGroupBox(pParentWidget);
261 }
262 else if (sName == u"GtkGrid")
263 {
264 pLayoutParentWidget = new QWidget(pParentWidget);
265 pObject = new QGridLayout(pLayoutParentWidget);
266 }
267 else if (sName == u"GtkIconView")
268 {
269 QListView* pListView = new QListView(pParentWidget);
270 pListView->setModel(new QStandardItemModel(pListView));
271 pListView->setViewMode(QListView::IconMode);
272 pListView->setMovement(QListView::Static);
273 pObject = pListView;
274 }
275 else if (sName == u"GtkImage")
276 {
277 QLabel* pLabel = new QLabel(pParentWidget);
278 const OUString sIconName = extractIconName(rMap);
279 if (!sIconName.isEmpty())
280 {
281 const Image aImage = loadThemeImage(sIconName);
282 if (!aImage.GetBitmap().IsEmpty())
283 {
284 pLabel->setPixmap(toQPixmap(aImage));
285 }
286 else
287 {
288 const QIcon aIcon = QIcon::fromTheme(toQString(sIconName));
289 assert(!aIcon.isNull() && "No icon found for that icon name");
290 const int nIconSize = QApplication::style()->pixelMetric(QStyle::PM_ButtonIconSize);
291 pLabel->setPixmap(aIcon.pixmap(nIconSize));
292 }
293 }
294 pObject = pLabel;
295 }
296 else if (sName == u"GtkLabel")
297 {
298 QLabel* pLabel = new QLabel(pParentWidget);
299 setLabelProperties(*pLabel, rMap);
300 extractMnemonicWidget(rId, rMap);
301 pObject = pLabel;
302 }
303 else if (sName == u"GtkLevelBar" || sName == u"GtkProgressBar")
304 {
305 QProgressBar* pProgressBar = new QProgressBar(pParentWidget);
306 // don't show text (progress in percent) by default
307 pProgressBar->setTextVisible(false);
308 pObject = pProgressBar;
309 }
310 else if (sName == u"GtkLinkButton")
311 {
312 QtHyperlinkLabel* pLabel = new QtHyperlinkLabel(pParentWidget);
313 if (rMap.contains(u"label"_ustr))
314 pLabel->setDisplayText(toQString(rMap[u"label"_ustr]));
315 if (rMap.contains(u"uri"_ustr))
316 pLabel->setUri(toQString(rMap[u"uri"_ustr]));
317
318 pObject = pLabel;
319 }
320 else if (sName == u"GtkMenuButton")
321 {
322 QToolButton* pMenuButton = new QToolButton(pParentWidget);
323 setMenuButtonProperties(*pMenuButton, rMap, pParentWidget);
324 pObject = pMenuButton;
325 }
326 else if (sName == u"GtkNotebook")
327 {
328 pObject = new QTabWidget(pParentWidget);
329 }
330 else if (sName == u"GtkPaned")
331 {
332 pObject = new QSplitter(pParentWidget);
333 }
334 else if (sName == u"GtkPopover")
335 {
336 QWidget* pWidget = new QWidget(pParentWidget, Qt::Popup);
337 pWidget->setLayout(new QVBoxLayout);
338 pObject = pWidget;
339 }
340 else if (sName == u"GtkRadioButton")
341 {
342 QRadioButton* pRadioButton = new QRadioButton(pParentWidget);
343 // apply GtkCheckButton properties because GtkRadioButton subclasses GtkCheckButton in GTK 3
344 setCheckButtonProperties(*pRadioButton, rMap, pParentWidget);
345 extractRadioButtonGroup(rId, rMap);
346 pObject = pRadioButton;
347 }
348 else if (sName == u"GtkScale")
349 {
350 QSlider* pSlider = new QSlider(pParentWidget);
351 setScaleProperties(*pSlider, rMap);
352 pObject = pSlider;
353 }
354 else if (sName == u"GtkScrollbar")
355 {
356 pObject = new QScrollBar(pParentWidget);
357 }
358 else if (sName == u"GtkScrolledWindow")
359 {
360 QScrollArea* pScrollArea = new QScrollArea(pParentWidget);
361 pScrollArea->setWidgetResizable(true);
362 pObject = pScrollArea;
363 }
364 else if (sName == u"GtkSeparator")
365 {
366 const bool bVertical = hasOrientationVertical(rMap);
367 QFrame* pFrame = new QFrame(pParentWidget);
368 pFrame->setFrameShape(bVertical ? QFrame::VLine : QFrame::HLine);
369 pObject = pFrame;
370 }
371 else if (sName == u"GtkSeparatorToolItem")
372 {
373 QToolBar* pToolBar = qobject_cast<QToolBar*>(pParentWidget);
374 assert(pToolBar && "GtkSeparatorToolItem doesn't have a toolbar parent");
375 pToolBar->addSeparator();
376 }
377 else if (sName == u"GtkSpinButton")
378 {
379 QtDoubleSpinBox* pSpinBox = new QtDoubleSpinBox(pParentWidget);
380 setSpinButtonProperties(*pSpinBox, rMap);
381 pObject = pSpinBox;
382 }
383 else if (sName == u"GtkSpinner")
384 {
385 // use a QProgressBar in undetermined state (i.e. with range [0, 0])
386 QProgressBar* pProgressBar = new QProgressBar(pParentWidget);
387 pProgressBar->setRange(0, 0);
388 pObject = pProgressBar;
389 }
390 else if (sName == u"GtkTextView")
391 {
392 QPlainTextEdit* pTextEdit = new QPlainTextEdit(pParentWidget);
393 setTextViewProperties(*pTextEdit, rMap);
394 pObject = pTextEdit;
395 }
396 else if (sName == u"GtkToggleButton")
397 {
398 QToolButton* pButton = new QToolButton(pParentWidget);
399 setButtonProperties(*pButton, rMap, pParentWidget);
400 pObject = pButton;
401 }
402 else if (sName == u"GtkToolbar")
403 {
404 pObject = new QToolBar(pParentWidget);
405 }
406 else if (sName == u"GtkToggleToolButton" || sName == u"GtkToolButton")
407 {
408 QToolButton* pToolButton = new QToolButton(pParentWidget);
409 const OUString sIconName = extractIconName(rMap);
410 if (!sIconName.isEmpty())
411 {
412 const Image aImage = loadThemeImage(sIconName);
413 pToolButton->setIcon(toQPixmap(aImage));
414 }
415 pToolButton->setText(toQString(extractLabel(rMap)));
416 pToolButton->setCheckable(sName == u"GtkToggleToolButton");
417 pObject = pToolButton;
418 }
419 else if (sName == u"GtkTreeView")
420 {
421 QTreeView* pTreeView = new QTreeView(pParentWidget);
422 QStandardItemModel* pItemModel = new QStandardItemModel(pTreeView);
423 QSortFilterProxyModel* pProxyModel = new QSortFilterProxyModel(pTreeView);
424 pProxyModel->setSourceModel(pItemModel);
425 pTreeView->setModel(pProxyModel);
426 pTreeView->setHeaderHidden(!extractHeadersVisible(rMap));
427 pTreeView->setRootIsDecorated(extractShowExpanders(rMap));
428 pObject = pTreeView;
429 }
430 else if (sName == u"GtkTreeViewColumn")
431 {
432 QTreeView* pTreeView = qobject_cast<QTreeView*>(pParentWidget);
433 assert(pTreeView && "Tree view column doesn't have a tree view parent");
434 QAbstractItemModel* pModel = pTreeView->model();
435 assert(pModel && "Tree view doesn't have model set");
436 const int nCol = pModel->columnCount();
437 pModel->insertColumn(nCol);
438 pModel->setHeaderData(nCol, Qt::Horizontal, toQString(extractTitle(rMap)));
439
440 // add initially empty list of supported roles for the new column that will
441 // be extended based on the GtkCellRenderer children of the column
442 QList<QList<Qt::ItemDataRole>> aColumnRoles = QtInstanceTreeView::columnRoles(*pTreeView);
443 aColumnRoles.push_back({});
444 QtInstanceTreeView::setColumnRoles(*pTreeView, aColumnRoles);
445
446 // nothing else to do, return tree view parent for the widget
447 return pTreeView;
448 }
449 else if (sName == u"GtkViewport")
450 {
451 // GtkViewport is an adaptor to make GtkWidgets scrollable
452 // inside a GtkScrolledWindow; no equivalent needed for widgets
453 // inside QScrollArea - just create a simple QWidget
454 pObject = new QWidget(pParentWidget);
455 }
456 else
457 {
458 SAL_WARN("vcl.qt", "Widget type not supported yet: "
459 << OUStringToOString(sName, RTL_TEXTENCODING_UTF8));
460 assert(false && "Widget type not supported yet");
461 }
462
463 QWidget* pWidget = qobject_cast<QWidget*>(pObject);
464 if (pWidget)
465 setWidgetProperties(*pWidget, rMap);
466 else
467 pWidget = pLayoutParentWidget;
468
469 if (QSplitter* pSplitterParent = qobject_cast<QSplitter*>(pParentWidget))
470 {
471 assert(pWidget);
472 pSplitterParent->addWidget(pWidget);
473 // unset to not create a layout below
474 pParentWidget = nullptr;
475 }
476 else if (QTabWidget* pParentTabWidget = qobject_cast<QTabWidget*>(pParentWidget))
477 {
478 // remove QTabWidget child widget, set via QTabWidget::addTab instead
479 assert(pWidget);
480 pWidget->setParent(nullptr);
481 // initially, add tab with empty label, QtBuilder::applyTabChildProperties will evaluate actual one
482 pParentTabWidget->addTab(pWidget, QStringLiteral());
483 // unset pParentWidget to not create a layout below
484 pParentWidget = nullptr;
485 }
486 else if (QToolBar* pParentToolBar = qobject_cast<QToolBar*>(pParentWidget))
487 {
488 pParentToolBar->addWidget(pWidget);
489 // unset pParentWidget to not create a layout below
490 pParentWidget = nullptr;
491 }
492 else if (QtExpander* pExpander = qobject_cast<QtExpander*>(pParentWidget))
493 {
494 // set the content (not the label) child as the expander's widget
495 if (sType != "label")
496 {
497 pExpander->setContentWidget(pWidget);
498 // erase "visible" property, QtExpander shows/hides the widget as needed
499 rMap.erase("visible");
500 }
501 }
502
503 if (pWidget)
504 {
505 if (!pParentLayout && pParentWidget)
506 {
507 // if the parent is a widget, use the widget's layout, and ensure it has one set
508 pParentLayout = pParentWidget->layout();
509 if (!pParentLayout)
510 pParentLayout = new QVBoxLayout(pParentWidget);
511 }
512
513 // add widget to parent layout
514 if (pParentLayout)
515 pParentLayout->addWidget(pWidget);
516
517 QtInstanceWidget::setHelpId(*pWidget, getHelpRoot() + rId);
518
519 pWidget->setToolTip(toRichTextTooltip(extractTooltipText(rMap)));
520 pWidget->setVisible(extractVisible(rMap));
521
522 #if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
523 // Set GtkBuilder ID as accessible ID
524 pWidget->setAccessibleIdentifier(toQString(rId));
525 #endif
526 }
527 else if (QLayout* pLayout = qobject_cast<QLayout*>(pObject))
528 {
529 // add layout to parent layout
530 if (QBoxLayout* pParentBoxLayout = qobject_cast<QBoxLayout*>(pParentLayout))
531 pParentBoxLayout->addLayout(pLayout);
532 else if (QGridLayout* pParentGridLayout = qobject_cast<QGridLayout*>(pParentLayout))
533 pParentGridLayout->addLayout(pLayout, pParentGridLayout->rowCount(), 0);
534 }
535
536 if (pObject)
537 pObject->setObjectName(toQString(rId));
538
539 if (pWidget)
540 m_aWidgets[rId] = pWidget;
541
542 return pObject;
543 }
544
545 void QtBuilder::tweakInsertedChild(QObject* pParent, QObject* pCurrentChild, std::string_view sType,
546 std::string_view sInternalChild)
547 {
548 if (sInternalChild == "entry" && qobject_cast<QComboBox*>(pParent))
549 {
550 // an editable GtkComboBox has an internal GtkEntry child,
551 // but QComboBox doesn't need a separate widget for it, so
552 // delete it
553 deleteObject(pCurrentChild);
554 }
555
556 if (sType == "label")
557 {
558 if (QLabel* pLabel = qobject_cast<QLabel*>(pCurrentChild))
559 {
560 if (QGroupBox* pGroupBox = qobject_cast<QGroupBox*>(pParent))
561 {
562 // GtkFrame has a `child-type="label"` child for the GtkFrame label
563 // in the GtkBuilder .ui file, s. https://docs.gtk.org/gtk3/class.Frame.html
564 // For QGroupBox, the title can be set directly. Therefore, take over the
565 // title from the label and delete the separate label widget again
566 pGroupBox->setTitle(pLabel->text());
567 deleteObject(pLabel);
568 }
569 else if (QtExpander* pExpander = qobject_cast<QtExpander*>(pParent))
570 {
571 // GtkExpander has a `child-type="label"` child for the expander label
572 // in the GtkBuilder .ui file, s. https://docs.gtk.org/gtk3/class.Expander.html
573 // For QtExpander, the (button) text can be set directly. Therefore, take over
574 // text from the label and delete the separate label widget again
575 pExpander->setText(pLabel->text());
576 deleteObject(pLabel);
577 }
578 }
579 }
580
581 if (QScrollArea* pScrollAreaParent = qobject_cast<QScrollArea*>(pParent))
582 {
583 if (QAbstractScrollArea* pScrollArea = qobject_cast<QAbstractScrollArea*>(pCurrentChild))
584 {
585 // if the child provides scrolling capabilities itself, it doesn't need
586 // another scroll area parent -> mark parent scroll area for removal
587 m_aWidgetReplacements.emplace_back(pScrollAreaParent, pScrollArea);
588 }
589 else
590 {
591 // set as the scroll area's widget
592 QWidget* pCurrentWidget = nullptr;
593 if (pCurrentChild->isWidgetType())
594 pCurrentWidget = static_cast<QWidget*>(pCurrentChild);
595 else
596 pCurrentWidget = static_cast<QLayout*>(pCurrentChild)->parentWidget();
597 assert(pCurrentWidget);
598 pScrollAreaParent->setWidget(pCurrentWidget);
599 }
600 }
601
602 if (QDialog* pDialog = qobject_cast<QDialog*>(pCurrentChild))
603 {
604 // no action needed for QMessageBox, where the default button box is used
605 // and button click is handled in QtInstanceMessageDialog
606 if (!qobject_cast<QMessageBox*>(pDialog))
607 {
608 if (QDialogButtonBox* pButtonBox = QtInstanceDialog::findButtonBox(pDialog))
609 {
610 // ensure that button box is the last item in QDialog's layout
611 // (that seems to be implicitly the case for GtkDialog in GTK)
612 QLayout* pLayout = pDialog->layout();
613 assert(pLayout && "dialog has no layout");
614 pLayout->removeWidget(pButtonBox);
615 pLayout->addWidget(pButtonBox);
616
617 // connect button click handler
618 const QList<QAbstractButton*> aButtons = pButtonBox->buttons();
619 for (QAbstractButton* pButton : aButtons)
620 {
621 assert(pButton);
622 QObject::connect(pButton, &QAbstractButton::clicked, pDialog,
623 [pDialog, pButton] {
624 QtInstanceDialog::handleButtonClick(*pDialog, *pButton);
625 });
626 }
627 }
628 }
629 }
630 }
631
632 void QtBuilder::setMnemonicWidget(const OUString& rLabelId, const OUString& rMnemonicWidgetId)
633 {
634 QLabel* pLabel = get<QLabel>(rLabelId);
635 QWidget* pBuddy = get_by_name(rMnemonicWidgetId);
636
637 if (!pLabel || !pBuddy)
638 return;
639
640 pLabel->setBuddy(pBuddy);
641 }
642
643 void QtBuilder::setRadioButtonGroup(const OUString& rRadioButtonId, const OUString& rRadioGroupId)
644 {
645 // insert all buttons into a button group owned by button whose ID matches the group's
646 QRadioButton* pGroupOwner = get<QRadioButton>(rRadioGroupId);
647 assert(pGroupOwner && "No radio button with the given group name");
648
649 QButtonGroup* pButtonGroup = nullptr;
650 static const char* const pPropertyKey = "PROPERTY_BUTTONGROUP";
651 QVariant aVariant = pGroupOwner->property(pPropertyKey);
652 if (aVariant.canConvert<QButtonGroup*>())
653 {
654 pButtonGroup = aVariant.value<QButtonGroup*>();
655 }
656 else
657 {
658 pButtonGroup = new QButtonGroup(pGroupOwner);
659 pButtonGroup->addButton(pGroupOwner);
660 }
661
662 QRadioButton* pRadioButton = get<QRadioButton>(rRadioButtonId);
663 assert(pRadioButton && "No radio button with given ID");
664 pButtonGroup->addButton(pRadioButton);
665
666 pGroupOwner->setProperty(pPropertyKey, QVariant::fromValue(pButtonGroup));
667 }
668
669 void QtBuilder::setPriority(QObject*, int) { SAL_WARN("vcl.qt", "Ignoring priority"); }
670
671 void QtBuilder::setContext(QObject*, std::vector<vcl::EnumContext::Context>&&)
672 {
673 SAL_WARN("vcl.qt", "Ignoring context");
674 }
675
676 bool QtBuilder::isHorizontalTabControl(QObject* pObject)
677 {
678 QTabWidget* pTabWidget = qobject_cast<QTabWidget*>(pObject);
679 if (!pTabWidget)
680 return false;
681
682 const QTabWidget::TabPosition ePosition = pTabWidget->tabPosition();
683 return ePosition == QTabWidget::TabPosition::North
684 || ePosition == QTabWidget::TabPosition::South;
685 }
686
687 QMenu* QtBuilder::createMenu(const OUString& rId)
688 {
689 QMenu* pMenu = new QMenu;
690 pMenu->setObjectName(toQString(rId));
691 return pMenu;
692 }
693
694 void QtBuilder::setMenuActionGroup(QMenu* pMenu, QAction* pAction, const OUString& rRadioGroupId)
695 {
696 // use QActionGroup owned by and set as property for the QMenu
697 QActionGroup* pActionGroup = nullptr;
698 const OString sPropertyKey = OUString(u"ACTIONGROUP::"_ustr + rRadioGroupId).toUtf8();
699 QVariant aVariant = pMenu->property(sPropertyKey.getStr());
700 if (aVariant.isValid())
701 {
702 assert(aVariant.canConvert<QActionGroup*>());
703 pActionGroup = aVariant.value<QActionGroup*>();
704 }
705 else
706 {
707 pActionGroup = new QActionGroup(pMenu);
708 pMenu->setProperty(sPropertyKey.getStr(), QVariant::fromValue(pActionGroup));
709 // insert the menu item which defines the group (whose ID matches the group's ID)
710 QAction* pIdAction = pMenu->findChild<QAction*>(toQString(rRadioGroupId));
711 assert(pIdAction && "No action with the ID that the group refers to");
712 pActionGroup->addAction(pIdAction);
713 }
714
715 pActionGroup->addAction(pAction);
716 }
717
718 void QtBuilder::insertMenuObject(QMenu* pParent, QMenu* pSubMenu, const OUString& rClass,
719 const OUString& rId, stringmap& rProps, stringmap&, accelmap&)
720 {
721 const QString sLabel = convertAccelerator(extractLabel(rProps));
722 QAction* pAction = nullptr;
723 if (pSubMenu)
724 {
725 pAction = pParent->addMenu(pSubMenu);
726 pAction->setText(sLabel);
727 }
728 else
729 {
730 pAction = pParent->addAction(sLabel);
731 }
732 pAction->setObjectName(toQString(rId));
733
734 const OUString sActionName(extractActionName(rProps));
735 QtInstanceMenu::setActionName(*pAction, sActionName);
736
737 if (rClass == u"GtkCheckMenuItem")
738 {
739 pAction->setCheckable(true);
740 }
741 else if (rClass == u"GtkMenuItem")
742 {
743 // nothing else to do
744 }
745 else if (rClass == u"GtkRadioMenuItem")
746 {
747 pAction->setCheckable(true);
748 const OUString sGroup = extractGroup(rProps);
749 if (!sGroup.isEmpty())
750 setMenuActionGroup(pParent, pAction, sGroup);
751 }
752 else if (rClass == u"GtkSeparatorMenuItem")
753 {
754 pAction->setSeparator(true);
755 }
756 else
757 {
758 assert(false && "Not implemented yet");
759 }
760 }
761
762 void QtBuilder::applyAtkProperties(QObject* pObject, const stringmap& rProperties, bool)
763 {
764 if (!pObject || !pObject->isWidgetType())
765 return;
766
767 QWidget* pWidget = static_cast<QWidget*>(pObject);
768
769 for (auto const & [ rKey, rValue ] : rProperties)
770 {
771 if (rKey == "AtkObject::accessible-description")
772 pWidget->setAccessibleDescription(toQString(rValue));
773 else if (rKey == "AtkObject::accessible-name")
774 pWidget->setAccessibleName(toQString(rValue));
775 }
776 }
777
778 void QtBuilder::applyGridPackingProperties(QWidget* pCurrentChild, QGridLayout& rGrid,
779 const stringmap& rPackingProperties)
780 {
781 assert(pCurrentChild);
782
783 // properties not set when there's no explicit GtkGrid in the .ui file,
784 // like for the QGridLayout that's the (implicit) layout of a QMessageBox
785 if (!rPackingProperties.contains(u"left-attach"_ustr)
786 || !rPackingProperties.contains(u"top-attach"_ustr))
787 return;
788
789 const sal_Int32 nColumn = rPackingProperties.at(u"left-attach"_ustr).toInt32();
790 const sal_Int32 nRow = rPackingProperties.at(u"top-attach"_ustr).toInt32();
791
792 auto aWidthIt = rPackingProperties.find(u"width"_ustr);
793 sal_Int32 nColumnSpan = (aWidthIt == rPackingProperties.end()) ? 1 : aWidthIt->second.toInt32();
794
795 auto aHeightIt = rPackingProperties.find(u"height"_ustr);
796 sal_Int32 nRowSpan = (aHeightIt == rPackingProperties.end()) ? 1 : aHeightIt->second.toInt32();
797
798 rGrid.removeWidget(pCurrentChild);
799 rGrid.addWidget(pCurrentChild, nRow, nColumn, nRowSpan, nColumnSpan);
800 }
801
802 void QtBuilder::applyPackingProperties(QObject* pCurrentChild, QObject* pParent,
803 const stringmap& rPackingProperties)
804 {
805 if (!pCurrentChild)
806 return;
807
808 QWidget* pWidget = nullptr;
809 if (pCurrentChild->isWidgetType())
810 pWidget = static_cast<QWidget*>(pCurrentChild);
811 else
812 {
813 QObject* pParentObject = pCurrentChild->parent();
814 assert(pParent && "Non-widget (i.e. layout) has no parent");
815 if (pParentObject->isWidgetType())
816 pWidget = static_cast<QWidget*>(pParentObject);
817 }
818
819 if (!pWidget)
820 return;
821
822 // check parent's parent, due to extra QWidget parents for layouts
823 if (QGridLayout* pGrid = qobject_cast<QGridLayout*>(pParent))
824 applyGridPackingProperties(pWidget, *pGrid, rPackingProperties);
825 else
826 SAL_WARN("vcl.qt", "QtBuilder::applyPackingProperties not yet implemented for this case");
827 }
828
829 void QtBuilder::applyTabChildProperties(QObject* pParent, const std::vector<OUString>& rIds,
830 std::vector<vcl::EnumContext::Context>&,
831 stringmap& rProperties, stringmap&)
832 {
833 QTabWidget* pTabWidget = qobject_cast<QTabWidget*>(pParent);
834 assert(pTabWidget && "parent must be a QTabWidget");
835
836 // set ID and label for the last inserted tab
837 assert(rProperties.contains(u"label"_ustr) && "Tab has no label");
838 QtInstanceNotebook::setTabIdAndLabel(*pTabWidget, pTabWidget->count() - 1, rIds.front(),
839 rProperties.at(u"label"_ustr));
840 }
841
842 void QtBuilder::set_response(const OUString& rId, int nResponse)
843 {
844 QPushButton* pPushButton = get<QPushButton>(rId);
845 assert(pPushButton);
846 pPushButton->setProperty(QtInstanceMessageDialog::PROPERTY_VCL_RESPONSE_CODE, nResponse);
847 }
848
849 void QtBuilder::deleteObject(QObject* pObject)
850 {
851 if (pObject->isWidgetType())
852 static_cast<QWidget*>(pObject)->hide();
853 pObject->deleteLater();
854 }
855
856 QTreeView* QtBuilder::enableTreeViewColumnDataRole(QWidget* pParentWidget,
857 Qt::ItemDataRole eDataRole)
858 {
859 QTreeView* pTreeView = qobject_cast<QTreeView*>(pParentWidget);
860 if (!pTreeView)
861 return nullptr;
862
863 // Mark support for the new role for the tree view's last inserted column
864 // (the GtkCellRenderer is a child object of the GtkTreeViewColumn)
865 QList<QList<Qt::ItemDataRole>> aColumnRoles = QtInstanceTreeView::columnRoles(*pTreeView);
866 assert(!aColumnRoles.empty() && "Missing list of column data roles");
867 assert(!aColumnRoles.back().contains(eDataRole)
868 && "Using the same role multiple times in one column is not supported");
869 aColumnRoles.back().push_back(eDataRole);
870 QtInstanceTreeView::setColumnRoles(*pTreeView, aColumnRoles);
871
872 return pTreeView;
873 }
874
875 void QtBuilder::replaceWidget(QWidget* pOldWidget, QWidget* pNewWidget)
876 {
877 QWidget* pParent = pOldWidget->parentWidget();
878 assert(pParent);
879
880 // replace old with new widget and mark old widget for deletion
881 if (QLayout* pParentLayout = pParent->layout())
882 {
883 std::unique_ptr<QLayoutItem> pOldItem(pParentLayout->replaceWidget(pOldWidget, pNewWidget));
884 }
885 else if (QSplitter* pSplitter = qobject_cast<QSplitter*>(pParent))
886 {
887 const int nIndex = pSplitter->indexOf(pOldWidget);
888 assert(nIndex >= 0 && "old widget not a child of the splitter");
889 pSplitter->replaceWidget(nIndex, pNewWidget);
890 }
891 else
892 {
893 assert(false && "Unhandled case in replaceWidget - not implemented (yet)");
894 }
895
896 deleteObject(pOldWidget);
897 }
898
899 void QtBuilder::setButtonProperties(QAbstractButton& rButton, stringmap& rProps,
900 QWidget* pParentWidget)
901 {
902 for (auto const & [ rKey, rValue ] : rProps)
903 {
904 if (rKey == u"image")
905 {
906 QLabel* pImageLabel = get<QLabel>(rValue);
907 assert(pImageLabel && "Button has non-existent image set");
908 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
909 rButton.setIcon(QIcon(pImageLabel->pixmap()));
910 #else
911 rButton.setIcon(QIcon(pImageLabel->pixmap(Qt::ReturnByValue)));
912 #endif
913 // mark original label object for deletion
914 deleteObject(pImageLabel);
915 }
916 else if (rKey == u"label")
917 {
918 rButton.setText(convertAccelerator(rValue));
919 }
920 }
921
922 if (QDialogButtonBox* pButtonBox = qobject_cast<QDialogButtonBox*>(pParentWidget))
923 {
924 pButtonBox->addButton(&rButton, QDialogButtonBox::NoRole);
925
926 // for message boxes, avoid implicit standard buttons in addition to those explicitly added
927 if (QMessageBox* pMessageBox = qobject_cast<QMessageBox*>(pParentWidget->window()))
928 pMessageBox->setStandardButtons(QMessageBox::NoButton);
929 }
930 }
931
932 void QtBuilder::setCheckButtonProperties(QAbstractButton& rButton, stringmap& rProps,
933 QWidget* pParentWidget)
934 {
935 for (auto const & [ rKey, rValue ] : rProps)
936 {
937 if (rKey == u"active")
938 rButton.setChecked(toBool(rValue));
939 else if (rKey == u"inconsistent" && toBool(rValue))
940 {
941 if (QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(&rButton))
942 {
943 pCheckBox->setTristate(true);
944 pCheckBox->setCheckState(Qt::PartiallyChecked);
945 }
946 }
947 }
948
949 setButtonProperties(rButton, rProps, pParentWidget);
950 }
951
952 void QtBuilder::setDialogProperties(QDialog& rDialog, stringmap& rProps)
953 {
954 for (auto const & [ rKey, rValue ] : rProps)
955 {
956 if (rKey == u"modal")
957 rDialog.setModal(toBool(rValue));
958 else if (rKey == u"title")
959 rDialog.setWindowTitle(toQString(rValue));
960 }
961 }
962
963 void QtBuilder::setEntryProperties(QLineEdit& rLineEdit, stringmap& rProps)
964 {
965 auto aIt = rProps.find(u"placeholder-text"_ustr);
966 if (aIt != rProps.end())
967 rLineEdit.setPlaceholderText(toQString(aIt->second));
968
969 aIt = rProps.find(u"text"_ustr);
970 if (aIt != rProps.end())
971 rLineEdit.setText(toQString(aIt->second));
972
973 aIt = rProps.find(u"visibility"_ustr);
974 if (aIt != rProps.end() && !toBool(aIt->second))
975 rLineEdit.setEchoMode(QLineEdit::Password);
976 }
977
978 void QtBuilder::setLabelProperties(QLabel& rLabel, stringmap& rProps)
979 {
980 for (auto const & [ rKey, rValue ] : rProps)
981 {
982 if (rKey == u"label")
983 rLabel.setText(convertAccelerator(rValue));
984 else if (rKey == u"wrap")
985 rLabel.setWordWrap(toBool(rValue));
986 }
987 }
988
989 void QtBuilder::setMenuButtonProperties(QToolButton& rButton, stringmap& rProps,
990 QWidget* pParentWidget)
991 {
992 const OUString sMenu = extractPopupMenu(rProps);
993 if (!sMenu.isEmpty())
994 {
995 QMenu* pMenu = get_menu(sMenu);
996 assert(pMenu && "menu button references non-existing menu");
997 rButton.setMenu(pMenu);
998 }
999
1000 setButtonProperties(rButton, rProps, pParentWidget);
1001 }
1002
1003 void QtBuilder::setMessageDialogProperties(QMessageBox& rMessageBox, stringmap& rProps)
1004 {
1005 for (auto const & [ rKey, rValue ] : rProps)
1006 {
1007 if (rKey == u"buttons")
1008 {
1009 const VclButtonsType eButtons = BuilderBase::mapGtkToVclButtonsType(rValue);
1010 QtInstanceMessageDialog::addStandardButtons(rMessageBox, eButtons);
1011 }
1012 else if (rKey == u"text")
1013 {
1014 rMessageBox.setText(toQString(rValue));
1015 }
1016 else if (rKey == u"title")
1017 {
1018 rMessageBox.setWindowTitle(toQString(rValue));
1019 }
1020 else if (rKey == u"secondary-text")
1021 {
1022 rMessageBox.setInformativeText(toQString(rValue));
1023 }
1024 else if (rKey == u"message-type")
1025 {
1026 if (rValue == u"error")
1027 rMessageBox.setIcon(QMessageBox::Critical);
1028 else if (rValue == u"info")
1029 rMessageBox.setIcon(QMessageBox::Information);
1030 else if (rValue == u"question")
1031 rMessageBox.setIcon(QMessageBox::Question);
1032 else if (rValue == u"warning")
1033 rMessageBox.setIcon(QMessageBox::Warning);
1034 else
1035 assert(false && "Unhandled message-type");
1036 }
1037 }
1038 }
1039
1040 void QtBuilder::setScaleProperties(QSlider& rSlider, stringmap& rProps)
1041 {
1042 if (!hasOrientationVertical(rProps))
1043 rSlider.setOrientation(Qt::Horizontal);
1044
1045 auto aAdjustmentIt = rProps.find("adjustment");
1046 if (aAdjustmentIt != rProps.end())
1047 {
1048 const Adjustment* pAdjustment = get_adjustment_by_name(aAdjustmentIt->second);
1049 assert(pAdjustment && "referenced adjustment doesn't exist");
1050 for (auto const & [ rKey, rValue ] : *pAdjustment)
1051 {
1052 if (rKey == u"upper")
1053 rSlider.setMaximum(rValue.toInt32());
1054 else if (rKey == u"lower")
1055 rSlider.setMinimum(rValue.toInt32());
1056 else if (rKey == "value")
1057 rSlider.setValue(rValue.toInt32());
1058 else if (rKey == "page-increment")
1059 rSlider.setPageStep(rValue.toInt32());
1060 else if (rKey == "step-increment")
1061 rSlider.setSingleStep(rValue.toInt32());
1062 }
1063 }
1064 }
1065
1066 void QtBuilder::setSpinButtonProperties(QDoubleSpinBox& rSpinBox, stringmap& rProps)
1067 {
1068 auto aDigitsIt = rProps.find(u"digits"_ustr);
1069 sal_Int32 nDigits = (aDigitsIt != rProps.end()) ? aDigitsIt->second.toInt32() : 0;
1070 rSpinBox.setDecimals(nDigits);
1071
1072 auto aAdjustmentIt = rProps.find("adjustment");
1073 if (aAdjustmentIt != rProps.end())
1074 {
1075 const Adjustment* pAdjustment = get_adjustment_by_name(aAdjustmentIt->second);
1076 assert(pAdjustment && "referenced adjustment doesn't exist");
1077 for (auto const & [ rKey, rValue ] : *pAdjustment)
1078 {
1079 if (rKey == u"upper")
1080 rSpinBox.setMaximum(rValue.toDouble());
1081 else if (rKey == u"lower")
1082 rSpinBox.setMinimum(rValue.toDouble());
1083 else if (rKey == "value")
1084 rSpinBox.setValue(rValue.toDouble());
1085 else if (rKey == "step-increment")
1086 rSpinBox.setSingleStep(rValue.toDouble());
1087 }
1088 }
1089 }
1090
1091 void QtBuilder::setTextViewProperties(QPlainTextEdit& rTextEdit, stringmap& rProps)
1092 {
1093 for (auto const & [ rKey, rValue ] : rProps)
1094 {
1095 if (rKey == u"accepts-tab")
1096 {
1097 rTextEdit.setTabChangesFocus(!toBool(rValue));
1098 }
1099 else if (rKey == u"buffer")
1100 {
1101 const TextBuffer* pTextBuffer = get_buffer_by_name(rValue);
1102 assert(pTextBuffer && "No TextBuffer with the given ID");
1103 auto aIt = pTextBuffer->find(u"text"_ustr);
1104 if (aIt != pTextBuffer->end())
1105 rTextEdit.setPlainText(toQString(aIt->second));
1106 }
1107 else if (rKey == u"editable")
1108 {
1109 rTextEdit.setReadOnly(!toBool(rValue));
1110 }
1111 }
1112 }
1113
1114 void QtBuilder::setWidgetProperties(QWidget& rWidget, stringmap& rProps)
1115 {
1116 auto aHeightRequest = rProps.find(u"height-request"_ustr);
1117 if (aHeightRequest != rProps.end())
1118 rWidget.setMinimumHeight(aHeightRequest->second.toInt32());
1119 auto aSensitiveIt = rProps.find(u"sensitive"_ustr);
1120 if (aSensitiveIt != rProps.end())
1121 rWidget.setEnabled(toBool(aSensitiveIt->second));
1122 auto aWidthRequestIt = rProps.find(u"width-request"_ustr);
1123 if (aWidthRequestIt != rProps.end())
1124 rWidget.setMinimumWidth(aWidthRequestIt->second.toInt32());
1125 }
1126
1127 QWidget* QtBuilder::windowForObject(QObject* pObject)
1128 {
1129 if (QWidget* pWidget = qobject_cast<QWidget*>(pObject))
1130 return pWidget->window();
1131
1132 if (QLayout* pLayout = qobject_cast<QLayout*>(pObject))
1133 {
1134 if (QWidget* pParentWidget = pLayout->parentWidget())
1135 return pParentWidget->window();
1136 }
1137
1138 return nullptr;
1139 }
1140
1141 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */