Engauge Digitizer  2
DlgSettingsMainWindow.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "DlgSettingsMainWindow.h"
8 #include "EngaugeAssert.h"
9 #include "Logger.h"
10 #include "MainWindow.h"
11 #include "MainWindowModel.h"
12 #include <QCheckBox>
13 #include <QComboBox>
14 #include <QGraphicsScene>
15 #include <QGridLayout>
16 #include <QGroupBox>
17 #include <QLabel>
18 #include <qmath.h>
19 #include <QPushButton>
20 #include <QSettings>
21 #include <QSpinBox>
22 #include "QtToString.h"
23 #include "Settings.h"
24 #include "ZoomControl.h"
25 #include "ZoomFactorInitial.h"
26 #include "ZoomLabels.h"
27 
29  DlgSettingsAbstractBase (tr ("Main Window"),
30  "DlgSettingsMainWindow",
31  mainWindow),
32  m_modelMainWindowBefore (0),
33  m_modelMainWindowAfter (0)
34 {
35  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::DlgSettingsMainWindow";
36 
37  QWidget *subPanel = createSubPanel ();
38  finishPanel (subPanel);
39 }
40 
41 DlgSettingsMainWindow::~DlgSettingsMainWindow()
42 {
43  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::~DlgSettingsMainWindow";
44 }
45 
46 void DlgSettingsMainWindow::createControls (QGridLayout *layout,
47  int &row)
48 {
49  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createControls";
50 
51  const int COLUMN0 = 0;
52 
53  QLabel *labelZoomFactor = new QLabel (tr ("Initial zoom:"));
54  layout->addWidget (labelZoomFactor, row, 1);
55 
56  m_cmbZoomFactor = new QComboBox;
57  m_cmbZoomFactor->addItem (LABEL_ZOOM_16_TO_1 , QVariant (ZOOM_INITIAL_16_TO_1));
58  m_cmbZoomFactor->addItem (LABEL_ZOOM_8_TO_1 , QVariant (ZOOM_INITIAL_8_TO_1));
59  m_cmbZoomFactor->addItem (LABEL_ZOOM_4_TO_1 , QVariant (ZOOM_INITIAL_4_TO_1));
60  m_cmbZoomFactor->addItem (LABEL_ZOOM_2_TO_1 , QVariant (ZOOM_INITIAL_2_TO_1));
61  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_1 , QVariant (ZOOM_INITIAL_1_TO_1));
62  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_2 , QVariant (ZOOM_INITIAL_1_TO_2));
63  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_4 , QVariant (ZOOM_INITIAL_1_TO_4));
64  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_8 , QVariant (ZOOM_INITIAL_1_TO_8));
65  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_16 , QVariant (ZOOM_INITIAL_1_TO_16));
66  m_cmbZoomFactor->addItem (LABEL_ZOOM_FILL , QVariant (ZOOM_INITIAL_FILL));
67  m_cmbZoomFactor->addItem (LABEL_ZOOM_PREVIOUS , QVariant (ZOOM_INITIAL_PREVIOUS));
68  m_cmbZoomFactor->setWhatsThis(tr ("Initial Zoom\n\n"
69  "Select the initial zoom factor when a new document is loaded. Either the previous "
70  "zoom can be kept, or the specified zoom can be applied."));
71  connect (m_cmbZoomFactor, SIGNAL (currentTextChanged (const QString)), this, SLOT (slotZoomFactor(const QString)));
72  layout->addWidget (m_cmbZoomFactor, row++, 2);
73 
74  QLabel *labelZoomControl = new QLabel (tr ("Zoom control:"));
75  layout->addWidget (labelZoomControl, row, 1);
76 
77  m_cmbZoomControl = new QComboBox;
78  m_cmbZoomControl->addItem (tr ("Menu only" ), QVariant (ZOOM_CONTROL_MENU_ONLY));
79  m_cmbZoomControl->addItem (tr ("Menu and mouse wheel" ), QVariant (ZOOM_CONTROL_MENU_WHEEL));
80  m_cmbZoomControl->addItem (tr ("Menu and +/- keys" ), QVariant (ZOOM_CONTROL_MENU_PLUSMINUS));
81  m_cmbZoomControl->addItem (tr ("Menu, mouse wheel and +/- keys"), QVariant (ZOOM_CONTROL_MENU_WHEEL_PLUSMINUS));
82  m_cmbZoomControl->setWhatsThis (tr ("Zoom Control\n\n"
83  "Select which inputs are used to zoom in and out."));
84  connect (m_cmbZoomControl, SIGNAL (currentTextChanged (const QString)), this, SLOT (slotZoomControl(const QString)));
85  layout->addWidget (m_cmbZoomControl, row++, 2);
86 
87  QLabel *labelLocale = new QLabel (tr ("Locale:"));
88  layout->addWidget (labelLocale, row, 1);
89 
90  // Initialization of combobox is liberated from Qt Calendar example
91  m_cmbLocale = new QComboBox;
92  m_cmbLocale->setWhatsThis(tr ("Locale\n\n"
93  "Select the locale that will be used in numbers (immediately), and the language in the user "
94  "interface (after restart).\n\n"
95  "The locale determines how numbers are formatted. Specifically, either commas or "
96  "periods will be used as group delimiters in each number entered "
97  "by the user, displayed in the user interface, or exported to a file."));
98  for (int indexLang = QLocale::C; indexLang <= QLocale::LastLanguage; indexLang++) {
99  QLocale::Language lang = static_cast<QLocale::Language> (indexLang);
100  QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
101  for (int indexCountry = 0; indexCountry < countries.count(); indexCountry++) {
102  QLocale::Country country = countries.at(indexCountry);
103  QLocale locale (lang, country);
104  QString label = QLocaleToString (locale);
105  m_cmbLocale->addItem (label, locale);
106  }
107  }
108  m_cmbLocale->model()->sort(COLUMN0); // Sort the new entries
109  connect (m_cmbLocale, SIGNAL (currentIndexChanged (int)), this, SLOT (slotLocale (int)));
110  layout->addWidget (m_cmbLocale, row++, 2);
111 
112  QLabel *labelRecent = new QLabel (tr ("Recent file list:"));
113  layout->addWidget (labelRecent, row, 1);
114 
115  m_btnRecentClear = new QPushButton (tr ("Clear"));
116  m_btnRecentClear->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
117  m_btnRecentClear->setWhatsThis (tr ("Recent File List Clear\n\n"
118  "Clear the recent file list in the File menu."));
119  connect (m_btnRecentClear, SIGNAL (pressed ()), &mainWindow(), SLOT (slotRecentFileClear ()));
120  connect (m_btnRecentClear, SIGNAL (pressed ()), this, SLOT (slotRecentFileClear()));
121  layout->addWidget (m_btnRecentClear, row++, 2);
122 
123  QLabel *labelTitleBarFormat = new QLabel (tr ("Include title bar path:"));
124  layout->addWidget (labelTitleBarFormat, row, 1);
125 
126  m_chkTitleBarFormat = new QCheckBox;
127  m_chkTitleBarFormat->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
128  m_chkTitleBarFormat->setWhatsThis (tr ("Title Bar Filename\n\n"
129  "Includes or excludes the path and file extension from the filename in the title bar."));
130  connect (m_chkTitleBarFormat, SIGNAL (toggled (bool)), this, SLOT (slotTitleBarFormat(bool)));
131  layout->addWidget (m_chkTitleBarFormat, row++, 2);
132 }
133 
134 void DlgSettingsMainWindow::createOptionalSaveDefault (QHBoxLayout * /* layout */)
135 {
136  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createOptionalSaveDefault";
137 }
138 
140 {
141  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createSubPanel";
142 
143  QWidget *subPanel = new QWidget ();
144  QGridLayout *layout = new QGridLayout (subPanel);
145  subPanel->setLayout (layout);
146 
147  layout->setColumnStretch(0, 1); // Empty first column
148  layout->setColumnStretch(1, 0); // Labels
149  layout->setColumnStretch(2, 0); // Values
150  layout->setColumnStretch(3, 1); // Empty first column
151 
152  int row = 0;
153  createControls (layout, row);
154 
155  return subPanel;
156 }
157 
159 {
160  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::handleOk";
161 
162  mainWindow().updateSettingsMainWindow (*m_modelMainWindowAfter);
163 
164  hide ();
165 }
166 void DlgSettingsMainWindow::load (CmdMediator & /* cmdMediator */)
167 {
168  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::load";
169 
170  ENGAUGE_ASSERT (false);
171 }
172 
174  const MainWindowModel &modelMainWindow)
175 {
176  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::loadMainWindowModel";
177 
178  setCmdMediator (cmdMediator);
179 
180  // Flush old data
181  if (m_modelMainWindowBefore != 0) {
182  delete m_modelMainWindowBefore;
183  }
184  if (m_modelMainWindowAfter != 0) {
185  delete m_modelMainWindowAfter;
186  }
187 
188  // Save new data
189  m_modelMainWindowBefore = new MainWindowModel (modelMainWindow);
190  m_modelMainWindowAfter = new MainWindowModel (modelMainWindow);
191 
192  // Populate controls
193  int index = m_cmbZoomFactor->findData (m_modelMainWindowAfter->zoomFactorInitial());
194  m_cmbZoomFactor->setCurrentIndex (index);
195  index = m_cmbZoomControl->findData (m_modelMainWindowAfter->zoomControl());
196  m_cmbZoomControl->setCurrentIndex (index);
197  QString locLabel = QLocaleToString (m_modelMainWindowAfter->locale());
198  index = m_cmbLocale->findText (locLabel);
199  m_cmbLocale->setCurrentIndex(index);
200  m_chkTitleBarFormat->setChecked (m_modelMainWindowAfter->mainTitleBarFormat() == MAIN_TITLE_BAR_FORMAT_PATH);
201 
202  updateControls ();
203  enableOk (false); // Disable Ok button since there not yet any changes
204 }
205 
206 void DlgSettingsMainWindow::slotLocale (int index)
207 {
208  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotLocale";
209 
210  m_modelMainWindowAfter->setLocale (m_cmbLocale->itemData (index).toLocale());
211  updateControls();
212 }
213 
214 void DlgSettingsMainWindow::slotRecentFileClear()
215 {
216  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotRecentFileClear";
217 
218  // The signal that triggered the call to this method was also sent to MainWindow to clear the list there
219  updateControls();
220 }
221 
222 void DlgSettingsMainWindow::slotTitleBarFormat(bool)
223 {
224  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotTitleBarFormat";
225 
226  m_modelMainWindowAfter->setMainTitleBarFormat(m_chkTitleBarFormat->isChecked() ?
227  MAIN_TITLE_BAR_FORMAT_PATH :
228  MAIN_TITLE_BAR_FORMAT_NO_PATH);
229  updateControls();
230 }
231 
232 void DlgSettingsMainWindow::slotZoomControl(const QString)
233 {
234  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotZoomControl";
235 
236  m_modelMainWindowAfter->setZoomControl ((ZoomControl) m_cmbZoomControl->currentData().toInt());
237  updateControls();
238 }
239 
240 void DlgSettingsMainWindow::slotZoomFactor(const QString)
241 {
242  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWIndow::slotZoomFactor";
243 
244  m_modelMainWindowAfter->setZoomFactorInitial((ZoomFactorInitial) m_cmbZoomFactor->currentData().toInt());
245  updateControls();
246 }
247 
248 void DlgSettingsMainWindow::updateControls ()
249 {
250  enableOk (true);
251 }
virtual void createOptionalSaveDefault(QHBoxLayout *layout)
Let subclass define an optional Save As Default button.
void updateSettingsMainWindow(const MainWindowModel &modelMainWindow)
Update with new main window properties.
virtual void handleOk()
Process slotOk.
MainTitleBarFormat mainTitleBarFormat() const
Get method for MainWindow titlebar filename format.
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
void setLocale(QLocale::Language language, QLocale::Country country)
Set method for locale given attributes.
void loadMainWindowModel(CmdMediator &cmdMediator, const MainWindowModel &modelMainWindow)
Replaced load method since the main window settings are independent of document, unlike other DlgSett...
virtual void load(CmdMediator &cmdMediator)
Load settings from Document.
DlgSettingsMainWindow(MainWindow &mainWindow)
Single constructor.
ZoomControl zoomControl() const
Get method for zoom control.
Model for DlgSettingsMainWindow.
virtual QWidget * createSubPanel()
Create dialog-specific panel to which base class will add Ok and Cancel buttons.
void finishPanel(QWidget *subPanel)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
void enableOk(bool enable)
Let leaf subclass control the Ok button.
void setZoomControl(ZoomControl zoomControl)
Set method for zoom control.
void setMainTitleBarFormat(MainTitleBarFormat mainTitleBarFormat)
Set method for MainWindow titlebar filename format.
Command queue stack.
Definition: CmdMediator.h:23
void setZoomFactorInitial(ZoomFactorInitial zoomFactorInitial)
Set method for initial zoom factor.
Abstract base class for all Settings dialogs.
ZoomFactorInitial zoomFactorInitial() const
Get method for initial zoom factor.
QLocale locale() const
Get method for locale.
MainWindow & mainWindow()
Get method for MainWindow.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:82