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
optionsdialog.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 "optionsdialog.h"
21 #include <QMessageBox>
22 #include <QSpacerItem>
23 #include <QScrollArea>
24 #include <QVBoxLayout>
25 #include <QToolBar>
26 #include <assert.h>
27 #include <QDebug>
28 #include "../core/system/log.h"
29 #include "../core/config.h"
30 #include "../core/core.h"
31 #include "../core/adapters/projectmanageradapter.h"
32 #include "mainwindow.h"
33 #include "audio/optionspageaudio.h"
35 #include "../settingsforqt.h"
36 #include "qtconfig.h"
37 #include "displaysize.h"
38 #include "../core/messages/messagehandler.h"
39 
40 namespace options {
41 
43  : QDialog(mainWindow, Qt::Window),
44  mMainWindow(mainWindow),
45  mCore(mainWindow->getCore()),
46  mCentralWidget(0),
47  mChangesMade(false) {
48  setWindowTitle(tr("Options"));
49  setModal(true);
50 
51  mMainLayout = new QGridLayout(this);
52 
53  mMainLayout->addWidget(mTitle = new QLabel(), 0, 1);
54  QFont titleFont(mTitle->font());
55  titleFont.setPointSizeF(titleFont.pointSizeF() * 1.5);
56  titleFont.setBold(true);
57  mTitle->setFont(titleFont);
58 
59  QButtonGroup *group = new QButtonGroup(this);
60  mPageButtons = group;
61 
62  QToolBar *selectionToolBar = new QToolBar;
63  //selectionToolBar->setAllowedAreas(Qt::LeftToolBarArea);
64  //selectionToolBar->setFloatable(true);
65  selectionToolBar->setOrientation(Qt::Vertical);
66  int iconSize = selectionToolBar->fontMetrics().height() * 2;
67  selectionToolBar->setIconSize(QSize(iconSize, iconSize));
68  mMainLayout->addWidget(selectionToolBar, 1, 0);
69 
70  addPageButton(QIcon(":/media/icons/user-desktop.png"), tr("Environment"), group, selectionToolBar, PAGE_ENVIRONMENT);
71  addPageButton(QIcon::fromTheme("audio-card", QIcon(":/media/icons/audio-card.png")), tr("Audio"), group, selectionToolBar, PAGE_AUDIO);
72 
73  QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel, this);
74  mMainLayout->addWidget(buttonBox, 2, 1);
75  QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
76  QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
77  QObject::connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(onApply()));
78 
79  setLayout(mMainLayout);
80 
81  int startupId = std::max<int>(0, SettingsForQt::getSingleton().getLastVisitedOptionsPage());
82  group->button(startupId)->setChecked(true);
83  onCurrentSelectionChanged(startupId);
84  QObject::connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onCurrentSelectionChanged(int)));
85 
86  resize(mMainWindow->centralWidget()->size());
87  SHOW_DIALOG(this);
88 }
89 
91 {
92 }
93 
95  onApply();
96  QDialog::accept();
97 }
98 
100  if (!checkForChanges(true)) {return;}
101 
102  QDialog::reject();
103 }
104 
106  checkForChanges(false);
107 
108  clearPages();
109 
110 
111  mChangesMade = false;
112 
113  switch (index) {
114  case PAGE_ENVIRONMENT:
115  mCentralWidget = new PageEnvironment(this);
116  break;
117  case PAGE_AUDIO:
118  mCentralWidget = new PageAudio(this);
119  break;
120  default:
121  LogW("Message page %d not implemented.", index);
122  return;
123  }
124 
125  assert(mCentralWidget);
126 
127  mMainLayout->addWidget(mCentralWidget->getWidget(), 1, 1);
128 
129  mCentralWidget->getWidget()->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
130 
131  mTitle->setText(mPageButtons->checkedButton()->text());
132 
134 }
135 
137  assert(mCentralWidget);
138  if (mChangesMade) {
140  mChangesMade = false;
142 
143  }
144 }
145 
147  mChangesMade = true;
148 }
149 
150 bool OptionsDialog::checkForChanges(bool allowCancel) {
151  if (mChangesMade) {
152  QMessageBox msgBox(this);
153  msgBox.setWindowTitle(this->windowTitle());
154  msgBox.setIcon(QMessageBox::Question);
155  msgBox.setText("There are unsaved changes.");
156  msgBox.setInformativeText("Do you want to save your changes?");
157  if (allowCancel) {
158  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
159  } else {
160  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
161  }
162 
163  switch (msgBox.exec()) {
164  case QMessageBox::Yes:
165  onApply();
166  break;
167  case QMessageBox::Cancel:
168  return false;
169  default:
170  break;
171  }
172  mChangesMade = false;
173  }
174 
175  return true;
176 }
177 
179  if (mCentralWidget) {
180  mMainLayout->removeWidget(mCentralWidget->getWidget());
181  delete mCentralWidget;
182  mCentralWidget = nullptr;
183  }
184 }
185 
186 void OptionsDialog::addPageButton(QIcon icon, QString text, QButtonGroup *group, QToolBar *tb, OptionPages page) {
187  QToolButton *button = new QToolButton(tb);
188  button->setIcon(icon);
189  button->setText(text);
190  button->setCheckable(true);
191  button->setChecked(false);
192  button->setStyleSheet("text-align: left");
193  button->setToolButtonStyle(DisplaySizeDefines::getSingleton()->optionsDialogToolButtonStyle());
194  button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
195  button->setIconSize(tb->iconSize());
196  group->addButton(button, page);
197  tb->addWidget(button);
198 }
199 
200 } // options
201 
bool checkForChanges(bool allowCancel)
MainWindow * mMainWindow
Definition: optionsdialog.h:80
#define LogW(...)
Definition: log.h:56
#define SHOW_DIALOG(d)
Definition: qtconfig.h:41
void accept() overridefinal
void reject() overridefinal
static void send(Args &&...args)
short function for creating and sending a message
QButtonGroup * mPageButtons
Definition: optionsdialog.h:85
int getLastVisitedOptionsPage() const
Getter function for mLastVisitedOptionsPage.
void setLastVisitedOptionsPage(int id)
Setter function for mLastVisitedOptionsPage.
QGridLayout * mMainLayout
Definition: optionsdialog.h:82
CentralWidgetInterface * mCentralWidget
Definition: optionsdialog.h:83
OptionsDialog(MainWindow *mainWindow)
settings in the system options changed
Definition: message.h:56
void onCurrentSelectionChanged(int)
void addPageButton(QIcon icon, QString text, QButtonGroup *group, QToolBar *tb, OptionPages page)
static const std::unique_ptr< DisplaySizeDefines > & getSingleton()
Definition: displaysize.cpp:29
The main window.
Definition: mainwindow.h:49
static SettingsForQt & getSingleton()
Getter function for the singleton.