1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Designer of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "previewactiongroup.h"
30 
31 #include <deviceprofile_p.h>
32 #include <shared_settings_p.h>
33 
34 #include <QtWidgets/qstylefactory.h>
35 #include <QtCore/qvariant.h>
36 
37 QT_BEGIN_NAMESPACE
38 
39 enum { MaxDeviceActions = 20 };
40 
41 namespace qdesigner_internal {
42 
PreviewActionGroup(QDesignerFormEditorInterface * core,QObject * parent)43 PreviewActionGroup::PreviewActionGroup(QDesignerFormEditorInterface *core, QObject *parent) :
44     QActionGroup(parent),
45     m_core(core)
46 {
47     /* Create a list of up to MaxDeviceActions invisible actions to be
48      * populated with device profiles (actiondata: index) followed by the
49      * standard style actions (actiondata: style name). */
50     connect(this, &PreviewActionGroup::triggered, this, &PreviewActionGroup::slotTriggered);
51     setExclusive(true);
52 
53     const QString objNamePostfix = QStringLiteral("_action");
54     // Create invisible actions for devices. Set index as action data.
55     QString objNamePrefix = QStringLiteral("__qt_designer_device_");
56     for (int i = 0; i < MaxDeviceActions; i++) {
57         QAction *a = new QAction(this);
58         QString objName = objNamePrefix;
59         objName += QString::number(i);
60         objName += objNamePostfix;
61         a->setObjectName(objName);
62         a->setVisible(false);
63         a->setData(i);
64         addAction(a);
65     }
66     // Create separator at index MaxDeviceActions
67     QAction *sep = new QAction(this);
68     sep->setObjectName(QStringLiteral("__qt_designer_deviceseparator"));
69     sep->setSeparator(true);
70     sep->setVisible(false);
71     addAction(sep);
72     // Populate devices
73     updateDeviceProfiles();
74 
75     // Add style actions
76     const QStringList styles = QStyleFactory::keys();
77     const QStringList::const_iterator cend = styles.constEnd();
78     // Make sure ObjectName  is unique in case toolbar solution is used.
79     objNamePrefix = QStringLiteral("__qt_designer_style_");
80     // Create styles. Set style name string as action data.
81     for (QStringList::const_iterator it = styles.constBegin(); it !=  cend ;++it) {
82         QAction *a = new QAction(tr("%1 Style").arg(*it), this);
83         QString objName = objNamePrefix;
84         objName += *it;
85         objName += objNamePostfix;
86         a->setObjectName(objName);
87         a->setData(*it);
88         addAction(a);
89     }
90 }
91 
updateDeviceProfiles()92 void PreviewActionGroup::updateDeviceProfiles()
93 {
94     const QDesignerSharedSettings settings(m_core);
95     const auto profiles = settings.deviceProfiles();
96     const auto al = actions();
97     // Separator?
98     const bool hasProfiles = !profiles.isEmpty();
99     al.at(MaxDeviceActions)->setVisible(hasProfiles);
100     int index = 0;
101     if (hasProfiles) {
102         // Make actions visible
103         const int maxIndex = qMin(static_cast<int>(MaxDeviceActions), profiles.size());
104         for (; index < maxIndex; index++) {
105             const QString name = profiles.at(index).name();
106             al.at(index)->setText(name);
107             al.at(index)->setVisible(true);
108         }
109     }
110     // Hide rest
111     for ( ; index < MaxDeviceActions; index++)
112         al.at(index)->setVisible(false);
113 }
114 
slotTriggered(QAction * a)115 void PreviewActionGroup::slotTriggered(QAction *a)
116 {
117     // Device or style according to data.
118     const QVariant data = a->data();
119     switch (data.type()) {
120     case QVariant::String:
121         emit preview(data.toString(), -1);
122         break;
123     case QVariant::Int:
124         emit preview(QString(), data.toInt());
125         break;
126     default:
127         break;
128     }
129 }
130 
131 }
132 
133 QT_END_NAMESPACE
134