[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra/calligra/2.6] plugins/textshape/dialogs: Fix crash on create new character style
From:       Pierre Stirnweiss <pstirnweiss () googlemail ! com>
Date:       2013-01-18 20:04:55
Message-ID: 20130118200455.C65FBA60C8 () git ! kde ! org
[Download RAW message or body]

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/textshape/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, \
SIGNAL(nameChanged(const QString&))); +    m_nameHidden = false;
 }
 
 void CharacterGeneral::hideStyleName(bool hide)
@@ -114,8 +115,9 @@ void CharacterGeneral::save(KoCharacterStyle *style)
 {
     KoCharacterStyle *savingStyle;
     if (style == 0) {
-        if (m_style == 0)
+        if (m_style == 0) {
             return;
+        }
         else
             savingStyle = m_style;
     }
diff --git a/plugins/textshape/dialogs/FormattingPreview.cpp \
b/plugins/textshape/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 KoCharacterStyle* \
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 = contentsRect();
 
     p->fillRect(rectang, QBrush(QColor(Qt::white)));
-    p->drawImage(rectang, \
m_thumbnailer->thumbnail(m_characterStyle,m_paragraphStyle,rectang.size(),m_previewLayoutRequired, \
KoStyleThumbnailer::NoFlags)); +    if (m_characterStyle) {
+        p->drawImage(rectang, \
m_thumbnailer->thumbnail(m_characterStyle,m_paragraphStyle,rectang.size(),m_previewLayoutRequired, \
KoStyleThumbnailer::NoFlags)); +    }
 
     m_previewLayoutRequired = 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 = true;
     m_selectedCharStyle = 0;
     m_selectedParagStyle = style;
@@ -162,6 +166,9 @@ void StyleManager::setParagraphStyle(KoParagraphStyle *style)
 
 void StyleManager::setCharacterStyle(KoCharacterStyle *style, bool canDelete)
 {
+    if (!style) {
+        return;
+    }
     m_blockStyleChangeSignals = true;
     m_selectedParagStyle = 0;
     m_selectedCharStyle = style;
@@ -170,8 +177,8 @@ void StyleManager::setCharacterStyle(KoCharacterStyle *style, \
bool canDelete)  widget.characterStylePage->save();
     KoCharacterStyle *localStyle;
 
-    if (m_draftParagraphStyles.contains(style->styleId())) {
-        localStyle = m_draftParagraphStyles[style->styleId()];
+    if (m_draftCharacterStyles.contains(style->styleId())) {
+        localStyle = m_draftCharacterStyles[style->styleId()];
     }
     else if (!m_alteredCharacterStyles.contains(style->styleId())) {
         localStyle = style->clone();
@@ -207,6 +214,7 @@ void StyleManager::save()
     m_styleManager->beginEdit();
 
     m_paragraphStylesModel->clearDraftStyles(); // clear draft styles in Style \
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(), localStyle);
 
+        widget.characterStylesListView->setCurrentIndex(m_characterStylesModel->indexForCharacterStyle(*m_selectedCharStyle));
  widget.characterStylePage->setStyle(localStyle);
     }
     else
@@ -248,6 +257,7 @@ void StyleManager::save()
 
         m_alteredParagraphStyles.insert(m_selectedParagStyle->styleId(), \
localStyle);  
+        widget.paragraphStylesListView->setCurrentIndex(m_paragraphStylesModel->indexForParagraphStyle(*m_selectedParagStyle));
  widget.paragraphStylePage->setStyle(localStyle);
     }
     else
@@ -342,7 +352,14 @@ void StyleManager::buttonNewPressed()
         return;
     }
     if (widget.tabs->indexOf(widget.paragraphStylesListView) == \
                widget.tabs->currentIndex()){
-        KoParagraphStyle *newStyle = m_selectedParagStyle->clone();
+        KoParagraphStyle *newStyle;
+        if (m_selectedParagStyle) {
+            newStyle = m_selectedParagStyle->clone();
+        }
+        else {
+            newStyle = 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 = m_selectedCharStyle->clone();
+        KoCharacterStyle *newStyle;
+        if (m_selectedCharStyle) {
+            newStyle = m_selectedCharStyle->clone();
+        }
+        else {
+            newStyle = 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<int> styleListChar = m_characterStylesModel->StyleList();
         QList<int>::iterator iterChar = styleListChar.begin();
         for ( ; iterChar != styleListChar.end(); ++iterChar) {
-            KoCharacterStyle *temp = m_styleManager->characterStyle(*iterChar);;
-            if (!temp && m_draftCharacterStyles.contains(*iterChar))
+            KoCharacterStyle *temp = m_styleManager->characterStyle(*iterChar);
+            if (!temp && m_draftCharacterStyles.contains(*iterChar)){
                 temp = m_draftCharacterStyles[*iterChar];
+            }
 
             if (widget.characterStylePage->styleName() == temp->name()) {
                 if (temp != m_selectedCharStyle) {
diff --git a/plugins/textshape/dialogs/StyleManager.h \
b/plugins/textshape/dialogs/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 != m_styleManager->defaultCharacterStyle()) { //The default \
character style is not user selectable. It only provides individual property 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_styleMapper, \
                SLOT(map()));
-	}
+        }
     }
 
     endResetModel();
@@ -440,7 +440,7 @@ void StylesModel::updateName(int styleId)
                 }
                 if (oldIndex != newIndex) {
                     // beginMoveRows needs the index where it would be placed when \
                it is still in the old position
-                    // so add one when newIndex > oldIndex 
+                    // so add one when newIndex > oldIndex
                     beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), \
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 != newIndex) {
                     // beginMoveRows needs the index where it would be placed when \
                it is still in the old position
-                    // so add one when newIndex > oldIndex 
+                    // so add one when newIndex > oldIndex
                     beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), \
newIndex > oldIndex ? newIndex + 1 : newIndex);  m_styleList.removeAt(oldIndex);
                     m_styleList.insert(newIndex, styleId);
@@ -520,10 +520,7 @@ void StylesModel::addDraftParagraphStyle(KoParagraphStyle \
*style)  
 void StylesModel::addDraftCharacterStyle(KoCharacterStyle *style)
 {
-    if (m_draftCharStyleList.count() == 0) // we have a character style \
                "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 corresponds to \
style "None"  m_draftCharStyleList.insert(style->styleId(), style);
     addCharacterStyle(style);
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic