Skip to content

Commit 96024ed

Browse files
If label already exists in given coordinates, it will be updated
1 parent 8e8625a commit 96024ed

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

trikControl/include/trikControl/display.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ public slots:
4040
/// supported formats, but .jpg, .png, .bmp, .gif are supported.
4141
void showImage(QString const &fileName);
4242

43-
/// Add a label to the specific position of the screen.
43+
/// Add a label to the specific position of the screen. If there already is a label in these coordinates, its
44+
/// contents will be updated.
4445
/// @param text - label text.
45-
/// @param x @param y - label coordinates.
46+
/// @param x - label x coordinate.
47+
/// @param y - label y coordinate.
4648
void addLabel(QString const &text, int x, int y);
4749

4850
/// Remove all labels from the screen.

trikControl/src/display.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void trikControl::Display::showImage(QString const &fileName)
5252
emit threadShowImage(fileName);
5353
}
5454

55-
void Display::addLabel(const QString &text, int x, int y)
55+
void Display::addLabel(QString const &text, int x, int y)
5656
{
5757
emit threadAddLabel(text, x, y);
5858
}

trikControl/src/guiWorker.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,27 @@ void GuiWorker::showImage(QString const &fileName)
5252
mImageWidget.show();
5353
}
5454

55-
void GuiWorker::addLabel(const QString &text, int x, int y)
55+
void GuiWorker::addLabel(QString const &text, int x, int y)
5656
{
57-
QLabel *label = new QLabel(&mImageWidget);
57+
QLabel *label = findLabel(x, y);
58+
label = label ? label : new QLabel(&mImageWidget);
5859
label->setText(text);
5960
label->setGeometry(x, y, label->width(), label->height());
6061
label->show();
61-
mLabels.append(label);
62+
if (!mLabels.contains(x ^ y, label)) {
63+
mLabels.insertMulti(x ^ y, label);
64+
}
65+
6266
mImageWidget.show();
6367
}
6468

6569
void GuiWorker::removeLabels()
6670
{
67-
foreach (QLabel *label, mLabels) {
71+
foreach (QLabel * const label, mLabels.values()) {
6872
label->close();
6973
delete label;
7074
}
75+
7176
mLabels.clear();
7277
}
7378

@@ -127,3 +132,13 @@ void GuiWorker::hide()
127132
mImageWidget.hide();
128133
}
129134

135+
QLabel *GuiWorker::findLabel(int x, int y) const
136+
{
137+
foreach (QLabel * const label, mLabels.values(x ^ y)) {
138+
if (label->x() == x && label->y() == y) {
139+
return label;
140+
}
141+
}
142+
143+
return NULL;
144+
}

trikControl/src/guiWorker.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#pragma once
1616

1717
#include <QtCore/qglobal.h>
18-
#include <QtCore/QHash>
18+
#include <QtCore/QMultiHash>
1919
#include <QtCore/QList>
2020
#include <QtGui/QPixmap>
2121

@@ -29,6 +29,7 @@
2929

3030
namespace trikControl {
3131

32+
/// Works in GUI thread and is responsible for all output to display.
3233
class GuiWorker : public QObject
3334
{
3435
Q_OBJECT
@@ -37,21 +38,38 @@ class GuiWorker : public QObject
3738
GuiWorker();
3839

3940
public slots:
41+
/// Shows image with given filename on display. Image is scaled to fill the screen and is cached on first read
42+
/// for better performance.
4043
void showImage(QString const &fileName);
44+
45+
/// Add a label to the specific position of the screen. If there already is a label in these coordinates, its
46+
/// contents will be updated.
47+
/// @param text - label text.
48+
/// @param x - label x coordinate.
49+
/// @param y - label y coordinate.
4150
void addLabel(QString const &text, int x, int y);
51+
52+
/// Remove all labels from the screen.
4253
void removeLabels();
54+
55+
/// Queues worker object for deletion. It is actually deleted when control flow returns to event loop.
4356
void deleteWorker();
57+
58+
/// Hides image widget.
4459
void hide();
4560

4661
/// Sets background for a picture.
4762
/// @param color - color of a background.
4863
void setBackground(QString const &color);
4964

5065
private:
66+
/// Returns existing label with given coordinates or NULL if no such label exists.
67+
QLabel *findLabel(int x, int y) const;
68+
5169
QWidget mImageWidget;
5270
QLabel mImageLabel;
5371
QHash<QString, QPixmap> mImagesCache;
54-
QList<QLabel *> mLabels; // Has ownership.
72+
QMultiHash<int, QLabel *> mLabels; // Has ownership.
5573
};
5674

5775
}

0 commit comments

Comments
 (0)