Skip to content

Commit 978ef8d

Browse files
authored
Merge pull request #34 from dwlsalmeida/master
Label shortcuts for quicker annotations and different text colors (black/white only)
2 parents ef64006 + 8a01ae2 commit 978ef8d

8 files changed

Lines changed: 54 additions & 4 deletions

File tree

src/label_widget.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ LabelWidget::LabelWidget(const LabelInfo &label, QWidget *parent , Qt::WindowFla
1313

1414
void LabelWidget::setNewLabel(const LabelInfo &label) {
1515
_label = label;
16-
setText(label.name);
16+
QString text = (label.shortcut) ?
17+
label.name + " (" + label.shortcut->key().toString() + ")" :
18+
label.name;
19+
20+
setText(text);
1721
setAlignment(Qt::AlignHCenter);
18-
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + invColor(_label.color).name() + "; font: bold 14px; }");
22+
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + readableColor(_label.color).name() + "; font: 14px; }");
1923
}
2024

2125
void LabelWidget::setSelected(bool s) {
@@ -36,4 +40,8 @@ void LabelWidget::paintEvent(QPaintEvent *event) {
3640
p.drawRect(rect());
3741
p.end();
3842
}
43+
}
44+
45+
QString LabelWidget::getName(){
46+
return this->_label.name;
3947
}

src/label_widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class LabelWidget : public QLabel {
1818

1919
void setNewLabel(const LabelInfo &label);
2020
void setSelected(bool s);
21+
QString getName();
2122

2223
public : // override
2324
void paintEvent(QPaintEvent *event) override;

src/labels.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LabelInfo::LabelInfo() {
1515
this->id_categorie = 0;
1616
this->color = QColor(0, 0, 0);
1717
item = NULL;
18+
shortcut = nullptr;
1819
}
1920

2021
LabelInfo::LabelInfo(QString name, QString categorie, int id, int id_categorie, QColor color) {
@@ -24,6 +25,7 @@ LabelInfo::LabelInfo(QString name, QString categorie, int id, int id_categorie,
2425
this->id_categorie = id_categorie;
2526
this->color = color;
2627
item = NULL;
28+
shortcut = nullptr;
2729
}
2830

2931
void LabelInfo::read(const QJsonObject &json) {

src/labels.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <QListWidgetItem>
55
#include <QJsonObject>
6+
#include <QtWidgets/QShortcut>
67

78
class LabelInfo {
89
public:
@@ -12,6 +13,7 @@ class LabelInfo {
1213
int id_categorie ;
1314
QColor color ;
1415
QListWidgetItem *item;
16+
QShortcut *shortcut ;
1517
LabelInfo();
1618
LabelInfo(QString name, QString categorie, int id, int id_categorie, QColor color);
1719
void read(const QJsonObject &json);

src/main_window.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,27 @@ void MainWindow::loadConfigLabels() {
118118
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
119119
list_label->addItem(item);
120120
list_label->setItemWidget(item, label_widget);
121-
labels[it.key()].item = item;
121+
122+
auto& ref = labels[it.key()];
123+
ref.item = item;
124+
125+
int id = list_label->row(item);
126+
const QString shortcut_key = (id < 9) ? QString("Ctrl+%1").arg(id + 1) :
127+
(id < 19) ? QString("Alt+%1").arg(id - 10 + 1) :
128+
(id < 29) ? QString("Ctrl+Alt+%1").arg(id - 20 + 1) :
129+
(id < 39) ? QString("Ctrl+Shift+Alt+%1").arg(id - 30 + 1) :
130+
QString();
131+
132+
if(id < 39) {
133+
QShortcut *shortcut = new QShortcut(QKeySequence(shortcut_key), this);
134+
ref.shortcut = shortcut;
135+
136+
QString text = label.name + " (" + ref.shortcut->key().toString() + ")";
137+
label_widget->setText(text);
138+
139+
connect(shortcut, &QShortcut::activated, this, [=]{onLabelShortcut(id);});
140+
}
141+
122142
}
123143
id_labels = getId2Label(labels);
124144
}
@@ -157,7 +177,7 @@ void MainWindow::changeLabel(QListWidgetItem* current, QListWidgetItem* previous
157177
label->setSelected(true);
158178

159179
QString str;
160-
QString key = label->text();
180+
QString key = label->getName();
161181
QTextStream sstr(&str);
162182
sstr <<"label=["<< key <<"] id=[" << labels[key].id << "] categorie=[" << labels[key].categorie << "] color=[" << labels[key].color.name() << "]" ;
163183
statusBar()->showMessage(str);
@@ -421,3 +441,7 @@ void MainWindow::swapView() {
421441
ic->update();
422442
}
423443

444+
void MainWindow::onLabelShortcut(int row) {
445+
if(list_label->isEnabled())
446+
list_label->setCurrentRow(row);
447+
}

src/main_window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public slots:
8181
void clearMask();
8282
void updateConnect(int index);
8383
void treeWidgetClicked();
84+
void onLabelShortcut(int row);
8485
};
8586

8687
#endif

src/utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result
4747
}
4848
}
4949

50+
QColor readableColor(const QColor & color)
51+
{
52+
int r, g, b;
53+
color.getRgb(&r, &g, &b);
54+
55+
if ((r*0.299 + g*0.587 + b*0.114) > 150)
56+
return QColor(0, 0, 0);
57+
58+
return QColor(255, 255, 255);
59+
60+
}
5061
QColor invColor(const QColor& color) {
5162
int h, s, v;
5263
color.getHsv(&h, &s, &v);

src/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ QImage idToColor(const QImage &image_id, const Id2Labels& id_label);
1313
void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result);
1414
inline bool operator<(const QColor & a, const QColor & b) { return a.rgb() < b.rgb(); }
1515
QColor invColor(const QColor & color);
16+
QColor readableColor(const QColor & color);
1617
QVector<QColor> colorMap(int size);
1718
cv::Mat convertMat32StoRGBC3(const cv::Mat &mat);
1819
QImage watershed(const QImage& qimage, const QImage & qmarkers_mask);

0 commit comments

Comments
 (0)