~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/messagebubble.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
34
34
 
35
35
// Local
36
36
 
37
 
namespace Gwenview {
 
37
namespace Gwenview
 
38
{
38
39
 
39
40
static const int TIMEOUT = 10000;
40
41
 
41
 
class PieWidget : public QWidget {
 
42
class PieWidget : public QWidget
 
43
{
42
44
public:
43
 
        PieWidget(QWidget* parent=0)
44
 
        : QWidget(parent)
45
 
        , mValue(0)
46
 
        {
47
 
                setMinimumSize(16, 16);
48
 
        }
49
 
 
50
 
        void setValue(qreal value) {
51
 
                mValue = value;
52
 
                update();
53
 
        }
54
 
 
55
 
        int heightForWidth(int width) const {
56
 
                return width;
57
 
        }
 
45
    PieWidget(QWidget* parent = 0)
 
46
        : QWidget(parent)
 
47
        , mValue(0) {
 
48
        setMinimumSize(16, 16);
 
49
    }
 
50
 
 
51
    void setValue(qreal value)
 
52
    {
 
53
        mValue = value;
 
54
        update();
 
55
    }
 
56
 
 
57
    int heightForWidth(int width) const {
 
58
        return width;
 
59
    }
58
60
 
59
61
protected:
60
 
        void paintEvent(QPaintEvent*) {
61
 
                QPainter painter(this);
62
 
                painter.setRenderHint(QPainter::Antialiasing);
63
 
                const int circle = 5760;
64
 
                const int start = circle / 4; // Start at 12h, not 3h
65
 
                const int end = int(circle * mValue);
66
 
                painter.setBrush(palette().dark());
67
 
                painter.setPen(palette().light().color());
 
62
    void paintEvent(QPaintEvent*)
 
63
    {
 
64
        QPainter painter(this);
 
65
        painter.setRenderHint(QPainter::Antialiasing);
 
66
        const int circle = 5760;
 
67
        const int start = circle / 4; // Start at 12h, not 3h
 
68
        const int end = int(circle * mValue);
 
69
        painter.setBrush(palette().dark());
 
70
        painter.setPen(palette().light().color());
68
71
 
69
 
                QRectF square = QRectF(rect()).adjusted(.5, .5, -.5, -.5);
70
 
                qreal width = square.width();
71
 
                qreal height= square.height();
72
 
                if (width < height) {
73
 
                        square.setHeight(width);
74
 
                        square.moveTop((height - width) / 2);
75
 
                } else {
76
 
                        square.setWidth(height);
77
 
                        square.moveLeft((width - height) / 2);
78
 
                }
79
 
                painter.drawPie(square, start, end);
80
 
        }
 
72
        QRectF square = QRectF(rect()).adjusted(.5, .5, -.5, -.5);
 
73
        qreal width = square.width();
 
74
        qreal height = square.height();
 
75
        if (width < height) {
 
76
            square.setHeight(width);
 
77
            square.moveTop((height - width) / 2);
 
78
        } else {
 
79
            square.setWidth(height);
 
80
            square.moveLeft((width - height) / 2);
 
81
        }
 
82
        painter.drawPie(square, start, end);
 
83
    }
81
84
 
82
85
private:
83
 
        qreal mValue;
 
86
    qreal mValue;
84
87
};
85
88
 
86
 
 
87
89
struct MessageBubblePrivate {
88
 
        PieWidget* mCountDownWidget;
89
 
        QWidget* mWidget;
90
 
        QLabel* mLabel;
 
90
    PieWidget* mCountDownWidget;
 
91
    QWidget* mWidget;
 
92
    QLabel* mLabel;
91
93
};
92
94
 
93
 
 
94
95
MessageBubble::MessageBubble(QWidget* parent)
95
96
: HudWidget(parent)
96
 
, d(new MessageBubblePrivate) {
97
 
        d->mWidget = new QWidget;
98
 
        d->mCountDownWidget = new PieWidget;
99
 
        d->mCountDownWidget->setValue(1);
100
 
        d->mLabel = new QLabel;
101
 
 
102
 
        QTimeLine* timeLine = new QTimeLine(TIMEOUT, this);
103
 
        connect(timeLine, SIGNAL(valueChanged(qreal)),
104
 
                SLOT(slotTimeLineChanged(qreal)));
105
 
        connect(timeLine, SIGNAL(finished()),
106
 
                SLOT(deleteLater()));
107
 
        timeLine->start();
108
 
 
109
 
        QHBoxLayout* layout = new QHBoxLayout(d->mWidget);
110
 
        layout->setMargin(0);
111
 
        layout->addWidget(d->mCountDownWidget);
112
 
        layout->addWidget(d->mLabel);
113
 
 
114
 
        init(d->mWidget, HudWidget::OptionCloseButton);
115
 
}
116
 
 
117
 
 
118
 
MessageBubble::~MessageBubble() {
119
 
        delete d;
120
 
}
121
 
 
122
 
 
123
 
void MessageBubble::setText(const QString& text) {
124
 
        d->mLabel->setText(text);
125
 
        adjustSize();
126
 
}
127
 
 
128
 
 
129
 
QToolButton* MessageBubble::addButton(const KGuiItem& guiItem) {
130
 
        QToolButton* button = new QToolButton;
131
 
        button->setText(guiItem.text());
132
 
        button->setIcon(guiItem.icon());
133
 
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
134
 
        d->mWidget->layout()->addWidget(button);
135
 
        adjustSize();
136
 
        return button;
137
 
}
138
 
 
139
 
 
140
 
void MessageBubble::slotTimeLineChanged(qreal value) {
141
 
        d->mCountDownWidget->setValue(1 - value);
142
 
}
143
 
 
 
97
, d(new MessageBubblePrivate)
 
98
{
 
99
    d->mWidget = new QWidget;
 
100
    d->mCountDownWidget = new PieWidget;
 
101
    d->mCountDownWidget->setValue(1);
 
102
    d->mLabel = new QLabel;
 
103
 
 
104
    QTimeLine* timeLine = new QTimeLine(TIMEOUT, this);
 
105
    connect(timeLine, SIGNAL(valueChanged(qreal)),
 
106
            SLOT(slotTimeLineChanged(qreal)));
 
107
    connect(timeLine, SIGNAL(finished()),
 
108
            SLOT(deleteLater()));
 
109
    timeLine->start();
 
110
 
 
111
    QHBoxLayout* layout = new QHBoxLayout(d->mWidget);
 
112
    layout->setMargin(0);
 
113
    layout->addWidget(d->mCountDownWidget);
 
114
    layout->addWidget(d->mLabel);
 
115
 
 
116
    init(d->mWidget, HudWidget::OptionCloseButton);
 
117
}
 
118
 
 
119
MessageBubble::~MessageBubble()
 
120
{
 
121
    delete d;
 
122
}
 
123
 
 
124
void MessageBubble::setText(const QString& text)
 
125
{
 
126
    d->mLabel->setText(text);
 
127
    adjustSize();
 
128
}
 
129
 
 
130
QToolButton* MessageBubble::addButton(const KGuiItem& guiItem)
 
131
{
 
132
    QToolButton* button = new QToolButton;
 
133
    button->setText(guiItem.text());
 
134
    button->setIcon(guiItem.icon());
 
135
    button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
136
    d->mWidget->layout()->addWidget(button);
 
137
    adjustSize();
 
138
    return button;
 
139
}
 
140
 
 
141
void MessageBubble::slotTimeLineChanged(qreal value)
 
142
{
 
143
    d->mCountDownWidget->setValue(1 - value);
 
144
}
144
145
 
145
146
} // namespace