File indexing completed on 2024-05-12 15:28:00

0001 /***************************************************************************
0002     File                 : NotesDock.cpp
0003     Project              : LabPlot
0004     Description          : Dock for configuring notes
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2016 Garvit Khatri (garvitdelhi@gmail.com)
0007 
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *  This program is free software; you can redistribute it and/or modify   *
0013  *  it under the terms of the GNU General Public License as published by   *
0014  *  the Free Software Foundation; either version 2 of the License, or      *
0015  *  (at your option) any later version.                                    *
0016  *                                                                         *
0017  *  This program is distributed in the hope that it will be useful,        *
0018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0020  *  GNU General Public License for more details.                           *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program; if not, write to the Free Software           *
0024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0025  *   Boston, MA  02110-1301  USA                                           *
0026  *                                                                         *
0027  ***************************************************************************/
0028 
0029 #include "NoteDock.h"
0030 #include "kdefrontend/TemplateHandler.h"
0031 
0032 #include <QDir>
0033 #include <KConfigGroup>
0034 #include <KLocalizedString>
0035 #include <KConfig>
0036 
0037 NoteDock::NoteDock(QWidget *parent) : BaseDock(parent) {
0038     ui.setupUi(this);
0039     m_leName = ui.leName;
0040     m_leComment = ui.leComment;
0041 
0042     connect(ui.leName, &QLineEdit::textChanged, this, &NoteDock::nameChanged);
0043     connect(ui.leComment, &QLineEdit::textChanged, this, &NoteDock::commentChanged);
0044 
0045     connect(ui.kcbBgColor, &KColorButton::changed, this, &NoteDock::backgroundColorChanged);
0046     connect(ui.kcbTextColor, &KColorButton::changed, this, &NoteDock::textColorChanged);
0047     connect(ui.kfrTextFont, &KFontRequester::fontSelected, this, &NoteDock::textFontChanged);
0048 
0049     auto* templateHandler = new TemplateHandler(this, TemplateHandler::ClassName::Worksheet);
0050     ui.gridLayout->addWidget(templateHandler, 8, 3);
0051     templateHandler->show();
0052     connect(templateHandler, &TemplateHandler::loadConfigRequested, this, &NoteDock::loadConfigFromTemplate);
0053     connect(templateHandler, &TemplateHandler::saveConfigRequested, this, &NoteDock::saveConfigAsTemplate);
0054 }
0055 
0056 void NoteDock::setNotesList(QList< Note* > list) {
0057     m_notesList = list;
0058     m_notes = list.first();
0059     m_aspect = list.first();
0060 
0061     m_initializing = true;
0062     ui.leName->setText(m_notes->name());
0063     ui.leName->setStyleSheet("");
0064     ui.leName->setToolTip("");
0065     ui.kcbBgColor->setColor(m_notes->backgroundColor());
0066     ui.kcbTextColor->setColor(m_notes->textColor());
0067     ui.kfrTextFont->setFont(m_notes->textFont());
0068     m_initializing = false;
0069 }
0070 
0071 //*************************************************************
0072 //********** SLOTs for changes triggered in NoteDock **********
0073 //*************************************************************
0074 void NoteDock::backgroundColorChanged(const QColor& color) {
0075     if (m_initializing)
0076         return;
0077 
0078     for (auto* note : m_notesList)
0079         note->setBackgroundColor(color);
0080 }
0081 
0082 void NoteDock::textColorChanged(const QColor& color) {
0083     if (m_initializing)
0084         return;
0085 
0086     for (auto* note : m_notesList)
0087         note->setTextColor(color);
0088 }
0089 
0090 void NoteDock::textFontChanged(const QFont& font) {
0091     if (m_initializing)
0092         return;
0093 
0094     for (auto* note : m_notesList)
0095         note->setTextFont(font);
0096 }
0097 
0098 //*************************************************************
0099 //************************* Settings **************************
0100 //*************************************************************
0101 void NoteDock::loadConfigFromTemplate(KConfig& config) {
0102     QString name;
0103     int index = config.name().lastIndexOf(QLatin1String("/"));
0104     if (index != -1)
0105         name = config.name().right(config.name().size() - index - 1);
0106     else
0107         name = config.name();
0108 
0109     KConfigGroup group = config.group("Notes");
0110     ui.kcbBgColor->setColor(group.readEntry("BackgroundColor", m_notes->backgroundColor()));
0111     ui.kcbTextColor->setColor(group.readEntry("TextColor", m_notes->textColor()));
0112     ui.kfrTextFont->setFont(group.readEntry("TextColor", m_notes->textFont()));
0113 }
0114 
0115 void NoteDock::saveConfigAsTemplate(KConfig& config) {
0116     KConfigGroup group = config.group("Notes");
0117 
0118     group.writeEntry("BackgroundColor", ui.kcbBgColor->color());
0119     group.writeEntry("TextColor", ui.kcbTextColor->color());
0120     group.writeEntry("TextFont", ui.kfrTextFont->font());
0121 }