2010-02-26 11:08:17 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "designerxmleditor.h"
|
|
|
|
|
#include "designerconstants.h"
|
2010-03-09 17:01:34 +01:00
|
|
|
#include "resourcehandler.h"
|
2010-03-09 15:48:01 +01:00
|
|
|
#include "qt_private/formwindowbase_p.h"
|
|
|
|
|
|
2010-03-09 17:01:34 +01:00
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/modemanager.h>
|
|
|
|
|
#include <coreplugin/imode.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
|
#include <coreplugin/uniqueidmanager.h>
|
2010-03-09 15:48:01 +01:00
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QtDesigner/QDesignerFormWindowInterface>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtCore/QFile>
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-02-26 15:20:50 +01:00
|
|
|
namespace Designer {
|
2010-03-09 15:48:01 +01:00
|
|
|
|
|
|
|
|
DesignerXmlEditorEditable::DesignerXmlEditorEditable(Internal::DesignerXmlEditor *editor,
|
|
|
|
|
QDesignerFormWindowInterface *form,
|
|
|
|
|
QObject *parent) :
|
|
|
|
|
Core::IEditor(parent),
|
|
|
|
|
m_textEditable(editor),
|
|
|
|
|
m_file(form)
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
Core::UniqueIDManager *uidm = Core::UniqueIDManager::instance();
|
|
|
|
|
m_context << uidm->uniqueIdentifier(QLatin1String(Designer::Constants::K_DESIGNER_XML_EDITOR_ID));
|
|
|
|
|
m_context << uidm->uniqueIdentifier(QLatin1String(Designer::Constants::C_DESIGNER_XML_EDITOR));
|
|
|
|
|
connect(form, SIGNAL(changed()), this, SIGNAL(changed()));
|
|
|
|
|
// Revert to saved/load externally modified files
|
|
|
|
|
connect(&m_file, SIGNAL(reload(QString)), this, SLOT(slotOpen(QString)));
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
bool DesignerXmlEditorEditable::createNew(const QString &contents)
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "DesignerXmlEditorEditable::createNew" << contents.size();
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(QString());
|
|
|
|
|
|
|
|
|
|
QDesignerFormWindowInterface *form = m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (contents.isEmpty())
|
|
|
|
|
return false;
|
2010-02-26 11:08:17 +01:00
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
form->setContents(contents);
|
|
|
|
|
if (form->mainContainer() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(contents);
|
|
|
|
|
m_file.setFileName(QString());
|
|
|
|
|
return true;
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
void DesignerXmlEditorEditable::slotOpen(const QString &fileName)
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
open(fileName);
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
bool DesignerXmlEditorEditable::open(const QString &fileName)
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "DesignerXmlEditorEditable::open" << fileName;
|
|
|
|
|
|
|
|
|
|
QDesignerFormWindowInterface *form = m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
|
setDisplayName(tr("untitled"));
|
|
|
|
|
return true;
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
2010-03-09 15:48:01 +01:00
|
|
|
|
|
|
|
|
const QFileInfo fi(fileName);
|
|
|
|
|
const QString absfileName = fi.absoluteFilePath();
|
|
|
|
|
|
|
|
|
|
QFile file(absfileName);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
form->setFileName(absfileName);
|
|
|
|
|
|
|
|
|
|
const QString contents = QString::fromUtf8(file.readAll());
|
|
|
|
|
form->setContents(contents);
|
|
|
|
|
file.close();
|
|
|
|
|
if (!form->mainContainer())
|
|
|
|
|
return false;
|
|
|
|
|
form->setDirty(false);
|
|
|
|
|
syncXmlEditor(contents);
|
|
|
|
|
|
|
|
|
|
setDisplayName(fi.fileName());
|
|
|
|
|
m_file.setFileName(absfileName);
|
|
|
|
|
|
2010-03-09 17:01:34 +01:00
|
|
|
if (Internal::ResourceHandler *rh = qFindChild<Designer::Internal::ResourceHandler*>(form))
|
|
|
|
|
rh->updateResources();
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
emit changed();
|
|
|
|
|
|
|
|
|
|
return true;
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
void DesignerXmlEditorEditable::syncXmlEditor()
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "DesignerXmlEditorEditable::syncXmlEditor" << m_file.fileName();
|
|
|
|
|
syncXmlEditor(contents());
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
void DesignerXmlEditorEditable::syncXmlEditor(const QString &contents)
|
|
|
|
|
{
|
|
|
|
|
m_textEditable.editor()->setPlainText(contents);
|
|
|
|
|
m_textEditable.editor()->setReadOnly(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IFile *DesignerXmlEditorEditable::file()
|
|
|
|
|
{
|
|
|
|
|
return &m_file;
|
|
|
|
|
}
|
2010-02-26 15:20:50 +01:00
|
|
|
|
2010-02-26 11:08:17 +01:00
|
|
|
QString DesignerXmlEditorEditable::id() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Designer::Constants::K_DESIGNER_XML_EDITOR_ID);
|
|
|
|
|
}
|
2010-02-26 15:20:50 +01:00
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
QString DesignerXmlEditorEditable::displayName() const
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
return m_textEditable.displayName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignerXmlEditorEditable::setDisplayName(const QString &title)
|
|
|
|
|
{
|
|
|
|
|
m_textEditable.setDisplayName(title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignerXmlEditorEditable::duplicateSupported() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IEditor *DesignerXmlEditorEditable::duplicate(QWidget *)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray DesignerXmlEditorEditable::saveState() const
|
|
|
|
|
{
|
|
|
|
|
return m_textEditable.saveState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignerXmlEditorEditable::restoreState(const QByteArray &state)
|
|
|
|
|
{
|
|
|
|
|
return m_textEditable.restoreState(state);
|
2010-02-26 11:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<int> DesignerXmlEditorEditable::context() const
|
|
|
|
|
{
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
QWidget *DesignerXmlEditorEditable::widget()
|
|
|
|
|
{
|
|
|
|
|
return m_textEditable.widget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignerXmlEditorEditable::isTemporary() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *DesignerXmlEditorEditable::toolBar()
|
2010-02-26 11:08:17 +01:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2010-03-09 10:26:20 +01:00
|
|
|
|
2010-03-09 15:48:01 +01:00
|
|
|
QString DesignerXmlEditorEditable::contents() const
|
|
|
|
|
{
|
|
|
|
|
const qdesigner_internal::FormWindowBase *fw = qobject_cast<const qdesigner_internal::FormWindowBase *>(m_file.formWindow());
|
|
|
|
|
QTC_ASSERT(fw, return QString());
|
|
|
|
|
return fw->fileContents(); // No warnings about spacers here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextDocument *DesignerXmlEditorEditable::textDocument()
|
|
|
|
|
{
|
|
|
|
|
return qobject_cast<TextEditor::BaseTextDocument*>(m_textEditable.file());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::PlainTextEditorEditable *DesignerXmlEditorEditable::textEditable()
|
|
|
|
|
{
|
|
|
|
|
return &m_textEditable;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-09 10:26:20 +01:00
|
|
|
namespace Internal {
|
2010-03-09 15:48:01 +01:00
|
|
|
|
|
|
|
|
DesignerXmlEditor::DesignerXmlEditor(QDesignerFormWindowInterface *form,
|
|
|
|
|
QWidget *parent) :
|
|
|
|
|
TextEditor::PlainTextEditor(parent),
|
|
|
|
|
m_editable(new DesignerXmlEditorEditable(this, form))
|
|
|
|
|
{
|
|
|
|
|
setReadOnly(true);
|
|
|
|
|
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
|
|
|
|
|
SLOT(updateEditorInfoBar(Core::IEditor*)));
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-09 10:26:20 +01:00
|
|
|
TextEditor::BaseTextEditorEditable *DesignerXmlEditor::createEditableInterface()
|
|
|
|
|
{
|
2010-03-09 15:48:01 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "DesignerXmlEditor::createEditableInterface()";
|
|
|
|
|
return m_editable->textEditable();
|
2010-03-09 10:26:20 +01:00
|
|
|
}
|
2010-03-09 15:48:01 +01:00
|
|
|
|
|
|
|
|
void DesignerXmlEditor::updateEditorInfoBar(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
if (editor == m_editable) {
|
|
|
|
|
Core::EditorManager::instance()->showEditorInfoBar(Constants::INFO_READ_ONLY,
|
|
|
|
|
tr("This file can only be edited in Design Mode."),
|
|
|
|
|
"Open Designer", this, SLOT(designerModeClicked()));
|
|
|
|
|
}
|
|
|
|
|
if (!editor)
|
|
|
|
|
Core::EditorManager::instance()->hideEditorInfoBar(Constants::INFO_READ_ONLY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignerXmlEditor::designerModeClicked()
|
|
|
|
|
{
|
|
|
|
|
Core::ICore::instance()->modeManager()->activateMode(QLatin1String(Core::Constants::MODE_DESIGN));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DesignerXmlEditorEditable *DesignerXmlEditor::designerEditable() const
|
|
|
|
|
{
|
|
|
|
|
return m_editable;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-09 10:26:20 +01:00
|
|
|
}
|
2010-02-26 15:20:50 +01:00
|
|
|
} // namespace Designer
|
2010-03-09 10:26:20 +01:00
|
|
|
|