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"
8 #include "DlgSettingsAbstractBase.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
22 int DlgSettingsAbstractBase::MINIMUM_PREVIEW_HEIGHT = 100;
23
DlgSettingsAbstractBase(const QString & title,const QString & dialogName,MainWindow & mainWindow)24 DlgSettingsAbstractBase::DlgSettingsAbstractBase(const QString &title,
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
~DlgSettingsAbstractBase()40 DlgSettingsAbstractBase::~DlgSettingsAbstractBase()
41 {
42 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::~DlgSettingsAbstractBase"
43 << " name=" << m_dialogName.toLatin1().data();
44 }
45
cmdMediator()46 CmdMediator &DlgSettingsAbstractBase::cmdMediator ()
47 {
48 ENGAUGE_CHECK_PTR (m_cmdMediator);
49
50 return *m_cmdMediator;
51 }
52
enableOk(bool enable)53 void DlgSettingsAbstractBase::enableOk (bool enable)
54 {
55 m_btnOk->setEnabled (enable);
56 }
57
finishPanel(QWidget * subPanel,int minimumWidth,int minimumHeightOrZero)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
mainWindow()123 MainWindow &DlgSettingsAbstractBase::mainWindow ()
124 {
125 return m_mainWindow;
126 }
127
mainWindow() const128 const MainWindow &DlgSettingsAbstractBase::mainWindow () const
129 {
130 return m_mainWindow;
131 }
132
populateColorComboWithoutTransparent(QComboBox & combo)133 void DlgSettingsAbstractBase::populateColorComboWithoutTransparent (QComboBox &combo)
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));
145 combo.addItem (colorPaletteToString (COLOR_PALETTE_MAGENTA),
146 QVariant (COLOR_PALETTE_MAGENTA));
147 combo.addItem (colorPaletteToString (COLOR_PALETTE_RED),
148 QVariant (COLOR_PALETTE_RED));
149 combo.addItem (colorPaletteToString (COLOR_PALETTE_YELLOW),
150 QVariant (COLOR_PALETTE_YELLOW));
151 }
152
populateColorComboWithTransparent(QComboBox & combo)153 void DlgSettingsAbstractBase::populateColorComboWithTransparent (QComboBox &combo)
154 {
155 populateColorComboWithoutTransparent (combo);
156 combo.addItem ("Transparent", QVariant (COLOR_PALETTE_TRANSPARENT));
157 }
158
saveGeometryToSettings()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
setCmdMediator(CmdMediator & cmdMediator)166 void DlgSettingsAbstractBase::setCmdMediator (CmdMediator &cmdMediator)
167 {
168 m_cmdMediator = &cmdMediator;
169 }
170
setDisableOkAtStartup(bool disableOkAtStartup)171 void DlgSettingsAbstractBase::setDisableOkAtStartup(bool disableOkAtStartup)
172 {
173 m_disableOkAtStartup = disableOkAtStartup;
174 }
175
hideEvent(QHideEvent *)176 void DlgSettingsAbstractBase::hideEvent (QHideEvent * /* event */)
177 {
178 saveGeometryToSettings();
179 }
180
showEvent(QShowEvent *)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
slotCancel()195 void DlgSettingsAbstractBase::slotCancel ()
196 {
197 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotCancel";
198
199 hide();
200 }
201
slotOk()202 void DlgSettingsAbstractBase::slotOk ()
203 {
204 LOG4CPP_INFO_S ((*mainCat)) << "DlgSettingsAbstractBase::slotOk";
205
206 // Forward to leaf class
207 handleOk ();
208 }
209