-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmextractormainwindow.cpp
More file actions
131 lines (122 loc) · 3.72 KB
/
mmextractormainwindow.cpp
File metadata and controls
131 lines (122 loc) · 3.72 KB
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
#include "mmextractormainwindow.h"
#include<QAudioDeviceInfo>
#include<QMessageBox>
#include<QFile>
#include<QMenuBar>
#include<QFileDialog>
void MMExtractorMainWindow::fillTree()
{
_tree->clear();
foreach(const QString& name, _parser.content().keys())
{
_tree->addTopLevelItem(new QTreeWidgetItem({name}));
}
}
MMExtractorMainWindow::MMExtractorMainWindow(QWidget *parent)
: QMainWindow(parent)
{
//gui
_tree = new QTreeWidget();
_tree->setHeaderLabel("Element");
_tree->setRootIsDecorated(false);
_tree->setSelectionMode(QAbstractItemView::ExtendedSelection);
_tree->setContextMenuPolicy(Qt::CustomContextMenu);
setCentralWidget(_tree);
connect(_tree,&QTreeWidget::currentItemChanged,this,&MMExtractorMainWindow::itemChanged);
connect(_tree,&QTreeWidget::customContextMenuRequested,this,&MMExtractorMainWindow::showTreeContextMenu);
//audio
_audioPlayer = new AudioPlayer(this);
//menu bar
fileMenu = this->menuBar()->addMenu("File");
selectFileAction = fileMenu->addAction("Select File",this,&MMExtractorMainWindow::selectFile);
fileMenu->addSeparator();
selectFileAction = fileMenu->addAction("Exit",this,&MMExtractorMainWindow::close);
//tree context menu
treeWidgetContextMenu.addAction("Export",this,&MMExtractorMainWindow::exportSelectedSounds);
}
MMExtractorMainWindow::~MMExtractorMainWindow()
{
}
void MMExtractorMainWindow::itemChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
{
if(_parser.content().contains(current->data(0,Qt::DisplayRole).toString()))
{
QByteArray content = _parser.content()[current->data(0,Qt::DisplayRole).toString()];
_audioPlayer->play(content);
}
}
void MMExtractorMainWindow::selectFile()
{
QString selected = QFileDialog::getOpenFileName(nullptr,"Select .snd file",currentSnd,"Might and Magic .snd file (*.snd);;All files (*)");
if(!selected.isEmpty())
{
currentSnd = selected;
_parser.load(currentSnd);
fillTree();
}
}
void MMExtractorMainWindow::showTreeContextMenu(const QPoint& pos)
{
if(_tree->selectedItems().size()>0)
{
treeWidgetContextMenu.exec(_tree->mapToGlobal(pos));
}
}
void MMExtractorMainWindow::exportSounds(const QList<QTreeWidgetItem*>& items)
{
if(items.size()==1)
{
QString location = QFileDialog::getSaveFileName(nullptr,"Save As",saveDir.absoluteFilePath(items[0]->data(0,Qt::DisplayRole).toString()+".wav"),"WAVE file (*.wav)");
if(!location.isEmpty())
{
saveDir = location;
QFile file(location);
if(file.open(QIODevice::WriteOnly))
{
file.write(_parser.content()[items[0]->data(0,Qt::DisplayRole).toString()]);
file.close();
}
else
{
QMessageBox::critical(nullptr,"Error","Error: Could not save file.");
}
}
}
else if(items.size()>1)
{
QString location = QFileDialog::getExistingDirectory(nullptr,"Select Directory",saveDir.absoluteFilePath("."));
if(!location.isEmpty())
{
saveDir = location;
int failCtr = 0;
foreach(auto item, items)
{
QFile file(saveDir.absoluteFilePath(item->data(0,Qt::DisplayRole).toString()+".wav"));
if(file.open(QIODevice::WriteOnly))
{
file.write(_parser.content()[item->data(0,Qt::DisplayRole).toString()]);
file.close();
}
else
{
failCtr++;
}
}
if(failCtr!=0)
{
if(failCtr==items.size())
{
QMessageBox::critical(nullptr,"Error","Error: Could not save files.");
}
else
{
QMessageBox::critical(nullptr,"Error",QString("Error: Could not save %1 files.").arg(QString::number(failCtr)));
}
}
}
}
}
void MMExtractorMainWindow::exportSelectedSounds()
{
exportSounds(_tree->selectedItems());
}