Commit 686def2d authored by Eike Ziller's avatar Eike Ziller

Resource editor: Remove lots of indirections

Because only the tree view had access to the resource model, many
methods in the IEditor instance and document were going their way
through IEditor -> QRC editor widget -> tree view -> model.
Create the model in the document instead, pass that model to the tree
view, and let the document work on the model directly.

Change-Id: I76405e60f118e2bbf63d3f9a4d39cd73be64aa14
Reviewed-by: default avatarDaniel Teske <daniel.teske@theqtcompany.com>
parent ae477c3a
......@@ -31,6 +31,9 @@
#include "qrceditor.h"
#include "undocommands_p.h"
#include <aggregation/aggregate.h>
#include <coreplugin/find/itemviewfind.h>
#include <QDebug>
#include <QScopedPointer>
#include <QMenu>
......@@ -40,9 +43,9 @@
using namespace ResourceEditor;
using namespace ResourceEditor::Internal;
QrcEditor::QrcEditor(QWidget *parent)
QrcEditor::QrcEditor(RelativeResourceModel *model, QWidget *parent)
: QWidget(parent),
m_treeview(new ResourceView(&m_history)),
m_treeview(new ResourceView(model, &m_history)),
m_addFileAction(0)
{
m_ui.setupUi(this);
......@@ -66,7 +69,6 @@ QrcEditor::QrcEditor(QWidget *parent)
connect(m_treeview, SIGNAL(removeItem()), this, SLOT(onRemove()));
connect(m_treeview->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(updateCurrent()));
connect(m_treeview, SIGNAL(dirtyChanged(bool)), this, SIGNAL(dirtyChanged(bool)));
connect(m_treeview, SIGNAL(itemActivated(QString)),
this, SIGNAL(itemActivated(QString)));
connect(m_treeview, SIGNAL(showContextMenu(QPoint,QString)),
......@@ -97,6 +99,11 @@ QrcEditor::QrcEditor(QWidget *parent)
connect(&m_history, SIGNAL(canRedoChanged(bool)), this, SLOT(updateHistoryControls()));
connect(&m_history, SIGNAL(canUndoChanged(bool)), this, SLOT(updateHistoryControls()));
Aggregation::Aggregate * agg = new Aggregation::Aggregate;
agg->add(m_treeview);
agg->add(new Core::ItemViewFind(m_treeview));
updateHistoryControls();
updateCurrent();
}
......@@ -105,27 +112,14 @@ QrcEditor::~QrcEditor()
{
}
QString QrcEditor::fileName() const
{
return m_treeview->fileName();
}
void QrcEditor::setFileName(const QString &fileName)
void QrcEditor::loaded(bool success)
{
m_treeview->setFileName(fileName);
}
bool QrcEditor::load(const QString &fileName)
{
const bool success = m_treeview->load(fileName);
if (success) {
// Set "focus"
m_treeview->setCurrentIndex(m_treeview->model()->index(0,0));
// Expand prefix nodes
m_treeview->expandAll();
}
return success;
if (!success)
return;
// Set "focus"
m_treeview->setCurrentIndex(m_treeview->model()->index(0,0));
// Expand prefix nodes
m_treeview->expandAll();
}
void QrcEditor::refresh()
......@@ -133,26 +127,6 @@ void QrcEditor::refresh()
m_treeview->refresh();
}
bool QrcEditor::save()
{
return m_treeview->save();
}
QString QrcEditor::contents() const
{
return m_treeview->contents();
}
bool QrcEditor::isDirty()
{
return m_treeview->isDirty();
}
void QrcEditor::setDirty(bool dirty)
{
m_treeview->setDirty(dirty);
}
// Propagates a change of selection in the tree
// to the alias/prefix/language edit controls
void QrcEditor::updateCurrent()
......
......@@ -44,20 +44,10 @@ class QrcEditor : public QWidget
Q_OBJECT
public:
QrcEditor(QWidget *parent = 0);
QrcEditor(RelativeResourceModel *model, QWidget *parent = 0);
virtual ~QrcEditor();
bool load(const QString &fileName);
bool save();
QString contents() const;
QTreeView *treeView() { return m_treeview; }
QString errorMessage() const { return m_treeview->errorMessage(); }
bool isDirty();
void setDirty(bool dirty);
QString fileName() const;
void setFileName(const QString &fileName);
void loaded(bool success);
void setResourceDragEnabled(bool e);
bool resourceDragEnabled() const;
......@@ -70,7 +60,6 @@ public:
QString currentResourcePath() const;
signals:
void dirtyChanged(bool dirty);
void itemActivated(const QString &fileName);
void showContextMenu(const QPoint &globalPos, const QString &fileName);
......
......@@ -33,6 +33,7 @@
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/fileutils.h>
#include <coreplugin/icore.h>
#include <coreplugin/removefiledialog.h>
#include <coreplugin/vcsmanager.h>
#include <utils/fileutils.h>
......@@ -1123,3 +1124,107 @@ QMimeData *ResourceModel::mimeData(const QModelIndexList &indexes) const
rc->setText(doc.toString());
return rc;
}
/*!
\class FileEntryBackup
Backups a file node.
*/
class FileEntryBackup : public EntryBackup
{
private:
int m_fileIndex;
QString m_alias;
public:
FileEntryBackup(ResourceModel &model, int prefixIndex, int fileIndex,
const QString &fileName, const QString &alias)
: EntryBackup(model, prefixIndex, fileName), m_fileIndex(fileIndex),
m_alias(alias) { }
void restore() const;
};
void FileEntryBackup::restore() const
{
m_model->insertFile(m_prefixIndex, m_fileIndex, m_name, m_alias);
}
/*!
\class PrefixEntryBackup
Backups a prefix node including children.
*/
class PrefixEntryBackup : public EntryBackup
{
private:
QString m_language;
QList<FileEntryBackup> m_files;
public:
PrefixEntryBackup(ResourceModel &model, int prefixIndex, const QString &prefix,
const QString &language, const QList<FileEntryBackup> &files)
: EntryBackup(model, prefixIndex, prefix), m_language(language), m_files(files) { }
void restore() const;
};
void PrefixEntryBackup::restore() const
{
m_model->insertPrefix(m_prefixIndex, m_name, m_language);
foreach (const FileEntryBackup &entry, m_files) {
entry.restore();
}
}
RelativeResourceModel::RelativeResourceModel(QObject *parent) :
ResourceModel(parent),
m_resourceDragEnabled(false)
{
}
Qt::ItemFlags RelativeResourceModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags rc = ResourceModel::flags(index);
if ((rc & Qt::ItemIsEnabled) && m_resourceDragEnabled)
rc |= Qt::ItemIsDragEnabled;
return rc;
}
EntryBackup * RelativeResourceModel::removeEntry(const QModelIndex &index)
{
const QModelIndex prefixIndex = this->prefixIndex(index);
const bool isPrefixNode = (prefixIndex == index);
// Create backup, remove, return backup
if (isPrefixNode) {
QString dummy;
QString prefixBackup;
getItem(index, prefixBackup, dummy);
const QString languageBackup = lang(index);
const int childCount = rowCount(index);
QList<FileEntryBackup> filesBackup;
for (int i = 0; i < childCount; i++) {
const QModelIndex childIndex = this->index(i, 0, index);
const QString fileNameBackup = file(childIndex);
const QString aliasBackup = alias(childIndex);
FileEntryBackup entry(*this, index.row(), i, fileNameBackup, aliasBackup);
filesBackup << entry;
}
deleteItem(index);
return new PrefixEntryBackup(*this, index.row(), prefixBackup, languageBackup, filesBackup);
} else {
const QString fileNameBackup = file(index);
const QString aliasBackup = alias(index);
if (!QFile::exists(fileNameBackup)) {
deleteItem(index);
return new FileEntryBackup(*this, prefixIndex.row(), index.row(), fileNameBackup, aliasBackup);
}
Core::RemoveFileDialog removeFileDialog(fileNameBackup, Core::ICore::mainWindow());
if (removeFileDialog.exec() == QDialog::Accepted) {
deleteItem(index);
Core::FileUtils::removeFile(fileNameBackup, removeFileDialog.isDeleteFileChecked());
return new FileEntryBackup(*this, prefixIndex.row(), index.row(), fileNameBackup, aliasBackup);
}
return 0;
}
}
......@@ -280,6 +280,55 @@ private:
QIcon m_prefixIcon;
};
/*!
\class EntryBackup
Holds the backup of a tree node including children.
*/
class EntryBackup
{
protected:
ResourceModel *m_model;
int m_prefixIndex;
QString m_name;
EntryBackup(ResourceModel &model, int prefixIndex, const QString &name)
: m_model(&model), m_prefixIndex(prefixIndex), m_name(name) { }
public:
virtual void restore() const = 0;
virtual ~EntryBackup() { }
};
class RelativeResourceModel : public ResourceModel
{
public:
RelativeResourceModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (!index.isValid())
return QVariant();
/*
void const * const internalPointer = index.internalPointer();
if ((role == Qt::DisplayRole) && (internalPointer != NULL))
return ResourceModel::data(index, Qt::ToolTipRole);
*/
return ResourceModel::data(index, role);
}
void setResourceDragEnabled(bool e) { m_resourceDragEnabled = e; }
bool resourceDragEnabled() const { return m_resourceDragEnabled; }
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
EntryBackup * removeEntry(const QModelIndex &index);
private:
bool m_resourceDragEnabled;
};
} // namespace Internal
} // namespace ResourceEditor
......
......@@ -34,7 +34,6 @@
#include <coreplugin/fileutils.h>
#include <coreplugin/icore.h>
#include <coreplugin/removefiledialog.h>
#include <QDebug>
......@@ -47,150 +46,12 @@
#include <QMouseEvent>
#include <QUndoStack>
namespace ResourceEditor {
namespace Internal {
/*!
\class FileEntryBackup
Backups a file node.
*/
class FileEntryBackup : public EntryBackup
{
private:
int m_fileIndex;
QString m_alias;
public:
FileEntryBackup(ResourceModel &model, int prefixIndex, int fileIndex,
const QString &fileName, const QString &alias)
: EntryBackup(model, prefixIndex, fileName), m_fileIndex(fileIndex),
m_alias(alias) { }
void restore() const;
};
void FileEntryBackup::restore() const
{
m_model->insertFile(m_prefixIndex, m_fileIndex, m_name, m_alias);
}
/*!
\class PrefixEntryBackup
Backups a prefix node including children.
*/
class PrefixEntryBackup : public EntryBackup
{
private:
QString m_language;
QList<FileEntryBackup> m_files;
public:
PrefixEntryBackup(ResourceModel &model, int prefixIndex, const QString &prefix,
const QString &language, const QList<FileEntryBackup> &files)
: EntryBackup(model, prefixIndex, prefix), m_language(language), m_files(files) { }
void restore() const;
};
void PrefixEntryBackup::restore() const
{
m_model->insertPrefix(m_prefixIndex, m_name, m_language);
foreach (const FileEntryBackup &entry, m_files) {
entry.restore();
}
}
class RelativeResourceModel : public ResourceModel
{
public:
RelativeResourceModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (!index.isValid())
return QVariant();
/*
void const * const internalPointer = index.internalPointer();
if ((role == Qt::DisplayRole) && (internalPointer != NULL))
return ResourceModel::data(index, Qt::ToolTipRole);
*/
return ResourceModel::data(index, role);
}
void setResourceDragEnabled(bool e) { m_resourceDragEnabled = e; }
bool resourceDragEnabled() const { return m_resourceDragEnabled; }
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
EntryBackup * removeEntry(const QModelIndex &index);
private:
bool m_resourceDragEnabled;
};
RelativeResourceModel::RelativeResourceModel(QObject *parent) :
ResourceModel(parent),
m_resourceDragEnabled(false)
{
}
Qt::ItemFlags RelativeResourceModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags rc = ResourceModel::flags(index);
if ((rc & Qt::ItemIsEnabled) && m_resourceDragEnabled)
rc |= Qt::ItemIsDragEnabled;
return rc;
}
EntryBackup * RelativeResourceModel::removeEntry(const QModelIndex &index)
{
const QModelIndex prefixIndex = this->prefixIndex(index);
const bool isPrefixNode = (prefixIndex == index);
// Create backup, remove, return backup
if (isPrefixNode) {
QString dummy;
QString prefixBackup;
getItem(index, prefixBackup, dummy);
const QString languageBackup = lang(index);
const int childCount = rowCount(index);
QList<FileEntryBackup> filesBackup;
for (int i = 0; i < childCount; i++) {
const QModelIndex childIndex = this->index(i, 0, index);
const QString fileNameBackup = file(childIndex);
const QString aliasBackup = alias(childIndex);
FileEntryBackup entry(*this, index.row(), i, fileNameBackup, aliasBackup);
filesBackup << entry;
}
deleteItem(index);
return new PrefixEntryBackup(*this, index.row(), prefixBackup, languageBackup, filesBackup);
} else {
const QString fileNameBackup = file(index);
const QString aliasBackup = alias(index);
if (!QFile::exists(fileNameBackup)) {
deleteItem(index);
return new FileEntryBackup(*this, prefixIndex.row(), index.row(), fileNameBackup, aliasBackup);
}
Core::RemoveFileDialog removeFileDialog(fileNameBackup, Core::ICore::mainWindow());
if (removeFileDialog.exec() == QDialog::Accepted) {
deleteItem(index);
Core::FileUtils::removeFile(fileNameBackup, removeFileDialog.isDeleteFileChecked());
return new FileEntryBackup(*this, prefixIndex.row(), index.row(), fileNameBackup, aliasBackup);
}
return 0;
}
}
} // namespace Internal
} // namespace ResourceEditor
using namespace ResourceEditor;
using namespace ResourceEditor::Internal;
ResourceView::ResourceView(QUndoStack *history, QWidget *parent) :
ResourceView::ResourceView(RelativeResourceModel *model, QUndoStack *history, QWidget *parent) :
Utils::TreeView(parent),
m_qrcModel(new RelativeResourceModel(this)),
m_qrcModel(model),
m_history(history),
m_mergeId(-1)
{
......@@ -201,8 +62,6 @@ ResourceView::ResourceView(QUndoStack *history, QWidget *parent) :
header()->hide();
connect(m_qrcModel, SIGNAL(dirtyChanged(bool)),
this, SIGNAL(dirtyChanged(bool)));
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(showContextMenu(QPoint)));
connect(this, SIGNAL(activated(QModelIndex)),
......@@ -213,16 +72,6 @@ ResourceView::~ResourceView()
{
}
bool ResourceView::isDirty() const
{
return m_qrcModel->dirty();
}
void ResourceView::setDirty(bool dirty)
{
m_qrcModel->setDirty(dirty);
}
void ResourceView::findSamePlacePostDeletionModelIndex(int &row, QModelIndex &parent) const
{
// Concept:
......@@ -370,32 +219,6 @@ QStringList ResourceView::fileNamesToAdd()
tr("All files (*)"));
}
bool ResourceView::load(const QString &fileName)
{
const QFileInfo fi(fileName);
m_qrcModel->setFileName(fi.absoluteFilePath());
if (!fi.exists())
return false;
return m_qrcModel->reload();
}
bool ResourceView::save()
{
return m_qrcModel->save();
}
QString ResourceView::contents() const
{
return m_qrcModel->contents();
}
QString ResourceView::errorMessage() const
{
return m_qrcModel->errorMessage();
}
QString ResourceView::currentAlias() const
{
const QModelIndex current = currentIndex();
......@@ -534,11 +357,6 @@ QString ResourceView::fileName() const
return m_qrcModel->fileName();
}
void ResourceView::setFileName(const QString &fileName)
{
m_qrcModel->setFileName(fileName);
}
void ResourceView::setResourceDragEnabled(bool e)
{
setDragEnabled(e);
......
......@@ -47,26 +47,6 @@ QT_END_NAMESPACE
namespace ResourceEditor {
namespace Internal {
/*!
\class EntryBackup
Holds the backup of a tree node including children.
*/
class EntryBackup
{
protected:
ResourceModel *m_model;
int m_prefixIndex;
QString m_name;
EntryBackup(ResourceModel &model, int prefixIndex, const QString &name)
: m_model(&model), m_prefixIndex(prefixIndex), m_name(name) { }
public:
virtual void restore() const = 0;
virtual ~EntryBackup() { }
};
class RelativeResourceModel;
class ResourceView : public Utils::TreeView
......@@ -80,18 +60,10 @@ public:
LanguageProperty
};
explicit ResourceView(QUndoStack *history, QWidget *parent = 0);
explicit ResourceView(RelativeResourceModel *model, QUndoStack *history, QWidget *parent = 0);
~ResourceView();
bool load(const QString &fileName);
bool save();
QString contents() const;
QString errorMessage() const;
QString fileName() const;
void setFileName(const QString &fileName);
bool isDirty() const;
void setDirty(bool dirty);
bool isPrefix(const QModelIndex &index) const;
......@@ -126,7 +98,6 @@ protected:
signals:
void removeItem();
void dirtyChanged(bool b);
void itemActivated(const QString &fileName);
void showContextMenu(const QPoint &globalPos, const QString &fileName);
......
......@@ -31,15 +31,14 @@
#include "resourceeditorw.h"
#include "resourceeditorplugin.h"
#include "resourceeditorconstants.h"
#include "resourcefile_p.h"
#include <resourceeditor/qrceditor/qrceditor.h>
#include <aggregation/aggregate.h>
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/commandbutton.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/find/itemviewfind.h>
#include <utils/reloadpromptutils.h>
#include <utils/fileutils.h>
......@@ -62,14 +61,15 @@ enum { debugResourceEditorW = 0 };
ResourceEditorDocument::ResourceEditorDocument(ResourceEditorW *parent) :
ResourceEditorDocument::ResourceEditorDocument(QObject *parent) :
IDocument(parent),
m_blockDirtyChanged(false),
m_parent(parent)
m_model(new RelativeResourceModel(this))
{
setId(ResourceEditor::Constants::RESOURCEEDITOR_ID);
setMimeType(QLatin1String(ResourceEditor::Constants::C_RESOURCE_MIMETYPE));
setFilePath(FileName::fromString(parent->m_resourceEditor->fileName()));
connect(m_model, &RelativeResourceModel::dirtyChanged,
this, &ResourceEditorDocument::dirtyChanged);