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 "soundfontcellfull.h"
26 #include "ui_soundfontcellfull.h"
27 #include "soundfontinformation.h"
28 #include "repositorymanager.h"
29 #include "contextmanager.h"
30 
31 SoundfontCellFull::IconContainer * SoundfontCellFull::s_icons = nullptr;
32 
33 SoundfontCellFull::SoundfontCellFull(SoundfontInformation* soundfontInfo, QWidget *parent) :
34     QWidget(parent),
35     ui(new Ui::SoundfontCellFull),
36     _active(true),
37     _soundfontId(soundfontInfo->getId())
38 {
39     ui->setupUi(this);
40 
41     if (s_icons == nullptr)
42         s_icons = new IconContainer();
43 
44     // Title
45     ui->labelTitle->setTextToElide(soundfontInfo->getTitle());
46 
47     // Numbers
48     ui->labelCommentNumber->setText(QString::number(soundfontInfo->getCommentNumber()));
49     ui->labelDownloadNumber->setText(QString::number(soundfontInfo->getDownloadNumber()));
50     ui->widgetStars->setScore(soundfontInfo->getRating());
51 
52     // Author
53     _authorTextNoColor = soundfontInfo->getAuthor();
54     _authorTextNoColor = "<a style=\"text-decoration:none;color:%1;\" href=\"" + _authorTextNoColor + "\">" + _authorTextNoColor + "</a>";
55 
56     // Date
57     ui->labelDate->setText(soundfontInfo->getDateTime().toString(Qt::SystemLocaleShortDate));
58 
59     // License
60     ui->labelLicense->setTextToElide(RepositoryManager::getInstance()->getLicenseLabel(soundfontInfo->getLicense()),
61                                      RepositoryManager::getInstance()->getLicenseLink(soundfontInfo->getLicense()));
62 
63     // Attributes
64     connect(ui->line3, SIGNAL(itemClicked(SoundfontFilter*)), this, SIGNAL(itemClicked(SoundfontFilter*)));
65     ui->line3->addCategory(soundfontInfo->getCategoryId());
66     foreach (SoundfontInformation::Property key, soundfontInfo->getProperties().keys())
67         foreach (QString value, soundfontInfo->getProperties()[key])
68             ui->line3->addProperty(key, value);
69     foreach (QString tag, soundfontInfo->getTags())
70         ui->line3->addTag(tag);
71 
72     // Style when the cell is activated and when it's not
73     QColor buttonBackground = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND);
74     QColor buttonText = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT);
75     QColor buttonBackgroundHover = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND, ThemeManager::HOVERED);
76     QColor buttonBackgroundHover2 = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT, ThemeManager::HOVERED);
77     QString tmp = QString("QPushButton{background-color:%1; color:%2;border:0px;padding:5px;border-radius:4px;}") +
78             "QPushButton:hover{ background-color:%3;}QLabel#labelAuthor,ElidedLabel#labelLicense{color:%1;}";
79     _normalStyleSheet = tmp.arg(buttonBackground.name()).arg(buttonText.name()).arg(buttonBackgroundHover.name());
80     _activeStyleSheet = tmp.arg(buttonText.name()).arg(buttonBackground.name()).arg(buttonBackgroundHover2.name()) +
81             "QLabel{color:" + ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT).name() + ";}";
82 
83     // Initialize the style
84     this->setActive(false);
85 }
86 
87 SoundfontCellFull::~SoundfontCellFull()
88 {
89     delete ui;
90 }
91 
92 void SoundfontCellFull::setActive(bool isActive)
93 {
94     if (isActive && !_active)
95     {
96         // Colors
97         this->setStyleSheet(_activeStyleSheet);
98 
99         // Icons
100         ui->iconComment->setPixmap(s_icons->_commentIconSelected);
101         ui->iconDownload->setPixmap(s_icons->_downloadIconSelected);
102         ui->iconAuthor->setPixmap(s_icons->_userIconSelected);
103         ui->iconDate->setPixmap(s_icons->_dateIconSelected);
104         ui->iconLicense->setPixmap(s_icons->_copyrightIconSelected);
105 
106         // Author and license texts
107         QColor linkColor = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_TEXT);
108         ui->labelAuthor->setText(QString(_authorTextNoColor).arg(linkColor.name()));
109     }
110     else if (!isActive && _active)
111     {
112         // Colors
113         this->setStyleSheet(_normalStyleSheet);
114 
115         // Icons
116         ui->iconComment->setPixmap(s_icons->_commentIcon);
117         ui->iconDownload->setPixmap(s_icons->_downloadIcon);
118         ui->iconAuthor->setPixmap(s_icons->_userIcon);
119         ui->iconDate->setPixmap(s_icons->_dateIcon);
120         ui->iconLicense->setPixmap(s_icons->_copyrightIcon);
121 
122         // Author and license texts
123         QColor linkColor = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND);
124         ui->labelAuthor->setText(QString(_authorTextNoColor).arg(linkColor.name()));
125     }
126     _active = isActive;
127 
128     // Stars
129     ui->widgetStars->setActive(isActive);
130 
131     // So that the stylesheet updates...
132     this->style()->polish(ui->labelTitle);
133     this->style()->polish(ui->labelDownloadNumber);
134     this->style()->polish(ui->labelCommentNumber);
135     this->style()->polish(ui->labelDate);
136     this->style()->polish(ui->labelLicense);
137     ui->line3->polish(this->style());
138 }
139 
140 SoundfontCellFull::IconContainer::IconContainer()
141 {
142     // Base icons
143     _commentIcon = ContextManager::theme()->getColoredSvg(":/icons/comment.svg", QSize(24, 24), ThemeManager::ColorType::LIST_TEXT);
144     _downloadIcon = ContextManager::theme()->getColoredSvg(":/icons/download.svg", QSize(24, 24), ThemeManager::ColorType::LIST_TEXT);
145     _userIcon = ContextManager::theme()->getColoredSvg(":/icons/user.svg", QSize(16, 16), ThemeManager::ColorType::LIST_TEXT);
146     _dateIcon = ContextManager::theme()->getColoredSvg(":/icons/calendar.svg", QSize(16, 16), ThemeManager::ColorType::LIST_TEXT);
147     _copyrightIcon = ContextManager::theme()->getColoredSvg(":/icons/copyright.svg", QSize(16, 16), ThemeManager::ColorType::LIST_TEXT);
148 
149     // Icons under selection
150     _commentIconSelected = ContextManager::theme()->getColoredSvg(":/icons/comment.svg", QSize(24, 24), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
151     _downloadIconSelected = ContextManager::theme()->getColoredSvg(":/icons/download.svg", QSize(24, 24), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
152     _userIconSelected = ContextManager::theme()->getColoredSvg(":/icons/user.svg", QSize(16, 16), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
153     _dateIconSelected = ContextManager::theme()->getColoredSvg(":/icons/calendar.svg", QSize(16, 16), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
154     _copyrightIconSelected = ContextManager::theme()->getColoredSvg(":/icons/copyright.svg", QSize(16, 16), ThemeManager::ColorType::HIGHLIGHTED_TEXT);
155 }
156 
157 void SoundfontCellFull::on_labelAuthor_linkActivated(const QString &link)
158 {
159     SoundfontFilter * filter = new SoundfontFilter();
160     filter->setSearchText(QString("Author:\"%1\"").arg(link));
161     emit(itemClicked(filter));
162 }
163 
164 int SoundfontCellFull::heightForWidth(int width) const
165 {
166     // 2 * 9px (margins) + 2 * 6px (spaces) => 30px
167     return 30 + ui->line1->height() + ui->line2->height() + ui->line3->heightForWidth(width - 18);
168 }
169 
170 bool SoundfontCellFull::hasHeightForWidth() const
171 {
172     return true;
173 }
174 
175 void SoundfontCellFull::mouseDoubleClickEvent(QMouseEvent *event)
176 {
177     if (event->button() == Qt::LeftButton && _active)
178     {
179         event->accept();
180         RepositoryManager::getInstance()->openSoundfont(_soundfontId, false);
181         return;
182     }
183 
184     QWidget::mouseDoubleClickEvent(event);
185 }
186