Git commit 9b0e48951612e87322ec9cad27bb82f7da05bf49 by Pierre Stirnweiss. Committed on 21/01/2013 at 20:19. Pushed by pstirnweiss into branch 'master'. Fix crash in Krita on the new styles combo In Krita, there is no style loaded from a file. Instead, Krita is using the default paragraph style. The new styles combo used to hide this style. This patch creates a default set of styles for these type of applications. Also the default style is not hidden in the combo. REVIEW: 108532 BUG: 313414 Cherry-picked from textshape-stylesWidget-PierreSt Conflicts: plugins/textshape/dialogs/DockerStylesComboModel.cpp M +4 -0 libs/kotext/opendocument/KoTextSharedLoadingData.cpp M +77 -16 libs/kotext/styles/KoStyleManager.cpp M +14 -0 libs/kotext/styles/KoStyleManager.h M +3 -1 plugins/textshape/TextShapeFactory.cpp M +9 -6 plugins/textshape/dialogs/DockerStylesComboModel.cpp M +1 -1 plugins/textshape/dialogs/StylesModel.cpp http://commits.kde.org/calligra/9b0e48951612e87322ec9cad27bb82f7da05bf49 diff --git a/libs/kotext/opendocument/KoTextSharedLoadingData.cpp b/libs/ko= text/opendocument/KoTextSharedLoadingData.cpp index dc797ed..a8b6d9c 100644 --- a/libs/kotext/opendocument/KoTextSharedLoadingData.cpp +++ b/libs/kotext/opendocument/KoTextSharedLoadingData.cpp @@ -145,6 +145,10 @@ void KoTextSharedLoadingData::addDefaultParagraphStyle= (KoShapeLoadingContext &co = void KoTextSharedLoadingData::loadOdfStyles(KoShapeLoadingContext &shapeCo= ntext, KoStyleManager *styleManager) { + if (styleManager) { + styleManager->useLoadedStyles(); + } + KoOdfLoadingContext &context =3D shapeContext.odfLoadingContext(); = // only add styles of office:styles to the style manager diff --git a/libs/kotext/styles/KoStyleManager.cpp b/libs/kotext/styles/KoS= tyleManager.cpp index c2a64e9..4110284 100644 --- a/libs/kotext/styles/KoStyleManager.cpp +++ b/libs/kotext/styles/KoStyleManager.cpp @@ -57,7 +57,7 @@ class KoStyleManager::Private { public: - Private() : defaultCharacterStyle(0), defaultParagraphStyle(0), defaul= tListStyle(0), defaultOutlineStyle(0), outlineStyle(0), undoStack(0), chang= eCommand(0) + Private() : defaultCharacterStyle(0), defaultParagraphStyle(0), defaul= tListStyle(0), defaultOutlineStyle(0), outlineStyle(0), undoStack(0), chang= eCommand(0), usingDefaultSet(false) { } ~Private() { @@ -92,6 +92,8 @@ public: KUndo2Stack *undoStack; ChangeStylesMacroCommand *changeCommand; = + bool usingDefaultSet; + QVector m_usedCharacterStyles; QVector m_usedParagraphStyles; }; @@ -132,21 +134,7 @@ KoStyleManager::KoStyleManager(QObject *parent) } = //default styles for ToCs - int maxOutLineLevel =3D 10; - for (int outlineLevel =3D 1; outlineLevel <=3D maxOutLineLevel; outlin= eLevel++) { - KoParagraphStyle *style =3D new KoParagraphStyle(); - style->setName("Contents " + QString::number(outlineLevel)); - style->setLeftMargin(QTextLength(QTextLength::FixedLength, (outlin= eLevel - 1) * 8)); - add(style); - d->defaultToCEntriesStyleId.append(style->styleId()); - } - - for (int typeIndex =3D 0; typeIndex < KoOdfBibliographyConfiguration::= bibTypes.size(); typeIndex++) { - KoParagraphStyle *style =3D new KoParagraphStyle(); - style->setName("Bibliography " + KoOdfBibliographyConfiguration::b= ibTypes.at(typeIndex)); - add(style); - d->defaultBibEntriesStyleId.append(style->styleId()); - } + createTOCStyles(); = d->footNotesConfiguration =3D 0; d->endNotesConfiguration =3D 0; @@ -1169,4 +1157,77 @@ KoTextTableTemplate *KoStyleManager::tableTemplate(i= nt id) const return d->tableTemplates.value(id, 0); } = +void KoStyleManager::createDefaultSet() +{ + d->paragStyles.clear(); + d->usingDefaultSet =3D true; + + KoParagraphStyle *paragStyle =3D new KoParagraphStyle(); + paragStyle->setName(i18n("Standard")); + paragStyle->setFontPointSize(12); + add(paragStyle); + d->defaultParagraphStyle =3D paragStyle; + + paragStyle =3D new KoParagraphStyle(); + paragStyle->setName(i18n("Document title")); + paragStyle->setFontPointSize(26); + paragStyle->setFontWeight(75); + paragStyle->setAlignment(Qt::AlignHCenter); + paragStyle->setNextStyle(d->defaultParagraphStyle->styleId()); + add(paragStyle); + + paragStyle =3D new KoParagraphStyle(); + paragStyle->setName(i18n("Head 1")); + paragStyle->setFontPointSize(20); + paragStyle->setFontWeight(75); + paragStyle->setNextStyle(d->defaultParagraphStyle->styleId()); + add(paragStyle); + + paragStyle =3D new KoParagraphStyle(); + paragStyle->setName(i18n("Head 2")); + paragStyle->setFontPointSize(16); + paragStyle->setFontWeight(75); + paragStyle->setNextStyle(d->defaultParagraphStyle->styleId()); + add(paragStyle); +} + +bool KoStyleManager::isUsingDefaultSet() +{ + return d->usingDefaultSet; +} + +void KoStyleManager::useLoadedStyles() +{ + KoParagraphStyle *style; + foreach(style, d->paragStyles.values()) { + disconnect(style, SIGNAL(styleApplied(const KoParagraphStyle*)), t= his, SLOT(slotAppliedStyle(const KoParagraphStyle*))); + } + + d->paragStyles.clear(); + d->usingDefaultSet =3D false; + + add(d->defaultParagraphStyle); + + createTOCStyles(); +} + +void KoStyleManager::createTOCStyles() +{ + int maxOutLineLevel =3D 10; + for (int outlineLevel =3D 1; outlineLevel <=3D maxOutLineLevel; outlin= eLevel++) { + KoParagraphStyle *style =3D new KoParagraphStyle(); + style->setName("Contents " + QString::number(outlineLevel)); + style->setLeftMargin(QTextLength(QTextLength::FixedLength, (outlin= eLevel - 1) * 8)); + add(style); + d->defaultToCEntriesStyleId.append(style->styleId()); + } + + for (int typeIndex =3D 0; typeIndex < KoOdfBibliographyConfiguration::= bibTypes.size(); typeIndex++) { + KoParagraphStyle *style =3D new KoParagraphStyle(); + style->setName("Bibliography " + KoOdfBibliographyConfiguration::b= ibTypes.at(typeIndex)); + add(style); + d->defaultBibEntriesStyleId.append(style->styleId()); + } +} + #include diff --git a/libs/kotext/styles/KoStyleManager.h b/libs/kotext/styles/KoSty= leManager.h index c44ba32..c097080 100644 --- a/libs/kotext/styles/KoStyleManager.h +++ b/libs/kotext/styles/KoStyleManager.h @@ -456,6 +456,18 @@ public: QVector usedCharacterStyles() const; QVector usedParagraphStyles() const; = + /** Creates a set of text styles to be used by applications which do n= ot use full blown styling (loaded from an odt for example). + * For example Krita. These applications do not create a KoStyleManage= r by default. When they create a textshape, a KoStyleManager is created. + * The textshape factory will then call this method to generate a defa= ult set of styles. This will also set a flag (isUsingDefaultSet) which will= be tested in the + * textshape ui in order not to hide the Standard style (default). + * TODO: maybe find a better system for all this. + */ + + void createDefaultSet(); + bool isUsingDefaultSet(); + + void useLoadedStyles(); + signals: void styleAdded(KoParagraphStyle*); void styleAdded(KoCharacterStyle*); @@ -538,6 +550,8 @@ public slots: void slotAppliedStyle(const KoParagraphStyle*); = private: + void createTOCStyles(); + friend class ChangeFollower; friend class ChangeStylesMacroCommand; void remove(ChangeFollower *cf); diff --git a/plugins/textshape/TextShapeFactory.cpp b/plugins/textshape/Tex= tShapeFactory.cpp index d7e8fe8..56829cb 100644 --- a/plugins/textshape/TextShapeFactory.cpp +++ b/plugins/textshape/TextShapeFactory.cpp @@ -153,7 +153,9 @@ void TextShapeFactory::newDocumentResourceManager(KoDoc= umentResourceManager *man manager->setUndoStack(new KUndo2Stack(manager)); } if (!manager->hasResource(KoText::StyleManager)) { - variant.setValue(new KoStyleManager(manager)); + KoStyleManager *styleManager =3D new KoStyleManager(manager); + styleManager->createDefaultSet(); + variant.setValue(styleManager); manager->setResource(KoText::StyleManager, variant); } if (!manager->imageCollection()) diff --git a/plugins/textshape/dialogs/DockerStylesComboModel.cpp b/plugins= /textshape/dialogs/DockerStylesComboModel.cpp index a669b77..69efa70 100644 --- a/plugins/textshape/dialogs/DockerStylesComboModel.cpp +++ b/plugins/textshape/dialogs/DockerStylesComboModel.cpp @@ -119,7 +119,7 @@ void DockerStylesComboModel::setStyleManager(KoStyleMan= ager *sm) for ( ; begin !=3D m_usedStyles.end(); ++begin) { if (m_sourceModel->index(*begin, 0, QModelIndex()).int= ernalId() !=3D -1) { //styleNone (internalId=3D-1) is a virtual style provi= ded only for the UI. it does not exist in KoStyleManager KoCharacterStyle *s =3D m_styleManager->characterS= tyle(m_sourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(compareStyle->n= ame(), s->name()) < 0) { + if (!s || KStringHandler::naturalCompare(compareSt= yle->name(), s->name()) < 0) { break; } } @@ -138,7 +138,7 @@ void DockerStylesComboModel::setStyleManager(KoStyleMan= ager *sm) for ( ; begin !=3D m_usedStyles.end(); ++begin) { if (m_sourceModel->index(*begin, 0, QModelIndex()).int= ernalId() !=3D -1) { //styleNone (internalId=3D-1) is a virtual style provi= ded only for the UI. it does not exist in KoStyleManager KoParagraphStyle *s =3D m_styleManager->paragraphS= tyle(m_sourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(compareStyle->n= ame(), s->name()) < 0) { + if (!s || KStringHandler::naturalCompare(compareSt= yle->name(), s->name()) < 0) { break; } } @@ -153,6 +153,9 @@ void DockerStylesComboModel::setStyleManager(KoStyleMan= ager *sm) = void DockerStylesComboModel::styleApplied(const KoCharacterStyle *style) { + if (!style) { + return; + } if (!m_usedStylesId.contains(style->styleId())) { m_usedStylesId.append(style->styleId()); if (m_sourceModel->stylesType() =3D=3D AbstractStylesModel::Charac= terStyle) { @@ -160,7 +163,7 @@ void DockerStylesComboModel::styleApplied(const KoChara= cterStyle *style) for ( ; begin !=3D m_usedStyles.end(); ++begin) { if (m_sourceModel->index(*begin, 0, QModelIndex()).interna= lId() !=3D -1) { //styleNone (internalId=3D-1) is a virtual style provided = only for the UI. it does not exist in KoStyleManager KoCharacterStyle *s =3D m_styleManager->characterStyle= (m_sourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(style->name(), s->n= ame()) < 0) { + if (!s || KStringHandler::naturalCompare(style->name()= , s->name()) < 0) { break; } } @@ -171,7 +174,7 @@ void DockerStylesComboModel::styleApplied(const KoChara= cterStyle *style) QVector::iterator begin =3D m_usedStyles.begin(); for ( ; begin !=3D m_usedStyles.end(); ++begin) { KoParagraphStyle *s =3D m_styleManager->paragraphStyle(m_s= ourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(style->name(), s->name(= )) < 0) { + if (!s || KStringHandler::naturalCompare(style->name(), s-= >name()) < 0) { break; } } @@ -215,7 +218,7 @@ void DockerStylesComboModel::createMapping() QVector::iterator begin =3D m_unusedStyles.be= gin(); for ( ; begin !=3D m_unusedStyles.end(); ++begin) { KoParagraphStyle *style =3D m_styleManager->pa= ragraphStyle(m_sourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(paragStyle-= >name(), style->name()) < 0) { + if (!style || KStringHandler::naturalCompare(p= aragStyle->name(), style->name()) < 0) { break; } } @@ -233,7 +236,7 @@ void DockerStylesComboModel::createMapping() QVector::iterator begin =3D m_unusedStyles.be= gin(); for ( ; begin !=3D m_unusedStyles.end(); ++begin) { KoCharacterStyle *style =3D m_styleManager->ch= aracterStyle(m_sourceModel->index(*begin, 0, QModelIndex()).internalId()); - if (KStringHandler::naturalCompare(charStyle->= name(), style->name()) < 0) { + if (!style || KStringHandler::naturalCompare(c= harStyle->name(), style->name()) < 0) { break; } } diff --git a/plugins/textshape/dialogs/StylesModel.cpp b/plugins/textshape/= dialogs/StylesModel.cpp index 2ac95fa..3926e34 100644 --- a/plugins/textshape/dialogs/StylesModel.cpp +++ b/plugins/textshape/dialogs/StylesModel.cpp @@ -353,7 +353,7 @@ void StylesModel::updateParagraphStyles() qSort(styles.begin(), styles.end(), sortParagraphStyleByName); = foreach(KoParagraphStyle *style, styles) { - if (style !=3D m_styleManager->defaultParagraphStyle()) { //The de= fault character style is not user selectable. It only provides individual p= roperty defaults and is not a style per say. + if (style !=3D m_styleManager->defaultParagraphStyle() || m_styleM= anager->isUsingDefaultSet()) { //If we are not using the default set (for a= pplications like Krita), the default paragraph style is not user selectable= . It only provides individual property defaults and is not a style per say. m_styleList.append(style->styleId()); m_styleMapper->setMapping(style, style->styleId()); connect(style, SIGNAL(nameChanged(const QString&)), m_styleMap= per, SLOT(map()));