1 /***************************************************************************
2 **                                                                        **
3 **  Polyphone, a soundfont editor                                         **
4 **  Copyright (C) 2013-2019 Davy Triponney                                **
5 **                                                                        **
6 **  This program is free software: you can redistribute it and/or modify  **
7 **  it under the terms of the GNU General Public License as published by  **
8 **  the Free Software Foundation, either version 3 of the License, or     **
9 **  (at your option) any later version.                                   **
10 **                                                                        **
11 **  This program is distributed in the hope that it will be useful,       **
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          **
14 **  GNU General Public License for more details.                          **
15 **                                                                        **
16 **  You should have received a copy of the GNU General Public License     **
17 **  along with this program. If not, see http://www.gnu.org/licenses/.    **
18 **                                                                        **
19 ****************************************************************************
20 **           Author: Davy Triponney                                       **
21 **  Website/Contact: https://www.polyphone-soundfonts.com                 **
22 **             Date: 01.01.2013                                           **
23 ***************************************************************************/
24 
25 #include "soundfontcell.h"
26 #include "ui_soundfontcell.h"
27 #include "soundfontinformation.h"
28 #include "soundfontfilter.h"
29 #include "contextmanager.h"
30 #include "repositorymanager.h"
31 
32 SoundfontCell::IconContainer * SoundfontCell::s_icons = nullptr;
33 
34 SoundfontCell::SoundfontCell(SoundfontInformation* soundfontInfo, QWidget *parent) :
35     QWidget(parent),
36     ui(new Ui::SoundfontCell),
37     _active(true),
38     _soundfontId(soundfontInfo->getId())
39 {
40     ui->setupUi(this);
41     this->installEventFilter(this);
42 
43     if (s_icons == nullptr)
44         s_icons = new IconContainer();
45 
46     // Title
47     ui->labelTitle->setTextToElide(soundfontInfo->getTitle());
48 
49     // Author
50     _authorTextNoColor = soundfontInfo->getAuthor();
51     _authorTextNoColor = "<a style=\"text-decoration:none;color:%1;\" href=\"" + _authorTextNoColor + "\">" + _authorTextNoColor + "</a>";
52 
53     // Attributes
54     connect(ui->line3, SIGNAL(itemClicked(SoundfontFilter*)), this, SIGNAL(itemClicked(SoundfontFilter*)));
55     ui->line3->addCategory(soundfontInfo->getCategoryId());
56     foreach (SoundfontInformation::Property key, soundfontInfo->getProperties().keys())
57         foreach (QString value, soundfontInfo->getProperties()[key])
58             ui->line3->addProperty(key, value);
59     foreach (QString tag, soundfontInfo->getTags())
60         ui->line3->addTag(tag);
61 
62     // Style when the cell is activated and when it's not
63     QColor buttonBackground = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND);
64     QColor buttonText = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT);
65     QColor buttonBackgroundHover = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND, ThemeManager::HOVERED);
66     QColor buttonBackgroundHover2 = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT, ThemeManager::HOVERED);
67     _normalStyleSheet = QString("QPushButton{background-color:") + buttonBackground.name() + "; color:" + buttonText.name() +
68             ";border:0px;padding:5px;border-radius:4px;}" +
69             "QPushButton:hover{ background-color:" + buttonBackgroundHover.name() + ";}";
70     _activeStyleSheet = QString("QPushButton{background-color:") + buttonText.name() + "; color:" + buttonBackground.name() +
71             ";border:0px;padding:5px;border-radius:4px;}" +
72             "QPushButton:hover{ background-color:" + buttonBackgroundHover2.name() + ";}" +
73             "ElidedLabel{color:" + ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT).name() + ";}";
74 
75     // Initialize the style
76     this->setActive(false);
77 }
78 
79 SoundfontCell::~SoundfontCell()
80 {
81     delete ui;
82 }
83 
84 void SoundfontCell::setActive(bool isActive)
85 {
86     if (isActive && !_active)
87     {
88         this->setStyleSheet(_activeStyleSheet);
89 
90         // Author texts
91         ui->iconAuthor->setPixmap(s_icons->_userIconSelected);
92         QString linkColor = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT).name();
93         ui->labelAuthor->setText(QString(_authorTextNoColor).arg(linkColor));
94     }
95     else if (!isActive && _active)
96     {
97         this->setStyleSheet(_normalStyleSheet);
98 
99         // Author texts
100         ui->iconAuthor->setPixmap(s_icons->_userIcon);
101         QString linkColor = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND).name();
102         ui->labelAuthor->setText(QString(_authorTextNoColor).arg(linkColor));
103     }
104     _active = isActive;
105 
106     // So that the stylesheet updates...
107     this->style()->polish(ui->labelTitle);
108     ui->line3->polish(this->style());
109 }
110 
111 SoundfontCell::IconContainer::IconContainer()
112 {
113     // Base icons
114     _userIcon = ContextManager::theme()->getColoredSvg(":/icons/user.svg", QSize(16, 16), ThemeManager::ColorType::LIST_TEXT);
115 
116     // Icons under selection
117     _userIconSelected = ContextManager::theme()->getColoredSvg(":/icons/user.svg", QSize(16, 16), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
118 }
119 
120 void SoundfontCell::on_labelAuthor_linkActivated(const QString &link)
121 {
122     SoundfontFilter * filter = new SoundfontFilter();
123     filter->setSearchText(QString("Author:\"%1\"").arg(link));
124     emit(itemClicked(filter));
125 }
126 
127 int SoundfontCell::heightForWidth(int width) const
128 {
129     // 2 * 9px (margins) + 2 * 6px (spaces) => 30px
130     return 30 + ui->line1->height() + ui->line2->height() + ui->line3->heightForWidth(width - 18);
131 }
132 
133 bool SoundfontCell::hasHeightForWidth() const
134 {
135     return true;
136 }
137 
138 void SoundfontCell::mouseDoubleClickEvent(QMouseEvent *event)
139 {
140     if (event->button() == Qt::LeftButton && _active)
141     {
142         RepositoryManager::getInstance()->openSoundfont(_soundfontId, true);
143         event->accept();
144         return;
145     }
146 
147     QWidget::mouseDoubleClickEvent(event);
148 }
149