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