1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2 *
3 * @file NoteParameter.cpp
4 *
5 * Copyright 2017 Sebastien Fourey
6 *
7 * This file is part of G'MIC-Qt, a generic plug-in for raster graphics
8 * editors, offering hundreds of filters thanks to the underlying G'MIC
9 * image processing framework.
10 *
11 * gmic_qt is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * gmic_qt is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with gmic_qt. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25 #include "FilterParameters/NoteParameter.h"
26 #include <QDebug>
27 #include <QDesktopServices>
28 #include <QGridLayout>
29 #include <QLabel>
30 #include <QUrl>
31 #include "Common.h"
32 #include "DialogSettings.h"
33 #include "HtmlTranslator.h"
34
NoteParameter(QObject * parent)35 NoteParameter::NoteParameter(QObject * parent) : AbstractParameter(parent, false), _label(nullptr) {}
36
~NoteParameter()37 NoteParameter::~NoteParameter()
38 {
39 delete _label;
40 }
41
addTo(QWidget * widget,int row)42 bool NoteParameter::addTo(QWidget * widget, int row)
43 {
44 _grid = dynamic_cast<QGridLayout *>(widget->layout());
45 Q_ASSERT_X(_grid, __PRETTY_FUNCTION__, "No grid layout in widget");
46 _row = row;
47 delete _label;
48 _label = new QLabel(_text, widget);
49 _label->setTextFormat(Qt::RichText);
50 _label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
51 _label->setWordWrap(true);
52 connect(_label, SIGNAL(linkActivated(QString)), this, SLOT(onLinkActivated(QString)));
53 _grid->addWidget(_label, row, 0, 1, 3);
54 return true;
55 }
56
textValue() const57 QString NoteParameter::textValue() const
58 {
59 return QString();
60 }
61
setValue(const QString &)62 void NoteParameter::setValue(const QString &) {}
63
reset()64 void NoteParameter::reset() {}
65
initFromText(const char * text,int & textLength)66 bool NoteParameter::initFromText(const char * text, int & textLength)
67 {
68 QList<QString> list = parseText("note", text, textLength);
69 if (list.isEmpty()) {
70 return false;
71 }
72 _text = list[1].trimmed().remove(QRegExp("^\"")).remove(QRegExp("\"$")).replace(QString("\\\""), "\"");
73 _text.replace(QString("\\n"), "<br/>");
74
75 if (DialogSettings::darkThemeEnabled()) {
76 _text.replace(QRegExp("color\\s*=\\s*\"purple\""), QString("color=\"#ff00ff\""));
77 _text.replace(QRegExp("foreground\\s*=\\s*\"purple\""), QString("foreground=\"#ff00ff\""));
78 _text.replace(QRegExp("color\\s*=\\s*\"blue\""), QString("color=\"#9b9bff\""));
79 _text.replace(QRegExp("foreground\\s*=\\s*\"blue\""), QString("foreground=\"#9b9bff\""));
80 }
81
82 _text.replace(QRegExp("color\\s*=\\s*\""), QString("style=\"color:"));
83 _text.replace(QRegExp("foreground\\s*=\\s*\""), QString("style=\"color:"));
84 _text = HtmlTranslator::fromUtf8Escapes(_text);
85 return true;
86 }
87
onLinkActivated(const QString & link)88 void NoteParameter::onLinkActivated(const QString & link)
89 {
90 QDesktopServices::openUrl(QUrl(link));
91 }
92