File indexing completed on 2022-12-06 18:50:22

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "pagebreakentry.h"
0007 
0008 #include <QTextCursor>
0009 #include <QTextCharFormat>
0010 #include <QPalette>
0011 #include <QJsonValue>
0012 #include <QJsonObject>
0013 #include <KColorScheme>
0014 #include <KLocalizedString>
0015 
0016 #include "lib/jupyterutils.h"
0017 
0018 PageBreakEntry::PageBreakEntry(Worksheet* worksheet)
0019   : WorksheetEntry(worksheet)
0020 {
0021     m_msgItem = new WorksheetTextItem(this);
0022 
0023     QTextCursor cursor = m_msgItem->textCursor();
0024     KColorScheme color = KColorScheme(QPalette::Normal, KColorScheme::View);
0025     QTextCharFormat cformat(cursor.charFormat());
0026     cformat.setForeground(color.foreground(KColorScheme::InactiveText));
0027 
0028     cursor.insertText(i18n("--- Page Break ---"), cformat);
0029     m_msgItem->setAlignment(Qt::AlignCenter);
0030 
0031     setFlag(QGraphicsItem::ItemIsFocusable);
0032 }
0033 
0034 bool PageBreakEntry::isEmpty()
0035 {
0036     return false;
0037 }
0038 
0039 int PageBreakEntry::type() const
0040 {
0041     return Type;
0042 }
0043 
0044 void PageBreakEntry::populateMenu(QMenu* menu, QPointF pos)
0045 {
0046     WorksheetEntry::populateMenu(menu, pos);
0047 }
0048 
0049 bool PageBreakEntry::acceptRichText()
0050 {
0051     return false;
0052 }
0053 
0054 void PageBreakEntry::setContent(const QString& content)
0055 {
0056     Q_UNUSED(content);
0057     return;
0058 }
0059 
0060 void PageBreakEntry::setContent(const QDomElement& content, const KZip& file)
0061 {
0062     Q_UNUSED(content);
0063     Q_UNUSED(file);
0064     return;
0065 }
0066 
0067 void PageBreakEntry::setContentFromJupyter(const QJsonObject& cell)
0068 {
0069     Q_UNUSED(cell);
0070     return;
0071 }
0072 
0073 QJsonValue PageBreakEntry::toJupyterJson()
0074 {
0075     QJsonObject root;
0076 
0077     root.insert(QLatin1String("cell_type"), QLatin1String("raw"));
0078     QJsonObject metadata;
0079 
0080     // "raw_mimetype" vs "format"?
0081     // See https://github.com/jupyter/notebook/issues/4730
0082     // For safety set both keys
0083     metadata.insert(QLatin1String("format"), QLatin1String("text/latex"));
0084     metadata.insert(QLatin1String("raw_mimetype"), QLatin1String("text/latex"));
0085 
0086     QJsonObject cantor;
0087     cantor.insert(QLatin1String("from_page_break"), true);
0088     metadata.insert(Cantor::JupyterUtils::cantorMetadataKey, cantor);
0089 
0090     root.insert(Cantor::JupyterUtils::metadataKey, metadata);
0091     Cantor::JupyterUtils::setSource(root, QLatin1String("\\pagebreak"));
0092 
0093     return root;
0094 }
0095 
0096 QDomElement PageBreakEntry::toXml(QDomDocument& doc, KZip* archive)
0097 {
0098     Q_UNUSED(archive);
0099 
0100     QDomElement pgbrk = doc.createElement(QLatin1String("PageBreak"));
0101     return pgbrk;
0102 }
0103 
0104 QString PageBreakEntry::toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq)
0105 {
0106     Q_UNUSED(commandSep);
0107 
0108     return commentStartingSeq + QLatin1String("page break") + commentEndingSeq;
0109 }
0110 
0111 void PageBreakEntry::layOutForWidth(qreal entry_zone_x, qreal w, bool force)
0112 {
0113     if (size().width() == w && m_msgItem->pos().x() == entry_zone_x && !force)
0114         return;
0115 
0116     const qreal margin = worksheet()->isPrinting() ? 0 : RightMargin;
0117 
0118     if (m_msgItem->isVisible()) {
0119         m_msgItem->setGeometry(entry_zone_x, 0, w - margin - entry_zone_x, true);
0120 
0121         setSize(QSizeF(m_msgItem->width() + margin + entry_zone_x, m_msgItem->height() + VerticalMargin));
0122     } else {
0123         setSize(QSizeF(w, 0));
0124     }
0125 }
0126 
0127 bool PageBreakEntry::evaluate(EvaluationOption evalOp)
0128 {
0129     evaluateNext(evalOp);
0130     return true;
0131 }
0132 
0133 void PageBreakEntry::updateEntry()
0134 {
0135     if (worksheet()->isPrinting()) {
0136         m_msgItem->setVisible(false);
0137         recalculateSize();
0138     } else if (!m_msgItem->isVisible()) {
0139         m_msgItem->setVisible(true);
0140         recalculateSize();
0141     }
0142     return;
0143 }
0144 
0145 /*
0146 void PageBreakEntry::paint(QPainter* painter, const QStyleOptionGraphicsItem*,
0147                            QWidget*)
0148 {
0149     if (worksheet()->isPrinting()) {
0150         QPaintDevice* device = painter->paintEngine()->paintDevice();
0151         QPrinter* printer = qobject_cast<QPrinter*>(device);
0152         if (printer)
0153             printer->newPage();
0154     }
0155 }
0156 */
0157 
0158 bool PageBreakEntry::wantToEvaluate()
0159 {
0160     return false;
0161 }
0162 
0163 bool PageBreakEntry::wantFocus()
0164 {
0165     return false;
0166 }
0167 
0168 bool PageBreakEntry::isConvertableToPageBreakEntry(const QJsonObject& cell)
0169 {
0170     if (!Cantor::JupyterUtils::isRawCell(cell))
0171         return false;
0172 
0173     QJsonObject metadata = Cantor::JupyterUtils::getCantorMetadata(cell);
0174     QJsonValue value = metadata.value(QLatin1String("from_page_break"));
0175 
0176     return value.isBool() && value.toBool() == true;
0177 }
0178