1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "formeditorstack.h"
34 #include "formwindoweditor.h"
35 #include "formeditorw.h"
36 #include "designerconstants.h"
38 #include <widgethost.h>
40 #include <coreplugin/coreconstants.h>
41 #include <coreplugin/modemanager.h>
42 #include <coreplugin/imode.h>
44 #include <utils/qtcassert.h>
46 #include <QtDesigner/QDesignerFormWindowInterface>
47 #include <QtDesigner/QDesignerFormWindowManagerInterface>
48 #include <QtDesigner/QDesignerFormEditorInterface>
49 #include <QtDesigner/QDesignerPropertyEditorInterface>
51 #include <QtCore/QDebug>
52 #include <QtCore/QVariant>
53 #include <QtCore/QRect>
58 FormEditorStack::FormEditorStack(QWidget *parent) :
59 QStackedWidget(parent),
62 setObjectName(QLatin1String("FormEditorStack"));
65 void FormEditorStack::add(const EditorData &data)
67 if (m_designerCore == 0) { // Initialize first time here
68 m_designerCore = data.widgetHost->formWindow()->core();
69 connect(m_designerCore->formWindowManager(), SIGNAL(activeFormWindowChanged(QDesignerFormWindowInterface*)),
70 this, SLOT(updateFormWindowSelectionHandles()));
71 connect(Core::ModeManager::instance(), SIGNAL(currentModeAboutToChange(Core::IMode*)),
72 this, SLOT(modeAboutToChange(Core::IMode*)));
75 if (Designer::Constants::Internal::debug)
76 qDebug() << "FormEditorStack::add" << data.formWindowEditor << data.widgetHost;
78 m_formEditors.append(data);
79 addWidget(data.widgetHost);
80 // Editors are normally removed by listening to EditorManager::editorsClosed.
81 // However, in the case opening a file fails, EditorManager just deletes the editor, which
82 // is caught by the destroyed() signal.
83 connect(data.formWindowEditor, SIGNAL(destroyed(QObject*)),
84 this, SLOT(removeFormWindowEditor(QObject*)));
86 connect(data.widgetHost, SIGNAL(formWindowSizeChanged(int,int)),
87 this, SLOT(formSizeChanged(int,int)));
89 if (Designer::Constants::Internal::debug)
90 qDebug() << "FormEditorStack::add" << data.widgetHost << m_formEditors.size();
92 // Since we have 1 pixel splitters we enforce no frame
93 // on the content widget
94 if (QFrame *frame = qobject_cast<QFrame*>(data.widgetHost))
95 frame->setFrameStyle(QFrame::NoFrame);
98 int FormEditorStack::indexOfFormWindow(const QDesignerFormWindowInterface *fw) const
100 const int count = m_formEditors.size();
101 for(int i = 0; i < count; ++i)
102 if (m_formEditors[i].widgetHost->formWindow() == fw)
107 int FormEditorStack::indexOfFormEditor(const QObject *xmlEditor) const
109 const int count = m_formEditors.size();
110 for(int i = 0; i < count; ++i)
111 if (m_formEditors[i].formWindowEditor == xmlEditor)
116 EditorData FormEditorStack::activeEditor() const
118 // Should actually be in sync with current index.
119 if (QDesignerFormWindowInterface *afw = m_designerCore->formWindowManager()->activeFormWindow()) {
120 const int index = indexOfFormWindow(afw);
122 return m_formEditors.at(index);
127 SharedTools::WidgetHost *FormEditorStack::formWindowEditorForFormWindow(const QDesignerFormWindowInterface *fw) const
129 const int i = indexOfFormWindow(fw);
130 return i != -1 ? m_formEditors[i].widgetHost : static_cast<SharedTools::WidgetHost *>(0);
133 void FormEditorStack::removeFormWindowEditor(QObject *xmlEditor)
135 const int i = indexOfFormEditor(xmlEditor);
136 if (Designer::Constants::Internal::debug)
137 qDebug() << "FormEditorStack::removeFormWindowEditor" << xmlEditor << i << " of " << m_formEditors.size() ;
138 if (i == -1) // Fail silently as this is invoked for all editors from EditorManager
139 return; // and editor deletion signal.
141 removeWidget(m_formEditors[i].widgetHost);
142 m_formEditors[i].widgetHost->deleteLater();
143 m_formEditors.removeAt(i);
146 bool FormEditorStack::setVisibleEditor(Core::IEditor *xmlEditor)
148 if (Designer::Constants::Internal::debug)
149 qDebug() << "FormEditorStack::setVisibleEditor" << xmlEditor;
150 const int i = indexOfFormEditor(xmlEditor);
151 QTC_ASSERT(i != -1, return false);
153 if (i != currentIndex())
158 void FormEditorStack::updateFormWindowSelectionHandles()
160 // Display form selection handles only on active window
161 if (Designer::Constants::Internal::debug)
162 qDebug() << "updateFormWindowSelectionHandles";
163 QDesignerFormWindowInterface *activeFormWindow = m_designerCore->formWindowManager()->activeFormWindow();
164 foreach(const EditorData &fdm, m_formEditors) {
165 const bool active = activeFormWindow == fdm.widgetHost->formWindow();
166 fdm.widgetHost->updateFormWindowSelectionHandles(active);
170 void FormEditorStack::formSizeChanged(int w, int h)
172 // Handle main container resize.
173 if (Designer::Constants::Internal::debug)
174 qDebug() << Q_FUNC_INFO << w << h;
175 if (const SharedTools::WidgetHost *wh = qobject_cast<const SharedTools::WidgetHost *>(sender())) {
176 wh->formWindow()->setDirty(true);
177 static const QString geometry = QLatin1String("geometry");
178 m_designerCore->propertyEditor()->setPropertyValue(geometry, QRect(0,0,w,h) );
182 SharedTools::WidgetHost *FormEditorStack::formWindowEditorForXmlEditor(const Core::IEditor *xmlEditor) const
184 const int i = indexOfFormEditor(xmlEditor);
185 return i != -1 ? m_formEditors.at(i).widgetHost : static_cast<SharedTools::WidgetHost *>(0);
188 void FormEditorStack::modeAboutToChange(Core::IMode *m)
190 if (Designer::Constants::Internal::debug && m)
191 qDebug() << "FormEditorStack::modeAboutToChange" << m->id();
193 // Sync the editor when entering edit mode
194 if (m && m->id() == QLatin1String(Core::Constants::MODE_EDIT))
195 foreach(const EditorData &data, m_formEditors)
196 data.formWindowEditor->syncXmlEditor();