File indexing completed on 2024-05-19 03:48:58
0001 /* 0002 File : Notes.cpp 0003 Project : LabPlot 0004 Description : Notes Widget for taking notes 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2009-2015 Garvit Khatri <garvitdelhi@gmail.com> 0007 SPDX-FileCopyrightText: 2016-2023 Alexander Semke <alexander.semke@web.de> 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "Note.h" 0012 #include "backend/core/Project.h" 0013 #include "backend/core/Settings.h" 0014 #include "backend/lib/XmlStreamReader.h" 0015 #include "backend/lib/macros.h" 0016 #include "commonfrontend/note/NoteView.h" 0017 0018 #include <QFileDialog> 0019 #include <QMessageBox> 0020 #include <QPalette> 0021 #include <QPrintDialog> 0022 #include <QPrintPreviewDialog> 0023 #include <QPrinter> 0024 #include <QTextStream> 0025 0026 #include <KConfig> 0027 #include <KConfigGroup> 0028 #include <KLocalizedString> 0029 0030 Note::Note(const QString& name) 0031 : AbstractPart(name, AspectType::Note) { 0032 KConfig config; 0033 KConfigGroup group = config.group(QStringLiteral("Notes")); 0034 0035 m_backgroundColor = group.readEntry(QStringLiteral("BackgroundColor"), QColor(Qt::yellow)); 0036 m_textColor = group.readEntry(QStringLiteral("TextColor"), QColor(Qt::black)); 0037 m_textFont = group.readEntry(QStringLiteral("TextFont"), QFont()); 0038 } 0039 0040 QIcon Note::icon() const { 0041 return QIcon::fromTheme(QStringLiteral("document-new")); 0042 } 0043 0044 bool Note::printView() { 0045 QPrinter printer; 0046 auto* dlg = new QPrintDialog(&printer, m_view); 0047 dlg->setWindowTitle(i18nc("@title:window", "Print Note")); 0048 bool ret; 0049 if ((ret = (dlg->exec() == QDialog::Accepted))) 0050 m_view->print(&printer); 0051 0052 delete dlg; 0053 return ret; 0054 } 0055 0056 bool Note::printPreview() const { 0057 auto* dlg = new QPrintPreviewDialog(m_view); 0058 connect(dlg, &QPrintPreviewDialog::paintRequested, m_view, &NoteView::print); 0059 return dlg->exec(); 0060 } 0061 0062 bool Note::exportView() const { 0063 KConfigGroup conf = Settings::group(QStringLiteral("ExportNote")); 0064 QString dir = conf.readEntry("LastDir", ""); 0065 QString extensions = i18n("Text file (*.txt)"); 0066 0067 const QString path = QFileDialog::getSaveFileName(view(), i18nc("@title:window", "Export to File"), dir, extensions); 0068 0069 if (path.isEmpty()) 0070 return false; 0071 0072 int pos = path.lastIndexOf(QStringLiteral("/")); 0073 if (pos != -1) { 0074 QString newDir = path.left(pos); 0075 if (newDir != dir) 0076 conf.writeEntry(QStringLiteral("LastDir"), newDir); 0077 } 0078 0079 QFile file(path); 0080 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { 0081 QMessageBox::critical(view(), i18n("Export failed"), i18n("Failed to open '%1' for writing.", path)); 0082 return false; 0083 } 0084 0085 QTextStream out(&file); 0086 out << m_note; 0087 file.close(); 0088 0089 return true; 0090 } 0091 0092 void Note::setNote(const QString& note) { 0093 m_note = note; 0094 project()->setChanged(true); 0095 } 0096 0097 const QString& Note::note() const { 0098 return m_note; 0099 } 0100 0101 void Note::setBackgroundColor(const QColor& color) { 0102 m_backgroundColor = color; 0103 Q_EMIT backgroundColorChanged(color); 0104 } 0105 0106 const QColor& Note::backgroundColor() const { 0107 return m_backgroundColor; 0108 } 0109 0110 void Note::setTextColor(const QColor& color) { 0111 m_textColor = color; 0112 Q_EMIT textColorChanged(color); 0113 } 0114 0115 const QColor& Note::textColor() const { 0116 return m_textColor; 0117 } 0118 0119 void Note::setTextFont(const QFont& font) { 0120 m_textFont = font; 0121 Q_EMIT textFontChanged(font); 0122 } 0123 0124 const QFont& Note::textFont() const { 0125 return m_textFont; 0126 } 0127 0128 QWidget* Note::view() const { 0129 if (!m_partView) { 0130 m_view = new NoteView(const_cast<Note*>(this)); 0131 m_partView = m_view; 0132 } 0133 return m_partView; 0134 } 0135 0136 // ############################################################################## 0137 // ################## Serialization/Deserialization ########################### 0138 // ############################################################################## 0139 //! Save as XML 0140 void Note::save(QXmlStreamWriter* writer) const { 0141 writer->writeStartElement(QStringLiteral("note")); 0142 writeBasicAttributes(writer); 0143 writeCommentElement(writer); 0144 0145 writer->writeStartElement(QStringLiteral("background")); 0146 WRITE_QCOLOR(m_backgroundColor); 0147 writer->writeEndElement(); 0148 0149 writer->writeStartElement(QStringLiteral("text")); 0150 WRITE_QCOLOR(m_textColor); 0151 WRITE_QFONT(m_textFont); 0152 writer->writeAttribute(QStringLiteral("text"), m_note); 0153 writer->writeEndElement(); 0154 0155 writer->writeEndElement(); // close "note" section 0156 } 0157 0158 bool Note::load(XmlStreamReader* reader, bool preview) { 0159 if (!reader->isStartElement() || reader->name() != QLatin1String("note")) { 0160 reader->raiseError(i18n("no note element found")); 0161 return false; 0162 } 0163 0164 if (!readBasicAttributes(reader)) 0165 return false; 0166 0167 QXmlStreamAttributes attribs; 0168 QString str; 0169 0170 while (!reader->atEnd()) { 0171 reader->readNext(); 0172 if (reader->isEndElement() && reader->name() == QLatin1String("note")) 0173 break; 0174 0175 if (!reader->isStartElement()) 0176 continue; 0177 0178 if (reader->name() == QLatin1String("comment")) { 0179 if (!readCommentElement(reader)) 0180 return false; 0181 } else if (!preview && reader->name() == QLatin1String("background")) { 0182 attribs = reader->attributes(); 0183 READ_QCOLOR(m_backgroundColor); 0184 } else if (!preview && reader->name() == QLatin1String("text")) { 0185 attribs = reader->attributes(); 0186 READ_QCOLOR(m_textColor); 0187 READ_QFONT(m_textFont); 0188 m_note = attribs.value(QStringLiteral("text")).toString(); 0189 } else { // unknown element 0190 reader->raiseUnknownElementWarning(); 0191 if (!reader->skipToEndElement()) 0192 return false; 0193 } 0194 } 0195 0196 return true; 0197 }