Engauge Digitizer  2
DlgSettingsMainWindow.cpp
1 #include "CmdSettingsMainWindow.h"
2 #include "DlgSettingsMainWindow.h"
3 #include "EngaugeAssert.h"
4 #include "Logger.h"
5 #include "MainWindow.h"
6 #include "MainWindowModel.h"
7 #include <QComboBox>
8 #include <QGraphicsScene>
9 #include <QGridLayout>
10 #include <QGroupBox>
11 #include <QLabel>
12 #include <qmath.h>
13 #include <QPushButton>
14 #include <QSettings>
15 #include <QSpinBox>
16 #include "Settings.h"
17 #include "ZoomControl.h"
18 #include "ZoomFactorInitial.h"
19 #include "ZoomLabels.h"
20 
22  DlgSettingsAbstractBase ("MainWindow",
23  "DlgSettingsMainWindow",
24  mainWindow),
25  m_modelMainWindowBefore (0),
26  m_modelMainWindowAfter (0)
27 {
28  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::DlgSettingsMainWindow";
29 
30  QWidget *subPanel = createSubPanel ();
31  finishPanel (subPanel);
32 }
33 
34 DlgSettingsMainWindow::~DlgSettingsMainWindow()
35 {
36  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::~DlgSettingsMainWindow";
37 }
38 
39 void DlgSettingsMainWindow::createControls (QGridLayout *layout,
40  int &row)
41 {
42  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createControls";
43 
44  const int COLUMN0 = 0;
45 
46  QLabel *labelZoomFactor = new QLabel ("Initial zoom:");
47  layout->addWidget (labelZoomFactor, row, 1);
48 
49  m_cmbZoomFactor = new QComboBox;
50  m_cmbZoomFactor->addItem (LABEL_ZOOM_16_TO_1 , QVariant (ZOOM_INITIAL_16_TO_1));
51  m_cmbZoomFactor->addItem (LABEL_ZOOM_8_TO_1 , QVariant (ZOOM_INITIAL_8_TO_1));
52  m_cmbZoomFactor->addItem (LABEL_ZOOM_4_TO_1 , QVariant (ZOOM_INITIAL_4_TO_1));
53  m_cmbZoomFactor->addItem (LABEL_ZOOM_2_TO_1 , QVariant (ZOOM_INITIAL_2_TO_1));
54  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_1 , QVariant (ZOOM_INITIAL_1_TO_1));
55  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_2 , QVariant (ZOOM_INITIAL_1_TO_2));
56  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_4 , QVariant (ZOOM_INITIAL_1_TO_4));
57  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_8 , QVariant (ZOOM_INITIAL_1_TO_8));
58  m_cmbZoomFactor->addItem (LABEL_ZOOM_1_TO_16 , QVariant (ZOOM_INITIAL_1_TO_16));
59  m_cmbZoomFactor->addItem (LABEL_ZOOM_FILL , QVariant (ZOOM_INITIAL_FILL));
60  m_cmbZoomFactor->addItem (LABEL_ZOOM_PREVIOUS , QVariant (ZOOM_INITIAL_PREVIOUS));
61  m_cmbZoomFactor->setWhatsThis(tr ("Initial Zoom\n\n"
62  "Select the initial zoom factor when a new document is loaded. Either the previous "
63  "zoom can be kept, or the specified zoom can be applied."));
64  connect (m_cmbZoomFactor, SIGNAL (currentTextChanged (const QString)), this, SLOT (slotZoomFactor(const QString)));
65  layout->addWidget (m_cmbZoomFactor, row++, 2);
66 
67  QLabel *labelZoomControl = new QLabel ("Zoom control:");
68  layout->addWidget (labelZoomControl, row, 1);
69 
70  m_cmbZoomControl = new QComboBox;
71  m_cmbZoomControl->addItem ("Menu only" , QVariant (ZOOM_CONTROL_MENU_ONLY));
72  m_cmbZoomControl->addItem ("Menu and mouse wheel" , QVariant (ZOOM_CONTROL_MENU_WHEEL));
73  m_cmbZoomControl->addItem ("Menu and +/- keys" , QVariant (ZOOM_CONTROL_MENU_PLUSMINUS));
74  m_cmbZoomControl->addItem ("Menu, mouse wheel and +/- keys", QVariant (ZOOM_CONTROL_MENU_WHEEL_PLUSMINUS));
75  m_cmbZoomControl->setWhatsThis (tr ("Zoom Control\n\n"
76  "Select which inputs are used to zoom in and out."));
77  connect (m_cmbZoomControl, SIGNAL (currentTextChanged (const QString)), this, SLOT (slotZoomControl(const QString)));
78  layout->addWidget (m_cmbZoomControl, row++, 2);
79 
80  QLabel *labelLocale = new QLabel ("Locale:");
81  layout->addWidget (labelLocale, row, 1);
82 
83  // Initialization of combobox is liberated from Qt Calendar example
84  m_cmbLocale = new QComboBox;
85  m_cmbLocale->setWhatsThis(tr ("Locale\n\n"
86  "Select the locale that will be used when converting between numbers and strings. "
87  "This affects the use of commas or periods as group delimiters in each number entered "
88  "by the user, displayed in the user interface, or exported to a file."));
89  for (int indexLang = QLocale::C; indexLang <= QLocale::LastLanguage; indexLang++) {
90  QLocale::Language lang = static_cast<QLocale::Language> (indexLang);
91  QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
92  for (int indexCountry = 0; indexCountry < countries.count(); indexCountry++) {
93  QLocale::Country country = countries.at(indexCountry);
94  QString label = localeLabel (lang,
95  country);
96  QLocale locale (lang, country);
97  m_cmbLocale->addItem (label, locale);
98  }
99  }
100  m_cmbLocale->model()->sort(COLUMN0); // Sort the new entries
101  connect (m_cmbLocale, SIGNAL (currentIndexChanged (int)), this, SLOT (slotLocale (int)));
102  layout->addWidget (m_cmbLocale, row++, 2);
103 
104 }
105 
106 void DlgSettingsMainWindow::createOptionalSaveDefault (QHBoxLayout * /* layout */)
107 {
108  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createOptionalSaveDefault";
109 }
110 
112 {
113  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::createSubPanel";
114 
115  QWidget *subPanel = new QWidget ();
116  QGridLayout *layout = new QGridLayout (subPanel);
117  subPanel->setLayout (layout);
118 
119  layout->setColumnStretch(0, 1); // Empty first column
120  layout->setColumnStretch(1, 0); // Labels
121  layout->setColumnStretch(2, 0); // Values
122  layout->setColumnStretch(3, 1); // Empty first column
123 
124  int row = 0;
125  createControls (layout, row);
126 
127  return subPanel;
128 }
129 
131 {
132  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::handleOk";
133 
135  cmdMediator ().document(),
136  *m_modelMainWindowBefore,
137  *m_modelMainWindowAfter);
138  cmdMediator ().push (cmd);
139 
140  hide ();
141 }
142 void DlgSettingsMainWindow::load (CmdMediator & /* cmdMediator */)
143 {
144  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::load";
145 
146  ENGAUGE_ASSERT (false);
147 }
148 
150  const MainWindowModel &modelMainWindow)
151 {
152  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::loadMainWindowModel";
153 
154  setCmdMediator (cmdMediator);
155 
156  // Flush old data
157  if (m_modelMainWindowBefore != 0) {
158  delete m_modelMainWindowBefore;
159  }
160  if (m_modelMainWindowAfter != 0) {
161  delete m_modelMainWindowAfter;
162  }
163 
164  // Save new data
165  m_modelMainWindowBefore = new MainWindowModel (modelMainWindow);
166  m_modelMainWindowAfter = new MainWindowModel (modelMainWindow);
167 
168  // Populate controls
169  int index = m_cmbZoomFactor->findData (m_modelMainWindowAfter->zoomFactorInitial());
170  m_cmbZoomFactor->setCurrentIndex (index);
171  index = m_cmbZoomControl->findData (m_modelMainWindowAfter->zoomControl());
172  m_cmbZoomControl->setCurrentIndex (index);
173  QString locLabel = localeLabel (m_modelMainWindowAfter->locale().language(),
174  m_modelMainWindowBefore->locale().country());
175  index = m_cmbLocale->findText (locLabel);
176  m_cmbLocale->setCurrentIndex(index);
177 
178  updateControls ();
179  enableOk (false); // Disable Ok button since there not yet any changes
180 }
181 
182 QString DlgSettingsMainWindow::localeLabel (QLocale::Language lang,
183  QLocale::Country country) const
184 {
185  return QString ("%1/%2")
186  .arg (QLocale::languageToString (lang))
187  .arg (QLocale::countryToString(country));
188 }
189 
190 void DlgSettingsMainWindow::slotLocale (int index)
191 {
192  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotLocale";
193 
194  m_modelMainWindowAfter->setLocale (m_cmbLocale->itemData (index).toLocale());
195  updateControls();
196 }
197 
198 void DlgSettingsMainWindow::slotZoomControl(const QString)
199 {
200  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWindow::slotZoomControl";
201 
202  m_modelMainWindowAfter->setZoomControl ((ZoomControl) m_cmbZoomControl->currentData().toInt());
203  updateControls();
204 }
205 
206 void DlgSettingsMainWindow::slotZoomFactor(const QString)
207 {
208  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsMainWIndow::slotZoomFactor";
209 
210  m_modelMainWindowAfter->setZoomFactorInitial((ZoomFactorInitial) m_cmbZoomFactor->currentData().toInt());
211  updateControls();
212 }
213 
214 void DlgSettingsMainWindow::updateControls ()
215 {
216  enableOk (true);
217 }
virtual void createOptionalSaveDefault(QHBoxLayout *layout)
Let subclass define an optional Save As Default button.
virtual void handleOk()
Process slotOk.
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 and CmdSettingsMainWindow.
Command 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.
Command queue stack.
Definition: CmdMediator.h:16
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:66
CmdMediator & cmdMediator()
Provide access to Document information wrapped inside CmdMediator.