~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/SocialWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2012, Teo Mrnjavac <teo@kde.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "SocialWidget.h"
 
21
#include "ui_SocialWidget.h"
 
22
 
 
23
#include "GlobalActionManager.h"
 
24
#include "Source.h"
 
25
 
 
26
#include "utils/ImageRegistry.h"
 
27
#include "utils/TomahawkUtilsGui.h"
 
28
#include "utils/Logger.h"
 
29
 
 
30
#include <QPainter>
 
31
#include <QDialog>
 
32
#include <QPropertyAnimation>
 
33
 
 
34
#define ARROW_HEIGHT 6
 
35
 
 
36
 
 
37
SocialWidget::SocialWidget( QWidget* parent )
 
38
    : QWidget( parent ) // this is on purpose!
 
39
    , ui( new Ui::SocialWidget )
 
40
    , m_parent( parent )
 
41
    , m_parentRect( parent->rect() )
 
42
{
 
43
    ui->setupUi( this );
 
44
    setWindowFlags( Qt::Popup | Qt::FramelessWindowHint );
 
45
 
 
46
    setAutoFillBackground( false );
 
47
    setAttribute( Qt::WA_TranslucentBackground, true );
 
48
    setAttribute( Qt::WA_NoSystemBackground, true );
 
49
 
 
50
    TomahawkUtils::unmarginLayout( layout() );
 
51
 
 
52
#ifndef Q_OS_MAC
 
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 );
 
56
#else
 
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 );
 
61
#endif
 
62
 
 
63
 
 
64
    m_timer.setSingleShot( true );
 
65
    connect( &m_timer, SIGNAL( timeout() ), this, SLOT( hide() ) );
 
66
 
 
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" ) );
 
72
 
 
73
    ui->textEdit->setStyleSheet( "border: 1px solid " + TomahawkUtils::Colors::BORDER_LINE.name() );
 
74
 
 
75
    m_parent->installEventFilter( this );
 
76
 
 
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 ) ) );
 
83
 
 
84
    onChanged();
 
85
 
 
86
    ui->twitterButton->setChecked( true );
 
87
    ui->twitterButton->setVisible( false );
 
88
    ui->facebookButton->setVisible( false );
 
89
}
 
90
 
 
91
 
 
92
SocialWidget::~SocialWidget()
 
93
{
 
94
    delete ui;
 
95
}
 
96
 
 
97
 
 
98
void
 
99
SocialWidget::setPosition( QPoint position )
 
100
{
 
101
    m_position = position;
 
102
    onGeometryUpdate();
 
103
}
 
104
 
 
105
 
 
106
void
 
107
SocialWidget::show( int timeoutSecs )
 
108
{
 
109
    if ( !isEnabled() )
 
110
        return;
 
111
 
 
112
    if( timeoutSecs > 0 )
 
113
        m_timer.start( timeoutSecs * 1000 );
 
114
 
 
115
    QWidget::show();
 
116
}
 
117
 
 
118
 
 
119
void
 
120
SocialWidget::hide()
 
121
{
 
122
    if ( !isEnabled() )
 
123
        return;
 
124
 
 
125
    QWidget::hide();
 
126
}
 
127
 
 
128
 
 
129
bool
 
130
SocialWidget::shown() const
 
131
{
 
132
    if ( !isEnabled() )
 
133
        return false;
 
134
 
 
135
    return isVisible();
 
136
}
 
137
 
 
138
 
 
139
void
 
140
SocialWidget::paintEvent( QPaintEvent* event )
 
141
{
 
142
    Q_UNUSED( event );
 
143
 
 
144
    QPainterPath outline;
 
145
 
 
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 );
 
151
 
 
152
    TomahawkUtils::drawCompositedPopup( this,
 
153
                                        outline,
 
154
                                        TomahawkUtils::Colors::BORDER_LINE,
 
155
                                        TomahawkUtils::Colors::POPUP_BACKGROUND,
 
156
                                        TomahawkUtils::POPUP_OPACITY );
 
