2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2010-03-09 10:26:20 +01:00
|
|
|
#include "formwindoweditor.h"
|
2010-03-10 10:21:52 +01:00
|
|
|
#include "formwindowfile.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "designerconstants.h"
|
2010-03-10 10:21:52 +01:00
|
|
|
#include "resourcehandler.h"
|
|
|
|
|
#include "qt_private/formwindowbase_p.h"
|
|
|
|
|
#include "designerxmleditor.h"
|
|
|
|
|
#include <widgethost.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2010-05-21 14:18:34 +02:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2010-03-10 10:21:52 +01:00
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
#include <texteditor/plaintexteditor.h>
|
2010-03-09 10:26:20 +01:00
|
|
|
|
2009-05-13 15:10:09 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtDesigner/QDesignerFormWindowInterface>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
2010-03-10 10:21:52 +01:00
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtCore/QFile>
|
2010-09-24 15:19:14 +02:00
|
|
|
#include <QtGui/QApplication>
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
namespace Designer {
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
struct FormWindowEditorPrivate
|
|
|
|
|
{
|
2010-03-10 10:21:52 +01:00
|
|
|
explicit FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor,
|
|
|
|
|
QDesignerFormWindowInterface *form);
|
|
|
|
|
|
2011-02-21 19:30:43 +01:00
|
|
|
TextEditor::PlainTextEditor m_textEditor;
|
2010-03-10 10:21:52 +01:00
|
|
|
Internal::FormWindowFile m_file;
|
2010-06-25 12:56:16 +02:00
|
|
|
Core::Context m_context;
|
2010-03-10 10:21:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FormWindowEditorPrivate::FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor,
|
|
|
|
|
QDesignerFormWindowInterface *form) :
|
2011-02-21 19:30:43 +01:00
|
|
|
m_textEditor(editor), m_file(form)
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditor *editor,
|
2011-02-21 19:30:43 +01:00
|
|
|
QDesignerFormWindowInterface *form,
|
|
|
|
|
QObject *parent) :
|
2010-03-10 10:21:52 +01:00
|
|
|
Core::IEditor(parent),
|
|
|
|
|
d(new FormWindowEditorPrivate(editor, form))
|
|
|
|
|
{
|
2010-06-25 17:37:59 +02:00
|
|
|
d->m_context.add(Designer::Constants::K_DESIGNER_XML_EDITOR_ID);
|
|
|
|
|
d->m_context.add(Designer::Constants::C_DESIGNER_XML_EDITOR);
|
2010-03-10 10:21:52 +01:00
|
|
|
connect(form, SIGNAL(changed()), this, SIGNAL(changed()));
|
2011-02-21 19:30:43 +01:00
|
|
|
// Revert to saved/load externally modified files.
|
|
|
|
|
connect(&d->m_file, SIGNAL(reload(QString)), this, SLOT(slotOpen(QString)));
|
2010-03-12 16:54:32 +01:00
|
|
|
// Force update of open editors model.
|
2011-02-21 19:30:43 +01:00
|
|
|
connect(&d->m_file, SIGNAL(saved()), this, SIGNAL(changed()));
|
|
|
|
|
connect(&d->m_file, SIGNAL(changed()), this, SIGNAL(changed()));
|
2010-05-21 14:18:34 +02:00
|
|
|
connect(this, SIGNAL(changed()), this, SLOT(configureXmlEditor()));
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
FormWindowEditor::~FormWindowEditor()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::createNew(const QString &contents)
|
|
|
|
|
{
|
|
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "FormWindowEditor::createNew" << contents.size();
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(QString());
|
|
|
|
|
|
|
|
|
|
QDesignerFormWindowInterface *form = d->m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (contents.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-09-24 15:19:14 +02:00
|
|
|
// If we have an override cursor, reset it over Designer loading,
|
|
|
|
|
// should it pop up messages about missing resources or such.
|
|
|
|
|
const bool hasOverrideCursor = QApplication::overrideCursor();
|
|
|
|
|
QCursor overrideCursor;
|
|
|
|
|
if (hasOverrideCursor) {
|
|
|
|
|
overrideCursor = QCursor(*QApplication::overrideCursor());
|
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
form->setContents(contents);
|
2010-09-24 15:19:14 +02:00
|
|
|
|
|
|
|
|
if (hasOverrideCursor)
|
|
|
|
|
QApplication::setOverrideCursor(overrideCursor);
|
|
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
if (form->mainContainer() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
syncXmlEditor(contents);
|
|
|
|
|
d->m_file.setFileName(QString());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
void FormWindowEditor::slotOpen(const QString &fileName)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-03-10 10:21:52 +01:00
|
|
|
open(fileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
bool FormWindowEditor::open(const QString &fileName)
|
2009-10-01 16:38:08 +02:00
|
|
|
{
|
2010-03-09 10:26:20 +01:00
|
|
|
if (Designer::Constants::Internal::debug)
|
2010-03-10 10:21:52 +01:00
|
|
|
qDebug() << "FormWindowEditor::open" << fileName;
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
QDesignerFormWindowInterface *form = d->m_file.formWindow();
|
|
|
|
|
QTC_ASSERT(form, return false);
|
|
|
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
|
setDisplayName(tr("untitled"));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
d->m_file.setFileName(absfileName);
|
|
|
|
|
|
|
|
|
|
if (Internal::ResourceHandler *rh = qFindChild<Designer::Internal::ResourceHandler*>(form))
|
|
|
|
|
rh->updateResources();
|
|
|
|
|
|
|
|
|
|
emit changed();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormWindowEditor::syncXmlEditor()
|
|
|
|
|
{
|
|
|
|
|
if (Designer::Constants::Internal::debug)
|
|
|
|
|
qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_file.fileName();
|
|
|
|
|
syncXmlEditor(contents());
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-21 14:18:34 +02:00
|
|
|
void FormWindowEditor::configureXmlEditor() const
|
|
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
TextEditor::PlainTextEditorWidget *editor =
|
2011-02-21 19:30:43 +01:00
|
|
|
qobject_cast<TextEditor::PlainTextEditorWidget *>(d->m_textEditor.editorWidget());
|
2010-05-21 14:18:34 +02:00
|
|
|
if (editor)
|
|
|
|
|
editor->configure(Core::ICore::instance()->mimeDatabase()->findByFile(
|
|
|
|
|
d->m_file.fileName()));
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-10 10:21:52 +01:00
|
|
|
void FormWindowEditor::syncXmlEditor(const QString &contents)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
d->m_textEditor.editorWidget()->setPlainText(contents);
|
|
|
|
|
d->m_textEditor.editorWidget()->setReadOnly(true);
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IFile *FormWindowEditor::file()
|
|
|
|
|
{
|
|
|
|
|
return &d->m_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FormWindowEditor::id() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Designer::Constants::K_DESIGNER_XML_EDITOR_ID);
|
2009-10-01 16:38:08 +02:00
|
|
|
}
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
QString FormWindowEditor::displayName() const
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.displayName();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormWindowEditor::setDisplayName(const QString &title)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
d->m_textEditor.setDisplayName(title);
|
2010-11-30 12:55:41 +01:00
|
|
|
emit changed();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::duplicateSupported() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IEditor *FormWindowEditor::duplicate(QWidget *)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray FormWindowEditor::saveState() const
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.saveState();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::restoreState(const QByteArray &state)
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.restoreState(state);
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
Core::Context FormWindowEditor::context() const
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
|
|
|
|
return d->m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *FormWindowEditor::widget()
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return d->m_textEditor.widget();
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormWindowEditor::isTemporary() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *FormWindowEditor::toolBar()
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FormWindowEditor::contents() const
|
|
|
|
|
{
|
|
|
|
|
const qdesigner_internal::FormWindowBase *fw = qobject_cast<const qdesigner_internal::FormWindowBase *>(d->m_file.formWindow());
|
|
|
|
|
QTC_ASSERT(fw, return QString());
|
|
|
|
|
return fw->fileContents(); // No warnings about spacers here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextDocument *FormWindowEditor::textDocument()
|
|
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return qobject_cast<TextEditor::BaseTextDocument*>(d->m_textEditor.file());
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 19:30:43 +01:00
|
|
|
TextEditor::PlainTextEditor *FormWindowEditor::textEditor()
|
2010-03-10 10:21:52 +01:00
|
|
|
{
|
2011-02-21 19:30:43 +01:00
|
|
|
return &d->m_textEditor;
|
2010-03-10 10:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
2010-09-14 15:15:57 +02:00
|
|
|
QString FormWindowEditor::preferredModeType() const
|
2010-03-16 16:51:45 +01:00
|
|
|
{
|
2010-09-14 15:15:57 +02:00
|
|
|
return QLatin1String(Core::Constants::MODE_DESIGN_TYPE);
|
2010-03-16 16:51:45 +01:00
|
|
|
}
|
2010-03-10 10:21:52 +01:00
|
|
|
|
|
|
|
|
} // namespace Designer
|
|
|
|
|
|