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

« back to all changes in this revision

Viewing changes to src/libtomahawk/widgets/SourceTreePopupDialog.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 Leo Franchi <lfranchi@kde.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 "SourceTreePopupDialog.h"
 
21
 
 
22
#include <QApplication>
 
23
#include <QPaintEvent>
 
24
#include <QPainter>
 
25
#include <QDialogButtonBox>
 
26
#include <QVBoxLayout>
 
27
#include <QHBoxLayout>
 
28
#include <QLabel>
 
29
#include <QPushButton>
 
30
#include <QCheckBox>
 
31
#include <QTimer>
 
32
 
 
33
#ifdef QT_MAC_USE_COCOA
 
34
    #include "SourceTreePopupDialog_mac.h"
 
35
#endif
 
36
 
 
37
#include "utils/TomahawkUtilsGui.h"
 
38
#include "utils/ImageRegistry.h"
 
39
 
 
40
using namespace Tomahawk;
 
41
 
 
42
SourceTreePopupDialog::SourceTreePopupDialog()
 
43
    : QWidget( 0 )
 
44
    , m_layout( 0 )
 
45
    , m_result( false )
 
46
    , m_label( 0 )
 
47
    , m_buttons( 0 )
 
48
{
 
49
#ifndef ENABLE_HEADLESS
 
50
    setParent( QApplication::activeWindow() );
 
51
#endif
 
52
    setWindowFlags( Qt::Popup | Qt::FramelessWindowHint );
 
53
 
 
54
    setAutoFillBackground( false );
 
55
    setAttribute( Qt::WA_TranslucentBackground, true );
 
56
    setAttribute( Qt::WA_NoSystemBackground, true );
 
57
 
 
58
    //setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
 
59
 
 
60
    m_title = new QLabel( this );
 
61
    QFont titleFont = m_title->font();
 
62
    titleFont.setBold( true );
 
63
    m_title->setStyleSheet( "color: " + TomahawkUtils::Colors::GROUP_HEADER.name() );
 
64
    titleFont.setPointSize( TomahawkUtils::defaultFontSize() + 1 );
 
65
    m_title->setFont( titleFont );
 
66
    m_title->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
 
67
 
 
68
    m_label = new QLabel( this );
 
69
    m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
 
70
 
 
71
    m_buttons->button( QDialogButtonBox::Ok )->setIcon( ImageRegistry::instance()->icon( RESPATH "images/delete.svg" ) );
 
72
    m_buttons->button( QDialogButtonBox::Cancel )->setIcon( ImageRegistry::instance()->icon( RESPATH "images/cancel.svg" ) );
 
73
 
 
74
    connect( m_buttons, SIGNAL( accepted() ), this, SLOT( onAccepted() ) );
 
75
    connect( m_buttons, SIGNAL( rejected() ), this, SLOT( onRejected() ) );
 
76
 
 
77
    m_layout = new QVBoxLayout;
 
78
    TomahawkUtils::unmarginLayout( m_layout );
 
79
    setLayout( m_layout );
 
80
    m_layout->setSpacing( 8 );
 
81
    m_layout->setMargin( 6 );
 
82
 
 
83
    m_layout->addWidget( m_title );
 
84
 
 
85
    m_separatorLine = new QWidget( this );
 
86
    m_separatorLine->setFixedHeight( 1 );
 
87
    m_separatorLine->setContentsMargins( 0, 0, 0, 0 );
 
88
    m_separatorLine->setStyleSheet( "QWidget { border-top: 1px solid " +
 
89
                                    TomahawkUtils::Colors::BORDER_LINE.name() + "; }" );
 
90
    m_layout->addWidget( m_separatorLine );
 
91
    m_layout->addWidget( m_label );
 
92
    m_layout->addWidget( m_buttons );
 
93
    setContentsMargins( contentsMargins().left() + 12,
 
94
                        contentsMargins().top() + 8,
 
95
                        contentsMargins().right() + 8,
 
96
                        contentsMargins().bottom() + 8 );
 
97
 
 
98
    m_title->setVisible( false );
 
99
    m_separatorLine->setVisible( false );
 
100
 
 
101
/*
 
102
    m_buttons->button( QDialogButtonBox::Ok )->setStyleSheet(
 
103
                       "QPushButton { \
 
104
                            background-color: #F15C5E; \
 
105
                            border-style: solid; \
 
106
                            border-width: 1px; \
 
107
                            border-radius: 10px; \
 
108
                            border-color: #B64547; \
 
109
                            padding: 2px; \
 
110
                        } \
 
111
                        QPushButton:pressed { \
 
112
                            border-style: solid; \
 
113
                            border-width: 1px; \
 
114
                            border-radius: 10px; \
 
115
                            border-color: #B64547; \
 
116
                            background-color: #D35052; \
 
117
                            border-style: flat; \
 
118
                        }" );*/
 
119
    setFixedHeight( 80 );
 
120
}
 
121
 
 
122
void
 
123
SourceTreePopupDialog::setTitle( const QString& text )
 
124
{
 
125
    m_title->setText( text.toUpper() );
 
126
    if ( m_title->text().isEmpty() )
 
127
    {
 
128
        m_title->setVisible( false );
 
129
        m_separatorLine->setVisible( false );
 
130
    }
 
131
    else
 
132
    {
 
133
        m_title->setVisible( true );
 
134
        m_separatorLine->setVisible( true );
 
135
    }
 
136
}
 
