ImageLabeler.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  1. /*
  2. * ImageLabeler.cpp
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "ImageLabeler.h"
  8. #include "functions.h"
  9. #include <QApplication>
  10. #include <QFrame>
  11. #include <QMenuBar>
  12. #include <QMenu>
  13. #include <QAction>
  14. #include <QBoxLayout>
  15. #include <QGridLayout>
  16. #include <QPixmap>
  17. #include <QLabel>
  18. #include <QScrollArea>
  19. #include <QPushButton>
  20. #include <QButtonGroup>
  21. #include <QListWidget>
  22. #include <QListWidgetItem>
  23. #include <QDesktopWidget>
  24. #include <QFileDialog>
  25. #include <QColorDialog>
  26. #include <QDir>
  27. #include <QMessageBox>
  28. #include <QListIterator>
  29. #include <QDomDocument>
  30. #include <QFile>
  31. #include <QKeyEvent>
  32. #include <QDebug>
  33. ImageLabeler::ImageLabeler(QWidget *aParent) :
  34. QMainWindow(aParent)
  35. {
  36. /*
  37. * Variables
  38. */
  39. list_images_ = new QStringList();
  40. main_label_ = -1;
  41. pure_data_ = 0;
  42. //label_ID_ = -1;
  43. /* options */
  44. auto_color_generation_ = 0;
  45. /* flags */
  46. interrupt_search_ = 0;
  47. unsaved_data_ = 0;
  48. /*
  49. * menu bar part begins
  50. */
  51. menu_bar_ = new QMenuBar(this);
  52. setMenuBar(menu_bar_);
  53. menu_file_ = new QMenu(menu_bar_);
  54. menu_file_->setTitle(tr("&File"));
  55. menu_view_ = new QMenu(menu_bar_);
  56. menu_view_->setTitle(tr("&View"));
  57. menu_edit_ = new QMenu(menu_bar_);
  58. menu_edit_->setTitle(tr("&Edit"));
  59. menu_help_ = new QMenu(menu_bar_);
  60. menu_help_->setTitle(tr("&Help"));
  61. /* menu file */
  62. action_open_image_ = new QAction(this);
  63. action_open_image_->setText(tr("&Load image"));
  64. action_open_images_ = new QAction(this);
  65. action_open_images_->setText(tr("&Load images(recursively)"));
  66. action_open_labeled_image_ = new QAction(this);
  67. action_open_labeled_image_->setText(tr("&Load labeled image"));
  68. action_load_legend_ = new QAction(this);
  69. action_load_legend_->setText(tr("&Load legend"));
  70. action_save_labels_ = new QAction(this);
  71. action_save_labels_->setText(tr("&Save all info"));
  72. action_save_labels_->setEnabled(false);
  73. action_save_segmented_ = new QAction(this);
  74. action_save_segmented_->setText(tr("Save segmented &picture"));
  75. action_save_segmented_->setEnabled(false);
  76. action_save_legend_ = new QAction(this);
  77. action_save_legend_->setText(tr("Save &legend"));
  78. action_save_legend_->setEnabled(false);
  79. action_quit_ = new QAction(this);
  80. action_quit_->setText(tr("&Quit"));
  81. /* menu view */
  82. action_view_normal_ = new QAction(this);
  83. action_view_normal_->setText(tr("&Normal"));
  84. action_view_normal_->setEnabled(false);
  85. action_view_segmented_ = new QAction(this);
  86. action_view_segmented_->setText(tr("&Segmented"));
  87. action_view_segmented_->setEnabled(false);
  88. /* menu edit */
  89. action_undo_ = new QAction(this);
  90. action_undo_->setText(tr("&Undo"));
  91. action_undo_->setEnabled(false);
  92. action_redo_ = new QAction(this);
  93. action_redo_->setText(tr("&Redo"));
  94. action_redo_->setEnabled(false);
  95. action_bound_box_tool_ = new QAction(this);
  96. action_bound_box_tool_->setText(tr("Bounding box tool"));
  97. action_bound_box_tool_->setEnabled(false);
  98. action_polygon_tool_ = new QAction(this);
  99. action_polygon_tool_->setText(tr("&Polygon tool"));
  100. action_polygon_tool_->setEnabled(false);
  101. action_tagging_tool_ = new QAction(this);
  102. action_tagging_tool_->setText(tr("&Tagging tool"));
  103. action_tagging_tool_->setEnabled(false);
  104. action_add_description_ = new QAction(this);
  105. action_add_description_->setText(tr("&Add image description"));
  106. action_add_description_->setEnabled(false);
  107. action_options_ = new QAction(this);
  108. action_options_->setText(tr("&Options"));
  109. /* menu help */
  110. action_help_content_ = new QAction(this);
  111. action_help_content_->setText(tr("&Help content"));
  112. action_help_content_->setEnabled(false);
  113. action_about_ = new QAction(this);
  114. action_about_->setText(tr("&About"));
  115. action_about_->setEnabled(false);
  116. /* ------------------ */
  117. menu_file_->addAction(action_open_image_);
  118. menu_file_->addAction(action_open_images_);
  119. menu_file_->addAction(action_open_labeled_image_);
  120. menu_file_->addAction(action_load_legend_);
  121. menu_file_->addAction(action_save_segmented_);
  122. menu_file_->addAction(action_save_legend_);
  123. menu_file_->addAction(action_save_labels_);
  124. menu_file_->addAction(action_quit_);
  125. menu_view_->addAction(action_view_normal_);
  126. menu_view_->addAction(action_view_segmented_);
  127. menu_edit_->addAction(action_undo_);
  128. menu_edit_->addAction(action_redo_);
  129. menu_edit_->addAction(action_bound_box_tool_);
  130. menu_edit_->addAction(action_polygon_tool_);
  131. menu_edit_->addAction(action_tagging_tool_);
  132. menu_edit_->addAction(action_add_description_);
  133. menu_edit_->addAction(action_options_);
  134. menu_help_->addAction(action_help_content_);
  135. menu_help_->addAction(action_about_);
  136. menu_bar_->addAction(menu_file_->menuAction());
  137. menu_bar_->addAction(menu_view_->menuAction());
  138. menu_bar_->addAction(menu_edit_->menuAction());
  139. menu_bar_->addAction(menu_help_->menuAction());
  140. /*
  141. * popup menu part begins
  142. */
  143. popup_area_list_ = new QMenu;
  144. action_delete_area_ = new QAction(this);
  145. action_delete_area_->setText(tr("&Delete area"));
  146. action_edit_area_ = new QAction(this);
  147. action_edit_area_->setText(tr("&Change area"));
  148. popup_area_list_->addAction(action_delete_area_);
  149. popup_area_list_->addAction(action_edit_area_);
  150. popup_label_list_ = new QMenu;
  151. action_set_color_ = new QAction(this);
  152. action_set_color_->setText(tr("Set &color"));
  153. action_toggle_priority_ = new QAction(this);
  154. action_toggle_priority_->setText(tr("Toggle &priority"));
  155. action_delete_label_ = new QAction(this);
  156. action_delete_label_->setText(tr("&Delete"));
  157. popup_label_list_->addAction(action_set_color_);
  158. popup_label_list_->addAction(action_toggle_priority_);
  159. popup_label_list_->addAction(action_delete_label_);
  160. popup_images_list_ = new QMenu;
  161. action_remove_image_ = new QAction(this);
  162. action_remove_image_->setText(tr("&remove image"));
  163. popup_images_list_->addAction(action_remove_image_);
  164. /*
  165. * widgets part begins
  166. */
  167. central_widget_ = new QWidget(this);
  168. setCentralWidget(central_widget_);
  169. frame_toolbox_ = new QFrame(central_widget_);
  170. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  171. frame_toolbox_->setLineWidth(0);
  172. frame_toolbox_->setMidLineWidth(0);
  173. frame_center_ = new QFrame(central_widget_);
  174. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  175. //frame_image_->setLineWidth(1);
  176. frame_image_ = new QScrollArea(frame_center_);
  177. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  178. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  179. frame_image_->setLineWidth(0);
  180. frame_image_->setMidLineWidth(0);
  181. frame_image_->setWidgetResizable(false);
  182. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  183. frame_image_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  184. frame_labelbox_ = new QFrame(central_widget_);
  185. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  186. frame_labelbox_->setLineWidth(0);
  187. frame_labelbox_->setMidLineWidth(0);
  188. image_ = new QPixmap(500, 500);
  189. image_->fill(QColor(Qt::white));
  190. //image_holder_ = new ImageHolder(frame_image_);
  191. image_holder_ = new ImageHolder;//(frame_image_);
  192. image_holder_->setPixmap(*image_);
  193. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  194. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  195. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  196. image_holder_->setFocusPolicy(Qt::StrongFocus);
  197. image_holder_->setFocus();
  198. image_holder_->setScaledContents(true);
  199. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  200. image_holder_->setLineWidth(0);
  201. frame_image_->setWidget(image_holder_);
  202. list_label_ = new QListWidget(central_widget_);
  203. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  204. list_areas_ = new QListWidget(central_widget_);
  205. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  206. list_images_widget_ = new QListWidget(central_widget_);
  207. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  208. // addLabel(0, false, "BACKGROUND");
  209. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  210. //QListWidgetItem *background = list_label_->item(0);
  211. //qDebug() << background;
  212. //background->setFlags(Qt::ItemIsSelectable);
  213. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  214. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  215. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  216. /* buttons */
  217. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  218. button_bound_box_tool_->setText(tr("bbox"));
  219. button_bound_box_tool_->setEnabled(false);
  220. button_bound_box_tool_->setCheckable(true);
  221. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  222. button_polygon_tool_->setText(tr("poly tool"));
  223. button_polygon_tool_->setEnabled(false);
  224. button_polygon_tool_->setCheckable(true);
  225. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  226. button_tagging_tool_->setText(tr("tagging"));
  227. button_tagging_tool_->setEnabled(false);
  228. button_tagging_tool_->setCheckable(true);
  229. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  230. button_clear_selection_tool_->setText(tr("clear selection"));
  231. button_clear_selection_tool_->setEnabled(false);
  232. button_generate_colors_ = new QPushButton(frame_toolbox_);
  233. button_generate_colors_->setText(tr("generate label colors"));
  234. button_generate_colors_->setEnabled(false);
  235. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  236. button_delete_all_labels_->setText(tr("delete all labels"));
  237. button_delete_all_labels_->setEnabled(false);
  238. group_tools_ = new QButtonGroup;
  239. group_tools_->addButton(button_bound_box_tool_);
  240. group_tools_->addButton(button_polygon_tool_);
  241. group_tools_->addButton(button_tagging_tool_);
  242. button_confirm_selection_ = new QPushButton(central_widget_);
  243. button_confirm_selection_->setText(tr("Confirm selection"));
  244. button_confirm_selection_->setEnabled(false);
  245. button_add_label_ = new QPushButton(frame_labelbox_);
  246. button_add_label_->setText(tr("Add label"));
  247. button_add_label_->setEnabled(false);
  248. button_remove_label_ = new QPushButton(frame_labelbox_);
  249. button_remove_label_->setText(tr("Remove label"));
  250. button_remove_label_->setEnabled(false);
  251. button_prev_image_ = new QPushButton(central_widget_);
  252. button_prev_image_->setText("←");
  253. button_next_image_ = new QPushButton(central_widget_);
  254. button_next_image_->setText("→");
  255. /*
  256. * layouts part begins
  257. */
  258. layout_main_ = new QHBoxLayout(central_widget_);
  259. layout_left_ = new QVBoxLayout();
  260. layout_toolbox_ = new QVBoxLayout();
  261. layout_center_ = new QVBoxLayout();
  262. layout_frame_image_ = new QVBoxLayout();
  263. layout_image_widget_ = new QGridLayout();
  264. layout_center_buttons_ = new QHBoxLayout();
  265. layout_right_ = new QVBoxLayout();
  266. layout_labelbox_ = new QVBoxLayout();
  267. layout_labelbox_buttons_ = new QHBoxLayout();
  268. layout_main_->addLayout(layout_left_);
  269. layout_main_->addLayout(layout_center_);
  270. layout_main_->addLayout(layout_right_);
  271. /* making the center part stretchable */
  272. layout_main_->setStretch(1, 1);
  273. /* left part */
  274. layout_left_->addWidget(frame_toolbox_);
  275. frame_toolbox_->setLayout(layout_toolbox_);
  276. layout_toolbox_->addWidget(label_toolbox_);
  277. layout_toolbox_->addWidget(button_bound_box_tool_);
  278. layout_toolbox_->addWidget(button_polygon_tool_);
  279. layout_toolbox_->addWidget(button_tagging_tool_);
  280. layout_toolbox_->addSpacing(10);
  281. layout_toolbox_->addWidget(button_clear_selection_tool_);
  282. layout_toolbox_->addWidget(button_delete_all_labels_);
  283. layout_toolbox_->addSpacing(10);
  284. layout_toolbox_->addWidget(button_generate_colors_);
  285. /* stretch is for making toolbox as small as it can be */
  286. layout_toolbox_->addSpacing(10);
  287. layout_left_->addWidget(label_list_images_);
  288. layout_left_->addWidget(list_images_widget_);
  289. list_images_widget_->setFixedWidth(200);
  290. layout_left_->addStretch(1);
  291. layout_left_->addWidget(button_confirm_selection_);
  292. /* central part */
  293. layout_center_->addWidget(frame_center_);
  294. frame_center_->setLayout(layout_frame_image_);
  295. //layout_frame_image_->addWidget(image_holder_);
  296. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  297. layout_frame_image_->addWidget(frame_image_);
  298. //frame_image_->setLayout(layout_image_widget_);
  299. // layout_image_widget_->setRowStretch(0, 1);
  300. // layout_image_widget_->setColumnStretch(0, 1);
  301. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  302. // layout_image_widget_->setRowStretch(2, 1);
  303. // layout_image_widget_->setColumnStretch(2, 1);
  304. //layout_frame_image_->addStretch(1);
  305. layout_frame_image_->addLayout(layout_center_buttons_);
  306. layout_center_buttons_->addWidget(button_prev_image_);
  307. layout_center_buttons_->addWidget(button_next_image_);
  308. /* right part */
  309. layout_right_->addWidget(frame_labelbox_);
  310. frame_labelbox_->setFixedWidth(200);
  311. frame_labelbox_->setLayout(layout_labelbox_);
  312. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  313. layout_labelbox_buttons_->addWidget(button_add_label_);
  314. layout_labelbox_buttons_->addWidget(button_remove_label_);
  315. layout_labelbox_->addWidget(list_label_);
  316. layout_labelbox_->addWidget(label_list_areas_);
  317. layout_labelbox_->addWidget(list_areas_);
  318. connect(
  319. action_quit_,
  320. SIGNAL(triggered()),
  321. this,
  322. SLOT(close())
  323. );
  324. connect(
  325. action_open_images_,
  326. SIGNAL(triggered()),
  327. this,
  328. SLOT(loadImages())
  329. );
  330. connect(
  331. action_open_image_,
  332. SIGNAL(triggered()),
  333. this,
  334. SLOT(loadImage())
  335. );
  336. connect(
  337. action_open_labeled_image_,
  338. SIGNAL(triggered()),
  339. this,
  340. SLOT(loadInfo())
  341. );
  342. connect(
  343. action_load_legend_,
  344. SIGNAL(triggered()),
  345. this,
  346. SLOT(loadLegendFromFile())
  347. );
  348. connect(
  349. action_save_legend_,
  350. SIGNAL(triggered()),
  351. this,
  352. SLOT(saveLegend())
  353. );
  354. connect(
  355. action_save_segmented_,
  356. SIGNAL(triggered()),
  357. this,
  358. SLOT(saveSegmentedPicture())
  359. );
  360. connect(
  361. action_save_labels_,
  362. SIGNAL(triggered()),
  363. this,
  364. SLOT(saveAllInfo())
  365. );
  366. connect(
  367. action_view_normal_,
  368. SIGNAL(triggered()),
  369. this,
  370. SLOT(viewNormal())
  371. );
  372. connect(
  373. action_view_segmented_,
  374. SIGNAL(triggered()),
  375. this,
  376. SLOT(viewSegmented())
  377. );
  378. connect(
  379. action_undo_,
  380. SIGNAL(triggered()),
  381. image_holder_,
  382. SLOT(undo())
  383. );
  384. connect(
  385. action_redo_,
  386. SIGNAL(triggered()),
  387. image_holder_,
  388. SLOT(redo())
  389. );
  390. connect(
  391. action_add_description_,
  392. SIGNAL(triggered()),
  393. &image_description_form_,
  394. SLOT(show())
  395. );
  396. connect(
  397. action_options_,
  398. SIGNAL(triggered()),
  399. &options_form_,
  400. SLOT(show())
  401. );
  402. connect(
  403. button_add_label_,
  404. SIGNAL(clicked()),
  405. this,
  406. SLOT(addLabel())
  407. );
  408. connect(
  409. button_remove_label_,
  410. SIGNAL(clicked()),
  411. this, SLOT(removeLabel())
  412. );
  413. connect(
  414. button_next_image_,
  415. SIGNAL(clicked()),
  416. this,
  417. SLOT(nextImage())
  418. );
  419. connect(
  420. button_prev_image_,
  421. SIGNAL(clicked()),
  422. this,
  423. SLOT(prevImage())
  424. );
  425. connect(
  426. button_bound_box_tool_,
  427. SIGNAL(toggled(bool)),
  428. this,
  429. SLOT(setBoundingBoxTool(bool))
  430. );
  431. connect(
  432. button_polygon_tool_,
  433. SIGNAL(toggled(bool)),
  434. this,
  435. SLOT(setPolygonTool(bool))
  436. );
  437. connect(
  438. button_clear_selection_tool_,
  439. SIGNAL(clicked()),
  440. this,
  441. SLOT(clearAllTool())
  442. );
  443. connect(
  444. button_generate_colors_,
  445. SIGNAL(clicked()),
  446. this,
  447. SLOT(generateColors())
  448. );
  449. connect(
  450. button_delete_all_labels_,
  451. SIGNAL(clicked()),
  452. this,
  453. SLOT(clearLabelList())
  454. );
  455. connect(
  456. button_confirm_selection_,
  457. SIGNAL(clicked()),
  458. this,
  459. SLOT(confirmSelection())
  460. );
  461. connect(
  462. list_label_,
  463. SIGNAL(itemChanged(QListWidgetItem *)),
  464. this,
  465. SLOT(editLabel(QListWidgetItem *))
  466. );
  467. connect(
  468. list_label_,
  469. SIGNAL(itemClicked(QListWidgetItem *)),
  470. this,
  471. SLOT(setLabelID(QListWidgetItem *))
  472. );
  473. connect(
  474. list_areas_,
  475. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  476. image_holder_,
  477. SLOT(focusOnArea(QListWidgetItem *))
  478. );
  479. connect(
  480. list_areas_,
  481. SIGNAL(customContextMenuRequested(const QPoint &)),
  482. this,
  483. SLOT(areaListPopupMenu(const QPoint &))
  484. );
  485. connect(
  486. list_areas_,
  487. SIGNAL(itemChanged(QListWidgetItem *)),
  488. this,
  489. SLOT(onAreaItemChange(QListWidgetItem *))
  490. );
  491. connect(
  492. list_label_,
  493. SIGNAL(customContextMenuRequested(const QPoint &)),
  494. this,
  495. SLOT(labelListPopupMenu(const QPoint &))
  496. );
  497. connect(
  498. list_images_widget_,
  499. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  500. this,
  501. SLOT(selectImage(QListWidgetItem *))
  502. );
  503. connect(
  504. list_images_widget_,
  505. SIGNAL(customContextMenuRequested(const QPoint &)),
  506. this,
  507. SLOT(imageListPopupMenu(const QPoint &))
  508. );
  509. connect(
  510. action_delete_area_,
  511. SIGNAL(triggered()),
  512. this,
  513. SLOT(deleteArea())
  514. );
  515. connect(
  516. action_edit_area_,
  517. SIGNAL(triggered()),
  518. this,
  519. SLOT(editArea())
  520. );
  521. connect(
  522. action_set_color_,
  523. SIGNAL(triggered()),
  524. this,
  525. SLOT(setLabelColor())
  526. );
  527. connect(
  528. action_toggle_priority_,
  529. SIGNAL(triggered()),
  530. this,
  531. SLOT(toggleLabelPriority())
  532. );
  533. connect(
  534. action_delete_label_,
  535. SIGNAL(triggered()),
  536. this,
  537. SLOT(removeLabel())
  538. );
  539. connect(
  540. action_remove_image_,
  541. SIGNAL(triggered()),
  542. this,
  543. SLOT(removeImage())
  544. );
  545. connect(
  546. image_holder_,
  547. SIGNAL(selectionStarted()),
  548. this,
  549. SLOT(onSelectionStarted())
  550. );
  551. connect(
  552. &image_description_form_,
  553. SIGNAL(descriptionSet(QString)),
  554. this,
  555. SLOT(setDescription(QString))
  556. );
  557. connect(
  558. image_holder_,
  559. SIGNAL(imageScaled()),
  560. this,
  561. SLOT(onImageScaled())
  562. );
  563. connect(
  564. image_holder_,
  565. SIGNAL(areaEdited()),
  566. this,
  567. SLOT(onAreaEdit())
  568. );
  569. connect(
  570. &options_form_,
  571. SIGNAL(optionsSet()),
  572. this,
  573. SLOT(onOptionsSet())
  574. );
  575. image_holder_->setBoundingBoxList(&list_bounding_box_);
  576. image_holder_->setPolygonList(&list_polygon_);
  577. image_holder_->setLabelColorList(&list_label_colors_);
  578. image_holder_->setMainLabelNum(&main_label_);
  579. image_holder_->setImage(image_);
  580. }
  581. ImageLabeler::~ImageLabeler()
  582. {
  583. delete action_quit_;
  584. delete action_open_labeled_image_;
  585. delete action_open_image_;
  586. delete action_open_images_;
  587. delete action_load_legend_;
  588. delete action_save_legend_;
  589. delete action_save_segmented_;
  590. delete action_save_labels_;
  591. delete action_view_normal_;
  592. delete action_view_segmented_;
  593. delete action_undo_;
  594. delete action_redo_;
  595. delete action_bound_box_tool_;
  596. delete action_polygon_tool_;
  597. delete action_tagging_tool_;
  598. delete action_add_description_;
  599. delete action_options_;
  600. delete action_about_;
  601. delete action_help_content_;
  602. delete menu_file_;
  603. delete menu_view_;
  604. delete menu_edit_;
  605. delete menu_help_;
  606. delete menu_bar_;
  607. delete action_delete_area_;
  608. delete action_edit_area_;
  609. delete popup_area_list_;
  610. delete action_toggle_priority_;
  611. delete action_set_color_;
  612. delete action_delete_label_;
  613. delete popup_label_list_;
  614. delete action_remove_image_;
  615. delete popup_images_list_;
  616. delete image_;
  617. delete image_holder_;
  618. delete button_add_label_;
  619. delete button_remove_label_;
  620. delete button_bound_box_tool_;
  621. delete button_polygon_tool_;
  622. delete button_tagging_tool_;
  623. delete button_clear_selection_tool_;
  624. delete button_generate_colors_;
  625. delete button_delete_all_labels_;
  626. delete button_next_image_;
  627. delete button_prev_image_;
  628. delete button_confirm_selection_;
  629. delete label_list_areas_;
  630. delete label_toolbox_;
  631. delete label_list_images_;
  632. delete list_areas_;
  633. delete list_label_;
  634. delete list_images_widget_;
  635. delete layout_toolbox_;
  636. delete layout_right_;
  637. delete layout_center_buttons_;
  638. delete layout_frame_image_;
  639. delete layout_center_;
  640. delete layout_labelbox_buttons_;
  641. delete layout_labelbox_;
  642. delete layout_left_;
  643. delete layout_main_;
  644. delete frame_labelbox_;
  645. delete frame_toolbox_;
  646. delete frame_image_;
  647. delete frame_center_;
  648. delete central_widget_;
  649. //delete current_image_;
  650. if (pure_data_) {
  651. delete[] *pure_data_;
  652. delete pure_data_;
  653. }
  654. delete list_images_;
  655. }
  656. void
  657. ImageLabeler::addImage(QString *anImage)
  658. {
  659. QListWidgetItem *newItem = new QListWidgetItem;
  660. QString itemText = QString("%1: %2").
  661. arg(list_images_widget_->count()).
  662. arg(getFilenameFromPath(anImage));
  663. newItem->setText(itemText);
  664. list_images_widget_->addItem(newItem);
  665. list_images_->append(*anImage);
  666. }
  667. void
  668. ImageLabeler::addLabel()
  669. {
  670. QListWidgetItem *newItem = new QListWidgetItem;
  671. QString label;
  672. int itemNum = list_label_->count();
  673. label.append(QString("%1: ").arg(itemNum));
  674. if (itemNum) {
  675. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  676. label.append("New label");
  677. }
  678. else {
  679. newItem->setFlags(
  680. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  681. Qt::ItemIsEnabled
  682. );
  683. label.append("BACKGROUND");
  684. }
  685. newItem->setText(label);
  686. QPixmap iconPix = QPixmap(20, 20);
  687. QColor color;
  688. if (0 != itemNum)
  689. color = Qt::white;
  690. else
  691. color = Qt::black;
  692. iconPix.fill(color);
  693. QIcon icon(iconPix);
  694. list_label_colors_.append(color.rgb());
  695. newItem->setIcon(icon);
  696. list_label_->addItem(newItem);
  697. list_label_->setItemSelected(newItem, true);
  698. label_ID_ = list_label_->count() - 1;
  699. unsaved_data_ = 1;
  700. }
  701. void
  702. ImageLabeler::addLabel(
  703. int aLabelID,
  704. bool isMain,
  705. QString aLabel
  706. )
  707. {
  708. QListWidgetItem *newItem = new QListWidgetItem;
  709. if (0 == aLabelID) {
  710. aLabel = QString("BACKGROUND");
  711. newItem->setFlags(
  712. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  713. Qt::ItemIsEnabled
  714. );
  715. }
  716. QString label;
  717. label.append(QString("%1: %2").
  718. arg(aLabelID).
  719. arg(aLabel)
  720. );
  721. /* TODO: check if there is another main label and make it common */
  722. if (isMain) {
  723. main_label_ = aLabelID;
  724. }
  725. QPixmap iconPix = QPixmap(20, 20);
  726. QColor color;
  727. if (0 != aLabelID)
  728. color = Qt::white;
  729. else
  730. color = Qt::black;
  731. iconPix.fill(color);
  732. QIcon icon(iconPix);
  733. list_label_colors_.append(color.rgb());
  734. newItem->setIcon(icon);
  735. newItem->setText(label);
  736. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  737. list_label_->addItem(newItem);
  738. list_label_->setItemSelected(newItem, true);
  739. unsaved_data_ = 1;
  740. }
  741. void
  742. ImageLabeler::editLabel(QListWidgetItem *anItem)
  743. {
  744. QString label = anItem->text();
  745. int itemRow = list_label_->row(anItem);
  746. QString prefix = QString("%1: ").arg(itemRow);
  747. if (-1 != label.indexOf(prefix)) {
  748. return;
  749. /* NOTREACHED */
  750. }
  751. label.prepend(QString("%1: ").arg(itemRow));
  752. if (main_label_ == itemRow) {
  753. label.append(" #main");
  754. }
  755. list_label_->blockSignals(true);
  756. anItem->setText(label);
  757. list_label_->blockSignals(false);
  758. unsaved_data_ = 1;
  759. }
  760. void
  761. ImageLabeler::removeLabel()
  762. {
  763. if (0 == list_label_->count()) {
  764. return;
  765. /* NOTREACHED */
  766. }
  767. //QListWidgetItem *current = list_label_->currentItem();
  768. /* we need to keep BACKGROUND category */
  769. if (label_ID_ < 1) {
  770. return;
  771. /* NOTREACHED */
  772. }
  773. if (list_label_->count() <= label_ID_ ||
  774. list_label_colors_.count() <= label_ID_) {
  775. return;
  776. /* NOTREACHED */
  777. }
  778. list_label_->takeItem(label_ID_);
  779. list_label_colors_.takeAt(label_ID_);
  780. unsaved_data_ = 1;
  781. }
  782. void
  783. ImageLabeler::setLabelID(
  784. QListWidgetItem *anItem
  785. )
  786. {
  787. Q_UNUSED(anItem)
  788. if (!list_label_->count()) {
  789. return;
  790. /* NOTREACHED */
  791. }
  792. label_ID_ = list_label_->row(anItem);
  793. }
  794. void
  795. ImageLabeler::addBBoxArea(
  796. int anID,
  797. BoundingBox aBBox,
  798. int itemID
  799. )
  800. {
  801. QListWidgetItem *newItem = new QListWidgetItem;
  802. if (-1 == itemID)
  803. itemID = list_areas_->count();
  804. QString label;
  805. label.append(QString("%1: ").arg(itemID));
  806. label.append(QString("BBox #%1; ").arg(anID));
  807. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  808. label.append(
  809. QString("data:%1;%2;%3;%4; ").
  810. arg(aBBox.rect.topLeft().x()).
  811. arg(aBBox.rect.topLeft().y()).
  812. arg(aBBox.rect.width()).
  813. arg(aBBox.rect.height())
  814. );
  815. newItem->setText(label);
  816. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  817. list_areas_->insertItem(itemID, newItem);
  818. list_areas_->setItemSelected(newItem, true);
  819. }
  820. void
  821. ImageLabeler::addPolyArea(
  822. int aPolyID,
  823. Polygon aPoly,
  824. int itemID
  825. )
  826. {
  827. QListWidgetItem *newItem = new QListWidgetItem;
  828. if (-1 == itemID)
  829. itemID = list_areas_->count();
  830. QString label;
  831. label.append(QString("%1: ").arg(itemID));
  832. label.append(QString("Poly #%1; ").arg(aPolyID));
  833. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  834. label.append("points:");
  835. for (int i = 0; i < aPoly.poly.count(); i++) {
  836. label.append(
  837. QString("%1;%2;").
  838. arg(aPoly.poly.point(i).x()).
  839. arg(aPoly.poly.point(i).y())
  840. );
  841. }
  842. newItem->setText(label);
  843. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  844. list_areas_->insertItem(itemID, newItem);
  845. list_areas_->setItemSelected(newItem, true);
  846. }
  847. void
  848. ImageLabeler::editArea()
  849. {
  850. bool oldState = list_areas_->blockSignals(true);
  851. if (!list_areas_->currentItem() || !list_areas_->count()) {
  852. return;
  853. /* NOTREACHED */
  854. }
  855. QListWidgetItem *current = list_areas_->currentItem();
  856. old_area_string_ = current->text();
  857. current->setFlags(current->flags() | Qt::ItemIsEditable);
  858. list_areas_->editItem(current);
  859. list_areas_->blockSignals(oldState);
  860. }
  861. void
  862. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  863. {
  864. list_areas_->blockSignals(true);
  865. QString areaString = anItem->text();
  866. int oldID = -1;
  867. if (-1 != areaString.indexOf("Poly")) {
  868. Polygon *poly = new Polygon;
  869. *poly = polyFromString(&areaString, &oldID);
  870. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  871. -1 < oldID) {
  872. list_polygon_.takeAt(oldID);
  873. list_polygon_.insert(oldID, poly);
  874. }
  875. else
  876. anItem->setText(old_area_string_);
  877. }
  878. else if (-1 != areaString.indexOf("BBox")) {
  879. BoundingBox *bbox = new BoundingBox;
  880. *bbox = BBoxFromString(&areaString, &oldID);
  881. if (-1 < bbox->label_ID_ && -1 < oldID) {
  882. list_bounding_box_.takeAt(oldID);
  883. list_bounding_box_.insert(oldID, bbox);
  884. }
  885. else
  886. anItem->setText(old_area_string_);
  887. }
  888. else {
  889. showWarning(tr("record format is corrupted, try again"));
  890. anItem->setText(old_area_string_);
  891. }
  892. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  893. list_areas_->blockSignals(false);
  894. image_holder_->update();
  895. unsaved_data_ = 1;
  896. }
  897. void
  898. ImageLabeler::onAreaEdit()
  899. {
  900. if (!list_areas_->count() ||
  901. -1 == image_holder_->focusedSelection()) {
  902. //showWarning(tr("You haven't added any label"));
  903. return;
  904. /* NOTREACHED */
  905. }
  906. int figureID = image_holder_->focusedSelection();
  907. Figure figure = image_holder_->focusedSelectionType();
  908. for (int i = 0; i < list_areas_->count(); i++) {
  909. QListWidgetItem *item = list_areas_->item(i);
  910. QString text = item->text();
  911. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  912. bool ok = 0;
  913. int num = getNumFromString(&text, "BBox #", ";", &ok);
  914. if (ok && num == figureID) {
  915. list_areas_->takeItem(i);
  916. addBBoxArea(num, *list_bounding_box_.at(num), i);
  917. }
  918. }
  919. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  920. bool ok = 0;
  921. int num = getNumFromString(&text, "Poly #", ";", &ok);
  922. if (ok && num == figureID) {
  923. list_areas_->takeItem(i);
  924. addPolyArea(num, *list_polygon_.at(num), i);
  925. }
  926. }
  927. }
  928. unsaved_data_ = 1;
  929. }
  930. void
  931. ImageLabeler::deleteArea()
  932. {
  933. if (!list_areas_->currentItem() || !list_areas_->count()) {
  934. return;
  935. /* NOTREACHED */
  936. }
  937. QString text = list_areas_->currentItem()->text();
  938. bool ok = 0;
  939. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  940. // if (!ok) {
  941. // return;
  942. // /* NOTREACHED */
  943. // }
  944. /*
  945. * 0 - bbox
  946. * 1 - poly
  947. */
  948. QString shape;
  949. if (-1 != text.indexOf("BBox"))
  950. shape = QString("BBox");
  951. else if (-1 != text.indexOf("Poly"))
  952. shape = QString("Poly");
  953. else {
  954. return;
  955. /* NOTREACHED */
  956. }
  957. int areaNum = getNumFromString(&text, "#", ";", &ok);
  958. if (!ok) {
  959. return;
  960. /* NOTREACHED */
  961. }
  962. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  963. /* changing all shapes(depends on current) which are next in the list */
  964. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  965. QListWidgetItem item = *(list_areas_->item(i));
  966. QString newText = item.text();
  967. if (-1 == newText.indexOf(shape))
  968. continue;
  969. int num = getNumFromString(&newText, "#", ";", &ok);
  970. num--;
  971. QString numString = QString("%1").arg(num);
  972. int numPos = newText.indexOf("#") + 1;
  973. newText.replace(numPos, numString.size(), numString);
  974. list_areas_->takeItem(i);
  975. list_areas_->insertItem(i, newText);
  976. }
  977. list_areas_->takeItem(currentItemRow);
  978. if (shape == "BBox")
  979. list_bounding_box_.removeAt(areaNum);
  980. else
  981. list_polygon_.removeAt(areaNum);
  982. image_holder_->update();
  983. unsaved_data_ = 1;
  984. }
  985. void
  986. ImageLabeler::toggleLabelPriority()
  987. {
  988. if (!list_label_->count()) {
  989. return;
  990. /* NOTREACHED */
  991. }
  992. QListWidgetItem *item = list_label_->currentItem();
  993. /* because we need to keep BACKGROUND category */
  994. if (0 == list_label_->row(item)) {
  995. return;
  996. /* NOTREACHED */
  997. }
  998. int itemRow = list_label_->row(item);
  999. QString text = item->text();
  1000. /* cleaning previous " #main" mark */
  1001. if (-1 != main_label_) {
  1002. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1003. QString lastMainText = lastMain->text();
  1004. int mainPos = lastMainText.indexOf(" #main");
  1005. lastMainText = lastMainText.mid(0, mainPos);
  1006. list_label_->blockSignals(true);
  1007. lastMain->setText(lastMainText);
  1008. list_label_->blockSignals(false);
  1009. }
  1010. if (main_label_ == itemRow) {
  1011. int mainPos = text.indexOf(" #main");
  1012. text = text.mid(0, mainPos);
  1013. main_label_ = -1;
  1014. }
  1015. else {
  1016. text.append(" #main");
  1017. main_label_ = list_label_->row(item);
  1018. }
  1019. list_label_->blockSignals(true);
  1020. item->setText(text);
  1021. list_label_->blockSignals(false);
  1022. image_holder_->update();
  1023. unsaved_data_ = 1;
  1024. }
  1025. void
  1026. ImageLabeler::getImagesFromDir(const QDir &dir)
  1027. {
  1028. /* avoiding freezing during recursive search for files */
  1029. QApplication::processEvents();
  1030. if (interrupt_search_) {
  1031. clearAll();
  1032. return;
  1033. }
  1034. QStringList filenameFilter;
  1035. filenameFilter <<
  1036. "*.jpeg" <<
  1037. "*.jpg" <<
  1038. "*.gif" <<
  1039. "*.png" <<
  1040. "*.bmp" <<
  1041. "*.tiff"
  1042. ;
  1043. QStringList listImages =
  1044. dir.entryList(filenameFilter, QDir::Files);
  1045. foreach (QString file, listImages) {
  1046. QString itemText = dir.absoluteFilePath(file);
  1047. addImage(&itemText);
  1048. //list_label_->setItemSelected(newItem, true);
  1049. //list_images_->append(dir.absoluteFilePath(file));
  1050. }
  1051. QStringList listDir = dir.entryList(QDir::Dirs);
  1052. foreach (QString subdir, listDir) {
  1053. if ("." == subdir || ".." == subdir)
  1054. continue;
  1055. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1056. }
  1057. }
  1058. void
  1059. ImageLabeler::nextImage()
  1060. {
  1061. if (list_images_->isEmpty()) {
  1062. return;
  1063. /* NOTREACHED */
  1064. }
  1065. if (askForUnsavedData()) {
  1066. return;
  1067. /* NOTREACHED */
  1068. }
  1069. if (list_images_widget_->count() - 1 == image_ID_) {
  1070. current_image_ = list_images_->at(0);
  1071. image_ID_ = 0;
  1072. }
  1073. else {
  1074. image_ID_ ++;
  1075. current_image_ = list_images_->at(image_ID_);
  1076. }
  1077. list_images_widget_->setCurrentRow(image_ID_);
  1078. if (current_image_.isEmpty()) {
  1079. return;
  1080. /* NOTREACHED */
  1081. }
  1082. QString winTitle;
  1083. winTitle.append("ImageLabeler - ");
  1084. winTitle.append(current_image_);
  1085. setWindowTitle(winTitle);
  1086. image_->load(current_image_);
  1087. image_holder_->resize(image_->size());
  1088. image_holder_->setPixmap(*image_);
  1089. list_bounding_box_.clear();
  1090. list_polygon_.clear();
  1091. list_areas_->clear();
  1092. image_holder_->clearAll();
  1093. //setWindowTitle(tr("ImageLabeler"));
  1094. }
  1095. void
  1096. ImageLabeler::prevImage()
  1097. {
  1098. if (!list_images_widget_->count()) {
  1099. return;
  1100. /* NOTREACHED */
  1101. }
  1102. if (askForUnsavedData()) {
  1103. return;
  1104. /* NOTREACHED */
  1105. }
  1106. if (!image_ID_) {
  1107. image_ID_ = list_images_widget_->count() - 1;
  1108. current_image_ = list_images_->at(image_ID_);
  1109. }
  1110. else {
  1111. image_ID_--;
  1112. current_image_ = list_images_->at(image_ID_);
  1113. }
  1114. list_images_widget_->setCurrentRow(image_ID_);
  1115. QString winTitle;
  1116. winTitle.append("ImageLabeler - ");
  1117. winTitle.append(current_image_);
  1118. setWindowTitle(winTitle);
  1119. image_->load(current_image_);
  1120. image_holder_->resize(image_->size());
  1121. image_holder_->setPixmap(*image_);
  1122. list_bounding_box_.clear();
  1123. list_polygon_.clear();
  1124. list_areas_->clear();
  1125. image_holder_->clearAll();
  1126. //setWindowTitle(tr("ImageLabeler"));
  1127. }
  1128. void
  1129. ImageLabeler::saveAllInfo()
  1130. {
  1131. if (!list_images_widget_->count()) {
  1132. showWarning("You have not opened any image yet");
  1133. return;
  1134. /* NOTREACHED */
  1135. }
  1136. /* ------------------------------------------------------------------------
  1137. * XML part
  1138. */
  1139. QDomDocument doc(tr("ImageLabeler"));
  1140. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1141. doc.appendChild(root);
  1142. QDomElement image = doc.createElement(tr("image"));
  1143. root.appendChild(image);
  1144. QDomText pathToImage = doc.createTextNode(current_image_);
  1145. image.appendChild(pathToImage);
  1146. if (!segmented_image_.isEmpty()) {
  1147. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1148. root.appendChild(segmentedImage);
  1149. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1150. segmentedImage.appendChild(pathToSegmented);
  1151. }
  1152. QDomElement description = doc.createElement(tr("description"));
  1153. root.appendChild(description);
  1154. QDomText descriptionText = doc.createTextNode(image_description_);
  1155. description.appendChild(descriptionText);
  1156. legendToXml(&doc, &root);
  1157. objectsToXml(&doc, &root);
  1158. setPureData();
  1159. QDomElement pureData = doc.createElement(tr("pure_data"));
  1160. QSize imageSize = image_->size();
  1161. QString pixelValues;
  1162. for (int i = 0; i < imageSize.height(); i++) {
  1163. for (int j = 0; j < imageSize.width(); j++) {
  1164. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1165. //pixelValues.append(QString(""));
  1166. }
  1167. pixelValues.append("\n");
  1168. }
  1169. QDomText pureDataText = doc.createTextNode(pixelValues);
  1170. pureData.appendChild(pureDataText);
  1171. root.appendChild(pureData);
  1172. QString xml = doc.toString();
  1173. /* ------------------------------------------------------------------------
  1174. * XML part ends
  1175. */
  1176. QFileDialog fileDialog(0, tr("Save all info"));
  1177. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1178. fileDialog.setDefaultSuffix("dat");
  1179. fileDialog.setFileMode(QFileDialog::AnyFile);
  1180. QString dir = getDirFromPath(&current_image_);
  1181. /* altering the name of a new file */
  1182. QString newFileName = alterFileName(current_image_, "_labeled");
  1183. fileDialog.selectFile(newFileName);
  1184. fileDialog.setDirectory(dir);
  1185. QString filename;
  1186. if (fileDialog.exec()) {
  1187. filename = fileDialog.selectedFiles().last();
  1188. }
  1189. else {
  1190. //showWarning(tr("Can not open file dialog"));
  1191. return;
  1192. /* NOTREACHED */
  1193. }
  1194. if (filename.isEmpty()) {
  1195. return;
  1196. /* NOTREACHED */
  1197. }
  1198. QFile file(filename);
  1199. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1200. showWarning(tr("Can not open file for writing"));
  1201. return;
  1202. /* NOTREACHED */
  1203. }
  1204. file.write(xml.toLocal8Bit());
  1205. file.close();
  1206. unsaved_data_ = 0;
  1207. }
  1208. void
  1209. ImageLabeler::saveSegmentedPicture()
  1210. {
  1211. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1212. return;
  1213. /* NOTREACHED */
  1214. }
  1215. setPureData();
  1216. // QString suffix = *current_image_;
  1217. // int dotPos = suffix.lastIndexOf(".");
  1218. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1219. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1220. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1221. fileDialog.setDefaultSuffix("png");
  1222. fileDialog.setFileMode(QFileDialog::AnyFile);
  1223. QString dir = getDirFromPath(&current_image_);
  1224. /* altering the name of a new file */
  1225. QString newFileName = alterFileName(current_image_, "_segmented");
  1226. fileDialog.selectFile(newFileName);
  1227. fileDialog.setDirectory(dir);
  1228. QString filename;
  1229. if (fileDialog.exec()) {
  1230. filename = fileDialog.selectedFiles().last();
  1231. }
  1232. else {
  1233. return;
  1234. /* NOTREACHED */
  1235. }
  1236. QSize imageSize = image_holder_->pixmap()->size();
  1237. QImage newImage(imageSize, QImage::Format_RGB32);
  1238. bool generateColorsFlag = auto_color_generation_;
  1239. bool flag = 0;
  1240. /* checking if all the colors are are different from white(default) */
  1241. if (!generateColorsFlag) {
  1242. for (int i = 1; i < list_label_->count(); i++) {
  1243. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1244. flag = 1;
  1245. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1246. QMessageBox msgBox;
  1247. msgBox.setText(tr("There are few labels with default white color."));
  1248. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1249. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1250. msgBox.setDefaultButton(QMessageBox::Yes);
  1251. msgBox.setIcon(QMessageBox::Question);
  1252. int ret = msgBox.exec();
  1253. if (QMessageBox::Yes == ret) {
  1254. generateColorsFlag = 1;
  1255. break;
  1256. }
  1257. else {
  1258. return;
  1259. /* NOTREACHED */
  1260. }
  1261. }
  1262. }
  1263. }
  1264. /* generating colors for labels */
  1265. if (list_label_colors_.count() < list_label_->count() ||
  1266. generateColorsFlag) {
  1267. generateColors();
  1268. }
  1269. for (int i = 0; i < imageSize.height(); i++)
  1270. for (int j = 0; j < imageSize.width(); j++) {
  1271. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1272. }
  1273. if (!newImage.save(filename, "png", 100)) {
  1274. showWarning(tr("An error occurred while saving the segmented image"));
  1275. return;
  1276. /* NOTREACHED */
  1277. }
  1278. segmented_image_ = filename;
  1279. action_view_segmented_->setEnabled(true);
  1280. }
  1281. void
  1282. ImageLabeler::saveLegend()
  1283. {
  1284. if (!list_label_->count()) {
  1285. showWarning("You have not added any label yet");
  1286. return;
  1287. /* NOTREACHED */
  1288. }
  1289. /* ------------------------------------------------------------------------
  1290. * XML part
  1291. */
  1292. QDomDocument doc(tr("ImageLabeler"));
  1293. QDomElement root = doc.createElement(tr("root"));
  1294. doc.appendChild(root);
  1295. legendToXml(&doc, &root);
  1296. QString xml = doc.toString();
  1297. /* ------------------------------------------------------------------------
  1298. * XML part ends
  1299. */
  1300. QFileDialog fileDialog(0, tr("Save legend"));
  1301. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1302. fileDialog.setDefaultSuffix("dat");
  1303. fileDialog.setFileMode(QFileDialog::AnyFile);
  1304. QString dir = getDirFromPath(&current_image_);
  1305. /* altering the name of a new file */
  1306. QString newFileName = alterFileName(current_image_, "_legend");
  1307. fileDialog.selectFile(newFileName);
  1308. fileDialog.setDirectory(dir);
  1309. QString filename;
  1310. if (fileDialog.exec()) {
  1311. filename = fileDialog.selectedFiles().last();
  1312. }
  1313. else {
  1314. //showWarning(tr("Can not open file dialog"));
  1315. return;
  1316. /* NOTREACHED */
  1317. }
  1318. if (filename.isEmpty()) {
  1319. return;
  1320. /* NOTREACHED */
  1321. }
  1322. QFile file(filename);
  1323. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1324. showWarning(tr("Can not open file for writing"));
  1325. return;
  1326. /* NOTREACHED */
  1327. }
  1328. file.write(xml.toLocal8Bit());
  1329. file.close();
  1330. }
  1331. void
  1332. ImageLabeler::loadInfo()
  1333. {
  1334. if (askForUnsavedData()) {
  1335. return;
  1336. /* NOTREACHED */
  1337. }
  1338. QFileDialog fileDialog(0, tr("Load file with info"));
  1339. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1340. fileDialog.setDefaultSuffix("dat");
  1341. fileDialog.setFileMode(QFileDialog::AnyFile);
  1342. QString filename;
  1343. if (fileDialog.exec()) {
  1344. filename = fileDialog.selectedFiles().last();
  1345. }
  1346. else {
  1347. //showWarning(tr("Can not open file dialog"));
  1348. return;
  1349. /* NOTREACHED */
  1350. }
  1351. QDomDocument doc("Image Labeler");
  1352. QFile file(filename);
  1353. if (!file.open(QIODevice::ReadOnly)) {
  1354. showWarning(tr("Can not open such file"));
  1355. return;
  1356. /* NOTREACHED */
  1357. }
  1358. QString errMsg;
  1359. if (!doc.setContent(&file, &errMsg)) {
  1360. showWarning(errMsg);
  1361. file.close();
  1362. return;
  1363. /* NOTREACHED */
  1364. }
  1365. file.close();
  1366. clearAll();
  1367. list_label_->clear();
  1368. enableTools();
  1369. /* getting all info */
  1370. QDomElement elements = doc.documentElement();
  1371. QDomNode rootNode = elements.firstChild();
  1372. QString string;
  1373. while(!rootNode.isNull()) {
  1374. QDomElement element = rootNode.toElement();
  1375. if(!element.isNull()) {
  1376. /* path to the image */
  1377. if (element.tagName() == "image") {
  1378. string = element.text();
  1379. if (string.isEmpty()) {
  1380. showWarning(
  1381. tr(
  1382. "The file with data doesn't contain path to the image"
  1383. )
  1384. );
  1385. return;
  1386. /* NOTREACHED */
  1387. }
  1388. if (!image_->load(string)) {
  1389. return;
  1390. /* NOTREACHED */
  1391. }
  1392. addImage(&string);
  1393. current_image_ = string;
  1394. image_ID_ = list_images_widget_->count() - 1;
  1395. list_images_widget_->setCurrentRow(image_ID_);
  1396. QString winTitle;
  1397. winTitle.append("ImageLabeler - ");
  1398. winTitle.append(current_image_);
  1399. setWindowTitle(winTitle);
  1400. image_holder_->resize(image_->size());
  1401. image_holder_->setPixmap(*image_);
  1402. }
  1403. /* path to the segmented image */
  1404. if (element.tagName() == "segmented") {
  1405. string = element.text();
  1406. if (string.isEmpty()) {
  1407. continue;
  1408. }
  1409. segmented_image_ = string;
  1410. action_view_segmented_->setEnabled(true);
  1411. }
  1412. /* image description */
  1413. else if (element.tagName() == "description" &&
  1414. !element.text().isEmpty()) {
  1415. image_description_ = element.text();
  1416. }
  1417. /* legend */
  1418. else if (element.tagName() == "legend") {
  1419. loadLegendFromNode(&element);
  1420. }
  1421. /* objects */
  1422. else if (element.tagName() == "objects") {
  1423. QDomNode subNode = element.firstChild();
  1424. QDomElement subElement;
  1425. while(!subNode.isNull()) {
  1426. subElement = subNode.toElement();
  1427. if (subElement.isNull() || subElement.text().isEmpty()) {
  1428. subNode = subNode.nextSibling();
  1429. continue;
  1430. }
  1431. string = subElement.attribute("id");
  1432. bool ok = 1;
  1433. int id = string.toInt(&ok, 10);
  1434. if (!ok) {
  1435. qDebug() <<
  1436. "loadInfo: "
  1437. "poly id format is corrupted";
  1438. subNode = subNode.nextSibling();
  1439. continue;
  1440. }
  1441. string = subElement.text();
  1442. if (subElement.tagName() == "bbox") {
  1443. setBBoxFromData(&string, &id);
  1444. }
  1445. if (subElement.tagName() == "poly") {
  1446. setPolyFromData(&string, &id);
  1447. }
  1448. subNode = subNode.nextSibling();
  1449. }
  1450. }
  1451. }
  1452. rootNode = rootNode.nextSibling();
  1453. }
  1454. }
  1455. void
  1456. ImageLabeler::loadImage()
  1457. {
  1458. if (askForUnsavedData()) {
  1459. return;
  1460. /* NOTREACHED */
  1461. }
  1462. QFileDialog fileDialog(0, tr("Load image"));
  1463. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1464. QStringList filters;
  1465. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1466. << "Any files (*)";
  1467. fileDialog.setNameFilters(filters);
  1468. QString filename;
  1469. if (fileDialog.exec()) {
  1470. filename = fileDialog.selectedFiles().last();
  1471. }
  1472. else {
  1473. //showWarning(tr("Could not open file dialog"));
  1474. return;
  1475. /* NOTREACHED */
  1476. }
  1477. if (filename.isEmpty()) {
  1478. return;
  1479. /* NOTREACHED */
  1480. }
  1481. clearAllTool();
  1482. QString winTitle;
  1483. winTitle.append("ImageLabeler - ");
  1484. winTitle.append(current_image_);
  1485. setWindowTitle(winTitle);
  1486. if (!image_->load(filename)) {
  1487. return;
  1488. /* NOTREACHED */
  1489. }
  1490. image_holder_->resize(image_->size());
  1491. image_holder_->setPixmap(*image_);
  1492. if (!list_images_widget_->count()) {
  1493. return;
  1494. /* NOTREACHED */
  1495. }
  1496. current_image_ = filename;
  1497. image_ID_ = list_images_widget_->count() - 1;
  1498. list_images_widget_->setCurrentRow(image_ID_);
  1499. addImage(&filename);
  1500. enableTools();
  1501. }
  1502. void
  1503. ImageLabeler::loadImages()
  1504. {
  1505. if (askForUnsavedData()) {
  1506. return;
  1507. /* NOTREACHED */
  1508. }
  1509. QFileDialog fileDialog(0, tr("Load images"));
  1510. fileDialog.setFileMode(QFileDialog::Directory);
  1511. QString dirName("");
  1512. if (fileDialog.exec()) {
  1513. /* TODO: make it possible to select multiple folders */
  1514. dirName = fileDialog.selectedFiles().last();
  1515. }
  1516. else {
  1517. //showWarning(tr("Could not open file dialog"));
  1518. return;
  1519. /* NOTREACHED */
  1520. }
  1521. clearAllTool();
  1522. QWidget *searchInProgress = new QWidget(0);
  1523. searchInProgress->setWindowTitle(tr("Loading images"));
  1524. QLabel *info = new QLabel(searchInProgress);
  1525. info->setText(
  1526. tr("Program is looking for all image files in your directory recursively."));
  1527. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1528. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1529. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1530. searchInProgress->setLayout(layout);
  1531. layout->addWidget(info);
  1532. layout->addWidget(cancel);
  1533. searchInProgress->adjustSize();
  1534. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1535. searchInProgress->show();
  1536. getImagesFromDir(QDir(dirName));
  1537. cancel->disconnect();
  1538. searchInProgress->hide();
  1539. delete info;
  1540. delete cancel;
  1541. delete layout;
  1542. delete searchInProgress;
  1543. if (interrupt_search_) {
  1544. interrupt_search_ = 0;
  1545. return;
  1546. /* NOTREACHED */
  1547. }
  1548. if (!list_images_widget_->count()) {
  1549. showWarning(tr("The folder you selected contains no images"));
  1550. return;
  1551. /* NOTREACHED */
  1552. }
  1553. if (!image_->load(list_images_->at(0))) {
  1554. return;
  1555. /* NOTREACHED */
  1556. }
  1557. current_image_ = list_images_->at(0);
  1558. image_ID_ = 0;
  1559. list_images_widget_->setCurrentRow(image_ID_);
  1560. QString winTitle;
  1561. winTitle.append("ImageLabeler - ");
  1562. winTitle.append(current_image_);
  1563. setWindowTitle(winTitle);
  1564. image_holder_->resize(image_->size());
  1565. image_holder_->setPixmap(*image_);
  1566. enableTools();
  1567. }
  1568. void
  1569. ImageLabeler::loadLegendFromFile()
  1570. {
  1571. QFileDialog fileDialog(0, tr("Load legend"));
  1572. fileDialog.setFileMode(QFileDialog::AnyFile);
  1573. QString filename;
  1574. if (fileDialog.exec()) {
  1575. filename = fileDialog.selectedFiles().last();
  1576. }
  1577. else {
  1578. //showWarning(tr("Could not open file dialog"));
  1579. return;
  1580. /* NOTREACHED */
  1581. }
  1582. QDomDocument doc("Image Labeler");
  1583. QFile file(filename);
  1584. if (!file.open(QIODevice::ReadOnly)) {
  1585. showWarning(tr("Can not open such file"));
  1586. return;
  1587. /* NOTREACHED */
  1588. }
  1589. QString errMsg;
  1590. if (!doc.setContent(&file, &errMsg)) {
  1591. showWarning(errMsg);
  1592. file.close();
  1593. return;
  1594. /* NOTREACHED */
  1595. }
  1596. file.close();
  1597. list_label_->clear();
  1598. /* getting legend */
  1599. QDomElement elements = doc.documentElement();
  1600. QDomNode rootNode = elements.firstChild();
  1601. QString string;
  1602. while(!rootNode.isNull()) {
  1603. QDomElement element = rootNode.toElement();
  1604. if(!element.isNull()) {
  1605. if (element.tagName() == "legend") {
  1606. loadLegendFromNode(&element);
  1607. }
  1608. }
  1609. rootNode = rootNode.nextSibling();
  1610. }
  1611. }
  1612. void
  1613. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  1614. {
  1615. if (!anElement) {
  1616. return;
  1617. /* NOTREACHED */
  1618. }
  1619. QDomNode subNode = anElement->firstChild();
  1620. QDomElement subElement;
  1621. QString string;
  1622. int id = -1;
  1623. bool isMain;
  1624. uint color = 0xff000000;
  1625. while(!subNode.isNull()) {
  1626. subElement = subNode.toElement();
  1627. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  1628. string = subElement.attribute("id");
  1629. bool ok = 0;
  1630. id = string.toInt(&ok, 10);
  1631. if (!ok) {
  1632. qDebug() <<
  1633. "loadLegendFromNode: "
  1634. "label id format is corrupted";
  1635. subNode = subNode.nextSibling();
  1636. continue;
  1637. }
  1638. string = subElement.attribute("isMain");
  1639. isMain = string.toInt(&ok, 2);
  1640. if (!ok) {
  1641. qDebug() <<
  1642. "loadLegendFromNode: "
  1643. "label isMain flag format is corrupted";
  1644. subNode = subNode.nextSibling();
  1645. continue;
  1646. }
  1647. string = subElement.attribute("color");
  1648. color = string.toUInt(&ok, 16);
  1649. if (!ok) {
  1650. qDebug() <<
  1651. "loadLegendFromNode: "
  1652. "label color format is corrupted";
  1653. subNode = subNode.nextSibling();
  1654. continue;
  1655. }
  1656. string = subElement.text();
  1657. addLabel(id, isMain, string);
  1658. setLabelColor(id, color);
  1659. }
  1660. subNode = subNode.nextSibling();
  1661. }
  1662. }
  1663. void
  1664. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1665. {
  1666. QDomElement legend = aDoc->createElement(tr("legend"));
  1667. aRoot->appendChild(legend);
  1668. /* storing all labels made by user */
  1669. int labelCount = list_label_->count();
  1670. for (int i = 0; i < labelCount; i++) {
  1671. QDomElement label = aDoc->createElement(tr("label"));
  1672. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  1673. label.setAttribute("id", i);
  1674. QString priority;
  1675. if (main_label_ == i)
  1676. priority.append("1");
  1677. else
  1678. priority.append("0");
  1679. label.setAttribute("isMain", priority);
  1680. QString labelText = list_label_->item(i)->text();
  1681. /* removing the number prefix of label */
  1682. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  1683. labelText = labelText.mid(3, labelText.size() - 3);
  1684. }
  1685. QDomText labelName = aDoc->createTextNode(labelText);
  1686. label.appendChild(labelName);
  1687. legend.appendChild(label);
  1688. }
  1689. /* in case we have no labels */
  1690. if (0 == labelCount) {
  1691. QDomElement label = aDoc->createElement(tr("label"));
  1692. label.setAttribute(tr("id"), -1);
  1693. legend.appendChild(label);
  1694. }
  1695. }
  1696. void
  1697. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  1698. {
  1699. QDomElement objects = aDoc->createElement(tr("objects"));
  1700. aRoot->appendChild(objects);
  1701. /* rects first */
  1702. for (int i = 0; i < list_bounding_box_.size(); i++) {
  1703. QDomElement rectData = aDoc->createElement(tr("bbox"));
  1704. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  1705. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  1706. QString rectDataString =
  1707. QString("%1;%2;%3;%4;").
  1708. arg(rect.x()).
  1709. arg(rect.y()).
  1710. arg(rect.width()).
  1711. arg(rect.height());
  1712. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  1713. rectData.appendChild(rectDataText);
  1714. objects.appendChild(rectData);
  1715. }
  1716. /* polys next */
  1717. for (int i = 0; i < list_polygon_.size(); i++) {
  1718. QDomElement polyData = aDoc->createElement(tr("poly"));
  1719. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  1720. QPolygon poly = list_polygon_.at(i)->poly;
  1721. QString polyDataString;
  1722. for (int j = 0; j < poly.count(); j++)
  1723. polyDataString.append(
  1724. QString("%1;%2;").
  1725. arg(poly.point(j).x()).
  1726. arg(poly.point(j).y())
  1727. );
  1728. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  1729. polyData.appendChild(polyDataText);
  1730. objects.appendChild(polyData);
  1731. }
  1732. }
  1733. void
  1734. ImageLabeler::generateColors()
  1735. {
  1736. int labelCount = list_label_->count();
  1737. if (!labelCount) {
  1738. showWarning(tr("you have not added any labels yet"));
  1739. return;
  1740. /* NOTREACHED */
  1741. }
  1742. int coeff = (0xff / labelCount) * 3;
  1743. //QList< uint > colors;
  1744. list_label_colors_.clear();
  1745. list_label_colors_.append(0);
  1746. uchar red = 0xff;
  1747. uchar green = 0xff;
  1748. uchar blue = 0xff;
  1749. uchar iterationColor = coeff;
  1750. uint color = 0xffffffff;
  1751. int j = 1;
  1752. for (int i = 1; i < labelCount; i++) {
  1753. if (6 == j) {
  1754. iterationColor += coeff;
  1755. j = 1;
  1756. }
  1757. if (5 == j) {
  1758. red = 0xff - iterationColor;
  1759. green = 0xff - iterationColor;
  1760. blue = 0xff;
  1761. }
  1762. else if (4 == j) {
  1763. red = 0xff - iterationColor;
  1764. green = 0xff;
  1765. blue = 0xff - iterationColor;
  1766. }
  1767. else if (3 == j) {
  1768. red = 0xff - iterationColor;
  1769. green = 0xff;
  1770. blue = 0xff;
  1771. }
  1772. else if (2 == j) {
  1773. red = 0xff;
  1774. green = 0xff - iterationColor;
  1775. blue = 0xff;
  1776. }
  1777. else if (1 == j){
  1778. red = 0xff;
  1779. green = 0xff;
  1780. blue = 0xff - iterationColor;
  1781. }
  1782. j++;
  1783. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  1784. int itemNo = list_label_colors_.count();
  1785. QPixmap iconPix = QPixmap(20, 20);
  1786. iconPix.fill(color);
  1787. QIcon icon(iconPix);
  1788. list_label_->item(itemNo)->setIcon(icon);
  1789. list_label_colors_.append(color);
  1790. }
  1791. }
  1792. void
  1793. ImageLabeler::setBBoxFromData(
  1794. QString *aBBoxData,
  1795. int *ID
  1796. )
  1797. {
  1798. BoundingBox *bbox = new BoundingBox;
  1799. *bbox = BBoxFromData(aBBoxData);
  1800. if (!bbox->rect.isValid() || !ID) {
  1801. return;
  1802. /* NOTREACHED */
  1803. }
  1804. bbox->label_ID_ = *ID;
  1805. list_bounding_box_.append(bbox);
  1806. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  1807. }
  1808. BoundingBox
  1809. ImageLabeler::BBoxFromData(
  1810. QString *aBBoxData
  1811. )
  1812. {
  1813. qDebug() << *aBBoxData;
  1814. BoundingBox bbox;
  1815. QString buffer;
  1816. bbox.rect.setRect(-1, -1, -1, -1);
  1817. int startPos = 0;
  1818. bool ok = 1;
  1819. for (int i = 0; i < aBBoxData->size(); i++) {
  1820. if (';' != aBBoxData->at(i))
  1821. continue;
  1822. buffer = aBBoxData->mid(startPos, i - startPos);
  1823. int bboxData = buffer.toInt(&ok, 10);
  1824. if (!ok) {
  1825. qDebug() <<
  1826. "BBoxFromData: "
  1827. "bbox format is corrupted";
  1828. break;
  1829. }
  1830. if (-1 == bbox.rect.x()) {
  1831. bbox.rect.setX(bboxData);
  1832. bbox.rect.setWidth(-1);
  1833. }
  1834. else if (-1 == bbox.rect.y()) {
  1835. bbox.rect.setY(bboxData);
  1836. bbox.rect.setHeight(-1);
  1837. }
  1838. else if (-1 == bbox.rect.width()) {
  1839. bbox.rect.setWidth(bboxData);
  1840. }
  1841. else if (-1 == bbox.rect.height()) {
  1842. bbox.rect.setHeight(bboxData);
  1843. }
  1844. startPos = i + 1;
  1845. }
  1846. if (!bbox.rect.isValid()) {
  1847. qDebug() <<
  1848. "BBoxFromData: "
  1849. "bbox format is corrupted";
  1850. bbox.rect.setRect(-1, -1, -1, -1);
  1851. }
  1852. else if (!ok) {
  1853. bbox.rect.setRect(-1, -1, -1, -1);
  1854. }
  1855. return bbox;
  1856. }
  1857. BoundingBox
  1858. ImageLabeler::BBoxFromString(
  1859. QString *aString,
  1860. int *oldID
  1861. )
  1862. {
  1863. BoundingBox bbox;
  1864. bbox.label_ID_ = -1;
  1865. *oldID = -1;
  1866. if (!aString) {
  1867. return bbox;
  1868. /* NOTREACHED */
  1869. }
  1870. if (-1 == aString->indexOf("BBox")) {
  1871. return bbox;
  1872. /* NOTREACHED */
  1873. }
  1874. /* getting bbox id in the list(it cannot be changed) */
  1875. bool ok = 0;
  1876. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  1877. if (!ok || bboxID <= -1) {
  1878. qDebug() <<
  1879. "BBoxFromString: bboxID is corrupted";
  1880. return bbox;
  1881. /* NOTREACHED */
  1882. }
  1883. /* getting new label id */
  1884. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1885. if (!ok || labelID <= -1) {
  1886. showWarning(
  1887. tr("new LabelID is wrong, area can not be changed")
  1888. );
  1889. return bbox;
  1890. /* NOTREACHED */
  1891. }
  1892. /* getting new points */
  1893. int pointsPos = aString->indexOf("data:") + 5;
  1894. int pointsLen = aString->size() - pointsPos;
  1895. if (pointsLen <= 0) {
  1896. showWarning(
  1897. tr("new data is wrong, area can not be changed")
  1898. );
  1899. return bbox;
  1900. /* NOTREACHED */
  1901. }
  1902. QString pointsData = aString->mid(pointsPos, pointsLen);
  1903. bbox = BBoxFromData(&pointsData);
  1904. bbox.label_ID_ = labelID;
  1905. *oldID = bboxID;
  1906. return bbox;
  1907. }
  1908. void
  1909. ImageLabeler::setPolyFromData(
  1910. QString *aPolyData,
  1911. int *ID
  1912. )
  1913. {
  1914. Polygon *poly = new Polygon;
  1915. *poly = polyFromData(aPolyData);
  1916. if (poly->poly.isEmpty() || !ID) {
  1917. return;
  1918. /* NOTREACHED */
  1919. }
  1920. poly->label_ID_ = *ID;
  1921. list_polygon_.append(poly);
  1922. addPolyArea(list_polygon_.count() - 1, *poly);
  1923. }
  1924. Polygon
  1925. ImageLabeler::polyFromData(
  1926. QString *aPolyData
  1927. )
  1928. {
  1929. Polygon poly;
  1930. poly.label_ID_ = -1;
  1931. QPoint point;
  1932. QString buffer;
  1933. int startPos = 0;
  1934. bool ok = 1;
  1935. bool evenFlag = 0;
  1936. for (int i = 0; i < aPolyData->size(); i++) {
  1937. if (';' != aPolyData->at(i))
  1938. continue;
  1939. buffer = aPolyData->mid(startPos, i - startPos);
  1940. int polyCoor = buffer.toInt(&ok, 10);
  1941. if (!ok) {
  1942. qDebug() <<
  1943. "polyFromData: "
  1944. "poly format is corrupted";
  1945. break;
  1946. }
  1947. if (!evenFlag) {
  1948. point.setX(polyCoor);
  1949. evenFlag = 1;
  1950. }
  1951. else {
  1952. point.setY(polyCoor);
  1953. poly.poly.append(point);
  1954. evenFlag = 0;
  1955. }
  1956. startPos = i + 1;
  1957. }
  1958. if (evenFlag) {
  1959. qDebug() <<
  1960. "polyFromData: "
  1961. "poly format is corrupted";
  1962. poly.poly.clear();
  1963. }
  1964. else if (!ok) {
  1965. poly.poly.clear();
  1966. }
  1967. return poly;
  1968. }
  1969. Polygon
  1970. ImageLabeler::polyFromString(
  1971. QString *aString,
  1972. int *oldID
  1973. )
  1974. {
  1975. Polygon poly;
  1976. poly.label_ID_ = -1;
  1977. *oldID = -1;
  1978. if (!aString) {
  1979. return poly;
  1980. /* NOTREACHED */
  1981. }
  1982. if (-1 == aString->indexOf("Poly")) {
  1983. return poly;
  1984. /* NOTREACHED */
  1985. }
  1986. /* getting poly id in the list(it cannot be changed) */
  1987. bool ok = 0;
  1988. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  1989. if (!ok || polyID <= -1) {
  1990. qDebug() <<
  1991. "polyFromString: poly ID is corrupted";
  1992. return poly;
  1993. /* NOTREACHED */
  1994. }
  1995. /* getting new label id */
  1996. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  1997. if (!ok || labelID <= -1) {
  1998. showWarning(
  1999. tr("new LabelID is wrong, area can not be changed")
  2000. );
  2001. return poly;
  2002. /* NOTREACHED */
  2003. }
  2004. /* getting new points */
  2005. int pointsPos = aString->indexOf("points:") + 7;
  2006. int pointsLen = aString->size() - pointsPos;
  2007. if (pointsLen <= 0) {
  2008. showWarning(
  2009. tr("new points data is wrong, area can not be changed")
  2010. );
  2011. return poly;
  2012. /* NOTREACHED */
  2013. }
  2014. QString pointsData = aString->mid(pointsPos, pointsLen);
  2015. poly = polyFromData(&pointsData);
  2016. poly.label_ID_ = labelID;
  2017. *oldID = polyID;
  2018. return poly;
  2019. }
  2020. void
  2021. ImageLabeler::showWarning(
  2022. const QString text
  2023. )
  2024. {
  2025. if (text.isEmpty()) {
  2026. return;
  2027. /* NOTREACHED */
  2028. }
  2029. QMessageBox msgBox;
  2030. msgBox.setText(text);
  2031. msgBox.setIcon(QMessageBox::Warning);
  2032. msgBox.exec();
  2033. }
  2034. bool
  2035. ImageLabeler::askForUnsavedData()
  2036. {
  2037. if ((!list_bounding_box_.isEmpty() ||
  2038. !list_polygon_.isEmpty()) &&
  2039. unsaved_data_)
  2040. {
  2041. QMessageBox msgBox;
  2042. msgBox.setText(tr("There is some unsaved data"));
  2043. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2044. msgBox.setStandardButtons(
  2045. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2046. msgBox.setDefaultButton(QMessageBox::Save);
  2047. msgBox.setIcon(QMessageBox::Question);
  2048. int ret = msgBox.exec();
  2049. if (QMessageBox::Save == ret)
  2050. saveAllInfo();
  2051. else if (QMessageBox::Cancel == ret)
  2052. return true;
  2053. }
  2054. return false;
  2055. }
  2056. void
  2057. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2058. {
  2059. if (aButtonPressed)
  2060. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2061. else {
  2062. image_holder_->setTool(ImageHolder::NoTool);
  2063. image_holder_->clearLast();
  2064. }
  2065. }
  2066. void
  2067. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2068. {
  2069. if (aButtonPressed) {
  2070. image_holder_->setTool(ImageHolder::PolygonTool);
  2071. image_holder_->setFocus();
  2072. }
  2073. else {
  2074. image_holder_->setTool(ImageHolder::NoTool);
  2075. image_holder_->clearLast();
  2076. }
  2077. }
  2078. void
  2079. ImageLabeler::onSelectionStarted()
  2080. {
  2081. button_confirm_selection_->setEnabled(true);
  2082. }
  2083. void
  2084. ImageLabeler::onImageScaled()
  2085. {
  2086. }
  2087. void
  2088. ImageLabeler::confirmSelection()
  2089. {
  2090. if (!list_label_->count()) {
  2091. showWarning(tr("You haven't added any label"));
  2092. return;
  2093. /* NOTREACHED */
  2094. }
  2095. image_holder_->confirmSelection();
  2096. if (label_ID_ < 0)
  2097. label_ID_ = 0;
  2098. ImageHolder::Tool tool = image_holder_->tool();
  2099. switch (tool) {
  2100. case ImageHolder::BoundingBoxTool:
  2101. list_bounding_box_.last()->label_ID_ = label_ID_;
  2102. addBBoxArea(
  2103. list_bounding_box_.count() - 1,
  2104. *(list_bounding_box_.last())
  2105. );
  2106. break;
  2107. case ImageHolder::PolygonTool:
  2108. list_polygon_.last()->label_ID_ = label_ID_;
  2109. addPolyArea(
  2110. list_polygon_.count() - 1,
  2111. *(list_polygon_.last())
  2112. );
  2113. break;
  2114. default:
  2115. break;
  2116. }
  2117. button_confirm_selection_->setEnabled(false);
  2118. unsaved_data_ = 1;
  2119. }
  2120. void
  2121. ImageLabeler::clearAll()
  2122. {
  2123. clearLabelList();
  2124. list_areas_->clear();
  2125. list_bounding_box_.clear();
  2126. list_polygon_.clear();
  2127. list_images_->clear();
  2128. list_images_widget_->clear();
  2129. main_label_ = -1;
  2130. image_holder_->clearAll();
  2131. segmented_image_.clear();
  2132. action_view_normal_->setEnabled(false);
  2133. action_view_segmented_->setEnabled(false);
  2134. }
  2135. void
  2136. ImageLabeler::clearAllTool()
  2137. {
  2138. list_areas_->clear();
  2139. list_bounding_box_.clear();
  2140. list_polygon_.clear();
  2141. main_label_ = -1;
  2142. image_holder_->clearAll();
  2143. }
  2144. void ImageLabeler::clearLabelList()
  2145. {
  2146. list_label_->clear();
  2147. addLabel(0, false, "BACKGROUND");
  2148. list_label_->item(0)->setFlags(
  2149. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2150. Qt::ItemIsEnabled
  2151. );
  2152. }
  2153. void
  2154. ImageLabeler::enableTools()
  2155. {
  2156. action_save_labels_->setEnabled(true);
  2157. action_save_segmented_->setEnabled(true);
  2158. action_save_legend_->setEnabled(true);
  2159. action_bound_box_tool_->setEnabled(true);
  2160. action_polygon_tool_->setEnabled(true);
  2161. action_add_description_->setEnabled(true);
  2162. action_undo_->setEnabled(true);
  2163. action_redo_->setEnabled(true);
  2164. button_bound_box_tool_->setEnabled(true);
  2165. button_polygon_tool_->setEnabled(true);
  2166. button_add_label_->setEnabled(true);
  2167. button_remove_label_->setEnabled(true);
  2168. button_prev_image_->setEnabled(true);
  2169. button_next_image_->setEnabled(true);
  2170. button_clear_selection_tool_->setEnabled(true);
  2171. button_generate_colors_->setEnabled(true);
  2172. button_delete_all_labels_->setEnabled(true);
  2173. }
  2174. void
  2175. ImageLabeler::disableTools()
  2176. {
  2177. action_save_labels_->setEnabled(false);
  2178. action_save_segmented_->setEnabled(false);
  2179. action_save_legend_->setEnabled(false);
  2180. action_bound_box_tool_->setEnabled(false);
  2181. action_polygon_tool_->setEnabled(false);
  2182. action_add_description_->setEnabled(false);
  2183. action_undo_->setEnabled(false);
  2184. action_redo_->setEnabled(false);
  2185. button_bound_box_tool_->setEnabled(false);
  2186. button_polygon_tool_->setEnabled(false);
  2187. button_add_label_->setEnabled(false);
  2188. button_remove_label_->setEnabled(false);
  2189. button_prev_image_->setEnabled(false);
  2190. button_next_image_->setEnabled(false);
  2191. button_clear_selection_tool_->setEnabled(false);
  2192. button_generate_colors_->setEnabled(false);
  2193. button_delete_all_labels_->setEnabled(false);
  2194. }
  2195. void
  2196. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2197. {
  2198. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2199. QModelIndex index = list_areas_->indexAt(aPos);
  2200. if (-1 == index.row()) {
  2201. return;
  2202. /* NOTREACHED */
  2203. }
  2204. list_areas_->item(index.row())->setSelected(true);
  2205. popup_area_list_->exec(globalPos);
  2206. }
  2207. void
  2208. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2209. {
  2210. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2211. QModelIndex index = list_label_->indexAt(aPos);
  2212. if (-1 == index.row() || !index.row()) {
  2213. return;
  2214. /* NOTREACHED */
  2215. }
  2216. list_label_->item(index.row())->setSelected(true);
  2217. popup_label_list_->exec(globalPos);
  2218. }
  2219. void
  2220. ImageLabeler::setDescription(QString aDescription)
  2221. {
  2222. image_description_ = aDescription;
  2223. setWindowTitle(image_description_);
  2224. }
  2225. void
  2226. ImageLabeler::setPureData()
  2227. {
  2228. /* initializing array */
  2229. if (pure_data_) {
  2230. delete[] *pure_data_;
  2231. delete pure_data_;
  2232. }
  2233. QSize imageSize = image_->size();
  2234. pure_data_ = new int *[imageSize.height()];
  2235. if (!pure_data_) {
  2236. return;
  2237. /* NOTREACHED */
  2238. }
  2239. for (int i = 0; i < imageSize.height(); i++) {
  2240. pure_data_[i] = new int[imageSize.width()];
  2241. if (!pure_data_[i]) {
  2242. return;
  2243. /* NOTREACHED */
  2244. }
  2245. }
  2246. int bboxCnt = list_bounding_box_.count();
  2247. int polyCnt = list_polygon_.count();
  2248. for (int i = 0; i < imageSize.height(); i++)
  2249. for (int j = 0; j < imageSize.width(); j++) {
  2250. pure_data_[i][j] = 0;
  2251. /* bboxes first */
  2252. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2253. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2254. if (bbox->rect.contains(j, i)) {
  2255. pure_data_[i][j] = bbox->label_ID_;
  2256. }
  2257. }
  2258. /* polys next */
  2259. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2260. Polygon *poly = list_polygon_.at(cnt);
  2261. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2262. pure_data_[i][j] = poly->label_ID_;
  2263. }
  2264. }
  2265. }
  2266. }
  2267. void
  2268. ImageLabeler::setLabelColor()
  2269. {
  2270. if (list_label_colors_.count() < list_label_->count()) {
  2271. generateColors();
  2272. }
  2273. QListWidgetItem *current = list_label_->currentItem();
  2274. int labelID = list_label_->row(current);
  2275. QColor defaultColor;
  2276. defaultColor.setRgb(list_label_colors_.at(labelID));
  2277. QColor newColor = QColorDialog::getColor(
  2278. defaultColor,
  2279. list_label_
  2280. );
  2281. if (!newColor.isValid()) {
  2282. return;
  2283. /* NOTREACHED */
  2284. }
  2285. list_label_colors_.takeAt(labelID);
  2286. list_label_colors_.insert(labelID, newColor.rgb());
  2287. QPixmap iconPix = QPixmap(20, 20);
  2288. iconPix.fill(newColor);
  2289. QIcon icon(iconPix);
  2290. current->setIcon(icon);
  2291. }
  2292. void
  2293. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2294. {
  2295. if (anID < 0) {
  2296. return;
  2297. /* NOTREACHED */
  2298. }
  2299. if (list_label_colors_.count() < list_label_->count()) {
  2300. generateColors();
  2301. }
  2302. QListWidgetItem *item = list_label_->item(anID);
  2303. list_label_colors_.takeAt(anID);
  2304. list_label_colors_.insert(anID, aColor.rgb());
  2305. QPixmap iconPix = QPixmap(20, 20);
  2306. iconPix.fill(aColor);
  2307. QIcon icon(iconPix);
  2308. item->setIcon(icon);
  2309. }
  2310. void
  2311. ImageLabeler::onOptionsSet()
  2312. {
  2313. auto_color_generation_ = options_form_.autoColorGeneration();
  2314. }
  2315. void
  2316. ImageLabeler::viewNormal()
  2317. {
  2318. if (current_image_.isEmpty()) {
  2319. return;
  2320. /* NOTREACHED */
  2321. }
  2322. image_->load(current_image_);
  2323. image_holder_->setPixmap(*image_);
  2324. action_view_segmented_->setEnabled(true);
  2325. action_view_normal_->setEnabled(false);
  2326. }
  2327. void
  2328. ImageLabeler::viewSegmented()
  2329. {
  2330. if (segmented_image_.isEmpty()) {
  2331. return;
  2332. /* NOTREACHED */
  2333. }
  2334. image_->load(segmented_image_);
  2335. image_holder_->setPixmap(*image_);
  2336. action_view_segmented_->setEnabled(false);
  2337. action_view_normal_->setEnabled(true);
  2338. }
  2339. void
  2340. ImageLabeler::interruptSearch()
  2341. {
  2342. interrupt_search_ = 1;
  2343. }
  2344. void
  2345. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2346. {
  2347. if (!anItem || list_images_widget_->row(anItem) < 0) {
  2348. return;
  2349. /* NOTREACHED */
  2350. }
  2351. image_ID_ = list_images_widget_->row(anItem);
  2352. image_->load(list_images_->at(image_ID_));
  2353. image_holder_->setPixmap(*image_);
  2354. image_holder_->resize(image_->size());
  2355. }
  2356. void
  2357. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2358. {
  2359. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2360. QModelIndex index = list_images_widget_->indexAt(aPos);
  2361. if (-1 == index.row()) {
  2362. return;
  2363. /* NOTREACHED */
  2364. }
  2365. list_images_widget_->item(index.row())->setSelected(true);
  2366. popup_images_list_->exec(globalPos);
  2367. }
  2368. void
  2369. ImageLabeler::removeImage()
  2370. {
  2371. int num = list_images_widget_->currentRow();
  2372. list_images_widget_->takeItem(num);
  2373. list_images_->takeAt(num);
  2374. for (int i = num ; i < list_images_widget_->count(); i++) {
  2375. QString newStr = getFilenameFromPath(&(list_images_->at(i)));
  2376. newStr.prepend(QString("%1: ").arg(i));
  2377. list_images_widget_->item(i)->setText(newStr);
  2378. }
  2379. }
  2380. void
  2381. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2382. {
  2383. QWidget::resizeEvent(anEvent);
  2384. }
  2385. void
  2386. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2387. {
  2388. QWidget::mousePressEvent(anEvent);
  2389. }
  2390. void
  2391. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2392. {
  2393. keyboard_modifier_ = anEvent->modifiers();
  2394. if (Qt::Key_Left == anEvent->key() &&
  2395. Qt::ControlModifier == keyboard_modifier_) {
  2396. prevImage();
  2397. }
  2398. if (Qt::Key_Right == anEvent->key() &&
  2399. Qt::ControlModifier == keyboard_modifier_) {
  2400. nextImage();
  2401. }
  2402. if ((Qt::Key_Enter == anEvent->key() ||
  2403. Qt::Key_Return == anEvent->key()) &&
  2404. // Qt::NoModifier == anEvent->modifiers() &&
  2405. ImageHolder::NewSelection == image_holder_->state()) {
  2406. confirmSelection();
  2407. }
  2408. if ((Qt::Key_Enter == anEvent->key() ||
  2409. Qt::Key_Return == anEvent->key()) &&
  2410. // Qt::NoModifier == anEvent->modifiers() &&
  2411. -1 != image_holder_->focusedSelection()) {
  2412. image_holder_->clearFocusOnArea();
  2413. image_holder_->clearEdition();
  2414. image_holder_->update();
  2415. }
  2416. if (Qt::Key_Escape == anEvent->key()) {
  2417. image_holder_->clearLast();
  2418. image_holder_->clearFocusOnArea();
  2419. image_holder_->clearEdition();
  2420. image_holder_->update();
  2421. }
  2422. if (Qt::Key_Z == anEvent->key() &&
  2423. Qt::ControlModifier == anEvent->modifiers()) {
  2424. image_holder_->undo();
  2425. }
  2426. if (Qt::Key_Y == anEvent->key() &&
  2427. Qt::ControlModifier == anEvent->modifiers()) {
  2428. image_holder_->redo();
  2429. }
  2430. QWidget::keyPressEvent(anEvent);
  2431. }
  2432. void
  2433. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  2434. {
  2435. keyboard_modifier_ = anEvent->modifiers();
  2436. }
  2437. void
  2438. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2439. {
  2440. /* zoomin */
  2441. if (0 < anEvent->delta() &&
  2442. Qt::ControlModifier == keyboard_modifier_) {
  2443. image_holder_->scaleImage(ZoomIn, 1.1);
  2444. }
  2445. /* zoomout */
  2446. else if (anEvent->delta() < 0 &&
  2447. Qt::ControlModifier == keyboard_modifier_) {
  2448. image_holder_->scaleImage(ZoomOut, 1.1);
  2449. }
  2450. }
  2451. /*
  2452. *
  2453. */