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 "attributeflow.h"
26 #include "flowlayout.h"
27 #include "repositorymanager.h"
28 #include "windowmanager.h"
29 #include "elidedpushbutton.h"
30 
AttributeFlow(QWidget * parent)31 AttributeFlow::AttributeFlow(QWidget *parent) : QWidget(parent)
32 {
33     _layout = new FlowLayout(this, 6, 6);
34     _layout->setMargin(0);
35     this->setLayout(_layout);
36 }
37 
~AttributeFlow()38 AttributeFlow::~AttributeFlow()
39 {
40     this->clear();
41     delete _layout;
42 }
43 
clear()44 void AttributeFlow::clear()
45 {
46     while (!_layout->isEmpty())
47         delete _layout->takeAt(0);
48 }
49 
addCategory(int id)50 void AttributeFlow::addCategory(int id)
51 {
52     ElidedPushButton * widget = createItem(RepositoryManager::getInstance()->getCategoryName(id));
53     _filterDefinitions[widget]._type = FilterType::CATEGORY;
54     _filterDefinitions[widget]._id = id;
55     _filters << widget;
56     _layout->addWidget(widget);
57 }
58 
addProperty(SoundfontInformation::Property property,QString propertyValue)59 void AttributeFlow::addProperty(SoundfontInformation::Property property, QString propertyValue)
60 {
61     ElidedPushButton * widget = createItem(propertyValue);
62     _filterDefinitions[widget]._type = FilterType::PROPERTY;
63     _filterDefinitions[widget]._property = property;
64     _filterDefinitions[widget]._propertyValue = propertyValue;
65     _filters << widget;
66     _layout->addWidget(widget);
67 }
68 
addTag(QString tagName)69 void AttributeFlow::addTag(QString tagName)
70 {
71     ElidedPushButton * widget = createItem(tagName);
72     _filterDefinitions[widget]._type = FilterType::TAG;
73     _filterDefinitions[widget]._tag = tagName;
74     _filters << widget;
75     _layout->addWidget(widget);
76 }
77 
createItem(QString text)78 ElidedPushButton * AttributeFlow::createItem(QString text)
79 {
80     ElidedPushButton * button = new ElidedPushButton(this);
81     button->setTextToElide(text, this->width() - 12);
82     button->setCursor(Qt::PointingHandCursor);
83     connect(button, SIGNAL(clicked(bool)), this, SLOT(onClick(bool)));
84     return button;
85 }
86 
resizeEvent(QResizeEvent * event)87 void AttributeFlow::resizeEvent(QResizeEvent * event)
88 {
89     Q_UNUSED(event)
90 
91     foreach (ElidedPushButton * button, _filters)
92         button->setAvailableWidth(this->width());
93     _layout->setGeometry(QRect(0, 0, this->width(), 0));
94 
95     QWidget::resizeEvent(event);
96 }
97 
onClick(bool checked)98 void AttributeFlow::onClick(bool checked)
99 {
100     Q_UNUSED(checked)
101 
102     // Create a soundfont filter
103     ElidedPushButton * sender = dynamic_cast<ElidedPushButton*>(QObject::sender());
104     if (_filterDefinitions.contains(sender))
105     {
106         SoundfontFilter * filter = new SoundfontFilter();
107         switch (_filterDefinitions[sender]._type)
108         {
109         case FilterType::CATEGORY:
110             filter->setCategory(_filterDefinitions[sender]._id);
111             break;
112         case FilterType::PROPERTY:
113             filter->setProperties(_filterDefinitions[sender]._property, QStringList(_filterDefinitions[sender]._propertyValue));
114             break;
115         case FilterType::TAG:
116             filter->setTags(QStringList(_filterDefinitions[sender]._tag));
117             break;
118         }
119 
120         // Notify the click with the filter
121         emit(itemClicked(filter));
122     }
123 }
124 
polish(QStyle * style)125 void AttributeFlow::polish(QStyle * style)
126 {
127     foreach (ElidedPushButton * button, _filters)
128         style->polish(button);
129 }
130 
hasHeightForWidth() const131 bool AttributeFlow::hasHeightForWidth() const
132 {
133     return true;
134 }
135 
heightForWidth(int width) const136 int AttributeFlow::heightForWidth(int width) const
137 {
138     return _layout->heightForWidth(width);
139 }
140