labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Note.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : Notes.cpp
3  Project : LabPlot
4  Description : Notes Widget for taking notes
5  --------------------------------------------------------------------
6  Copyright : (C) 2009-2015 Garvit Khatri (garvitdelhi@gmail.com)
7  Copyright : (C) 2016 Alexander Semke (alexander.semke@web.de)
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 
30 #include "Note.h"
33 #include "backend/lib/macros.h"
34 
35 #include <QPalette>
36 #include <QPrinter>
37 #include <QPrintDialog>
38 #include <QPrintPreviewDialog>
39 
40 #include <KConfig>
41 #include <KConfigGroup>
42 #include <KLocalizedString>
43 
44 Note::Note(const QString& name) : AbstractPart(name, AspectType::Note) {
45  KConfig config;
46  KConfigGroup group = config.group("Notes");
47 
48  m_backgroundColor = group.readEntry("BackgroundColor", QColor(Qt::yellow));
49  m_textColor = group.readEntry("TextColor", QColor(Qt::black));
50  m_textFont = group.readEntry("TextFont", QFont());
51 }
52 
53 QIcon Note::icon() const {
54  return QIcon::fromTheme("document-new");
55 }
56 
58  QPrinter printer;
59  auto* dlg = new QPrintDialog(&printer, m_view);
60  dlg->setWindowTitle(i18nc("@title:window", "Print Worksheet"));
61  bool ret;
62  if ( (ret = (dlg->exec() == QDialog::Accepted)) )
63  m_view->print(&printer);
64 
65  delete dlg;
66  return ret;
67 }
68 
69 bool Note::printPreview() const {
70  auto* dlg = new QPrintPreviewDialog(m_view);
71  connect(dlg, &QPrintPreviewDialog::paintRequested, m_view, &NoteView::print);
72  return dlg->exec();
73 }
74 
75 bool Note::exportView() const {
76  return false;
77 }
78 
79 void Note::setNote(const QString& note) {
80  m_note = note;
81 }
82 
83 const QString& Note::note() const {
84  return m_note;
85 }
86 
87 void Note::setBackgroundColor(const QColor& color) {
88  m_backgroundColor = color;
89  emit backgroundColorChanged(color);
90 }
91 
92 const QColor& Note::backgroundColor() const {
93  return m_backgroundColor;
94 }
95 
96 void Note::setTextColor(const QColor& color) {
97  m_textColor = color;
98  emit textColorChanged(color);
99 }
100 
101 const QColor& Note::textColor() const{
102  return m_textColor;
103 }
104 
105 void Note::setTextFont(const QFont& font) {
106  m_textFont = font;
107  emit textFontChanged(font);
108 }
109 
110 const QFont& Note::textFont() const {
111  return m_textFont;
112 }
113 
114 QWidget* Note::view() const {
115  if (!m_partView) {
116  m_view = new NoteView(const_cast<Note*>(this));
117  m_partView = m_view;
118  }
119  return m_partView;
120 }
121 
122 //##############################################################################
123 //################## Serialization/Deserialization ###########################
124 //##############################################################################
125 //! Save as XML
126 void Note::save(QXmlStreamWriter* writer) const {
127  writer->writeStartElement("note");
128  writeBasicAttributes(writer);
129  writeCommentElement(writer);
130 
131  writer->writeStartElement("background");
133  writer->writeEndElement();
134 
135  writer->writeStartElement("text");
138  writer->writeAttribute("text", m_note);
139  writer->writeEndElement();
140 
141  writer->writeEndElement(); // close "note" section
142 }
143 
144 bool Note::load(XmlStreamReader* reader, bool preview) {
145  if (!reader->isStartElement() || reader->name() != "note") {
146  reader->raiseError(i18n("no note element found"));
147  return false;
148  }
149 
150  if (!readBasicAttributes(reader))
151  return false;
152 
153  KLocalizedString attributeWarning = ki18n("Attribute '%1' missing or empty, default value is used");
154  QXmlStreamAttributes attribs;
155  QString str;
156 
157  while (!reader->atEnd()) {
158  reader->readNext();
159  if (reader->isEndElement() && reader->name() == "note")
160  break;
161 
162  if (!reader->isStartElement())
163  continue;
164 
165  if (reader->name() == "comment") {
166  if (!readCommentElement(reader))
167  return false;
168  } else if (!preview && reader->name() == "background") {
169  attribs = reader->attributes();
171  } else if (!preview && reader->name() == "text") {
172  attribs = reader->attributes();
175  m_note = attribs.value("text").toString();
176  }
177  }
178 
179  return true;
180 }
AspectType
static const QRgb black
Definition: ImageEditor.cpp:38
bool readCommentElement(XmlStreamReader *)
Load comment from an XML element.
void writeBasicAttributes(QXmlStreamWriter *) const
Save name and creation time to XML.
bool readBasicAttributes(XmlStreamReader *)
Load name and creation time from XML.
void writeCommentElement(QXmlStreamWriter *) const
Save the comment to XML.
Base class of Aspects with MDI windows as views (AspectParts).
Definition: AbstractPart.h:36
QWidget * m_partView
Definition: AbstractPart.h:65
void print(QPrinter *) const
Definition: NoteView.cpp:60
Definition: Note.h:41
void setNote(const QString &)
Definition: Note.cpp:79
QWidget * view() const override
Construct a primary view on me.
Definition: Note.cpp:114
const QColor & textColor() const
Definition: Note.cpp:101
void backgroundColorChanged(QColor)
void setTextFont(const QFont &)
Definition: Note.cpp:105
void textColorChanged(QColor)
bool load(XmlStreamReader *, bool preview) override
Load from XML.
Definition: Note.cpp:144
bool printView() override
Definition: Note.cpp:57
bool printPreview() const override
Definition: Note.cpp:69
const QFont & textFont() const
Definition: Note.cpp:110
bool exportView() const override
Definition: Note.cpp:75
const QColor & backgroundColor() const
Definition: Note.cpp:92
Note(const QString &name)
Definition: Note.cpp:44
QColor m_backgroundColor
Definition: Note.h:77
void textFontChanged(QFont)
QFont m_textFont
Definition: Note.h:79
void save(QXmlStreamWriter *) const override
Save as XML.
Definition: Note.cpp:126
NoteView * m_view
Definition: Note.h:76
void setBackgroundColor(const QColor &)
Definition: Note.cpp:87
QColor m_textColor
Definition: Note.h:78
void setTextColor(const QColor &)
Definition: Note.cpp:96
QString m_note
Definition: Note.h:80
const QString & note() const
Definition: Note.cpp:83
QIcon icon() const override
Return an icon to be used for decorating my views.
Definition: Note.cpp:53
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...
void raiseError(const QString &)
#define WRITE_QCOLOR(color)
Definition: macros.h:286
#define READ_QCOLOR(color)
Definition: macros.h:293
#define WRITE_QFONT(font)
Definition: macros.h:361
#define READ_QFONT(font)
Definition: macros.h:370
#define i18n(m)
Definition: nsl_common.h:38