Skip to content

Commit

Permalink
Merge pull request #34 from dwlsalmeida/master
Browse files Browse the repository at this point in the history
Label shortcuts for quicker annotations and different text colors (black/white only)
  • Loading branch information
abreheret authored Apr 1, 2019
2 parents ef64006 + 8a01ae2 commit 978ef8d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/label_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ LabelWidget::LabelWidget(const LabelInfo &label, QWidget *parent , Qt::WindowFla

void LabelWidget::setNewLabel(const LabelInfo &label) {
_label = label;
setText(label.name);
QString text = (label.shortcut) ?
label.name + " (" + label.shortcut->key().toString() + ")" :
label.name;

setText(text);
setAlignment(Qt::AlignHCenter);
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + invColor(_label.color).name() + "; font: bold 14px; }");
setStyleSheet("QLabel { background-color : " + label.color.name() + "; color : " + readableColor(_label.color).name() + "; font: 14px; }");
}

void LabelWidget::setSelected(bool s) {
Expand All @@ -36,4 +40,8 @@ void LabelWidget::paintEvent(QPaintEvent *event) {
p.drawRect(rect());
p.end();
}
}

QString LabelWidget::getName(){
return this->_label.name;
}
1 change: 1 addition & 0 deletions src/label_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LabelWidget : public QLabel {

void setNewLabel(const LabelInfo &label);
void setSelected(bool s);
QString getName();

public : // override
void paintEvent(QPaintEvent *event) override;
Expand Down
2 changes: 2 additions & 0 deletions src/labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LabelInfo::LabelInfo() {
this->id_categorie = 0;
this->color = QColor(0, 0, 0);
item = NULL;
shortcut = nullptr;
}

LabelInfo::LabelInfo(QString name, QString categorie, int id, int id_categorie, QColor color) {
Expand All @@ -24,6 +25,7 @@ LabelInfo::LabelInfo(QString name, QString categorie, int id, int id_categorie,
this->id_categorie = id_categorie;
this->color = color;
item = NULL;
shortcut = nullptr;
}

void LabelInfo::read(const QJsonObject &json) {
Expand Down
2 changes: 2 additions & 0 deletions src/labels.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QListWidgetItem>
#include <QJsonObject>
#include <QtWidgets/QShortcut>

class LabelInfo {
public:
Expand All @@ -12,6 +13,7 @@ class LabelInfo {
int id_categorie ;
QColor color ;
QListWidgetItem *item;
QShortcut *shortcut ;
LabelInfo();
LabelInfo(QString name, QString categorie, int id, int id_categorie, QColor color);
void read(const QJsonObject &json);
Expand Down
28 changes: 26 additions & 2 deletions src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,27 @@ void MainWindow::loadConfigLabels() {
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
list_label->addItem(item);
list_label->setItemWidget(item, label_widget);
labels[it.key()].item = item;

auto& ref = labels[it.key()];
ref.item = item;

int id = list_label->row(item);
const QString shortcut_key = (id < 9) ? QString("Ctrl+%1").arg(id + 1) :
(id < 19) ? QString("Alt+%1").arg(id - 10 + 1) :
(id < 29) ? QString("Ctrl+Alt+%1").arg(id - 20 + 1) :
(id < 39) ? QString("Ctrl+Shift+Alt+%1").arg(id - 30 + 1) :
QString();

if(id < 39) {
QShortcut *shortcut = new QShortcut(QKeySequence(shortcut_key), this);
ref.shortcut = shortcut;

QString text = label.name + " (" + ref.shortcut->key().toString() + ")";
label_widget->setText(text);

connect(shortcut, &QShortcut::activated, this, [=]{onLabelShortcut(id);});
}

}
id_labels = getId2Label(labels);
}
Expand Down Expand Up @@ -157,7 +177,7 @@ void MainWindow::changeLabel(QListWidgetItem* current, QListWidgetItem* previous
label->setSelected(true);

QString str;
QString key = label->text();
QString key = label->getName();
QTextStream sstr(&str);
sstr <<"label=["<< key <<"] id=[" << labels[key].id << "] categorie=[" << labels[key].categorie << "] color=[" << labels[key].color.name() << "]" ;
statusBar()->showMessage(str);
Expand Down Expand Up @@ -421,3 +441,7 @@ void MainWindow::swapView() {
ic->update();
}

void MainWindow::onLabelShortcut(int row) {
if(list_label->isEnabled())
list_label->setCurrentRow(row);
}
1 change: 1 addition & 0 deletions src/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public slots:
void clearMask();
void updateConnect(int index);
void treeWidgetClicked();
void onLabelShortcut(int row);
};

#endif
11 changes: 11 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result
}
}

QColor readableColor(const QColor & color)
{
int r, g, b;
color.getRgb(&r, &g, &b);

if ((r*0.299 + g*0.587 + b*0.114) > 150)
return QColor(0, 0, 0);

return QColor(255, 255, 255);

}
QColor invColor(const QColor& color) {
int h, s, v;
color.getHsv(&h, &s, &v);
Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ QImage idToColor(const QImage &image_id, const Id2Labels& id_label);
void idToColor(const QImage &image_id, const Id2Labels& id_label, QImage *result);
inline bool operator<(const QColor & a, const QColor & b) { return a.rgb() < b.rgb(); }
QColor invColor(const QColor & color);
QColor readableColor(const QColor & color);
QVector<QColor> colorMap(int size);
cv::Mat convertMat32StoRGBC3(const cv::Mat &mat);
QImage watershed(const QImage& qimage, const QImage & qmarkers_mask);
Expand Down

0 comments on commit 978ef8d

Please sign in to comment.