1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2016 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "sharewidget.h"
|
---|
20 | #include "sharedata.h"
|
---|
21 | #include "images.h"
|
---|
22 |
|
---|
23 | #include <QHBoxLayout>
|
---|
24 | #include <QDesktopServices>
|
---|
25 | #include <QSettings>
|
---|
26 | #include <QEvent>
|
---|
27 | #include <QTime>
|
---|
28 |
|
---|
29 | //#define TEST_SHAREWIDGET
|
---|
30 |
|
---|
31 | #define SHAREBUTTON_MIN QSize(24,24)
|
---|
32 | #define SHAREBUTTON_MAX QSize(32,32)
|
---|
33 |
|
---|
34 | ShareButton::ShareButton(const QString icon_name, const QString & tooltip, QWidget * parent)
|
---|
35 | : QPushButton("", parent)
|
---|
36 | {
|
---|
37 | setAttribute(Qt::WA_Hover, true);
|
---|
38 |
|
---|
39 | setIconSize(SHAREBUTTON_MIN);
|
---|
40 | setIcon(Images::icon(icon_name));
|
---|
41 | setToolTip(tooltip);
|
---|
42 | }
|
---|
43 |
|
---|
44 | QSize ShareButton::sizeHint() const {
|
---|
45 | QSize s(SHAREBUTTON_MAX);
|
---|
46 | s += QSize(4,4);
|
---|
47 | return s;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ShareButton::enterEvent(QEvent *) {
|
---|
51 | //qDebug("ShareButton::enterEvent");
|
---|
52 | setIconSize(SHAREBUTTON_MAX);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void ShareButton::leaveEvent(QEvent *) {
|
---|
56 | //qDebug("ShareButton::leaveEvent");
|
---|
57 | setIconSize(SHAREBUTTON_MIN);
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | ShareWidget::ShareWidget(QSettings * settings, QWidget * parent, Qt::WindowFlags f)
|
---|
63 | : QWidget(parent,f)
|
---|
64 | , set(settings)
|
---|
65 | , actions_taken(0)
|
---|
66 | , count(0)
|
---|
67 | , display(Random)
|
---|
68 | {
|
---|
69 | QTime now = QTime::currentTime();
|
---|
70 | qsrand(now.msec());
|
---|
71 |
|
---|
72 | donate_button = new ShareButton("paypal", "", this);
|
---|
73 | fb_button = new ShareButton("social_facebook", "", this);
|
---|
74 | twitter_button = new ShareButton("social_twitter", "", this);
|
---|
75 |
|
---|
76 | support_button = new QPushButton(this);
|
---|
77 | support_button->setObjectName("support_button");
|
---|
78 | connect(support_button, SIGNAL(clicked()), this, SIGNAL(supportClicked()));
|
---|
79 |
|
---|
80 | QHBoxLayout * hlayout = new QHBoxLayout;
|
---|
81 | hlayout->setSpacing(0);
|
---|
82 | //hlayout->addSpacerItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
|
---|
83 | hlayout->addWidget(donate_button);
|
---|
84 | hlayout->addWidget(fb_button);
|
---|
85 | hlayout->addWidget(twitter_button);
|
---|
86 | //hlayout->addSpacerItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
|
---|
87 |
|
---|
88 | QVBoxLayout * vlayout = new QVBoxLayout(this);
|
---|
89 | vlayout->setSpacing(0);
|
---|
90 | vlayout->addLayout(hlayout);
|
---|
91 | vlayout->addWidget(support_button);
|
---|
92 |
|
---|
93 | /*
|
---|
94 | setBackgroundRole(QPalette::Window);
|
---|
95 | setAutoFillBackground(true);
|
---|
96 | setStyleSheet("background: yellow;");
|
---|
97 | */
|
---|
98 |
|
---|
99 | connect(donate_button, SIGNAL(clicked()), this, SLOT(donate()));
|
---|
100 | connect(fb_button, SIGNAL(clicked()), this, SLOT(facebook()));
|
---|
101 | connect(twitter_button, SIGNAL(clicked()), this, SLOT(twitter()));
|
---|
102 |
|
---|
103 | retranslateStrings();
|
---|
104 | loadConfig();
|
---|
105 | updateButtons();
|
---|
106 | }
|
---|
107 |
|
---|
108 | ShareWidget::~ShareWidget() {
|
---|
109 | saveConfig();
|
---|
110 | }
|
---|
111 |
|
---|
112 | void ShareWidget::retranslateStrings() {
|
---|
113 | donate_button->setToolTip(tr("Donate with PayPal"));
|
---|
114 | fb_button->setToolTip(tr("Share SMPlayer in Facebook"));
|
---|
115 | twitter_button->setToolTip(tr("Share SMPlayer in Twitter"));
|
---|
116 |
|
---|
117 | support_button->setText(tr("Support SMPlayer"));
|
---|
118 | support_button->setToolTip(tr("Donate / Share SMPlayer with your friends"));
|
---|
119 | }
|
---|
120 |
|
---|
121 | void ShareWidget::updateButtons() {
|
---|
122 | qDebug("ShareWidget::updateButtons: actions_taken: %d", actions_taken);
|
---|
123 |
|
---|
124 | if ((actions_taken & ShareData::Donate) > 0) donate_button->hide();
|
---|
125 | if ((actions_taken & ShareData::Facebook) > 0) fb_button->hide();
|
---|
126 | if ((actions_taken & ShareData::Twitter) > 0) twitter_button->hide();
|
---|
127 |
|
---|
128 | if (actions_taken > 0) support_button->hide();
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void ShareWidget::loadConfig() {
|
---|
133 | if (set) {
|
---|
134 | set->beginGroup("reminder");
|
---|
135 | actions_taken = set->value("action", 0).toInt();
|
---|
136 | count = set->value("count", 0).toInt();
|
---|
137 | display = set->value("show", Random).toInt();
|
---|
138 | set->endGroup();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | void ShareWidget::saveConfig() {
|
---|
143 | if (set) {
|
---|
144 | set->beginGroup("reminder");
|
---|
145 | set->setValue("action", actions_taken);
|
---|
146 | set->setValue("count", count);
|
---|
147 | set->setValue("show", display);
|
---|
148 | set->endGroup();
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | void ShareWidget::setActions(int a) {
|
---|
153 | actions_taken = a;
|
---|
154 | updateButtons();
|
---|
155 | saveConfig();
|
---|
156 | }
|
---|
157 |
|
---|
158 | void ShareWidget::setActionPerformed(int action) {
|
---|
159 | actions_taken |= action;
|
---|
160 | updateButtons();
|
---|
161 | }
|
---|
162 |
|
---|
163 | void ShareWidget::setVisible(bool visible) {
|
---|
164 | //qDebug("ShareWidget::setVisible: %d", visible);
|
---|
165 |
|
---|
166 | #if 1
|
---|
167 | if (!visible) {
|
---|
168 | QWidget::setVisible(false);
|
---|
169 | } else {
|
---|
170 | bool v = true;
|
---|
171 | switch (display) {
|
---|
172 | case Never: v = false; break;
|
---|
173 | case Always: v = visible; break;
|
---|
174 | case Random: {
|
---|
175 | if (actions_taken == 7) {
|
---|
176 | // User already clicked all buttons
|
---|
177 | v = false;
|
---|
178 | } else {
|
---|
179 | int r = (qrand() % 10);
|
---|
180 | //qDebug("ShareWidget::setVisible: r: %d", r);
|
---|
181 | if (r != 1) v = false;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | //qDebug("ShareWidget::setVisible: v: %d", v);
|
---|
186 | QWidget::setVisible(v);
|
---|
187 | }
|
---|
188 | #else
|
---|
189 | QWidget::setVisible(visible);
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 |
|
---|
193 | void ShareWidget::donate() {
|
---|
194 | qDebug("ShareWidget::donate");
|
---|
195 | setActionPerformed(ShareData::Donate);
|
---|
196 | #ifndef TEST_SHAREWIDGET
|
---|
197 | QDesktopServices::openUrl( ShareData::donateUrl() );
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 |
|
---|
201 | void ShareWidget::facebook() {
|
---|
202 | qDebug("ShareWidget::facebook");
|
---|
203 | setActionPerformed(ShareData::Facebook);
|
---|
204 | #ifndef TEST_SHAREWIDGET
|
---|
205 | QDesktopServices::openUrl( ShareData::facebookUrl() );
|
---|
206 | #endif
|
---|
207 | }
|
---|
208 |
|
---|
209 | void ShareWidget::twitter() {
|
---|
210 | qDebug("ShareWidget::twitter");
|
---|
211 | setActionPerformed(ShareData::Twitter);
|
---|
212 | #ifndef TEST_SHAREWIDGET
|
---|
213 | QDesktopServices::openUrl( ShareData::twitterUrl() );
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 |
|
---|
217 | // Language change stuff
|
---|
218 | void ShareWidget::changeEvent(QEvent *e) {
|
---|
219 | if (e->type() == QEvent::LanguageChange) {
|
---|
220 | retranslateStrings();
|
---|
221 | } else {
|
---|
222 | QWidget::changeEvent(e);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | #include "moc_sharewidget.cpp"
|
---|
227 |
|
---|