Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
projectmanageradapter.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner 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  * Entropy Piano Tuner 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 for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Adapter for project management
22 //=============================================================================
23 
24 
25 #include "projectmanageradapter.h"
26 
27 #include "filemanager.h"
28 #include "../core.h"
29 #include "../system/eptexception.h"
30 #include "../system/log.h"
31 #include "../messages/messagehandler.h"
32 #include "../messages/messageprojectfile.h"
33 
34 using piano::FT_CSV;
35 using piano::FT_EPT;
36 using piano::FT_NONE;
38 
40  path(p),
41  fileType(parseFileType(p.substr(p.find_last_of(".") + 1))) {
42 }
43 
44 //-----------------------------------------------------------------------------
45 // Constructor
46 //-----------------------------------------------------------------------------
47 
49  : mCore(nullptr), // no pointer to core
50  mChangesInFile(false) // no changes
51 {
52 }
53 
54 
55 //-----------------------------------------------------------------------------
56 // Initialize the project management adapter
57 //-----------------------------------------------------------------------------
58 
63 
65 {
66  mCore = core;
67 
68  // use default file, but fill with current values
70  MessageHandler::send<MessageProjectFile>(
73 }
74 
75 
76 //-----------------------------------------------------------------------------
77 // Set a callback function for file changes
78 //-----------------------------------------------------------------------------
79 
90 
92 {
93  mCallback = cb;
94 }
95 
96 
97 
98 //-----------------------------------------------------------------------------
99 // Menu command: New file
100 //-----------------------------------------------------------------------------
101 
108 
110 {
111 
112  if (checkForNoChanges() == R_CANCELED) {return R_CANCELED;}
113 
114  // create new file
115  mCurrentFilePath.clear();
118 
119  setChangesInFile(true);
120  MessageHandler::send<MessageProjectFile>(MessageProjectFile::FILE_CREATED,
122 
123  // edit this new file
124  if (editFile() == R_ACCEPTED)
125  {
126  MessageHandler::send<MessageProjectFile>(MessageProjectFile::FILE_EDITED,
128  }
129  return R_ACCEPTED;
130 }
131 
132 
133 
134 //-----------------------------------------------------------------------------
135 // Menu command: Save
136 //-----------------------------------------------------------------------------
137 
139  if (mCurrentFilePath.size() == 0)
140  {
141  // no path specified, use save as
142  return onSaveFileAs();
143  }
144 
145  // save at current location
147  return R_ACCEPTED;
148 }
149 
150 
151 //-----------------------------------------------------------------------------
152 // Menu command: Save as...
153 //-----------------------------------------------------------------------------
154 
157  if (!r.isValid()) {
158  // user cancelled
159  return R_CANCELED;
160  }
161 
162  // file was stored
163  saveFile(r.path, r.fileType);
164  return R_ACCEPTED;
165 }
166 
167 
168 //-----------------------------------------------------------------------------
169 // Menu command: Open
170 //-----------------------------------------------------------------------------
171 
173 {
174  if (checkForNoChanges() == R_CANCELED) {return R_CANCELED;}
175 
177  if (!r.isValid()) {
178  // user cancelled
179  return R_CANCELED;
180  }
181 
182 
183  // file was opened
184  openFile(r.path);
185 
186  return R_ACCEPTED;
187 }
188 
189 
190 //-----------------------------------------------------------------------------
191 // Menu command: Quit
192 //-----------------------------------------------------------------------------
193 
195  if (mChangesInFile) {
196  switch (askForSaving()) {
197  case R_NO:
198  // dont save file, but resume
199  break;
200  case R_CANCELED:
201  return false;
202  default:
203  // save the file
204  if (onSaveFile() == R_CANCELED) {
205  // cancel aswell
206  return R_CANCELED;
207  }
208  break;
209  }
210  }
211 
212  LogI("Quitting accepted");
213 
214  return true;
215 }
216 
217 
218 //-----------------------------------------------------------------------------
219 // Menu command: Edit piano data sheet
220 //-----------------------------------------------------------------------------
221 
223 {
224  Results r = editFile();
225  // edit current file
226  if (r == R_ACCEPTED)
227  {
228  // there are changes
229  setChangesInFile(true);
230  // notify system that we edit the file
231  MessageHandler::send<MessageProjectFile>(MessageProjectFile::FILE_EDITED, mCore->getPianoManager()->getPiano());
232  }
233 
234  return r;
235 }
236 
237 
238 //-----------------------------------------------------------------------------
239 // Menu command: Edit piano data sheet
240 //-----------------------------------------------------------------------------
241 
242 
243 
245  if (checkForNoChanges() != R_ACCEPTED) {return R_CANCELED;}
246 
247  // share file
248  return share();
249 }
250 
251 
253  if (checkForNoChanges() != R_ACCEPTED) {return R_CANCELED;}
254 
256  if (!r.isValid()) {
257  // user cancelled
258  return R_CANCELED;
259  }
260 
261  // file was stored
262  saveFile(r.path, r.fileType);
263  return R_ACCEPTED;
264 }
265 
266 
268  if (mChangesInFile) {
269  switch (askForSaving()) {
270  case R_NO:
271  // dont save file, but resume
272  break;
273  case R_CANCELED:
274  return R_CANCELED;
275  default:
276  // save the file first
277  if (onSaveFile() == R_CANCELED) {
278  // canceling the save progress will also cancel the next desired process
279  return R_CANCELED;
280  }
281  break;
282  }
283  }
284  return R_ACCEPTED;
285 }
286 
288  switch (m->getType()) {
293  setChangesInFile(true);
294  break;
295  default:
296  break;
297  }
298 }
299 
301  mChangesInFile = b;
302  if (mCallback) {
304  }
305 
306 }
307 
309  EptAssert(type != FT_NONE, "File type not valid.");
310  EptAssert(path.size() > 0, "Path not valid.");
311 
312  try {
313  if (!mPianoFile.write(path, mCore->getPianoManager()->getPiano(), type)) {
314  LogW("File could not be saved");
315  return R_CANCELED;
316  }
317 
318 
319  LogI("File saved!");
320  mCurrentFilePath = path;
321  setChangesInFile(false);
322 
323  MessageHandler::send<MessageProjectFile>(MessageProjectFile::FILE_SAVED, mCore->getPianoManager()->getPiano());
324  }
325  catch (const EptException &e) {
326  LogW("Exception during saving a file: %s", e.what());
327  // show message box, that there is an error
328  showSaveError();
329  return R_CANCELED;
330  }
331  return R_ACCEPTED;
332 }
333 
335  try {
336  if (!mPianoFile.read(path, mCore->getPianoManager()->getPiano())) {
337  LogW("File could not be opened");
338  return R_CANCELED;
339  }
340 
341 
342  LogI("File opened!");
343  if (cached) {
344  // file is in cache, we need to save it at a new position
345  mCurrentFilePath.clear();
346  setChangesInFile(true);
347  } else {
348  mCurrentFilePath = path;
349  setChangesInFile(false);
350  }
351 
352  MessageHandler::send<MessageProjectFile>(MessageProjectFile::FILE_OPENED, mCore->getPianoManager()->getPiano());
353  }
354  catch (const EptException &e) {
355  LogW("Exception during opening a file: %s", e.what());
356  // show message box, that there is an error
357  showOpenError();
358  return R_CANCELED;
359  }
360  return R_ACCEPTED;
361 }
sent by SignalAnalyzer if FFT is ready
Definition: message.h:66
PianoManager * getPianoManager()
Definition: core.h:67
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
virtual void showOpenError()=0
Show a message box that an error occurred during the open process.
virtual void fillNew(Piano &piano)=0
function to fill the piano data with the current new settings
called when the recording was cleared
Definition: message.h:50
void setCallback(FileChangesCallback *cb)
Set a callback function for file changes.
data of a key changed
Definition: message.h:62
const char * what() const
Override std::exception::what.
Definition: eptexception.h:109
void setChangesInFile(bool b)
change the state of mChangesInFile and notify the listener
FileType
supported piano file types
Definition: pianodefines.h:59
#define LogW(...)
Definition: log.h:56
Definition: piano.h:40
Piano & getPiano()
Definition: pianomanager.h:45
std::string mCurrentFilePath
current file path
PianoFile mPianoFile
the piano file
bool write(const std::string &absolutePath, const Piano &piano, piano::FileType fileType) const
Definition: pianofile.cpp:62
bool read(const std::string &absolutePath, Piano &piano)
Definition: pianofile.cpp:47
Results
Enumeration of the possible outcomes of a typical mouse button.
Results openFile(const std::string &path, bool cached=false)
The ProjectManagerChangesInFileUpdatedCallback class.
Results onNewFile()
New-file menu response.
virtual FileDialogResult getSavePath(int fileType)=0
get a path were to save the file
#define LogI(...)
Definition: log.h:50
void init(Core *core)
Initialize the project management adapter.
Message that the tuning curve has been adapted.
Definition: message.h:60
virtual void showSaveError()=0
Show a message box that an error occurred during the save process.
virtual Results share()=0
shares the current file with the actual device
CORE : Class managing the core.
Definition: core.h:45
virtual void handleMessage(MessagePtr m) override
message listener callback
#define EptAssert(a, b)
Definition: eptexception.h:47
ProjectManagerAdapter()
Constructor.
FileType parseFileType(const std::string &fileEnding)
bool mChangesInFile
stores whether there are unsaved changes in the current file
virtual void changesInFileUpdated(bool changes)=0
virtual FileDialogResult getOpenPath(int fileType)=0
get a path to a file to open
Results saveFile(const std::string &path, piano::FileType type)
virtual Results askForSaving()=0
asks the user whether to save the current project file
FileChangesCallback * mCallback
callback if mChangesInFileChanged
virtual Results editFile()=0
edit a project file