Commit 0ecb083b authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Adapt LanguageModel for usage with Repository

parent 913b9417
......@@ -86,7 +86,7 @@ MainWindowEditor::MainWindowEditor(ContributorRepository *repository)
m_widget->resize(QSize(800, 600));
m_widget->rootContext()->setContextObject(new KLocalizedContext(m_widget));
m_widget->rootContext()->setContextProperty(QStringLiteral("g_repository"), m_repository);
m_widget->rootContext()->setContextProperty(QStringLiteral("editorSession"), m_repository);
m_widget->rootContext()->setContextProperty(QStringLiteral("g_editorSession"), m_editorSession);
// set starting screen
m_widget->setSource(QUrl(QStringLiteral("qrc:/artikulate/qml/Editor.qml")));
......
......@@ -34,18 +34,18 @@ class LanguageModel : public QSortFilterProxyModel
Q_PROPERTY(LanguageResourceView view READ view WRITE setView NOTIFY viewChanged)
public:
Q_ENUMS(LanguageResourceView);
enum LanguageResourceView {
NonEmptyContributorOnlyResources,
NonEmptyGhnsOnlyLanguages,
NonEmptyLanguages,
AllLanguages
};
Q_ENUM(LanguageResourceView)
explicit LanguageModel(QObject *parent = nullptr);
LanguageResourceModel * resourceModel() const;
void setResourceModel(LanguageResourceModel *resourceModel);
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
void setView(LanguageResourceView view);
LanguageResourceView view() const;
Q_INVOKABLE QVariant language(int index) const;
......
......@@ -22,8 +22,6 @@
#include "application.h"
#include "core/language.h"
#include "core/iresourcerepository.h"
#include "core/resources/languageresource.h"
#include "core/resources/courseresource.h"
#include <QAbstractListModel>
#include <QSignalMapper>
......@@ -58,13 +56,11 @@ void LanguageResourceModel::setResourceRepository(IResourceRepository *repositor
return;
}
beginResetModel();
if (m_repository) {
m_repository->disconnect(this);
}
m_repository = repository;
updateResources();
endResetModel();
updateDisplayedLanguages();
emit resourceRepositoryChanged();
}
......@@ -80,11 +76,11 @@ QVariant LanguageResourceModel::data(const QModelIndex &index, int role) const
return QVariant();
}
if (index.row() >= m_resources.count()) {
if (index.row() >= m_languages.count()) {
return QVariant();
}
Language * const language = m_resources.at(index.row())->language();
Language * const language = m_languages.at(index.row());
switch(role)
{
case Qt::DisplayRole:
......@@ -101,7 +97,7 @@ QVariant LanguageResourceModel::data(const QModelIndex &index, int role) const
case DataRole:
return QVariant::fromValue<QObject*>(language);
case CourseNumberRole:
return m_resources.count();
return m_languages.count();
default:
return QVariant();
}
......@@ -112,45 +108,44 @@ int LanguageResourceModel::rowCount(const QModelIndex &parent) const
if (parent.isValid()) {
return 0;
}
return m_resources.count();
return m_languages.count();
}
void LanguageResourceModel::onLanguageResourceAboutToBeAdded(LanguageResource *resource, int index)
void LanguageResourceModel::onLanguageAboutToBeAdded(Language *language, int index)
{
beginInsertRows(QModelIndex(), index, index);
m_resources.append(resource);
m_languages.append(language);
connect(resource->language(), SIGNAL(titleChanged()), m_signalMapper, SLOT(map()));
connect(resource->language(), SIGNAL(phonemesChanged()), m_signalMapper, SLOT(map()));
connect(resource->language(), SIGNAL(phonemeGroupsChanged()), m_signalMapper, SLOT(map()));
connect(language, SIGNAL(titleChanged()), m_signalMapper, SLOT(map()));
connect(language, SIGNAL(phonemesChanged()), m_signalMapper, SLOT(map()));
connect(language, SIGNAL(phonemeGroupsChanged()), m_signalMapper, SLOT(map()));
}
void LanguageResourceModel::onLanguageResourceAdded()
void LanguageResourceModel::onLanguageAdded()
{
updateMappings();
endInsertRows();
}
void LanguageResourceModel::onLanguageResourceAboutToBeRemoved(int index)
void LanguageResourceModel::onLanguageAboutToBeRemoved(int index)
{
//FIXME adapt to repository
// if (!m_repository) {
// return;
// }
if (!m_repository) {
return;
}
// LanguageResource *originalResource = m_repository->languages().at(index);
// int modelIndex = m_resources.indexOf(originalResource);
Language *originalLanguage = m_repository->languages().at(index);
int modelIndex = m_languages.indexOf(originalLanguage);
// if (modelIndex == -1) {
// qCWarning(ARTIKULATE_LOG) << "Cannot remove language from model, not registered";
// return;
// }
// beginRemoveRows(QModelIndex(), modelIndex, modelIndex);
// originalResource->disconnect(m_signalMapper);
// m_resources.removeAt(modelIndex);
if (modelIndex == -1) {
qCWarning(ARTIKULATE_LOG()) << "Cannot remove language from model, not registered";
return;
}
beginRemoveRows(QModelIndex(), modelIndex, modelIndex);
originalLanguage->disconnect(m_signalMapper);
m_languages.removeAt(modelIndex);
}
void LanguageResourceModel::onLanguageResourceRemoved()
void LanguageResourceModel::onLanguageRemoved()
{
endRemoveRows();
}
......@@ -177,16 +172,18 @@ void LanguageResourceModel::setView(LanguageModel::LanguageResourceView view)
if (m_view == view) {
return;
}
beginResetModel();
m_view = view;
updateResources();
endResetModel();
updateDisplayedLanguages();
}
void LanguageResourceModel::updateDisplayedLanguages()
{
beginResetModel();
updateResources();
m_languages.clear();
if (m_repository) {
m_languages = m_repository->languages();
}
updateMappings();
endResetModel();
}
......@@ -195,25 +192,10 @@ LanguageModel::LanguageResourceView LanguageResourceModel::view() const
return m_view;
}
void LanguageResourceModel::updateResources()
{
if (!m_repository) {
return;
}
m_resources.clear();
QVector<Language*> resources = m_repository->languages();
//FIXME complete switch to language resources
// for (LanguageResource *language : resources) {
// m_resources.append(language);
// }
updateMappings();
}
void LanguageResourceModel::updateMappings()
{
int languages = m_resources.count();
int languages = m_languages.count();
for (int i = 0; i < languages; i++) {
m_signalMapper->setMapping(m_resources.at(i), i);
m_signalMapper->setMapping(m_languages.at(i), i);
}
}
......@@ -25,7 +25,6 @@
#include "languagemodel.h"
class IResourceRepository;
class LanguageResource;
class Language;
class QSignalMapper;
......@@ -61,18 +60,17 @@ Q_SIGNALS:
void resourceRepositoryChanged();
private Q_SLOTS:
void onLanguageResourceAboutToBeAdded(LanguageResource *resource, int index);
void onLanguageResourceAdded();
void onLanguageResourceAboutToBeRemoved(int index);
void onLanguageResourceRemoved();
void onLanguageAboutToBeAdded(Language *resource, int index);
void onLanguageAdded();
void onLanguageAboutToBeRemoved(int index);
void onLanguageRemoved();
void emitLanguageChanged(int row);
void updateDisplayedLanguages();
private:
void updateResources();
void updateMappings();
IResourceRepository *m_repository;
QList<LanguageResource *> m_resources;
QVector<Language *> m_languages;
LanguageModel::LanguageResourceView m_view;
QSignalMapper *m_signalMapper;
};
......
......@@ -49,11 +49,11 @@ Item {
}
CourseModel {
id: courseModel
language: editorSession.language
language: g_editorSession.language
}
UnitModel {
id: selectedUnitModel
course: editorSession.course
course: g_editorSession.course
}
ColumnLayout {
......@@ -80,7 +80,7 @@ Item {
}
textRole: "title"
onCurrentIndexChanged: {
editorSession.skeleton = skeletonModel.skeleton(currentIndex)
g_editorSession.skeleton = skeletonModel.skeleton(currentIndex)
}
}
Button {
......@@ -89,7 +89,7 @@ Item {
text: i18n("Edit Prototype")
icon.name: "code-class"
checkable: true
onClicked: editorSession.editSkeleton = checked
onClicked: g_editorSession.editSkeleton = checked
}
Item { Layout.fillWidth: true }
Button {
......@@ -102,15 +102,15 @@ Item {
ToolTip.delay: 1000
ToolTip.timeout: 5000
ToolTip.text: i18n("Update the course with elements from prototype.")
onClicked: editorSession.updateCourseFromSkeleton()
onClicked: g_editorSession.updateCourseFromSkeleton()
}
CheckBox {
Layout.alignment: Qt.AlignRight
enabled: false//FIXME for now deactivating non-skeleton mode
text: i18n("Prototype Mode")
checked: editorSession.skeletonMode
checked: g_editorSession.skeletonMode
onClicked: {
editorSession.skeletonMode = !editorSession.skeletonMode
g_editorSession.skeletonMode = !g_editorSession.skeletonMode
}
}
}
......@@ -127,7 +127,7 @@ Item {
model: languageModel
textRole: "i18nTitle"
onCurrentIndexChanged: {
editorSession.language = languageModel.language(currentIndex)
g_editorSession.language = languageModel.language(currentIndex)
}
}
}
......@@ -137,12 +137,12 @@ Item {
if (buttonEditSkeleton.checked) {
return false
}
if (editorSession.skeletonMode && editorSession.course !== null) {
if (g_editorSession.skeletonMode && g_editorSession.course !== null) {
return false
}
if (!editorSession.skeletonMode
&& editorSession.language !== null
&& editorSession.course !== null
if (!g_editorSession.skeletonMode
&& g_editorSession.language !== null
&& g_editorSession.course !== null
) {
return false
}
......@@ -154,18 +154,18 @@ Item {
}
ComboBox { // course selection only necessary when we do not edit skeleton derived course
id: comboCourse
visible: !editorSession.skeletonMode
visible: !g_editorSession.skeletonMode
Layout.fillWidth: true
model: courseModel
textRole: "title"
onCurrentIndexChanged: {
if (courseModel.course(currentIndex)) {
editorSession.course = courseModel.course(currentIndex)
g_editorSession.course = courseModel.course(currentIndex)
}
}
onVisibleChanged: {
if (visible && courseModel.course(currentIndex)) {
editorSession.course = courseModel.course(currentIndex)
g_editorSession.course = courseModel.course(currentIndex)
}
}
}
......@@ -173,14 +173,14 @@ Item {
text: i18n("Create Course")
icon.name: "journal-new"
onClicked: {
editorSession.course = g_repository.createCourse(editorSession.language, editorSession.skeleton)
g_editorSession.course = g_repository.createCourse(g_editorSession.language, g_editorSession.skeleton)
}
}
Item { Layout.fillHeight: true } //dummy
}
RowLayout {
id: mainRow
visible: editorSession.course !== null
visible: g_editorSession.course !== null
Layout.fillHeight: true
ColumnLayout {
ScrollView {
......@@ -200,7 +200,7 @@ Item {
}
model: PhraseModel {
id: phraseModel
course: editorSession.course
course: g_editorSession.course
}
selection: ItemSelectionModel {
model: phraseTree.model
......@@ -215,21 +215,21 @@ Item {
}
onClicked: {
if (phraseModel.isPhrase(index)) {
editorSession.phrase = phraseModel.phrase(index)
g_editorSession.phrase = phraseModel.phrase(index)
} else {
editorSession.phrase = null
editorSession.unit = phraseModel.unit(index)
g_editorSession.phrase = null
g_editorSession.unit = phraseModel.unit(index)
}
}
Connections {
target: editorSession
target: g_editorSession
onPhraseChanged: {
if (editorSession.phrase === null) {
if (g_editorSession.phrase === null) {
return
}
phraseTree.expand(phraseModel.indexUnit(editorSession.phrase.unit))
phraseTree.expand(phraseModel.indexUnit(g_editorSession.phrase.unit))
phraseTree.selection.setCurrentIndex(
phraseModel.indexPhrase(editorSession.phrase),
phraseModel.indexPhrase(g_editorSession.phrase),
ItemSelectionModel.ClearAndSelect)
}
}
......@@ -237,7 +237,7 @@ Item {
}
Button { // add units only if skeleton
id: newUnitButton
visible: !editorSession.skeletonMode || editorSession.editSkeleton
visible: !g_editorSession.skeletonMode || g_editorSession.editSkeleton
icon.name: "list-add"
text: i18n("New Unit")
onClicked: phraseModel.course.createUnit()
......@@ -245,14 +245,14 @@ Item {
}
ColumnLayout {
UnitEditor {
visible: editorSession.unit !== null && editorSession.phrase === null
unit: editorSession.unit
editPhrases: editorSession.skeletonMode && editorSession.editSkeleton
visible: g_editorSession.unit !== null && g_editorSession.phrase === null
unit: g_editorSession.unit
editPhrases: g_editorSession.skeletonMode && g_editorSession.editSkeleton
}
PhraseEditor {
visible: editorSession.phrase !== null
phrase: editorSession.phrase
isSkeletonPhrase: editorSession.editSkeleton
visible: g_editorSession.phrase !== null
phrase: g_editorSession.phrase
isSkeletonPhrase: g_editorSession.editSkeleton
Layout.minimumWidth: Math.floor(main.width * 0.6)
Layout.fillHeight: true
}
......
......@@ -150,20 +150,20 @@ Item {
Layout.alignment: Qt.AlignBottom
width: 48
height: 48
enabled: editorSession.hasPreviousPhrase
enabled: g_editorSession.hasPreviousPhrase
icon.name: "go-previous"
onClicked: {
editorSession.switchToPreviousPhrase()
g_editorSession.switchToPreviousPhrase()
}
}
ToolButton {
Layout.alignment: Qt.AlignBottom
width: 48
height: 48
enabled: editorSession.hasNextPhrase
enabled: g_editorSession.hasNextPhrase
icon.name: "go-next"
onClicked: {
editorSession.switchToNextPhrase()
g_editorSession.switchToNextPhrase()
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment