Git commit eb2696c33941d0c62cd23fbb75b0fa3c7c957c55 by Pierre Stirnweiss. Committed on 17/01/2013 at 19:00. Pushed by pstirnweiss into branch 'calligra/2.6'. Fix crash on create new character style When creating a new character style, Words crashed because an null pointer was passed around and used. Also, the name of the style was not updated. BUG: 313233 REVIEW: 108444 M +3 -1 plugins/textshape/dialogs/CharacterGeneral.cpp M +9 -1 plugins/textshape/dialogs/FormattingPreview.cpp M +31 -6 plugins/textshape/dialogs/StyleManager.cpp M +2 -0 plugins/textshape/dialogs/StyleManager.h M +5 -8 plugins/textshape/dialogs/StylesModel.cpp http://commits.kde.org/calligra/eb2696c33941d0c62cd23fbb75b0fa3c7c957c55 diff --git a/plugins/textshape/dialogs/CharacterGeneral.cpp b/plugins/texts= hape/dialogs/CharacterGeneral.cpp index d3eead6..bb7ee45 100644 --- a/plugins/textshape/dialogs/CharacterGeneral.cpp +++ b/plugins/textshape/dialogs/CharacterGeneral.cpp @@ -74,6 +74,7 @@ CharacterGeneral::CharacterGeneral(QWidget *parent) m_languageTab->setVisible(false); = connect(widget.name, SIGNAL(textChanged(const QString &)), this, SIGNA= L(nameChanged(const QString&))); + m_nameHidden =3D false; } = void CharacterGeneral::hideStyleName(bool hide) @@ -114,8 +115,9 @@ void CharacterGeneral::save(KoCharacterStyle *style) { KoCharacterStyle *savingStyle; if (style =3D=3D 0) { - if (m_style =3D=3D 0) + if (m_style =3D=3D 0) { return; + } else savingStyle =3D m_style; } diff --git a/plugins/textshape/dialogs/FormattingPreview.cpp b/plugins/text= shape/dialogs/FormattingPreview.cpp index 1ed3b83..1648649 100644 --- a/plugins/textshape/dialogs/FormattingPreview.cpp +++ b/plugins/textshape/dialogs/FormattingPreview.cpp @@ -84,6 +84,9 @@ void FormattingPreview::setText(const QString &sampleText) //Character properties void FormattingPreview::setCharacterStyle(const KoCharacterStyle* style) { + if (!style) { + return; + } if (m_characterStyle) { delete m_characterStyle; } @@ -97,6 +100,9 @@ void FormattingPreview::setCharacterStyle(const KoCharac= terStyle* style) = void FormattingPreview::setParagraphStyle(const KoParagraphStyle *style) { + if (!style) { + return; + } if (m_paragraphStyle) { delete m_paragraphStyle; } @@ -120,7 +126,9 @@ void FormattingPreview::paintEvent(QPaintEvent *event) QRect rectang =3D contentsRect(); = p->fillRect(rectang, QBrush(QColor(Qt::white))); - p->drawImage(rectang, m_thumbnailer->thumbnail(m_characterStyle,m_para= graphStyle,rectang.size(),m_previewLayoutRequired, KoStyleThumbnailer::NoFl= ags)); + if (m_characterStyle) { + p->drawImage(rectang, m_thumbnailer->thumbnail(m_characterStyle,m_= paragraphStyle,rectang.size(),m_previewLayoutRequired, KoStyleThumbnailer::= NoFlags)); + } = m_previewLayoutRequired =3D false; = diff --git a/plugins/textshape/dialogs/StyleManager.cpp b/plugins/textshape= /dialogs/StyleManager.cpp index c561d35..04264e3 100644 --- a/plugins/textshape/dialogs/StyleManager.cpp +++ b/plugins/textshape/dialogs/StyleManager.cpp @@ -47,6 +47,7 @@ StyleManager::StyleManager(QWidget *parent) , m_blockStyleChangeSignals(false) , m_unappliedStyleChanges(false) , m_currentStyleChanged(false) + , m_nonExistingStyleId(8094) { widget.setupUi(this); layout()->setMargin(0); @@ -131,6 +132,9 @@ void StyleManager::setStyleManager(KoStyleManager *sm) = void StyleManager::setParagraphStyle(KoParagraphStyle *style) { + if (!style) { + return; + } m_blockStyleChangeSignals =3D true; m_selectedCharStyle =3D 0; m_selectedParagStyle =3D style; @@ -162,6 +166,9 @@ void StyleManager::setParagraphStyle(KoParagraphStyle *= style) = void StyleManager::setCharacterStyle(KoCharacterStyle *style, bool canDele= te) { + if (!style) { + return; + } m_blockStyleChangeSignals =3D true; m_selectedParagStyle =3D 0; m_selectedCharStyle =3D style; @@ -170,8 +177,8 @@ void StyleManager::setCharacterStyle(KoCharacterStyle *= style, bool canDelete) widget.characterStylePage->save(); KoCharacterStyle *localStyle; = - if (m_draftParagraphStyles.contains(style->styleId())) { - localStyle =3D m_draftParagraphStyles[style->styleId()]; + if (m_draftCharacterStyles.contains(style->styleId())) { + localStyle =3D m_draftCharacterStyles[style->styleId()]; } else if (!m_alteredCharacterStyles.contains(style->styleId())) { localStyle =3D style->clone(); @@ -207,6 +214,7 @@ void StyleManager::save() m_styleManager->beginEdit(); = m_paragraphStylesModel->clearDraftStyles(); // clear draft styles in S= tyle Model. + m_characterStylesModel->clearDraftStyles(); foreach(KoParagraphStyle *style, m_draftParagraphStyles.values()) { m_styleManager->add(style); } @@ -238,6 +246,7 @@ void StyleManager::save() = m_alteredCharacterStyles.insert(m_selectedCharStyle->styleId(), lo= calStyle); = + widget.characterStylesListView->setCurrentIndex(m_characterStylesM= odel->indexForCharacterStyle(*m_selectedCharStyle)); widget.characterStylePage->setStyle(localStyle); } else @@ -248,6 +257,7 @@ void StyleManager::save() = m_alteredParagraphStyles.insert(m_selectedParagStyle->styleId(), l= ocalStyle); = + widget.paragraphStylesListView->setCurrentIndex(m_paragraphStylesM= odel->indexForParagraphStyle(*m_selectedParagStyle)); widget.paragraphStylePage->setStyle(localStyle); } else @@ -342,7 +352,14 @@ void StyleManager::buttonNewPressed() return; } if (widget.tabs->indexOf(widget.paragraphStylesListView) =3D=3D widget= .tabs->currentIndex()){ - KoParagraphStyle *newStyle =3D m_selectedParagStyle->clone(); + KoParagraphStyle *newStyle; + if (m_selectedParagStyle) { + newStyle =3D m_selectedParagStyle->clone(); + } + else { + newStyle =3D new KoParagraphStyle(); + newStyle->setStyleId(-m_nonExistingStyleId++); + } newStyle->setName(i18n("New Style")); m_paragraphStylesModel->addDraftParagraphStyle(newStyle); m_draftParagraphStyles.insert(newStyle->styleId(), newStyle); @@ -350,7 +367,14 @@ void StyleManager::buttonNewPressed() widget.paragraphStylePage->selectName(); } else { - KoCharacterStyle *newStyle =3D m_selectedCharStyle->clone(); + KoCharacterStyle *newStyle; + if (m_selectedCharStyle) { + newStyle =3D m_selectedCharStyle->clone(); + } + else { + newStyle =3D new KoCharacterStyle(); + newStyle->setStyleId(-m_nonExistingStyleId++); + } newStyle->setName(i18n("New Style")); m_characterStylesModel->addDraftCharacterStyle(newStyle); m_draftCharacterStyles.insert(newStyle->styleId(), newStyle); @@ -419,9 +443,10 @@ bool StyleManager::checkUniqueStyleName() QList styleListChar =3D m_characterStylesModel->StyleList(); QList::iterator iterChar =3D styleListChar.begin(); for ( ; iterChar !=3D styleListChar.end(); ++iterChar) { - KoCharacterStyle *temp =3D m_styleManager->characterStyle(*ite= rChar);; - if (!temp && m_draftCharacterStyles.contains(*iterChar)) + KoCharacterStyle *temp =3D m_styleManager->characterStyle(*ite= rChar); + if (!temp && m_draftCharacterStyles.contains(*iterChar)){ temp =3D m_draftCharacterStyles[*iterChar]; + } = if (widget.characterStylePage->styleName() =3D=3D temp->name()= ) { if (temp !=3D m_selectedCharStyle) { diff --git a/plugins/textshape/dialogs/StyleManager.h b/plugins/textshape/d= ialogs/StyleManager.h index a45a58a..66671a7 100644 --- a/plugins/textshape/dialogs/StyleManager.h +++ b/plugins/textshape/dialogs/StyleManager.h @@ -81,6 +81,8 @@ private: bool m_blockStyleChangeSignals; bool m_unappliedStyleChanges; bool m_currentStyleChanged; + + int m_nonExistingStyleId; }; = #endif diff --git a/plugins/textshape/dialogs/StylesModel.cpp b/plugins/textshape/= dialogs/StylesModel.cpp index 80ea48d..7559917 100644 --- a/plugins/textshape/dialogs/StylesModel.cpp +++ b/plugins/textshape/dialogs/StylesModel.cpp @@ -378,10 +378,10 @@ void StylesModel::updateCharacterStyles() = foreach(KoCharacterStyle *style, styles) { if (style !=3D m_styleManager->defaultCharacterStyle()) { //The de= fault character style is not user selectable. It only provides individual p= roperty defaults and is not a style per say. - m_styleList.append(style->styleId()); + m_styleList.append(style->styleId()); m_styleMapper->setMapping(style, style->styleId()); connect(style, SIGNAL(nameChanged(const QString&)), m_styleMap= per, SLOT(map())); - } + } } = endResetModel(); @@ -440,7 +440,7 @@ void StylesModel::updateName(int styleId) } if (oldIndex !=3D newIndex) { // beginMoveRows needs the index where it would be pla= ced when it is still in the old position - // so add one when newIndex > oldIndex = + // so add one when newIndex > oldIndex beginMoveRows(QModelIndex(), oldIndex, oldIndex, QMode= lIndex(), newIndex > oldIndex ? newIndex + 1 : newIndex); m_styleList.removeAt(oldIndex); m_styleList.insert(newIndex, styleId); @@ -477,7 +477,7 @@ void StylesModel::updateName(int styleId) } if (oldIndex !=3D newIndex) { // beginMoveRows needs the index where it would be pla= ced when it is still in the old position - // so add one when newIndex > oldIndex = + // so add one when newIndex > oldIndex beginMoveRows(QModelIndex(), oldIndex, oldIndex, QMode= lIndex(), newIndex > oldIndex ? newIndex + 1 : newIndex); m_styleList.removeAt(oldIndex); m_styleList.insert(newIndex, styleId); @@ -520,10 +520,7 @@ void StylesModel::addDraftParagraphStyle(KoParagraphSt= yle *style) = void StylesModel::addDraftCharacterStyle(KoCharacterStyle *style) { - if (m_draftCharStyleList.count() =3D=3D 0) // we have a character styl= e "m_defaultCharacterStyle" with style id -1 in style model. - style->setStyleId(-(m_draftCharStyleList.count()+2)); - else - style->setStyleId(-(m_draftCharStyleList.count()+1)); + style->setStyleId(-(m_draftCharStyleList.count()+2)); //id -1 correspo= nds to style "None" m_draftCharStyleList.insert(style->styleId(), style); addCharacterStyle(style); }