File indexing completed on 2024-05-05 17:04:26
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk> 0003 * 0004 * This program is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU General Public License as 0006 * published by the Free Software Foundation; either version 2 of 0007 * the License or (at your option) version 3 or any later version 0008 * accepted by the membership of KDE e.V. (or its successor approved 0009 * by the membership of KDE e.V.), which shall act as a proxy 0010 * defined in Section 14 of version 3 of the license. 0011 * 0012 * This program is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0015 * GNU General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU General Public License 0018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0019 * 0020 */ 0021 0022 #include "ParagraphStylesModel.h" 0023 #include <KoParagraphStyle.h> 0024 #include <KoStyleManager.h> 0025 #include <KoTextEditor.h> 0026 #include <KWDocument.h> 0027 0028 class ParagraphStylesModel::Private { 0029 public: 0030 Private(ParagraphStylesModel* qq) 0031 : q(qq) 0032 , document(0) 0033 { 0034 } 0035 ParagraphStylesModel* q; 0036 QPointer<KWDocument> document; 0037 QList<KoParagraphStyle*> styles; 0038 QPointer<KoTextEditor> textEditor; 0039 KoParagraphStyle* cursorStyle; 0040 QFont cursorFont; 0041 qreal zoomLevel; 0042 0043 void updateStylesList() 0044 { 0045 q->beginResetModel(); 0046 styles.clear(); 0047 if(document) 0048 { 0049 KoStyleManager* styleManager = document->resourceManager()->resource(KoText::StyleManager).value<KoStyleManager*>(); 0050 QList<KoParagraphStyle *> paragraphStyles = styleManager->paragraphStyles(); 0051 KoParagraphStyle *defaultParagraphStyle = styleManager->defaultParagraphStyle(); 0052 foreach (KoParagraphStyle *style, paragraphStyles) { 0053 if (style != defaultParagraphStyle) { 0054 styles.append(style); 0055 } 0056 } 0057 } 0058 q->endResetModel(); 0059 } 0060 }; 0061 0062 ParagraphStylesModel::ParagraphStylesModel() 0063 : d(new Private(this)) 0064 { 0065 QHash<int,QByteArray> roleNames; 0066 roleNames[Name] = "name"; 0067 roleNames[Current] = "current"; 0068 roleNames[Font] = "font"; 0069 roleNames[FontFamily] = "fontFamily"; 0070 roleNames[FontPointSize] = "fontPointSize"; 0071 roleNames[FontWeight] = "fontWeight"; 0072 roleNames[FontItalic] = "fontItalic"; 0073 roleNames[FontUnderline] = "fontUnderline"; 0074 setRoleNames(roleNames); 0075 } 0076 0077 ParagraphStylesModel::~ParagraphStylesModel() 0078 { 0079 delete d; 0080 } 0081 0082 QVariant ParagraphStylesModel::data(const QModelIndex& index, int role) const 0083 { 0084 QVariant data; 0085 if(index.isValid() && index.row() < d->styles.count() && d->document) 0086 { 0087 KoCharacterStyle* style = d->styles.at(index.row()); 0088 QFont font = style->font(); 0089 switch(role) 0090 { 0091 case Name: 0092 data.setValue(style->name()); 0093 break; 0094 case Current: 0095 data.setValue(style == d->cursorStyle); 0096 break; 0097 case Font: 0098 font.setUnderline((style->underlineStyle() != KoCharacterStyle::NoLineStyle)); 0099 font.setPointSize(font.pointSize() * d->zoomLevel); 0100 data.setValue(font); 0101 break; 0102 case FontFamily: 0103 data.setValue(style->fontFamily()); 0104 break; 0105 case FontPointSize: 0106 data.setValue(style->fontPointSize() * d->zoomLevel); 0107 break; 0108 case FontItalic: 0109 data.setValue(style->fontItalic()); 0110 break; 0111 case FontWeight: 0112 data.setValue(style->fontWeight()); 0113 break; 0114 case FontUnderline: 0115 data.setValue((style->underlineStyle() != KoCharacterStyle::NoLineStyle)); 0116 break; 0117 default: 0118 data.setValue(QString("Unknown role")); 0119 break; 0120 } 0121 } 0122 return data; 0123 } 0124 0125 int ParagraphStylesModel::rowCount(const QModelIndex& parent) const 0126 { 0127 if(parent.isValid()) 0128 return 0; 0129 return d->styles.count(); 0130 } 0131 0132 QObject* ParagraphStylesModel::document() const 0133 { 0134 return d->document; 0135 } 0136 0137 void ParagraphStylesModel::setDocument(QObject* newDocument) 0138 { 0139 d->document = qobject_cast<KWDocument*>(newDocument); 0140 d->updateStylesList(); 0141 emit documentChanged(); 0142 } 0143 0144 QObject* ParagraphStylesModel::textEditor() const 0145 { 0146 return d->textEditor; 0147 } 0148 0149 void ParagraphStylesModel::setTextEditor(QObject* newEditor) 0150 { 0151 if(d->textEditor) 0152 d->textEditor->disconnect(this); 0153 d->textEditor = qobject_cast<KoTextEditor*>(newEditor); 0154 if(d->textEditor) 0155 connect(d->textEditor, SIGNAL(cursorPositionChanged()), SLOT(cursorPositionChanged())); 0156 emit textEditorChanged(); 0157 } 0158 0159 void ParagraphStylesModel::cursorPositionChanged() 0160 { 0161 QTextBlockFormat bf = d->textEditor->blockFormat(); 0162 d->cursorStyle = d->document->resourceManager()->resource(KoText::StyleManager).value<KoStyleManager*>()->paragraphStyle(bf.intProperty(KoParagraphStyle::StyleId)); 0163 dataChanged(index(0), index(d->styles.count() - 1)); 0164 0165 QTextCharFormat charFormat = d->textEditor->charFormat(); 0166 d->cursorFont = charFormat.font(); 0167 emit cursorFontChanged(); 0168 } 0169 0170 void ParagraphStylesModel::activate(int index) 0171 { 0172 if(d->textEditor && index > -1 && index < d->styles.count()) { 0173 KoParagraphStyle* style = d->styles.at(index); 0174 if(style == d->cursorStyle) 0175 return; 0176 d->textEditor->setStyle(style); 0177 cursorPositionChanged(); 0178 } 0179 } 0180 0181 QFont ParagraphStylesModel::cursorFont() const 0182 { 0183 return d->cursorFont; 0184 } 0185 0186 int ParagraphStylesModel::currentStyle() const 0187 { 0188 return d->styles.indexOf(d->cursorStyle); 0189 } 0190 0191 qreal ParagraphStylesModel::zoomLevel() const 0192 { 0193 return d->zoomLevel; 0194 } 0195 0196 void ParagraphStylesModel::setZoomLevel(const qreal& newZoom) 0197 { 0198 d->zoomLevel = newZoom; 0199 if(d->styles.count() > 0) 0200 dataChanged(index(0), index(d->styles.count() - 1)); 0201 emit zoomLevelChanged(); 0202 }