Commit 4a46fc51 authored by Thorsten Zachmann's avatar Thorsten Zachmann
Browse files

Rework stylesmanager

This fixes various crash conditions which where present in the styles manager.
It used a different model for the style manager that handles the temporary styles
during they are modified and not yet saved.

REVIEW: 109651
BUG: 314516
parent ebe09478
......@@ -553,6 +553,7 @@ private:
Private * const d;
};
Q_DECLARE_METATYPE(KoCharacterStyle *)
Q_DECLARE_METATYPE(QSharedPointer<KoCharacterStyle>)
#endif
......@@ -102,7 +102,7 @@ QImage KoStyleThumbnailer::thumbnail(KoParagraphStyle *style, QSize size, bool r
if (!size.isValid() || size.isNull()) {
size = d->defaultSize;
}
QString imageKey = "p_" + QString::number(style->styleId()) + "_" + QString::number(size.width()) + "_" + QString::number(size.height());
QString imageKey = "p_" + QString::number(reinterpret_cast<unsigned long>(style)) + "_" + QString::number(size.width()) + "_" + QString::number(size.height());
if (!recreateThumbnail && d->thumbnailCache.object(imageKey)) {
return QImage(*(d->thumbnailCache.object(imageKey)));
......@@ -150,12 +150,16 @@ QImage KoStyleThumbnailer::thumbnail(KoCharacterStyle *characterStyle, KoParagra
} else if ((! (flags & UseStyleNameText)) && d->thumbnailText.isEmpty()) {
return QImage();
}
else if (characterStyle == 0) {
return QImage();
}
if (!size.isValid() || size.isNull()) {
size = d->defaultSize;
}
int paragraphStyleId = (paragraphStyle)?paragraphStyle->styleId():0;
QString imageKey = "c_" + QString::number(characterStyle->styleId()) + "_" + "p_" + QString::number(paragraphStyleId) + "_" + QString::number(size.width()) + "_" + QString::number(size.height());
QString imageKey = "c_" + QString::number(reinterpret_cast<unsigned long>(characterStyle)) + "_"
+ "p_" + QString::number(reinterpret_cast<unsigned long>(paragraphStyle)) + "_"
+ QString::number(size.width()) + "_" + QString::number(size.height());
if (!recreateThumbnail && d->thumbnailCache.object(imageKey)) {
return QImage(*(d->thumbnailCache.object(imageKey)));
......@@ -282,13 +286,13 @@ void KoStyleThumbnailer::layoutThumbnail(QSize size, QImage *im, KoStyleThumbnai
void KoStyleThumbnailer::removeFromCache(KoParagraphStyle *style)
{
QString imageKey = "p_" + QString::number(style->styleId()) + "_";
QString imageKey = "p_" + QString::number(reinterpret_cast<unsigned long>(style)) + "_";
removeFromCache(imageKey);
}
void KoStyleThumbnailer::removeFromCache(KoCharacterStyle *style)
{
QString imageKey = "c_" + QString::number(style->styleId()) + "_";
QString imageKey = "c_" + QString::number(reinterpret_cast<unsigned long>(style)) + "_";
removeFromCache(imageKey);
}
......
......@@ -65,6 +65,8 @@ SET ( textshape_SRCS
dialogs/ParagraphDropCaps.cpp
dialogs/ListsSpinBox.cpp
dialogs/StylesModel.cpp
dialogs/StylesManagerModel.cpp
dialogs/StylesSortFilterProxyModel.cpp
dialogs/AbstractStylesModel.cpp
dialogs/StylesFilteredModelBase.cpp
dialogs/ValidParentStylesProxyModel.cpp
......
......@@ -183,4 +183,9 @@ int CharacterGeneral::nextStyleId()
return m_styleManager->paragraphStyle(m_paragraphStyleModel->index(widget.nextStyle->currentIndex()).internalId())->styleId();
}
KoCharacterStyle *CharacterGeneral::style() const
{
return m_style;
}
#include <CharacterGeneral.moc>
......@@ -48,6 +48,8 @@ public:
void updateNextStyleCombo(KoParagraphStyle *style); // set current style in next style combo
int nextStyleId(); //return the current style id in next style combo
KoCharacterStyle *style() const;
public slots:
void save(KoCharacterStyle *style = 0);
......
......@@ -219,4 +219,9 @@ void ParagraphGeneral::setStyleManager(KoStyleManager *sm)
m_paragraphInheritedStyleModel->setStyleManager(m_styleManager);
}
KoParagraphStyle *ParagraphGeneral::style() const
{
return m_style;
}
#include <ParagraphGeneral.moc>
......@@ -57,6 +57,8 @@ public:
KoImageCollection *imageCollection();
void setStyleManager(KoStyleManager *sm);
KoParagraphStyle *style() const;
public slots:
void save(KoParagraphStyle *style = 0);
......
This diff is collapsed.
/* This file is part of the KDE project
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2013 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -24,7 +25,8 @@
#include <QWidget>
class StylesModel;
class StylesManagerModel;
class StylesSortFilterProxyModel;
class KoStyleManager;
class KoStyleThumbnailer;
class KoParagraphStyle;
......@@ -53,34 +55,34 @@ public slots:
bool unappliedStyleChanges();
private slots:
void currentStyleChanged();
void slotParagraphStyleSelected(QModelIndex index);
void slotCharacterStyleSelected(QModelIndex index);
void addParagraphStyle(KoParagraphStyle*);
void addCharacterStyle(KoCharacterStyle*);
void removeParagraphStyle(KoParagraphStyle*);
void removeCharacterStyle(KoCharacterStyle*);
void slotStyleSelected(QModelIndex index);
void currentParagraphStyleChanged();
void currentParagraphNameChanged(const QString &name);
void currentCharacterStyleChanged();
void currentCharacterNameChanged(const QString &name);
void buttonNewPressed();
void tabChanged(int index);
private:
bool checkUniqueStyleName(int widgetIndex);
Ui::StyleManager widget;
KoStyleManager *m_styleManager;
QMap<int, KoParagraphStyle*> m_alteredParagraphStyles;
QMap<int, KoCharacterStyle*> m_alteredCharacterStyles;
QMap<int, KoParagraphStyle*> m_draftParagraphStyles;
QMap<int, KoCharacterStyle*> m_draftCharacterStyles;
QMap<KoParagraphStyle *, KoParagraphStyle *> m_modifiedParagraphStyles;
QMap<KoCharacterStyle *, KoCharacterStyle *> m_modifiedCharacterStyles;
StylesModel *m_paragraphStylesModel;
StylesModel *m_characterStylesModel;
StylesManagerModel *m_paragraphStylesModel;
StylesManagerModel *m_characterStylesModel;
StylesSortFilterProxyModel *m_paragraphProxyModel;
StylesSortFilterProxyModel *m_characterProxyModel;
KoStyleThumbnailer *m_thumbnailer;
KoParagraphStyle *m_selectedParagStyle;
KoCharacterStyle *m_selectedCharStyle;
bool m_blockSignals;
bool m_blockStyleChangeSignals;
bool m_unappliedStyleChanges;
bool m_currentStyleChanged;
};
#endif
/* This file is part of the KDE project
* Copyright (C) 2013 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "StylesManagerModel.h"
#include <KoCharacterStyle.h>
#include <KoStyleThumbnailer.h>
#include <QDebug>
StylesManagerModel::StylesManagerModel(QObject *parent)
: QAbstractListModel(parent)
, m_styleThumbnailer(0)
{
}
QVariant StylesManagerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
int row = index.row();
if (row < 0 || row >= m_styles.size()) {
return QVariant();
}
//qDebug() << Q_FUNC_INFO << row << role;
QVariant retval;
switch (role) {
case Qt::DisplayRole:
retval = m_styles[row]->name();
break;
case Qt::DecorationRole:
if (!m_styleThumbnailer) {
retval = QPixmap();
}
else {
retval = m_styleThumbnailer->thumbnail(m_styles[row]);
}
break;
case StylePointer:
retval = QVariant::fromValue(m_styles[row]);
break;
case Qt::SizeHintRole:
retval = QVariant(QSize(250, 48));
break;
default:
break;
};
return retval;
}
int StylesManagerModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_styles.size();
}
void StylesManagerModel::setStyleThumbnailer(KoStyleThumbnailer *thumbnailer)
{
m_styleThumbnailer = thumbnailer;
}
void StylesManagerModel::setStyles(const QList<KoCharacterStyle *> styles)
{
beginResetModel();
m_styles = styles;
endResetModel();
}
void StylesManagerModel::addStyle(KoCharacterStyle *style)
{
if (m_styles.indexOf(style) == -1) {
beginInsertRows(QModelIndex(), m_styles.size(), m_styles.size());
m_styles.append(style);
endInsertRows();
}
}
void StylesManagerModel::removeStyle(KoCharacterStyle *style)
{
int row = m_styles.indexOf(style);
Q_ASSERT(row != -1);
if (row != -1) {
beginRemoveRows(QModelIndex(), row, row);
m_styles.removeAt(row);
endRemoveRows();
}
}
void StylesManagerModel::replaceStyle(KoCharacterStyle *oldStyle, KoCharacterStyle *newStyle)
{
qDebug() << Q_FUNC_INFO << oldStyle << "->" << newStyle;
int row = m_styles.indexOf(oldStyle);
Q_ASSERT(row != -1);
if (row != -1) {
m_styles[row] = newStyle;
QModelIndex index = this->index(row);
emit dataChanged(index, index);
}
}
void StylesManagerModel::updateStyle(KoCharacterStyle *style)
{
int row = m_styles.indexOf(style);
Q_ASSERT(row != -1);
if (row != -1) {
qDebug() << Q_FUNC_INFO << style << style->name();
m_styleThumbnailer->removeFromCache(style);
QModelIndex index = this->index(row);
emit dataChanged(index, index);
}
}
QModelIndex StylesManagerModel::styleIndex(KoCharacterStyle *style)
{
QModelIndex index;
int row = m_styles.indexOf(style);
if (row != -1) {
index = this->index(row);
}
return index;
}
/* This file is part of the KDE project
* Copyright (C) 2013 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef STYLESMODELNEW_H
#define STYLESMODELNEW_H
#include <QAbstractListModel>
#include <QList>
class KoCharacterStyle;
class KoStyleThumbnailer;
class StylesManagerModel : public QAbstractListModel
{
public:
enum Roles {
StylePointer = Qt::UserRole + 1,
};
explicit StylesManagerModel(QObject *parent = 0);
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
void setStyleThumbnailer(KoStyleThumbnailer *thumbnailer);
void setStyles(const QList<KoCharacterStyle *> styles);
void addStyle(KoCharacterStyle *style);
void removeStyle(KoCharacterStyle *style);
void replaceStyle(KoCharacterStyle *oldStyle, KoCharacterStyle *newStyle);
void updateStyle(KoCharacterStyle *style);
QModelIndex styleIndex(KoCharacterStyle *style);
private:
QList<KoCharacterStyle *> m_styles;
KoStyleThumbnailer *m_styleThumbnailer;
};
#endif /* STYLESMODELNEW_H */
/* This file is part of the KDE project
* Copyright (C) 2013 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "StylesSortFilterProxyModel.h"
#include <QDebug>
StylesSortFilterProxyModel::StylesSortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
bool StylesSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QVariant leftData= sourceModel()->data(left, Qt::DisplayRole);
QVariant rightData = sourceModel()->data(right, Qt::DisplayRole);
QString leftName = leftData.toString();
QString rightName = rightData.toString();
return QString::localeAwareCompare(leftName, rightName) < 0;
}
/* This file is part of the KDE project
* Copyright (C) 2013 Thorsten Zachmann <zachmann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef STYLESSORTFILTERPROXYMODEL_H
#define STYLESSORTFILTERPROXYMODEL_H
#include <QSortFilterProxyModel>
class StylesSortFilterProxyModel : public QSortFilterProxyModel
{
public:
explicit StylesSortFilterProxyModel(QObject *parent = 0);
protected:
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
};
#endif /* STYLESSORTFILTERPROXYMODEL_H */
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