comparison src/pngplayer.cpp @ 3:248d5d1cdb38

Add functionalty to control buttons and make picture resizable
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Mar 2015 19:10:01 +0100
parents 97d2c8869c39
children 107e435cb569
comparison
equal deleted inserted replaced
2:97d2c8869c39 3:248d5d1cdb38
3 * This file is Free Software under the GNU GPL (v>=2) 3 * This file is Free Software under the GNU GPL (v>=2)
4 * and comes with ABSOLUTELY NO WARRANTY! 4 * and comes with ABSOLUTELY NO WARRANTY!
5 * See LICENSE.txt for details. 5 * See LICENSE.txt for details.
6 */ 6 */
7 #include "pngplayer.h" 7 #include "pngplayer.h"
8 #include "constants.h"
9 #include "imagelabel.h"
8 #include <QVBoxLayout> 10 #include <QVBoxLayout>
9 #include <QPixmap> 11 #include <QPixmap>
10 #include <QLabel> 12 #include <QLabel>
13 #include <QPushButton>
14 #include <QHBoxLayout>
15 #include <QSlider>
16
17 #include <QDebug>
11 18
12 PNGPlayer::PNGPlayer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { 19 PNGPlayer::PNGPlayer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
13 setupGUI(); 20 setupGUI();
14 return;
15 } 21 }
16 22
17 void PNGPlayer::setupGUI() { 23 void PNGPlayer::setupGUI() {
18 QVBoxLayout *baseLayout = new QVBoxLayout; 24 QVBoxLayout *baseLayout = new QVBoxLayout;
19 mPNGLabel = new QLabel; 25 mPNGLabel = new ImageLabel;
26 mPNGLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
27
28 QHBoxLayout *controlArea = new QHBoxLayout;
29 QHBoxLayout *controlBtns = new QHBoxLayout;
30 controlArea->addLayout(controlBtns);
31
32 QPushButton *firstBtn = new QPushButton("First");
33 controlBtns->addWidget(firstBtn);
34 connect(firstBtn, &QPushButton::clicked, this, &PNGPlayer::firstClicked);
35
36 QPushButton *prevBtn = new QPushButton("Prev");
37 controlBtns->addWidget(prevBtn);
38 connect(prevBtn, &QPushButton::clicked, this, &PNGPlayer::back);
39
40 mPlayBtn = new QPushButton("Play");
41 mPlayBtn->setCheckable(true);
42 controlBtns->addWidget(mPlayBtn);
43 connect(mPlayBtn, &QPushButton::clicked, this, &PNGPlayer::togglePlay);
44
45 QPushButton *nextBtn = new QPushButton("Next");
46 connect(nextBtn, &QPushButton::clicked, this, &PNGPlayer::advance);
47 controlBtns->addWidget(nextBtn);
48
49 QPushButton *lastBtn = new QPushButton("Last");
50 controlBtns->addWidget(lastBtn);
51 connect(lastBtn, &QPushButton::clicked, this, &PNGPlayer::lastClicked);
52
53 mSlider = new QSlider(Qt::Horizontal);
54 mSlider->setTickPosition(QSlider::TicksBelow);
55 connect(mSlider, &QSlider::valueChanged, this, &PNGPlayer::sliderChanged);
56 connect(mSlider, &QSlider::sliderPressed, this, &PNGPlayer::sliderPressed);
57 connect(mSlider, &QSlider::sliderReleased, this, &PNGPlayer::sliderReleased);
58
59 QVBoxLayout *speedInfoArea = new QVBoxLayout;
60 QHBoxLayout *speedArea = new QHBoxLayout;
61 speedInfoArea->addLayout(speedArea);
62
63 QLabel *speedLabel = new QLabel(tr("Speed:"));
64 speedArea->addWidget(speedLabel);
65
66 mSpeedSlider = new QSlider(Qt::Horizontal);
67 mSpeedSlider->setMaximum(10000);
68 mSpeedSlider->setTickInterval(500);
69 mSpeedSlider->setTickPosition(QSlider::TicksBelow);
70 speedArea->addWidget(mSpeedSlider);
71
72 mCurSpeedLabel = new QLabel;
73 speedArea->addWidget(mCurSpeedLabel);
74 speedArea->addStretch(-1);
75
76 mPositionLabel = new QLabel;
77 speedInfoArea->addWidget(mPositionLabel);
78
79 controlArea->addLayout(speedInfoArea);
80
20 baseLayout->addWidget(mPNGLabel); 81 baseLayout->addWidget(mPNGLabel);
82 baseLayout->addLayout(controlArea);
83 baseLayout->addWidget(mSlider);
84
21 connect(&mAdvanceTimer, SIGNAL(timeout()), this, SIGNAL(advance())); 85 connect(&mAdvanceTimer, SIGNAL(timeout()), this, SIGNAL(advance()));
22 setLayout(baseLayout); 86 setLayout(baseLayout);
23 mAdvanceTimer.setInterval(2000);
24 mAdvanceTimer.start();
25 } 87 }
26 88
27 void PNGPlayer::showPicture(const QString& fileName, const QString& info) { 89 void PNGPlayer::showPicture(const QString& fileName, int current, int max,
90 const QDateTime& timestamp) {
28 QPixmap pic(mBaseDir.filePath(fileName)); 91 QPixmap pic(mBaseDir.filePath(fileName));
29 /* If this is too slow we could use a pixmap cache here and do 92 /* If this is too slow we could use a pixmap cache here and do
30 * some intelligent preloading */ 93 * some intelligent preloading */
31 if (pic.isNull()) { 94 if (pic.isNull()) {
32 emit error(tr("Failed to load picture: '%1'").arg(fileName)); 95 emit error(tr("Failed to load picture: '%1'").arg(fileName));
33 return; 96 return;
34 } 97 }
35 mPNGLabel->setPixmap(pic); 98 mPNGLabel->setPixmap(pic);
99 updatePositions(current, max, timestamp);
36 } 100 }
101
102 void PNGPlayer::updatePositions(int current, int max, const QDateTime& timestamp) {
103 mMax = max;
104 mSlider->blockSignals(true); /* We only want user generated changes */
105 mSlider->setValue(current);
106 mSlider->blockSignals(false);
107 mSlider->setMaximum(max);
108 mPositionLabel->setText("<b>" + tr("Screenshot Nr.:") + " </b>" +
109 QString("%1 (%2)").arg(current).arg(max) + " / <b>" +
110 tr("Timestamp:") + " </b>" + timestamp.toString(LONG_DATE_FORMAT));
111
112 if (mMax == current && mAdvanceTimer.isActive()) {
113 togglePlay();
114 }
115 }
116
117 void PNGPlayer::setSpeed(int mSecsPerPicture) {
118 if (mSecsPerPicture == 1000) {
119 mCurSpeedLabel->setText(tr("%1 second per Picture").
120 arg(mSecsPerPicture / 1000));
121 } else {
122 mCurSpeedLabel->setText(tr("%1 seconds per Picture").
123 arg(mSecsPerPicture / 1000));
124 }
125 mSpeedSlider->setValue(mSecsPerPicture);
126 mAdvanceTimer.setInterval(mSecsPerPicture);
127 }
128
129 void PNGPlayer::togglePlay() {
130 if (mAdvanceTimer.isActive()) {
131 mAdvanceTimer.stop();
132 mPlayBtn->setText("Play");
133 mPlayBtn->setChecked(false);
134 } else {
135 mAdvanceTimer.start();
136 mPlayBtn->setText("Pause");
137 mPlayBtn->setChecked(true);
138 emit advance();
139 }
140 }
141
142 void PNGPlayer::sliderChanged() {
143 qDebug() << "Slider moved. ";
144 emit jumpToFrame(mSlider->value());
145 }
146
147 void PNGPlayer::sliderPressed() {
148 qDebug() << "Slider pressed. Deactivating timer. ";
149 mAdvanceTimer.stop();
150 }
151
152 void PNGPlayer::sliderReleased() {
153 if (mPlayBtn->isChecked()) {
154 mAdvanceTimer.start();
155 }
156 }
157
158 void PNGPlayer::firstClicked() {
159 emit jumpToFrame(0);
160 }
161
162 void PNGPlayer::lastClicked() {
163 emit jumpToFrame(mMax);
164 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)