Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
previewactiongroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <deviceprofile_p.h>
7#include <shared_settings_p.h>
8
9#include <QtWidgets/qstylefactory.h>
10#include <QtCore/qvariant.h>
11
13
14using namespace Qt::StringLiterals;
15
16enum { MaxDeviceActions = 20 };
17
18namespace qdesigner_internal {
19
20PreviewActionGroup::PreviewActionGroup(QDesignerFormEditorInterface *core, QObject *parent) :
21 QActionGroup(parent),
22 m_core(core)
23{
24 /* Create a list of up to MaxDeviceActions invisible actions to be
25 * populated with device profiles (actiondata: index) followed by the
26 * standard style actions (actiondata: style name). */
27 connect(this, &PreviewActionGroup::triggered, this, &PreviewActionGroup::slotTriggered);
28 setExclusive(true);
29
30 // Create invisible actions for devices. Set index as action data.
31 for (int i = 0; i < MaxDeviceActions; i++) {
32 QAction *a = new QAction(this);
33 a->setObjectName(QString::asprintf("__qt_designer_device_%d_action", i));
34 a->setVisible(false);
35 a->setData(i);
36 addAction(a);
37 }
38 // Create separator at index MaxDeviceActions
39 QAction *sep = new QAction(this);
40 sep->setObjectName(u"__qt_designer_deviceseparator"_s);
41 sep->setSeparator(true);
42 sep->setVisible(false);
43 addAction(sep);
44 // Populate devices
45 updateDeviceProfiles();
46
47 // Add style actions
48 const QStringList styles = QStyleFactory::keys();
49 // Make sure ObjectName is unique in case toolbar solution is used.
50 // Create styles. Set style name string as action data.
51 for (const auto &s : styles) {
52 QAction *a = new QAction(tr("%1 Style").arg(s), this);
53 a->setObjectName("__qt_designer_style_"_L1 + s + "_action"_L1);
54 a->setData(s);
55 addAction(a);
56 }
57}
58
59void PreviewActionGroup::updateDeviceProfiles()
60{
61 const QDesignerSharedSettings settings(m_core);
62 const auto profiles = settings.deviceProfiles();
63 const auto al = actions();
64 // Separator?
65 const bool hasProfiles = !profiles.isEmpty();
66 al.at(MaxDeviceActions)->setVisible(hasProfiles);
67 int index = 0;
68 if (hasProfiles) {
69 // Make actions visible
70 const int maxIndex = qMin(static_cast<int>(MaxDeviceActions), profiles.size());
71 for (; index < maxIndex; index++) {
72 const QString name = profiles.at(index).name();
73 al.at(index)->setText(name);
74 al.at(index)->setVisible(true);
75 }
76 }
77 // Hide rest
78 for ( ; index < MaxDeviceActions; index++)
79 al.at(index)->setVisible(false);
80}
81
82void PreviewActionGroup::slotTriggered(QAction *a)
83{
84 // Device or style according to data.
85 const QVariant data = a->data();
86 switch (data.metaType().id()) {
87 case QMetaType::QString:
88 emit preview(data.toString(), -1);
89 break;
90 case QMetaType::Int:
91 emit preview(QString(), data.toInt());
92 break;
93 default:
94 break;
95 }
96}
97
98}
99
100QT_END_NAMESPACE
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
@ MaxDeviceActions