Engauge Digitizer  2
DlgSettingsAbstractBase.cpp
Go to the documentation of this file.
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 "CmdMediator.h"
9 #include "EngaugeAssert.h"
10 #include "Logger.h"
11 #include "MainWindow.h"
12 #include <QColor>
13 #include <QComboBox>
14 #include <QPushButton>
15 #include <QScrollArea>
16 #include <QSettings>
17 #include <QSpacerItem>
18 #include <QVBoxLayout>
19 #include "Settings.h"
20 
21 int DlgSettingsAbstractBase::MINIMUM_DIALOG_WIDTH = 380; // May be overridden by subclass
23 
25  const QString &dialogName,
26  MainWindow &mainWindow) :
27  QDialog (&mainWindow),
28  m_mainWindow (mainWindow),
29  m_cmdMediator (nullptr),
30  m_dialogName (dialogName),
31  m_disableOkAtStartup (true)
32 {
33  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::DlgSettingsAbstractBase"
34  << " name=" << m_dialogName.toLatin1().data();
35 
36  setWindowTitle (title);
37  setModal (true);
38 }
39 
41 {
42  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::~DlgSettingsAbstractBase"
43  << " name=" << m_dialogName.toLatin1().data();
44 }
45 
47 {
48  ENGAUGE_CHECK_PTR (m_cmdMediator);
49 
50  return *m_cmdMediator;
51 }
52 
54 {
55  m_btnOk->setEnabled (enable);
56 }
57 
58 void DlgSettingsAbstractBase::finishPanel (QWidget *subPanel,
59  int minimumWidth,
60  int minimumHeightOrZero)
61 {
62  const int STRETCH_OFF = 0, STRETCH_ON = 1;
63 
64  m_scroll = new QScrollArea (this);
65  m_scroll->setStyleSheet ("QScrollArea { border: 0; margin: 0; padding: 0;}"); // Need QScrollArea or interior frames are affected
66  m_scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
67  m_scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAsNeeded);
68  m_scroll->setSizePolicy (QSizePolicy::Minimum,
69  QSizePolicy::Minimum);
70  m_scroll->setMinimumWidth (minimumWidth);
71 
72  QWidget *viewport = new QWidget (this);
73  m_scroll->setWidget (viewport);
74  m_scroll->setWidgetResizable (true);
75 
76  QHBoxLayout *scrollLayout = new QHBoxLayout (this);
77  scrollLayout->addWidget (m_scroll);
78  setLayout (scrollLayout);
79 
80  QVBoxLayout *panelLayout = new QVBoxLayout (viewport);
81  viewport->setLayout (panelLayout);
82 
83  panelLayout->addWidget (subPanel);
84  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_ON);
85 
86  QWidget *panelButtons = new QWidget (this);
87  QHBoxLayout *buttonLayout = new QHBoxLayout (panelButtons);
88 
89  createOptionalSaveDefault(buttonLayout);
90 
91  QHBoxLayout *layoutRightSide = new QHBoxLayout;
92 
93  QWidget *widgetRightSide = new QWidget;
94  widgetRightSide->setLayout (layoutRightSide);
95  buttonLayout->addWidget (widgetRightSide);
96 
97  QSpacerItem *spacerExpanding = new QSpacerItem (40, 5, QSizePolicy::Expanding, QSizePolicy::Expanding);
98  layoutRightSide->addItem (spacerExpanding);
99 
100  m_btnOk = new QPushButton (tr ("Ok"));
101  m_btnOk->setEnabled (false); // Nothing to save initially
102  layoutRightSide->addWidget (m_btnOk, 0, Qt::AlignRight);
103  connect (m_btnOk, SIGNAL (released ()), this, SLOT (slotOk ()));
104 
105  QSpacerItem *spacerFixed = new QSpacerItem (40, 5, QSizePolicy::Fixed, QSizePolicy::Fixed);
106  layoutRightSide->addItem (spacerFixed);
107 
108  m_btnCancel = new QPushButton (tr ("Cancel"));
109  layoutRightSide->addWidget (m_btnCancel, 0, Qt::AlignRight);
110  connect (m_btnCancel, SIGNAL (released ()), this, SLOT (slotCancel ()));
111 
112  panelLayout->addWidget (panelButtons, STRETCH_ON);
113  panelLayout->setStretch (panelLayout->count () - 1, STRETCH_OFF);
114 
115  setSizePolicy (QSizePolicy::Minimum,
116  QSizePolicy::Minimum);
117 
118  if (minimumHeightOrZero > 0) {
119  m_scroll->setMinimumHeight (minimumHeightOrZero);
120  }
121 }
122 
124 {
125  return m_mainWindow;
126 }
127 
129 {
130  return m_mainWindow;
131 }
132 
134 {
135  combo.addItem (colorPaletteToString (COLOR_PALETTE_BLUE),
136  QVariant (COLOR_PALETTE_BLUE));
137  combo.addItem (colorPaletteToString (COLOR_PALETTE_BLACK),
138  QVariant (COLOR_PALETTE_BLACK));
139  combo.addItem (colorPaletteToString (COLOR_PALETTE_CYAN),
140  QVariant (COLOR_PALETTE_CYAN));
141  combo.addItem (colorPaletteToString (COLOR_PALETTE_GOLD),
142  QVariant (COLOR_PALETTE_GOLD));
143  combo.addItem (colorPaletteToString (COLOR_PALETTE_GREEN),
144  QVariant (COLOR_PALETTE_GREEN));
146  QVariant (COLOR_PALETTE_MAGENTA));
147  combo.addItem (colorPaletteToString (COLOR_PALETTE_RED),
148  QVariant (COLOR_PALETTE_RED));
150  QVariant (COLOR_PALETTE_YELLOW));
151 }
152 
154 {
156  combo.addItem ("Transparent", QVariant (COLOR_PALETTE_TRANSPARENT));
157 }
158 
159 void DlgSettingsAbstractBase::saveGeometryToSettings()
160 {
161  // Store the settings for use by showEvent
162  QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
163  settings.setValue (m_dialogName, saveGeometry ());
164 }
165 
167 {
168  m_cmdMediator = &cmdMediator;
169 }
170 
172 {
173  m_disableOkAtStartup = disableOkAtStartup;
174 }
175 
176 void DlgSettingsAbstractBase::hideEvent (QHideEvent * /* event */)
177 {
178  saveGeometryToSettings();
179 }
180 
181 void DlgSettingsAbstractBase::showEvent (QShowEvent * /* event */)
182 {
183  if (m_disableOkAtStartup) {
184  m_btnOk->setEnabled (false);
185  }
186 
187  QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
188  if (settings.contains (m_dialogName)) {
189 
190  // Restore the settings that were stored by the last call to saveGeometryToSettings
191  restoreGeometry (settings.value (m_dialogName).toByteArray ());
192  }
193 }
194 
195 void DlgSettingsAbstractBase::slotCancel ()
196 {
197  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotCancel";
198 
199  hide();
200 }
201 
202 void DlgSettingsAbstractBase::slotOk ()
203 {
204  LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotOk";
205 
206  // Forward to leaf class
207  handleOk ();
208 }
static int MINIMUM_DIALOG_WIDTH
Dialog layout constant that guarantees every widget has sufficient room. Can be increased by finishPa...
void setDisableOkAtStartup(bool disableOkAtStartup)
Override the default Ok button behavior applied in showEvent.
DlgSettingsAbstractBase(const QString &title, const QString &dialogName, MainWindow &mainWindow)
Single constructor.
QString colorPaletteToString(ColorPalette colorPalette)
Definition: ColorPalette.cpp:9
void setCmdMediator(CmdMediator &cmdMediator)
Store CmdMediator for easy access by the leaf class.
const QString SETTINGS_DIGITIZER
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
virtual void createOptionalSaveDefault(QHBoxLayout *layout)=0
Let subclass define an optional Save As Default button.
void finishPanel(QWidget *subPanel, int minimumWidth=MINIMUM_DIALOG_WIDTH, int minimumHeightOrZero=0)
Add Ok and Cancel buttons to subpanel to get the whole dialog.
#define ENGAUGE_CHECK_PTR(ptr)
#endif
Definition: EngaugeAssert.h:27
void populateColorComboWithoutTransparent(QComboBox &combo)
Add colors in color palette to combobox, without transparent entry at end.
const QString SETTINGS_ENGAUGE
log4cpp::Category * mainCat
Definition: Logger.cpp:14
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:23
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:91
CmdMediator & cmdMediator()
Provide access to Document information wrapped inside CmdMediator.