1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2012, Teo Mrnjavac <teo@kde.org>
6
* Tomahawk 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.
11
* Tomahawk 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.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "SocialWidget.h"
21
#include "ui_SocialWidget.h"
23
#include "GlobalActionManager.h"
26
#include "utils/ImageRegistry.h"
27
#include "utils/TomahawkUtilsGui.h"
28
#include "utils/Logger.h"
32
#include <QPropertyAnimation>
34
#define ARROW_HEIGHT 6
37
SocialWidget::SocialWidget( QWidget* parent )
38
: QWidget( parent ) // this is on purpose!
39
, ui( new Ui::SocialWidget )
41
, m_parentRect( parent->rect() )
44
setWindowFlags( Qt::Popup | Qt::FramelessWindowHint );
46
setAutoFillBackground( false );
47
setAttribute( Qt::WA_TranslucentBackground, true );
48
setAttribute( Qt::WA_NoSystemBackground, true );
50
TomahawkUtils::unmarginLayout( layout() );
53
ui->verticalLayout->setContentsMargins( 12, 4, 12, 12 );
54
setContentsMargins( contentsMargins().left() + 2, contentsMargins().top() + 2,
55
contentsMargins().right() + 2, contentsMargins().bottom() + 2 + ARROW_HEIGHT );
57
ui->verticalLayout->setContentsMargins( 12, 0, 12, 16 );
58
setContentsMargins( contentsMargins().left() + 2, 4,
59
contentsMargins().right() + 2, contentsMargins().bottom() + 2 + ARROW_HEIGHT );
60
ui->horizontalLayout->setContentsMargins( 0, 0, 0, 0 );
64
m_timer.setSingleShot( true );
65
connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
67
ui->charsLeftLabel->setForegroundRole( QPalette::Text );
68
ui->charsLeftLabel->setStyleSheet( "text: black" );
69
ui->buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Tweet" ) );
70
ui->buttonBox->button( QDialogButtonBox::Ok )->setIcon( ImageRegistry::instance()->icon( RESPATH "images/tweet.svg" ) );
71
ui->buttonBox->button( QDialogButtonBox::Cancel )->setIcon( ImageRegistry::instance()->icon( RESPATH "images/cancel.svg" ) );
73
ui->textEdit->setStyleSheet( "border: 1px solid " + TomahawkUtils::Colors::BORDER_LINE.name() );
75
m_parent->installEventFilter( this );
77
connect( ui->buttonBox, SIGNAL( accepted() ), SLOT( accept() ) );
78
connect( ui->buttonBox, SIGNAL( rejected() ), SLOT( close() ) );
79
connect( ui->textEdit, SIGNAL( textChanged() ), SLOT( onChanged() ) );
80
connect( ui->facebookButton, SIGNAL( clicked( bool ) ), SLOT( onChanged() ) );
81
connect( ui->twitterButton, SIGNAL( clicked( bool ) ), SLOT( onChanged() ) );
82
connect( GlobalActionManager::instance(), SIGNAL( shortLinkReady( QUrl, QUrl, QVariant ) ), SLOT( onShortLinkReady( QUrl, QUrl, QVariant ) ) );
86
ui->twitterButton->setChecked( true );
87
ui->twitterButton->setVisible( false );
88
ui->facebookButton->setVisible( false );
92
SocialWidget::~SocialWidget()
99
SocialWidget::setPosition( QPoint position )
101
m_position = position;
107
SocialWidget::show( int timeoutSecs )
112
if( timeoutSecs > 0 )
113
m_timer.start( timeoutSecs * 1000 );
130
SocialWidget::shown() const
140
SocialWidget::paintEvent( QPaintEvent* event )
144
QPainterPath outline;
146
QRect r = contentsRect();
147
outline.addRoundedRect( r, TomahawkUtils::POPUP_ROUNDING_RADIUS, TomahawkUtils::POPUP_ROUNDING_RADIUS );
148
outline.moveTo( r.right() - ARROW_HEIGHT * 2, r.bottom()+1 );
149
outline.lineTo( r.right() - ARROW_HEIGHT * 3, r.bottom()+1 + ARROW_HEIGHT );
150
outline.lineTo( r.right() - ARROW_HEIGHT * 4, r.bottom()+1 );
152
TomahawkUtils::drawCompositedPopup( this,
154
TomahawkUtils::Colors::BORDER_LINE,
155
TomahawkUtils::Colors::POPUP_BACKGROUND,
156
TomahawkUtils::POPUP_OPACITY );
161
SocialWidget::onShortLinkReady( const QUrl& longUrl, const QUrl& shortUrl, const QVariant& callbackObj )
164
Q_UNUSED( callbackObj );
166
if ( m_query->album().isEmpty() )
167
ui->textEdit->setText( tr( "Listening to \"%1\" by %2. %3" ).arg( m_query->track() ).arg( m_query->artist() ).arg( shortUrl.toString() ) );
169
ui->textEdit->setText( tr( "Listening to \"%1\" by %2 on \"%3\". %4" ).arg( m_query->track() ).arg( m_query->artist() ).arg( m_query->album() ).arg( shortUrl.toString() ) );
174
SocialWidget::setQuery( const Tomahawk::query_ptr& query )
177
ui->coverImage->setPixmap( TomahawkUtils::addDropShadow( query->cover( ui->coverImage->size() ), ui->coverImage->size() ) );
178
onShortLinkReady( QString(), QString(), QVariant() );
181
QUrl longUrl = GlobalActionManager::instance()->openLinkFromQuery( query );
182
GlobalActionManager::instance()->shortenLink( longUrl );
187
SocialWidget::onChanged()
189
const int remaining = charsAvailable() - ui->textEdit->toPlainText().length();
190
ui->charsLeftLabel->setText( tr( "%1 characters left" ).arg( remaining ) );
191
ui->buttonBox->button( QDialogButtonBox::Ok )->setEnabled( remaining >= 0 && ( ui->facebookButton->isChecked() || ui->twitterButton->isChecked() ) );
196
SocialWidget::accept()
198
tDebug() << "Sharing social link!";
200
QVariantMap shareInfo;
201
Tomahawk::InfoSystem::InfoStringHash trackInfo;
203
trackInfo["title"] = m_query->track();
204
trackInfo["artist"] = m_query->artist();
205
trackInfo["album"] = m_query->album();
207
shareInfo["trackinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo );
208
shareInfo["message"] = ui->textEdit->toPlainText();
209
shareInfo["accountlist"] = QStringList( "all" );
211
Tomahawk::InfoSystem::InfoPushData pushData( uuid(), Tomahawk::InfoSystem::InfoShareTrack, shareInfo, Tomahawk::InfoSystem::PushNoFlag );
212
Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );
219
SocialWidget::close()
227
SocialWidget::charsAvailable() const
229
if ( ui->twitterButton->isChecked() )
232
return 420; // facebook max length
237
SocialWidget::onGeometryUpdate()
239
QPoint p( m_parent->rect().width() - m_parentRect.width(), m_parent->rect().height() - m_parentRect.height() );
241
m_parentRect = m_parent->rect();
243
QPoint position( m_position - QPoint( size().width(), size().height() )
244
+ QPoint( 2 + ARROW_HEIGHT * 3, 0 ) );
246
if ( position != pos() )
254
SocialWidget::eventFilter( QObject* object, QEvent* event )
256
if ( event->type() == QEvent::Resize )
261
return QObject::eventFilter( object, event );
266
SocialWidget::query() const
273
SocialWidget::position() const