137
 
 
138
 
 
139
void
 
140
SourceTreePopupDialog::setMainText( const QString& text )
 
141
{
 
142
    m_label->setText( text );
 
143
    QFontMetrics fm = m_label->fontMetrics();
 
144
    setFixedWidth( fm.width( text ) + 20 );
 
145
}
 
146
 
 
147
 
 
148
void
 
149
SourceTreePopupDialog::setOkButtonText( const QString& text )
 
150
{
 
151
    if ( m_buttons && m_buttons->button( QDialogButtonBox::Ok ) )
 
152
        m_buttons->button( QDialogButtonBox::Ok )->setText( text );
 
153
}
 
154
 
 
155
 
 
156
void
 
157
SourceTreePopupDialog::setExtraQuestions( const Tomahawk::PlaylistDeleteQuestions& questions )
 
158
{
 
159
    m_questions = questions;
 
160
 
 
161
    int baseHeight = 80;
 
162
    int idx = m_layout->indexOf( m_label ) + 1;
 
163
    foreach ( const Tomahawk::PlaylistDeleteQuestion& question, m_questions )
 
164
    {
 
165
        QCheckBox* cb = new QCheckBox( question.first, this );
 
166
        cb->setLayoutDirection( Qt::RightToLeft );
 
167
        cb->setProperty( "data", question.second );
 
168
 
 
169
        QHBoxLayout* h = new QHBoxLayout;
 
170
        h->addStretch( 1 );
 
171
        h->addWidget( cb );
 
172
//         m_layout->insertLayout( h, cb, 0 );
 
173
        m_layout->insertLayout( idx, h, 0 );
 
174
 
 
175
        m_questionCheckboxes << cb;
 
176
        idx++;
 
177
        baseHeight += cb->height() + m_layout->spacing();
 
178
    }
 
179
    setFixedHeight( baseHeight );
 
180
}
 
181
 
 
182
 
 
183
void
 
184
SourceTreePopupDialog::paintEvent( QPaintEvent* event )
 
185
{
 
186
    Q_UNUSED( event );
 
187
 
 
188
    // Constants for painting
 
189
    const int leftTriangleWidth = 12;
 
190
    const int cornerRounding = TomahawkUtils::POPUP_ROUNDING_RADIUS;
 
191
    const int leftEdgeOffset = 2 /*margin*/ + leftTriangleWidth / 2;
 
192
    const QRect brect = rect().adjusted( 2, 3, -2, -3 );
 
193
 
 
194
    QPainterPath outline;
 
195
 
 
196
    // Main rect
 
197
    outline.addRoundedRect( brect.adjusted( leftTriangleWidth / 2, 0, 0, 0 ), cornerRounding, cornerRounding );
 
198
 
 
199
    // Left triangle top branch
 
200
    outline.moveTo( brect.left(), brect.top() + brect.height() / 2 );
 
201
    outline.lineTo( leftEdgeOffset, brect.top() + brect.height() / 2 - leftTriangleWidth / 2 );
 
202
 
 
203
    // Left triangle bottom branch
 
204
    outline.lineTo( leftEdgeOffset, brect.top() + brect.height() / 2 + leftTriangleWidth / 2 );
 
205
    outline.lineTo( brect.left(), brect.top() + brect.height() / 2 );
 
206
 
 
207
#ifndef Q_OS_MAC
 
208
    TomahawkUtils::drawCompositedPopup( this,
 
209
                                        outline,
 
210
                                        TomahawkUtils::Colors::BORDER_LINE,
 
211
                                        TomahawkUtils::Colors::POPUP_BACKGROUND,
 
212
                                        TomahawkUtils::POPUP_OPACITY );
 
213
#else
 
214
    TomahawkUtils::drawCompositedPopup( this,
 
215
                                        outline,
 
216
                                        TomahawkUtils::Colors::BORDER_LINE,
 
217
                                        QColor( "#D6E3F1" ),
 
218
                                        0.93 );
 
219
#endif
 
220
}
 
221
 
 
222
 
 
223
void
 
224
SourceTreePopupDialog::focusOutEvent( QFocusEvent* )
 
225
{
 
226
    hide();
 
227
}
 
228
 
 
229
 
 
230
void
 
231
SourceTreePopupDialog::showEvent( QShowEvent* )
 
232
{
 
233
    m_result = false;
 
234
}
 
235
 
 
236
 
 
237
void
 
238
SourceTreePopupDialog::onAccepted()
 
239
{
 
240
    hide();
 
241
    m_result = true;
 
242
    calculateResults();
 
243
    emit result( m_result );
 
244
}
 
245
 
 
246
 
 
247
void
 
248
SourceTreePopupDialog::onRejected()
 
249
{
 
250
    hide();
 
251
    m_result = false;
 
252
    calculateResults();
 
253
    emit result( m_result );
 
254
}
 
255
 
 
256
 
 
257
void
 
258
SourceTreePopupDialog::calculateResults()
 
259
{
 
260
    foreach ( const QCheckBox* b, m_questionCheckboxes )
 
261
    {
 
262
        if ( b->property( "data" ).toInt() != 0 )
 
263
        {
 
264
            m_questionResults[ b->property( "data" ).toInt() ] = ( b->checkState() == Qt::Checked );
 
265
        }
 
266
    }
 
267
}