Sayonara Player
SayonaraWidgetTemplate.h
1 /* SayonaraWidgetTemplate.h */
2 
3 /* Copyright (C) 2011-2016 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 #ifndef SAYONARAWIDGETTEMPLATE_H
24 #define SAYONARAWIDGETTEMPLATE_H
25 
26 #include <QObject>
27 #include <QWidget>
28 #include <QComboBox>
29 #include <QPalette>
30 #include <QColor>
31 #include <QShortcut>
32 
33 #include "Helper/Settings/SayonaraClass.h"
34 
35 #define combo_current_index_changed_int static_cast<void (QComboBox::*) (int)>(&QComboBox::currentIndexChanged)
36 #define combo_activated_int static_cast<void (QComboBox::*) (int)>(&QComboBox::activated)
37 #define spinbox_value_changed_int static_cast<void (QSpinBox::*) (int)>(&QSpinBox::valueChanged)
38 
39 template<typename T>
47  public T,
48  protected SayonaraClass
49 {
50 
51 public:
52  SayonaraWidgetTemplate(QWidget* parent=nullptr) :
53  T(parent),
55  {
56 
57  }
58 
59  virtual ~SayonaraWidgetTemplate(){
60 
61  }
62 
67  bool is_dark() const
68  {
69  bool dark = (_settings->get(Set::Player_Style) == 1);
70 
71  QPalette palette = this->palette();
72  QColor color = palette.color(QPalette::Normal, QPalette::Background);
73 
74  if(color.lightness() < 128 || dark){
75  return true;
76  }
77 
78  else{
79  return false;
80  }
81  }
82 
83 
84 
85 protected:
86 
87  QString elide_text(const QString &text, QWidget *widget, int max_lines){
88 
89  QFontMetrics metric = widget->fontMetrics();
90  int width = widget->width();
91 
92  QStringList splitted = text.split(" ");
93  QStringList ret;
94  QString tmp;
95  QString line;
96 
97  for( const QString& str : splitted){
98 
99  tmp = line + str;
100 
101  if(metric.boundingRect(tmp).width() > width){
102  ret << line;
103 
104  if(ret.size() == max_lines){
105  line = "";
106  break;
107  }
108 
109  line = str;
110  }
111 
112  else{
113  line += str + " ";
114  }
115  }
116 
117 
118  QString final_str;
119  if(ret.isEmpty()){
120  final_str = text;
121  }
122 
123  else if(line.isEmpty()){
124  final_str = ret.join("\n");
125  final_str += "...";
126  }
127 
128  else {
129  final_str = ret.join("\n") + line;
130  }
131 
132 
133  return final_str;
134  }
135 };
136 
137 
138 #endif // SAYONARAWIDGETTEMPLATE_H
The SayonaraClass class provides access to Settings and notifications.
Definition: SayonaraClass.h:31
Template for Sayonara Widgets. This template is responsible for holding a reference to the settings a...
Definition: SayonaraWidgetTemplate.h:46
bool is_dark() const
Returns, if the current skin is dark.
Definition: SayonaraWidgetTemplate.h:67