OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / designer / formeditorstack.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "formeditorstack.h"
34 #include "formwindoweditor.h"
35 #include "formeditorw.h"
36 #include "designerconstants.h"
37
38 #include <widgethost.h>
39
40 #include <coreplugin/coreconstants.h>
41 #include <coreplugin/modemanager.h>
42 #include <coreplugin/imode.h>
43
44 #include <utils/qtcassert.h>
45
46 #include <QtDesigner/QDesignerFormWindowInterface>
47 #include <QtDesigner/QDesignerFormWindowManagerInterface>
48 #include <QtDesigner/QDesignerFormEditorInterface>
49 #include <QtDesigner/QDesignerPropertyEditorInterface>
50
51 #include <QtCore/QDebug>
52 #include <QtCore/QVariant>
53 #include <QtCore/QRect>
54
55 namespace Designer {
56 namespace Internal {
57
58 FormEditorStack::FormEditorStack(QWidget *parent) :
59     QStackedWidget(parent),
60     m_designerCore(0)
61 {
62     setObjectName(QLatin1String("FormEditorStack"));
63 }
64
65 void FormEditorStack::add(const EditorData &data)
66 {
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*)));
73     }
74
75     if (Designer::Constants::Internal::debug)
76         qDebug() << "FormEditorStack::add"  << data.formWindowEditor << data.widgetHost;
77
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*)));
85
86     connect(data.widgetHost, SIGNAL(formWindowSizeChanged(int,int)),
87             this, SLOT(formSizeChanged(int,int)));
88
89     if (Designer::Constants::Internal::debug)
90         qDebug() << "FormEditorStack::add" << data.widgetHost << m_formEditors.size();
91
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);
96 }
97
98 int FormEditorStack::indexOfFormWindow(const QDesignerFormWindowInterface *fw) const
99 {
100     const int count = m_formEditors.size();
101      for(int i = 0; i < count; ++i)
102          if (m_formEditors[i].widgetHost->formWindow() == fw)
103              return i;
104      return -1;
105 }
106
107 int FormEditorStack::indexOfFormEditor(const QObject *xmlEditor) const
108 {
109     const int count = m_formEditors.size();
110     for(int i = 0; i < count; ++i)
111         if (m_formEditors[i].formWindowEditor == xmlEditor)
112             return i;
113     return -1;
114 }
115
116 EditorData FormEditorStack::activeEditor() const
117 {
118     // Should actually be in sync with current index.
119     if (QDesignerFormWindowInterface *afw = m_designerCore->formWindowManager()->activeFormWindow()) {
120         const int index = indexOfFormWindow(afw);
121         if (index >= 0)
122             return m_formEditors.at(index);
123     }
124     return EditorData();
125 }
126
127 SharedTools::WidgetHost *FormEditorStack::formWindowEditorForFormWindow(const QDesignerFormWindowInterface *fw) const
128 {
129     const int i = indexOfFormWindow(fw);
130     return i != -1 ? m_formEditors[i].widgetHost : static_cast<SharedTools::WidgetHost *>(0);
131 }
132
133 void FormEditorStack::removeFormWindowEditor(QObject *xmlEditor)
134 {
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.
140
141     removeWidget(m_formEditors[i].widgetHost);
142     m_formEditors[i].widgetHost->deleteLater();
143     m_formEditors.removeAt(i);
144 }
145
146 bool FormEditorStack::setVisibleEditor(Core::IEditor *xmlEditor)
147 {
148     if (Designer::Constants::Internal::debug)
149         qDebug() << "FormEditorStack::setVisibleEditor"  << xmlEditor;
150     const int i = indexOfFormEditor(xmlEditor);
151     QTC_ASSERT(i != -1, return false);
152
153     if (i != currentIndex())
154         setCurrentIndex(i);
155     return true;
156 }
157
158 void FormEditorStack::updateFormWindowSelectionHandles()
159 {
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);
167     }
168 }
169
170 void FormEditorStack::formSizeChanged(int w, int h)
171 {
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) );
179     }
180 }
181
182 SharedTools::WidgetHost *FormEditorStack::formWindowEditorForXmlEditor(const Core::IEditor *xmlEditor) const
183 {
184     const int i = indexOfFormEditor(xmlEditor);
185     return i != -1 ? m_formEditors.at(i).widgetHost : static_cast<SharedTools::WidgetHost *>(0);
186 }
187
188 void FormEditorStack::modeAboutToChange(Core::IMode *m)
189 {
190     if (Designer::Constants::Internal::debug && m)
191         qDebug() << "FormEditorStack::modeAboutToChange"  << m->id();
192
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();
197 }
198
199 } // Internal
200 } // Designer