Warning, file /office/calligra/gemini/ParagraphStylesModel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  *
0006  */
0007 
0008 #include "ParagraphStylesModel.h"
0009 #include <KoParagraphStyle.h>
0010 #include <KoStyleManager.h>
0011 #include <KoTextEditor.h>
0012 #include <KWDocument.h>
0013 
0014 class ParagraphStylesModel::Private {
0015 public:
0016     Private(ParagraphStylesModel* qq)
0017         : q(qq)
0018         , document(0)
0019     {
0020     }
0021     ParagraphStylesModel* q;
0022     QPointer<KWDocument> document;
0023     QList<KoParagraphStyle*> styles;
0024     QPointer<KoTextEditor> textEditor;
0025     KoParagraphStyle* cursorStyle;
0026     QFont cursorFont;
0027     qreal zoomLevel;
0028 
0029     void updateStylesList()
0030     {
0031         q->beginResetModel();
0032         styles.clear();
0033         if(document)
0034         {
0035             KoStyleManager* styleManager = document->resourceManager()->resource(KoText::StyleManager).value<KoStyleManager*>();
0036             QList<KoParagraphStyle *> paragraphStyles = styleManager->paragraphStyles();
0037             KoParagraphStyle *defaultParagraphStyle = styleManager->defaultParagraphStyle();
0038             foreach (KoParagraphStyle *style, paragraphStyles) {
0039                 if (style != defaultParagraphStyle) {
0040                     styles.append(style);
0041                 }
0042             }
0043         }
0044         q->endResetModel();
0045     }
0046 };
0047 
0048 ParagraphStylesModel::ParagraphStylesModel()
0049     : d(new Private(this))
0050 {
0051 }
0052 
0053 ParagraphStylesModel::~ParagraphStylesModel()
0054 {
0055     delete d;
0056 }
0057 
0058 
0059 QHash<int, QByteArray> ParagraphStylesModel::roleNames() const
0060 {
0061     QHash<int,QByteArray> roleNames;
0062     roleNames[Name] = "name";
0063     roleNames[Current] = "current";
0064     roleNames[Font] = "font";
0065     roleNames[FontFamily] = "fontFamily";
0066     roleNames[FontPointSize] = "fontPointSize";
0067     roleNames[FontWeight] = "fontWeight";
0068     roleNames[FontItalic] = "fontItalic";
0069     roleNames[FontUnderline] = "fontUnderline";
0070     return roleNames;
0071 }
0072 
0073 QVariant ParagraphStylesModel::data(const QModelIndex& index, int role) const
0074 {
0075     QVariant data;
0076     if(index.isValid() && index.row() < d->styles.count() && d->document)
0077     {
0078         KoCharacterStyle* style = d->styles.at(index.row());
0079         QFont font = style->font();
0080         switch(role)
0081         {
0082             case Name:
0083                 data.setValue(style->name());
0084                 break;
0085             case Current:
0086                 data.setValue(style == d->cursorStyle);
0087                 break;
0088             case Font:
0089                 font.setUnderline((style->underlineStyle() != KoCharacterStyle::NoLineStyle));
0090                 font.setPointSize(font.pointSize() * d->zoomLevel);
0091                 data.setValue(font);
0092                 break;
0093             case FontFamily:
0094                 data.setValue(style->fontFamily());
0095                 break;
0096             case FontPointSize:
0097                 data.setValue(style->fontPointSize() * d->zoomLevel);
0098                 break;
0099             case FontItalic:
0100                 data.setValue(style->fontItalic());
0101                 break;
0102             case FontWeight:
0103                 data.setValue(style->fontWeight());
0104                 break;
0105             case FontUnderline:
0106                 data.setValue((style->underlineStyle() != KoCharacterStyle::NoLineStyle));
0107                 break;
0108             default:
0109                 data.setValue(QString("Unknown role"));
0110                 break;
0111         }
0112     }
0113     return data;
0114 }
0115 
0116 int ParagraphStylesModel::rowCount(const QModelIndex& parent) const
0117 {
0118     if(parent.isValid())
0119         return 0;
0120     return d->styles.count();
0121 }
0122 
0123 QObject* ParagraphStylesModel::document() const
0124 {
0125     return d->document;
0126 }
0127 
0128 void ParagraphStylesModel::setDocument(QObject* newDocument)
0129 {
0130     d->document = qobject_cast<KWDocument*>(newDocument);
0131     d->updateStylesList();
0132     emit documentChanged();
0133 }
0134 
0135 QObject* ParagraphStylesModel::textEditor() const
0136 {
0137     return d->textEditor;
0138 }
0139 
0140 void ParagraphStylesModel::setTextEditor(QObject* newEditor)
0141 {
0142     if(d->textEditor)
0143         d->textEditor->disconnect(this);
0144     d->textEditor = qobject_cast<KoTextEditor*>(newEditor);
0145     if(d->textEditor)
0146         connect(d->textEditor.data(), &KoTextEditor::cursorPositionChanged, this, &ParagraphStylesModel::cursorPositionChanged);
0147     emit textEditorChanged();
0148 }
0149 
0150 void ParagraphStylesModel::cursorPositionChanged()
0151 {
0152     QTextBlockFormat bf = d->textEditor->blockFormat();
0153     d->cursorStyle =  d->document->resourceManager()->resource(KoText::StyleManager).value<KoStyleManager*>()->paragraphStyle(bf.intProperty(KoParagraphStyle::StyleId));
0154     dataChanged(index(0), index(d->styles.count() - 1));
0155 
0156     QTextCharFormat charFormat = d->textEditor->charFormat();
0157     d->cursorFont = charFormat.font();
0158     emit cursorFontChanged();
0159 }
0160 
0161 void ParagraphStylesModel::activate(int index)
0162 {
0163     if(d->textEditor && index > -1 && index < d->styles.count()) {
0164         KoParagraphStyle* style = d->styles.at(index);
0165         if(style == d->cursorStyle)
0166             return;
0167         d->textEditor->setStyle(style);
0168         cursorPositionChanged();
0169     }
0170 }
0171 
0172 QFont ParagraphStylesModel::cursorFont() const
0173 {
0174     return d->cursorFont;
0175 }
0176 
0177 int ParagraphStylesModel::currentStyle() const
0178 {
0179     return d->styles.indexOf(d->cursorStyle);
0180 }
0181 
0182 qreal ParagraphStylesModel::zoomLevel() const
0183 {
0184     return d->zoomLevel;
0185 }
0186 
0187 void ParagraphStylesModel::setZoomLevel(const qreal& newZoom)
0188 {
0189     d->zoomLevel = newZoom;
0190     if(d->styles.count() > 0)
0191         dataChanged(index(0), index(d->styles.count() - 1));
0192     emit zoomLevelChanged();
0193 }