157
}
 
158
 
 
159
 
 
160
void
 
161
SocialWidget::onShortLinkReady( const QUrl& longUrl, const QUrl& shortUrl, const QVariant& callbackObj )
 
162
{
 
163
    Q_UNUSED( longUrl );
 
164
    Q_UNUSED( callbackObj );
 
165
 
 
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() ) );
 
168
    else
 
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() ) );
 
170
}
 
171
 
 
172
 
 
173
void
 
174
SocialWidget::setQuery( const Tomahawk::query_ptr& query )
 
175
{
 
176
    m_query = query;
 
177
    ui->coverImage->setPixmap( TomahawkUtils::addDropShadow( query->cover( ui->coverImage->size() ), ui->coverImage->size() ) );
 
178
    onShortLinkReady( QString(), QString(), QVariant() );
 
179
    onChanged();
 
180
 
 
181
    QUrl longUrl = GlobalActionManager::instance()->openLinkFromQuery( query );
 
182
    GlobalActionManager::instance()->shortenLink( longUrl );
 
183
}
 
184
 
 
185
 
 
186
void
 
187
SocialWidget::onChanged()
 
188
{
 
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() ) );
 
192
}
 
193
 
 
194
 
 
195
void
 
196
SocialWidget::accept()
 
197
{
 
198
    tDebug() << "Sharing social link!";
 
199
 
 
200
    QVariantMap shareInfo;
 
201
    Tomahawk::InfoSystem::InfoStringHash trackInfo;
 
202
 
 
203
    trackInfo["title"] = m_query->track();
 
204
    trackInfo["artist"] = m_query->artist();
 
205
    trackInfo["album"] = m_query->album();
 
206
 
 
207
    shareInfo["trackinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo );
 
208
    shareInfo["message"] = ui->textEdit->toPlainText();
 
209
    shareInfo["accountlist"] = QStringList( "all" );
 
210
 
 
211
    Tomahawk::InfoSystem::InfoPushData pushData( uuid(), Tomahawk::InfoSystem::InfoShareTrack, shareInfo, Tomahawk::InfoSystem::PushNoFlag );
 
212
    Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );
 
213
 
 
214
    deleteLater();
 
215
}
 
216
 
 
217
 
 
218
void
 
219
SocialWidget::close()
 
220
{
 
221
    QWidget::hide();
 
222
    deleteLater();
 
223
}
 
224
 
 
225
 
 
226
unsigned int
 
227
SocialWidget::charsAvailable() const
 
228
{
 
229
    if ( ui->twitterButton->isChecked() )
 
230
        return 140;
 
231
 
 
232
    return 420; // facebook max length
 
233
}
 
234
 
 
235
 
 
236
void
 
237
SocialWidget::onGeometryUpdate()
 
238
{
 
239
    QPoint p( m_parent->rect().width() - m_parentRect.width(), m_parent->rect().height() - m_parentRect.height() );
 
240
    m_position += p;
 
241
    m_parentRect = m_parent->rect();
 
242
 
 
243
    QPoint position( m_position - QPoint( size().width(), size().height() )
 
244
                     + QPoint( 2 + ARROW_HEIGHT * 3, 0 ) );
 
245
 
 
246
    if ( position != pos() )
 
247
    {
 
248
        move( position );
 
249
    }
 
250
}
 
251
 
 
252
 
 
253
bool
 
254
SocialWidget::eventFilter( QObject* object, QEvent* event )
 
255
{
 
256
    if ( event->type() == QEvent::Resize )
 
257
    {
 
258
        onGeometryUpdate();
 
259
    }
 
260
 
 
261
    return QObject::eventFilter( object, event );
 
262
}
 
263
 
 
264
 
 
265
Tomahawk::query_ptr
 
266
SocialWidget::query() const
 
267
{
 
268
    return m_query;
 
269
}
 
270
 
 
271
 
 
272
QPoint
 
273
SocialWidget::position() const
 
274
{
 
275
    return m_position;
 
276
}