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

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007  *
0008  */
0009 
0010 #include "CQTextDocumentNotesModel.h"
0011 
0012 #include <QColor>
0013 
0014 struct Entry {
0015     Entry() : colorCount(1), shape(0), expanded(false) {};
0016     QString text;
0017     QString image;
0018     QString color;
0019     QString categoryName;
0020     int colorCount;
0021     KoShape* shape;
0022     bool expanded;
0023 };
0024 
0025 class CQTextDocumentNotesModel::Private {
0026 public:
0027     Private() {}
0028     ~Private() {
0029         qDeleteAll(entries);
0030     }
0031     QList<Entry*> entries;
0032 };
0033 
0034 CQTextDocumentNotesModel::CQTextDocumentNotesModel(QObject* parent)
0035     : QAbstractListModel(parent)
0036     , d(new Private)
0037 {
0038     QHash<int, QByteArray> roles;
0039     roles[Text] = "text";
0040     roles[Image] = "image";
0041     roles[Color] = "color";
0042     roles[ColorCount] = "colorCount";
0043     roles[CategoryName] = "categoryName";
0044     roles[FirstOfThisColor] = "firstOfThisColor";
0045     roles[Position] = "position";
0046     roles[Expanded] = "expanded";
0047     setRoleNames(roles);
0048 }
0049 
0050 CQTextDocumentNotesModel::~CQTextDocumentNotesModel()
0051 {
0052     delete d;
0053 }
0054 
0055 QVariant CQTextDocumentNotesModel::data(const QModelIndex& index, int role) const
0056 {
0057     QVariant data;
0058     if (index.isValid() && index.row() < d->entries.count()) {
0059         Entry* entry = d->entries.at(index.row());
0060         switch(role) {
0061             case Text:
0062                 data = entry->text;
0063                 break;
0064             case Image:
0065                 data = entry->image;
0066                 break;
0067             case Color:
0068                 data = entry->color;
0069                 break;
0070             case ColorCount:
0071                 data = entry->colorCount;
0072                 break;
0073             case CategoryName:
0074                 data = entry->categoryName;
0075                 break;
0076             case FirstOfThisColor:
0077                 data = true;
0078                 if (index.row() > 0 && d->entries.at(index.row() - 1)->color == entry->color) {
0079                     data = false;
0080                 }
0081                 break;
0082             case Position:
0083                 data = entry->shape->absolutePosition();
0084                 break;
0085             case Expanded:
0086                 data = entry->expanded;
0087                 break;
0088             default:
0089                 data = QLatin1String("No such role. Supported roles are text, image, color, colorCount and position.");
0090                 break;
0091         }
0092     }
0093     return data;
0094 }
0095 
0096 int CQTextDocumentNotesModel::rowCount(const QModelIndex& parent) const
0097 {
0098     if (parent.isValid()) {
0099         return 0;
0100     }
0101     return d->entries.count();
0102 }
0103 
0104 int CQTextDocumentNotesModel::count() const
0105 {
0106     return d->entries.count();
0107 }
0108 
0109 void CQTextDocumentNotesModel::toggleExpanded(int index)
0110 {
0111     if (index > -1 && index < d->entries.count()) {
0112         QColor color = d->entries.at(index)->color;
0113         foreach(Entry* entry, d->entries) {
0114             if (color == entry->color) {
0115                 entry->expanded = !entry->expanded;
0116             }
0117         }
0118     }
0119     dataChanged(this->index(0), this->index(d->entries.count() - 1));
0120 }
0121 
0122 void CQTextDocumentNotesModel::addEntry(const QString& text, const QString& image, const QString& color, KoShape* shape)
0123 {
0124     Entry* entry = new Entry();
0125     entry->text = text;
0126     entry->image = image;
0127     entry->shape = shape;
0128     entry->color = color;
0129     entry->categoryName = "Others";
0130     if (color == "#fd5134") {
0131         entry->categoryName = "Major Errors";
0132     } else if (color == "#ffb20c") {
0133         entry->categoryName = "Minor Errors";
0134     } else if (color == "#29b618") {
0135         entry->categoryName = "Successes";
0136     }
0137 
0138     QList<Entry*>::iterator before = d->entries.begin();
0139     bool reachedColor = false;
0140     int colorCount = 0, position = 0;
0141     for(; before != d->entries.end(); ++before) {
0142         if ((*before)->color == entry->color)
0143         {
0144             // We are now in the current entry's section as defined by colour
0145             // and we grab the current colour count for that section from
0146             // this item. Could just increase, but that's cause a double-loop
0147             // to update all the other entries.
0148             reachedColor = true;
0149             colorCount = (*before)->colorCount + 1;
0150             (*before)->colorCount = colorCount;
0151             entry->colorCount = colorCount;
0152             entry->expanded = (*before)->expanded;
0153         }
0154         if (reachedColor)
0155         {
0156             // If we find a new colour, that means we're out of the current
0157             // section, and we break out. This also conveniently leaves us
0158             // with the entry we want to insert the item before.
0159             if ((*before)->color != entry->color) {
0160                 break;
0161             }
0162             (*before)->colorCount = colorCount;
0163         }
0164         ++position;
0165     }
0166 
0167     // By default, the Neutral category is supposed to be expanded
0168     if (color == "Neutral" && colorCount == 1) {
0169         entry->expanded = true;
0170     }
0171 
0172     beginInsertRows(QModelIndex(), position, position);
0173     d->entries.insert(before, entry);
0174     endInsertRows();
0175     dataChanged(index(position - colorCount), index(position - 1));
0176     emit countChanged();
0177 }