Engauge Digitizer  2
DlgSettingsAbstractBase.cpp
1 #include "CmdMediator.h"
2 #include "DlgSettingsAbstractBase.h"
3 #include "EngaugeAssert.h"
4 #include "Logger.h"
5 #include "MainWindow.h"
6 #include <QColor>
7 #include <QComboBox>
8 #include <QPushButton>
9 #include <QSettings>
10 #include <QSpacerItem>
11 #include <QVBoxLayout>
12 
15 
17  const QString &dialogName,
18  MainWindow &mainWindow) :
19  QDialog (&mainWindow),
20  m_mainWindow (mainWindow),
21  m_cmdMediator (0),
22  m_dialogName (dialogName)
23 {
24  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::DlgSettingsAbstractBase"
25  << " name=" << m_dialogName.toLatin1().data();
26 
27  setWindowTitle (title);
28  setModal (true);
29 }
30 
31 DlgSettingsAbstractBase::~DlgSettingsAbstractBase()
32 {
33  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::~DlgSettingsAbstractBase"
34  << " name=" << m_dialogName.toLatin1().data();
35 }
36 
38 {
39  ENGAUGE_CHECK_PTR (m_cmdMediator);
40 
41  return *m_cmdMediator;
42 }
43 
45 {
46  m_btnOk->setEnabled (enable);
47 }
48 
49 void DlgSettingsAbstractBase::finishPanel (QWidget *subPanel)
50 {
51  const int STRETCH_OFF = 0, STRETCH_ON = 1;
52 
53  QVBoxLayout *panelLayout = new QVBoxLayout (this);
54 
55  setMinimumWidth (MINIMUM_DIALOG_WIDTH);
56  setLayout (panelLayout);
57 
58  panelLayout->addWidget (subPanel);
59  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_ON);
60 
61  QWidget *panelButtons = new QWidget (this);
62  QHBoxLayout *buttonLayout = new QHBoxLayout (panelButtons);
63 
64  m_btnCancel = new QPushButton (tr ("Cancel"));
65  buttonLayout->addWidget (m_btnCancel);
66  connect (m_btnCancel, SIGNAL (released ()), this, SLOT (slotCancel ()));
67 
68  QSpacerItem *spacer = new QSpacerItem (40, 5, QSizePolicy::Minimum, QSizePolicy::Minimum);
69  buttonLayout->addItem (spacer);
70 
71  m_btnOk = new QPushButton (tr ("Ok"));
72  m_btnOk->setEnabled (false); // Nothing to save initially
73  buttonLayout->addWidget (m_btnOk);
74  connect (m_btnOk, SIGNAL (released ()), this, SLOT (slotOk ()));
75 
76  panelLayout->addWidget (panelButtons, STRETCH_ON, Qt::AlignRight);
77  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_OFF);
78 }
79 
81 {
82  return m_mainWindow;
83 }
84 
86 {
87  return m_mainWindow;
88 }
89 
91 {
92  combo.addItem (colorPaletteToString (COLOR_PALETTE_BLUE),
93  QVariant (COLOR_PALETTE_BLUE));
94  combo.addItem (colorPaletteToString (COLOR_PALETTE_BLACK),
95  QVariant (COLOR_PALETTE_BLACK));
96  combo.addItem (colorPaletteToString (COLOR_PALETTE_CYAN),
97  QVariant (COLOR_PALETTE_CYAN));
98  combo.addItem (colorPaletteToString (COLOR_PALETTE_GOLD),
99  QVariant (COLOR_PALETTE_GOLD));
100  combo.addItem (colorPaletteToString (COLOR_PALETTE_GREEN),
101  QVariant (COLOR_PALETTE_GREEN));
102  combo.addItem (colorPaletteToString (COLOR_PALETTE_MAGENTA),
103  QVariant (COLOR_PALETTE_MAGENTA));
104  combo.addItem (colorPaletteToString (COLOR_PALETTE_RED),
105  QVariant (COLOR_PALETTE_RED));
106  combo.addItem (colorPaletteToString (COLOR_PALETTE_YELLOW),
107  QVariant (COLOR_PALETTE_YELLOW));
108 }
109 
111 {
113  combo.addItem ("Transparent", QVariant (COLOR_PALETTE_TRANSPARENT));
114 }
115 
116 void DlgSettingsAbstractBase::saveGeometryToSettings()
117 {
118  // Store the settings for use by showEvent
119  QSettings settings;
120  settings.setValue (m_dialogName, saveGeometry ());
121 }
122 
124 {
125  m_cmdMediator = &cmdMediator;
126 }
127 
128 void DlgSettingsAbstractBase::showEvent (QShowEvent * /* event */)
129 {
130  m_btnOk->setEnabled (false);
131 
132  QSettings settings;
133  if (settings.contains (m_dialogName)) {
134 
135  // Restore the settings that were stored by the last call to saveGeometryToSettings
136  restoreGeometry (settings.value (m_dialogName).toByteArray ());
137  }
138 }
139 
140 void DlgSettingsAbstractBase::slotCancel ()
141 {
142  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotCancel";
143 
144  saveGeometryToSettings();
145  hide();
146 }
147 
148 void DlgSettingsAbstractBase::slotOk ()
149 {
150  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotOk";
151 
152  saveGeometryToSettings();
153 
154  // Forward to leaf class
155  handleOk ();
156 }
static int MINIMUM_DIALOG_WIDTH
Dialog layout constant that guarantees every widget has sufficient room.
DlgSettingsAbstractBase(const QString &title, const QString &dialogName, MainWindow &mainWindow)
Single constructor.
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
void populateColorComboWithoutTransparent(QComboBox &combo)
Add colors in color palette to combobox, without transparent entry at end.
void finishPanel(QWidget *subPanel)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
static int MINIMUM_PREVIEW_HEIGHT
Dialog layout constant that guarantees preview has sufficent room.
void enableOk(bool enable)
Let leaf subclass control the Ok button.
Command queue stack.
Definition: CmdMediator.h:16
virtual void handleOk()=0
Process slotOk.
void populateColorComboWithTransparent(QComboBox &combo)
Add colors in color palette to combobox, with transparent entry at end.
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:60
CmdMediator & cmdMediator()
Provide access to Document information wrapped inside CmdMediator.