summaryrefslogtreecommitdiffstats
path: root/vrplayer/mainwindow.cpp
blob: 7f41eee9919307544c370c99f6e7a2f4e5dc656e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "mainwindow.h"
#include "ui_mainwindow.h"

/*
 * TODO
 *      o should we use tick marks in QSlider?
 */

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    acceptSliderMove = false;
    decoderThread = new DecoderThread();
    setupUI();

/* LK_TODO */
#if 0
    decoder = new Decoder(this);
    connect(this, SIGNAL(onGeometryChanged(int, int, int, int)),
            decoder, SLOT(onGeometryChanged(int, int, int, int)));
#endif

    /* register for signals/slots with decoderThread */
    connect(this, SIGNAL(on_geometryChanged(int,int,int,int)),
            decoderThread, SLOT(on_geometryChanged(int,int,int,int)));

    connect(decoderThread, SIGNAL(on_elapsedtime(int)),
            this, SLOT(on_elapsedTime(int)));

    connect(decoderThread, SIGNAL(on_decoderErrorMsg(QString, QString)),
            this, SLOT(on_decoderError(QString, QString)));

    connect(decoderThread, SIGNAL(on_mediaDurationInSeconds(int)),
            this, SLOT(on_mediaDurationInSeconds(int)));

    connect(this, SIGNAL(on_mediaSeek(int)), decoderThread, SLOT(on_mediaSeek(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int rv;

    rv = QMessageBox::question(this, "Closing application",
                               "Do you really want to close vrplayer?",
                               QMessageBox::Yes | QMessageBox::No);

    if (rv == QMessageBox::No)
    {
        event->ignore();
        return;
    }
    decoderThread->exit(0);
    event->accept();
}

void MainWindow::resizeEvent(QResizeEvent *e)
{
    QRect rect;

    getVdoGeometry(&rect);
    emit on_geometryChanged(rect.x(), rect.y(), rect.width(), rect.height());
}

void MainWindow::moveEvent(QMoveEvent *e)
{
    QRect rect;

    getVdoGeometry(&rect);
    emit on_geometryChanged(rect.x(), rect.y(), rect.width(), rect.height());
}

void MainWindow::setupUI()
{
    /* setup area to display video */
    lblVideo = new QLabel();
    lblVideo->setMinimumWidth(320);
    lblVideo->setMinimumHeight(200);
    QPalette palette = lblVideo->palette();
    palette.setColor(lblVideo->backgroundRole(), Qt::black);
    palette.setColor(lblVideo->foregroundRole(), Qt::black);
    lblVideo->setAutoFillBackground(true);
    lblVideo->setPalette(palette);
    hboxLayoutTop = new QHBoxLayout;
    hboxLayoutTop->addWidget(lblVideo);

    /* setup label to display current pos in media */
    lblCurrentPos = new QLabel("00:00:00");
    lblCurrentPos->setMinimumHeight(20);
    lblCurrentPos->setMaximumHeight(20);

    /* setup slider to seek into media */
    slider = new QSlider();
    slider->setOrientation(Qt::Horizontal);
    slider->setMinimumHeight(20);
    slider->setMaximumHeight(20);
    connect(slider, SIGNAL(actionTriggered(int)), this, SLOT(on_sliderActionTriggered(int)));
    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_sliderValueChanged(int)));

    /* setup label to display media duration */
    lblDuration = new QLabel("00:00:00");
    lblDuration->setMinimumHeight(20);
    lblDuration->setMaximumHeight(20);

    /* add above three widgets to mid layout */
    hboxLayoutMiddle = new QHBoxLayout;
    hboxLayoutMiddle->addWidget(lblCurrentPos);
    hboxLayoutMiddle->addWidget(slider);
    hboxLayoutMiddle->addWidget(lblDuration);

    /* setup play button */
    btnPlay = new QPushButton("P");
    btnPlay->setMinimumHeight(40);
    btnPlay->setMaximumHeight(40);
    btnPlay->setMinimumWidth(40);
    btnPlay->setMaximumWidth(40);
    connect(btnPlay, SIGNAL(clicked(bool)), this, SLOT(on_btnPlayClicked(bool)));

    /* setup stop button */
    btnStop = new QPushButton("S");
    btnStop->setMinimumHeight(40);
    btnStop->setMaximumHeight(40);
    btnStop->setMinimumWidth(40);
    btnStop->setMaximumWidth(40);

    /* setup rewind button */
    btnRewind = new QPushButton("R");
    btnRewind->setMinimumHeight(40);
    btnRewind->setMaximumHeight(40);
    btnRewind->setMinimumWidth(40);
    btnRewind->setMaximumWidth(40);

    /* add buttons to bottom panel */
    hboxLayoutBottom = new QHBoxLayout;
    hboxLayoutBottom->addWidget(btnPlay);
    hboxLayoutBottom->addWidget(btnStop);
    hboxLayoutBottom->addWidget(btnRewind);
    hboxLayoutBottom->addStretch();

    /* add all three layouts to one vertical layout */
    vboxLayout = new QVBoxLayout;
    vboxLayout->addLayout(hboxLayoutTop);
    vboxLayout->addLayout(hboxLayoutMiddle);
    vboxLayout->addLayout(hboxLayoutBottom);

    /* add all of them to central widget */
    window = new QWidget;
    window->setLayout(vboxLayout);
    this->setCentralWidget(window);
}

