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
projectmanagerforqt.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 #include "projectmanagerforqt.h"
21 #include "editpianosheetdialog.h"
22 #include <QDialog>
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <QStandardPaths>
26 #include <QDate>
27 #ifdef __ANDROID__
28 #include <QAndroidJniObject>
29 #include <QAndroidJniEnvironment>
30 #endif
31 
32 #include "qtconfig.h"
33 #include "settingsforqt.h"
34 #include "../core/system/log.h"
35 #include "../core/config.h"
36 #include "mainwindow.h"
37 #include "simplefiledialog.h"
38 
41  mMainWindow(mainwindow) {
42 
43 }
44 
46 {
47 
48 }
49 
50 
53 
54  if (d.exec() == QDialog::Accepted) {
56  return R_ACCEPTED;
57  } else {
58  return R_CANCELED;
59  }
60 }
61 
63  QMessageBox msgBox(mMainWindow);
64  msgBox.setWindowTitle(mMainWindow->windowTitle());
65  msgBox.setIcon(QMessageBox::Question);
66  msgBox.setText(MainWindow::tr("The document has been modified."));
67  msgBox.setInformativeText(MainWindow::tr("Do you want to save your changes?"));
68  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
69  switch (msgBox.exec()) {
70  case QMessageBox::Yes:
71  return R_YES;
72  case QMessageBox::No:
73  return R_NO;
74  default:
75  return R_CANCELED;
76  }
77 }
78 
80 #if CONFIG_USE_SIMPLE_FILE_DIALOG
81  Q_UNUSED(fileType);
82  return SimpleFileDialog::getSaveFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toStdString();
83 #else
84  QString path(getCurrentPath());
85  QDir().mkdir(path);
86 
87 
88  QFileDialog d(mMainWindow, MainWindow::tr("Save"), path, getFileFilters(fileType, true));
89  d.setAcceptMode(QFileDialog::AcceptSave);
90  d.setFileMode(QFileDialog::AnyFile);
91  if (fileType & piano::FT_EPT) {d.setDefaultSuffix("ept");}
92  else if (fileType & piano::FT_CSV) {d.setDefaultSuffix("csv");}
93  SHOW_DIALOG(&d);
94  if (d.exec() == QDialog::Accepted) {
95  setCurrentPath(d.directory().absolutePath());
96  const QString file = d.selectedFiles().first();
97  return file.toStdString();
98  } else {
99  return std::string();
100  }
101 #endif
102 }
103 
105 #if CONFIG_USE_SIMPLE_FILE_DIALOG
106  Q_UNUSED(fileType);
107  return SimpleFileDialog::getOpenFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toStdString();
108 #else
109  QString path(getCurrentPath());
110  QDir().mkdir(path);
111  QFileDialog d(mMainWindow, MainWindow::tr("Open"), path, getFileFilters(fileType, true));
112  d.setAcceptMode(QFileDialog::AcceptOpen);
113  d.setFileMode(QFileDialog::AnyFile);
114  SHOW_DIALOG(&d);
115  if (d.exec() == QDialog::Accepted) {
116  setCurrentPath(d.directory().absolutePath());
117  return d.selectedFiles().first().toStdString();
118  } else {
119  return std::string();
120  }
121 #endif
122 }
123 
125 #ifdef __ANDROID__
126  // on android we have to call a java method, that does the sharing
127 
128  // get the application instance
129  QAndroidJniObject instance = QAndroidJniObject::callStaticObjectMethod("org/uniwue/tp3/TunerApplication", "getInstance", "()Lorg/uniwue/tp3/TunerApplication;");
130  QAndroidJniObject jTitle = QAndroidJniObject::fromString(MainWindow::tr("Share tuning data"));
131  QAndroidJniObject jPath = QAndroidJniObject::fromString(QString::fromStdString(getCurrentFilePath()));
132  // get the path to the file to open, zero length if there is none
133  instance.callMethod<void>("shareFile", "(Ljava/lang/String;Ljava/lang/String;)V", jTitle.object<jstring>(), jPath.object<jstring>());
134  // check for errors
135  QAndroidJniEnvironment env;
136  if (env->ExceptionCheck()) {
137  env->ExceptionDescribe();
138  env->ExceptionClear();
139  }
140 #else
141  LogI("Sharing for this platform is not supported yet.");
142 #endif
143  return R_CANCELED; // function has to return something, therefore I put this here
144 }
145 
147  piano.setName(MainWindow::tr("New piano").toStdString());
148  piano.setSerialNumber("0000-0000");
149  piano.setManufactureYear(QString("%1").arg(QDate::currentDate().year()).toStdString());
150  piano.setManufactureLocation(MainWindow::tr("Unknown").toStdString());
151 
152  piano.setTuningLocation(MainWindow::tr("Unknown").toStdString());
157 }
158 
160  QMessageBox::critical(mMainWindow, MainWindow::tr("Error"), MainWindow::tr("File could not be opened."));
161 }
162 
164  QMessageBox::critical(mMainWindow, MainWindow::tr("Error"), MainWindow::tr("File could not be saved."));
165 }
166 
168  QSettings s;
169  return s.value(SettingsForQt::KEY_CURRENT_FILE_DIALOG_PATH, QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
170 }
171 
173  QSettings s;
175  EptAssert(QDir(path).exists(), "Default path should exist.");
176 }
177 
178 QString ProjectManagerForQt::getFileFilters(int fileTypes, bool addAll) const {
179  QString files;
180  if (fileTypes & piano::FT_EPT) {
181  files += MainWindow::tr("Entropy piano tuner") + " (*.ept);;";
182  }
183  if (fileTypes & piano::FT_CSV) {
184  files += MainWindow::tr("Comma-separated values") + " (*.csv);;";
185  }
186  if (addAll) {
187  files += MainWindow::tr("All files") + " (*)";
188  }
189  return files;
190 }
void setConcertPitch(double pitch)
Definition: piano.h:79
PianoManager * getPianoManager()
Definition: core.h:67
ProjectManagerForQt(MainWindow *mainwindow)
Default constructor.
static const QString KEY_CURRENT_FILE_DIALOG_PATH
Definition: settingsforqt.h:36
static const double DEFAULT_CONCERT_PITCH
default concert pitch of A
Definition: piano.h:46
void setCurrentPath(QString path)
Function to set the current path for the file dialog.
virtual void fillNew(Piano &piano) overridefinal
Fills the given Piano with default values and texts.
void setName(const std::string &name)
Definition: piano.h:56
void changeKeyboardConfiguration(int numberOfKeys, int keyNumberOfA)
Change keyboard configuration.
Definition: keyboard.cpp:73
QString getCurrentPath() const
Function to get the current path for the file dialog.
MainWindow * mMainWindow
The main window.
#define SHOW_DIALOG(d)
Definition: qtconfig.h:41
static const int DEFAULT_KEY_NUMBER_OF_A
Definition: piano.h:44
Definition: piano.h:40
virtual Results share() overridefinal
Handle the share button.
Piano & getPiano()
Definition: pianomanager.h:45
virtual void showSaveError() overridefinal
Shows an error message if an error during saving occured.
virtual void showOpenError() overridefinal
Shows an error message if an error during open occured.
Dialog to edit the piano data sheet stored in a Piano.
Results
Enumeration of the possible outcomes of a typical mouse button.
void setSerialNumber(const std::string &number)
Definition: piano.h:63
virtual ~ProjectManagerForQt()
Empty virtual destructor.
virtual Results askForSaving() overridefinal
Displays a standard message box if the user wants to save the changes made.
static QString getSaveFile(QDir dir)
const Keyboard & getKeyboard() const
Definition: piano.h:83
void setTuningTimeToActualTime()
This function resets the tuning time to the actual time. This is used as default value.
Definition: piano.cpp:70
void setManufactureYear(const std::string &year)
Definition: piano.h:66
virtual FileDialogResult getOpenPath(int fileType) overridefinal
Opens a QFileDialog to get a path to open a file.
static const int DEFAULT_NUMBER_OF_KEYS
Definition: piano.h:43
#define LogI(...)
Definition: log.h:50
static QString getOpenFile(QDir dir)
QString getFileFilters(int fileTypes, bool addAll) const
#define EptAssert(a, b)
Definition: eptexception.h:47
void setTuningLocation(const std::string &loc)
Definition: piano.h:72
void setManufactureLocation(const std::string &loc)
Definition: piano.h:69
virtual Results editFile() overridefinal
Opens the EditPianoSheetDialog to edit the piano data sheet.
const std::string & getCurrentFilePath() const
The main window.
Definition: mainwindow.h:49
virtual FileDialogResult getSavePath(int fileType) overridefinal
Opens a QFileDialog to get a path to save the file to.
Project manager adapter class.
void applyData(Piano *piano) const
Applys the current values in the dialog to the given piano.