#include <QKeyEvent>
#include <QLabel>
#include <QPainter>
#include <QSound>
#include <QSettings>
#include <QTimer>
#include "mainmenu.h"
#include "image.h"
const char *iconMap[] = {
"slideshow",
"photos",
"music",
"videos",
"info",
"settings",
"shutdown"
};
const char *nameMap[] = {
QT_TRANSLATE_NOOP("DPF", " Slide Show"),
QT_TRANSLATE_NOOP("DPF", " Photos"),
QT_TRANSLATE_NOOP("DPF", " Music"),
QT_TRANSLATE_NOOP("DPF", " Videos"),
QT_TRANSLATE_NOOP("DPF", " Information"),
QT_TRANSLATE_NOOP("DPF", " Settings"),
QT_TRANSLATE_NOOP("DPF", " Shut Down")
};
const char *menuItems[] = {
QT_TRANSLATE_NOOP("DPF", "Slide Show"),
QT_TRANSLATE_NOOP("DPF", "Browse Photos"),
QT_TRANSLATE_NOOP("DPF", "Browse Music"),
QT_TRANSLATE_NOOP("DPF", "Browse Videos"),
QT_TRANSLATE_NOOP("DPF", "Information"),
QT_TRANSLATE_NOOP("DPF", "Settings"),
QT_TRANSLATE_NOOP("DPF", "Shut Down")
};
const int menuSize = sizeof(menuItems) / sizeof(const char *);
class AnimatedIcon : public QWidget
{
public:
AnimatedIcon(QWidget *p) : QWidget(p) {
// nothing
}
~AnimatedIcon() {
// nothing
}
void setPixmaps(int _a, int _b) {
a = Image::icon(iconMap[_a], 256);
b = Image::icon(iconMap[_b], 256);
}
void setFrame(int f) {
frame = f;
}
void paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setOpacity((20-frame)/20.0);
p.drawPixmap(160-frame*10, 0, a);
p.setOpacity(frame/20.0);
p.drawPixmap(360-frame*10, 0, b);
}
QPixmap a, b;
int frame;
};
MainMenu::MainMenu(QWidget *parent) : Stage(parent)
{
curItem = 0;
items = new MenuItem*[menuSize];
for (int i = 0; i < menuSize; i++)
items[i] = new MenuItem(this, 60, 80+i*50, menuItems[i]);
items[curItem]->select();
timerId = 0;
frame = 0;
QTimer::singleShot(0, this, SLOT(init()));
icon = new AnimatedIcon(this);
icon->setFrame(0);
icon->setPixmaps(1,0);
icon->setGeometry(320,80,640,300);
icon->show();
icon->setPixmaps(0, 0);
icon->setFrame(0);
icon->update();
item1 = new Text(this, 432, 342, "", 40, QColor(0,0,0,128));
item1->setText(nameMap[curItem]);
item2 = new Text(this, 430, 340, "", 40, QColor(255,255,255));
item2->setText(nameMap[curItem]);
//animate(1, menuSize-1, 0);
}
MainMenu::~MainMenu()
{
for (int i = 0; i < menuSize; i++)
delete items[i];
killTimer(timerId);
}
void MainMenu::init()
{
changeBackground("any");
}
void MainMenu::changeBackground(QString backg)
{
QSettings settings("dpf.ini", QSettings::IniFormat);
QString background = settings.value("Background", "image").toString();
bg->setPixmap(QPixmap("pics/" + background + "_bg0.png"));
}
void MainMenu::animate(int dir, int before, int now)
{
direction = dir;
killTimer(timerId);
if (dir == -1)
frame = 20;
else
frame = 0;
if (dir == -1) {
item1->setText(nameMap[before]);
item2->setText(nameMap[before]);
} else {
item1->setText(nameMap[now]);
item2->setText(nameMap[now]);
}
timerId = startTimer(15);
icon->setPixmaps(before, now);
}
void MainMenu::timerEvent(QTimerEvent *te)
{
frame += direction;
icon->setFrame(frame);
icon->update();
if ( frame >= 20 || frame <= 0)
killTimer(timerId);
}
void MainMenu::keyPressEvent(QKeyEvent *ke)
{
int prevItem = curItem;
if ( ke->key() == Qt::Key_Up ) {
items[curItem]->unselect();
curItem--;
if (curItem < 0)
curItem = menuSize - 1;
if (curItem == 0)
QSound::play("sounds/Prev.wav");
else
QSound::play("sounds/Next.wav");
items[curItem]->select();
animate(-1, curItem, prevItem);
} else if ( ke->key() == Qt::Key_Down ) {
items[curItem]->unselect();
curItem++;
if (curItem > (menuSize-1))
curItem = 0;
if (curItem == (menuSize-1))
QSound::play("sounds/Prev.wav");
else
QSound::play("sounds/Next.wav");
items[curItem]->select();
animate(1, prevItem, curItem);
// } else if ( ke->key() == Qt::Key_Select ) {
} else if ( ke->key() == Qt::Key_Return ) {
QSound::play("sounds/Select.wav");
emit setStage(curItem);
}
}