Engauge Digitizer  2
StatusBar.cpp
1 #include "EngaugeAssert.h"
2 #include "Logger.h"
3 #include <QFrame>
4 #include <QHBoxLayout>
5 #include <QLineEdit>
6 #include <QStatusBar>
7 #include <QTextEdit>
8 #include <QTimer>
9 #include <QWhatsThis>
10 #include "StatusBar.h"
11 #include "ZoomFactor.h"
12 #include "ZoomLabels.h"
13 
14 const QString LABEL_COORDS_SCREEN ("Coordinates (pixels):");
15 const QString LABEL_COORDS_GRAPH ("Coordinates (graph):");
16 const QString LABEL_RESOLUTION_GRAPH ("Resolution (graph):");
17 
18 const int TEMPORARY_MESSAGE_LIFETIME = 5000; // Milliseconds. Two seconds is too fast even when the text is anticipated
19 
20 const int MIN_WIDTH_COMBO_UNITS = 160;
21 const int MAX_WIDTH_GROUP_UNITS = 400;
22 const int MAX_SIZE_EDIT_COORDS = 550; // Need lots of space in case date/time and degrees/minutes/seconds are used simultaneously
23 const int MAX_HEIGHT_EDIT_COORDS = 24;
24 
25 StatusBar::StatusBar(QStatusBar &statusBar) :
26  m_statusBar (statusBar),
27  m_statusBarMode (STATUS_BAR_MODE_ALWAYS),
28  m_timer (0)
29 {
30  createZoom ();
31  createGroupUnits ();
32 
33  connect (&m_statusBar, SIGNAL (messageChanged (const QString &)), this, SLOT (slotStatusBarChanged (const QString &)));
34 
35  m_statusBar.setMaximumHeight (60);
36  m_statusBar.hide();
37 }
38 
39 StatusBar::~StatusBar ()
40 {
41  if (m_timer != 0) {
42  delete m_timer;
43  m_timer = 0;
44  }
45 }
46 
47 void StatusBar::createGroupUnits ()
48 {
49  m_cmbUnits = new QComboBox;
50  m_cmbUnits->setEnabled (false); // Disabled until file is opened
51  m_cmbUnits->addItem (LABEL_COORDS_SCREEN, QVariant (STATUS_BAR_UNITS_COORDS_SCREEN));
52  m_cmbUnits->addItem (LABEL_COORDS_GRAPH, QVariant (STATUS_BAR_UNITS_COORDS_GRAPH));
53  m_cmbUnits->addItem (LABEL_RESOLUTION_GRAPH, QVariant (STATUS_BAR_UNITS_RESOLUTION_GRAPH));
54  m_cmbUnits->setCurrentText (LABEL_COORDS_GRAPH);
55  m_cmbUnits->setMaximumWidth (MIN_WIDTH_COMBO_UNITS);
56  m_cmbUnits->setToolTip (tr ("Select cursor coordinate values to display."));
57  m_cmbUnits->setWhatsThis (tr("Select Cursor Coordinate Values\n\n"
58  "Values at cursor coordinates to display. Coordinates are in screen (pixels) or "
59  "graph units. Resolution (which is the number of graph units per pixel) is "
60  "in graph units. Graph units are only available after axis points have been defined."));
61  connect (m_cmbUnits, SIGNAL (activated(const QString &)), this, SLOT (slotComboUnits (const QString &))); // activated() ignores code changes
62 
63  m_editCoords = new QTextEdit;
64  m_editCoords->setEnabled (false); // Disabled until file is opened
65  m_editCoords->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66  m_editCoords->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67  m_editCoords->setMinimumSize (MAX_SIZE_EDIT_COORDS, MAX_HEIGHT_EDIT_COORDS);
68  m_editCoords->setMaximumSize (MAX_SIZE_EDIT_COORDS, MAX_HEIGHT_EDIT_COORDS);
69  m_editCoords->setReadOnly(true);
70  m_editCoords->setToolTip (tr ("Cursor coordinate values."));
71  m_editCoords->setWhatsThis (tr ("Cursor Coordinate Values\n\n"
72  "Values at cursor coordinates. Coordinates are in screen (pixels) or "
73  "graph units. Resolution (which is the number of graph units per pixel) is "
74  "in graph units. Graph units are only available after axis points have been defined."));
75 
76  m_groupUnits = new QFrame;
77  m_groupUnits->setFrameStyle (QFrame::Box);
78  QPalette *palette = new QPalette;
79  palette->setColor (QPalette::Foreground, Qt::gray);
80  m_groupUnits->setPalette (*palette);
81  m_groupUnits->setMaximumWidth (MAX_WIDTH_GROUP_UNITS);
82 
83  QHBoxLayout *groupLayout = new QHBoxLayout;
84  m_groupUnits->setLayout (groupLayout);
85  groupLayout->setContentsMargins (0, 0, 0, 0);
86  groupLayout->addWidget (m_cmbUnits);
87  groupLayout->addWidget (m_editCoords);
88  groupLayout->setMargin (2);
89 
90  m_statusBar.addPermanentWidget (m_groupUnits);
91 }
92 
93 void StatusBar::createZoom ()
94 {
95  m_cmbZoom = new QComboBox ();
96  m_cmbZoom->setEnabled (false); // Disabled until file is opened
97  m_cmbZoom->addItem (LABEL_ZOOM_16_TO_1);
98  m_cmbZoom->addItem (LABEL_ZOOM_8_TO_1);
99  m_cmbZoom->addItem (LABEL_ZOOM_4_TO_1);
100  m_cmbZoom->addItem (LABEL_ZOOM_2_TO_1);
101  m_cmbZoom->addItem (LABEL_ZOOM_1_TO_1);
102  m_cmbZoom->addItem (LABEL_ZOOM_1_TO_2);
103  m_cmbZoom->addItem (LABEL_ZOOM_1_TO_4);
104  m_cmbZoom->addItem (LABEL_ZOOM_1_TO_8);
105  m_cmbZoom->addItem (LABEL_ZOOM_1_TO_16);
106  m_cmbZoom->addItem (LABEL_ZOOM_FILL);
107  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_1);
108  m_cmbZoom->setMaximumWidth (80);
109  m_cmbZoom->setToolTip (tr ("Select zoom."));
110  m_cmbZoom->setWhatsThis (tr("Select Zoom\n\n"
111  "Points can be more accurately placed by zooming in."));
112  // Zoom combobox must use currentTextChanged rather than activated or else fill-zoom-at-startup never takes effect
113  connect (m_cmbZoom, SIGNAL (currentTextChanged(const QString &)), this, SLOT (slotComboZoom (const QString &)));
114 
115  m_statusBar.addPermanentWidget (m_cmbZoom);
116 }
117 
118 void StatusBar::setCoordinates (const QString &coordsScreen,
119  const QString &coordsGraph,
120  const QString &resolutionGraph)
121 {
122 // LOG4CPP_DEBUG_S ((*mainCat)) << "StatusBar::setCoordinates"
123 // << " screen=" << coordsScreen.toLatin1 ().data ()
124 // << " graph=" << coordsGraph.toLatin1 ().data ()
125 // << " resolution=" << resolutionGraph.toLatin1 ().data ();
126 
127  if (m_cmbUnits->isEnabled ()) {
128 
129  m_coordsScreen = coordsScreen;
130  m_coordsGraph = coordsGraph;
131  m_resolutionGraph = resolutionGraph;
132 
133  updateCoordsText();
134  }
135 }
136 
137 void StatusBar::setStatusBarMode(StatusBarMode statusBarMode)
138 {
139  m_statusBarMode = statusBarMode;
140  if (m_statusBarMode == STATUS_BAR_MODE_ALWAYS) {
141  m_statusBar.show();
142  } else {
143  m_statusBar.hide();
144  }
145 }
146 
147 void StatusBar::showTemporaryMessage(const QString &message)
148 {
149  LOG4CPP_DEBUG_S ((*mainCat)) << "StatusBar::showTemporaryMessage message=" << message.toLatin1 ().data ();
150 
151  if (m_statusBarMode != STATUS_BAR_MODE_NEVER) {
152  if (m_statusBarMode == STATUS_BAR_MODE_TEMPORARY) {
153  // Calling m_statusBar.show here will have no effect since this is called while processing a signal. Use a timer to
154  // show the status bar as soon as possible
155  m_timer = new QTimer;
156  connect (m_timer, SIGNAL (timeout ()), this, SLOT (slotTimeout()));
157  m_timer->setSingleShot(true);
158  m_timer->start (0);
159  }
160  m_statusBar.showMessage (message, TEMPORARY_MESSAGE_LIFETIME);
161  }
162 }
163 
164 void StatusBar::slotComboUnits (const QString &text)
165 {
166  LOG4CPP_DEBUG_S ((*mainCat)) << "StatusBar::slotComboUnits text=" << text.toLatin1 ().data ();
167 
168  updateCoordsText();
169 }
170 
171 void StatusBar::slotComboZoom (const QString &text)
172 {
173  LOG4CPP_DEBUG_S ((*mainCat)) << "StatusBar::slotComboZoom text=" << text.toLatin1 ().data ();
174 
175  if (text == LABEL_ZOOM_16_TO_1) {
176  emit signalZoom (ZOOM_16_TO_1);
177  } else if (text == LABEL_ZOOM_8_TO_1) {
178  emit signalZoom (ZOOM_8_TO_1);
179  } else if (text == LABEL_ZOOM_4_TO_1) {
180  emit signalZoom (ZOOM_4_TO_1);
181  } else if (text == LABEL_ZOOM_2_TO_1) {
182  emit signalZoom (ZOOM_2_TO_1);
183  } else if (text == LABEL_ZOOM_1_TO_1) {
184  emit signalZoom (ZOOM_1_TO_1);
185  } else if (text == LABEL_ZOOM_1_TO_2) {
186  emit signalZoom (ZOOM_1_TO_2);
187  } else if (text == LABEL_ZOOM_1_TO_4) {
188  emit signalZoom (ZOOM_1_TO_4);
189  } else if (text == LABEL_ZOOM_1_TO_8) {
190  emit signalZoom (ZOOM_1_TO_8);
191  } else if (text == LABEL_ZOOM_1_TO_16) {
192  emit signalZoom (ZOOM_1_TO_16);
193  } else if (text == LABEL_ZOOM_FILL) {
194  emit signalZoom (ZOOM_FILL);
195  } else {
196  ENGAUGE_ASSERT (false);
197  }
198 }
199 
200 void StatusBar::slotStatusBarChanged(const QString &message)
201 {
202  LOG4CPP_DEBUG_S ((*mainCat)) << "StatusBar::slotStatusBarChanged message=" << message.toLatin1 ().data ();
203 
204  if (m_statusBarMode == STATUS_BAR_MODE_TEMPORARY) {
205  m_statusBar.hide();
206  }
207 }
208 
209 void StatusBar::slotTimeout()
210 {
211  LOG4CPP_INFO_S ((*mainCat)) << "StatusBar::slotTimeout";
212 
213  delete m_timer;
214  m_timer = 0;
215 
216  m_statusBar.show();
217 }
218 
219 void StatusBar::slotZoom(int zoom)
220 {
221  LOG4CPP_INFO_S ((*mainCat)) << "StatusBar::slotZoom zoom=" << zoom;
222 
223  // Show string for the numeric zoom value
224  switch ((ZoomFactor) zoom) {
225  case ZOOM_16_TO_1:
226  m_cmbZoom->setCurrentText (LABEL_ZOOM_16_TO_1);
227  break;
228  case ZOOM_8_TO_1:
229  m_cmbZoom->setCurrentText (LABEL_ZOOM_8_TO_1);
230  break;
231  case ZOOM_4_TO_1:
232  m_cmbZoom->setCurrentText (LABEL_ZOOM_4_TO_1);
233  break;
234  case ZOOM_2_TO_1:
235  m_cmbZoom->setCurrentText (LABEL_ZOOM_2_TO_1);
236  break;
237  case ZOOM_1_TO_1:
238  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_1);
239  break;
240  case ZOOM_1_TO_2:
241  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_2);
242  break;
243  case ZOOM_1_TO_4:
244  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_4);
245  break;
246  case ZOOM_1_TO_8:
247  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_8);
248  break;
249  case ZOOM_1_TO_16:
250  m_cmbZoom->setCurrentText (LABEL_ZOOM_1_TO_16);
251  break;
252  case ZOOM_FILL:
253  m_cmbZoom->setCurrentText (LABEL_ZOOM_FILL);
254  }
255 }
256 
257 void StatusBar::updateCoordsText()
258 {
259  if (m_cmbUnits->currentText() == LABEL_COORDS_SCREEN) {
260  m_editCoords->setText (m_coordsScreen);
261  } else if (m_cmbUnits->currentText() == LABEL_COORDS_GRAPH) {
262  m_editCoords->setText (m_coordsGraph);
263  } else {
264  m_editCoords->setText (m_resolutionGraph);
265  }
266 }
267 
269 {
270  if (!m_cmbUnits->isEnabled ()) {
271 
272  // First file has just been read in, so enable the widgets
273  m_cmbZoom->setEnabled (true);
274  m_cmbUnits->setEnabled (true);
275  m_editCoords->setEnabled (true);
276  }
277 }
void setStatusBarMode(StatusBarMode statusBarMode)
Set the status bar visibility mode.
Definition: StatusBar.cpp:137
StatusBar(QStatusBar &statusBar)
Single constructor that accepts the previously-constructed standard QStatusBar.
Definition: StatusBar.cpp:25
void setCoordinates(const QString &coordsScreen, const QString &coordsGraph, const QString &resolutionGraph)
Populate the coordinates fields. Unavailable values are empty. Html-encoding to highlight with colors...
Definition: StatusBar.cpp:118
void slotZoom(int)
Receive zoom selection from MainWindow.
Definition: StatusBar.cpp:219
void wakeUp()
Enable all widgets in the status bar. This is called just after a Document becomes active...
Definition: StatusBar.cpp:268
void signalZoom(int)
Send zoom factor, that was just selected in the status bar, to MainWindow.
StatusBarMode statusBarMode() const
Current mode for status bar visibility. This is tracked locally so this class knows when to hide/show...
Definition: StatusBar.h:36
void showTemporaryMessage(const QString &message)
Show temporary message in status bar. After a short interval the message will disappear.
Definition: StatusBar.cpp:147