ImageLabeler.cpp 63 KB

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