Just Intonation  Version 1.3.1 (19)
Explore key-independent dynamically adapting tuning in just intonation
filehandler.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * JustIntonation is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Open File Dialog
22 //=============================================================================
23 
24 
25 #include "filehandler.h"
26 #include "system/shared.h"
27 #include "config.h"
28 
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QStandardPaths>
32 
33 //-----------------------------------------------------------------------------
34 // Constructor
35 //-----------------------------------------------------------------------------
39 
41 {
42  setModuleName("FileHandler");
43 }
44 
45 
46 //-----------------------------------------------------------------------------
47 // Open file open dialog
48 //-----------------------------------------------------------------------------
52 
53 void FileHandler::openMidiFileDialog (int width, int height)
54 {
55  QString path = QStandardPaths::locate(QStandardPaths::DocumentsLocation,"",
56  QStandardPaths::LocateDirectory);
57  if (path.endsWith("/") or path.endsWith("\\")) path.remove(path.size()-1,1);
58  LOGSTATUS << "Open file open dialog" << width << height;
59  QString fileName = QFileDialog::getOpenFileName(nullptr,
60  tr("Please choose a Midi file"), "", tr("Midi files (*.mid *.midi *.MID)"));
61  LOGMESSAGE << "Open file" << fileName;
62  emit openMidiFile(fileName,true);
63 }
64 
65 
66 //-----------------------------------------------------------------------------
67 // Save data (string) to file
68 //-----------------------------------------------------------------------------
74 
75 
76 void FileHandler::saveData (QString data1, QString data2, QString data3)
77 {
78  QString path = QStandardPaths::locate(INT_TEMPERAMENT_DIR,"",
79  QStandardPaths::LocateDirectory);
80  if (path.endsWith("/") or path.endsWith("\\")) path.remove(path.size()-1,1);
81  LOGSTATUS << "Searching path is:" << path << " Open file-save dialog";
82  QString filename = QFileDialog::getSaveFileName(0,tr("Save custom temperament"),
83  path, tr("Temperament files")+" (*.tem);;"+tr("All files") + " (*)");
84  if (filename == "") return;
85  if (not filename.contains(".")) filename += ".tem";
86  LOGMESSAGE << "Saving to file" << filename;
87  LOGSTATUS << "Content" << data1;
88  QFile file (filename);
89  if (not file.open(QIODevice::WriteOnly))
90  {
91  QMessageBox ::information (0,tr("Could not write to disk."),file.errorString());
92  return;
93  }
94  else
95  {
96  QTextStream out(&file);
97  out << tag << "\n" << data1 << "\n" << data2 << "\n" << data3;
98  file.close();
99  return;
100  }
101 }
102 
103 
104 //-----------------------------------------------------------------------------
105 // Load data (string) from file
106 //-----------------------------------------------------------------------------
111 
113 {
114  QString path = QStandardPaths::locate(INT_TEMPERAMENT_DIR,"",
115  QStandardPaths::LocateDirectory);
116  if (path.endsWith("/") or path.endsWith("\\")) path.remove(path.size()-1,1);
117  LOGSTATUS << "Searching path is:" << path;
118  QString filename = QFileDialog::getOpenFileName(0,tr("Load custom temperament"),
119  path, tr("Temperament files")+" (*.tem);;"+tr("All files") + " (*)");
120  if (filename == "") return;
121  if (not filename.contains(".")) filename += ".tem";
122  LOGMESSAGE << "Loading from file" << filename;
123  QFile file (filename);
124  if (not file.open(QIODevice::ReadOnly))
125  {
126  QMessageBox ::information (0,tr("Could not read from disk."),file.errorString());
127  return;
128  }
129  else
130  {
131  QTextStream in(&file);
132  QString firstline = in.readLine();
133  if (firstline != tag)
134  {
135  QMessageBox ::information (0,tr("Reading Error"),tr("This is not a data file generated by JustIntonation"));
136  return;
137  }
138  else
139  {
140  QString data1 = in.readLine();
141  QString data2 = in.readLine();
142  QString data3 = in.readLine();
143  LOGSTATUS << "Content: " << data1 << data2 << data3;
144  emit loadedData(data1,data2,data3);
145 
146  }
147  }
148 }
149 
FileHandler()
Constructor.
Definition: filehandler.cpp:40
void setModuleName(const QString &name)
Specify the name of the class-specific module.
Definition: log.cpp:82
#define LOGMESSAGE
Definition: log.h:43
void loadedData(QVariant data1, QVariant data2, QVariant data3)
void openMidiFile(QString fileName, bool autostart)
const QString tag
Definition: filehandler.h:50
void saveData(QString data1, QString data2, QString data3)
Save data (string) to file.
Definition: filehandler.cpp:76
void loadData()
Load data (string) from file.
#define LOGSTATUS
Definition: log.h:42
#define INT_TEMPERAMENT_DIR
Definition: config.h:105
void openMidiFileDialog(int width, int height)
Private slot: Open file open dialog.
Definition: filehandler.cpp:53