File indexing completed on 2024-05-12 03:48:26

0001 /*
0002     File                 : NotesView.cpp
0003     Project              : LabPlot
0004     Description          : Notes View for taking notes
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016-2016 Garvit Khatri <garvitdelhi@gmail.com>
0007     SPDX-FileCopyrightText: 2016-2024 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "NoteView.h"
0012 #include "backend/note/Note.h"
0013 
0014 #include <QHBoxLayout>
0015 #include <QPrinter>
0016 #include <QTextEdit>
0017 
0018 NoteView::NoteView(Note* note)
0019     : m_note(note)
0020     , m_textEdit(new QTextEdit(this)) {
0021     auto* layout = new QHBoxLayout(this);
0022     layout->setContentsMargins(0, 0, 0, 0);
0023 
0024     QPalette palette = m_textEdit->palette();
0025     palette.setColor(QPalette::Base, m_note->backgroundColor());
0026     palette.setColor(QPalette::Text, m_note->textColor());
0027 
0028     m_textEdit->setPalette(palette);
0029     m_textEdit->setFont(m_note->textFont());
0030     m_textEdit->setText(m_note->note());
0031 
0032     layout->addWidget(m_textEdit);
0033 
0034     connect(m_note, &Note::backgroundColorChanged, this, &NoteView::backgroundColorChanged);
0035     connect(m_note, &Note::textColorChanged, this, &NoteView::textColorChanged);
0036     connect(m_note, &Note::textFontChanged, this, &NoteView::textFontChanged);
0037     connect(m_textEdit, &QTextEdit::textChanged, this, &NoteView::textChanged);
0038 }
0039 
0040 void NoteView::print(QPrinter* printer) const {
0041     m_textEdit->print(printer);
0042 }
0043 
0044 void NoteView::textChanged() {
0045     m_note->setNote(m_textEdit->toPlainText());
0046 }
0047 
0048 void NoteView::backgroundColorChanged(QColor color) {
0049     QString red = QString::number(color.red());
0050     QString green = QString::number(color.green());
0051     QString blue = QString::number(color.blue());
0052     m_textEdit->setStyleSheet(QStringLiteral("QTextEdit{background-color: rgb(%1, %2, %3);}").arg(red, green, blue));
0053 }
0054 
0055 void NoteView::textFontChanged(const QFont& font) {
0056     m_textEdit->setFont(font);
0057 }
0058 
0059 void NoteView::textColorChanged(QColor color) {
0060     m_textEdit->setTextColor(color);
0061 }