Mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent), timer(QTime::currentTime()), record(false)
{
QString notes = "Q1W2ER4T5Y6UI8O9P[-]=,K.ZAXSCVFBGNHM";
bar = new QMenuBar;
bar->setFixedHeight(25);
QMenu* menu_file = new QMenu("&Файл");
menu_file->addAction("&Открыть запись",this,SLOT(openFile()),Qt::CTRL+Qt::Key_O);
menu_file->addAction("&Сохранить запись",this,SLOT(saveFile()),Qt::CTRL+Qt::Key_S);
menu_file->addSeparator();
menu_file->addAction("&Выход",qApp,SLOT(quit()),Qt::CTRL+Qt::Key_Q);
bar->addMenu(menu_file);
bar->addAction("&Разработчик",this,SLOT(about_dev()));
list_piano = new QList<PianoButton*>;
PianoButton* temp;
for(int i=0;i<2;++i)
{
temp = new PianoButton(0,notes.mid(i*12+0,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+1,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+2,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+3,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+4,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+5,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+6,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+7,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+8,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+9,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+10,1));
list_piano->append(temp);
temp = new PianoButton(0,notes.mid(i*12+11,1));
list_piano->append(temp);
}
PianoButton* btn;
for(int i=0;i<list_piano->count();++i)
{
btn = list_piano->at(i);
connect(this,SIGNAL(press(QKeyEvent*)),btn,SLOT(onPress(QKeyEvent*)));
connect(this,SIGNAL(press(QKeyEvent*,QTime)),btn,SLOT(onPress(QKeyEvent*,QTime)));
connect(this,&MainWindow::release,btn,&PianoButton::onRelease);
connect(this,&MainWindow::restart,btn,&PianoButton::onRestart);
connect(this,&MainWindow::play,btn,&PianoButton::onReplyRecord);
connect(this,&MainWindow::stop,btn,&PianoButton::onStopRecord);
list_piano->at(i)->effect->setSource(QUrl::fromLocalFile(":/sounds/"+QString::number(i)+".wav"));
}
tools = new QToolBar;
tools->addAction("Запись",this,SLOT(onRecord()));
tools->addAction("Стоп запись",this,SLOT(onStopRecord()));
tools->addAction("Проиграть",this,SIGNAL(play()));
QGridLayout* grid = new QGridLayout;
grid->setMargin(5);
grid->setSpacing(5);
for(int i=0;i<2;++i)
{
grid->addWidget(list_piano->at(i*12+0),1,i*7+0,1,1);
grid->addWidget(list_piano->at(i*12+1),0,i*7+0,1,2,Qt::AlignHCenter);
grid->addWidget(list_piano->at(i*12+2),1,i*7+1,1,1);
grid->addWidget(list_piano->at(i*12+3),0,i*7+1,1,2,Qt::AlignHCenter);
grid->addWidget(list_piano->at(i*12+4),1,i*7+2,1,1);
grid->addWidget(list_piano->at(i*12+5),1,i*7+3,1,1);
grid->addWidget(list_piano->at(i*12+6),0,i*7+3,1,2,Qt::AlignHCenter);
grid->addWidget(list_piano->at(i*12+7),1,i*7+4,1,1);
grid->addWidget(list_piano->at(i*12+8),0,i*7+4,1,2,Qt::AlignHCenter);
grid->addWidget(list_piano->at(i*12+9),1,i*7+5,1,1);
grid->addWidget(list_piano->at(i*12+10),0,i*7+5,1,2,Qt::AlignHCenter);
grid->addWidget(list_piano->at(i*12+11),1,i*7+6,1,1);
}
QVBoxLayout* vlayo = new QVBoxLayout;
vlayo->setMargin(5);
vlayo->setSpacing(5);
vlayo->addWidget(bar);
vlayo->addWidget(tools);
vlayo->addLayout(grid);
this->setLayout(vlayo);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(record)
{
emit press(event,QTime::currentTime());
} else
{
emit press(event);
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
emit release(event);
}
void MainWindow::onRecord()
{
timer = QTime::currentTime();
emit restart(timer);
record = true;
}
void MainWindow::onStopRecord()
{
record = false;
emit stop();
}
void MainWindow::openFile()
{
QString str = QFileDialog::getOpenFileName(this,"Открыть запись",QString(),"My melody files (*.mym)");
if(!str.isEmpty())
{
QFile file(str);
QTextStream stream(&file);
if(file.open(QIODevice::ReadOnly))
{
while(!stream.atEnd())
{
QStringList list = stream.readLine().split(';',QString::SkipEmptyParts);
// qDebug() << list;
list_piano->at(list.at(0).toInt())->start = QTime::fromString(list.at(1),"hh:mm:ss.zzz");
list_piano->at(list.at(0).toInt())->que->clear();
for(int i=2;i<list.count();++i)
{
list_piano->at(list.at(0).toInt())->que->enqueue(QTime::fromString(list.at(i),"hh:mm:ss.zzz"));
}
}
}
file.close();
}
}
void MainWindow::saveFile()
{
QString str = QFileDialog::getSaveFileName(this,"Сохранить запись",QString(),"My melody files (*.mym)");
if(!str.isEmpty())
{
QFile file(str);
QTextStream stream(&file);
if(file.open(QIODevice::WriteOnly))
{
for(int i=0;i<list_piano->count();++i)
{
QQueue<QTime> temp = *list_piano->at(i)->que;
if(temp.count()>0)
{
stream << QString::number(i)+";";
stream << list_piano->at(i)->start.toString("hh:mm:ss.zzz")+";";
while(!temp.isEmpty())
{
stream << temp.dequeue().toString("hh:mm:ss.zzz")+";";
}
stream << "\n";
}
}
}
file.close();
}
}
void MainWindow::about_dev(){
QMessageBox::information(this,"Разработчик","Резанов Богдан КИТ-83");
}
Дата добавления: 2014-12-06; просмотров: 722;