void MainWindow::openMediaFile()
{
    /* TODO take last stored value from QSettings */

    if (filename.length() == 0)
    {
        /* no previous selection - open user's home folder TODO */
        // TODO filename = QFileDialog::getOpenFileName(this, "Select Media File", "/");
        filename = QFileDialog::getOpenFileName(this, "Select Media File", "/home/lk/vbox_share");
    }
    else
    {
        /* show last selected file */
        filename = QFileDialog::getOpenFileName(this, "Select Media File",
                                                filename);
    }
    decoderThread->setFilename(filename);
}

void MainWindow::getVdoGeometry(QRect *rect)
{
    int x = geometry().x() + lblVideo->geometry().x();

    int y = pos().y() + lblVideo->geometry().y() +
            ui->mainToolBar->geometry().height() * 4 + 10;

    rect->setX(x);
    rect->setY(y);
    rect->setWidth(lblVideo->geometry().width());
    rect->setHeight(lblVideo->geometry().height());
}

/*******************************************************************************
 *                       actions and slots go here                             *
 ******************************************************************************/

void MainWindow::on_actionOpen_Media_File_triggered()
{
    openMediaFile();
}

void MainWindow::on_actionExit_triggered()
{
    /* TODO: confirm app exit */
    this->close();
}

void MainWindow::on_actionPlay_Media_triggered()
{
    // LK_TODO do we need this? if yes, should be same as on_btnPlayClicked()
#if 1
    decoderThread->start();
#else
    if (!decoder)
        return;

    decoder->init(filename);
#endif
}

void MainWindow::on_decoderError(QString title, QString msg)
{
    QMessageBox::information(this, title, msg);
}

void MainWindow::on_btnPlayClicked(bool flag)
{
    if (filename.length() == 0)
        openMediaFile();

    decoderThread->start();
}

void MainWindow::on_mediaDurationInSeconds(int duration)
{
    int  hours   = 0;
    int  minutes = 0;
    int  secs    = 0;
    char buf[20];

    /* setup progress bar */
    slider->setMinimum(0);
    slider->setMaximum(duration * 100); /* in hundredth of a sec */
    slider->setValue(0);

    /* convert from seconds to hours:minutes:seconds */
    hours = duration / 3600;
    if (hours)
        duration -= (hours * 3600);

    minutes = duration / 60;
    if (minutes)
        duration -= minutes * 60;

    secs = duration;

    sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, secs);
    lblDuration->setText(QString(buf));
}

/**
 * time elapsed in 1/100th sec units since play started
 ******************************************************************************/
void MainWindow::on_elapsedTime(int val)
{
    int  hours    = 0;
    int  minutes  = 0;
    int  secs     = 0;
    int  duration = val / 100;
    char buf[20];

    /* if slider bar is down, do not update */
    if (slider->isSliderDown())
        return;

    /* update progress bar */
    slider->setSliderPosition(val);

    /* convert from seconds to hours:minutes:seconds */
    hours = duration / 3600;
    if (hours)
        duration -= (hours * 3600);

    minutes = duration / 60;
    if (minutes)
        duration -= minutes * 60;

    secs = duration;

    /* update current position in progress bar */
    sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, secs);
    lblCurrentPos->setText(QString(buf));
}

void MainWindow::on_sliderValueChanged(int value)
{
    if (acceptSliderMove)
    {
        acceptSliderMove = false;
        emit on_mediaSeek(value / 100);
    }
}

void MainWindow::on_sliderActionTriggered(int action)
{
    switch (action)
    {
    case QAbstractSlider::SliderPageStepAdd:
        acceptSliderMove = true;
        break;

    case QAbstractSlider::SliderPageStepSub:
        acceptSliderMove = true;
        break;

    case QAbstractSlider::SliderMove:
        if (slider->isSliderDown())
            acceptSliderMove = true;
        break;
    }
}

#if 1
// LK_TODO delete this
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    //qDebug() << "mouseMoveEvent: x=" << e->globalX() << "y=" << e->globalY();
}
#endif