diff --git a/src/noteitem.cpp b/src/noteitem.cpp index f70c001..2ad3a28 100644 --- a/src/noteitem.cpp +++ b/src/noteitem.cpp @@ -1,285 +1,276 @@ /* * SPDX-FileCopyrightText: (C) 2003 by Sébastien Laoût * SPDX-FileCopyrightText: (C) 2020 by Ismael Asensio * SPDX-License-Identifier: GPL-2.0-or-later */ #include "basketscenemodel.h" #include "notefactory.h" #include "xmlwork.h" #include namespace { QString iconNameForType(NoteType::Id type) { switch (type) { case NoteType::Group: return "package"; case NoteType::Text: return "text-x-plain"; case NoteType::Html: return "text-html"; case NoteType::Image: return "folder-pictures-symbolic"; case NoteType::Animation: return "folder-video-symbolic"; case NoteType::Sound: return "folder-music-symbolic"; case NoteType::File: return "application-x-zerosize"; case NoteType::Link: return "edit-link"; case NoteType::CrossReference: return "basket"; case NoteType::Launcher: return "run-build"; case NoteType::Color: return "color-profile"; case NoteType::Unknown: return "application-octet-stream"; } return QString(); } } BasketScene *NoteItem::s_basket; NoteItem::NoteItem() : m_parent(nullptr) - , m_note(nullptr) , m_helperNote(new Note(s_basket)) { } NoteItem::~NoteItem() { delete m_helperNote; qDeleteAll(m_children); } int NoteItem::row() const { if (!m_parent) { return 0; } return m_parent->m_children.indexOf(const_cast(this)); } NoteItem *NoteItem::parent() const { return m_parent; } void NoteItem::setParent(NoteItem *parent) { m_parent = parent; } QVector NoteItem::children() const { return m_children; } int NoteItem::childrenCount() const { return m_children.count(); } void NoteItem::insertAt(int row, NoteItem *item) { if (!item) return; item->setParent(this); if (row >= 0 && row < m_children.count() ) { m_children.insert(row, item); } else { m_children.append(item); } } void NoteItem::removeAt(int row) { if (row < 0 || row >= m_children.count()) { return; } delete m_children[row]; m_children.remove(row); } NoteItem *NoteItem::takeAt(int row) { if (row < 0 || row >= m_children.count()) { return nullptr; } return m_children.takeAt(row); } NotePtr NoteItem::note() const { - if (!m_note) { - return m_helperNote; - } - return m_note; -} - -void NoteItem::setNote(NotePtr note) -{ - m_note = note; + return m_helperNote; } void NoteItem::setBasketParent(BasketScene* basket) { s_basket = basket; } QString NoteItem::displayText() const { if (!note()) { return QStringLiteral("NULL NOTE"); } if (type() == NoteType::Group) { return QStringLiteral("GROUP"); } return content()->toText(QString()); } QIcon NoteItem::decoration() const { if (!note()) { return QIcon::fromTheme("data-error"); } if (type() == NoteType::Group) { return QIcon::fromTheme("package"); } if (!content()) { return QIcon::fromTheme("empty"); } return QIcon::fromTheme(iconNameForType(content()->type())); } NoteContent *NoteItem::content() const { if (!note()) { return nullptr; } return note()->content(); } NoteType::Id NoteItem::type() const { if (!note()) { return NoteType::Unknown; } if (!note()->content()) { return NoteType::Group; } return note()->content()->type(); } QRect NoteItem::bounds() const { return QRect( note()->x(), note()->y(), note()->width(), note()->height() ); } NoteItem::EditInfo NoteItem::editInformation() const { return EditInfo { note()->addedDate(), note()->lastModificationDate() }; } QString NoteItem::formatAddress(void *ptr) { return QString::number(reinterpret_cast(ptr), 16); } QString NoteItem::address() const { if (!note()) { return QStringLiteral("root"); } return formatAddress(note()); } QString NoteItem::toolTipInfo() const { QStringList toolTip; // fullAddress and position within parent (debug) toolTip << QStringLiteral("%1 (@ <%2>[%3])") .arg(formatAddress(note())) .arg(m_parent->address()) .arg(row()); // type toolTip << QStringLiteral("Type: %1").arg(content() ? content()->typeName() : "Group"); // geometry const QRect geometry = bounds(); toolTip << QStringLiteral("x:%1 y:%2 w:%3 h:%4") .arg(geometry.x()).arg(geometry.y()) .arg(geometry.width()).arg(geometry.height()); // edition information const EditInfo info = editInformation(); toolTip << QStringLiteral("created: %1\nmodified: %2") .arg(info.created.toString()) .arg(info.modified.toString()); return toolTip.join(QStringLiteral("\n")); } void NoteItem::loadFromXMLNode(const QDomElement& node) { for (QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling()) { QDomElement e = n.toElement(); if (e.isNull()) { continue; } NoteItem *noteItem = new NoteItem(); noteItem->setParent(this); m_children.append(noteItem); NotePtr note = noteItem->note(); // Helper Note object if (e.tagName() == "group") { // Node is a group. Recursively load from this element noteItem->loadFromXMLNode(e); } else if (e.tagName() == "note" || e.tagName() == "item") { // "item" is to keep compatible with 0.6.0 Alpha 1 (required?) // Load note content NoteFactory::loadNode(XMLWork::getElement(e, "content"), e.attribute("type"), note, true); //lazyload } // Load dates if (e.hasAttribute("added")) { note->setAddedDate(QDateTime::fromString(e.attribute("added"), Qt::ISODate)); } if (e.hasAttribute("lastModification")) { note->setLastModificationDate(QDateTime::fromString(e.attribute("lastModification"), Qt::ISODate)); } // Free Note Properties: if (note->isFree()) { int x = e.attribute("x").toInt(); int y = e.attribute("y").toInt(); note->setX(x < 0 ? 0 : x); note->setY(y < 0 ? 0 : y); } // Resizeable Note Properties: if (note->hasResizer() || note->isColumn()) { note->setGroupWidth(e.attribute("width", "200").toInt()); } // Group Properties: if (note->isGroup() && !note->isColumn() && XMLWork::trueOrFalse(e.attribute("folded", "false"))) { note->toggleFolded(); } // Tags: if (note->content()) { const QString tagsString = XMLWork::getElementText(e, QStringLiteral("tags"), QString()); const QStringList tagsId = tagsString.split(';'); for (const QString &tagId : tagsId) { State *state = Tag::stateForId(tagId); if (state) { note->addState(state, /*orReplace=*/true); } } } } } diff --git a/src/noteitem.h b/src/noteitem.h index e35503d..7bce333 100644 --- a/src/noteitem.h +++ b/src/noteitem.h @@ -1,86 +1,83 @@ /* * SPDX-FileCopyrightText: 2020 by Ismael Asensio * SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include "note.h" #include "notecontent.h" #include class QDomElement; typedef Note * NotePtr; /** NoteItem: Container that stores a Note object within a tree model * Eventually implement the managing functionallity here and use directly the `NoteContent` object */ class NoteItem { public: struct EditInfo { QDateTime created; QDateTime modified; }; public: explicit NoteItem(); explicit NoteItem(BasketScene *basket); ~NoteItem(); // Tree structure int row() const; NoteItem *parent() const; void setParent(NoteItem *parent); QVector children() const; int childrenCount() const; void insertAt(int row, NoteItem *item); void removeAt(int row); NoteItem *takeAt(int row); // Accesors for compatibility with current code NotePtr note() const; - void setNote(NotePtr note); static void setBasketParent(BasketScene *basket); // NoteItem property getters QString displayText() const; QIcon decoration() const; NoteContent *content() const; NoteType::Id type() const; QRect bounds() const; EditInfo editInformation() const; // Recursive loader from an XML node void loadFromXMLNode(const QDomElement &node); // Find and debug Notes by its pointer address static QString formatAddress(void *ptr); QString address() const; QString toolTipInfo() const; private: NoteItem *m_parent; QVector m_children; -// TODO: Remove m_note and store the information here - NotePtr m_note; NotePtr m_helperNote; // Dummy note to help with the code transition. static BasketScene *s_basket; // Stored to set a parent to the notes (and avoid crashes) // NoteContent *m_content; // QRect m_bounds; // EditInfo m_editInfo; // QVector m_tags; }; Q_DECLARE_METATYPE(NoteContent *) Q_DECLARE_METATYPE(NoteItem::EditInfo)