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 "formwindowcursor.h" |
30 | #include "formwindow.h" |
31 | |
32 | // sdk |
33 | #include <QtDesigner/propertysheet.h> |
34 | #include <QtDesigner/qextensionmanager.h> |
35 | #include <qdesigner_propertycommand_p.h> |
36 | |
37 | #include <QtCore/qdebug.h> |
38 | |
39 | QT_BEGIN_NAMESPACE |
40 | |
41 | namespace qdesigner_internal { |
42 | |
43 | FormWindowCursor::FormWindowCursor(FormWindow *fw, QObject *parent) |
44 | : QObject(parent), |
45 | m_formWindow(fw) |
46 | { |
47 | update(); |
48 | connect(sender: fw, signal: &QDesignerFormWindowInterface::changed, receiver: this, slot: &FormWindowCursor::update); |
49 | } |
50 | |
51 | FormWindowCursor::~FormWindowCursor() = default; |
52 | |
53 | QDesignerFormWindowInterface *FormWindowCursor::formWindow() const |
54 | { |
55 | return m_formWindow; |
56 | } |
57 | |
58 | bool FormWindowCursor::movePosition(MoveOperation op, MoveMode mode) |
59 | { |
60 | if (widgetCount() == 0) |
61 | return false; |
62 | |
63 | int iterator = position(); |
64 | |
65 | if (mode == MoveAnchor) |
66 | m_formWindow->clearSelection(changePropertyDisplay: false); |
67 | |
68 | switch (op) { |
69 | case Next: |
70 | ++iterator; |
71 | if (iterator >= widgetCount()) |
72 | iterator = 0; |
73 | |
74 | m_formWindow->selectWidget(w: m_formWindow->widgetAt(index: iterator), select: true); |
75 | return true; |
76 | |
77 | case Prev: |
78 | --iterator; |
79 | if (iterator < 0) |
80 | iterator = widgetCount() - 1; |
81 | |
82 | if (iterator < 0) |
83 | return false; |
84 | |
85 | m_formWindow->selectWidget(w: m_formWindow->widgetAt(index: iterator), select: true); |
86 | return true; |
87 | |
88 | default: |
89 | return false; |
90 | } |
91 | } |
92 | |
93 | int FormWindowCursor::position() const |
94 | { |
95 | const int index = m_formWindow->widgets().indexOf(t: current()); |
96 | return index == -1 ? 0 : index; |
97 | } |
98 | |
99 | void FormWindowCursor::setPosition(int pos, MoveMode mode) |
100 | { |
101 | if (!widgetCount()) |
102 | return; |
103 | |
104 | if (mode == MoveAnchor) |
105 | m_formWindow->clearSelection(changePropertyDisplay: false); |
106 | |
107 | if (pos >= widgetCount()) |
108 | pos = 0; |
109 | |
110 | m_formWindow->selectWidget(w: m_formWindow->widgetAt(index: pos), select: true); |
111 | } |
112 | |
113 | QWidget *FormWindowCursor::current() const |
114 | { |
115 | return m_formWindow->currentWidget(); |
116 | } |
117 | |
118 | bool FormWindowCursor::hasSelection() const |
119 | { |
120 | return !m_formWindow->selectedWidgets().isEmpty(); |
121 | } |
122 | |
123 | int FormWindowCursor::selectedWidgetCount() const |
124 | { |
125 | int N = m_formWindow->selectedWidgets().count(); |
126 | return N ? N : 1; |
127 | } |
128 | |
129 | QWidget *FormWindowCursor::selectedWidget(int index) const |
130 | { |
131 | return hasSelection() |
132 | ? m_formWindow->selectedWidgets().at(i: index) |
133 | : m_formWindow->mainContainer(); |
134 | } |
135 | |
136 | void FormWindowCursor::update() |
137 | { |
138 | // ### todo |
139 | } |
140 | |
141 | int FormWindowCursor::widgetCount() const |
142 | { |
143 | return m_formWindow->widgetCount(); |
144 | } |
145 | |
146 | QWidget *FormWindowCursor::widget(int index) const |
147 | { |
148 | return m_formWindow->widgetAt(index); |
149 | } |
150 | |
151 | void FormWindowCursor::setProperty(const QString &name, const QVariant &value) |
152 | { |
153 | |
154 | // build selection |
155 | const int N = selectedWidgetCount(); |
156 | Q_ASSERT(N); |
157 | |
158 | QObjectList selection; |
159 | for (int i=0; i<N; ++i) |
160 | selection.push_back(t: selectedWidget(index: i)); |
161 | |
162 | |
163 | SetPropertyCommand* setPropertyCommand = new SetPropertyCommand(m_formWindow); |
164 | if (setPropertyCommand->init(list: selection, propertyName: name, newValue: value, referenceObject: current())) { |
165 | m_formWindow->commandHistory()->push(cmd: setPropertyCommand); |
166 | } else { |
167 | delete setPropertyCommand; |
168 | qDebug() << "Unable to set property " << name << '.'; |
169 | } |
170 | } |
171 | |
172 | void FormWindowCursor::setWidgetProperty(QWidget *widget, const QString &name, const QVariant &value) |
173 | { |
174 | SetPropertyCommand *cmd = new SetPropertyCommand(m_formWindow); |
175 | if (cmd->init(object: widget, propertyName: name, newValue: value)) { |
176 | m_formWindow->commandHistory()->push(cmd); |
177 | } else { |
178 | delete cmd; |
179 | qDebug() << "Unable to set property " << name << '.'; |
180 | } |
181 | } |
182 | |
183 | void FormWindowCursor::resetWidgetProperty(QWidget *widget, const QString &name) |
184 | { |
185 | ResetPropertyCommand *cmd = new ResetPropertyCommand(m_formWindow); |
186 | if (cmd->init(object: widget, propertyName: name)) { |
187 | m_formWindow->commandHistory()->push(cmd); |
188 | } else { |
189 | delete cmd; |
190 | qDebug() << "Unable to reset property " << name << '.'; |
191 | } |
192 | } |
193 | |
194 | } |
195 | |
196 | QT_END_NAMESPACE |
197 | |