-
Notifications
You must be signed in to change notification settings - Fork 0
/
regionlistframe.cpp
193 lines (152 loc) · 5.09 KB
/
regionlistframe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "regionlistframe.h"
#include "ui_regionlistframe.h"
#include <QKeyEvent>
#include <QRect>
#include <QApplication>
#include <QDesktopWidget>
#include <QListWidgetItem>
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <QMenu>
#include "annotatorwnd.h"
RegionListFrame::RegionListFrame(QWidget *parent, AnnotatorWnd *annWnd) :
QFrame(parent),
ui(new Ui::RegionListFrame),
mAnnotatorWnd(annWnd)
{
ui->setupUi(this);
connect( ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(listCurrentItemChanged(QListWidgetItem*,QListWidgetItem*)));
connect( ui->butSave, SIGNAL(clicked()), this, SLOT(saveAsClicked()) );
connect( ui->listWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(listClickedSignal(QModelIndex)) );
connect( ui->butLabelRegion, SIGNAL(clicked()), this, SLOT(butLabelRegionClicked()) );
}
void RegionListFrame::keyReleaseEvent(QKeyEvent *evt)
{
int key = evt->key();
if ((key >= '0') && (key < '3'))
{
emit labelRegion( ui->listWidget->currentRow() , key - '0' );
}
}
void RegionListFrame::closeEvent(QCloseEvent *event)
{
emit widgetClosed();
event->accept();
}
void RegionListFrame::butLabelRegionClicked()
{
QMenu menu("Assign to", this);
QStringList sList;
mAnnotatorWnd->getLabelClassList( sList );
foreach( const QString &s, sList )
{
menu.addAction( s );
}
QAction *res = menu.exec( ui->butLabelRegion->mapToGlobal( QPoint(0,0) ) );
if (res == 0)
return;
unsigned int i=0;
foreach( const QString &s, sList )
{
if ( res->text() == s )
{
emit labelRegion( ui->listWidget->currentRow() , i );
break;
}
i++;
}
}
void RegionListFrame::listClickedSignal(QModelIndex mIdx)
{
static int lastCurrentItem = -1;
int newItem = ui->listWidget->currentRow();
if ((lastCurrentItem == -1) || (newItem != lastCurrentItem)) {
lastCurrentItem = newItem;
return;
}
Qt::CheckState newState;
switch( ui->listWidget->item(newItem)->checkState() )
{
case Qt::Unchecked:
newState = Qt::PartiallyChecked;
break;
case Qt::PartiallyChecked:
newState = Qt::Checked;
break;
case Qt::Checked:
newState = Qt::Unchecked;
break;
}
ui->listWidget->item(newItem)->setCheckState( newState );
lastCurrentItem = newItem;
//qDebug("Clicked");
}
void RegionListFrame::setRegionData( const std::vector< ShapeStatistics<> > &info )
{
mRegionDescriptions = info;
// update
ui->listWidget->clear();
for (unsigned int i=0; i < info.size(); i++)
{
QString name = QString("Region %1").arg(i+1);
QListWidgetItem *item = new QListWidgetItem( ui->listWidget );
item->setText( name );
item->setFlags( item->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsTristate );
Qt::CheckState state = Qt::Unchecked;
if (mRegionDescriptions[i].annotationLabel() == 0)
state = Qt::PartiallyChecked;
else if(mRegionDescriptions[i].annotationLabel() == 1)
state = Qt::Checked;
item->setCheckState( state );
ui->listWidget->addItem( item );
}
ui->listWidget->setCurrentRow(0);
}
/*** Saves a txt file: "<LABEL_ID>\t<NUM_PIXS>\t<ANNOTATION_LABEL>"
* where ANNOTATION_LABEL is 0 or 1 according to the checkbox
* NUM_PIXS is saved to provide a safe key if those labels are used later on
*/
void RegionListFrame::saveAsClicked()
{
QString fName = QFileDialog::getSaveFileName( this, "Save region annotation", ".", "*.txt" );
if (fName.isEmpty())
return;
QFile file(fName);
file.open( QIODevice::WriteOnly );
QString strFmt = "%1\t%2\t%3\n";
// add descr
file.write( ("%%" + strFmt.arg("LABEL_ID").arg("NUM_PIXS").arg("ANNOTATION_LABEL")).toLatin1() );
for (unsigned int i=0; i < mRegionDescriptions.size(); i++)
{
unsigned int checked = ui->listWidget->item( i )->checkState() == Qt::Checked;
file.write( strFmt.arg( mRegionDescriptions[i].labelIdx() ).arg( mRegionDescriptions[i].numVoxels() ).arg( checked ).toLatin1() );
}
file.close();
}
void RegionListFrame::listCurrentItemChanged( QListWidgetItem *, QListWidgetItem * )
{
if (ui->listWidget->selectedItems().count() == 0)
return;
const int curItem = ui->listWidget->currentRow();
// show descr
QString s = QString("<b>Region Label = %1</b><br>").arg( mRegionDescriptions.at(curItem).labelIdx() );
s += mRegionDescriptions.at(curItem).toString();
ui->textBrowser->setHtml( s );
emit currentRegionChanged(curItem);
}
void RegionListFrame::moveToBottomLeftCorner()
{
const QRect scrnRect = QApplication::desktop()->rect();
QRect wndRect;
wndRect.setX( scrnRect.width() - width() );
wndRect.setY( scrnRect.height() - height() );
wndRect.setWidth( this->width() );
wndRect.setHeight( this->height() );
this->setGeometry( wndRect );
}
RegionListFrame::~RegionListFrame()
{
delete ui;
}