1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "notelabel.h"
25 #include "support/utils.h"
26 #include <QVBoxLayout>
27 #include <QFont>
28 #include <QVariant>
29
init(QLabel * label)30 static void init(QLabel *label)
31 {
32 static const int constMinFontSize=9;
33
34 label->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop);
35 label->setWordWrap(true);
36 if (label->font().pointSize()>constMinFontSize) {
37 label->setFont(Utils::smallFont(label->font()));
38 }
39 }
40
init(QWidget * p,bool url)41 static QLabel * init(QWidget *p, bool url)
42 {
43 int layoutSpacing=Utils::layoutSpacing(p);
44 int spacing=p->fontMetrics().height()*(Utils::limitedHeight(p) ? 0.25 : 1.0)-layoutSpacing;
45 if (spacing<layoutSpacing) {
46 spacing=layoutSpacing;
47 }
48
49 QVBoxLayout *l=new QVBoxLayout(p);
50 l->setMargin(0);
51 l->setSpacing(0);
52 QLabel *label;
53 if (url) {
54 label=new UrlLabel(p);
55 } else {
56 label=new StateLabel(p);
57 }
58 init(label);
59 l->addItem(new QSpacerItem(2, spacing, QSizePolicy::Fixed, QSizePolicy::Fixed));
60 l->addWidget(label);
61 return label;
62 }
63
formatText(const QString & text)64 QString NoteLabel::formatText(const QString &text)
65 {
66 return tr("<i><b>NOTE:</b> %1</i>").arg(text);
67 }
68
NoteLabel(QWidget * parent)69 NoteLabel::NoteLabel(QWidget *parent)
70 : QWidget(parent)
71 {
72 label=static_cast<StateLabel *>(init(this, false));
73 label->setTextInteractionFlags(Qt::TextBrowserInteraction);
74 label->setOpenExternalLinks(true);
75 }
76
setProperty(const char * name,const QVariant & value)77 void NoteLabel::setProperty(const char *name, const QVariant &value)
78 {
79 if (name && !strcmp(name, "text") && QVariant::String==value.type()) {
80 setText(value.toString());
81 }
82 }
83
UrlNoteLabel(QWidget * parent)84 UrlNoteLabel::UrlNoteLabel(QWidget *parent)
85 : QWidget(parent)
86 {
87 label=static_cast<UrlLabel *>(init(this, true));
88 connect(label, SIGNAL(leftClickedUrl()), this, SIGNAL(leftClickedUrl()));
89 }
90
setProperty(const char * name,const QVariant & value)91 void UrlNoteLabel::setProperty(const char *name, const QVariant &value)
92 {
93 if (name && !strcmp(name, "text") && QVariant::String==value.type()) {
94 setText(value.toString());
95 }
96 }
97
PlainNoteLabel(QWidget * parent)98 PlainNoteLabel::PlainNoteLabel(QWidget *parent)
99 : StateLabel(parent)
100 {
101 init(this);
102 setTextInteractionFlags(Qt::TextBrowserInteraction);
103 setOpenExternalLinks(true);
104 }
105
PlainUrlNoteLabel(QWidget * parent)106 PlainUrlNoteLabel::PlainUrlNoteLabel(QWidget *parent)
107 : UrlLabel(parent)
108 {
109 init(this);
110 }
111
112 #include "moc_notelabel.cpp"
113