1 /***************************************************************************
2 * Copyright (C) 2009-2021 by Ilya Kotov *
3 * forkotov02@ya.ru *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
20 #include <QTimer>
21 #include <QSettings>
22 #include <QPainter>
23 #include <QMenu>
24 #include <QActionGroup>
25 #include <QHBoxLayout>
26 #include <QSplitter>
27 #include <QListWidget>
28 #include <math.h>
29 #include <stdlib.h>
30 #include <locale.h>
31 #include <libprojectM/projectM.hpp>
32 #include <qmmp/buffer.h>
33 #include <qmmp/output.h>
34 #include "projectmwidget.h"
35 #include "projectmplugin.h"
36
ProjectMPlugin(QWidget * parent)37 ProjectMPlugin::ProjectMPlugin (QWidget *parent)
38 : Visual (parent, Qt::Window | Qt::MSWindowsOwnDC)
39 {
40 setlocale(LC_NUMERIC, "C"); //fixes problem with non-english locales
41 setWindowTitle(tr("ProjectM"));
42 setWindowIcon(parent->windowIcon());
43
44 m_splitter = new QSplitter(Qt::Horizontal, this);
45 QListWidget *listWidget = new QListWidget(m_splitter);
46 listWidget->setAlternatingRowColors(true);
47 m_splitter->addWidget(listWidget);
48 m_projectMWidget = new ProjectMWidget(listWidget, m_splitter);
49 m_splitter->addWidget(m_projectMWidget);
50
51 m_splitter->setStretchFactor(1,1);
52 QHBoxLayout *layout = new QHBoxLayout;
53 layout->addWidget(m_splitter);
54 layout->setContentsMargins(0,0,0,0);
55 setLayout(layout);
56 addActions(m_projectMWidget->actions());
57 connect(m_projectMWidget, SIGNAL(showMenuToggled(bool)), listWidget, SLOT(setVisible(bool)));
58 connect(m_projectMWidget, SIGNAL(fullscreenToggled(bool)), SLOT(setFullScreen(bool)));
59 listWidget->hide();
60 resize(600,400);
61 QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
62 restoreGeometry(settings.value("ProjectM/geometry").toByteArray());
63 m_splitter->setSizes(QList<int>() << 300 << 300);
64 m_splitter->restoreState(settings.value("ProjectM/splitter_sizes").toByteArray());
65
66 m_timer = new QTimer(this);
67 m_timer->setInterval(0);
68 connect(m_timer, SIGNAL(timeout()), SLOT(onTimeout()));
69 }
70
~ProjectMPlugin()71 ProjectMPlugin::~ProjectMPlugin()
72 {}
73
start()74 void ProjectMPlugin::start()
75 {
76 if(isVisible())
77 m_timer->start();
78 }
79
stop()80 void ProjectMPlugin::stop()
81 {
82 update();
83 }
84
onTimeout()85 void ProjectMPlugin::onTimeout()
86 {
87 projectM *instance = m_projectMWidget->projectMInstance();
88 if (!instance)
89 return;
90
91 if(takeData(m_left, m_right))
92 {
93 for(size_t i = 0; i < 512; i++)
94 {
95 m_buf[0][i] = m_left[i] * 32767.0;
96 m_buf[1][i] = m_right[i] * 32767.0;
97 }
98
99 m_projectMWidget->projectMInstance()->pcm()->addPCM16(m_buf);
100 }
101
102 m_projectMWidget->update();
103 }
104
setFullScreen(bool yes)105 void ProjectMPlugin::setFullScreen(bool yes)
106 {
107 if(yes)
108 setWindowState(windowState() | Qt::WindowFullScreen);
109 else
110 setWindowState(windowState() & ~Qt::WindowFullScreen);
111 }
112
closeEvent(QCloseEvent * event)113 void ProjectMPlugin::closeEvent (QCloseEvent *event)
114 {
115 //save geometry
116 QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
117 settings.setValue("ProjectM/geometry", saveGeometry());
118 settings.setValue("ProjectM/splitter_sizes", m_splitter->saveState());
119 Visual::closeEvent(event); //removes visualization object
120 }
121
showEvent(QShowEvent *)122 void ProjectMPlugin::showEvent(QShowEvent *)
123 {
124 m_timer->start();
125 }
126
hideEvent(QHideEvent *)127 void ProjectMPlugin::hideEvent(QHideEvent *)
128 {
129 m_timer->stop();
130 }
131