#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);
QTransform trans;
float scale = 1.0;
// Draw icon 'a'
trans.reset();
scale = 0.5 + 0.5 * ((20.0 - frame) / 20.0);
trans.scale(scale, scale);
p.setWorldTransform(trans);
p.setOpacity((20 - frame) / 20.0);
p.drawPixmap(480 * (1.0 - scale) + 160 - frame * 10, 0, a);
p.resetTransform();
// Draw icon 'a' reflected
trans.reset();
trans.scale(scale, -scale);
p.setWorldTransform(trans);
p.setOpacity(0.3 * ((20 - frame) / 20.0));
p.drawPixmap(480 * (1.0 - scale) + 160 - frame * 10, -2.0 * a.height(), a);
p.resetTransform();
// Draw icon 'b'
trans.reset();
scale = 0.5 + 0.5 * (frame / 20.0);
trans.scale(scale, scale);
p.setWorldTransform(trans);
p.setOpacity(frame/20.0);
p.drawPixmap(720 * (1.0 - scale) + 360 - frame * 10, 0, b);
p.resetTransform();
// Draw icon 'b' reflected
trans.reset();
trans.scale(scale, -scale);
p.setWorldTransform(trans);
p.setOpacity(0.3 * (frame / 20.0));
p.drawPixmap(720 * (1.0 - scale) + 360 - frame * 10, -2.0 * b.height(), b);
p.resetTransform();
}
QPixmap a, b;
int frame;
};
MainMenu::MainMenu(QWidget *parent) : Stage(parent)
{
static const bool labelAbove = true;
static const bool menuLeft = false;
animateIcons = QSettings("dpf.ini", QSettings::IniFormat).value("AnimateIcons", true).toBool();
curItem = 0;
icon = new AnimatedIcon(this);
icon->setFrame(0);
icon->setPixmaps(1,0);
icon->setGeometry(menuLeft ? 320 : -70, labelAbove ? 150 : 80, 640, 600);
icon->show();
icon->setPixmaps(0, 0);
icon->setFrame(0);
icon->update();
item1 = new Text(this, menuLeft ? 432 : 82, labelAbove ? 52: 342, "", 40, QColor(0, 0, 0, 128));
item1->setText(nameMap[curItem]);
item2 = new Text(this, menuLeft ? 430 : 80, labelAbove ? 50 : 340, "", 40, QColor(255, 255, 255));
item2->setText(nameMap[curItem]);
items = new MenuItem * [menuSize];
for (int i = 0; i < menuSize; i++)
items[i] = new MenuItem(this, menuLeft ? 60 : 450, 80 + i * 50, menuItems[i]);
items[curItem]->select();
timerId = 0;
frame = 0;
QTimer::singleShot(0, this, SLOT(init()));
}
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;
icon->setFrame(frame);
icon->update();
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);
if (!animateIcons) {
icon->setFrame(0);
icon->update();
killTimer(timerId);
}
} 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);
if (!animateIcons) {
icon->setFrame(20);
icon->update();
killTimer(timerId);
}
// } else if ( ke->key() == Qt::Key_Select ) {
} else if ( ke->key() == Qt::Key_Return ) {
QSound::play("sounds/Select.wav");
emit setStage(curItem);
}
}